]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
Added enabled flag for UpdateOp
[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         protected boolean enabled = true;
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.deny(s, pred);
98                         graph.claimLiteral(s, pred, value);
99                 } else {
100                         graph.deny(s,pred);
101                 }
102                 applied = true;
103                 
104         }
105         
106         /**
107          * Sets selected state. 
108          * @param select
109          * @return true if selection state was changed
110          */
111         public boolean select(boolean select) {
112             if (!enabled)
113                 return false;
114                 if (applied)
115                         return false;
116                 this.selected = select;
117                 return true;
118         }
119         
120         /**
121          * Is change selected.
122          * @return
123          */
124         public boolean selected() {
125                 return selected;
126         }
127         
128         /**
129          * Has change been applied
130          * @return
131          */
132         public boolean applied() {
133                 return applied;
134         }
135         
136         /**
137          * Is change visible
138          * @return
139          */
140         public boolean isVisible() {
141                 return visible;
142         }
143         
144         public void setVisible(boolean visible) {
145                 this.visible = visible;
146         }
147         
148         /**
149          * Is change enabled. Disabled changes do not allow changing selected state.
150          * @return
151          */
152         public boolean enabled() {
153         return enabled;
154     }
155         
156         public void setEnabled(boolean enabled) {
157         this.enabled = enabled;
158     }
159         
160         @Override
161         public String toString() {
162                 String s = "PropertyChange";
163                 if (pair.first != null)
164                         s += " L(" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")";
165                 if (pair.second != null)
166                         s += " R(" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")";
167                 if (selected)
168                     s += " selected";
169                 if (enabled)
170                     s += " enabled";
171                 if (visible)
172             s += " visible";
173                 if (applied)
174                     s += " applied";
175                 return s;
176         }
177         
178 }