Skip to content

Commit

Permalink
Allows to wipe preferences
Browse files Browse the repository at this point in the history
Finally implements the "Revert to default values" of GAMA preferences, by wiping them clean and forcing a restart afterwards.
  • Loading branch information
AlexisDrogoul committed Jan 24, 2021
1 parent 6b3aeb5 commit 49c1a44
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
45 changes: 31 additions & 14 deletions msi.gama.core/src/msi/gama/common/preferences/GamaPreferences.java
Expand Up @@ -527,23 +527,26 @@ public static class External {
32648, IType.INT, true).in(NAME, GEOTOOLS)
.addChangeListener((IPreferenceBeforeChangeListener<Integer>) newValue -> {
final var codes = CRS.getSupportedCodes(newValue.toString());
if (codes.isEmpty()) { return false; }
if (codes.isEmpty())
return false;
return true;
});

public static final Pref<Integer> LIB_INITIAL_CRS =
create("pref_gis_initial_crs", "...or use the following CRS (EPSG code)", 4326, IType.INT, true)
.in(NAME, GEOTOOLS).addChangeListener((IPreferenceBeforeChangeListener<Integer>) newValue -> {
final var codes = CRS.getSupportedCodes(newValue.toString());
if (codes.isEmpty()) { return false; }
if (codes.isEmpty())
return false;
return true;
});

public static final Pref<Integer> LIB_OUTPUT_CRS =
create("pref_gis_output_crs", "... or use this following CRS (EPSG code)", 4326, IType.INT, true)
.in(NAME, GEOTOOLS).addChangeListener((IPreferenceBeforeChangeListener<Integer>) newValue -> {
final var codes = CRS.getSupportedCodes(newValue.toString());
if (codes.isEmpty()) { return false; }
if (codes.isEmpty())
return false;
return true;
});

Expand All @@ -555,11 +558,13 @@ public static class External {
private static String getDefaultRPath() {
final var os = System.getProperty("os.name");
final var osbit = System.getProperty("os.arch");
if (os.startsWith("Mac")) {
if (os.startsWith("Mac"))
return "/Library/Frameworks/R.framework/Resources/library/rJava/jri/libjri.jnilib";
} else if (os.startsWith("Linux")) { return "/usr/local/lib/libjri.so"; }
else if (os.startsWith("Linux"))
return "/usr/local/lib/libjri.so";
if (os.startsWith("Windows")) {
if (osbit.endsWith("64")) { return "C:\\Program Files\\R\\R-3.4.0\\library\\rJava\\jri\\jri.dll"; }
if (osbit.endsWith("64"))
return "C:\\Program Files\\R\\R-3.4.0\\library\\rJava\\jri\\jri.dll";
return "C:\\Program Files\\R\\R-3.4.0\\library\\rJava\\jri\\jri.dll";
}
return "";
Expand Down Expand Up @@ -637,7 +642,8 @@ public static <T> Pref<T> create(final String key, final String title, final Val
private static void register(final Pref gp) {
final IScope scope = null;
final var key = gp.key;
if (key == null) { return; }
if (key == null)
return;
prefs.put(key, gp);
final var value = gp.value;
if (storeKeys.contains(key)) {
Expand Down Expand Up @@ -775,14 +781,25 @@ public static void setNewPreferences(final Map<String, Object> modelValues) {
*
*/
public static void revertToDefaultValues(final Map<String, Object> modelValues) {
for (final String name : modelValues.keySet()) {
final Pref e = prefs.get(name);
if (e == null) {
continue;
}
modelValues.put(name, e.getInitialValue(null));
e.set(e.initial);
// First we erase all preferences
final var store = Preferences.userRoot().node("gama");
try {
store.removeNode();
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
// storeKeys.clear();
// reloadPreferences(modelValues);
//
//
// for (final String name : modelValues.keySet()) {
// final Pref e = prefs.get(name);
// if (e == null) {
// continue;
// }
// modelValues.put(name, e.getInitialValue(null));
// e.set(e.initial);
// }

}

Expand Down
Expand Up @@ -235,8 +235,9 @@ private void buildGroupContents(final Composite compo, final List<Pref> list, fi
modelValues.put(e.getKey(), value);
checkActivables(e, value);
checkRefreshables(e, value);
if (e.isRestartRequired())
if (e.isRestartRequired()) {
setRestartRequired();
}
} else {
GamaPreferencesView.this.showError("" + value + " is not accepted for parameter " + e.getKey());
}
Expand Down Expand Up @@ -332,7 +333,8 @@ public void widgetSelected(final SelectionEvent e) {
final var fd = new FileDialog(shell, SWT.OPEN);
fd.setFilterExtensions(new String[] { "*.prefs" });
final var path = fd.open();
if (path == null) { return; }
if (path == null)
return;
GamaPreferences.applyPreferencesFrom(path, modelValues);
for (final IParameterEditor ed : editors.values()) {
ed.updateValue(true);
Expand All @@ -353,7 +355,8 @@ public void widgetSelected(final SelectionEvent e) {
fd.setFilterExtensions(new String[] { "*.gaml" });
fd.setOverwrite(false);
final var path = fd.open();
if (path == null) { return; }
if (path == null)
return;
GamaPreferences.savePreferencesTo(path);
}

Expand Down Expand Up @@ -404,11 +407,15 @@ public void widgetSelected(final SelectionEvent e) {

@Override
public void widgetSelected(final SelectionEvent e) {
if (!Messages.question("Revert to default",
"Do you want to revert all preferences to their default values ? A restart of the platform will be performed immediately"))
return;
GamaPreferences.revertToDefaultValues(modelValues);
for (final IParameterEditor ed : editors.values()) {
ed.updateValue(true);
}

// for (final IParameterEditor ed : editors.values()) {
// // ed.forceUpdateValueAsynchronously();
// ed.updateValue(true);
// }
PlatformUI.getWorkbench().restart(true);
}

});
Expand Down Expand Up @@ -476,7 +483,8 @@ private void saveTab() {
}

private void saveDialogProperties() {
if (shell.isDisposed()) { return; }
if (shell.isDisposed())
return;
saveLocation();
saveSize();
saveTab();
Expand Down

0 comments on commit 49c1a44

Please sign in to comment.