-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in 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.modeling.ui.preferences;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
-import org.eclipse.jface.preference.FieldEditor;\r
-import org.eclipse.jface.preference.FieldEditorPreferencePage;\r
-import org.eclipse.jface.preference.IPreferenceStore;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.SelectionAdapter;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.widgets.Combo;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.ui.IWorkbench;\r
-import org.eclipse.ui.IWorkbenchPreferencePage;\r
-import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.diagram.flag.DiagramFlagPreferences;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.utils.datastructures.map.Tuple;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class DiagramFlagPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {\r
-\r
- private SchemeSelector scheme;\r
- private List<LabelingScheme> schemes;\r
-\r
- public DiagramFlagPreferencePage() {\r
- super(FLAT);\r
- }\r
-\r
- @Override\r
- public void init(IWorkbench workbench) {\r
- }\r
-\r
- @Override\r
- protected IPreferenceStore doGetPreferenceStore() {\r
- return new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
- }\r
-\r
- static class LabelingScheme extends Tuple {\r
- public LabelingScheme(String label, String description, String uri, Resource scheme) {\r
- super(label, description, uri, scheme);\r
- }\r
- public String getLabel() {\r
- return (String) getField(0);\r
- }\r
- public String getDescription() {\r
- return (String) getField(1);\r
- }\r
- public String getURI() {\r
- return (String) getField(2);\r
- }\r
- public Resource getResource() {\r
- return (Resource) getField(3);\r
- }\r
- }\r
-\r
- static class SchemeSelector extends FieldEditor {\r
-\r
- /**\r
- * The <code>Combo</code> widget.\r
- */\r
- private Combo fCombo;\r
-\r
- /**\r
- * The value (not the name) of the currently selected item in the Combo widget.\r
- */\r
- private LabelingScheme fValue;\r
-\r
- private LabelingScheme fOriginalValue;\r
-\r
- private LabelingScheme fDefaultValue;\r
-\r
- private List<LabelingScheme> fValues;\r
-\r
- /**\r
- * Create the combo box field editor.\r
- * \r
- * @param label the name of the preference this field editor works on\r
- * @param labelText the label text of the field editor\r
- * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be\r
- * arranged as: { {name1, value1}, {name2, value2}, ...}\r
- * @param parent the parent composite\r
- */\r
- public SchemeSelector(String labelText, List<LabelingScheme> values, LabelingScheme originalValue, LabelingScheme defaultValue, Composite parent) {\r
- init("labelingScheme", labelText);\r
- fValues = values;\r
- fOriginalValue = originalValue;\r
- fDefaultValue = defaultValue;\r
- createControl(parent);\r
- }\r
-\r
- protected void adjustForNumColumns(int numColumns) {\r
- if (numColumns > 1) {\r
- Control control = getLabelControl();\r
- int left = numColumns;\r
- if (control != null) {\r
- ((GridData)control.getLayoutData()).horizontalSpan = 1;\r
- left = left - 1;\r
- }\r
- ((GridData)fCombo.getLayoutData()).horizontalSpan = left;\r
- } else {\r
- Control control = getLabelControl();\r
- if (control != null) {\r
- ((GridData)control.getLayoutData()).horizontalSpan = 1;\r
- }\r
- ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;\r
- }\r
- }\r
-\r
- protected void doFillIntoGrid(Composite parent, int numColumns) {\r
- int comboC = 1;\r
- if (numColumns > 1) {\r
- comboC = numColumns - 1;\r
- }\r
- Control control = getLabelControl(parent);\r
- GridData gd = new GridData();\r
- gd.horizontalSpan = 1;\r
- control.setLayoutData(gd);\r
- control = getComboBoxControl(parent);\r
- gd = new GridData();\r
- gd.horizontalSpan = comboC;\r
- gd.horizontalAlignment = GridData.FILL;\r
- control.setLayoutData(gd);\r
- control.setFont(parent.getFont());\r
- }\r
-\r
- public int getNumberOfControls() {\r
- return 2;\r
- }\r
-\r
- private Combo getComboBoxControl(Composite parent) {\r
- if (fCombo == null) {\r
- fCombo = new Combo(parent, SWT.READ_ONLY);\r
- fCombo.setFont(parent.getFont());\r
- for (int i = 0; i < fValues.size(); i++) {\r
- fCombo.add(fValues.get(i).getLabel(), i);\r
- }\r
-\r
- fCombo.addSelectionListener(new SelectionAdapter() {\r
- public void widgetSelected(SelectionEvent evt) {\r
- LabelingScheme oldValue = fValue;\r
- String name = fCombo.getText();\r
- fValue = getValueForName(name);\r
- setPresentsDefaultValue(false);\r
- fireValueChanged(VALUE, oldValue, fValue);\r
- }\r
- });\r
- }\r
- return fCombo;\r
- }\r
-\r
- private LabelingScheme getValueForName(String name) {\r
- for (int i = 0; i < fValues.size(); i++) {\r
- LabelingScheme scheme = fValues.get(i);\r
- if (scheme.getLabel().equals(name)) {\r
- return scheme;\r
- }\r
- }\r
- return fValues.get(0);\r
- }\r
-\r
- private void updateComboForValue(LabelingScheme value) {\r
- fValue = value;\r
- for (int i = 0; i < fValues.size(); i++) {\r
- if (value.equals(fValues.get(i))) {\r
- fCombo.setText(value.getLabel());\r
- return;\r
- }\r
- }\r
- if (fValues.size() > 0) {\r
- fValue = fValues.get(0);\r
- fCombo.setText(fValue.getLabel());\r
- }\r
- }\r
-\r
- public void setEnabled(boolean enabled, Composite parent) {\r
- super.setEnabled(enabled, parent);\r
- getComboBoxControl(parent).setEnabled(enabled);\r
- }\r
-\r
- @Override\r
- protected void doLoad() {\r
- updateComboForValue(fOriginalValue);\r
- }\r
-\r
- protected void doLoadDefault() {\r
- updateComboForValue(fDefaultValue);\r
- }\r
-\r
- @Override\r
- protected void doStore() {\r
- if (fValue == null) {\r
- fOriginalValue = fDefaultValue;\r
- DiagramFlagPreferences.setProjectFlagLabelingScheme(fDefaultValue.getResource());\r
- return;\r
- }\r
- fOriginalValue = fValue;\r
- DiagramFlagPreferences.setProjectFlagLabelingScheme(fValue.getResource());\r
- }\r
-\r
- }\r
-\r
- static class FindSchemes implements Read<List<LabelingScheme>> {\r
- @Override\r
- public List<LabelingScheme> perform(ReadGraph graph) throws DatabaseException {\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- List<LabelingScheme> result = new ArrayList<LabelingScheme>();\r
- for (Resource scheme : graph.syncRequest(new ObjectsWithType(DIA.FlagLabelingScheme, L0.ConsistsOf, DIA.FlagLabelingScheme))) {\r
- String label = graph.adapt(scheme, String.class);\r
- String description = graph.getPossibleRelatedValue(scheme, L0.HasDescription);\r
- String uri = graph.getURI(scheme);\r
- result.add(new LabelingScheme(label, description, uri, scheme));\r
- }\r
- return result;\r
- }\r
- }\r
-\r
- @Override\r
- protected void createFieldEditors() {\r
- schemes = Collections.emptyList();\r
- Resource schemeValue = null;\r
- try {\r
- schemes = Simantics.getSession().syncRequest(new FindSchemes());\r
- schemeValue = DiagramFlagPreferences.getActiveFlagLabelingSchemeResource(Simantics.getSession());\r
- } catch (DatabaseException e) {\r
- ExceptionUtils.logAndShowError(e);\r
- }\r
-\r
- LabelingScheme previousValue = null;\r
- LabelingScheme defaultValue = null;\r
-\r
- for (int i = 0; i < schemes.size(); ++i) {\r
- LabelingScheme s = schemes.get(i);\r
- if (DiagramResource.URIs.FlagLabelingScheme_Alphabetical.equals(s.getURI()))\r
- defaultValue = s;\r
- }\r
- if (defaultValue == null && !schemes.isEmpty())\r
- defaultValue = schemes.get(0);\r
-\r
- if (schemeValue != null) {\r
- for (int i = 0; i < schemes.size(); ++i) {\r
- LabelingScheme s = schemes.get(i);\r
- if (schemeValue.equals(s.getResource()))\r
- previousValue = s;\r
- }\r
- }\r
- if (previousValue == null)\r
- previousValue = defaultValue;\r
-\r
- scheme = new SchemeSelector("Labeling Scheme", schemes, previousValue, defaultValue, getFieldEditorParent());\r
- addField(scheme);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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.modeling.ui.preferences;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.jface.preference.FieldEditor;
+import org.eclipse.jface.preference.FieldEditorPreferencePage;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPreferencePage;
+import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.diagram.flag.DiagramFlagPreferences;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.utils.datastructures.map.Tuple;
+import org.simantics.utils.ui.ExceptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class DiagramFlagPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
+
+ private SchemeSelector scheme;
+ private List<LabelingScheme> schemes;
+
+ public DiagramFlagPreferencePage() {
+ super(FLAT);
+ }
+
+ @Override
+ public void init(IWorkbench workbench) {
+ }
+
+ @Override
+ protected IPreferenceStore doGetPreferenceStore() {
+ return new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+ }
+
+ static class LabelingScheme extends Tuple {
+ public LabelingScheme(String label, String description, String uri, Resource scheme) {
+ super(label, description, uri, scheme);
+ }
+ public String getLabel() {
+ return (String) getField(0);
+ }
+ public String getDescription() {
+ return (String) getField(1);
+ }
+ public String getURI() {
+ return (String) getField(2);
+ }
+ public Resource getResource() {
+ return (Resource) getField(3);
+ }
+ }
+
+ static class SchemeSelector extends FieldEditor {
+
+ /**
+ * The <code>Combo</code> widget.
+ */
+ private Combo fCombo;
+
+ /**
+ * The value (not the name) of the currently selected item in the Combo widget.
+ */
+ private LabelingScheme fValue;
+
+ private LabelingScheme fOriginalValue;
+
+ private LabelingScheme fDefaultValue;
+
+ private List<LabelingScheme> fValues;
+
+ /**
+ * Create the combo box field editor.
+ *
+ * @param label the name of the preference this field editor works on
+ * @param labelText the label text of the field editor
+ * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be
+ * arranged as: { {name1, value1}, {name2, value2}, ...}
+ * @param parent the parent composite
+ */
+ public SchemeSelector(String labelText, List<LabelingScheme> values, LabelingScheme originalValue, LabelingScheme defaultValue, Composite parent) {
+ init("labelingScheme", labelText);
+ fValues = values;
+ fOriginalValue = originalValue;
+ fDefaultValue = defaultValue;
+ createControl(parent);
+ }
+
+ protected void adjustForNumColumns(int numColumns) {
+ if (numColumns > 1) {
+ Control control = getLabelControl();
+ int left = numColumns;
+ if (control != null) {
+ ((GridData)control.getLayoutData()).horizontalSpan = 1;
+ left = left - 1;
+ }
+ ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
+ } else {
+ Control control = getLabelControl();
+ if (control != null) {
+ ((GridData)control.getLayoutData()).horizontalSpan = 1;
+ }
+ ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
+ }
+ }
+
+ protected void doFillIntoGrid(Composite parent, int numColumns) {
+ int comboC = 1;
+ if (numColumns > 1) {
+ comboC = numColumns - 1;
+ }
+ Control control = getLabelControl(parent);
+ GridData gd = new GridData();
+ gd.horizontalSpan = 1;
+ control.setLayoutData(gd);
+ control = getComboBoxControl(parent);
+ gd = new GridData();
+ gd.horizontalSpan = comboC;
+ gd.horizontalAlignment = GridData.FILL;
+ control.setLayoutData(gd);
+ control.setFont(parent.getFont());
+ }
+
+ public int getNumberOfControls() {
+ return 2;
+ }
+
+ private Combo getComboBoxControl(Composite parent) {
+ if (fCombo == null) {
+ fCombo = new Combo(parent, SWT.READ_ONLY);
+ fCombo.setFont(parent.getFont());
+ for (int i = 0; i < fValues.size(); i++) {
+ fCombo.add(fValues.get(i).getLabel(), i);
+ }
+
+ fCombo.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent evt) {
+ LabelingScheme oldValue = fValue;
+ String name = fCombo.getText();
+ fValue = getValueForName(name);
+ setPresentsDefaultValue(false);
+ fireValueChanged(VALUE, oldValue, fValue);
+ }
+ });
+ }
+ return fCombo;
+ }
+
+ private LabelingScheme getValueForName(String name) {
+ for (int i = 0; i < fValues.size(); i++) {
+ LabelingScheme scheme = fValues.get(i);
+ if (scheme.getLabel().equals(name)) {
+ return scheme;
+ }
+ }
+ return fValues.get(0);
+ }
+
+ private void updateComboForValue(LabelingScheme value) {
+ fValue = value;
+ for (int i = 0; i < fValues.size(); i++) {
+ if (value.equals(fValues.get(i))) {
+ fCombo.setText(value.getLabel());
+ return;
+ }
+ }
+ if (fValues.size() > 0) {
+ fValue = fValues.get(0);
+ fCombo.setText(fValue.getLabel());
+ }
+ }
+
+ public void setEnabled(boolean enabled, Composite parent) {
+ super.setEnabled(enabled, parent);
+ getComboBoxControl(parent).setEnabled(enabled);
+ }
+
+ @Override
+ protected void doLoad() {
+ updateComboForValue(fOriginalValue);
+ }
+
+ protected void doLoadDefault() {
+ updateComboForValue(fDefaultValue);
+ }
+
+ @Override
+ protected void doStore() {
+ if (fValue == null) {
+ fOriginalValue = fDefaultValue;
+ DiagramFlagPreferences.setProjectFlagLabelingScheme(fDefaultValue.getResource());
+ return;
+ }
+ fOriginalValue = fValue;
+ DiagramFlagPreferences.setProjectFlagLabelingScheme(fValue.getResource());
+ }
+
+ }
+
+ static class FindSchemes implements Read<List<LabelingScheme>> {
+ @Override
+ public List<LabelingScheme> perform(ReadGraph graph) throws DatabaseException {
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ Layer0 L0 = Layer0.getInstance(graph);
+ List<LabelingScheme> result = new ArrayList<LabelingScheme>();
+ for (Resource scheme : graph.syncRequest(new ObjectsWithType(DIA.FlagLabelingScheme, L0.ConsistsOf, DIA.FlagLabelingScheme))) {
+ String label = graph.adapt(scheme, String.class);
+ String description = graph.getPossibleRelatedValue(scheme, L0.HasDescription);
+ String uri = graph.getURI(scheme);
+ result.add(new LabelingScheme(label, description, uri, scheme));
+ }
+ return result;
+ }
+ }
+
+ @Override
+ protected void createFieldEditors() {
+ schemes = Collections.emptyList();
+ Resource schemeValue = null;
+ try {
+ schemes = Simantics.getSession().syncRequest(new FindSchemes());
+ schemeValue = DiagramFlagPreferences.getActiveFlagLabelingSchemeResource(Simantics.getSession());
+ } catch (DatabaseException e) {
+ ExceptionUtils.logAndShowError(e);
+ }
+
+ LabelingScheme previousValue = null;
+ LabelingScheme defaultValue = null;
+
+ for (int i = 0; i < schemes.size(); ++i) {
+ LabelingScheme s = schemes.get(i);
+ if (DiagramResource.URIs.FlagLabelingScheme_Alphabetical.equals(s.getURI()))
+ defaultValue = s;
+ }
+ if (defaultValue == null && !schemes.isEmpty())
+ defaultValue = schemes.get(0);
+
+ if (schemeValue != null) {
+ for (int i = 0; i < schemes.size(); ++i) {
+ LabelingScheme s = schemes.get(i);
+ if (schemeValue.equals(s.getResource()))
+ previousValue = s;
+ }
+ }
+ if (previousValue == null)
+ previousValue = defaultValue;
+
+ scheme = new SchemeSelector("Labeling Scheme", schemes, previousValue, defaultValue, getFieldEditorParent());
+ addField(scheme);
+ }
+
+}