1 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 38 public class OpenWorkspaceAction extends Action implements 39 ActionFactory.IWorkbenchAction { 40 41 48 class OpenDialogAction extends Action { 49 50 OpenDialogAction() { 51 super(IDEWorkbenchMessages.OpenWorkspaceAction_other); 52 setToolTipText(IDEWorkbenchMessages.OpenWorkspaceAction_toolTip); 53 } 54 55 60 public void run() { 61 OpenWorkspaceAction.this.run(); 62 } 63 } 64 65 70 class WorkspaceMRUAction extends Action { 71 72 private ChooseWorkspaceData data; 73 74 private String location; 75 76 WorkspaceMRUAction(String location, ChooseWorkspaceData data) { 77 this.location = location; setText(location); 82 setToolTipText(location); 83 this.data = data; 84 } 85 86 91 public void run() { 92 data.workspaceSelected(location); 93 data.writePersistedData(); 94 restart(location); 95 } 96 } 97 98 private static final String PROP_VM = "eclipse.vm"; 100 private static final String PROP_VMARGS = "eclipse.vmargs"; 102 private static final String PROP_COMMANDS = "eclipse.commands"; 104 private static final String PROP_EXIT_CODE = "eclipse.exitcode"; 106 private static final String PROP_EXIT_DATA = "eclipse.exitdata"; 108 private static final String CMD_DATA = "-data"; 110 private static final String CMD_VMARGS = "-vmargs"; 112 private static final String NEW_LINE = "\n"; 114 private IWorkbenchWindow window; 115 116 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 (); 129 } 130 131 133 this.window = window; 134 setToolTipText(IDEWorkbenchMessages.OpenWorkspaceAction_toolTip); 135 setActionDefinitionId("org.eclipse.ui.file.openWorkspace"); setMenuCreator(new IMenuCreator() { 137 private MenuManager dropDownMenuMgr; 138 139 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 current = data.getInitialDefault(); 149 String [] 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 168 public Menu getMenu(Control parent) { 169 createDropDownMenuMgr(); 170 return dropDownMenuMgr.createContextMenu(parent); 171 } 172 173 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 199 public void dispose() { 200 if (dropDownMenuMgr != null) { 201 dropDownMenuMgr.dispose(); 202 dropDownMenuMgr = null; 203 } 204 } 205 }); 206 } 207 208 213 public void run() { 214 String path = promptForWorkspace(); 215 if (path == null) { 216 return; 217 } 218 219 restart(path); 220 } 221 222 229 private void restart(String path) { 230 String 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 245 private String promptForWorkspace() { 246 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 String selection = data.getSelection(); 255 if (selection == null) { 256 return null; 257 } 258 259 data.writePersistedData(); 261 return selection; 262 } 263 264 273 private String buildCommandLine(String workspace) { 274 String 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 result = new StringBuffer (512); 288 result.append(property); 289 result.append(NEW_LINE); 290 291 String vmargs = System.getProperty(PROP_VMARGS); 293 if (vmargs != null) { 294 result.append(vmargs); 295 } 296 297 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 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 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 338 public void dispose() { 339 window = null; 340 } 341 } 342 | Popular Tags |