1 package org.simantics.interop.update.model;
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;
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;
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;
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.");
40 this.changes = changes;
41 this.leftSubject = left;
42 this.rightSubject = right;
46 public Resource getFirstSubject() {
50 public Statement getFirst() {
54 public Resource getSecondSubject() {
58 public Statement getSecond() {
62 public Resource getPredicate() {
63 if (pair.first != null)
64 return pair.first.getPredicate();
65 return pair.second.getPredicate();
68 public GraphChanges getChanges() {
73 public int hashCode() {
74 return pair.hashCode();
78 public boolean equals(Object obj) {
81 if (obj.getClass() != this.getClass())
83 PropertyChange c = (PropertyChange)obj;
84 if (!leftSubject.equals(c.leftSubject))
86 if (!rightSubject.equals(c.rightSubject))
88 if (pair.first != null && pair.first.equals(c.pair.first))
90 if (pair.second != null && pair.second.equals(c.pair.second))
95 public void apply(WriteGraph graph) throws DatabaseException {
98 if (pair.second == null) {
99 graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
102 Layer0 L0 = Layer0.getInstance(graph);
103 Resource s = leftSubject;
104 if (graph.isInstanceOf(pair.second.getObject(), L0.Literal)) {
106 if (customValue != null)
108 else if (graph.hasValue(pair.second.getObject())) {
109 value = graph.getValue(pair.second.getObject());
111 Resource pred = pair.second.getPredicate();
112 if (getChanges().getComparable().containsRight(pred))
113 pred = getChanges().getComparable().getLeft(pred);
117 graph.claimLiteral(s, pred, value);
121 } else if (graph.isInstanceOf(pair.second.getObject(), L0.SCLValue)) {
122 Resource pred = pair.second.getPredicate();
124 Resource valueResource = graph.newResource();
125 graph.claim(valueResource, L0.InstanceOf, graph.getSingleObject(pair.second.getObject(), L0.InstanceOf));
126 AddDeleteUpdateOp.copyProperties(graph, pair.second.getObject(), valueResource);
127 graph.claim(s, pred, valueResource);
133 * Sets selected state.
135 * @return true if selection state was changed
137 public boolean select(boolean select) {
142 this.selected = select;
147 * Is change selected.
150 public boolean selected() {
155 * Has change been applied
158 public boolean applied() {
166 public boolean isVisible() {
170 public void setVisible(boolean visible) {
171 this.visible = visible;
175 * Is change enabled. Disabled changes do not allow changing selected state.
178 public boolean enabled() {
182 public void setEnabled(boolean enabled) {
183 this.enabled = enabled;
187 public String toString() {
188 String s = "PropertyChange";
189 if (pair.first != null)
190 s += " L(" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
191 if (pair.second != null)
192 s += " R(" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
204 public Object getCustomValue() {
208 public void setCustomValue(Object customValue) {
209 this.customValue = customValue;