--- /dev/null
+/*******************************************************************************\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