]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
75d8456140d77f82709f61381fea218e4e086af2
[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.utils.datastructures.Pair;
9
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         
19         
20         public PropertyChange(GraphChanges changes, Resource left, Statement first, Resource right, Statement second) {
21                 if (first == null && second == null)
22                         throw new IllegalArgumentException("At least one of the stamenents must be non null.");
23                 if (left == null || right == null)
24                         throw new IllegalArgumentException("Subject resources cannot be null.");
25                 this.pair = new Pair<Statement, Statement>(first, second);
26                 this.changes = changes;
27                 this.leftSubject = left;
28                 this.rightSubject = right;
29         }
30         
31         public PropertyChange(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
32                 if (change == null || (change.first == null && change.second == null))
33                         throw new IllegalArgumentException("At least one of the stamenents must be non null.");
34                 if (left == null || right == null)
35                         throw new IllegalArgumentException("Subject resources cannot be null.");
36                 this.pair = change;
37                 this.changes = changes;
38                 this.leftSubject = left;
39                 this.rightSubject = right;
40         }
41         
42         
43         public Resource getFirstSubject() {
44                 return leftSubject;
45         }
46         
47         public Statement getFirst() {
48                 return pair.first;
49         }
50         
51         public Resource getSecondSubject() {
52                 return rightSubject;
53         }
54         
55         public Statement getSecond() {
56                 return pair.second;
57         }
58         
59         public GraphChanges getChanges() {
60                 return changes;
61         }
62         
63         @Override
64         public int hashCode() {
65                 return pair.hashCode();
66         }
67         
68         @Override
69         public boolean equals(Object obj) {
70                 if (obj == null)
71                         return false;
72                 if (obj.getClass() != this.getClass())
73                         return false;
74                 PropertyChange c = (PropertyChange)obj;
75                 return pair.equals(c.pair);
76         }
77         
78         public void apply(WriteGraph graph) throws DatabaseException {
79                 if (applied)
80                         return;
81                 if (pair.second == null) {
82                         graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
83                         return;
84                 } 
85                 Resource s = leftSubject;
86                 //Resource s = changes.getComparable().getLeft(rightSubject);
87                 //Resource s = pair.first.getSubject();
88                 Resource pred = pair.second.getPredicate();
89                 if (graph.hasValue(pair.second.getObject())) {
90                         Object value = graph.getValue(pair.second.getObject());
91                         graph.claimLiteral(s, pred, value);
92                 } else {
93                         graph.deny(s,pred);
94                 }
95                 applied = true;
96                 
97         }
98         
99         public boolean select(boolean select) {
100                 if (applied)
101                         return false;
102                 this.selected = select;
103                 return true;
104         }
105         
106         
107         public boolean selected() {
108                 return selected;
109         }
110         
111         public boolean applied() {
112                 return applied;
113         }
114         
115         public boolean isVisible() {
116                 return visible;
117         }
118         
119         public void setVisible(boolean visible) {
120                 this.visible = visible;
121         }
122         
123         @Override
124         public String toString() {
125                 String s = "PropertyChange";
126                 if (pair.first != null)
127                         s += " (" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
128                 if (pair.second != null)
129                         s += " (" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
130                 return s;
131         }
132         
133 }