]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java
72e17856a4a28589a02b09e21795097ea1da93b5
[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 (select) {
108                         if (selectParentOps()) {
109                                 for (UpdateOp op : parentOps)
110                                         op._select(true);
111                         }
112                         
113                         selected = true;
114                         manualSelection = false;
115                         if (selectSubOps()) {
116                                 for (UpdateOp op : subOps)
117                                         op._select(true);
118                         }
119                         
120                 } else {
121                         selected = false;
122                         manualSelection = false;
123                         for (UpdateOp op : subOps) {
124                                 if (op.selectParentOps())
125                                         op._select(false);
126                                 else if (!op.manualSelection)
127                                         op._select(false);
128                         }
129                         for (UpdateOp op : parentOps)
130                                 if (op.selectSubOps())
131                                         op._select(false);
132                         return true;
133                 }
134                 return false;
135         }
136         public boolean selected() {
137                 return selected;
138         }
139         
140         public boolean applied() {
141                 return applied;
142         }
143         public void apply(WriteGraph g) throws DatabaseException {
144                 if (applied)
145                         return;
146                 _apply(g);
147                 applied = true;
148                 
149         }
150         
151         /**
152          * Applies the changes into the database.
153          * 
154          * @param g
155          * @throws DatabaseException
156          */
157         protected abstract void _apply(WriteGraph g) throws DatabaseException;
158         
159         /**
160          * Returns resource that this operation is changing.
161          * @return
162          */
163         public abstract Resource getResource();
164         
165         /**
166          * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
167          * @return
168          */
169         public abstract Resource getCreatedResource();
170
171 }