Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cf02db0f394004257138074620fcdf457e5b974 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.wizards;


import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.model.RemoteContentProvider;
import org.eclipse.team.internal.ccvs.ui.repo.RepositoryComparator;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchLabelProvider;

public class ModuleSelectionPage extends CVSWizardPage {
	Button useProjectNameButton;
	Button useSpecifiedNameButton;
	private Button selectModuleButton;
	private Button useModuleAndProjectNameButton;
	Text text;
	TreeViewer moduleList;
	
	String moduleName;
	
	// The project being associated with the remote module (or null)
	private IProject project;
	private ICVSRepositoryLocation location;
	private boolean badLocation = false;
	private String helpContextId;
	private boolean supportsMultiSelection;
	
	private boolean isFetchingModules = false;
	private Object fetchingModulesLock = new Object();
	
	private String SEPARATOR = "/"; //$NON-NLS-1$

	public ModuleSelectionPage(String pageName, String title, ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}
	
	public void setHelpContxtId(String helpContextId) {
		this.helpContextId = helpContextId;
	}
	
	public void createControl(Composite parent) {
		Composite composite = createComposite(parent, 2, false);

		if (helpContextId != null)
            PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, helpContextId);
		
		Listener listener = new Listener() {
			public void handleEvent(Event event) {
				updateEnablements(false);
			}
		};
		
		if (project != null) {
			useProjectNameButton = createRadioButton(composite, CVSUIMessages.ModuleSelectionPage_moduleIsProject, 2); 
			useProjectNameButton.addListener(SWT.Selection, listener);
		}
		useSpecifiedNameButton = createRadioButton(composite, CVSUIMessages.ModuleSelectionPage_specifyModule, 1); 
		useSpecifiedNameButton.addListener(SWT.Selection, listener);

		text = createTextField(composite);
		text.addListener(SWT.Modify, listener);
		
		selectModuleButton = createRadioButton(composite, CVSUIMessages.ModuleSelectionPage_2, 2); 
		selectModuleButton.addListener(SWT.Selection, listener);
		
		if (project != null) {
			useModuleAndProjectNameButton = new Button(composite, SWT.CHECK);
			useModuleAndProjectNameButton.setText(CVSUIMessages.ModuleSelectionPage_3);
			GridData data = new GridData();
			data.horizontalSpan = 2;
			data.horizontalIndent = 10;
			useModuleAndProjectNameButton.setLayoutData(data);
			useModuleAndProjectNameButton.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					updateText();
				}
			});
		}
		
		moduleList = createModuleTree(composite, 2);
		
		// Set the initial enablement
		if (useProjectNameButton != null) {
			useProjectNameButton.setSelection(true);
			useSpecifiedNameButton.setSelection(false);
		} else {
			useSpecifiedNameButton.setSelection(true);
		}
		selectModuleButton.setSelection(false);
		if (useModuleAndProjectNameButton != null)
			useModuleAndProjectNameButton.setSelection(false);
		updateEnablements(false);
		setControl(composite);
        Dialog.applyDialogFont(parent);
	}
	
	private void updateText() {
		updateEnablements(false);
		ICVSRemoteFolder[] modules = internalGetSelectedModules();
		if (modules.length == 1) {
			// There is at 1 module selected
			ICVSRemoteFolder selectedModule = modules[0];
			String repositoryRelativePath = selectedModule.getRepositoryRelativePath();
			if (!repositoryRelativePath.equals(FolderSyncInfo.VIRTUAL_DIRECTORY)) {
				text.setText(repositoryRelativePath);
			}
		} else {
			text.setText(useModuleAndProjectName() ? project.getName() : ""); //$NON-NLS-1$
		}
	}
	
	private boolean useModuleAndProjectName() {
		return useModuleAndProjectNameButton != null
				&& useModuleAndProjectNameButton.getSelection();
	}
	
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if (visible) {
			IWizard w = getWizard();
			if (w instanceof CheckoutWizard) {
				((CheckoutWizard)w).resetSubwizard();
			}
			if (useProjectNameButton != null && useProjectNameButton.getSelection()) {
				useProjectNameButton.setFocus();
			} else if (useSpecifiedNameButton.getSelection()) {
				text.setFocus();
			} else {
				moduleList.getControl().setFocus();
			}
		}
	}
	
	protected void updateEnablements(boolean updateModulesList) {
		if (useProjectNameButton != null && useProjectNameButton.getSelection()) {
			text.setEnabled(false);
			moduleList.getControl().setEnabled(false);
			if (useModuleAndProjectNameButton != null)
				useModuleAndProjectNameButton.setEnabled(false);
			moduleName = null;
			setPageComplete(true);
		} else if (useSpecifiedNameButton.getSelection()) {
			text.setEnabled(true);
			moduleList.getControl().setEnabled(false);
			if (useModuleAndProjectNameButton != null)
				useModuleAndProjectNameButton.setEnabled(false);
			moduleName = text.getText();
			if (moduleName.length() == 0) {
				moduleName = null;
				setPageComplete(false);
			} else {
				setPageComplete(true);
			}
		} else if (!badLocation){
			text.setEnabled(false);
			if (useModuleAndProjectNameButton != null)
				useModuleAndProjectNameButton.setEnabled(true);
			moduleList.getControl().setEnabled(true);
			moduleName = null;
			if (moduleList.getInput() == null || updateModulesList) {
				boolean fetchModules = false;
				// The input is set after the page is shown to avoid
				// fetching if the user wants to specify the name manually
				try {
					// This can be called from different events in the event loop.
					// Ensure that we only fetch the input once
					synchronized (fetchingModulesLock) {
						if (!isFetchingModules) {
							// This the first thread in so fetch the modules
							fetchModules = true;
							isFetchingModules = true;
						}
					}
					if (fetchModules) {
						// Validate the location first since the module fecthing is
						// done in a deferred fashion
						getContainer().run(true, true, new IRunnableWithProgress() {
							public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
								try {
									validateLocation(monitor);
								} catch (CVSException e) {
									throw new InvocationTargetException(e);
								}
							}
						});
						setModuleListInput();
					}
				} catch (InvocationTargetException e) {
					if (!badLocation) {
						badLocation = true;
						CVSUIPlugin.openError(getShell(), null, null, e);
						// This will null the module list input
						setModuleListInput();
					}
				} catch (InterruptedException e) {
					// Canceled by the user
				} finally {
					synchronized (fetchingModulesLock) {
						if (fetchModules) {
							isFetchingModules = false;
						}
					}
				}
			}
			setPageComplete(internalGetSelectedModules().length > 0);
		}
	}

	/* package */ ICVSRemoteFolder[] internalGetSelectedModules() {
		if (moduleList != null && moduleList.getControl().isEnabled()) {
			ISelection selection = moduleList.getSelection();
			if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
				IStructuredSelection ss = (IStructuredSelection)selection;
				ArrayList result = new ArrayList();
				for (Iterator iter = ss.iterator(); iter.hasNext();) {
					Object element = iter.next();
					if (element instanceof ICVSRemoteFolder) {
						if (useModuleAndProjectName()) {
							String relativePath = ((ICVSRemoteFolder)element).getRepositoryRelativePath();
							ICVSRemoteFolder remoteFolder = internalCreateModuleHandle(relativePath + SEPARATOR + project.getName())[0];
							result.add(remoteFolder);
						} else {
							result.add(element);
						}
					}
					
				}
				return (ICVSRemoteFolder[]) result.toArray(new ICVSRemoteFolder[result.size()]);
			}
		} else {
			if (moduleName != null) {
				return internalCreateModuleHandle(moduleName);
			} else if (project != null) {
				return internalCreateModuleHandle(project.getName());
			}
		} 
		return new ICVSRemoteFolder[0];
	}
	
	private ICVSRemoteFolder[] internalCreateModuleHandle(String name) {
		ICVSRepositoryLocation location = getLocation();
		if (location == null) return new ICVSRemoteFolder[0];
		String[] names = name.split(","); //$NON-NLS-1$
		int length = names.length;
		java.util.List folders = new ArrayList();
		for (int i = 0; i < length; i++) {
			// call trim() in case the user has added spaces after the commas
			String trimmedName = names[i].trim();
			if (trimmedName.length() > 0)
				folders.add(location.getRemoteFolder(trimmedName, CVSTag.DEFAULT));
		}
		return (ICVSRemoteFolder[]) folders.toArray(new ICVSRemoteFolder[folders.size()]);
	}
	
	/**
	 * Return the selected existing remote folder. If this
	 * method returns <code>null</code>, then <code>getModuleName()</code>
	 * can be used to get the name entered manually by the use.
	 * @return the selected existing remote module
	 */
	public ICVSRemoteFolder getSelectedModule() {
		ICVSRemoteFolder[] selectedModules = getSelectedModules();
		if (selectedModules.length > 0) {
			return selectedModules[0];
		} else {
			return null;
		}
	}
	
	public ICVSRemoteFolder[] getSelectedModules() {
		final ICVSRemoteFolder[][] folder = new ICVSRemoteFolder[][] { null };
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				folder[0] = internalGetSelectedModules();
			}
		});
		return folder[0];
	}
	
	private TreeViewer createModuleTree(Composite composite, int horizontalSpan) {
		Tree tree = new Tree(composite, (supportsMultiSelection ? SWT.MULTI : SWT.SINGLE) | SWT.BORDER);
		GridData data = new GridData(GridData.FILL_BOTH);
				
		// see bug 158380
		data.heightHint = Math.max(composite.getParent().getSize().y, 100);
		
		data.horizontalSpan = horizontalSpan;
		tree.setLayoutData(data);	
		TreeViewer result = new TreeViewer(tree) {
			/*
			 * Fix to allow filtering to be used without triggering fetching 
			 * of the contents of all children (see bug 62268)
			 */
			public boolean isExpandable(Object element) {
				ITreeContentProvider cp = (ITreeContentProvider) getContentProvider();
				if(cp == null)
					return false;
				
				return cp.hasChildren(element);
			}
		};
		result.setContentProvider(new RemoteContentProvider());
		result.setLabelProvider(new WorkbenchLabelProvider());
		result.addFilter(new ViewerFilter() {
			public boolean select(Viewer viewer, Object parentElement, Object element) {
				return !(element instanceof ICVSRemoteFile);
			}
		});
		result.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				updateText();
			}
		});
		result.getTree().addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent e) {
				if (getSelectedModule() != null) {
					gotoNextPage();
				}
			}
		});
		result.setComparator(new RepositoryComparator());
		return result;
	}
	
	private void setModuleListInput() {
		ICVSRepositoryLocation location = getLocation();
		if (location == null || badLocation) return;
		moduleList.setInput(location.getRemoteFolder(ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, CVSTag.DEFAULT));
	}

	private ICVSRepositoryLocation getLocation() {
		return location;
	}
	
	public void setLocation(ICVSRepositoryLocation location) {
		boolean refresh = location != null && !location.equals(this.location);
		this.location = location;
		badLocation = false;
		if (moduleList != null) {
			updateEnablements(refresh);
		}
	}

	public void setProject(IProject project) {
		this.project = project;
	}
	public void setSupportsMultiSelection(boolean supportsMultiSelection) {
		this.supportsMultiSelection = supportsMultiSelection;
	}

	/* package */ void gotoNextPage() {
		getContainer().showPage(getNextPage());
	}

	/* package */ void validateLocation(IProgressMonitor monitor) throws CVSException {
		location.validateConnection(monitor);
	}
}

Back to the top