KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.wizards;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.wizard.IWizardPage;
24 import org.eclipse.jface.wizard.Wizard;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.team.core.RepositoryProvider;
29 import org.eclipse.team.core.TeamException;
30 import org.eclipse.team.internal.ccvs.core.*;
31 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
32 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
33 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
34 import org.eclipse.team.internal.ccvs.ui.*;
35 import org.eclipse.team.internal.ccvs.ui.Policy;
36 import org.eclipse.team.internal.ccvs.ui.operations.*;
37 import org.eclipse.team.internal.ccvs.ui.tags.*;
38 import org.eclipse.team.internal.ui.Utils;
39 import org.eclipse.team.ui.IConfigurationWizard;
40 import org.eclipse.team.ui.synchronize.ModelSynchronizeParticipant;
41 import org.eclipse.ui.IWorkbench;
42
43 /**
44  * This wizard helps the user to import a new project in their workspace
45  * into a CVS repository for the first time.
46  */

47 public class SharingWizard extends Wizard implements IConfigurationWizard, ICVSWizard {
48     // The project to configure
49
private IProject project;
50
51     // The autoconnect page is used if CVS/ directories already exist.
52
private ConfigurationWizardAutoconnectPage autoconnectPage;
53     
54     // The import page is used if CVS/ directories do not exist.
55
private RepositorySelectionPage locationPage;
56     
57     // The page that prompts the user for connection information.
58
private ConfigurationWizardMainPage createLocationPage;
59     
60     // The page that prompts the user for module name.
61
private ModuleSelectionPage modulePage;
62
63     // The page that lets the user pick a branch to share against
64
private TagSelectionWizardPage tagPage;
65     
66     // The page that allows the user to commit or update resources
67
private SharingWizardSyncPage syncPage;
68     
69     // Keep track of location state so we know what to do at the end
70
private ICVSRepositoryLocation location;
71     private boolean isNewLocation;
72     
73     // Keep track of the folder that existed the last time we checked
74
private ICVSRemoteFolder existingRemote;
75     
76     public SharingWizard() {
77         IDialogSettings cvsSettings = CVSUIPlugin.getPlugin().getDialogSettings();
78         IDialogSettings section = cvsSettings.getSection("SharingWizard");//$NON-NLS-1$
79
if (section == null) {
80             section = cvsSettings.addNewSection("SharingWizard");//$NON-NLS-1$
81
}
82         setDialogSettings(section);
83         setNeedsProgressMonitor(true);
84         setWindowTitle(CVSUIMessages.SharingWizard_title);
85     }
86         
87     public void addPages() {
88         ImageDescriptor sharingImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_SHARE);
89         boolean autoconnect = false;
90         if (doesCVSDirectoryExist()) {
91             autoconnectPage = new ConfigurationWizardAutoconnectPage("autoconnectPage", CVSUIMessages.SharingWizard_autoConnectTitle, sharingImage); //$NON-NLS-1$
92
if (autoconnectPage.setProject(project)) {
93                 autoconnectPage.setDescription(CVSUIMessages.SharingWizard_autoConnectTitleDescription);
94                 addPage(autoconnectPage);
95                 autoconnect = true;
96             }
97         }
98         if (!autoconnect) {
99             ICVSRepositoryLocation[] locations = CVSUIPlugin.getPlugin().getRepositoryManager().getKnownRepositoryLocations();
100             if (locations.length > 0) {
101                 locationPage = new RepositorySelectionPage("importPage", CVSUIMessages.SharingWizard_importTitle, sharingImage); //$NON-NLS-1$
102
locationPage.setDescription(CVSUIMessages.SharingWizard_importTitleDescription);
103                 addPage(locationPage);
104             }
105             createLocationPage = new ConfigurationWizardMainPage("createLocationPage", CVSUIMessages.SharingWizard_enterInformation, sharingImage); //$NON-NLS-1$
106
createLocationPage.setDescription(CVSUIMessages.SharingWizard_enterInformationDescription);
107             createLocationPage.setCVSWizard(this);
108             createLocationPage.setDialogSettings(NewLocationWizard.getLocationDialogSettings());
109             addPage(createLocationPage);
110             modulePage = new ModuleSelectionPage("modulePage", CVSUIMessages.SharingWizard_enterModuleName, sharingImage); //$NON-NLS-1$
111
modulePage.setDescription(CVSUIMessages.SharingWizard_enterModuleNameDescription);
112             modulePage.setCVSWizard(this);
113             modulePage.setProject(project);
114             modulePage.setHelpContxtId(IHelpContextIds.SHARING_MODULE_PAGE);
115             addPage(modulePage);
116             
117             addTagPage(sharingImage);
118             addSyncPage(sharingImage);
119         }
120     }
121     
122     private void addTagPage(ImageDescriptor sharingImage) {
123         tagPage = new TagSelectionWizardPage("tagPage", //$NON-NLS-1$
124
CVSUIMessages.SharingWizard_selectTagTitle,
125             sharingImage,
126             CVSUIMessages.SharingWizard_selectTag,
127             TagSource.EMPTY, // start with an empty tag source
128
TagSourceWorkbenchAdapter.INCLUDE_HEAD_TAG | TagSourceWorkbenchAdapter.INCLUDE_BRANCHES);
129         tagPage.setCVSWizard(this);
130         tagPage.setHelpContxtId(IHelpContextIds.SHARING_TAG_SELETION_PAGE);
131         addPage(tagPage);
132     }
133     
134     private void addSyncPage(ImageDescriptor sharingImage) {
135         syncPage = new SharingWizardSyncPage("syncPagePage", //$NON-NLS-1$
136
CVSUIMessages.SharingWizard_23,
137             sharingImage,
138             CVSUIMessages.SharingWizard_24);
139         syncPage.setProject(project);
140         syncPage.setCVSWizard(this);
141         addPage(syncPage);
142     }
143     
144     public boolean canFinish() {
145         IWizardPage page = getContainer().getCurrentPage();
146         return (page == autoconnectPage || page == syncPage);
147     }
148     
149     public IWizardPage getNextPage(IWizardPage page) {
150         // Assume the page is about to be shown when this method is
151
// invoked
152
return getNextPage(page, true /* about to show*/);
153     }
154     
155     public IWizardPage getNextPage(IWizardPage page, boolean aboutToShow) {
156         if (page == autoconnectPage) return null;
157         if (page == locationPage) {
158             if (locationPage.getLocation() == null) {
159                 return createLocationPage;
160             } else {
161                 if (aboutToShow) {
162                     try {
163                         modulePage.setLocation(getLocation());
164                     } catch (TeamException e1) {
165                         CVSUIPlugin.log(e1);
166                     }
167                 }
168                 return modulePage;
169             }
170         }
171         if (page == createLocationPage) {
172             if (aboutToShow) {
173                 try {
174                     modulePage.setLocation(getLocation());
175                 } catch (TeamException e1) {
176                     CVSUIPlugin.log(e1);
177                 }
178             }
179             return modulePage;
180         }
181         try {
182             if (page == modulePage) {
183                 if (aboutToShow) {
184                     ICVSRemoteFolder remoteFolder = getRemoteFolder();
185                     if (exists(remoteFolder)) {
186                         prepareTagPage(remoteFolder);
187                         return tagPage;
188                     } else {
189                         try {
190                             populateSyncPage(false /* remote exists */);
191                         } catch (InvocationTargetException JavaDoc e) {
192                             CVSUIPlugin.openError(getShell(), null, null, e);
193                             if (!RepositoryProvider.isShared(project)) {
194                                 // Only stay on the current page if the sharing was a total failure
195
return null;
196                             }
197                         }
198                         return syncPage;
199                     }
200                 } else {
201                     return syncPage;
202                 }
203             }
204             if (page == tagPage) {
205                 if (aboutToShow) {
206                     populateSyncPage(true /* remote exists */);
207                 }
208                 return syncPage;
209             }
210         } catch (InvocationTargetException JavaDoc e) {
211             // Show the error and fall through to return null as the next page
212
CVSUIPlugin.openError(getShell(), null, null, e);
213         } catch (InterruptedException JavaDoc e) {
214             // The user cancelled. Falll through and return null as the next page.
215
}
216         return null;
217     }
218
219     /*
220      * @see IWizard#performFinish
221      */

222     public boolean performFinish() {
223         final boolean[] result = new boolean[] { true };
224         if (isAutoconnect()) {
225             try {
226                 getContainer().run(true /* fork */, true /* cancel */, new IRunnableWithProgress() {
227                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
228                         try {
229                             result[0] = autoconnectCVSProject(monitor);
230                         } catch (TeamException e) {
231                             throw new InvocationTargetException JavaDoc(e);
232                         } finally {
233                             monitor.done();
234                         }
235                     }
236                 });
237             } catch (InterruptedException JavaDoc e) {
238                 return true;
239             } catch (InvocationTargetException JavaDoc e) {
240                 CVSUIPlugin.openError(getContainer().getShell(), null, null, e);
241                 result[0] = false;
242             }
243         }
244         // Add the location to the provider if it is new
245
if (result[0] && isNewLocation) {
246             KnownRepositories.getInstance().addRepository(location, true /* broadcast */);
247         }
248         
249         final Shell parentShell= getShell().getParent().getShell();
250         if (getContainer().getCurrentPage() == syncPage) {
251             syncPage.saveSettings();
252             if (syncPage.commitChanges()) {
253                 Display.getCurrent().asyncExec(new Runnable JavaDoc() {
254                     public void run() {
255                         try {
256                             CommitWizard.run(null, parentShell, new IResource[] { syncPage.getProject() });
257                         } catch (CVSException e) {
258                             //TODO:handle
259
}
260                     }
261                 });
262             }
263         }
264         return result[0];
265     }
266
267     /* (non-Javadoc)
268      * @see org.eclipse.jface.wizard.IWizard#performCancel()
269      */

270     public boolean performCancel() {
271         boolean disposeLocation = isNewLocation;
272         ICVSRepositoryLocation location;
273         try {
274             location = getLocation();
275         } catch (TeamException e) {
276             CVSUIPlugin.log(e);
277             return true;
278         }
279         if (location == null) return true;
280         // If on the last page, offer to disconnect
281
if (getContainer().getCurrentPage() == syncPage
282                 && RepositoryProvider.getProvider(project) != null) {
283             // Prompt to see if we should undo out work
284
if (promptToKeepMapping()) {
285                 // If we didn't disconnect, don't dispose the repo
286
disposeLocation = false;
287                 // Add the location to the provider if it is new
288
if (isNewLocation) {
289                     KnownRepositories.getInstance().addRepository(location, true /* broadcast */);
290                 }
291             } else {
292                 try {
293                     getContainer().run(true, true, new IRunnableWithProgress() {
294                         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
295                             new DisconnectOperation(null, new IProject[] { project }, true)
296                                 .run(monitor);
297                         }
298                     });
299                 } catch (InvocationTargetException JavaDoc e) {
300                     CVSUIPlugin.log(IStatus.ERROR, e.getMessage(), e.getTargetException());
301                 } catch (InterruptedException JavaDoc e) {
302                     // Ignore
303
}
304             }
305         }
306         // Dispose of the location if appropriate
307
if (disposeLocation) {
308             KnownRepositories.getInstance().disposeRepository(location);
309         }
310         return super.performCancel();
311     }
312
313     private boolean promptToKeepMapping() {
314         return (MessageDialog.openQuestion(getShell(), CVSUIMessages.SharingWizard_26, NLS.bind(CVSUIMessages.SharingWizard_27, new String JavaDoc[] { project.getName() }))); //
315
}
316
317     private void reconcileProject(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
318         new ReconcileProjectOperation(getShell(), project, getRemoteFolder()).run(monitor);
319     }
320     
321     /**
322      * Return an ICVSRepositoryLocation
323      */

324     private ICVSRepositoryLocation getLocation() throws TeamException {
325         // If there is an autoconnect page then it has the location
326
if (autoconnectPage != null) {
327             return recordLocation(autoconnectPage.getLocation());
328         }
329         
330         // If the location page has a location, use it.
331
if (locationPage != null) {
332             ICVSRepositoryLocation newLocation = locationPage.getLocation();
333             if (newLocation != null) {
334                 return recordLocation(newLocation);
335             }
336         }
337         
338         // Otherwise, get the location from the create location page
339
final ICVSRepositoryLocation[] locations = new ICVSRepositoryLocation[] { null };
340         final CVSException[] exception = new CVSException[] { null };
341         getShell().getDisplay().syncExec(new Runnable JavaDoc() {
342             public void run() {
343                 try {
344                     locations[0] = createLocationPage.getLocation();
345                 } catch (CVSException e) {
346                     exception[0] = e;
347                 }
348             }
349         });
350         if (exception[0] != null) {
351             throw exception[0];
352         }
353         return recordLocation(locations[0]);
354     }
355
356     private ICVSRepositoryLocation recordLocation(ICVSRepositoryLocation newLocation) {
357         if (newLocation == null) return location;
358         if (location == null || !newLocation.equals(location)) {
359             if (location != null && isNewLocation) {
360                 // Dispose of the previous location
361
KnownRepositories.getInstance().disposeRepository(location);
362             }
363             location = newLocation;
364             isNewLocation = !KnownRepositories.getInstance().isKnownRepository(newLocation.getLocation(false));
365             if (isNewLocation) {
366                 // Add the location silently so we can work with it
367
location = KnownRepositories.getInstance().addRepository(location, false /* silently */);
368             }
369         }
370         return location;
371     }
372     
373     /*
374      * @see IConfigurationWizard#init(IWorkbench, IProject)
375      */

376     public void init(IWorkbench workbench, IProject project) {
377         this.project = project;
378     }
379     
380     private boolean doesCVSDirectoryExist() {
381         // Determine if there is an existing CVS/ directory from which configuration
382
// information can be retrieved.
383
Shell shell = null;
384         if (getContainer() != null) {
385             shell = getContainer().getShell();
386         }
387         final boolean[] isCVSFolder = new boolean[] { false };
388         try {
389             CVSUIPlugin.runWithRefresh(shell, new IResource[] { project }, new IRunnableWithProgress() {
390                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
391                     try {
392                         ICVSFolder folder = (ICVSFolder)CVSWorkspaceRoot.getCVSResourceFor(project);
393                         FolderSyncInfo info = folder.getFolderSyncInfo();
394                         isCVSFolder[0] = info != null;
395                     } catch (final TeamException e) {
396                         throw new InvocationTargetException JavaDoc(e);
397                     }
398                 }
399             }, null);
400         } catch (InvocationTargetException JavaDoc e) {
401             CVSUIPlugin.openError(shell, null, null, e);
402         } catch (InterruptedException JavaDoc e) {
403             // Cancelled. Just fall through
404
}
405         return isCVSFolder[0];
406     }
407     
408     /*
409      * Shoudl the project be auto-connected
410      */

411     /* private*/ boolean isAutoconnect() {
412         return autoconnectPage != null && doesCVSDirectoryExist();
413     }
414     
415     /*
416      * Auto-connect to the repository using CVS/ directories
417      */

418     /*private */ boolean autoconnectCVSProject(IProgressMonitor monitor) throws TeamException {
419         try {
420             monitor.beginTask(null, 100);
421             
422             FolderSyncInfo info = autoconnectPage.getFolderSyncInfo();
423             if (info == null) {
424                 // Error!
425
return false;
426             }
427             
428             // Get the repository location (the get will add the locatin to the provider)
429
ICVSRepositoryLocation location = getLocation();
430     
431             // Validate the connection if the user wants to
432
boolean validate = autoconnectPage.getValidate();
433             if (validate) {
434                 // Do the validation
435
try {
436                     location.validateConnection(Policy.subMonitorFor(monitor, 50));
437                 } catch (final TeamException e) {
438                     // Exception validating. We can continue if the user wishes.
439
final boolean[] keep = new boolean[] { false };
440                     getShell().getDisplay().syncExec(new Runnable JavaDoc() {
441                         public void run() {
442                             keep[0] = MessageDialog.openQuestion(getContainer().getShell(),
443                                 CVSUIMessages.SharingWizard_validationFailedTitle,
444                                 NLS.bind(CVSUIMessages.SharingWizard_validationFailedText, (new Object JavaDoc[] {e.getStatus().getMessage()})));
445                         }
446                     });
447                     if (!keep[0]) {
448                         return false;
449                     }
450                     // They want to keep the connection anyway. Fall through.
451
}
452             }
453             
454             // Set the sharing
455
CVSWorkspaceRoot.setSharing(project, info, Policy.subMonitorFor(monitor, 50));
456             return true;
457         } finally {
458             monitor.done();
459         }
460     }
461     
462     private boolean shareProject(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
463         monitor.beginTask(null, 100);
464         ICVSRepositoryLocation location = null;
465         try {
466             location = getLocation();
467             location.validateConnection(Policy.subMonitorFor(monitor, 50));
468         } catch (TeamException e) {
469             CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC);
470             if (isNewLocation && location != null) location.flushUserInfo();
471             return false;
472         }
473         
474         // Create the remote module for the project
475
ShareProjectOperation op = new ShareProjectOperation(null, location, project, getRemoteFolder().getRepositoryRelativePath());
476         op.setShell(getShell());
477         op.run(Policy.subMonitorFor(monitor, 50));
478         return true;
479     }
480     
481     private CVSTag getTag() {
482         if (tagPage == null || tagPage.getSelectedTag() == null) {
483             return CVSTag.DEFAULT;
484         }
485         return tagPage.getSelectedTag();
486     }
487     
488     private ICVSRemoteFolder getRemoteFolder() {
489         ICVSRemoteFolder folder = modulePage.getSelectedModule();
490         return (ICVSRemoteFolder)folder.forTag(getTag());
491     }
492     
493     private boolean exists(ICVSRemoteFolder folder, IProgressMonitor monitor) throws TeamException {
494         if (existingRemote != null && existingRemote.equals(folder)) return true;
495         if (folder.exists(monitor)) {
496             existingRemote = folder;
497             return true;
498         } else {
499             existingRemote = null;
500             return false;
501         }
502     }
503     
504     private boolean exists(final ICVSRemoteFolder folder) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
505         final boolean[] result = new boolean[] { false };
506         getContainer().run(true, true, new IRunnableWithProgress() {
507             public void run(IProgressMonitor monitor)
508                     throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
509                 try {
510                     result[0] = exists(folder, monitor);
511                 } catch (TeamException e) {
512                     throw new InvocationTargetException JavaDoc(e);
513                 }
514             }
515         });
516         return result[0];
517     }
518     
519     private void populateSyncPage(final boolean exists) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
520         getContainer().run(true, true, new IRunnableWithProgress() {
521             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
522                 monitor.beginTask(null, IProgressMonitor.UNKNOWN);
523                 if (exists) {
524                     reconcileProject(Policy.subMonitorFor(monitor, 50));
525                 } else {
526                     shareProject(Policy.subMonitorFor(monitor, 50));
527                 }
528                 try {
529                     getParticipant().getContext().refresh(Utils.getResourceMappings(new IProject[] { project }), Policy.subMonitorFor(monitor, 50));
530                 } catch (CoreException e) {
531                     throw new InvocationTargetException JavaDoc(e);
532                 }
533                 if (monitor.isCanceled()) {
534                     throw new InterruptedException JavaDoc();
535                 }
536                 monitor.done();
537             }
538         });
539     }
540     
541     /* (non-Javadoc)
542      * @see org.eclipse.jface.wizard.IWizard#getPreviousPage(org.eclipse.jface.wizard.IWizardPage)
543      */

544     public IWizardPage getPreviousPage(IWizardPage page) {
545         if (page == syncPage) {
546             // There's no going back from the sync page
547
return null;
548         }
549         return super.getPreviousPage(page);
550     }
551     
552     private void prepareTagPage(ICVSRemoteFolder remote) {
553         tagPage.setTagSource(TagSource.create(remote));
554         tagPage.setDescription(NLS.bind(CVSUIMessages.SharingWizard_25, new String JavaDoc[] { remote.getRepositoryRelativePath() }));
555     }
556
557     private ModelSynchronizeParticipant getParticipant() {
558         return syncPage.getParticipant();
559     }
560 }
561
Popular Tags