KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > MoveProjectAction


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.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.net.URI JavaDoc;
15
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.filesystem.URIUtil;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.dialogs.ProjectLocationMoveDialog;
27 import org.eclipse.ui.ide.undo.MoveProjectOperation;
28 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
31 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
32 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
33
34 /**
35  * The MoveProjectAction is the action designed to move projects specifically as
36  * they have different semantics from other resources.
37  */

38 public class MoveProjectAction extends CopyProjectAction {
39     private static String JavaDoc MOVE_TOOL_TIP = IDEWorkbenchMessages.MoveProjectAction_toolTip;
40
41     private static String JavaDoc MOVE_TITLE = IDEWorkbenchMessages.MoveProjectAction_text;
42
43     private static String JavaDoc PROBLEMS_TITLE = IDEWorkbenchMessages.MoveProjectAction_dialogTitle;
44
45     /**
46      * The id of this action.
47      */

48     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".MoveProjectAction";//$NON-NLS-1$
49

50     /**
51      * Creates a new project move action with the given text.
52      *
53      * @param shell
54      * the shell for any dialogs
55      */

56     public MoveProjectAction(Shell shell) {
57         super(shell, MOVE_TITLE);
58         setToolTipText(MOVE_TOOL_TIP);
59         setId(MoveProjectAction.ID);
60         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
61                 IIDEHelpContextIds.MOVE_PROJECT_ACTION);
62     }
63
64     /**
65      * Return the title of the errors dialog.
66      *
67      * @return java.lang.String
68      *
69      * @deprecated As of 3.3, the error handling is performed by the undoable
70      * operation which handles the move.
71      */

72     protected String JavaDoc getErrorsTitle() {
73         return PROBLEMS_TITLE;
74     }
75
76     /**
77      * Moves the project to the new values.
78      *
79      * @param project
80      * the project to move
81      * @param newLocation
82      * URI
83      * @return <code>true</code> if the copy operation completed, and
84      * <code>false</code> if it was abandoned part way
85      */

86     boolean performMove(final IProject project,
87             final URI JavaDoc newLocation) {
88         
89         IRunnableWithProgress op = new IRunnableWithProgress() {
90             public void run(IProgressMonitor monitor) {
91                 MoveProjectOperation op = new MoveProjectOperation(project, newLocation, IDEWorkbenchMessages.MoveProjectAction_moveTitle);
92                 op.setModelProviderIds(getModelProviderIds());
93                 try {
94                     PlatformUI.getWorkbench().getOperationSupport()
95                             .getOperationHistory().execute(op, monitor,
96                                     WorkspaceUndoUtil.getUIInfoAdapter(shell));
97                 } catch (ExecutionException e) {
98                     if (e.getCause() instanceof CoreException) {
99                         recordError((CoreException)e.getCause());
100                     } else {
101                         IDEWorkbenchPlugin.log(e.getMessage(), e);
102                         displayError(e.getMessage());
103                     }
104                 }
105             }
106         };
107         
108         try {
109             new ProgressMonitorJobsDialog(shell).run(true, true, op);
110         } catch (InterruptedException JavaDoc e) {
111             return false;
112         } catch (InvocationTargetException JavaDoc e) {
113             // CoreExceptions are collected by the operation, but unexpected runtime
114
// exceptions and errors may still occur.
115
IDEWorkbenchPlugin.log(getClass(),
116                     "performMove()", e.getTargetException()); //$NON-NLS-1$
117
displayError(NLS.bind(IDEWorkbenchMessages.MoveProjectAction_internalError, e.getTargetException().getMessage()));
118             return false;
119         }
120
121         return true;
122     }
123
124     /**
125      * Query for a new project destination using the parameters in the existing
126      * project.
127      *
128      * @return Object[] or null if the selection is cancelled
129      * @param project
130      * the project we are going to move.
131      */

132     protected Object JavaDoc[] queryDestinationParameters(IProject project) {
133         ProjectLocationMoveDialog dialog = new ProjectLocationMoveDialog(shell,
134                 project);
135         dialog.setTitle(IDEWorkbenchMessages.MoveProjectAction_moveTitle);
136         dialog.open();
137         return dialog.getResult();
138     }
139
140     /**
141      * Implementation of method defined on <code>IAction</code>.
142      */

143     public void run() {
144
145         errorStatus = null;
146
147         IProject project = (IProject) getSelectedResources().get(0);
148
149         //Get the project name and location
150
Object JavaDoc[] destinationPaths = queryDestinationParameters(project);
151         if (destinationPaths == null) {
152             return;
153         }
154
155         // Ideally we would have gotten the URI directly from the
156
// ProjectLocationDialog, but for backward compatibility, we
157
// use the raw string and map back to a URI.
158
URI JavaDoc newLocation = URIUtil.toURI((String JavaDoc)destinationPaths[1]);
159         
160         
161         boolean completed = performMove(project, newLocation);
162
163         if (!completed) {
164             return; // not appropriate to show errors
165
}
166
167         // If errors occurred, open an Error dialog
168
if (errorStatus != null) {
169             ErrorDialog
170                     .openError(this.shell, PROBLEMS_TITLE, null, errorStatus);
171             errorStatus = null;
172         }
173     }
174 }
175
Popular Tags