*******************************************************************************/\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
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
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
\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