/
WorkspacePreferences.java
259 lines (226 loc) · 9.52 KB
/
WorkspacePreferences.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*********************************************************************************************
*
* 'WorkspacePreferences.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.workspace;
import static msi.gama.common.preferences.GamaPreferenceStore.getStore;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import msi.gama.application.Application;
import ummisco.gama.dev.utils.DEBUG;
public class WorkspacePreferences {
private static final String KEY_WORSPACE_PATH = "pref_workspace_path";
private static final String KEY_WORKSPACE_REMEMBER = "pref_workspace_remember";
private static final String KEY_WORKSPACE_LIST = "pref_workspace_list";
private static final String KEY_ASK_REBUILD = "pref_ask_rebuild";
private static final String KEY_ASK_OUTDATED = "pref_ask_outdated";
public static final String WORKSPACE_IDENTIFIER = ".gama_application_workspace";
private static String MODEL_IDENTIFIER = null;
static String selectedWorkspaceRootLocation;
/**
* Returns whether the user selected "remember workspace" in the preferences
*/
public static boolean isRememberWorkspace() {
return getStore().getBoolean(KEY_WORKSPACE_REMEMBER, false);
}
public static void isRememberWorkspace(final boolean remember) {
getStore().putBoolean(KEY_WORKSPACE_REMEMBER, remember);
}
public static String getLastUsedWorkspaces() {
return getStore().get(KEY_WORKSPACE_LIST, "");
}
public static void setLastUsedWorkspaces(final String used) {
getStore().put(KEY_WORKSPACE_LIST, used);
}
/**
* Returns the last set workspace directory from the preferences
*
* @return null if none
*/
public static String getLastSetWorkspaceDirectory() {
return getStore().get(KEY_WORSPACE_PATH, "");
}
public static void setLastSetWorkspaceDirectory(final String last) {
getStore().put(KEY_WORSPACE_PATH, last);
}
public static boolean askBeforeRebuildingWorkspace() {
// true by default
return getStore().getBoolean(KEY_ASK_REBUILD, true);
}
public static void askBeforeRebuildingWorkspace(final boolean ask) {
// true by default
getStore().putBoolean(KEY_ASK_REBUILD, ask);
}
public static boolean askBeforeUsingOutdatedWorkspace() {
// true by default
return getStore().getBoolean(KEY_ASK_OUTDATED, true);
}
public static void askBeforeUsingOutdatedWorkspace(final boolean ask) {
// true by default
getStore().putBoolean(KEY_ASK_OUTDATED, ask);
}
public static String getSelectedWorkspaceRootLocation() {
return selectedWorkspaceRootLocation;
}
public static void setSelectedWorkspaceRootLocation(final String s) {
selectedWorkspaceRootLocation = s;
}
public static String getCurrentGamaStampString() {
String gamaStamp = null;
try {
final URL tmpURL = new URL("platform:/plugin/msi.gama.models/models/");
final URL resolvedFileURL = FileLocator.toFileURL(tmpURL);
// We need to use the 3-arg constructor of URI in order to properly escape file system chars
final URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null).normalize();
final File modelsRep = new File(resolvedURI);
// loading file from URL Path is not a good idea. There are some bugs
// File modelsRep = new File(urlRep.getPath());
final long time = modelsRep.lastModified();
gamaStamp = ".built_in_models_" + time;
DEBUG.OUT(">GAMA version " + Platform.getProduct().getDefiningBundle().getVersion().toString()
+ " loading...");
DEBUG.OUT(">GAMA models library version: " + gamaStamp);
} catch (final IOException | URISyntaxException e) {
e.printStackTrace();
}
return gamaStamp;
}
/**
* Ensures a workspace directory is OK in regards of reading/writing, etc. This method will get called externally as
* well.
*
* @param parentShell
* Shell parent shell
* @param workspaceLocation
* Directory the user wants to use
* @param askCreate
* Whether to ask if to create the workspace or not in this location if it does not exist already
* @param fromDialog
* Whether this method was called from our dialog or from somewhere else just to check a location
* @return null if everything is ok, or an error message if not
*/
public static String checkWorkspaceDirectory(final String workspaceLocation, final boolean askCreate,
final boolean fromDialog, final boolean cloning) {
final File f = new File(workspaceLocation);
if (!f.exists() && askCreate) {
final boolean create = MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "New Directory",
workspaceLocation + " does not exist. Would you like to create a new workspace here"
+ (cloning ? ", copy the projects of your current workspace into it," : "")
+ " and proceeed ?");
if (create) {
try {
f.mkdirs();
final File wsDot = new File(workspaceLocation + File.separator + WORKSPACE_IDENTIFIER);
wsDot.createNewFile();
final File dotFile = new File(workspaceLocation + File.separator + getModelIdentifier());
dotFile.createNewFile();
} catch (final RuntimeException | IOException er) {
er.printStackTrace();
return "Error creating directories, please check folder permissions";
}
}
if (!f.exists()) return "The selected directory does not exist";
return null;
}
if (!f.canRead()) return "The selected directory is not readable";
if (!f.isDirectory()) return "The selected path is not a directory";
testWorkspaceSanity(f);
final File wsTest = new File(workspaceLocation + File.separator + WORKSPACE_IDENTIFIER);
if (fromDialog) {
if (!wsTest.exists()) {
final boolean create = MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
"New Workspace", "The directory '" + wsTest.getAbsolutePath()
+ "' exists but is not identified as a GAMA workspace. \n\nWould you like to use it anyway ?");
if (!create) return "Please select a directory for your workspace";
try {
f.mkdirs();
final File wsDot = new File(workspaceLocation + File.separator + WORKSPACE_IDENTIFIER);
wsDot.createNewFile();
} catch (final Exception err) {
return "Error creating directories, please check folder permissions";
}
if (!wsTest.exists()) return "The selected directory does not exist";
return null;
}
} else if (!wsTest.exists()) return "The selected directory is not a workspace directory";
final File dotFile = new File(workspaceLocation + File.separator + getModelIdentifier());
if (!dotFile.exists()) {
if (fromDialog) {
boolean create = true;
if (askBeforeUsingOutdatedWorkspace()) {
create = MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
"Different version of the models library",
"The workspace contains a different version of the models library. Do you want to proceed anyway ?");
}
if (create) {
try {
dotFile.createNewFile();
} catch (final IOException e) {
return "Error updating the models library";
}
return null;
}
}
return "models";
}
if (cloning) {
final boolean b = MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "Existing workspace",
"The path entered is a path to an existing workspace. All its contents will be erased and replaced by the current workspace contents. Proceed anyway ?");
if (!b) return "";
}
return null;
}
public static boolean testWorkspaceSanity(final File workspace) {
DEBUG.OUT("[GAMA] Checking for workspace sanity");
File[] files = workspace.listFiles((FileFilter) file -> ".metadata".equals(file.getName()));
if (files == null || files.length == 0) return true;
final File[] logs = files[0].listFiles((FileFilter) file -> file.getName().contains(".log"));
if (logs != null) {
for (final File log : logs) {
log.delete();
}
}
files = files[0].listFiles((FileFilter) file -> ".plugins".equals(file.getName()));
if (files == null) return false;
if (files.length == 0) return true;
files = files[0].listFiles((FileFilter) file -> "org.eclipse.core.resources".equals(file.getName()));
if (files == null) return false;
if (files.length == 0) return true;
files = files[0].listFiles((FileFilter) file -> file.getName().contains("snap"));
if (files == null) return false;
DEBUG.OUT("[GAMA] Workspace appears to be " + (files.length == 0 ? "clean" : "corrupted"));
if (files.length == 0) return true;
boolean rebuild = true;
if (askBeforeRebuildingWorkspace()) {
rebuild = MessageDialog.openQuestion(Display.getDefault().getActiveShell(), "Corrupted workspace",
"The workspace appears to be corrupted (due to a previous crash) or it is currently used by another instance of the platform. Would you like GAMA to clean it ?");
}
if (rebuild) {
for (final File file : files) {
if (file.exists()) { file.delete(); }
}
Application.ClearWorkspace(true);
return false;
}
return true;
}
public static String getModelIdentifier() {
if (MODEL_IDENTIFIER == null) { MODEL_IDENTIFIER = getCurrentGamaStampString(); }
return MODEL_IDENTIFIER;
}
}