]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java
PropertyChange class (instead of Pair)
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateOp.java
1 package  org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.interop.test.GraphChanges;
10
11 /**
12  * Base class for update operations (adding and deleting objects)  
13  * 
14  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
15  *
16  */
17 public abstract class UpdateOp {
18         
19         private GraphChanges changes;
20         
21         private boolean selected = false;
22         private boolean manualSelection = false;
23         protected boolean applied = false;
24         
25         private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
26         private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
27         
28         
29         public UpdateOp(GraphChanges changes) {
30                 this.changes = changes;
31         }
32         
33         public Collection<UpdateOp> getParentOps() {
34                 return parentOps;
35         }
36         
37         public Collection<UpdateOp> getSubOps() {
38                 return subOps;
39         }
40         
41         public void addParentOp(UpdateOp op) {
42                 assert (!op.equals(this));
43                 parentOps.add(op);
44         }
45         
46         public void addSubOp(UpdateOp op) {
47                 assert (!op.equals(this));
48                 subOps.add(op);
49         }
50         
51         public void removeParentOp(UpdateOp op) {
52                 parentOps.remove(op);
53         }
54         
55         public void removeSubOp(UpdateOp op) {
56                 subOps.remove(op);
57         }
58         
59         public GraphChanges getChanges() {
60                 return changes;
61         }
62         
63         public abstract boolean isAdd();
64         public abstract boolean isDelete();
65         
66         public boolean isChange() {
67                 return isAdd() || isDelete();
68         }
69         
70         /**
71          * Is parent operation applied before this.
72          * @return
73          */
74         public abstract boolean requiresParentOps();
75         /**
76          * Is child operation applied before this.
77          * @return
78          */
79         public abstract boolean requiresSubOps(); 
80         
81         /**
82          * Is parent operation selected when this is selected
83          * @return
84          */
85         public boolean selectParentOps() {
86                 return requiresParentOps();
87         }
88         
89         /**
90          * Is child operation selected when this is selected
91          * @return
92          */
93         public boolean selectSubOps() {
94                 return requiresSubOps();
95         }
96         
97         public boolean select(boolean select) {
98                 boolean b = _select(select);
99                 if (b)
100                         manualSelection = true;
101                 return b;
102         }
103         
104         private boolean _select(boolean select) {
105                 if (select == selected)
106                         return true;
107                 if (applied)
108                         return false;
109                 if (select) {
110                         if (selectParentOps()) {
111                                 for (UpdateOp op : parentOps)
112                                         op._select(true);
113                         }
114                         
115                         selected = true;
116                         manualSelection = false;
117                         if (selectSubOps()) {
118                                 for (UpdateOp op : subOps)
119                                         op._select(true);
120                         }
121                         
122                 } else {
123                         selected = false;
124                         manualSelection = false;
125                         for (UpdateOp op : subOps) {
126                                 if (op.selectParentOps())
127                                         op._select(false);
128                                 else if (!op.manualSelection)
129                                         op._select(false);
130                         }
131                         for (UpdateOp op : parentOps)
132                                 if (op.selectSubOps())
133                                         op._select(false);
134                         return true;
135                 }
136                 return false;
137         }
138         public boolean selected() {
139                 return selected;
140         }
141         
142         public boolean applied() {
143                 return applied;
144         }
145         public void apply(WriteGraph g) throws DatabaseException {
146                 if (applied)
147                         return;
148                 _apply(g);
149                 applied = true;
150                 
151         }
152         
153         /**
154          * Applies the changes into the database.
155          * 
156          * @param g
157          * @throws DatabaseException
158          */
159         protected abstract void _apply(WriteGraph g) throws DatabaseException;
160         
161         /**
162          * Returns resource that this operation is changing.
163          * @return
164          */
165         public abstract Resource getResource();
166         
167         /**
168          * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
169          * @return
170          */
171         public abstract Resource getCreatedResource();
172
173 }