Skip to main content
summaryrefslogtreecommitdiffstats
blob: 67a1f0811e827d1a00e7e36f859ce2deda751d7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial implementation
 ******************************************************************************/
package org.eclipse.team.internal.ui.target;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.core.target.Site;
import org.eclipse.team.internal.core.target.TargetManager;
import org.eclipse.team.internal.core.target.TargetProvider;
import org.eclipse.team.internal.ui.DetailsDialog;
import org.eclipse.team.internal.ui.Policy;

/**
 * Discards a remembered Site from Target Management. Before a Site can be
 * discarded all resources using this site must be deconfigured. This action will
 * prompt the user if a Site cannot be discarded because of existing connections.
 */
public class DiscardSiteAction extends TargetAction {

	private class AlreadyMappedDialog extends DetailsDialog {
		private IProject[] projects;
		private org.eclipse.swt.widgets.List detailsList;
		private Button unmap;
		
		public AlreadyMappedDialog(Shell shell, IProject[] projects) {
			super(shell, Policy.bind("SiteExplorerView.unmapDialogTitle")); //$NON-NLS-1$
			setImageKey(DLG_IMG_WARNING);
			this.projects = projects;
		}
			
		protected Composite createDropDownDialogArea(Composite parent) {
			// create a composite with standard margins and spacing
			Composite composite = new Composite(parent, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
			layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
			layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
			layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
			composite.setLayout(layout);
			composite.setLayoutData(new GridData(GridData.FILL_BOTH));
			composite.setFont(parent.getFont());
			
			detailsList = new org.eclipse.swt.widgets.List(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);	 
			GridData data = new GridData ();		
			data.heightHint = 75;
			data.horizontalAlignment = GridData.FILL;
			data.grabExcessHorizontalSpace = true;
			detailsList.setLayoutData(data);
			
			for (int i = 0; i < projects.length; i++) {
				IProject project = projects[i];
				TargetProvider target;
				try {
					target = TargetManager.getProvider(project);
				} catch (TeamException e) {
					continue;
				}
				detailsList.add(Policy.bind("SiteExplorerView.mappedProjects", project.getName(), target.getURL().toExternalForm())); //$NON-NLS-1$
			}			
			return composite;
		}

		protected void createMainDialogArea(Composite composite) {
			Label label = new Label(composite, SWT.WRAP);
			label.setText(Policy.bind("SiteExplorerView.projectsAlreadyMapped")); //$NON-NLS-1$
			GridData data = new GridData(
				GridData.GRAB_HORIZONTAL |
				GridData.GRAB_VERTICAL |
				GridData.HORIZONTAL_ALIGN_FILL |
				GridData.VERTICAL_ALIGN_CENTER);
			data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
			label.setLayoutData(data);
			label.setFont(composite.getFont());
						
			unmap = new Button(composite, SWT.CHECK);
			unmap.setText(Policy.bind("SiteExplorerView.unmapProjectsAndDisconnect")); //$NON-NLS-1$
			unmap.setSelection(false);
			unmap.addListener(SWT.Selection, new Listener() {				
				public void handleEvent(Event event) {
					updateEnablements();
				}
			});
			setPageComplete(false);
			updateEnablements();
		}

		protected void updateEnablements() {
			setPageComplete(unmap.getSelection());
		}
	}

	/**
	 * @see TeamAction#isEnabled()
	 */
	protected boolean isEnabled() throws TeamException {
		return getSelectedSites().length > 0;
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		deletedSelected();
	}
	
	private void deletedSelected() {
		try {
			Site[] sites = getSelectedSites();
			if(sites.length > 0) {
				
				// sites are already mapped, ask if they want to unmap projects first
				IProject[] mappedProjects = projectsMappedToSite(sites);
				if( mappedProjects.length > 0 ) {
					AlreadyMappedDialog dialog = new AlreadyMappedDialog(getShell(), mappedProjects);
					if(dialog.open() == dialog.OK) {
						for (int i = 0; i < mappedProjects.length; i++) {
							TargetManager.unmap(mappedProjects[i]);
						}
					} else {
						// nothing to do
						return;
					}
				} else {
					MessageDialog d = new MessageDialog(getShell(),
								Policy.bind("SiteExplorerView.promptForDeletingSitesTitle"), //$NON-NLS-1$
								null,
								Policy.bind("SiteExplorerView.promptForDeletingSites", new Integer(sites.length).toString()), //$NON-NLS-1$
								MessageDialog.QUESTION, 
								new String[] {
									IDialogConstants.YES_LABEL,
									IDialogConstants.CANCEL_LABEL }, 0);
					if( d.open() != d.OK ) {
						return;
					}
				}
				// sites aren't mapped, just ask then delete them form the view 
				// and from the target manager.
				for (int i = 0; i < sites.length; i++) {
					TargetManager.removeSite(sites[i]);
					sites[i].dispose();
				}					
			}
		} catch (TeamException e) {
			ErrorDialog.openError(getShell(),
				Policy.bind("Error"), //$NON-NLS-1$
				Policy.bind("CreateNewFolderAction.errorDeletingSites"), //$NON-NLS-1$
				e.getStatus());
		}
	}
	
	private IProject[] projectsMappedToSite(Site[] sites) throws TeamException {
		List mappedProjects = new ArrayList();
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		for (int i = 0; i < projects.length; i++) {
			IProject project = projects[i];
			TargetProvider target = TargetManager.getProvider(project);
			for (int j = 0; j < sites.length; j++) {
				if(target != null && target.getSite().equals(sites[j])) {
					mappedProjects.add(project);	
				}
			}
		}
		return (IProject[]) mappedProjects.toArray(new IProject[mappedProjects.size()]);
	}
}

Back to the top