]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
Added new interface for user filters
[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 Resource getPredicate() {
60             if (pair.first != null)
61                 return pair.first.getPredicate();
62             return pair.second.getPredicate();
63         }
64         
65         public GraphChanges getChanges() {
66                 return changes;
67         }
68         
69         @Override
70         public int hashCode() {
71                 return pair.hashCode();
72         }
73         
74         @Override
75         public boolean equals(Object obj) {
76                 if (obj == null)
77                         return false;
78                 if (obj.getClass() != this.getClass())
79                         return false;
80                 PropertyChange c = (PropertyChange)obj;
81                 return pair.equals(c.pair);
82         }
83         
84         public void apply(WriteGraph graph) throws DatabaseException {
85                 if (applied)
86                         return;
87                 if (pair.second == null) {
88                         graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
89                         return;
90                 } 
91                 Resource s = leftSubject;
92                 //Resource s = changes.getComparable().getLeft(rightSubject);
93                 //Resource s = pair.first.getSubject();
94                 Resource pred = pair.second.getPredicate();
95                 if (graph.hasValue(pair.second.getObject())) {
96                         Object value = graph.getValue(pair.second.getObject());
97                         graph.claimLiteral(s, pred, value);
98                 } else {
99                         graph.deny(s,pred);
100                 }
101                 applied = true;
102                 
103         }
104         
105         public boolean select(boolean select) {
106                 if (applied)
107                         return false;
108                 this.selected = select;
109                 return true;
110         }
111         
112         
113         public boolean selected() {
114                 return selected;
115         }
116         
117         public boolean applied() {
118                 return applied;
119         }
120         
121         public boolean isVisible() {
122                 return visible;
123         }
124         
125         public void setVisible(boolean visible) {
126                 this.visible = visible;
127         }
128         
129         @Override
130         public String toString() {
131                 String s = "PropertyChange";
132                 if (pair.first != null)
133                         s += " (" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
134                 if (pair.second != null)
135                         s += " (" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
136                 return s;
137         }
138         
139 }