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