]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/DiagramFlagPreferencePage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / preferences / DiagramFlagPreferencePage.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/DiagramFlagPreferencePage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/DiagramFlagPreferencePage.java
new file mode 100644 (file)
index 0000000..670208f
--- /dev/null
@@ -0,0 +1,282 @@
+/*******************************************************************************\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