1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.Statement;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.interop.test.GraphChanges;
13 import org.simantics.layer0.Layer0;
16 * Base class for update operations (adding and deleting objects)
18 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
21 public abstract class UpdateOp {
23 private GraphChanges changes;
25 private boolean selected = false;
26 private boolean manualSelection = false;
27 protected boolean applied = false;
28 protected boolean visible = true;
29 protected boolean enabled = true;
31 private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
32 private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
35 public UpdateOp(GraphChanges changes) {
36 this.changes = changes;
39 public Collection<UpdateOp> getParentOps() {
43 public <T extends UpdateOp> Collection<T> getParentOpsWithClass(Class<T> cls) {
44 List<T> ops = new ArrayList<T>(parentOps.size());
45 for (UpdateOp op : parentOps)
46 if (cls.isAssignableFrom(op.getClass()))
51 public Collection<UpdateOp> getSubOps() {
55 public <T extends UpdateOp> Collection<T> getSubOpsWithClass(Class<T> cls) {
56 List<T> ops = new ArrayList<T>(subOps.size());
57 for (UpdateOp op : subOps)
58 if (cls.isAssignableFrom(op.getClass()))
63 public void addParentOp(UpdateOp op) {
64 assert (!op.equals(this));
65 if (parentOps.contains(op))
70 public void addSubOp(UpdateOp op) {
71 assert (!op.equals(this));
72 if (subOps.contains(op))
77 public void removeParentOp(UpdateOp op) {
81 public void removeSubOp(UpdateOp op) {
85 public GraphChanges getChanges() {
89 public abstract boolean isAdd();
90 public abstract boolean isDelete();
92 public boolean isChange() {
93 return isAdd() || isDelete();
97 * Should given operation to be applied before this operation.
101 public boolean requiresOp(UpdateOp op) {
106 * Should selection state to be propagated to given op.
107 * @param op parent or sub op of this.
108 * @param select selection flag.
111 public boolean selectOp(UpdateOp op, boolean select) {
112 return requiresOp(op);
115 public boolean select(boolean select) {
120 boolean b = _select(select);
122 manualSelection = true;
126 private boolean _select(boolean select) {
127 if (select == selected)
133 manualSelection = false;
134 for (UpdateOp op : parentOps) {
135 if (selectOp(op,true))
138 for (UpdateOp op : subOps) {
139 if (selectOp(op,true))
145 manualSelection = false;
146 for (UpdateOp op : subOps) {
147 if (selectOp(op, false))
149 else if (!op.manualSelection)
152 for (UpdateOp op : parentOps)
153 if (selectOp(op, false))
159 public boolean selected() {
161 // Non change operations are not really selected, but the selected flag may be used for selection propagation
166 public boolean applied() {
170 public boolean isVisible() {
175 * Is change enabled. Disabled changes do not allow changing selected state.
178 public boolean enabled() {
182 public void setEnabled(boolean enabled) {
183 this.enabled = enabled;
187 public void apply(WriteGraph g) throws DatabaseException {
196 * Applies the changes into the database.
199 * @throws DatabaseException
201 protected abstract void _apply(WriteGraph g) throws DatabaseException;
204 * Returns resource that this operation is changing.
207 public abstract Resource getResource();
210 * Returns resource that this operation is changing.
213 public abstract Statement getStatement();
216 * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
219 public abstract Resource getCreatedResource();
221 public Resource getParentResource(ReadGraph g) throws DatabaseException {
222 Layer0 l0 = Layer0.getInstance(g);
223 return g.getPossibleObject(getResource(), l0.PartOf);
228 public String toString() {
229 String s = this.getClass().getSimpleName();