]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / EnumerationVariableModifier2.java
index 18fd778fb4330d1c959301763a3685792bac1b69..48c4ab4fb5bc207e83828f4226e64bb69e280e7f 100644 (file)
-/*******************************************************************************\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.browsing.ui.graph.impl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.UndoContext;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.utils.datastructures.Callback;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class EnumerationVariableModifier2 implements EnumerationModifier {\r
-\r
-    protected final Session      session;\r
-    protected final UndoContext  undoContext;\r
-    protected final Variable     variable;\r
-    protected final Set<String> allowedValues;\r
-    protected final String       defaultValue;\r
-\r
-    protected Throwable        modifierFailed;\r
-\r
-    public EnumerationVariableModifier2(RequestProcessor processor, UndoContext undoContext, Variable variable) {\r
-\r
-       this.session = processor.getSession();\r
-        this.undoContext = undoContext;\r
-        this.variable = variable;\r
-        this.allowedValues = computeAllowedValues(processor, variable);\r
-        this.defaultValue = computeDefaultValue(processor, variable);\r
-        \r
-    }\r
-\r
-    protected String computeDefaultValue(RequestProcessor processor, final Variable variable) {\r
-        try {\r
-           return processor.syncRequest(new Read<String>() {\r
-                @Override\r
-                public String perform(ReadGraph graph) throws DatabaseException {\r
-                       return variable.getPossiblePropertyValue(graph, Variables.LABEL);\r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            return "";\r
-        }\r
-    }\r
-    \r
-    protected Set<String> computeAllowedValues(RequestProcessor processor, final Variable variable) {\r
-        try {\r
-           return processor.syncRequest(new Read<Set<String>>() {\r
-                @Override\r
-                public Set<String> perform(ReadGraph graph) throws DatabaseException {\r
-\r
-                       Layer0 L0 = Layer0.getInstance(graph);\r
-                       Set<String> result = new HashSet<String>();\r
-                       Resource literal = variable.getPossibleRepresents(graph);\r
-                       Resource type = graph.getSingleObject(literal, L0.PartOf);\r
-                       for(Resource lit : graph.syncRequest(new ObjectsWithType(type, L0.ConsistsOf, type))) {\r
-                               String label = graph.getPossibleRelatedValue(lit, L0.HasLabel);\r
-                               if(label == null) label = graph.getRelatedValue(lit, L0.HasName);\r
-                               result.add(label);\r
-                       }\r
-                       return result;\r
-                       \r
-                }\r
-            });\r
-        } catch (DatabaseException e) {\r
-            return Collections.emptySet();\r
-        }\r
-    }\r
-\r
-    public class Write extends WriteRequest {\r
-\r
-        final private Variable variable;\r
-        final private String label;\r
-\r
-        public Write(Variable variable, String label) {\r
-            super((VirtualGraph)null);\r
-            this.variable = variable;\r
-            this.label = label;\r
-        }\r
-\r
-        @Override\r
-        public void perform(WriteGraph graph) throws DatabaseException {\r
-               variable.setValue(graph, label, Bindings.STRING);\r
-        }\r
-\r
-    }\r
-    \r
-    protected void doModify(final String label) {\r
-        session.asyncRequest(new Write(variable, label),\r
-                new Callback<DatabaseException>() {\r
-            @Override\r
-            public void run(DatabaseException parameter) {\r
-                if (parameter != null)\r
-                    ErrorLogger.defaultLogError(parameter);\r
-            }\r
-        });\r
-    }\r
-\r
-    @Override\r
-    public String getValue() {\r
-        return defaultValue;\r
-    }\r
-\r
-    @Override\r
-    public String isValid(String label) {\r
-        if (modifierFailed != null)\r
-            return "Could not resolve validator for this value, modification denied. Reason: "\r
-            + modifierFailed.getMessage();\r
-        // Validity should already be enforced by the editing UI for\r
-        // enumerations.\r
-        return null;\r
-    }\r
-\r
-    @Override\r
-    public final void modify(String label) {\r
-        if (modifierFailed != null)\r
-            // Should never end up here, isValid should prevent it.\r
-            throw new Error("modifier failed: " + modifierFailed.getMessage());\r
-        doModify(label);\r
-    }\r
-\r
-    @Override\r
-    public List<String> getValues() {\r
-       ArrayList<String> result = new ArrayList<String>();\r
-       result.addAll(allowedValues);\r
-       return result;\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.browsing.ui.graph.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.UndoContext;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.request.Read;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.datastructures.Callback;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class EnumerationVariableModifier2 implements EnumerationModifier {
+
+    protected final Session      session;
+    protected final UndoContext  undoContext;
+    protected final Variable     variable;
+    protected final Set<String> allowedValues;
+    protected final String       defaultValue;
+
+    protected Throwable        modifierFailed;
+
+    public EnumerationVariableModifier2(RequestProcessor processor, UndoContext undoContext, Variable variable) {
+
+       this.session = processor.getSession();
+        this.undoContext = undoContext;
+        this.variable = variable;
+        this.allowedValues = computeAllowedValues(processor, variable);
+        this.defaultValue = computeDefaultValue(processor, variable);
+        
+    }
+
+    protected String computeDefaultValue(RequestProcessor processor, final Variable variable) {
+        try {
+           return processor.syncRequest(new Read<String>() {
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+                       return variable.getPossiblePropertyValue(graph, Variables.LABEL);
+                }
+            });
+        } catch (DatabaseException e) {
+            return "";
+        }
+    }
+    
+    protected Set<String> computeAllowedValues(RequestProcessor processor, final Variable variable) {
+        try {
+           return processor.syncRequest(new Read<Set<String>>() {
+                @Override
+                public Set<String> perform(ReadGraph graph) throws DatabaseException {
+
+                       Layer0 L0 = Layer0.getInstance(graph);
+                       Set<String> result = new HashSet<String>();
+                       Resource literal = variable.getPossibleRepresents(graph);
+                       Resource type = graph.getSingleObject(literal, L0.PartOf);
+                       for(Resource lit : graph.syncRequest(new ObjectsWithType(type, L0.ConsistsOf, type))) {
+                               String label = graph.getPossibleRelatedValue(lit, L0.HasLabel);
+                               if(label == null) label = graph.getRelatedValue(lit, L0.HasName);
+                               result.add(label);
+                       }
+                       return result;
+                       
+                }
+            });
+        } catch (DatabaseException e) {
+            return Collections.emptySet();
+        }
+    }
+
+    public class Write extends WriteRequest {
+
+        final private Variable variable;
+        final private String label;
+
+        public Write(Variable variable, String label) {
+            super((VirtualGraph)null);
+            this.variable = variable;
+            this.label = label;
+        }
+
+        @Override
+        public void perform(WriteGraph graph) throws DatabaseException {
+               variable.setValue(graph, label, Bindings.STRING);
+        }
+
+    }
+    
+    protected void doModify(final String label) {
+        session.asyncRequest(new Write(variable, label),
+                new Callback<DatabaseException>() {
+            @Override
+            public void run(DatabaseException parameter) {
+                if (parameter != null)
+                    ErrorLogger.defaultLogError(parameter);
+            }
+        });
+    }
+
+    @Override
+    public String getValue() {
+        return defaultValue;
+    }
+
+    @Override
+    public String isValid(String label) {
+        if (modifierFailed != null)
+            return "Could not resolve validator for this value, modification denied. Reason: "
+            + modifierFailed.getMessage();
+        // Validity should already be enforced by the editing UI for
+        // enumerations.
+        return null;
+    }
+
+    @Override
+    public final void modify(String label) {
+        if (modifierFailed != null)
+            // Should never end up here, isValid should prevent it.
+            throw new Error("modifier failed: " + modifierFailed.getMessage());
+        doModify(label);
+    }
+
+    @Override
+    public List<String> getValues() {
+       ArrayList<String> result = new ArrayList<String>();
+       result.addAll(allowedValues);
+       return result;
+    }
+
+};