001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.actions;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.net.URI;
014:
015: import org.eclipse.core.commands.ExecutionException;
016: import org.eclipse.core.filesystem.URIUtil;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.jface.operation.IRunnableWithProgress;
022: import org.eclipse.osgi.util.NLS;
023: import org.eclipse.swt.widgets.Shell;
024: import org.eclipse.ui.PlatformUI;
025: import org.eclipse.ui.dialogs.ProjectLocationMoveDialog;
026: import org.eclipse.ui.ide.undo.MoveProjectOperation;
027: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
029: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
030: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
031: import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
032:
033: /**
034: * The MoveProjectAction is the action designed to move projects specifically as
035: * they have different semantics from other resources.
036: */
037: public class MoveProjectAction extends CopyProjectAction {
038: private static String MOVE_TOOL_TIP = IDEWorkbenchMessages.MoveProjectAction_toolTip;
039:
040: private static String MOVE_TITLE = IDEWorkbenchMessages.MoveProjectAction_text;
041:
042: private static String PROBLEMS_TITLE = IDEWorkbenchMessages.MoveProjectAction_dialogTitle;
043:
044: /**
045: * The id of this action.
046: */
047: public static final String ID = PlatformUI.PLUGIN_ID
048: + ".MoveProjectAction";//$NON-NLS-1$
049:
050: /**
051: * Creates a new project move action with the given text.
052: *
053: * @param shell
054: * the shell for any dialogs
055: */
056: public MoveProjectAction(Shell shell) {
057: super (shell, MOVE_TITLE);
058: setToolTipText(MOVE_TOOL_TIP);
059: setId(MoveProjectAction.ID);
060: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
061: IIDEHelpContextIds.MOVE_PROJECT_ACTION);
062: }
063:
064: /**
065: * Return the title of the errors dialog.
066: *
067: * @return java.lang.String
068: *
069: * @deprecated As of 3.3, the error handling is performed by the undoable
070: * operation which handles the move.
071: */
072: protected String getErrorsTitle() {
073: return PROBLEMS_TITLE;
074: }
075:
076: /**
077: * Moves the project to the new values.
078: *
079: * @param project
080: * the project to move
081: * @param newLocation
082: * URI
083: * @return <code>true</code> if the copy operation completed, and
084: * <code>false</code> if it was abandoned part way
085: */
086: boolean performMove(final IProject project, final URI newLocation) {
087:
088: IRunnableWithProgress op = new IRunnableWithProgress() {
089: public void run(IProgressMonitor monitor) {
090: MoveProjectOperation op = new MoveProjectOperation(
091: project,
092: newLocation,
093: IDEWorkbenchMessages.MoveProjectAction_moveTitle);
094: op.setModelProviderIds(getModelProviderIds());
095: try {
096: PlatformUI.getWorkbench().getOperationSupport()
097: .getOperationHistory().execute(
098: op,
099: monitor,
100: WorkspaceUndoUtil
101: .getUIInfoAdapter(shell));
102: } catch (ExecutionException e) {
103: if (e.getCause() instanceof CoreException) {
104: recordError((CoreException) e.getCause());
105: } else {
106: IDEWorkbenchPlugin.log(e.getMessage(), e);
107: displayError(e.getMessage());
108: }
109: }
110: }
111: };
112:
113: try {
114: new ProgressMonitorJobsDialog(shell).run(true, true, op);
115: } catch (InterruptedException e) {
116: return false;
117: } catch (InvocationTargetException e) {
118: // CoreExceptions are collected by the operation, but unexpected runtime
119: // exceptions and errors may still occur.
120: IDEWorkbenchPlugin.log(getClass(),
121: "performMove()", e.getTargetException()); //$NON-NLS-1$
122: displayError(NLS
123: .bind(
124: IDEWorkbenchMessages.MoveProjectAction_internalError,
125: e.getTargetException().getMessage()));
126: return false;
127: }
128:
129: return true;
130: }
131:
132: /**
133: * Query for a new project destination using the parameters in the existing
134: * project.
135: *
136: * @return Object[] or null if the selection is cancelled
137: * @param project
138: * the project we are going to move.
139: */
140: protected Object[] queryDestinationParameters(IProject project) {
141: ProjectLocationMoveDialog dialog = new ProjectLocationMoveDialog(
142: shell, project);
143: dialog
144: .setTitle(IDEWorkbenchMessages.MoveProjectAction_moveTitle);
145: dialog.open();
146: return dialog.getResult();
147: }
148:
149: /**
150: * Implementation of method defined on <code>IAction</code>.
151: */
152: public void run() {
153:
154: errorStatus = null;
155:
156: IProject project = (IProject) getSelectedResources().get(0);
157:
158: //Get the project name and location
159: Object[] destinationPaths = queryDestinationParameters(project);
160: if (destinationPaths == null) {
161: return;
162: }
163:
164: // Ideally we would have gotten the URI directly from the
165: // ProjectLocationDialog, but for backward compatibility, we
166: // use the raw string and map back to a URI.
167: URI newLocation = URIUtil.toURI((String) destinationPaths[1]);
168:
169: boolean completed = performMove(project, newLocation);
170:
171: if (!completed) {
172: return; // not appropriate to show errors
173: }
174:
175: // If errors occurred, open an Error dialog
176: if (errorStatus != null) {
177: ErrorDialog.openError(this.shell, PROBLEMS_TITLE, null,
178: errorStatus);
179: errorStatus = null;
180: }
181: }
182: }
|