KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  * Philippe Ombredanne - bug 84808
11  *******************************************************************************/

12 package org.eclipse.team.internal.ccvs.ui.wizards;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jface.operation.IRunnableWithProgress;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.wizard.IWizardPage;
21 import org.eclipse.jface.wizard.Wizard;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.team.core.TeamException;
24 import org.eclipse.team.internal.ccvs.core.*;
25 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
26 import org.eclipse.team.internal.ccvs.ui.*;
27 import org.eclipse.team.internal.ccvs.ui.operations.CheckoutMultipleProjectsOperation;
28 import org.eclipse.team.internal.ccvs.ui.operations.ProjectMetaFileOperation;
29 import org.eclipse.ui.*;
30
31 /**
32  * Gathers all information necessary for a checkout from a repository.
33  */

34 public class CheckoutWizard extends Wizard implements ICVSWizard, INewWizard {
35     
36     private RepositorySelectionPage locationPage;
37     private ConfigurationWizardMainPage createLocationPage;
38     private ModuleSelectionPage modulePage;
39     private CheckoutAsWizard wizard;
40     private ICVSRepositoryLocation location;
41     private boolean isNewLocation;
42     private CVSWizardPage dummyPage;
43     
44     public CheckoutWizard() {
45         setWindowTitle(CVSUIMessages.CheckoutWizard_0);
46     }
47     
48     /* (non-Javadoc)
49      * @see org.eclipse.jface.wizard.Wizard#addPages()
50      */

51     public void addPages() {
52         setNeedsProgressMonitor(true);
53         
54         ImageDescriptor substImage = getBannerImageDescriptor();
55
56         ICVSRepositoryLocation[] locations = CVSUIPlugin.getPlugin().getRepositoryManager().getKnownRepositoryLocations();
57         if (locations.length > 0) {
58             locationPage = new RepositorySelectionPage("locationSelection", CVSUIMessages.CheckoutWizard_7, substImage); //$NON-NLS-1$
59
locationPage.setDescription(CVSUIMessages.SharingWizard_importTitleDescription);
60             locationPage.setExtendedDescription(CVSUIMessages.CheckoutWizard_8);
61             addPage(locationPage);
62         }
63         
64         createLocationPage = new ConfigurationWizardMainPage("createLocationPage", CVSUIMessages.SharingWizard_enterInformation, substImage); //$NON-NLS-1$
65
createLocationPage.setDescription(CVSUIMessages.SharingWizard_enterInformationDescription);
66         addPage(createLocationPage);
67         createLocationPage.setDialogSettings(NewLocationWizard.getLocationDialogSettings());
68         
69         modulePage = new ModuleSelectionPage("moduleSelection", CVSUIMessages.CheckoutWizard_10, substImage); //$NON-NLS-1$
70
modulePage.setDescription(CVSUIMessages.CheckoutWizard_11);
71         modulePage.setHelpContxtId(IHelpContextIds.CHECKOUT_MODULE_SELECTION_PAGE);
72         modulePage.setSupportsMultiSelection(true);
73         addPage(modulePage);
74         
75         // Dummy page to allow lazy creation of CheckoutAsWizard
76
dummyPage = new CVSWizardPage("dummyPage") { //$NON-NLS-1$
77
public void createControl(Composite parent) {
78                 Composite composite = createComposite(parent, 1, false);
79                 setControl(composite);
80             }
81         };
82         addPage(dummyPage);
83     }
84
85     protected ImageDescriptor getBannerImageDescriptor() {
86         return CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_CHECKOUT);
87     }
88     
89     /* (non-Javadoc)
90      * @see org.eclipse.jface.wizard.Wizard#canFinish()
91      */

92     public boolean canFinish() {
93         return (wizard == null && getSelectedModules().length > 0) ||
94             (wizard != null && wizard.canFinish());
95     }
96     
97     /* (non-Javadoc)
98      * @see org.eclipse.jface.wizard.IWizard#performFinish()
99      */

100     public boolean performFinish() {
101         if (wizard != null) {
102             // The finish of the child wizard will get called directly.
103
// We only get here if it completed successfully
104
if (isNewLocation) {
105                 KnownRepositories.getInstance().addRepository(location, true /* broadcast */);
106             }
107             return true;
108         } else {
109             try {
110                 new CheckoutMultipleProjectsOperation(getPart(), getSelectedModules(), null)
111                     .run();
112                 if (isNewLocation) {
113                     KnownRepositories.getInstance().addRepository(location, true /* broadcast */);
114                 }
115                 return true;
116             } catch (InvocationTargetException JavaDoc e) {
117                 CVSUIPlugin.openError(getShell(), null, null, e);
118             } catch (InterruptedException JavaDoc e) {
119                 // Canceled. fall through.
120
}
121             return false;
122         }
123     }
124     
125     private IWorkbenchPart getPart() {
126         // This wizard doesn't have a part
127
return null;
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jface.wizard.Wizard#performCancel()
132      */

133     public boolean performCancel() {
134         if (location != null && isNewLocation) {
135             KnownRepositories.getInstance().disposeRepository(location);
136             location = null;
137         }
138         return wizard == null || wizard.performCancel();
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
143      */

144     public IWizardPage getNextPage(IWizardPage page) {
145         // Assume the page is about to be shown when this method is
146
// invoked
147
return getNextPage(page, true /* about to show*/);
148     }
149     
150     /* (non-Javadoc)
151      * @see org.eclipse.team.internal.ccvs.ui.wizards.ICVSWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage, boolean)
152      */

153     public IWizardPage getNextPage(IWizardPage page, boolean aboutToShow) {
154         if (page == locationPage) {
155             if (locationPage.getLocation() == null) {
156                 return createLocationPage;
157             } else {
158                 if (aboutToShow) {
159                     try {
160                         modulePage.setLocation(getLocation());
161                     } catch (TeamException e1) {
162                         CVSUIPlugin.log(e1);
163                     }
164                 }
165                 return modulePage;
166             }
167         }
168         if (page == createLocationPage) {
169             if (aboutToShow) {
170                 try {
171                     ICVSRepositoryLocation l = getLocation();
172                     if (l != null) {
173                         modulePage.setLocation(l);
174                     }
175                 } catch (TeamException e1) {
176                     CVSUIPlugin.log(e1);
177                 }
178             }
179             return modulePage;
180         }
181         if (page == modulePage) {
182             ICVSRemoteFolder[] selectedModules = getSelectedModules();
183             if (selectedModules.length == 0) return null;
184             for (int i = 0; i < selectedModules.length; i++) {
185                 ICVSRemoteFolder folder = selectedModules[i];
186                 if (folder.isDefinedModule()) {
187                     // No further configuration is possible for defined modules
188
return null;
189                 }
190             }
191             if (aboutToShow) {
192                 try {
193                     boolean hasMetafile = true;
194                     if (selectedModules.length == 1) {
195                         // Only allow configuration if one module is selected
196
final ICVSRemoteFolder[] folders = new ICVSRemoteFolder[] {selectedModules[0]};
197                         final boolean withName = CVSUIPlugin.getPlugin().isUseProjectNameOnCheckout();
198
199                         // attempt to retrieve the project description depending on preferences
200
// this is a bit convoluted to batch the meta-file check and retrieval in one operation
201
final ICVSRemoteFolder[] folderResult = new ICVSRemoteFolder [1];
202                         final boolean[] booleanResult = new boolean[] { true };
203                         
204                         getContainer().run(true, true, new IRunnableWithProgress() {
205                             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
206                                 ProjectMetaFileOperation op = new ProjectMetaFileOperation(getPart(), new ICVSRemoteFolder[] {folders[0]}, withName);
207                                 op.run(monitor);
208                                 folderResult[0] = op.getUpdatedFolders()[0];
209                                 booleanResult[0] = op.metaFileExists();
210                             }
211                         });
212                         hasMetafile = booleanResult[0];
213                         if (withName && hasMetafile)
214                             selectedModules[0] = folderResult[0];
215                     }
216                     resetSubwizard();
217                     wizard = new CheckoutAsWizard(getPart(), selectedModules, ! hasMetafile /* allow configuration */);
218                     wizard.addPages();
219                     return wizard.getStartingPage();
220                 } catch (InvocationTargetException JavaDoc e) {
221                     // Show the error and fall through to return null as the next page
222
CVSUIPlugin.openError(getShell(), null, null, e);
223                 } catch (InterruptedException JavaDoc e) {
224                     // Canceled by user. Fall through and return null
225
}
226                 return null;
227             } else {
228                 if (wizard == null) {
229                     return dummyPage;
230                 } else {
231                     return wizard.getStartingPage();
232                 }
233             }
234         }
235         if (wizard != null) {
236             return wizard.getNextPage(page);
237         }
238         return null;
239     }
240
241     private ICVSRemoteFolder[] getSelectedModules() {
242         if (modulePage == null) return null;
243         return modulePage.getSelectedModules();
244     }
245
246     /**
247      * Return an ICVSRepositoryLocation
248      */

249     private ICVSRepositoryLocation getLocation() throws TeamException {
250         // If the location page has a location, use it.
251
if (locationPage != null) {
252             ICVSRepositoryLocation newLocation = locationPage.getLocation();
253             if (newLocation != null) {
254                 return recordLocation(newLocation);
255             }
256         }
257         
258         // Otherwise, get the location from the create location page
259
final ICVSRepositoryLocation[] locations = new ICVSRepositoryLocation[] { null };
260         final CVSException[] exception = new CVSException[] { null };
261         getShell().getDisplay().syncExec(new Runnable JavaDoc() {
262             public void run() {
263                 try {
264                     locations[0] = createLocationPage.getLocation();
265                 } catch (CVSException e) {
266                     exception[0] = e;
267                 }
268             }
269         });
270         if (exception[0] != null) {
271             throw exception[0];
272         }
273         return recordLocation(locations[0]);
274     }
275
276     private ICVSRepositoryLocation recordLocation(ICVSRepositoryLocation newLocation) {
277         if (newLocation == null) return location;
278         if (location == null || !newLocation.equals(location)) {
279             if (location != null && isNewLocation) {
280                 // Dispose of the previous location
281
KnownRepositories.getInstance().disposeRepository(location);
282             }
283             location = newLocation;
284             isNewLocation = !KnownRepositories.getInstance().isKnownRepository(newLocation.getLocation(false));
285             if (isNewLocation) {
286                 // Add the location silently so we can work with it
287
location = KnownRepositories.getInstance().addRepository(location, false /* silently */);
288             }
289         }
290         return location;
291     }
292
293     /* (non-Javadoc)
294      * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
295      */

296     public void init(IWorkbench workbench, IStructuredSelection selection) {
297     }
298
299     /*
300      * Reset the sub-wizard
301      */

302     /* package */ void resetSubwizard() {
303         if (wizard != null) {
304             wizard.dispose();
305             wizard = null;
306         }
307     }
308 }
309
Popular Tags