]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Better logic and validation for user-selected OpenModelica (refs #4061)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Feb 2013 11:38:43 +0000 (11:38 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Feb 2013 11:38:43 +0000 (11:38 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26757 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java
org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java

index 21a37d6ad4a9c24188fddcc4d0e130bb1613afc7..4a1e42ba01eec72902ed45a1bb8089e31b8a6aa9 100644 (file)
  *******************************************************************************/\r
 package org.simantics.modelica.preferences;\r
 \r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
 import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;\r
-import org.eclipse.core.runtime.preferences.DefaultScope;\r
+import org.eclipse.core.runtime.preferences.ConfigurationScope;\r
 import org.eclipse.core.runtime.preferences.IScopeContext;\r
 import org.osgi.service.prefs.Preferences;\r
 import org.simantics.modelica.Activator;\r
 import org.simantics.modelica.ModelicaManager;\r
 \r
 public class ModelicaPreferenceInitializer extends AbstractPreferenceInitializer {  \r
-    \r
-  public ModelicaPreferenceInitializer() {  \r
-    \r
-  }  \r
-    \r
-  @Override  \r
-  public void initializeDefaultPreferences() {  \r
-      \r
-      IScopeContext context = DefaultScope.INSTANCE;\r
-      Preferences node = context.getNode(Activator.PLUGIN_ID);\r
-      \r
-      node.putBoolean(OpenModelicaPreferences.OM_AUTO_DETECT, true); \r
-      node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
-  }  \r
+\r
+    public ModelicaPreferenceInitializer() {  \r
+\r
+    }  \r
+\r
+    @Override  \r
+    public void initializeDefaultPreferences() {  \r
+\r
+        IScopeContext context = ConfigurationScope.INSTANCE;\r
+        Preferences node = context.getNode(Activator.PLUGIN_ID);\r
+\r
+        String omHome = node.get(OpenModelicaPreferences.OM_HOME, null);\r
+        if(omHome == null)\r
+            // No OM_HOME set\r
+            useDefault(node);\r
+        else {\r
+            File dir = new File(omHome);\r
+            if(dir == null || !dir.isDirectory())\r
+                // OM_HOME is not a directory\r
+                useDefault(node);\r
+            try {\r
+                ModelicaManager.getOMCVersion(dir);\r
+            } catch (IOException e) {\r
+                // OpenModelica not found from directory\r
+                useDefault(node);\r
+            }\r
+        }\r
+    }  \r
+\r
+    private void useDefault(Preferences node) {\r
+        node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
+    }\r
 \r
 }\r
index 3e36ef332425a0727df8581806324ac847cf2c35..bde3c6999958ce0cf5dbbd7529bd3581f5fb60ad 100644 (file)
@@ -13,8 +13,6 @@ package org.simantics.modelica.preferences;
 \r
 public class OpenModelicaPreferences {\r
     \r
-    public static String OM_VERSION = "OpenModelica Version";\r
-    public static String OM_AUTO_DETECT = "MODELICA_AUTO_DETECT";\r
     public static String OM_HOME = "MODELICA_HOME";\r
 \r
 }\r
index 8fb7339312a13aefb607ecccc42d4b6611fce91b..0b2315990c4bf9c90eb02beabed5246b2d158979 100644 (file)
@@ -14,13 +14,13 @@ package org.simantics.sysdyn.ui.preferences;
 import java.io.File;\r
 import java.io.IOException;\r
 \r
-import org.eclipse.core.runtime.preferences.DefaultScope;\r
+import org.eclipse.core.runtime.preferences.ConfigurationScope;\r
 import org.eclipse.core.runtime.preferences.IScopeContext;\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
 import org.eclipse.jface.preference.DirectoryFieldEditor;\r
 import org.eclipse.jface.preference.FieldEditorPreferencePage;\r
 import org.eclipse.jface.preference.IPreferenceStore;\r
 import org.eclipse.jface.preference.RadioGroupFieldEditor;\r
+import org.eclipse.jface.preference.StringFieldEditor;\r
 import org.eclipse.ui.IWorkbench;\r
 import org.eclipse.ui.IWorkbenchPreferencePage;\r
 import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
@@ -37,73 +37,23 @@ IWorkbenchPreferencePage {
     public static String MODELICA_AUTO_DETECT = "MODELICA_AUTO_DETECT";\r
     public static String MODELICA_HOME = "MODELICA_HOME";\r
 \r
-//    private BooleanFieldEditor auto;\r
     private DirectoryFieldEditor path;\r
     private RadioGroupFieldEditor rg;\r
 \r
     public ModelicaPreferencePage() {\r
         super(GRID);\r
 \r
-        IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE,  \r
+        IPreferenceStore store = new ScopedPreferenceStore(ConfigurationScope.INSTANCE,  \r
                 org.simantics.modelica.Activator.PLUGIN_ID);  \r
         setPreferenceStore(store);  \r
-\r
+        \r
         setDescription("Modelica preferences");\r
     }\r
 \r
     public void createFieldEditors() {\r
 \r
-//        auto = new BooleanFieldEditor(\r
-//                OpenModelicaPreferences.OM_AUTO_DETECT,\r
-//                "&Autodetect modelica installation folder",\r
-//                getFieldEditorParent()) {\r
-//            @Override\r
-//            public void load() {\r
-//                super.load();\r
-//                path.setEnabled(!getBooleanValue(), getFieldEditorParent());\r
-//                rg.setEnabled(!getBooleanValue(), getFieldEditorParent());\r
-//                if (getBooleanValue()) {\r
-//                    path.setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
-//                }\r
-//            }\r
-//\r
-//\r
-//\r
-//            @Override\r
-//            public void store() {\r
-//                super.store();\r
-//                path.setEnabled(!getBooleanValue(), getFieldEditorParent());\r
-//                rg.setEnabled(!getBooleanValue(), getFieldEditorParent());\r
-//                \r
-//                IScopeContext context = DefaultScope.INSTANCE;\r
-//                Preferences node = context.getNode(Activator.PLUGIN_ID);\r
-//                node.putBoolean(OpenModelicaPreferences.OM_AUTO_DETECT, getBooleanValue());\r
-//                \r
-//                if (getBooleanValue()) {\r
-//                    path.setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
-//                }\r
-//            }\r
-//\r
-//            @Override\r
-//            protected void valueChanged(boolean oldValue,\r
-//                    boolean newValue) {\r
-//                path.setEnabled(!newValue, getFieldEditorParent());\r
-//                rg.setEnabled(!getBooleanValue(), getFieldEditorParent());\r
-//                if (newValue) {\r
-//                    String value = ModelicaManager.getDefaultModelicaHome().getAbsolutePath();\r
-//                    path.setStringValue(value);\r
-//                    \r
-//                    IScopeContext context = DefaultScope.INSTANCE;\r
-//                    Preferences node = context.getNode(Activator.PLUGIN_ID);\r
-//                    node.put(OpenModelicaPreferences.OM_HOME, value);\r
-//                }\r
-//            }\r
-//        };\r
-//        addField(auto);\r
-\r
-\r
-        File installed = ModelicaManager.getInstalledOpenModelicaHome();\r
-        File builtIn = ModelicaManager.getBuiltinOpenModelicaHome();\r
+        final File installed = ModelicaManager.getInstalledOpenModelicaHome();\r
+        final File builtIn = ModelicaManager.getBuiltinOpenModelicaHome();\r
         String installedVersion = null;\r
         String builtInVersion = null;\r
         try {\r
@@ -140,49 +90,93 @@ IWorkbenchPreferencePage {
 \r
             @Override\r
             protected void fireValueChanged(String property, Object oldValue, Object newValue) {\r
-                if(newValue.equals(CUSTOM_PATH)) {\r
-\r
-                } else {\r
+                if((installed != null &&newValue.equals(installed.getAbsolutePath())) ||  newValue.equals(builtIn.getAbsolutePath())) {\r
                     path.setStringValue((String)newValue);\r
-                }              \r
+                    path.setEnabled(false, getFieldEditorParent());\r
+                } else {\r
+                    path.setEnabled(true, getFieldEditorParent());\r
+                }   \r
             }\r
-        };\r
-\r
 \r
-//        boolean isAuto = getPreferenceStore().getBoolean(OpenModelicaPreferences.OM_AUTO_DETECT);\r
-//        rg.setEnabled(!isAuto, getFieldEditorParent());\r
+            @Override\r
+            public void doLoadDefault() {\r
+                IScopeContext context = ConfigurationScope.INSTANCE;\r
+                Preferences node = context.getNode(Activator.PLUGIN_ID);\r
+                node.put(OpenModelicaPreferences.OM_HOME, ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
+                load();\r
+            }\r
+        };\r
 \r
         addField(rg);\r
 \r
         path = new DirectoryFieldEditor(OpenModelicaPreferences.OM_HOME, \r
                 "&Modelica Home:", getFieldEditorParent()) {\r
+\r
             @Override\r
-            public void load() {\r
-                super.load();\r
-//                if (auto.getBooleanValue()) {\r
-                    setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
-//                }\r
+            public void setValidateStrategy(int value) {\r
+                super.setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE);\r
             }\r
-\r
+            \r
             @Override\r
-            public void store() {\r
-                super.store();\r
-                IScopeContext context = DefaultScope.INSTANCE;\r
+            public void doStore() {\r
+                super.doStore();\r
+                IScopeContext context = ConfigurationScope.INSTANCE;\r
                 Preferences node = context.getNode(Activator.PLUGIN_ID);\r
                 node.put(OpenModelicaPreferences.OM_HOME, getStringValue());\r
             }\r
+\r
+            @Override\r
+            protected boolean doCheckState() {\r
+                boolean valid = super.doCheckState();\r
+                if(valid) {\r
+                    // path is a valid directory\r
+                    String path = getStringValue();\r
+                    File dir = new File(path);\r
+                    try {\r
+                        String version = ModelicaManager.getOMCVersion(dir);\r
+                        if(version == null || version.isEmpty()) {\r
+                            return false;\r
+                        }\r
+                    } catch (IOException e) {\r
+                        return false;\r
+                    }\r
+                }\r
+                \r
+                return valid;\r
+            }\r
+            \r
+            @Override\r
+            public void doLoad() {\r
+                super.doLoad();\r
+                String value = getStringValue();\r
+                updatePath(value);\r
+            }\r
+            \r
+            @Override\r
+            public void doLoadDefault() {\r
+                updatePath(ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
+            }\r
+            \r
+            private void updatePath(String newValue) {\r
+                if((installed != null && newValue.equals(installed.getAbsolutePath()) ||  newValue.equals(builtIn.getAbsolutePath()))) {\r
+                    path.setStringValue(newValue);\r
+                    path.setEnabled(false, getFieldEditorParent());\r
+                } else {\r
+                    path.setEnabled(true, getFieldEditorParent());\r
+                }   \r
+            }\r
+\r
+\r
         };\r
+        \r
+        path.setErrorMessage("Path must be a valid OpenModelica directory");\r
         addField(path);\r
     }\r
 \r
     @Override\r
     public void init(IWorkbench workbench) {\r
     }\r
-\r
-    @Override\r
-    protected void performDefaults() {\r
-        super.performDefaults();\r
-//        rg.setEnabled(!auto.getBooleanValue(), getFieldEditorParent());\r
-    }\r
+    \r
+    \r
 \r
 }\r