Commit | Line | Data |
---|---|---|
9c0ffa34 | 1 | /********************************************************************** |
ed902a2b | 2 | * Copyright (c) 2013, 2014 Ericsson |
9c0ffa34 BH |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | **********************************************************************/ | |
9d26aebf | 12 | package org.eclipse.tracecompass.internal.tracing.rcp.ui; |
9c0ffa34 | 13 | |
b42043af BH |
14 | import java.io.File; |
15 | import java.net.URL; | |
16 | import java.text.MessageFormat; | |
17 | ||
18 | import org.eclipse.core.runtime.Platform; | |
9c0ffa34 BH |
19 | import org.eclipse.equinox.app.IApplication; |
20 | import org.eclipse.equinox.app.IApplicationContext; | |
b42043af | 21 | import org.eclipse.jface.dialogs.MessageDialog; |
b42043af | 22 | import org.eclipse.osgi.service.datalocation.Location; |
9c0ffa34 | 23 | import org.eclipse.swt.widgets.Display; |
9d26aebf | 24 | import org.eclipse.tracecompass.internal.tracing.rcp.ui.messages.Messages; |
9c0ffa34 BH |
25 | import org.eclipse.ui.IWorkbench; |
26 | import org.eclipse.ui.PlatformUI; | |
27 | ||
28 | /** | |
29 | * This class controls all aspects of the application's execution | |
30 | * @author Bernd Hufmann | |
31 | */ | |
32 | public class Application implements IApplication { | |
33 | ||
b942e4f1 BH |
34 | private Location fInstanceLoc = null; |
35 | ||
9c0ffa34 BH |
36 | @Override |
37 | public Object start(IApplicationContext context) throws Exception { | |
38 | Display display = PlatformUI.createDisplay(); | |
39 | try { | |
b42043af | 40 | // fetch the Location that we will be modifying |
b942e4f1 | 41 | fInstanceLoc = Platform.getInstanceLocation(); |
b42043af BH |
42 | |
43 | // -data @noDefault in <applName>.ini allows us to set the workspace here. | |
44 | // If the user wants to change the location then he has to change | |
45 | // @noDefault to a specific location or remove -data @noDefault for | |
46 | // default location | |
b942e4f1 | 47 | if (!fInstanceLoc.allowsDefault() && !fInstanceLoc.isSet()) { |
b42043af BH |
48 | File workspaceRoot = new File(TracingRcpPlugin.getWorkspaceRoot()); |
49 | ||
50 | if (!workspaceRoot.exists()) { | |
51 | MessageDialog.openError(display.getActiveShell(), | |
52 | Messages.Application_WorkspaceCreationError, | |
53 | MessageFormat.format(Messages.Application_WorkspaceRootNotExistError, new Object[] { TracingRcpPlugin.getWorkspaceRoot() })); | |
54 | return IApplication.EXIT_OK; | |
55 | } | |
56 | ||
57 | if (!workspaceRoot.canWrite()) { | |
58 | MessageDialog.openError(display.getActiveShell(), | |
59 | Messages.Application_WorkspaceCreationError, | |
60 | MessageFormat.format(Messages.Application_WorkspaceRootPermissionError, new Object[] { TracingRcpPlugin.getWorkspaceRoot() })); | |
61 | return IApplication.EXIT_OK; | |
62 | } | |
63 | ||
64 | String workspace = TracingRcpPlugin.getWorkspaceRoot() + File.separator + TracingRcpPlugin.WORKSPACE_NAME; | |
65 | // set location to workspace | |
b942e4f1 BH |
66 | fInstanceLoc.set(new URL("file", null, workspace), false); //$NON-NLS-1$ |
67 | } | |
68 | ||
69 | if (!fInstanceLoc.lock()) { | |
70 | MessageDialog.openError(display.getActiveShell(), | |
71 | Messages.Application_WorkspaceCreationError, | |
72 | MessageFormat.format(Messages.Application_WorkspaceInUseError, new Object[] { fInstanceLoc.getURL().getPath() })); | |
73 | return IApplication.EXIT_OK; | |
b42043af BH |
74 | } |
75 | ||
9c0ffa34 BH |
76 | int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()); |
77 | if (returnCode == PlatformUI.RETURN_RESTART) { | |
78 | return IApplication.EXIT_RESTART; | |
79 | } | |
80 | return IApplication.EXIT_OK; | |
81 | } finally { | |
82 | display.dispose(); | |
83 | } | |
84 | } | |
85 | ||
86 | @Override | |
87 | public void stop() { | |
88 | if (!PlatformUI.isWorkbenchRunning()) { | |
89 | return; | |
90 | } | |
91 | final IWorkbench workbench = PlatformUI.getWorkbench(); | |
92 | final Display display = workbench.getDisplay(); | |
b942e4f1 | 93 | fInstanceLoc.release(); |
9c0ffa34 BH |
94 | display.syncExec(new Runnable() { |
95 | @Override | |
96 | public void run() { | |
97 | if (!display.isDisposed()) { | |
98 | workbench.close(); | |
99 | } | |
100 | } | |
101 | }); | |
102 | } | |
103 | } |