]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java
50954ec1ffca968cc3e06a6dfbd1b09a59e4765e
[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          * Should given operation to be applied before this operation.
75          * @param op
76          * @return
77          */
78         public boolean requiresOp(UpdateOp op) {
79             return false;
80         }
81         
82         /**
83          * Should selection state to be propagated to given op.
84          * @param op parent or sub op of this.
85          * @param select selection flag.
86          * @return
87          */
88         public boolean selectOp(UpdateOp op, boolean select) {
89             return requiresOp(op);
90     }
91         
92         public boolean select(boolean select) {
93             if (!enabled)
94                 return false;
95             if (!isChange())
96                 return false;
97                 boolean b = _select(select);
98                 if (b)
99                         manualSelection = true;
100                 return b;
101         }
102
103         private boolean _select(boolean select) {
104                 if (select == selected)
105                         return true;
106                 if (applied)
107                         return false;
108                 if (select) {
109                     selected = true;
110             manualSelection = false;
111                     for (UpdateOp op : parentOps) {
112                         if (selectOp(op,true))
113                    op._select(true);
114             }
115                         for (UpdateOp op : subOps) {
116                             if (selectOp(op,true))
117                    op._select(true);
118             }
119                         return true;
120                 } else {
121                         selected = false;
122                         manualSelection = false;
123                         for (UpdateOp op : subOps) {
124                             if (selectOp(op, false))
125                     op._select(false);
126                 else if (!op.manualSelection)
127                     op._select(false);
128             }
129                         for (UpdateOp op : parentOps)
130                            if (selectOp(op, false))
131                   op._select(false);
132                         return true;
133                 }
134         }
135         
136         public boolean selected() {
137             if (!isChange())
138                 // Non change operations are not really selected, but the selected flag may be used for selection propagation
139                 return false;
140                 return selected;
141         }
142         
143         public boolean applied() {
144                 return applied;
145         }
146         
147         public boolean isVisible() {
148                 return visible;
149         }
150         
151         /**
152      * Is change enabled. Disabled changes do not allow changing selected state.
153      * @return
154      */
155     public boolean enabled() {
156         return enabled;
157     }
158     
159     public void setEnabled(boolean enabled) {
160         this.enabled = enabled;
161     }
162         
163         
164         public void apply(WriteGraph g) throws DatabaseException {
165                 if (applied)
166                         return;
167                 _apply(g);
168                 applied = true;
169                 
170         }
171         
172         /**
173          * Applies the changes into the database.
174          * 
175          * @param g
176          * @throws DatabaseException
177          */
178         protected abstract void _apply(WriteGraph g) throws DatabaseException;
179         
180         /**
181          * Returns resource that this operation is changing.
182          * @return
183          */
184         public abstract Resource getResource();
185         
186         /**
187          * Returns resource that this operation is changing.
188          * @return
189          */
190         public abstract Statement getStatement();
191         
192         /**
193          * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
194          * @return
195          */
196         public abstract Resource getCreatedResource();
197         
198         @Override
199     public String toString() {
200         String s = this.getClass().getSimpleName();
201         if (selected)
202             s += " selected";
203         if (enabled)
204             s += " enabled";
205         if (visible)
206             s += " visible";
207         if (applied)
208             s += " applied";
209         return s;
210     }
211
212 }