Skip to content

Commit

Permalink
Correctly applies new preferences to the last workspace(s); see #3115
Browse files Browse the repository at this point in the history
**WARNING** first run of this new version will forget about the
workspace (whatever its scope is, global or local).
  • Loading branch information
AlexisDrogoul committed Aug 15, 2021
1 parent b488232 commit 89ce978
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 379 deletions.
79 changes: 36 additions & 43 deletions msi.gama.application/src/msi/gama/application/Application.java
@@ -1,8 +1,7 @@
/*********************************************************************************************
*
* 'Application.java, in plugin msi.gama.application, is part of the source code of the
* GAMA modeling and simulation platform.
* (v. 1.8.1)
* '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
*
Expand All @@ -14,8 +13,6 @@

import static java.lang.System.setProperty;
import static java.lang.Thread.setDefaultUncaughtExceptionHandler;
import static msi.gama.application.workspace.WorkspacePreferences.applyEclipsePreferences;
import static msi.gama.application.workspace.WorkspacePreferences.applyPrefs;
import static msi.gama.application.workspace.WorkspacePreferences.checkWorkspaceDirectory;
import static msi.gama.application.workspace.WorkspacePreferences.getLastSetWorkspaceDirectory;
import static msi.gama.application.workspace.WorkspacePreferences.getSelectedWorkspaceRootLocation;
Expand All @@ -30,10 +27,12 @@
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;
Expand All @@ -43,6 +42,7 @@
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;
Expand All @@ -61,7 +61,7 @@ public class Application implements IApplication {
public static final String CLEAR_WORKSPACE = "clearWorkspace";

public static void ClearWorkspace(final boolean clear) {
getInternalPreferenceStore().setValue(CLEAR_WORKSPACE, Boolean.valueOf(clear).toString());
getInternalPreferenceStore().setValue(CLEAR_WORKSPACE, Boolean.toString(clear));
saveInternalPrefs();
}

Expand All @@ -81,41 +81,39 @@ public static class OpenDocumentEventProcessor extends DelayedEventsProcessor {

@Override
public void handleEvent(final Event event) {
if ( event.text != null ) {
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;
if (filesToOpen.isEmpty()) return;

final String[] filePaths = filesToOpen.toArray(new String[filesToOpen.size()]);
filesToOpen.clear();

for ( final String path : filePaths ) {
for (final String path : filePaths) {
WorkspaceModelsManager.instance.openModelPassedAsArgument(path);
}
}
}

public static void createProcessor() {
final Display display = Display.getDefault();
if ( display == null )
return;
if (display == null) return;
processor = new OpenDocumentEventProcessor(display);
}

@Override
public Object start(final IApplicationContext context) throws Exception {

setDefaultUncaughtExceptionHandler((t, e) -> {
if ( e instanceof OutOfMemoryError ) {
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(); }
"GAMA is out of memory and will likely crash. Do you want to close now ?");
if (close) { this.stop(); }
e.printStackTrace();
}

Expand All @@ -125,8 +123,7 @@ public Object start(final IApplicationContext context) throws Exception {
// DEBUG.OUT(System.getProperties());
createProcessor();
final Object check = checkWorkspace();
if ( EXIT_OK.equals(check) )
return EXIT_OK;
if (EXIT_OK.equals(check)) return EXIT_OK;
// if ( check == EXIT_RESTART ) {
// ClearWorkspace(true);
// No need to restart : the value will be checked later
Expand All @@ -141,20 +138,19 @@ public Object start(final IApplicationContext context) throws Exception {
// 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;
if (returnCode == RETURN_RESTART) return EXIT_RESTART;
return EXIT_OK;
} finally {
if ( display != null ) { display.dispose(); }
if (display != null) { display.dispose(); }
final Location instanceLoc = Platform.getInstanceLocation();
if ( instanceLoc != null ) { instanceLoc.release(); }
if (instanceLoc != null) { instanceLoc.release(); }
}

}

private void checkWorkbenchXMI() {
final boolean removeWorkbenchXMI = IsClearWorkspace();
if ( removeWorkbenchXMI ) {
if (removeWorkbenchXMI) {
setProperty(CLEAR_PERSISTED_STATE, "true");
ClearWorkspace(false);
}
Expand All @@ -163,21 +159,21 @@ private void checkWorkbenchXMI() {

public static Object checkWorkspace() throws IOException, MalformedURLException {
final Location instanceLoc = Platform.getInstanceLocation();
if ( instanceLoc == null ) {
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() ) {
if (instanceLoc.isSet()) {
lastUsedWs = instanceLoc.getURL().getFile();
final String ret = WorkspacePreferences.checkWorkspaceDirectory(lastUsedWs, false, false, false);
if ( ret != null ) {
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(); }
if (isWorkbenchRunning()) { getWorkbench().close(); }
System.exit(0);
return EXIT_OK;
}
Expand All @@ -188,38 +184,36 @@ public static Object checkWorkspace() throws IOException, MalformedURLException
/* 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 (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 ) {
// 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 ?");
"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 ) {
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 ) {
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);
}

// 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;
Expand All @@ -228,11 +222,10 @@ public static Object checkWorkspace() throws IOException, MalformedURLException
@Override
public void stop() {
final IWorkbench workbench = getWorkbench();
if ( workbench == null )
return;
if (workbench == null) return;
final Display display = workbench.getDisplay();
display.syncExec(() -> {
if ( !display.isDisposed() ) { workbench.close(); }
if (!display.isDisposed()) { workbench.close(); }
});
}

Expand Down

0 comments on commit 89ce978

Please sign in to comment.