-
Notifications
You must be signed in to change notification settings - Fork 4
/
Application.java
232 lines (202 loc) · 8.52 KB
/
Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*********************************************************************************************
*
* 'Application.java, in plugin msi.gama.application, is part of the source code of the GAMA modeling and simulation
* platform. (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
*
*
**********************************************************************************************/
package msi.gama.application;
import static java.lang.System.setProperty;
import static java.lang.Thread.setDefaultUncaughtExceptionHandler;
import static msi.gama.application.workspace.WorkspacePreferences.checkWorkspaceDirectory;
import static msi.gama.application.workspace.WorkspacePreferences.getLastSetWorkspaceDirectory;
import static msi.gama.application.workspace.WorkspacePreferences.getSelectedWorkspaceRootLocation;
import static msi.gama.application.workspace.WorkspacePreferences.isRememberWorkspace;
import static org.eclipse.e4.ui.workbench.IWorkbench.CLEAR_PERSISTED_STATE;
import static org.eclipse.jface.dialogs.MessageDialog.openConfirm;
import static org.eclipse.jface.dialogs.MessageDialog.openError;
import static org.eclipse.jface.dialogs.MessageDialog.openQuestion;
import static org.eclipse.ui.PlatformUI.RETURN_RESTART;
import static org.eclipse.ui.PlatformUI.createAndRunWorkbench;
import static org.eclipse.ui.PlatformUI.getWorkbench;
import static org.eclipse.ui.PlatformUI.isWorkbenchRunning;
import static org.eclipse.ui.internal.util.PrefUtil.getInternalPreferenceStore;
import static org.eclipse.ui.internal.util.PrefUtil.saveInternalPrefs;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.ide.application.DelayedEventsProcessor;
import msi.gama.application.workbench.ApplicationWorkbenchAdvisor;
import msi.gama.application.workspace.PickWorkspaceDialog;
import msi.gama.application.workspace.WorkspaceModelsManager;
import msi.gama.application.workspace.WorkspacePreferences;
import ummisco.gama.dev.utils.DEBUG;
/** This class controls all aspects of the application's execution */
public class Application implements IApplication {
{
DEBUG.OFF();
}
public static OpenDocumentEventProcessor processor;
public static final String CLEAR_WORKSPACE = "clearWorkspace";
public static void ClearWorkspace(final boolean clear) {
getInternalPreferenceStore().setValue(CLEAR_WORKSPACE, Boolean.toString(clear));
saveInternalPrefs();
}
public static boolean IsClearWorkspace() {
final boolean result = getInternalPreferenceStore().getBoolean(CLEAR_WORKSPACE);
DEBUG.OUT("Value of clearWorkspace pref: " + result);
return result;
}
public static class OpenDocumentEventProcessor extends DelayedEventsProcessor {
OpenDocumentEventProcessor(final Display display) {
super(display);
}
private final ArrayList<String> filesToOpen = new ArrayList<>(1);
@Override
public void handleEvent(final Event event) {
if (event.text != null) {
filesToOpen.add(event.text);
DEBUG.OUT("RECEIVED FILE TO OPEN: " + event.text);
}
}
@Override
public void catchUp(final Display display) {
if (filesToOpen.isEmpty()) return;
final String[] filePaths = filesToOpen.toArray(new String[filesToOpen.size()]);
filesToOpen.clear();
for (final String path : filePaths) {
WorkspaceModelsManager.instance.openModelPassedAsArgument(path);
}
}
}
public static void createProcessor() {
final Display display = Display.getDefault();
if (display == null) return;
processor = new OpenDocumentEventProcessor(display);
}
@Override
public Object start(final IApplicationContext context) throws Exception {
setDefaultUncaughtExceptionHandler((t, e) -> {
if (e instanceof OutOfMemoryError) {
final boolean close = openConfirm(null, "Out of memory",
"GAMA is out of memory and will likely crash. Do you want to close now ?");
if (close) { this.stop(); }
e.printStackTrace();
}
});
Display.setAppName("Gama Platform");
Display.setAppVersion("1.8.2");
// DEBUG.OUT(System.getProperties());
createProcessor();
final Object check = checkWorkspace();
if (EXIT_OK.equals(check)) return EXIT_OK;
// if ( check == EXIT_RESTART ) {
// ClearWorkspace(true);
// No need to restart : the value will be checked later
// return EXIT_RESTART;
// }
Display display = null;
try {
display = PlatformUI.createDisplay();
checkWorkbenchXMI();
// final String splash = getProperty("org.eclipse.equinox.launcher.splash.location");
// if ( splash != null ) {
// setProperty("org.eclipse.equinox.launcher.splash.location", splash.replace("bmp", "png"));
// }
final int returnCode = createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == RETURN_RESTART) return EXIT_RESTART;
return EXIT_OK;
} finally {
if (display != null) { display.dispose(); }
final Location instanceLoc = Platform.getInstanceLocation();
if (instanceLoc != null) { instanceLoc.release(); }
}
}
private void checkWorkbenchXMI() {
final boolean removeWorkbenchXMI = IsClearWorkspace();
if (removeWorkbenchXMI) {
setProperty(CLEAR_PERSISTED_STATE, "true");
ClearWorkspace(false);
}
}
public static Object checkWorkspace() throws IOException, MalformedURLException {
final Location instanceLoc = Platform.getInstanceLocation();
if (instanceLoc == null) {
// -data @none was specified but GAMA requires a workspace
openError(null, "Error", "A workspace is required to run GAMA");
return EXIT_OK;
}
boolean remember = false;
String lastUsedWs = null;
if (instanceLoc.isSet()) {
lastUsedWs = instanceLoc.getURL().getFile();
final String ret = WorkspacePreferences.checkWorkspaceDirectory(lastUsedWs, false, false, false);
if (ret != null) {
// if ( ret.equals("Restart") ) { return EXIT_RESTART; }
/* If we dont or cant remember and the location is set, we cant do anything as we need a workspace */
openError(null, "Error", "The workspace provided cannot be used. Please change it");
if (isWorkbenchRunning()) { getWorkbench().close(); }
System.exit(0);
return EXIT_OK;
}
} else {
/* Get what the user last said about remembering the workspace location */
remember = isRememberWorkspace();
/* Get the last used workspace location */
lastUsedWs = getLastSetWorkspaceDirectory();
/* If we have a "remember" but no last used workspace, it's not much to remember */
if (remember && (lastUsedWs == null || lastUsedWs.length() == 0)) { remember = false; }
if (remember) {
/*
* If there's any problem with the workspace, force a dialog
*/
final String ret = checkWorkspaceDirectory(lastUsedWs, false, false, false);
// AD Added this check explicitly as the checkWorkspaceDirectory() was not supposed to return null at
// this stage
if (ret != null) {
remember = "models".equals(ret) && !openQuestion(null, "Different version of the models library",
"The workspace contains a different version of the models library. Do you want to use another workspace ?");
}
}
}
/* If we don't remember the workspace, show the dialog */
if (!remember) {
final int pick = new PickWorkspaceDialog().open();
/* If the user cancelled, we can't do anything as we need a workspace */
if (pick == 1 /* Window.CANCEL */ && getSelectedWorkspaceRootLocation() == null) {
openError(null, "Error", "The application can not start without a workspace and will now exit.");
System.exit(0);
return IApplication.EXIT_OK;
}
/* Tell Eclipse what the selected location was and continue */
instanceLoc.set(new URL("file", null, getSelectedWorkspaceRootLocation()), false);
// if ( applyPrefs() ) { applyEclipsePreferences(getSelectedWorkspaceRootLocation()); }
} else if (!instanceLoc.isSet()) {
/* Set the last used location and continue */
instanceLoc.set(new URL("file", null, lastUsedWs), false);
}
return null;
}
@Override
public void stop() {
final IWorkbench workbench = getWorkbench();
if (workbench == null) return;
final Display display = workbench.getDisplay();
display.syncExec(() -> {
if (!display.isDisposed()) { workbench.close(); }
});
}
}