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