From 32f57b97442744fdfd0621caeb5186a990f7eacc Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 12 Feb 2013 11:38:43 +0000 Subject: [PATCH] Better logic and validation for user-selected OpenModelica (refs #4061) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26757 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ModelicaPreferenceInitializer.java | 51 ++++-- .../preferences/OpenModelicaPreferences.java | 2 - .../preferences/ModelicaPreferencePage.java | 152 +++++++++--------- 3 files changed, 109 insertions(+), 96 deletions(-) diff --git a/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java b/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java index 21a37d6a..4a1e42ba 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java +++ b/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java @@ -11,27 +11,48 @@ *******************************************************************************/ package org.simantics.modelica.preferences; +import java.io.File; +import java.io.IOException; + import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; -import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.ConfigurationScope; import org.eclipse.core.runtime.preferences.IScopeContext; import org.osgi.service.prefs.Preferences; import org.simantics.modelica.Activator; import org.simantics.modelica.ModelicaManager; public class ModelicaPreferenceInitializer extends AbstractPreferenceInitializer { - - public ModelicaPreferenceInitializer() { - - } - - @Override - public void initializeDefaultPreferences() { - - IScopeContext context = DefaultScope.INSTANCE; - Preferences node = context.getNode(Activator.PLUGIN_ID); - - node.putBoolean(OpenModelicaPreferences.OM_AUTO_DETECT, true); - node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); - } + + public ModelicaPreferenceInitializer() { + + } + + @Override + public void initializeDefaultPreferences() { + + IScopeContext context = ConfigurationScope.INSTANCE; + Preferences node = context.getNode(Activator.PLUGIN_ID); + + String omHome = node.get(OpenModelicaPreferences.OM_HOME, null); + if(omHome == null) + // No OM_HOME set + useDefault(node); + else { + File dir = new File(omHome); + if(dir == null || !dir.isDirectory()) + // OM_HOME is not a directory + useDefault(node); + try { + ModelicaManager.getOMCVersion(dir); + } catch (IOException e) { + // OpenModelica not found from directory + useDefault(node); + } + } + } + + private void useDefault(Preferences node) { + node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); + } } diff --git a/org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java b/org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java index 3e36ef33..bde3c699 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java +++ b/org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java @@ -13,8 +13,6 @@ package org.simantics.modelica.preferences; public class OpenModelicaPreferences { - public static String OM_VERSION = "OpenModelica Version"; - public static String OM_AUTO_DETECT = "MODELICA_AUTO_DETECT"; public static String OM_HOME = "MODELICA_HOME"; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java index 8fb73393..0b231599 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java @@ -14,13 +14,13 @@ package org.simantics.sysdyn.ui.preferences; import java.io.File; import java.io.IOException; -import org.eclipse.core.runtime.preferences.DefaultScope; +import org.eclipse.core.runtime.preferences.ConfigurationScope; import org.eclipse.core.runtime.preferences.IScopeContext; -import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.jface.preference.DirectoryFieldEditor; import org.eclipse.jface.preference.FieldEditorPreferencePage; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.RadioGroupFieldEditor; +import org.eclipse.jface.preference.StringFieldEditor; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPreferencePage; import org.eclipse.ui.preferences.ScopedPreferenceStore; @@ -37,73 +37,23 @@ IWorkbenchPreferencePage { public static String MODELICA_AUTO_DETECT = "MODELICA_AUTO_DETECT"; public static String MODELICA_HOME = "MODELICA_HOME"; -// private BooleanFieldEditor auto; private DirectoryFieldEditor path; private RadioGroupFieldEditor rg; public ModelicaPreferencePage() { super(GRID); - IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, + IPreferenceStore store = new ScopedPreferenceStore(ConfigurationScope.INSTANCE, org.simantics.modelica.Activator.PLUGIN_ID); setPreferenceStore(store); - + setDescription("Modelica preferences"); } public void createFieldEditors() { -// auto = new BooleanFieldEditor( -// OpenModelicaPreferences.OM_AUTO_DETECT, -// "&Autodetect modelica installation folder", -// getFieldEditorParent()) { -// @Override -// public void load() { -// super.load(); -// path.setEnabled(!getBooleanValue(), getFieldEditorParent()); -// rg.setEnabled(!getBooleanValue(), getFieldEditorParent()); -// if (getBooleanValue()) { -// path.setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); -// } -// } -// -// -// -// @Override -// public void store() { -// super.store(); -// path.setEnabled(!getBooleanValue(), getFieldEditorParent()); -// rg.setEnabled(!getBooleanValue(), getFieldEditorParent()); -// -// IScopeContext context = DefaultScope.INSTANCE; -// Preferences node = context.getNode(Activator.PLUGIN_ID); -// node.putBoolean(OpenModelicaPreferences.OM_AUTO_DETECT, getBooleanValue()); -// -// if (getBooleanValue()) { -// path.setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); -// } -// } -// -// @Override -// protected void valueChanged(boolean oldValue, -// boolean newValue) { -// path.setEnabled(!newValue, getFieldEditorParent()); -// rg.setEnabled(!getBooleanValue(), getFieldEditorParent()); -// if (newValue) { -// String value = ModelicaManager.getDefaultModelicaHome().getAbsolutePath(); -// path.setStringValue(value); -// -// IScopeContext context = DefaultScope.INSTANCE; -// Preferences node = context.getNode(Activator.PLUGIN_ID); -// node.put(OpenModelicaPreferences.OM_HOME, value); -// } -// } -// }; -// addField(auto); - - - File installed = ModelicaManager.getInstalledOpenModelicaHome(); - File builtIn = ModelicaManager.getBuiltinOpenModelicaHome(); + final File installed = ModelicaManager.getInstalledOpenModelicaHome(); + final File builtIn = ModelicaManager.getBuiltinOpenModelicaHome(); String installedVersion = null; String builtInVersion = null; try { @@ -140,49 +90,93 @@ IWorkbenchPreferencePage { @Override protected void fireValueChanged(String property, Object oldValue, Object newValue) { - if(newValue.equals(CUSTOM_PATH)) { - - } else { + if((installed != null &&newValue.equals(installed.getAbsolutePath())) || newValue.equals(builtIn.getAbsolutePath())) { path.setStringValue((String)newValue); - } + path.setEnabled(false, getFieldEditorParent()); + } else { + path.setEnabled(true, getFieldEditorParent()); + } } - }; - -// boolean isAuto = getPreferenceStore().getBoolean(OpenModelicaPreferences.OM_AUTO_DETECT); -// rg.setEnabled(!isAuto, getFieldEditorParent()); + @Override + public void doLoadDefault() { + IScopeContext context = ConfigurationScope.INSTANCE; + Preferences node = context.getNode(Activator.PLUGIN_ID); + node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); + load(); + } + }; addField(rg); path = new DirectoryFieldEditor(OpenModelicaPreferences.OM_HOME, "&Modelica Home:", getFieldEditorParent()) { + @Override - public void load() { - super.load(); -// if (auto.getBooleanValue()) { - setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); -// } + public void setValidateStrategy(int value) { + super.setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE); } - + @Override - public void store() { - super.store(); - IScopeContext context = DefaultScope.INSTANCE; + public void doStore() { + super.doStore(); + IScopeContext context = ConfigurationScope.INSTANCE; Preferences node = context.getNode(Activator.PLUGIN_ID); node.put(OpenModelicaPreferences.OM_HOME, getStringValue()); } + + @Override + protected boolean doCheckState() { + boolean valid = super.doCheckState(); + if(valid) { + // path is a valid directory + String path = getStringValue(); + File dir = new File(path); + try { + String version = ModelicaManager.getOMCVersion(dir); + if(version == null || version.isEmpty()) { + return false; + } + } catch (IOException e) { + return false; + } + } + + return valid; + } + + @Override + public void doLoad() { + super.doLoad(); + String value = getStringValue(); + updatePath(value); + } + + @Override + public void doLoadDefault() { + updatePath(ModelicaManager.getDefaultModelicaHome().getAbsolutePath()); + } + + private void updatePath(String newValue) { + if((installed != null && newValue.equals(installed.getAbsolutePath()) || newValue.equals(builtIn.getAbsolutePath()))) { + path.setStringValue(newValue); + path.setEnabled(false, getFieldEditorParent()); + } else { + path.setEnabled(true, getFieldEditorParent()); + } + } + + }; + + path.setErrorMessage("Path must be a valid OpenModelica directory"); addField(path); } @Override public void init(IWorkbench workbench) { } - - @Override - protected void performDefaults() { - super.performDefaults(); -// rg.setEnabled(!auto.getBooleanValue(), getFieldEditorParent()); - } + + } -- 2.47.1