KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > ModuleSelectionPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.wizards;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.viewers.*;
24 import org.eclipse.jface.wizard.IWizard;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.MouseAdapter;
27 import org.eclipse.swt.events.MouseEvent;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.team.internal.ccvs.core.*;
31 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
32 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
33 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
34 import org.eclipse.team.internal.ccvs.ui.model.RemoteContentProvider;
35 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryComparator;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.model.WorkbenchLabelProvider;
38
39 public class ModuleSelectionPage extends CVSWizardPage {
40     Button useProjectNameButton;
41     Button useSpecifiedNameButton;
42     private Button selectModuleButton;
43     Text text;
44     TreeViewer moduleList;
45     
46     String JavaDoc moduleName;
47     
48     // The project being associated with the remote module (or null)
49
private IProject project;
50     private ICVSRepositoryLocation location;
51     private boolean badLocation = false;
52     private String JavaDoc helpContextId;
53     private boolean supportsMultiSelection;
54     
55     private boolean isFetchingModules = false;
56     private Object JavaDoc fetchingModulesLock = new Object JavaDoc();
57     
58     public ModuleSelectionPage(String JavaDoc pageName, String JavaDoc title, ImageDescriptor titleImage) {
59         super(pageName, title, titleImage);
60     }
61     
62     public void setHelpContxtId(String JavaDoc helpContextId) {
63         this.helpContextId = helpContextId;
64     }
65     
66     public void createControl(Composite parent) {
67         Composite composite = createComposite(parent, 2, false);
68
69         if (helpContextId != null)
70             PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, helpContextId);
71         
72         Listener listener = new Listener() {
73             public void handleEvent(Event event) {
74                 updateEnablements(false);
75             }
76         };
77         
78         if (project != null) {
79             useProjectNameButton = createRadioButton(composite, CVSUIMessages.ModuleSelectionPage_moduleIsProject, 2);
80             useProjectNameButton.addListener(SWT.Selection, listener);
81         }
82         useSpecifiedNameButton = createRadioButton(composite, CVSUIMessages.ModuleSelectionPage_specifyModule, 1);
83         useSpecifiedNameButton.addListener(SWT.Selection, listener);
84
85         text = createTextField(composite);
86         text.addListener(SWT.Modify, listener);
87         
88         selectModuleButton = createRadioButton(composite, CVSUIMessages.ModuleSelectionPage_2, 2);
89         selectModuleButton.addListener(SWT.Selection, listener);
90         moduleList = createModuleTree(composite, 2);
91         
92         // Set the initial enablement
93
if (useProjectNameButton != null) {
94             useProjectNameButton.setSelection(true);
95             useSpecifiedNameButton.setSelection(false);
96         } else {
97             useSpecifiedNameButton.setSelection(true);
98         }
99         selectModuleButton.setSelection(false);
100         updateEnablements(false);
101         setControl(composite);
102         Dialog.applyDialogFont(parent);
103     }
104     
105     public void setVisible(boolean visible) {
106         super.setVisible(visible);
107         if (visible) {
108             IWizard w = getWizard();
109             if (w instanceof CheckoutWizard) {
110                 ((CheckoutWizard)w).resetSubwizard();
111             }
112             if (useProjectNameButton != null && useProjectNameButton.getSelection()) {
113                 useProjectNameButton.setFocus();
114             } else if (useSpecifiedNameButton.getSelection()) {
115                 text.setFocus();
116             } else {
117                 moduleList.getControl().setFocus();
118             }
119         }
120     }
121     
122     protected void updateEnablements(boolean updateModulesList) {
123         if (useProjectNameButton != null && useProjectNameButton.getSelection()) {
124             text.setEnabled(false);
125             moduleList.getControl().setEnabled(false);
126             moduleName = null;
127             setPageComplete(true);
128         } else if (useSpecifiedNameButton.getSelection()) {
129             text.setEnabled(true);
130             moduleList.getControl().setEnabled(false);
131             moduleName = text.getText();
132             if (moduleName.length() == 0) {
133                 moduleName = null;
134                 setPageComplete(false);
135             } else {
136                 setPageComplete(true);
137             }
138         } else if (!badLocation){
139             text.setEnabled(false);
140             moduleList.getControl().setEnabled(true);
141             moduleName = null;
142             if (moduleList.getInput() == null || updateModulesList) {
143                 boolean fetchModules = false;
144                 // The input is set after the page is shown to avoid
145
// fetching if the user wants to specify the name manually
146
try {
147                     // This can be called from different events in the event loop.
148
// Ensure that we only fetch the input once
149
synchronized (fetchingModulesLock) {
150                         if (!isFetchingModules) {
151                             // This the first thread in so fetch the modules
152
fetchModules = true;
153                             isFetchingModules = true;
154                         }
155                     }
156                     if (fetchModules) {
157                         // Validate the location first since the module fecthing is
158
// done in a deferred fashion
159
getContainer().run(true, true, new IRunnableWithProgress() {
160                             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
161                                 try {
162                                     location.validateConnection(monitor);
163                                 } catch (CVSException e) {
164                                     throw new InvocationTargetException JavaDoc(e);
165                                 }
166                             }
167                         });
168                         setModuleListInput();
169                     }
170                 } catch (InvocationTargetException JavaDoc e) {
171                     if (!badLocation) {
172                         badLocation = true;
173                         CVSUIPlugin.openError(getShell(), null, null, e);
174                         // This will null the module list input
175
setModuleListInput();
176                     }
177                 } catch (InterruptedException JavaDoc e) {
178                     // Canceled by the user
179
} finally {
180                     synchronized (fetchingModulesLock) {
181                         if (fetchModules) {
182                             isFetchingModules = false;
183                         }
184                     }
185                 }
186             }
187             setPageComplete(internalGetSelectedModules().length > 0);
188         }
189     }
190
191     private ICVSRemoteFolder[] internalGetSelectedModules() {
192         if (moduleList != null && moduleList.getControl().isEnabled()) {
193             ISelection selection = moduleList.getSelection();
194             if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
195                 IStructuredSelection ss = (IStructuredSelection)selection;
196                 ArrayList JavaDoc result = new ArrayList JavaDoc();
197                 for (Iterator JavaDoc iter = ss.iterator(); iter.hasNext();) {
198                     Object JavaDoc element = iter.next();
199                     if (element instanceof ICVSRemoteFolder) {
200                         result.add(element);
201                     }
202                     
203                 }
204                 return (ICVSRemoteFolder[]) result.toArray(new ICVSRemoteFolder[result.size()]);
205             }
206         } else {
207             ICVSRemoteFolder folder = null;
208             if (moduleName != null) {
209                 folder = internalCreateModuleHandle(moduleName);
210             } else {
211                 if (project != null) {
212                     folder = internalCreateModuleHandle(project.getName());
213                 }
214             }
215             if (folder != null) {
216                 return new ICVSRemoteFolder[] { folder };
217             }
218         }
219         return new ICVSRemoteFolder[0];
220     }
221     
222     private ICVSRemoteFolder internalCreateModuleHandle(String JavaDoc name) {
223         ICVSRepositoryLocation location = getLocation();
224         if (location == null) return null;
225         return location.getRemoteFolder(name, CVSTag.DEFAULT);
226     }
227     
228     /**
229      * Return the selected existing remote folder. If this
230      * method returns <code>null</code>, then <code>getModuleName()</code>
231      * can be used to get the name entered manually by the use.
232      * @return the selected existing remote module
233      */

234     public ICVSRemoteFolder getSelectedModule() {
235         ICVSRemoteFolder[] selectedModules = getSelectedModules();
236         if (selectedModules.length > 0) {
237             return selectedModules[0];
238         } else {
239             return null;
240         }
241     }
242     
243     public ICVSRemoteFolder[] getSelectedModules() {
244         final ICVSRemoteFolder[][] folder = new ICVSRemoteFolder[][] { null };
245         Display.getDefault().syncExec(new Runnable JavaDoc() {
246             public void run() {
247                 folder[0] = internalGetSelectedModules();
248             }
249         });
250         return folder[0];
251     }
252     
253     private TreeViewer createModuleTree(Composite composite, int horizontalSpan) {
254         Tree tree = new Tree(composite, (supportsMultiSelection ? SWT.MULTI : SWT.SINGLE) | SWT.BORDER);
255         GridData data = new GridData(GridData.FILL_BOTH);
256                 
257         // see bug 158380
258
data.heightHint = Math.max(composite.getParent().getSize().y, 100);
259         
260         data.horizontalSpan = horizontalSpan;
261         tree.setLayoutData(data);
262         TreeViewer result = new TreeViewer(tree) {
263             /*
264              * Fix to allow filtering to be used without triggering fetching
265              * of the contents of all children (see bug 62268)
266              */

267             public boolean isExpandable(Object JavaDoc element) {
268                 ITreeContentProvider cp = (ITreeContentProvider) getContentProvider();
269                 if(cp == null)
270                     return false;
271                 
272                 return cp.hasChildren(element);
273             }
274         };
275         result.setContentProvider(new RemoteContentProvider());
276         result.setLabelProvider(new WorkbenchLabelProvider());
277         result.addFilter(new ViewerFilter() {
278             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
279                 return !(element instanceof ICVSRemoteFile);
280             }
281         });
282         result.addSelectionChangedListener(new ISelectionChangedListener() {
283             public void selectionChanged(SelectionChangedEvent event) {
284                 updateEnablements(false);
285                 ICVSRemoteFolder[] modules = internalGetSelectedModules();
286                 if (modules.length == 1) {
287                     // There is at 1 module selected
288
ICVSRemoteFolder selectedModule = modules[0];
289                     String JavaDoc repositoryRelativePath = selectedModule.getRepositoryRelativePath();
290                     if (!repositoryRelativePath.equals(FolderSyncInfo.VIRTUAL_DIRECTORY)) {
291                         text.setText(repositoryRelativePath);
292                     }
293                 } else {
294                     text.setText(""); //$NON-NLS-1$
295
}
296             }
297         });
298         result.getTree().addMouseListener(new MouseAdapter() {
299             public void mouseDoubleClick(MouseEvent e) {
300                 if (getSelectedModule() != null) {
301                     ModuleSelectionPage.this.getContainer().showPage(getNextPage());
302                 }
303             }
304         });
305         result.setComparator(new RepositoryComparator());
306         return result;
307     }
308     
309     private void setModuleListInput() {
310         ICVSRepositoryLocation location = getLocation();
311         if (location == null || badLocation) return;
312         moduleList.setInput(location.getRemoteFolder(ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, CVSTag.DEFAULT));
313     }
314
315     private ICVSRepositoryLocation getLocation() {
316         return location;
317     }
318     
319     public void setLocation(ICVSRepositoryLocation location) {
320         boolean refresh = location != null && !location.equals(this.location);
321         this.location = location;
322         badLocation = false;
323         if (moduleList != null) {
324             updateEnablements(refresh);
325         }
326     }
327
328     public void setProject(IProject project) {
329         this.project = project;
330     }
331     public void setSupportsMultiSelection(boolean supportsMultiSelection) {
332         this.supportsMultiSelection = supportsMultiSelection;
333     }
334 }
335
Popular Tags