]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagLabelModifier.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / FlagLabelModifier.java
index 1d5c7a669f89091ed7e821be2d9b3b9835eb6fa4..778a1eb1481d33f36a6c889557459dc849ef4dcd 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.diagram.flag;\r
-\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.Set;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.primitiverequest.OrderedSet;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.AbstractStringModifier;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
-\r
-/**\r
- * Makes sure that two things apply:\r
- * <ol>\r
- * <li>If a flag is joined with another flag, both flags must have the same label property</li>\r
- * <li>A flag's label is unique within its own diagram</li>\r
- * </ol> \r
- * @author Tuukka Lehtonen\r
- */\r
-public final class FlagLabelModifier extends AbstractStringModifier {\r
-\r
-    private Resource             ownerFlag;\r
-    private Resource             relation;\r
-    @SuppressWarnings("unused")\r
-    private Resource             property;\r
-    private Collection<Resource> diagrams = Collections.emptySet();\r
-    private Set<String>          originalLabels = new HashSet<String>();\r
-    private Set<String>          labelsInUse = Collections.emptySet();\r
-\r
-    public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException {\r
-       super(label);\r
-        this.ownerFlag = flag;\r
-        this.relation = relation;\r
-        this.property = label;\r
-        initialize(graph);\r
-    }\r
-       \r
-    private void initialize(ReadGraph graph) throws DatabaseException {\r
-        if (ownerFlag != null) {\r
-            diagrams = OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, ownerFlag);\r
-            Resource correspondence = FlagUtil.getPossibleCounterpart(graph, ownerFlag);\r
-\r
-            addPossibleLabel(graph, ownerFlag, originalLabels);\r
-            if (correspondence != null)\r
-                addPossibleLabel(graph, correspondence, originalLabels);\r
-        }\r
-        refreshUsedLabels(graph);\r
-    }\r
-\r
-    private void addPossibleLabel(ReadGraph graph, Resource flag, Set<String> result) throws DatabaseException {\r
-        String label = graph.getPossibleRelatedValue(flag, Layer0.getInstance(graph).HasLabel, Bindings.STRING);\r
-        if (label != null && !label.isEmpty())\r
-            result.add(label);\r
-    }\r
-\r
-    private void refreshUsedLabels(ReadGraph graph) throws DatabaseException {\r
-        Set<String> used = new HashSet<String>();\r
-        DiagramResource DIA = DiagramResource.getInstance(graph);\r
-        for (Resource diagram : diagrams) {\r
-            for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {\r
-                if (graph.isInstanceOf(element, DIA.Flag)) {\r
-                    addPossibleLabel(graph, element, used);\r
-                }\r
-            }\r
-        }\r
-        //System.out.println("used labels:" + used);\r
-        //System.out.println("original labels:" + originalLabels);\r
-        used.removeAll(originalLabels);\r
-\r
-        this.labelsInUse = used;\r
-        //System.out.println("final used labels:" + used);\r
-    }\r
-\r
-    @Override\r
-    public String isValid(String value) {\r
-        if (diagrams.isEmpty() || ownerFlag == null)\r
-            // No diagram or flag for property, cannot verify, therefore don't care.\r
-            return null;\r
-\r
-        if (labelsInUse.contains(value))\r
-            return "Label is already in use on the same diagram";\r
-        return null;\r
-    }\r
-\r
-    @Override\r
-    final public void modify(WriteGraph graph, String value) throws DatabaseException {\r
-        DiagramResource DIA = DiagramResource.getInstance(graph);\r
-\r
-        graph.claimLiteral(ownerFlag, relation, DIA.FlagLabel, value, Bindings.STRING);\r
-        Resource counterpart = FlagUtil.getPossibleCounterpart(graph, ownerFlag);\r
-        if (counterpart != null)\r
-            graph.claimLiteral(counterpart, relation, DIA.FlagLabel, value, Bindings.STRING);\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.diagram.flag;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.primitiverequest.OrderedSet;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.AbstractStringModifier;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
+
+/**
+ * Makes sure that two things apply:
+ * <ol>
+ * <li>If a flag is joined with another flag, both flags must have the same label property</li>
+ * <li>A flag's label is unique within its own diagram</li>
+ * </ol> 
+ * @author Tuukka Lehtonen
+ */
+public final class FlagLabelModifier extends AbstractStringModifier {
+
+    private Resource             ownerFlag;
+    private Resource             relation;
+    @SuppressWarnings("unused")
+    private Resource             property;
+    private Collection<Resource> diagrams = Collections.emptySet();
+    private Set<String>          originalLabels = new HashSet<String>();
+    private Set<String>          labelsInUse = Collections.emptySet();
+
+    public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException {
+       super(label);
+        this.ownerFlag = flag;
+        this.relation = relation;
+        this.property = label;
+        initialize(graph);
+    }
+       
+    private void initialize(ReadGraph graph) throws DatabaseException {
+        if (ownerFlag != null) {
+            diagrams = OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, ownerFlag);
+            Resource correspondence = FlagUtil.getPossibleCounterpart(graph, ownerFlag);
+
+            addPossibleLabel(graph, ownerFlag, originalLabels);
+            if (correspondence != null)
+                addPossibleLabel(graph, correspondence, originalLabels);
+        }
+        refreshUsedLabels(graph);
+    }
+
+    private void addPossibleLabel(ReadGraph graph, Resource flag, Set<String> result) throws DatabaseException {
+        String label = graph.getPossibleRelatedValue(flag, Layer0.getInstance(graph).HasLabel, Bindings.STRING);
+        if (label != null && !label.isEmpty())
+            result.add(label);
+    }
+
+    private void refreshUsedLabels(ReadGraph graph) throws DatabaseException {
+        Set<String> used = new HashSet<String>();
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        for (Resource diagram : diagrams) {
+            for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {
+                if (graph.isInstanceOf(element, DIA.Flag)) {
+                    addPossibleLabel(graph, element, used);
+                }
+            }
+        }
+        //System.out.println("used labels:" + used);
+        //System.out.println("original labels:" + originalLabels);
+        used.removeAll(originalLabels);
+
+        this.labelsInUse = used;
+        //System.out.println("final used labels:" + used);
+    }
+
+    @Override
+    public String isValid(String value) {
+        if (diagrams.isEmpty() || ownerFlag == null)
+            // No diagram or flag for property, cannot verify, therefore don't care.
+            return null;
+
+        if (labelsInUse.contains(value))
+            return "Label is already in use on the same diagram";
+        return null;
+    }
+
+    @Override
+    final public void modify(WriteGraph graph, String value) throws DatabaseException {
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+
+        graph.claimLiteral(ownerFlag, relation, DIA.FlagLabel, value, Bindings.STRING);
+        Resource counterpart = FlagUtil.getPossibleCounterpart(graph, ownerFlag);
+        if (counterpart != null)
+            graph.claimLiteral(counterpart, relation, DIA.FlagLabel, value, Bindings.STRING);
+    }
+
+}