KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > OpenWorkspaceAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.internal.ide.actions;
12
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.ActionContributionItem;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.action.IMenuCreator;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.action.Separator;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.actions.ActionFactory;
27 import org.eclipse.ui.internal.ide.ChooseWorkspaceData;
28 import org.eclipse.ui.internal.ide.ChooseWorkspaceDialog;
29 import org.eclipse.ui.internal.ide.ChooseWorkspaceWithSettingsDialog;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
31
32 /**
33  * Implements the open workspace action. Opens a dialog prompting for a
34  * directory and then restarts the IDE on that workspace.
35  *
36  * @since 3.0
37  */

38 public class OpenWorkspaceAction extends Action implements
39         ActionFactory.IWorkbenchAction {
40
41     /**
42      * Action responsible for opening the "Other..." dialog (ie: the workspace
43      * chooser).
44      *
45      * @since 3.3
46      *
47      */

48     class OpenDialogAction extends Action {
49
50         OpenDialogAction() {
51             super(IDEWorkbenchMessages.OpenWorkspaceAction_other);
52             setToolTipText(IDEWorkbenchMessages.OpenWorkspaceAction_toolTip);
53         }
54
55         /*
56          * (non-Javadoc)
57          *
58          * @see org.eclipse.jface.action.Action#run()
59          */

60         public void run() {
61             OpenWorkspaceAction.this.run();
62         }
63     }
64
65     /**
66      * Action responsible for opening a specific workspace location
67      *
68      * @since 3.3
69      */

70     class WorkspaceMRUAction extends Action {
71
72         private ChooseWorkspaceData data;
73
74         private String JavaDoc location;
75
76         WorkspaceMRUAction(String JavaDoc location, ChooseWorkspaceData data) {
77             this.location = location; // preserve the location directly -
78
// setText mucks with accelerators so we
79
// can't necessarily use it safely for
80
// manipulating the location later.
81
setText(location);
82             setToolTipText(location);
83             this.data = data;
84         }
85
86         /*
87          * (non-Javadoc)
88          *
89          * @see org.eclipse.jface.action.Action#run()
90          */

91         public void run() {
92             data.workspaceSelected(location);
93             data.writePersistedData();
94             restart(location);
95         }
96     }
97
98     private static final String JavaDoc PROP_VM = "eclipse.vm"; //$NON-NLS-1$
99

100     private static final String JavaDoc PROP_VMARGS = "eclipse.vmargs"; //$NON-NLS-1$
101

102     private static final String JavaDoc PROP_COMMANDS = "eclipse.commands"; //$NON-NLS-1$
103

104     private static final String JavaDoc PROP_EXIT_CODE = "eclipse.exitcode"; //$NON-NLS-1$
105

106     private static final String JavaDoc PROP_EXIT_DATA = "eclipse.exitdata"; //$NON-NLS-1$
107

108     private static final String JavaDoc CMD_DATA = "-data"; //$NON-NLS-1$
109

110     private static final String JavaDoc CMD_VMARGS = "-vmargs"; //$NON-NLS-1$
111

112     private static final String JavaDoc NEW_LINE = "\n"; //$NON-NLS-1$
113

114     private IWorkbenchWindow window;
115
116     /**
117      * Set definition for this action and text so that it will be used for File
118      * -> Open Workspace in the argument window.
119      *
120      * @param window
121      * the window in which this action should appear
122      */

123     public OpenWorkspaceAction(IWorkbenchWindow window) {
124         super(IDEWorkbenchMessages.OpenWorkspaceAction_text,
125                 IAction.AS_DROP_DOWN_MENU);
126
127         if (window == null) {
128             throw new IllegalArgumentException JavaDoc();
129         }
130
131         // TODO help?
132

133         this.window = window;
134         setToolTipText(IDEWorkbenchMessages.OpenWorkspaceAction_toolTip);
135         setActionDefinitionId("org.eclipse.ui.file.openWorkspace"); //$NON-NLS-1$
136
setMenuCreator(new IMenuCreator() {
137             private MenuManager dropDownMenuMgr;
138
139             /**
140              * Creates the menu manager for the drop-down.
141              */

142             private void createDropDownMenuMgr() {
143                 if (dropDownMenuMgr == null) {
144                     dropDownMenuMgr = new MenuManager();
145                     final ChooseWorkspaceData data = new ChooseWorkspaceData(
146                             Platform.getInstanceLocation().getURL());
147                     data.readPersistedData();
148                     String JavaDoc current = data.getInitialDefault();
149                     String JavaDoc[] workspaces = data.getRecentWorkspaces();
150                     for (int i = 0; i < workspaces.length; i++) {
151                         if (workspaces[i] != null
152                                 && !workspaces[i].equals(current)) {
153                             dropDownMenuMgr.add(new WorkspaceMRUAction(
154                                     workspaces[i], data));
155                         }
156                     }
157                     if (!dropDownMenuMgr.isEmpty())
158                         dropDownMenuMgr.add(new Separator());
159                     dropDownMenuMgr.add(new OpenDialogAction());
160                 }
161             }
162
163             /*
164              * (non-Javadoc)
165              *
166              * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
167              */

168             public Menu getMenu(Control parent) {
169                 createDropDownMenuMgr();
170                 return dropDownMenuMgr.createContextMenu(parent);
171             }
172
173             /*
174              * (non-Javadoc)
175              *
176              * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
177              */

178             public Menu getMenu(Menu parent) {
179                 createDropDownMenuMgr();
180                 Menu menu = new Menu(parent);
181                 IContributionItem[] items = dropDownMenuMgr.getItems();
182                 for (int i = 0; i < items.length; i++) {
183                     IContributionItem item = items[i];
184                     IContributionItem newItem = item;
185                     if (item instanceof ActionContributionItem) {
186                         newItem = new ActionContributionItem(
187                                 ((ActionContributionItem) item).getAction());
188                     }
189                     newItem.fill(menu, -1);
190                 }
191                 return menu;
192             }
193
194             /*
195              * (non-Javadoc)
196              *
197              * @see org.eclipse.jface.action.IMenuCreator#dispose()
198              */

199             public void dispose() {
200                 if (dropDownMenuMgr != null) {
201                     dropDownMenuMgr.dispose();
202                     dropDownMenuMgr = null;
203                 }
204             }
205         });
206     }
207
208     /*
209      * (non-Javadoc)
210      *
211      * @see org.eclipse.jface.action.Action#run()
212      */

213     public void run() {
214         String JavaDoc path = promptForWorkspace();
215         if (path == null) {
216             return;
217         }
218
219         restart(path);
220     }
221
222     /**
223      * Restart the workbench using the specified path as the workspace location.
224      *
225      * @param path
226      * the location
227      * @since 3.3
228      */

229     private void restart(String JavaDoc path) {
230         String JavaDoc command_line = buildCommandLine(path);
231         if (command_line == null) {
232             return;
233         }
234
235         System.setProperty(PROP_EXIT_CODE, Integer.toString(24));
236         System.setProperty(PROP_EXIT_DATA, command_line);
237         window.getWorkbench().restart();
238     }
239
240     /**
241      * Use the ChooseWorkspaceDialog to get the new workspace from the user.
242      *
243      * @return a string naming the new workspace and null if cancel was selected
244      */

245     private String JavaDoc promptForWorkspace() {
246         // get the current workspace as the default
247
ChooseWorkspaceData data = new ChooseWorkspaceData(Platform
248                 .getInstanceLocation().getURL());
249         ChooseWorkspaceDialog dialog = new ChooseWorkspaceWithSettingsDialog(
250                 window.getShell(), data, true, false);
251         dialog.prompt(true);
252
253         // return null if the user changed their mind
254
String JavaDoc selection = data.getSelection();
255         if (selection == null) {
256             return null;
257         }
258
259         // otherwise store the new selection and return the selection
260
data.writePersistedData();
261         return selection;
262     }
263
264     /**
265      * Create and return a string with command line options for eclipse.exe that
266      * will launch a new workbench that is the same as the currently running
267      * one, but using the argument directory as its workspace.
268      *
269      * @param workspace
270      * the directory to use as the new workspace
271      * @return a string of command line options or null on error
272      */

273     private String JavaDoc buildCommandLine(String JavaDoc workspace) {
274         String JavaDoc property = System.getProperty(PROP_VM);
275         if (property == null) {
276             MessageDialog
277                     .openError(
278                             window.getShell(),
279                             IDEWorkbenchMessages.OpenWorkspaceAction_errorTitle,
280                             NLS
281                                     .bind(
282                                             IDEWorkbenchMessages.OpenWorkspaceAction_errorMessage,
283                                             PROP_VM));
284             return null;
285         }
286
287         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
288         result.append(property);
289         result.append(NEW_LINE);
290
291         // append the vmargs and commands. Assume that these already end in \n
292
String JavaDoc vmargs = System.getProperty(PROP_VMARGS);
293         if (vmargs != null) {
294             result.append(vmargs);
295         }
296
297         // append the rest of the args, replacing or adding -data as required
298
property = System.getProperty(PROP_COMMANDS);
299         if (property == null) {
300             result.append(CMD_DATA);
301             result.append(NEW_LINE);
302             result.append(workspace);
303             result.append(NEW_LINE);
304         } else {
305             // find the index of the arg to replace its value
306
int cmd_data_pos = property.lastIndexOf(CMD_DATA);
307             if (cmd_data_pos != -1) {
308                 cmd_data_pos += CMD_DATA.length() + 1;
309                 result.append(property.substring(0, cmd_data_pos));
310                 result.append(workspace);
311                 result.append(property.substring(property.indexOf('\n',
312                         cmd_data_pos)));
313             } else {
314                 result.append(CMD_DATA);
315                 result.append(NEW_LINE);
316                 result.append(workspace);
317                 result.append(NEW_LINE);
318                 result.append(property);
319             }
320         }
321
322         // put the vmargs back at the very end (the eclipse.commands property
323
// already contains the -vm arg)
324
if (vmargs != null) {
325             result.append(CMD_VMARGS);
326             result.append(NEW_LINE);
327             result.append(vmargs);
328         }
329
330         return result.toString();
331     }
332
333     /*
334      * (non-Javadoc)
335      *
336      * @see org.eclipse.jface.action.Action#dispose()
337      */

338     public void dispose() {
339         window = null;
340     }
341 }
342
Popular Tags