1 11 package org.eclipse.ui.internal.ide.application; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.OutputStream ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Properties ; 21 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.IExecutableExtension; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Platform; 26 import org.eclipse.core.runtime.Status; 27 import org.eclipse.equinox.app.IApplication; 28 import org.eclipse.equinox.app.IApplicationContext; 29 import org.eclipse.jface.dialogs.MessageDialog; 30 import org.eclipse.osgi.service.datalocation.Location; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.MessageBox; 35 import org.eclipse.swt.widgets.Shell; 36 import org.eclipse.ui.IWorkbench; 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.internal.ide.ChooseWorkspaceData; 39 import org.eclipse.ui.internal.ide.ChooseWorkspaceDialog; 40 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 41 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 42 import org.eclipse.ui.internal.ide.StatusUtil; 43 44 49 public class IDEApplication implements IApplication, IExecutableExtension { 50 51 54 public static final String METADATA_FOLDER = ".metadata"; 56 private static final String VERSION_FILENAME = "version.ini"; 58 private static final String WORKSPACE_VERSION_KEY = "org.eclipse.core.runtime"; 60 private static final String WORKSPACE_VERSION_VALUE = "1"; 62 private static final String PROP_EXIT_CODE = "eclipse.exitcode"; 64 68 private static final Integer EXIT_RELAUNCH = new Integer (24); 69 70 73 public static final String PLUGIN_ID = "org.eclipse.ui.ide.application"; 75 78 public IDEApplication() { 79 } 81 82 85 public Object start(IApplicationContext appContext) throws Exception { 86 Display display = createDisplay(); 87 88 try { 89 Shell shell = new Shell(display, SWT.ON_TOP); 90 91 try { 92 if (!checkInstanceLocation(shell)) { 93 Platform.endSplash(); 94 return EXIT_OK; 95 } 96 } finally { 97 if (shell != null) { 98 shell.dispose(); 99 } 100 } 101 102 int returnCode = PlatformUI.createAndRunWorkbench(display, 107 new IDEWorkbenchAdvisor()); 108 109 if (returnCode != PlatformUI.RETURN_RESTART) { 113 return EXIT_OK; 114 } 115 116 return EXIT_RELAUNCH.equals(Integer.getInteger(PROP_EXIT_CODE)) ? EXIT_RELAUNCH 119 : EXIT_RESTART; 120 } finally { 121 if (display != null) { 122 display.dispose(); 123 } 124 } 125 } 126 127 132 protected Display createDisplay() { 133 return PlatformUI.createDisplay(); 134 } 135 136 139 public void setInitializationData(IConfigurationElement config, 140 String propertyName, Object data) { 141 } 143 144 151 private boolean checkInstanceLocation(Shell shell) { 152 Location instanceLoc = Platform.getInstanceLocation(); 154 if (instanceLoc == null) { 155 MessageDialog 156 .openError( 157 shell, 158 IDEWorkbenchMessages.IDEApplication_workspaceMandatoryTitle, 159 IDEWorkbenchMessages.IDEApplication_workspaceMandatoryMessage); 160 return false; 161 } 162 163 if (instanceLoc.isSet()) { 165 if (!checkValidWorkspace(shell, instanceLoc.getURL())) { 168 return false; 169 } 170 171 try { 174 if (instanceLoc.lock()) { 175 writeWorkspaceVersion(); 176 return true; 177 } 178 179 File workspaceDirectory = new File (instanceLoc.getURL().getFile()); 184 if (workspaceDirectory.exists()) { 185 MessageDialog.openError( 186 shell, 187 IDEWorkbenchMessages.IDEApplication_workspaceCannotLockTitle, 188 IDEWorkbenchMessages.IDEApplication_workspaceCannotLockMessage); 189 } else { 190 MessageDialog.openError( 191 shell, 192 IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, 193 IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); 194 } 195 } catch (IOException e) { 196 IDEWorkbenchPlugin.log("Could not obtain lock for workspace location", e); 198 MessageDialog 199 .openError( 200 shell, 201 IDEWorkbenchMessages.InternalError, 202 e.getMessage()); 203 } 204 return false; 205 } 206 207 ChooseWorkspaceData launchData = new ChooseWorkspaceData(instanceLoc 209 .getDefault()); 210 211 boolean force = false; 212 while (true) { 213 URL workspaceUrl = promptForWorkspace(shell, launchData, force); 214 if (workspaceUrl == null) { 215 return false; 216 } 217 218 force = true; 221 222 try { 223 if (instanceLoc.setURL(workspaceUrl, true)) { 226 launchData.writePersistedData(); 227 writeWorkspaceVersion(); 228 return true; 229 } 230 } catch (IllegalStateException e) { 231 MessageDialog 232 .openError( 233 shell, 234 IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, 235 IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); 236 return false; 237 } 238 239 MessageDialog.openError(shell, IDEWorkbenchMessages.IDEApplication_workspaceInUseTitle, 242 IDEWorkbenchMessages.IDEApplication_workspaceInUseMessage); 243 } 244 } 245 246 260 private URL promptForWorkspace(Shell shell, ChooseWorkspaceData launchData, 261 boolean force) { 262 URL url = null; 263 do { 264 new ChooseWorkspaceDialog(null, launchData, false, true).prompt(force); 267 String instancePath = launchData.getSelection(); 268 if (instancePath == null) { 269 return null; 270 } 271 272 force = true; 276 277 if (instancePath.length() <= 0) { 279 MessageDialog 280 .openError( 281 shell, 282 IDEWorkbenchMessages.IDEApplication_workspaceEmptyTitle, 283 IDEWorkbenchMessages.IDEApplication_workspaceEmptyMessage); 284 continue; 285 } 286 287 File workspace = new File (instancePath); 289 if (!workspace.exists()) { 290 workspace.mkdir(); 291 } 292 293 try { 294 String path = workspace.getAbsolutePath().replace( 297 File.separatorChar, '/'); 298 url = new URL ("file", null, path); } catch (MalformedURLException e) { 300 MessageDialog 301 .openError( 302 shell, 303 IDEWorkbenchMessages.IDEApplication_workspaceInvalidTitle, 304 IDEWorkbenchMessages.IDEApplication_workspaceInvalidMessage); 305 continue; 306 } 307 } while (!checkValidWorkspace(shell, url)); 308 309 return url; 310 } 311 312 321 private boolean checkValidWorkspace(Shell shell, URL url) { 322 if (url == null) { 324 return false; 325 } 326 327 String version = readWorkspaceVersion(url); 328 329 if (version == null) { 333 return true; 334 } 335 336 final int ide_version = Integer.parseInt(WORKSPACE_VERSION_VALUE); 337 int workspace_version = Integer.parseInt(version); 338 339 if (workspace_version == ide_version) { 342 return true; 343 } 344 345 String title = IDEWorkbenchMessages.IDEApplication_versionTitle; 349 String message = NLS.bind(IDEWorkbenchMessages.IDEApplication_versionMessage, url.getFile()); 350 351 MessageBox mbox = new MessageBox(shell, SWT.OK | SWT.CANCEL 352 | SWT.ICON_WARNING | SWT.APPLICATION_MODAL); 353 mbox.setText(title); 354 mbox.setMessage(message); 355 return mbox.open() == SWT.OK; 356 } 357 358 362 private static String readWorkspaceVersion(URL workspace) { 363 File versionFile = getVersionFile(workspace, false); 364 if (versionFile == null || !versionFile.exists()) { 365 return null; 366 } 367 368 try { 369 Properties props = new Properties (); 373 FileInputStream is = new FileInputStream (versionFile); 374 try { 375 props.load(is); 376 } finally { 377 is.close(); 378 } 379 380 return props.getProperty(WORKSPACE_VERSION_KEY); 381 } catch (IOException e) { 382 IDEWorkbenchPlugin.log("Could not read version file", new Status( IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 384 IStatus.ERROR, 385 e.getMessage() == null ? "" : e.getMessage(), e)); 387 return null; 388 } 389 } 390 391 396 private static void writeWorkspaceVersion() { 397 Location instanceLoc = Platform.getInstanceLocation(); 398 if (instanceLoc == null || instanceLoc.isReadOnly()) { 399 return; 400 } 401 402 File versionFile = getVersionFile(instanceLoc.getURL(), true); 403 if (versionFile == null) { 404 return; 405 } 406 407 OutputStream output = null; 408 try { 409 String versionLine = WORKSPACE_VERSION_KEY + '=' 410 + WORKSPACE_VERSION_VALUE; 411 412 output = new FileOutputStream (versionFile); 413 output.write(versionLine.getBytes("UTF-8")); } catch (IOException e) { 415 IDEWorkbenchPlugin.log("Could not write version file", StatusUtil.newStatus(IStatus.ERROR, e.getMessage(), e)); 417 } finally { 418 try { 419 if (output != null) { 420 output.close(); 421 } 422 } catch (IOException e) { 423 } 425 } 426 } 427 428 439 private static File getVersionFile(URL workspaceUrl, boolean create) { 440 if (workspaceUrl == null) { 441 return null; 442 } 443 444 try { 445 File metaDir = new File (workspaceUrl.getPath(), METADATA_FOLDER); 447 if (!metaDir.exists() && (!create || !metaDir.mkdir())) { 448 return null; 449 } 450 451 File versionFile = new File (metaDir, VERSION_FILENAME); 453 if (!versionFile.exists() 454 && (!create || !versionFile.createNewFile())) { 455 return null; 456 } 457 458 return versionFile; 459 } catch (IOException e) { 460 return null; 462 } 463 } 464 465 468 public void stop() { 469 final IWorkbench workbench = PlatformUI.getWorkbench(); 470 if (workbench == null) 471 return; 472 final Display display = workbench.getDisplay(); 473 display.syncExec(new Runnable () { 474 public void run() { 475 if (!display.isDisposed()) 476 workbench.close(); 477 } 478 }); 479 } 480 } 481 | Popular Tags |