]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Using Preferences and UI Preferences page to control OM version (refs #4061)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Feb 2013 06:20:52 +0000 (06:20 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 12 Feb 2013 06:20:52 +0000 (06:20 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26753 ac1ea38d-2e2b-0410-8846-a27921b304fc

14 files changed:
org.simantics.modelica/META-INF/MANIFEST.MF
org.simantics.modelica/plugin.xml
org.simantics.modelica/src/org/simantics/modelica/Activator.java
org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java
org.simantics.modelica/src/org/simantics/modelica/SimulationLocation.java
org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java [new file with mode: 0644]
org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java [new file with mode: 0644]
org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/modelica/SysdynModelicaEditor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferenceInitializer.java [deleted file]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferencePage.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java
org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java

index 3a44a2f65bda8d536acfffe7f586f54ae9e8d69e..88a27ec5a970d3a2c57b7e52b722094ecde1a127 100644 (file)
@@ -11,6 +11,7 @@ Require-Bundle: gnu.trove2;bundle-version="2.0.4",
  org.simantics.utils.datastructures;bundle-version="1.1.0"
 Export-Package: org.simantics.modelica,
  org.simantics.modelica.data,
+ org.simantics.modelica.preferences,
  org.simantics.modelica.reader
 Bundle-Activator: org.simantics.modelica.Activator
 Bundle-ActivationPolicy: lazy
index dbfb3c50ad1d594171d3d9fcc85a82b819208520..1e19229514d73a65c27d88dca0cb5c610262cf3b 100644 (file)
@@ -1,6 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>\r
 <?eclipse version="3.4"?>\r
 <plugin>\r
+   <extension\r
+         point="org.eclipse.core.runtime.preferences">\r
+      <initializer\r
+            class="org.simantics.modelica.preferences.ModelicaPreferenceInitializer">\r
+      </initializer>\r
+   </extension>\r
 \r
 \r
 </plugin>\r
index 47ae2ec93b4e7fd9971f1af437b264d33174ac64..bcbb8d5864f50b65d29f3a2720a0219a3f47356c 100644 (file)
@@ -16,7 +16,9 @@ import org.osgi.framework.BundleContext;
 \r
 public class Activator implements BundleActivator {\r
 \r
-       static BundleContext context;\r
+    public static final String PLUGIN_ID = "org.simantics.modelica";\r
+\r
+    static BundleContext context;\r
        \r
        @Override\r
        public void start(BundleContext context) throws Exception {\r
index 0b8a4137ebc670999771aab25e681dcad8abdd9f..5364078e099eb65b1dda761de1e47b9d3cd815e5 100644 (file)
@@ -45,7 +45,11 @@ import javax.xml.xpath.XPathFactory;
 \r
 import org.eclipse.core.runtime.FileLocator;\r
 import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.preferences.DefaultScope;\r
+import org.eclipse.core.runtime.preferences.IScopeContext;\r
 import org.osgi.framework.Bundle;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.modelica.preferences.OpenModelicaPreferences;\r
 import org.w3c.dom.Document;\r
 import org.w3c.dom.Node;\r
 import org.xml.sax.InputSource;\r
@@ -59,7 +63,6 @@ import org.xml.sax.SAXException;
  */\r
 public class ModelicaManager {\r
     \r
-    private static String builtInOMVersionName = "1.9.0 beta 4 (r15030)";\r
     private static String builtInOMVersion = "1.9.0";\r
 \r
     public enum OSType {\r
@@ -92,60 +95,37 @@ public class ModelicaManager {
      * @return\r
      */\r
     public static File getModelicaHome() {\r
-\r
-        // Get modelica home from environment variable\r
-        String dir = System.getenv("OPENMODELICAHOME");\r
-        File omhome = null;\r
+        \r
+        // Try preferences\r
+        IScopeContext context = DefaultScope.INSTANCE;\r
+        Preferences node = context.getNode(Activator.PLUGIN_ID);\r
+        String omHomePath = node.get(OpenModelicaPreferences.OM_HOME, null);\r
+        if(omHomePath != null) {\r
+            File omHome = new File(omHomePath);\r
+            if(omHome != null && omHome.isDirectory())\r
+               return omHome;\r
+        }\r
+        \r
+        return getDefaultModelicaHome();\r
+    }\r
+    \r
+    public static File getDefaultModelicaHome() {\r
+        // Try to find installed openModelica\r
+        File omhome = getInstalledOpenModelicaHome();\r
+        if(omhome != null)\r
+            return omhome;\r
 \r
         // Get operating system\r
-        String osName = System.getProperty("os.name");\r
         OSType os = calculateOS();\r
-\r
-        // operating system unknown\r
-        if (os == OSType.UNKNOWN)\r
-            throw new UnsatisfiedLinkError("unknown OS '" + osName + "' for running OpenModelica");\r
-\r
-        // If OPENMODELICAHOME is found, try to return the folder.\r
-        if(dir != null) {\r
-            switch (os) {\r
-                case APPLE:\r
-                case LINUX:\r
-                case SUN:\r
-                    omhome = new File(dir);\r
-                    if(omhome.isDirectory())\r
-                        return  omhome;\r
-                    else\r
-                        break;\r
-                case WINDOWS:\r
-                    omhome = new File(dir);\r
-                    if(omhome.isDirectory())\r
-                        return  omhome;\r
-                    else\r
-                        break;\r
-                default:\r
-            }\r
-        }\r
-\r
+        \r
         // OPENMODELICAHOMe was not found or the folder does not exist. Try built-in OpenModelica for windows\r
         if(os.equals(OSType.WINDOWS)) {\r
-\r
-            Bundle bundle = Platform.getBundle("org.simantics.openmodelica.win32");\r
-            if (bundle != null) {\r
-                try{\r
-                    URL entry = bundle.getEntry("/");\r
-                    if(entry != null) {\r
-                        URL fileURL = FileLocator.toFileURL(entry);\r
-                        File root = new File( URLDecoder.decode(fileURL.getPath(), "UTF-8") );\r
-                        File f = new File(root, "OpenModelica" + builtInOMVersion);\r
-                        return f;\r
-                    }\r
-                }\r
-                catch (Exception e) {\r
-                    e.printStackTrace();\r
-                }\r
-            }\r
+            File builtin = getBuiltinOpenModelicaHome();\r
+            if(builtin != null)\r
+                return builtin;\r
         }\r
 \r
+\r
         // OS was not windows or built-in OpenModelica did not work\r
         switch (os) {\r
             case APPLE:\r
@@ -159,44 +139,37 @@ public class ModelicaManager {
         }\r
     }\r
     \r
-    public static String getInstalledOpenModelicaVersion() {\r
+    public static File getInstalledOpenModelicaHome() {\r
         String dir = System.getenv("OPENMODELICAHOME");\r
         if(dir != null) {\r
             File omhome = new File(dir);\r
             if(omhome.isDirectory())\r
-                try {\r
-                    return getOMCVersion(omhome);\r
-                } catch (IOException e) {}\r
+                return omhome;\r
         }\r
         return null;\r
     }\r
     \r
-    public static String getBuiltInOpenModelicaVersion() {\r
-        return builtInOMVersionName;\r
-    }\r
-\r
-    \r
-    private static String omcVersion = getOMCVersion();\r
-    /**\r
-     * Get version of the OpenModelica that is used by this software\r
-     * @return\r
-     * @throws IOException\r
-     */\r
-    public static String getOMCVersion() {\r
-        if(omcVersion == null) {\r
-            // Add OMC as the first parameter\r
-            File openModelicaHome = getModelicaHome();\r
-            try {\r
-                omcVersion = getOMCVersion(openModelicaHome);\r
-            } catch (IOException e) {\r
-                omcVersion = null;\r
+    public static File getBuiltinOpenModelicaHome() {\r
+        Bundle bundle = Platform.getBundle("org.simantics.openmodelica.win32");\r
+        if (bundle != null) {\r
+            try{\r
+                URL entry = bundle.getEntry("/");\r
+                if(entry != null) {\r
+                    URL fileURL = FileLocator.toFileURL(entry);\r
+                    File root = new File( URLDecoder.decode(fileURL.getPath(), "UTF-8") );\r
+                    File f = new File(root, "OpenModelica" + builtInOMVersion);\r
+                    return f;\r
+                }\r
+            }\r
+            catch (Exception e) {\r
+                e.printStackTrace();\r
             }\r
         }\r
-        return omcVersion;\r
+        return null;\r
     }\r
 \r
     \r
-    private static String getOMCVersion(File OMHome) throws IOException {\r
+    public static String getOMCVersion(File OMHome) throws IOException {\r
         ArrayList<String> parameters = new ArrayList<String>();\r
         parameters.add(OMHome + "\\bin\\omc.exe");\r
         parameters.add("++v");\r
@@ -459,7 +432,7 @@ public class ModelicaManager {
         // Create full .mo for parameter comparison\r
         ArrayList<String> parameters = new ArrayList<String>();\r
         parameters.add(simulationLocation.fullMosFile.getAbsolutePath());\r
-        Process process = runOMC(simulationLocation.simulationDir, monitor, parameters);\r
+        Process process = runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, monitor, parameters);\r
         try {\r
             process.waitFor();\r
         } catch (InterruptedException e) {\r
@@ -480,7 +453,7 @@ public class ModelicaManager {
     public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException {\r
         ArrayList<String> parameters = new ArrayList<String>();\r
         parameters.add(simulationLocation.mosFile.getAbsolutePath());\r
-        runOMC(simulationLocation.simulationDir, monitor, parameters);\r
+        runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, monitor, parameters);\r
         createFullMo(simulationLocation, monitor);\r
 \r
         if(!simulationLocation.executableFile.isFile())\r
@@ -496,9 +469,19 @@ public class ModelicaManager {
      * @throws ModelicaException\r
      */\r
     public static Process runOMC(File workDir, IModelicaMonitor monitor, List<String> parameters) throws ModelicaException {\r
+        return runOMC(workDir, getModelicaHome(), monitor, parameters);\r
+    }\r
+    /**\r
+     * Generic method for running omc with some parameters\r
+     * \r
+     * @param workDir Working directory. (Usually the directory of scriptfile) \r
+     * @param monitor IModelicaMonitor for printing modelica output\r
+     * @param openModelicaHome OpenModelica home folder\r
+     * @throws ModelicaException\r
+     */\r
+    public static Process runOMC(File workDir, File openModelicaHome, IModelicaMonitor monitor, List<String> parameters) throws ModelicaException {\r
         try {\r
             // Add OMC as the first parameter\r
-            File openModelicaHome = getModelicaHome();\r
             parameters.add(0, openModelicaHome + "\\bin\\omc.exe");\r
             \r
             // Create the build process\r
@@ -557,7 +540,7 @@ public class ModelicaManager {
             commands.add(simulationLocation.executableFile.getAbsolutePath());\r
             // Write new initial values (parameters). No need to update xml if structure has changed. In that case also xml is up-to-date\r
             if(parameterChanges != null) {\r
-                String version = getOMCVersion();\r
+                String version = getOMCVersion(simulationLocation.omcHome);\r
                 if(version.startsWith("1.9")) {\r
                     // Handled in experiment\r
                     updateInitFile(simulationLocation, experimentParameters, parameterChanges);\r
@@ -670,7 +653,7 @@ public class ModelicaManager {
             ArrayList<String> parameters = new ArrayList<String>();\r
             parameters.add(simulationLocation.fullMosFile.getAbsolutePath());\r
             \r
-            Process process = runOMC(simulationLocation.simulationDir, null, parameters);\r
+            Process process = runOMC(simulationLocation.simulationDir, simulationLocation.omcHome, null, parameters);\r
             process.waitFor();\r
             \r
             trimExtraFromFullModel(simulationLocation);\r
index 66149d9fd303024606ce358157f0a6775206b785..91ef37b8516edb167a87ac0bfc53d00a1fa574e7 100644 (file)
@@ -39,6 +39,7 @@ public class SimulationLocation {
         this.initFile = initFile;\r
         this.executableFile = exeFile;\r
         this.fullModel = new File(getFullModelPath());\r
+        this.omcHome = ModelicaManager.getModelicaHome();\r
     }\r
     \r
     public void setOMCHomeFolder(File omcHome) {\r
diff --git a/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java b/org.simantics.modelica/src/org/simantics/modelica/preferences/ModelicaPreferenceInitializer.java
new file mode 100644 (file)
index 0000000..21a37d6
--- /dev/null
@@ -0,0 +1,37 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modelica.preferences;\r
+\r
+import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;\r
+import org.eclipse.core.runtime.preferences.DefaultScope;\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
+}\r
diff --git a/org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java b/org.simantics.modelica/src/org/simantics/modelica/preferences/OpenModelicaPreferences.java
new file mode 100644 (file)
index 0000000..3e36ef3
--- /dev/null
@@ -0,0 +1,20 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modelica.preferences;\r
+\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 eaa18d62252ad104287582dbeed780971fa20cbc..493f996bcfed3771ccc1ee20952891774c526fcc 100644 (file)
       </page>\r
    </extension>\r
    -->\r
+   <extension\r
+         point="org.eclipse.core.runtime.preferences">\r
+      <initializer\r
+            class="org.simantics.modelica.preferences.ModelicaPreferenceInitializer">\r
+      </initializer>\r
+   </extension>\r
 </plugin>\r
index 82b67ec35940103438ff7dc05afc845b5bf97675..5810dd668349532b5240b830ba469d42c23e7adb 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.modelica;\r
 \r
+import java.io.File;\r
+import java.io.IOException;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
 import org.eclipse.core.runtime.CoreException;\r
 import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
 import org.eclipse.jface.operation.IRunnableContext;\r
+import org.eclipse.jface.preference.IPreferenceStore;\r
 import org.eclipse.jface.text.Document;\r
 import org.eclipse.jface.text.IDocument;\r
 import org.eclipse.jface.text.PaintManager;\r
@@ -33,8 +37,11 @@ import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.PartInitException;\r
 import org.eclipse.ui.PlatformUI;\r
 import org.eclipse.ui.editors.text.TextEditor;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
 import org.eclipse.ui.texteditor.AbstractDocumentProvider;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.modelica.ModelicaManager;\r
+import org.simantics.modelica.preferences.OpenModelicaPreferences;\r
 import org.simantics.sysdyn.modelica.ModelicaWriter;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.IElement;\r
@@ -90,8 +97,19 @@ public class SysdynModelicaEditor extends TextEditor {
                     HashSet<Configuration> configurations = new HashSet<Configuration>();\r
                     configurations.add(configuration);\r
                     getConfigurations(configuration, configurations);\r
-\r
-                    return new Document(ModelicaWriter.write(configurations, RepresentationUtils.isGameExperimentActive()));\r
+                    \r
+                    IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE,  \r
+                            org.simantics.modelica.Activator.PLUGIN_ID);  \r
+                    String omHome = store.getString(OpenModelicaPreferences.OM_HOME);\r
+                    String version = null;\r
+                    if(omHome != null) {\r
+                        File omHomeDir = new File(omHome);\r
+                        if(omHomeDir != null && omHomeDir.isDirectory())\r
+                            try {\r
+                                version = ModelicaManager.getOMCVersion(omHomeDir);\r
+                            } catch (IOException e) { }\r
+                    }\r
+                    return new Document(ModelicaWriter.write(configurations, RepresentationUtils.isGameExperimentActive(), version));\r
                 } catch (DatabaseException e) {\r
                     e.printStackTrace();\r
                     throw new CoreException(STATUS_ERROR);\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferenceInitializer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/ModelicaPreferenceInitializer.java
deleted file mode 100644 (file)
index 2014e3a..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*******************************************************************************\r
- * Copyright (c) 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.sysdyn.ui.preferences;\r
-\r
-import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;\r
-import org.eclipse.jface.preference.IPreferenceStore;\r
-import org.simantics.sysdyn.ui.Activator;\r
-\r
-public class ModelicaPreferenceInitializer  extends AbstractPreferenceInitializer {\r
-\r
-    public ModelicaPreferenceInitializer() {\r
-    }\r
-\r
-    @Override\r
-    public void initializeDefaultPreferences() {\r
-      IPreferenceStore store = Activator.getDefault().getPreferenceStore();\r
-      store.setDefault(ModelicaPreferencePage.OM_VERSION, ModelicaPreferencePage.OM_BUILTIN);\r
-    }\r
-\r
-}\r
index 63dbde4e22834204949512985946f2ae157b0e5a..8fb7339312a13aefb607ecccc42d4b6611fce91b 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.preferences;\r
 \r
+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.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.ui.IWorkbench;\r
 import org.eclipse.ui.IWorkbenchPreferencePage;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.modelica.Activator;\r
 import org.simantics.modelica.ModelicaManager;\r
-import org.simantics.sysdyn.ui.Activator;\r
+import org.simantics.modelica.preferences.OpenModelicaPreferences;\r
 \r
 public class ModelicaPreferencePage extends FieldEditorPreferencePage implements\r
 IWorkbenchPreferencePage {\r
 \r
-    public static String OM_VERSION = "OpenModelica Version";\r
-    public static String OM_BUILTIN = "Built-in";\r
-    public static String OM_INSTALLED = "Installed";\r
-    \r
+    private static String CUSTOM_PATH = "Custom path";\r
+\r
+    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
+                org.simantics.modelica.Activator.PLUGIN_ID);  \r
+        setPreferenceStore(store);  \r
+\r
+        setDescription("Modelica preferences");\r
     }\r
 \r
     public void createFieldEditors() {\r
-        String installed = ModelicaManager.getInstalledOpenModelicaVersion();\r
-        String builtIn = ModelicaManager.getBuiltInOpenModelicaVersion();\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
+        String installedVersion = null;\r
+        String builtInVersion = null;\r
+        try {\r
+            if(installed != null)\r
+                installedVersion = ModelicaManager.getOMCVersion(installed);\r
+\r
+            builtInVersion = ModelicaManager.getOMCVersion(builtIn);\r
+        } catch (IOException e) {\r
+        }\r
+\r
 \r
         String[][] options;\r
         if(installed != null) {\r
             options = new String[][] \r
-                    {{"OpenModelica " + installed + " (local installation)", OM_INSTALLED },\r
-                        {"OpenModelica " + builtIn + " (built-in)", OM_BUILTIN}};\r
+                    {{CUSTOM_PATH, CUSTOM_PATH},\r
+                    {"Local installation (" + installedVersion + ")", installed.getAbsolutePath() },\r
+                    {"Built-in (" + builtInVersion + ")", builtIn.getAbsolutePath()},\r
+                    };\r
         } else {\r
             options = new String[][] \r
-                    {{"OpenModelica " + builtIn + " (built-in)", OM_BUILTIN}}; \r
+                    {{CUSTOM_PATH, CUSTOM_PATH},\r
+                    {"Built-in (" + builtInVersion + ")", builtIn.getAbsolutePath()},\r
+                    }; \r
         }\r
-        \r
-        RadioGroupFieldEditor rg = new RadioGroupFieldEditor(OM_VERSION,\r
-                "Choose the used OpenModelica version", 1,\r
-                options, getFieldEditorParent());\r
-        \r
+\r
+        rg = new RadioGroupFieldEditor(OpenModelicaPreferences.OM_HOME,\r
+                "&Choose the used OpenModelica version", 1,\r
+                options, getFieldEditorParent()) {\r
+\r
+            @Override\r
+            protected void doStore() {\r
+                // Do nothing. Path handles saving the value\r
+            }\r
+\r
+            @Override\r
+            protected void fireValueChanged(String property, Object oldValue, Object newValue) {\r
+                if(newValue.equals(CUSTOM_PATH)) {\r
+\r
+                } else {\r
+                    path.setStringValue((String)newValue);\r
+                }              \r
+            }\r
+        };\r
+\r
+\r
+//        boolean isAuto = getPreferenceStore().getBoolean(OpenModelicaPreferences.OM_AUTO_DETECT);\r
+//        rg.setEnabled(!isAuto, getFieldEditorParent());\r
+\r
         addField(rg);\r
+\r
+        path = new DirectoryFieldEditor(OpenModelicaPreferences.OM_HOME, \r
+                "&Modelica Home:", getFieldEditorParent()) {\r
+            @Override\r
+            public void load() {\r
+                super.load();\r
+//                if (auto.getBooleanValue()) {\r
+                    setStringValue(ModelicaManager.getDefaultModelicaHome().getAbsolutePath());\r
+//                }\r
+            }\r
+\r
+            @Override\r
+            public void store() {\r
+                super.store();\r
+                IScopeContext context = DefaultScope.INSTANCE;\r
+                Preferences node = context.getNode(Activator.PLUGIN_ID);\r
+                node.put(OpenModelicaPreferences.OM_HOME, getStringValue());\r
+            }\r
+        };\r
+        addField(path);\r
     }\r
 \r
     @Override\r
     public void init(IWorkbench workbench) {\r
-        setPreferenceStore(Activator.getDefault().getPreferenceStore());\r
-//        setDescription("");\r
+    }\r
+\r
+    @Override\r
+    protected void performDefaults() {\r
+        super.performDefaults();\r
+//        rg.setEnabled(!auto.getBooleanValue(), getFieldEditorParent());\r
     }\r
 \r
 }\r
index 35c6e9b361550bb5f7a38037a73d925d10b523d7..4030528539a0d20bc96fa6845ce8b1546e546c81 100644 (file)
@@ -26,6 +26,9 @@ import java.util.List;
 import java.util.concurrent.locks.Lock;\r
 \r
 import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.preferences.DefaultScope;\r
+import org.eclipse.core.runtime.preferences.IScopeContext;\r
+import org.osgi.service.prefs.Preferences;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.Session;\r
@@ -45,6 +48,7 @@ import org.simantics.modelica.SimulationLocation;
 import org.simantics.modelica.data.CSVSimulationResult;\r
 import org.simantics.modelica.data.MatSimulationResult;\r
 import org.simantics.modelica.data.SimulationResult;\r
+import org.simantics.modelica.preferences.OpenModelicaPreferences;\r
 import org.simantics.simulation.data.Datasource;\r
 import org.simantics.simulation.experiment.Experiment;\r
 import org.simantics.simulation.experiment.ExperimentState;\r
@@ -188,11 +192,11 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
      * @param isGame is the experiment a "game experiment". This is needed for modifying the code to work with FMU simulation\r
      * @return Modelica code\r
      */\r
-    protected String getModelicaCode(IModelicaMonitor monitor, boolean isGame) {\r
+    protected String getModelicaCode(IModelicaMonitor monitor, boolean isGame, String modelicaVersion) {\r
         String modelText = null;\r
         try {\r
             // Write all configurations once\r
-            modelText = ModelicaWriter.write(sysdynModel.getModules(), isGame);\r
+            modelText = ModelicaWriter.write(sysdynModel.getModules(), isGame, modelicaVersion);\r
         } catch (Exception e) {\r
             // Stop experiment and show console\r
             /*setExperimentStopped(experiment); =>*/ simulate(false);\r
@@ -448,7 +452,9 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
         progressMonitor.subTask("Write modelica classes");\r
 \r
         // Write Modelica files\r
-        String modelText = getModelicaCode(monitor, false);\r
+\r
+        \r
+        String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion());\r
         if(modelText == null)\r
             return;\r
 \r
@@ -478,7 +484,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
         // Add changed parameters in case that structure has not changed\r
         HashMap<String, String> changes = structureChanged ? null : new HashMap<String, String>();\r
         if (omcVersion == null) {\r
-            omcVersion = ModelicaManager.getOMCVersion();\r
+            omcVersion = ModelicaManager.getOMCVersion(simulationLocation.omcHome);\r
         }\r
         if(!structureChanged && previousParameters != null && omcVersion.startsWith("1.9")) {\r
             try {\r
@@ -506,6 +512,33 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment,
         process = null;\r
     }\r
 \r
+    /**\r
+     * Get the version of the OpenModelica compiler that is defined to be used\r
+     * in Preferences\r
+     * @return OpenModelica version\r
+     */\r
+    protected String getOpenModelicaVersion() {\r
+        String omVersion = null;\r
+\r
+        try {\r
+            IScopeContext context = DefaultScope.INSTANCE;\r
+            Preferences node = context.getNode(org.simantics.modelica.Activator.PLUGIN_ID);\r
+            String omHome = node.get(OpenModelicaPreferences.OM_HOME, null);\r
+            if(omHome != null) {\r
+                File omHomeDir = new File(omHome);\r
+                if(omHomeDir != null && omHomeDir.isDirectory())\r
+                    omVersion = ModelicaManager.getOMCVersion(omHomeDir);\r
+            }\r
+\r
+            if(omVersion == null) {\r
+                omVersion = ModelicaManager.getOMCVersion(ModelicaManager.getModelicaHome());\r
+            }\r
+            \r
+        } catch (IOException e) {\r
+        }\r
+        \r
+        return omVersion;\r
+    }\r
 \r
 \r
     /**\r
index 77332a9e7c2601a20edec6a41d57562c6e3c14a1..f105897817f32608df12318c313deda5139d1fec 100644 (file)
@@ -153,8 +153,8 @@ public class SysdynGameExperiment extends SysdynExperiment {
 \r
                progressMonitor.subTask("Write modelica classes");\r
 \r
-               // Write Modelica files\r
-               String modelText = getModelicaCode(monitor, true);\r
+               // Write Modelica files       \r
+               String modelText = getModelicaCode(monitor, true, getOpenModelicaVersion());\r
                if(modelText == null)\r
                        return;\r
 \r
index 2ced39d8571b358645c10f08c095bf0c673b0a4f..3c2934291966bf314ed5349f777c0a24d0b166a3 100644 (file)
@@ -18,7 +18,6 @@ import java.util.HashMap;
 import java.util.HashSet;\r
 import java.util.List;\r
 \r
-import org.simantics.modelica.ModelicaManager;\r
 import org.simantics.sysdyn.representation.Book;\r
 import org.simantics.sysdyn.representation.Configuration;\r
 import org.simantics.sysdyn.representation.Dependency;\r
@@ -47,7 +46,7 @@ public class ModelicaWriter {
      * @param Configurations Configurations, one main configuration and possible modules\r
      * @return Complete Modelica code of a model\r
      */\r
-    public static String write(Collection<Configuration> configurations, boolean isGame) {\r
+    public static String write(Collection<Configuration> configurations, boolean isGame, String omVersion) {\r
         Configuration modelConf = null;\r
         for(Configuration conf : configurations) {\r
             if(conf.getModel() != null) {\r
@@ -86,9 +85,7 @@ public class ModelicaWriter {
         b.append("end " + modelName + ";\n\n");\r
         \r
         // Insert spreadsheets\r
-        String version = null;\r
-        version = ModelicaManager.getOMCVersion();\r
-        if(version != null && version.startsWith("1.9")) {\r
+        if(omVersion != null && omVersion.startsWith("1.9")) {\r
             b.insert(spreadsheetlocation, getGlobalSpreadSheets(configurations));\r
         } else {\r
             b.append(getGlobalSpreadSheets(configurations));\r