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