1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.flag;
\r
14 import java.util.Collection;
\r
15 import java.util.Collections;
\r
16 import java.util.HashSet;
\r
17 import java.util.Set;
\r
19 import org.simantics.databoard.Bindings;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.WriteGraph;
\r
23 import org.simantics.db.common.primitiverequest.OrderedSet;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.adapter.AbstractStringModifier;
\r
26 import org.simantics.diagram.stubs.DiagramResource;
\r
27 import org.simantics.layer0.Layer0;
\r
28 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
\r
31 * Makes sure that two things apply:
\r
33 * <li>If a flag is joined with another flag, both flags must have the same label property</li>
\r
34 * <li>A flag's label is unique within its own diagram</li>
\r
36 * @author Tuukka Lehtonen
\r
38 public final class FlagLabelModifier extends AbstractStringModifier {
\r
40 private Resource ownerFlag;
\r
41 private Resource relation;
\r
42 @SuppressWarnings("unused")
\r
43 private Resource property;
\r
44 private Collection<Resource> diagrams = Collections.emptySet();
\r
45 private Set<String> originalLabels = new HashSet<String>();
\r
46 private Set<String> labelsInUse = Collections.emptySet();
\r
48 public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException {
\r
50 this.ownerFlag = flag;
\r
51 this.relation = relation;
\r
52 this.property = label;
\r
56 private void initialize(ReadGraph graph) throws DatabaseException {
\r
57 if (ownerFlag != null) {
\r
58 diagrams = OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, ownerFlag);
\r
59 Resource correspondence = FlagUtil.getPossibleCounterpart(graph, ownerFlag);
\r
61 addPossibleLabel(graph, ownerFlag, originalLabels);
\r
62 if (correspondence != null)
\r
63 addPossibleLabel(graph, correspondence, originalLabels);
\r
65 refreshUsedLabels(graph);
\r
68 private void addPossibleLabel(ReadGraph graph, Resource flag, Set<String> result) throws DatabaseException {
\r
69 String label = graph.getPossibleRelatedValue(flag, Layer0.getInstance(graph).HasLabel, Bindings.STRING);
\r
70 if (label != null && !label.isEmpty())
\r
74 private void refreshUsedLabels(ReadGraph graph) throws DatabaseException {
\r
75 Set<String> used = new HashSet<String>();
\r
76 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
77 for (Resource diagram : diagrams) {
\r
78 for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {
\r
79 if (graph.isInstanceOf(element, DIA.Flag)) {
\r
80 addPossibleLabel(graph, element, used);
\r
84 //System.out.println("used labels:" + used);
\r
85 //System.out.println("original labels:" + originalLabels);
\r
86 used.removeAll(originalLabels);
\r
88 this.labelsInUse = used;
\r
89 //System.out.println("final used labels:" + used);
\r
93 public String isValid(String value) {
\r
94 if (diagrams.isEmpty() || ownerFlag == null)
\r
95 // No diagram or flag for property, cannot verify, therefore don't care.
\r
98 if (labelsInUse.contains(value))
\r
99 return "Label is already in use on the same diagram";
\r
104 final public void modify(WriteGraph graph, String value) throws DatabaseException {
\r
105 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
107 graph.claimLiteral(ownerFlag, relation, DIA.FlagLabel, value, Bindings.STRING);
\r
108 Resource counterpart = FlagUtil.getPossibleCounterpart(graph, ownerFlag);
\r
109 if (counterpart != null)
\r
110 graph.claimLiteral(counterpart, relation, DIA.FlagLabel, value, Bindings.STRING);
\r