Skip to main content
summaryrefslogtreecommitdiffstats
blob: 12eeae941ed1b3a036b609c572c7521240d80f67 (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
package org.eclipse.team.internal.ccvs.ui.actions;

/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ccvs.ui.PromptingDialog;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.dialogs.ProjectLocationSelectionDialog;

/**
 * Add a remote resource to the workspace. Current implementation:
 * -Works only for remote folders
 * -Does not prompt for project name; uses folder name instead
 */
public class CheckoutAsAction extends AddToWorkspaceAction {
	/*
	 * @see IActionDelegate#run(IAction)
	 */
	public void execute(IAction action) {
		run(new WorkspaceModifyOperation() {
			public void execute(IProgressMonitor monitor) throws InterruptedException, InvocationTargetException {
				try {
					final Shell shell = getShell();
					ICVSRemoteFolder[] folders = getSelectedRemoteFolders();
					if (folders.length != 1) return;
					String name = folders[0].getName();
					// Prompt for name
					final int[] result = new int[] { Dialog.OK };
					IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
					final ProjectLocationSelectionDialog dialog = new ProjectLocationSelectionDialog(shell, project);
					dialog.setTitle(Policy.bind("CheckoutAsAction.enterProjectTitle", name)); //$NON-NLS-1$

					shell.getDisplay().syncExec(new Runnable() {
						public void run() {
							result[0] = dialog.open();
						}
					});
					if (result[0] != Dialog.OK) return;

					Object[] destinationPaths = dialog.getResult();
					if (destinationPaths == null) return;
					String newName = (String) destinationPaths[0];
					IPath newLocation = new Path((String) destinationPaths[1]);

					// prompt if the project exists locally
					project = ResourcesPlugin.getWorkspace().getRoot().getProject(newName);
					PromptingDialog prompt = new PromptingDialog(getShell(), new IResource[] { project },
						getOverwriteLocalAndFileSystemPrompt(), Policy.bind("ReplaceWithAction.confirmOverwrite"));//$NON-NLS-1$
					if (prompt.promptForMultiple().length == 0) return;

					monitor.beginTask(null, 100);
					monitor.setTaskName(Policy.bind("CheckoutAsAction.taskname", name, newName)); //$NON-NLS-1$

					// create the project
					try {
						if (newLocation.equals(Platform.getLocation())) {
							// create in default location
							project.create(Policy.subMonitorFor(monitor, 3));
						} else {
							// create in some other location
							IProjectDescription desc = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName());
							desc.setLocation(newLocation);
							project.create(desc, Policy.subMonitorFor(monitor, 3));
						}
						project.open(Policy.subMonitorFor(monitor, 2));
					} catch (CoreException e) {
						throw CVSException.wrapException(e);
					}

					CVSProviderPlugin.getProvider().checkout(folders, new IProject[] { project }, Policy.subMonitorFor(monitor, 95));
				} catch (TeamException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		}, Policy.bind("CheckoutAsAction.checkoutFailed"), this.PROGRESS_DIALOG); //$NON-NLS-1$
	}
	
	/*
	 * @see TeamAction#isEnabled()
	 */
	protected boolean isEnabled() throws TeamException {
		return getSelectedRemoteFolders().length == 1;
	}
}

Back to the top