KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 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.resources.*;
17 import org.eclipse.jface.dialogs.IDialogSettings;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.wizard.IWizardPage;
20 import org.eclipse.jface.wizard.Wizard;
21 import org.eclipse.team.internal.ccvs.core.*;
22 import org.eclipse.team.internal.ccvs.ui.*;
23 import org.eclipse.team.internal.ccvs.ui.operations.*;
24 import org.eclipse.team.internal.ccvs.ui.tags.*;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.actions.NewProjectAction;
28
29 /**
30  * @author Administrator
31  *
32  * To change the template for this generated type comment go to
33  * Window>Preferences>Java>Code Generation>Code and Comments
34  */

35 public class CheckoutAsWizard extends Wizard {
36     
37     private ICVSRemoteFolder[] remoteFolders;
38     private boolean allowProjectConfiguration;
39
40     private CheckoutAsMainPage mainPage;
41     private CheckoutAsProjectSelectionPage projectSelectionPage;
42     private CheckoutAsLocationSelectionPage locationSelectionPage;
43     private TagSelectionWizardPage tagSelectionPage;
44     private IWorkbenchPart part;
45
46     class NewProjectListener implements IResourceChangeListener {
47         private IProject newProject = null;
48         /**
49          * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
50          */

51         public void resourceChanged(IResourceChangeEvent event) {
52             IResourceDelta root = event.getDelta();
53             IResourceDelta[] projectDeltas = root.getAffectedChildren();
54             for (int i = 0; i < projectDeltas.length; i++) {
55                 IResourceDelta delta = projectDeltas[i];
56                 IResource resource = delta.getResource();
57                 if (delta.getKind() == IResourceDelta.ADDED) {
58                     newProject = (IProject)resource;
59                 }
60             }
61         }
62         /**
63          * Gets the newProject.
64          * @return Returns a IProject
65          */

66         public IProject getNewProject() {
67             return newProject;
68         }
69     }
70     
71     /**
72      * Return the settings used for all CheckoutAsWizard pages
73      */

74     public static IDialogSettings getCheckoutAsDialogSettings() {
75         IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
76         IDialogSettings section = workbenchSettings.getSection("CheckoutAsWizard");//$NON-NLS-1$
77
if (section == null) {
78             section = workbenchSettings.addNewSection("CheckoutAsWizard");//$NON-NLS-1$
79
}
80         return section;
81     }
82     
83     public CheckoutAsWizard(IWorkbenchPart part, ICVSRemoteFolder[] remoteFolders, boolean allowProjectConfiguration) {
84         this.part = part;
85         this.remoteFolders = remoteFolders;
86         setDialogSettings(getCheckoutAsDialogSettings());
87         setWindowTitle(CVSUIMessages.CheckoutAsWizard_title);
88         this.allowProjectConfiguration = allowProjectConfiguration;
89     }
90     
91     /**
92      * @see org.eclipse.jface.wizard.IWizard#addPages()
93      */

94     public void addPages() {
95         setNeedsProgressMonitor(true);
96         ImageDescriptor substImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_CHECKOUT);
97         
98         mainPage = new CheckoutAsMainPage(substImage, remoteFolders, allowProjectConfiguration);
99         addPage(mainPage);
100         
101         projectSelectionPage = new CheckoutAsProjectSelectionPage(substImage, remoteFolders);
102         addPage(projectSelectionPage);
103         
104         locationSelectionPage = new CheckoutAsLocationSelectionPage(substImage, remoteFolders);
105         addPage(locationSelectionPage);
106         
107         tagSelectionPage = new TagSelectionWizardPage("tagPage", CVSUIMessages.CheckoutAsWizard_3, substImage, CVSUIMessages.CheckoutAsWizard_4, TagSource.create(remoteFolders), //$NON-NLS-1$
108
TagSelectionArea.INCLUDE_HEAD_TAG |
109                 TagSelectionArea.INCLUDE_BRANCHES |
110                 TagSelectionArea.INCLUDE_VERSIONS |
111                 TagSelectionArea.INCLUDE_DATES
112                 );
113         if (remoteFolders.length > 0) {
114             try {
115                 CVSTag selectedTag = remoteFolders[0].getFolderSyncInfo().getTag();
116                 tagSelectionPage.setSelection(selectedTag);
117             } catch (CVSException e) {
118                 CVSUIPlugin.log(e);
119             }
120         }
121         tagSelectionPage.setHelpContxtId(IHelpContextIds.CHECKOUT_TAG_SELETION_PAGE);
122         addPage(tagSelectionPage);
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.jface.wizard.IWizard#performFinish()
127      */

128     public boolean performFinish() {
129         try {
130             if (mainPage.isPerformConfigure()) {
131                 return performConfigureAndCheckout();
132             } else if (mainPage.isPerformCheckoutAs()) {
133                 if (isSingleFolder()) {
134                     return performSingleCheckoutAs();
135                 } else {
136                     return performMultipleCheckoutAs();
137                 }
138             } else if (mainPage.isPerformCheckoutInto()) {
139                 return performCheckoutInto();
140             }
141         } catch (InvocationTargetException JavaDoc e) {
142             handle(e);
143             // drop through
144
} catch (InterruptedException JavaDoc e) {
145             // drop through
146
}
147         return false;
148     }
149
150     /**
151      * @return
152      */

153     private boolean isSingleFolder() {
154         return remoteFolders.length == 1;
155     }
156     
157     /* (non-Javadoc)
158      * @see org.eclipse.jface.wizard.IWizard#canFinish()
159      */

160     public boolean canFinish() {
161         return (mainPage.isPageComplete()
162         && (mainPage.isPerformConfigure()
163             || (mainPage.isPerformCheckoutInto() && projectSelectionPage.isPageComplete())
164             || (mainPage.isPerformCheckoutAs() && locationSelectionPage.isPageComplete())));
165     }
166
167     /* (non-Javadoc)
168      * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
169      */

170     public IWizardPage getNextPage(IWizardPage page) {
171         if (page == mainPage) {
172             if (mainPage.isPerformConfigure()) return tagSelectionPage;
173             if (mainPage.isPerformCheckoutInto()) return projectSelectionPage;
174             if (mainPage.isPerformCheckoutAs()) {
175                 if (isSingleFolder()) {
176                     locationSelectionPage.setProjectName(mainPage.getProjectName());
177                 } else {
178                     locationSelectionPage.setProject(null);
179                 }
180                 return locationSelectionPage;
181             }
182         }
183         // The tag selection page is always shown as the last page
184
if (page != tagSelectionPage) {
185             return tagSelectionPage;
186         }
187         return null;
188     }
189     
190     private void handle(Throwable JavaDoc e) {
191         CVSUIPlugin.openError(getShell(), CVSUIMessages.CheckoutAsWizard_error, null, e);
192     }
193     
194     /*
195      * Configure a local project and checkout the selected remote folder into the project.
196      * This only occurs for single folders.
197      */

198     private boolean performConfigureAndCheckout() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
199         IProject newProject = getNewProject();
200         if (newProject == null) return false;
201         // Run the checkout in the background
202
ICVSRemoteFolder folder = getRemoteFolder();
203         new CheckoutSingleProjectOperation(part, folder, newProject, null, true).run();
204         return true;
205     }
206     
207     /*
208      * Return the single remote folder to be checked out
209      */

210     private ICVSRemoteFolder getRemoteFolder() {
211         ICVSRemoteFolder folder = remoteFolders[0];
212         CVSTag selectedTag = getSelectedTag();
213         if (selectedTag != null) {
214             folder = (ICVSRemoteFolder)folder.forTag(selectedTag);
215         }
216         return folder;
217     }
218     
219     /*
220      * Return the remote folders to be checked out
221      */

222     private ICVSRemoteFolder[] getRemoteFolders() {
223         ICVSRemoteFolder[] folders = (ICVSRemoteFolder[]) remoteFolders.clone();
224         CVSTag selectedTag = getSelectedTag();
225         // see bug 160851
226
if(selectedTag != null){
227             for (int i = 0; i < remoteFolders.length; i++) {
228                 folders[i] = (ICVSRemoteFolder)folders[i].forTag(getSelectedTag());
229             }
230         }
231         return folders;
232     }
233
234     /*
235      * Return the remote folders to be checked out with
236      * Folder description if available based on preferrences settings
237      */

238     private ICVSRemoteFolder[] getRemoteFoldersWithProjectDescriptions() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
239         ICVSRemoteFolder[] folders = getRemoteFolders();
240         if (CVSUIPlugin.getPlugin().isUseProjectNameOnCheckout()) {
241             folders = ProjectMetaFileOperation.updateFoldersWithProjectName(part, folders);
242         }
243         return folders;
244     }
245
246     private CVSTag getSelectedTag() {
247         return tagSelectionPage.getSelectedTag();
248     }
249
250     private boolean performSingleCheckoutAs() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
251         IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(mainPage.getProjectName());
252         String JavaDoc targetLocation = locationSelectionPage.getTargetLocation();
253         // Run the checkout in the background
254
ICVSRemoteFolder folder = getRemoteFolder();
255         final boolean recurse = mainPage.isRecurse();
256         if (mainPage.shouldAddToWorkingSet()){
257             new CheckoutSingleProjectOperation(part, folder, newProject, targetLocation, false, mainPage.getWorkingSetName()) {
258                 protected boolean isRecursive() {
259                     return recurse;
260                 }
261             }.run();
262         } else {
263             new CheckoutSingleProjectOperation(part, folder, newProject, targetLocation, false) {
264                 protected boolean isRecursive() {
265                     return recurse;
266                 }
267             }.run();
268         }
269         return true;
270     }
271
272     /**
273      * Check out multiple folders to the workspace using a custom location if one is
274      * specified.
275      */

276     private boolean performMultipleCheckoutAs() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
277         String JavaDoc targetLocation = locationSelectionPage.getTargetLocation();
278         if (mainPage.shouldAddToWorkingSet()){
279             //Run the checkout in the background
280
new CheckoutMultipleProjectsOperation(part, getRemoteFoldersWithProjectDescriptions(), targetLocation, mainPage.getWorkingSetName()).run();
281         } else {
282             // Run the checkout in the background
283
new CheckoutMultipleProjectsOperation(part, getRemoteFoldersWithProjectDescriptions(), targetLocation).run();
284         }
285         return true;
286     }
287
288     private boolean performCheckoutInto() throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
289         CheckoutIntoOperation operation;
290         boolean recursive = mainPage.isRecurse();
291         if (isSingleFolder()) {
292             ICVSRemoteFolder folder = getRemoteFolder();
293             operation = new CheckoutIntoOperation(part, folder, projectSelectionPage.getLocalFolder(), recursive);
294         } else {
295             operation = new CheckoutIntoOperation(part, getRemoteFolders(), projectSelectionPage.getParentFolder(), recursive);
296         }
297         // Run the checkout in the background
298
operation.run();
299         return true;
300     }
301
302     /**
303      * Get a new project that is configured by the new project wizard.
304      * This is currently the only way to do this.
305      */

306     private IProject getNewProject() {
307         NewProjectListener listener = new NewProjectListener();
308         ResourcesPlugin.getWorkspace().addResourceChangeListener(listener, IResourceChangeEvent.POST_CHANGE);
309         (new NewProjectAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow())).run();
310         ResourcesPlugin.getWorkspace().removeResourceChangeListener(listener);
311         IProject project = listener.getNewProject();
312         return project;
313     }
314
315 }
316
Popular Tags