Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bda16e63e95c092d7e2a5a007efee492c01a4c0 (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
/*******************************************************************************
 * 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
 *     Philippe Ombredanne - bug 84808
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.*;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.operations.*;
import org.eclipse.team.internal.ccvs.ui.tags.*;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.NewProjectAction;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CheckoutAsWizard extends Wizard {
	
	private ICVSRemoteFolder[] remoteFolders;
	private boolean allowProjectConfiguration;

	private CheckoutAsMainPage mainPage;
	private CheckoutAsProjectSelectionPage projectSelectionPage;
	private CheckoutAsLocationSelectionPage locationSelectionPage;
	private TagSelectionWizardPage tagSelectionPage;
	private IWorkbenchPart part;

	class NewProjectListener implements IResourceChangeListener {
		private IProject newProject = null;
		/**
		 * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
		 */
		@Override
		public void resourceChanged(IResourceChangeEvent event) {
			IResourceDelta root = event.getDelta();
			IResourceDelta[] projectDeltas = root.getAffectedChildren();
			for (int i = 0; i < projectDeltas.length; i++) {							
				IResourceDelta delta = projectDeltas[i];
				IResource resource = delta.getResource();
				if (delta.getKind() == IResourceDelta.ADDED) {
					newProject = (IProject)resource;
				}
			}
		}
		/**
		 * Gets the newProject.
		 * @return Returns a IProject
		 */
		public IProject getNewProject() {
			return newProject;
		}
	}
	
    /**
     * Return the settings used for all CheckoutAsWizard pages
     */
    public static IDialogSettings getCheckoutAsDialogSettings() {
        IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
        IDialogSettings section = workbenchSettings.getSection("CheckoutAsWizard");//$NON-NLS-1$
        if (section == null) {
            section = workbenchSettings.addNewSection("CheckoutAsWizard");//$NON-NLS-1$
        }
        return section;
    }
    
	public CheckoutAsWizard(IWorkbenchPart part, ICVSRemoteFolder[] remoteFolders, boolean allowProjectConfiguration) {
		this.part = part;
		this.remoteFolders = remoteFolders;
        setDialogSettings(getCheckoutAsDialogSettings());
		setWindowTitle(CVSUIMessages.CheckoutAsWizard_title); 
		this.allowProjectConfiguration = allowProjectConfiguration;
	}
	
	/**
	 * @see org.eclipse.jface.wizard.IWizard#addPages()
	 */
	@Override
	public void addPages() {
		setNeedsProgressMonitor(true);
		ImageDescriptor substImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_CHECKOUT);
		
		mainPage = new CheckoutAsMainPage(substImage, remoteFolders, allowProjectConfiguration);
		addPage(mainPage);
		
		projectSelectionPage = new CheckoutAsProjectSelectionPage(substImage, remoteFolders);
		addPage(projectSelectionPage);
		
		locationSelectionPage = new CheckoutAsLocationSelectionPage(substImage, remoteFolders);
		addPage(locationSelectionPage);
		
		tagSelectionPage = new TagSelectionWizardPage("tagPage", CVSUIMessages.CheckoutAsWizard_3, substImage, CVSUIMessages.CheckoutAsWizard_4, TagSource.create(remoteFolders), //$NON-NLS-1$    
		        TagSelectionArea.INCLUDE_HEAD_TAG |
		        TagSelectionArea.INCLUDE_BRANCHES |
		        TagSelectionArea.INCLUDE_VERSIONS |
		        TagSelectionArea.INCLUDE_DATES
		        );
		if (remoteFolders.length > 0) {
			try {
				CVSTag selectedTag = remoteFolders[0].getFolderSyncInfo().getTag();
				tagSelectionPage.setSelection(selectedTag);
			} catch (CVSException e) {
				CVSUIPlugin.log(e);
			}
		}
		tagSelectionPage.setHelpContxtId(IHelpContextIds.CHECKOUT_TAG_SELETION_PAGE);
		addPage(tagSelectionPage);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.IWizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		try {
			if (mainPage.isPerformConfigure()) {
				return performConfigureAndCheckout();
			} else if (mainPage.isPerformCheckoutAs()) {
				if (isSingleFolder()) {
					return performSingleCheckoutAs();
				} else {
					return performMultipleCheckoutAs();
				}
			} else if (mainPage.isPerformCheckoutInto()) {
				return performCheckoutInto();
			}
		} catch (InvocationTargetException e) {
			handle(e);
			// drop through
		} catch (InterruptedException e) {
			// drop through
		}
		return false;
	}

	/**
	 * @return
	 */
	private boolean isSingleFolder() {
		return remoteFolders.length == 1;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.IWizard#canFinish()
	 */
	@Override
	public boolean canFinish() {
		return (mainPage.isPageComplete() 
		&& (mainPage.isPerformConfigure()
			|| (mainPage.isPerformCheckoutInto() && projectSelectionPage.isPageComplete()) 
			|| (mainPage.isPerformCheckoutAs() && locationSelectionPage.isPageComplete())));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
	 */
	@Override
	public IWizardPage getNextPage(IWizardPage page) {
		if (page == mainPage) {
			if (mainPage.isPerformConfigure()) return tagSelectionPage;
			if (mainPage.isPerformCheckoutInto()) return projectSelectionPage;
			if (mainPage.isPerformCheckoutAs()) {
				if (isSingleFolder()) {
					locationSelectionPage.setProjectName(mainPage.getProjectName());
				} else {
					locationSelectionPage.setProject(null);
				}
				return locationSelectionPage; 
			} 
		}
		// The tag selection page is always shown as the last page
		if (page != tagSelectionPage) {
			return tagSelectionPage;
		}
		return null;
	}
	
	private void handle(Throwable e) {
		CVSUIPlugin.openError(getShell(), CVSUIMessages.CheckoutAsWizard_error, null, e); 
	}
	
	/*
	 * Configure a local project and checkout the selected remote folder into the project.
	 * This only occurs for single folders.
	 */
	private boolean performConfigureAndCheckout() throws InvocationTargetException, InterruptedException {
		IProject newProject = getNewProject();
		if (newProject == null) return false;
		// Run the checkout in the background
		ICVSRemoteFolder folder = getRemoteFolder();
		new CheckoutSingleProjectOperation(part, folder, newProject, null, true).run();
		return true;
	}
	
	/*
	 * Return the single remote folder to be checked out
	 */
	private ICVSRemoteFolder getRemoteFolder() {
		ICVSRemoteFolder folder = remoteFolders[0];
		CVSTag selectedTag = getSelectedTag();
		if (selectedTag != null) {
			folder = (ICVSRemoteFolder)folder.forTag(selectedTag);
		}
		return folder;
	}
	
	/*
	 * Return the remote folders to be checked out
	 */
	private ICVSRemoteFolder[] getRemoteFolders() {
		ICVSRemoteFolder[] folders = remoteFolders.clone();
		CVSTag selectedTag = getSelectedTag();
		// see bug 160851
		if(selectedTag != null){
			for (int i = 0; i < remoteFolders.length; i++) {
				folders[i] = (ICVSRemoteFolder)folders[i].forTag(getSelectedTag());
			}
		}
		return folders;
	}

	/*
	 * Return the remote folders to be checked out with 
	 * Folder description if available based on preferrences settings
	 */
	private ICVSRemoteFolder[] getRemoteFoldersWithProjectDescriptions() throws InvocationTargetException, InterruptedException {
		ICVSRemoteFolder[] folders = getRemoteFolders();
		if (CVSUIPlugin.getPlugin().isUseProjectNameOnCheckout()) {
			folders = ProjectMetaFileOperation.updateFoldersWithProjectName(part, folders);
		}
		return folders;
	}

	private CVSTag getSelectedTag() {
		return tagSelectionPage.getSelectedTag();
	}

	private boolean performSingleCheckoutAs() throws InvocationTargetException, InterruptedException {
		IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(mainPage.getProjectName());
		String targetLocation = locationSelectionPage.getTargetLocation();
		// Run the checkout in the background
		ICVSRemoteFolder folder = getRemoteFolder();
		final boolean recurse = mainPage.isRecurse();
		new CheckoutSingleProjectOperation(part, folder, newProject, targetLocation, false, mainPage.getWorkingSets()) {
				@Override
				protected boolean isRecursive() {
					return recurse;
				}
			}.run();
		return true;
	}

	/**
	 * Check out multiple folders to the workspace using a custom location if one is
	 * specified.
	 */
	private boolean performMultipleCheckoutAs() throws InvocationTargetException, InterruptedException {
		String targetLocation = locationSelectionPage.getTargetLocation();
		// Run the checkout in the background
		new CheckoutMultipleProjectsOperation(part, getRemoteFoldersWithProjectDescriptions(), targetLocation, mainPage.getWorkingSets()).run();
		return true;
	}

	private boolean performCheckoutInto() throws InvocationTargetException, InterruptedException {
		CheckoutIntoOperation operation;
		boolean recursive = mainPage.isRecurse();
		if (isSingleFolder()) {
			ICVSRemoteFolder folder = getRemoteFolder();
			operation = new CheckoutIntoOperation(part, folder, projectSelectionPage.getLocalFolder(), recursive);
		} else {
			operation = new CheckoutIntoOperation(part, getRemoteFolders(), projectSelectionPage.getParentFolder(), recursive);
		}
		// Run the checkout in the background
		operation.run();
		return true;
	}

	/**
	 * Get a new project that is configured by the new project wizard.
	 * This is currently the only way to do this.
	 */
	private IProject getNewProject() {
		NewProjectListener listener = new NewProjectListener();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(listener, IResourceChangeEvent.POST_CHANGE);
		(new NewProjectAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow())).run();
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(listener);
		IProject project = listener.getNewProject();
		return project;
	}

}

Back to the top