]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
5220736d4259f803da5ed032273c8e6f20d46a56
[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                 return pair.equals(c.pair);
85         }
86         
87         public void apply(WriteGraph graph) throws DatabaseException {
88                 if (applied)
89                         return;
90                 if (pair.second == null) {
91                         graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
92                         return;
93                 } 
94                 Layer0 L0 = Layer0.getInstance(graph);
95                 Resource s = leftSubject;
96                 if (graph.isInstanceOf(pair.second.getObject(), L0.Literal)) {
97                         Object value = null;
98                         if (customValue != null)
99                                 value = customValue;
100                         else if (graph.hasValue(pair.second.getObject())) {
101                                 value = graph.getValue(pair.second.getObject());
102                         }
103                         Resource pred = pair.second.getPredicate();
104                         if (getChanges().getComparable().containsRight(pred))
105                                 pred = getChanges().getComparable().getLeft(pred);
106                         
107                         if (value != null) {
108                                 graph.deny(s, pred);
109                                 graph.claimLiteral(s, pred, value);
110                         } else {
111                                 graph.deny(s,pred);
112                         }
113                 } else if (graph.isInstanceOf(pair.second.getObject(), L0.SCLValue)) {
114                         Resource pred = pair.second.getPredicate();
115                         graph.deny(s, pred);
116                         Resource valueResource = graph.newResource();
117                         graph.claim(valueResource, L0.InstanceOf, graph.getSingleObject(pair.second.getObject(), L0.InstanceOf));
118                         AddDeleteUpdateOp.copyProperties(graph, pair.second.getObject(), valueResource);
119                         graph.claim(s, pred, valueResource);
120                 }
121                 applied = true;
122         }
123         
124         /**
125          * Sets selected state. 
126          * @param select
127          * @return true if selection state was changed
128          */
129         public boolean select(boolean select) {
130             if (!enabled)
131                 return false;
132                 if (applied)
133                         return false;
134                 this.selected = select;
135                 return true;
136         }
137         
138         /**
139          * Is change selected.
140          * @return
141          */
142         public boolean selected() {
143                 return selected;
144         }
145         
146         /**
147          * Has change been applied
148          * @return
149          */
150         public boolean applied() {
151                 return applied;
152         }
153         
154         /**
155          * Is change visible
156          * @return
157          */
158         public boolean isVisible() {
159                 return visible;
160         }
161         
162         public void setVisible(boolean visible) {
163                 this.visible = visible;
164         }
165         
166         /**
167          * Is change enabled. Disabled changes do not allow changing selected state.
168          * @return
169          */
170         public boolean enabled() {
171         return enabled;
172     }
173         
174         public void setEnabled(boolean enabled) {
175         this.enabled = enabled;
176     }
177         
178         @Override
179         public String toString() {
180                 String s = "PropertyChange";
181                 if (pair.first != null)
182                         s += " L(" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
183                 if (pair.second != null)
184                         s += " R(" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
185                 if (selected)
186                     s += " selected";
187                 if (enabled)
188                     s += " enabled";
189                 if (visible)
190             s += " visible";
191                 if (applied)
192                     s += " applied";
193                 return s;
194         }
195         
196         public Object getCustomValue() {
197                 return customValue;
198         }
199         
200         public void setCustomValue(Object customValue) {
201                 this.customValue = customValue;
202         }
203         
204 }