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