]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java
Support for custom property values.
[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         protected Object customValue = null;
20         
21         
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;
31         }
32         
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.");
38                 this.pair = change;
39                 this.changes = changes;
40                 this.leftSubject = left;
41                 this.rightSubject = right;
42         }
43         
44         
45         public Resource getFirstSubject() {
46                 return leftSubject;
47         }
48         
49         public Statement getFirst() {
50                 return pair.first;
51         }
52         
53         public Resource getSecondSubject() {
54                 return rightSubject;
55         }
56         
57         public Statement getSecond() {
58                 return pair.second;
59         }
60         
61         public Resource getPredicate() {
62             if (pair.first != null)
63                 return pair.first.getPredicate();
64             return pair.second.getPredicate();
65         }
66         
67         public GraphChanges getChanges() {
68                 return changes;
69         }
70         
71         @Override
72         public int hashCode() {
73                 return pair.hashCode();
74         }
75         
76         @Override
77         public boolean equals(Object obj) {
78                 if (obj == null)
79                         return false;
80                 if (obj.getClass() != this.getClass())
81                         return false;
82                 PropertyChange c = (PropertyChange)obj;
83                 return pair.equals(c.pair);
84         }
85         
86         public void apply(WriteGraph graph) throws DatabaseException {
87                 if (applied)
88                         return;
89                 if (pair.second == null) {
90                         graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject());
91                         return;
92                 } 
93                 Resource s = leftSubject;
94                 Object value = null;
95                 if (customValue != null)
96                         value = customValue;
97                 else if (graph.hasValue(pair.second.getObject())) {
98                         value = graph.getValue(pair.second.getObject());
99                 }
100                 Resource pred = pair.second.getPredicate();
101                 if (value != null) {
102                         graph.deny(s, pred);
103                         graph.claimLiteral(s, pred, value);
104                 } else {
105                         graph.deny(s,pred);
106                 }
107                 applied = true;
108         }
109         
110         /**
111          * Sets selected state. 
112          * @param select
113          * @return true if selection state was changed
114          */
115         public boolean select(boolean select) {
116             if (!enabled)
117                 return false;
118                 if (applied)
119                         return false;
120                 this.selected = select;
121                 return true;
122         }
123         
124         /**
125          * Is change selected.
126          * @return
127          */
128         public boolean selected() {
129                 return selected;
130         }
131         
132         /**
133          * Has change been applied
134          * @return
135          */
136         public boolean applied() {
137                 return applied;
138         }
139         
140         /**
141          * Is change visible
142          * @return
143          */
144         public boolean isVisible() {
145                 return visible;
146         }
147         
148         public void setVisible(boolean visible) {
149                 this.visible = visible;
150         }
151         
152         /**
153          * Is change enabled. Disabled changes do not allow changing selected state.
154          * @return
155          */
156         public boolean enabled() {
157         return enabled;
158     }
159         
160         public void setEnabled(boolean enabled) {
161         this.enabled = enabled;
162     }
163         
164         @Override
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() + ")";
171                 if (selected)
172                     s += " selected";
173                 if (enabled)
174                     s += " enabled";
175                 if (visible)
176             s += " visible";
177                 if (applied)
178                     s += " applied";
179                 return s;
180         }
181         
182         public Object getCustomValue() {
183                 return customValue;
184         }
185         
186         public void setCustomValue(Object customValue) {
187                 this.customValue = customValue;
188         }
189         
190 }