]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java
Allow binding statement changes to update operations
[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         /**
67          * Is parent operation applied before this.
68          * @return
69          */
70         public abstract boolean requiresParentOps();
71         /**
72          * Is child operation applied before this.
73          * @return
74          */
75         public abstract boolean requiresSubOps(); 
76         
77         /**
78          * Is parent operation selected when this is selected
79          * @return
80          */
81         public boolean selectParentOps() {
82                 return requiresParentOps();
83         }
84         
85         /**
86          * Is child operation selected when this is selected
87          * @return
88          */
89         public boolean selectSubOps() {
90                 return requiresSubOps();
91         }
92         
93         public boolean select(boolean select) {
94                 boolean b = _select(select);
95                 if (b)
96                         manualSelection = true;
97                 return b;
98         }
99         
100         private boolean _select(boolean select) {
101                 if (select == selected)
102                         return true;
103                 if (select) {
104                         if (selectParentOps()) {
105                                 for (UpdateOp op : parentOps)
106                                         op._select(true);
107                         }
108                         
109                         selected = true;
110                         manualSelection = false;
111                         if (selectSubOps()) {
112                                 for (UpdateOp op : subOps)
113                                         op._select(true);
114                         }
115                         
116                 } else {
117                         selected = false;
118                         manualSelection = false;
119                         for (UpdateOp op : subOps) {
120                                 if (op.selectParentOps())
121                                         op._select(false);
122                                 else if (!op.manualSelection)
123                                         op._select(false);
124                         }
125                         for (UpdateOp op : parentOps)
126                                 if (op.selectSubOps())
127                                         op._select(false);
128                         return true;
129                 }
130                 return false;
131         }
132         public boolean selected() {
133                 return selected;
134         }
135         
136         public boolean applied() {
137                 return applied;
138         }
139         public void apply(WriteGraph g) throws DatabaseException {
140                 if (applied)
141                         return;
142                 _apply(g);
143                 applied = true;
144                 
145         }
146         
147         /**
148          * Applies the changes into the database.
149          * 
150          * @param g
151          * @throws DatabaseException
152          */
153         protected abstract void _apply(WriteGraph g) throws DatabaseException;
154         
155         /**
156          * Returns resource that this operation is changing.
157          * @return
158          */
159         public abstract Resource getResource();
160         
161         /**
162          * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
163          * @return
164          */
165         public abstract Resource getCreatedResource();
166
167 }