]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
Applying enumeration (or SCL value) with model specific relation.
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / PropertyChange.java
1 package org.simantics.interop.update.model;
2
3 import org.simantics.db.Resource;
4 import org.simantics.db.Statement;
5 import org.simantics.db.WriteGraph;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.interop.test.GraphChanges;
8 import org.simantics.layer0.Layer0;
9 import org.simantics.utils.datastructures.Pair;
10
11 public class PropertyChange {
12         protected GraphChanges changes;
13         protected Resource leftSubject;
14         protected Resource rightSubject;
15         protected Pair<Statement, Statement> pair;
16         protected boolean applied = false;
17         protected boolean selected = false;
18         protected boolean visible = true;
19         protected boolean enabled = true;
20         protected Object customValue = null;
21         
22         
23         public PropertyChange(GraphChanges changes, Resource left, Statement first, Resource right, Statement second) {
24                 if (first == null && second == null)
25                         throw new IllegalArgumentException("At least one of the stamenents must be non null.");
26                 if (left == null || right == null)
27                         throw new IllegalArgumentException("Subject resources cannot be null.");
28                 this.pair = new Pair<Statement, Statement>(first, second);
29                 this.changes = changes;
30                 this.leftSubject = left;
31                 this.rightSubject = right;
32         }
33         
34         public PropertyChange(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
35                 if (change == null || (change.first == null && change.second == null))
36                         throw new IllegalArgumentException("At least one of the stamenents must be non null.");
37                 if (left == null || right == null)
38                         throw new IllegalArgumentException("Subject resources cannot be null.");
39                 this.pair = change;
40                 this.changes = changes;
41                 this.leftSubject = left;
42                 this.rightSubject = right;
43         }
44         
45         
46         public Resource getFirstSubject() {
47                 return leftSubject;
48         }
49         
50         public Statement getFirst() {
51                 return pair.first;
52         }
53         
54         public Resource getSecondSubject() {
55                 return rightSubject;
56         }
57         
58         public Statement getSecond() {
59                 return pair.second;
60         }
61         
62         public Resource getPredicate() {
63             if (pair.first != null)
64                 return pair.first.getPredicate();
65             return pair.second.getPredicate();
66         }
67         
68         public GraphChanges getChanges() {
69                 return changes;
70         }
71         
72         @Override
73         public int hashCode() {
74                 return pair.hashCode();
75         }
76         
77         @Override
78         public boolean equals(Object obj) {
79                 if (obj == null)
80                         return false;
81                 if (obj.getClass() != this.getClass())
82                         return false;
83                 PropertyChange c = (PropertyChange)obj;
84                 if (!leftSubject.equals(c.leftSubject))
85                         return false;
86                 if (!rightSubject.equals(c.rightSubject))
87                         return false;
88                 if (pair.first != null && pair.first.equals(c.pair.first))
89                         return true;
90                 if (pair.second != null && pair.second.equals(c.pair.second))
91                         return true;
92                 return false;
93         }
94         
95         public void apply(WriteGraph graph) throws DatabaseException {
96                 if (applied)
97                         return;
98                 if (pair.second == null) {
99                         graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
100                         return;
101                 } 
102                 Layer0 L0 = Layer0.getInstance(graph);
103                 Resource s = leftSubject;
104                 if (graph.isInstanceOf(pair.second.getObject(), L0.Literal)) {
105                         Object value = null;
106                         Resource type = null;
107                         if (customValue != null)
108                                 value = customValue;
109                         else if (graph.hasValue(pair.second.getObject())) {
110                                 value = graph.getValue(pair.second.getObject());
111                         }
112                         type = graph.getPossibleType(pair.second.getObject(), L0.Literal);
113                         
114                         Resource pred = _getPredicate();
115                         if (value != null) {
116                                 graph.deny(s, pred);
117                                 if (type != null) {
118                                         graph.claimLiteral(s, pred, type, value);
119                                 } else {
120                                         graph.claimLiteral(s, pred, value);
121                                 }
122                         } else {
123                                 graph.deny(s,pred);
124                         }
125                         applied = true;
126                 } else if (graph.isInstanceOf(pair.second.getObject(), L0.SCLValue)) {
127                         Resource pred = _getPredicate();
128                         graph.deny(s, pred);
129                         Resource valueResource = graph.newResource();
130                         graph.claim(valueResource, L0.InstanceOf, graph.getSingleObject(pair.second.getObject(), L0.InstanceOf));
131                         AddDeleteUpdateOp.copyProperties(graph, pair.second.getObject(), valueResource);
132                         graph.claim(s, pred, valueResource);
133                         applied = true;
134                 } else {
135                         Resource type = graph.getPossibleType(pair.second.getObject(), L0.Value);
136                         if (type != null && graph.hasStatement(type, L0.Enumeration, type)) {
137                                 Resource pred = _getPredicate();
138                                 graph.deny(s, pred);
139                                 graph.claim(s, pred, pair.second.getObject());
140                                 applied = true;
141                         }
142                         
143                 }
144                 
145         }
146         
147         private Resource _getPredicate() {
148                 Resource pred = pair.second.getPredicate();
149                 if (getChanges().getComparable().containsRight(pred))
150                         pred = getChanges().getComparable().getLeft(pred);
151                 return pred;
152         }
153         
154         /**
155          * Sets selected state. 
156          * @param select
157          * @return true if selection state was changed
158          */
159         public boolean select(boolean select) {
160             if (!enabled)
161                 return false;
162                 if (applied)
163                         return false;
164                 this.selected = select;
165                 return true;
166         }
167         
168         /**
169          * Is change selected.
170          * @return
171          */
172         public boolean selected() {
173                 return selected;
174         }
175         
176         /**
177          * Has change been applied
178          * @return
179          */
180         public boolean applied() {
181                 return applied;
182         }
183         
184         /**
185          * Is change visible
186          * @return
187          */
188         public boolean isVisible() {
189                 return visible;
190         }
191         
192         public void setVisible(boolean visible) {
193                 this.visible = visible;
194         }
195         
196         /**
197          * Is change enabled. Disabled changes do not allow changing selected state.
198          * @return
199          */
200         public boolean enabled() {
201         return enabled;
202     }
203         
204         public void setEnabled(boolean enabled) {
205         this.enabled = enabled;
206     }
207         
208         @Override
209         public String toString() {
210                 String s = "PropertyChange";
211                 if (pair.first != null)
212                         s += " L(" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
213                 if (pair.second != null)
214                         s += " R(" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
215                 if (selected)
216                     s += " selected";
217                 if (enabled)
218                     s += " enabled";
219                 if (visible)
220             s += " visible";
221                 if (applied)
222                     s += " applied";
223                 return s;
224         }
225         
226         public Object getCustomValue() {
227                 return customValue;
228         }
229         
230         public void setCustomValue(Object customValue) {
231                 if (applied) {
232                         throw new RuntimeException("Cannot change already applied value");
233                 }
234                 this.customValue = customValue;
235         }
236         
237 }