]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java
Base classes for model updates
[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 abstract boolean requiresParentOps();
67         public abstract boolean requiresSubOps(); 
68         
69         public boolean select(boolean select) {
70                 boolean b = _select(select);
71                 if (b)
72                         manualSelection = true;
73                 return b;
74         }
75         
76         private boolean _select(boolean select) {
77                 if (select == selected)
78                         return true;
79                 if (select) {
80                         if (requiresParentOps()) {
81                                 for (UpdateOp op : parentOps)
82                                         op._select(true);
83                         }
84                         
85                         selected = true;
86                         manualSelection = false;
87                         if (requiresSubOps()) {
88                                 for (UpdateOp op : subOps)
89                                         op._select(true);
90                         }
91                         
92                 } else {
93                         selected = false;
94                         manualSelection = false;
95                         for (UpdateOp op : subOps) {
96                                 if (op.requiresParentOps())
97                                         op._select(false);
98                                 else if (!op.manualSelection)
99                                         op._select(false);
100                         }
101                         for (UpdateOp op : parentOps)
102                                 if (op.requiresSubOps())
103                                         op._select(false);
104                         return true;
105                 }
106                 return false;
107         }
108         public boolean selected() {
109                 return selected;
110         }
111         
112         public boolean applied() {
113                 return applied;
114         }
115         public void apply(WriteGraph g) throws DatabaseException {
116                 if (applied)
117                         return;
118                 _apply(g);
119                 applied = true;
120                 
121         }
122         
123         protected abstract void _apply(WriteGraph g) throws DatabaseException;
124         
125         /**
126          * Returns resource that this operation is changing.
127          * @return
128          */
129         public abstract Resource getResource();
130         
131         /**
132          * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
133          * @return
134          */
135         public abstract Resource getCreatedResource();
136
137 }