]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagLabelModifier.java
Fix NPE from flagTransform
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / FlagLabelModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.flag;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Set;
18
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;
29
30 /**
31  * Makes sure that two things apply:
32  * <ol>
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>
35  * </ol> 
36  * @author Tuukka Lehtonen
37  */
38 public final class FlagLabelModifier extends AbstractStringModifier {
39
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();
47
48     public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException {
49         super(label);
50         this.ownerFlag = flag;
51         this.relation = relation;
52         this.property = label;
53         initialize(graph);
54     }
55         
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);
60
61             addPossibleLabel(graph, ownerFlag, originalLabels);
62             if (correspondence != null)
63                 addPossibleLabel(graph, correspondence, originalLabels);
64         }
65         refreshUsedLabels(graph);
66     }
67
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())
71             result.add(label);
72     }
73
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);
81                 }
82             }
83         }
84         //System.out.println("used labels:" + used);
85         //System.out.println("original labels:" + originalLabels);
86         used.removeAll(originalLabels);
87
88         this.labelsInUse = used;
89         //System.out.println("final used labels:" + used);
90     }
91
92     @Override
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.
96             return null;
97
98         if (labelsInUse.contains(value))
99             return "Label is already in use on the same diagram";
100         return null;
101     }
102
103     @Override
104     final public void modify(WriteGraph graph, String value) throws DatabaseException {
105         DiagramResource DIA = DiagramResource.getInstance(graph);
106
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);
111     }
112
113 }