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.utils.datastructures.Pair;
10 public class PropertyChange {
11 protected GraphChanges changes;
12 protected Resource leftSubject;
13 protected Resource rightSubject;
14 protected Pair<Statement, Statement> pair;
15 protected boolean applied = false;
16 protected boolean selected = false;
17 protected boolean visible = true;
18 protected boolean enabled = true;
19 protected Object customValue = null;
22 public PropertyChange(GraphChanges changes, Resource left, Statement first, Resource right, Statement second) {
23 if (first == null && second == null)
24 throw new IllegalArgumentException("At least one of the stamenents must be non null.");
25 if (left == null || right == null)
26 throw new IllegalArgumentException("Subject resources cannot be null.");
27 this.pair = new Pair<Statement, Statement>(first, second);
28 this.changes = changes;
29 this.leftSubject = left;
30 this.rightSubject = right;
33 public PropertyChange(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
34 if (change == null || (change.first == null && change.second == null))
35 throw new IllegalArgumentException("At least one of the stamenents must be non null.");
36 if (left == null || right == null)
37 throw new IllegalArgumentException("Subject resources cannot be null.");
39 this.changes = changes;
40 this.leftSubject = left;
41 this.rightSubject = right;
45 public Resource getFirstSubject() {
49 public Statement getFirst() {
53 public Resource getSecondSubject() {
57 public Statement getSecond() {
61 public Resource getPredicate() {
62 if (pair.first != null)
63 return pair.first.getPredicate();
64 return pair.second.getPredicate();
67 public GraphChanges getChanges() {
72 public int hashCode() {
73 return pair.hashCode();
77 public boolean equals(Object obj) {
80 if (obj.getClass() != this.getClass())
82 PropertyChange c = (PropertyChange)obj;
83 return pair.equals(c.pair);
86 public void apply(WriteGraph graph) throws DatabaseException {
89 if (pair.second == null) {
90 graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
93 Resource s = leftSubject;
95 if (customValue != null)
97 else if (graph.hasValue(pair.second.getObject())) {
98 value = graph.getValue(pair.second.getObject());
100 Resource pred = pair.second.getPredicate();
103 graph.claimLiteral(s, pred, value);
111 * Sets selected state.
113 * @return true if selection state was changed
115 public boolean select(boolean select) {
120 this.selected = select;
125 * Is change selected.
128 public boolean selected() {
133 * Has change been applied
136 public boolean applied() {
144 public boolean isVisible() {
148 public void setVisible(boolean visible) {
149 this.visible = visible;
153 * Is change enabled. Disabled changes do not allow changing selected state.
156 public boolean enabled() {
160 public void setEnabled(boolean enabled) {
161 this.enabled = enabled;
165 public String toString() {
166 String s = "PropertyChange";
167 if (pair.first != null)
168 s += " L(" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
169 if (pair.second != null)
170 s += " R(" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
182 public Object getCustomValue() {
186 public void setCustomValue(Object customValue) {
187 this.customValue = customValue;