1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.Collection;
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;
13 * Base class for update operations (adding and deleting objects)
15 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
18 public abstract class UpdateOp {
20 private GraphChanges changes;
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;
28 private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
29 private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
32 public UpdateOp(GraphChanges changes) {
33 this.changes = changes;
36 public Collection<UpdateOp> getParentOps() {
40 public Collection<UpdateOp> getSubOps() {
44 public void addParentOp(UpdateOp op) {
45 assert (!op.equals(this));
49 public void addSubOp(UpdateOp op) {
50 assert (!op.equals(this));
54 public void removeParentOp(UpdateOp op) {
58 public void removeSubOp(UpdateOp op) {
62 public GraphChanges getChanges() {
66 public abstract boolean isAdd();
67 public abstract boolean isDelete();
69 public boolean isChange() {
70 return isAdd() || isDelete();
74 * Should given operation to be applied before this operation.
78 public boolean requiresOp(UpdateOp op) {
83 * Should selection state to be propagated to given op.
84 * @param op parent or sub op of this.
85 * @param select selection flag.
88 public boolean selectOp(UpdateOp op, boolean select) {
89 return requiresOp(op);
92 public boolean select(boolean select) {
97 boolean b = _select(select);
99 manualSelection = true;
103 private boolean _select(boolean select) {
104 if (select == selected)
110 manualSelection = false;
111 for (UpdateOp op : parentOps) {
112 if (selectOp(op,true))
115 for (UpdateOp op : subOps) {
116 if (selectOp(op,true))
122 manualSelection = false;
123 for (UpdateOp op : subOps) {
124 if (selectOp(op, false))
126 else if (!op.manualSelection)
129 for (UpdateOp op : parentOps)
130 if (selectOp(op, false))
136 public boolean selected() {
138 // Non change operations are not really selected, but the selected flag may be used for selection propagation
143 public boolean applied() {
147 public boolean isVisible() {
152 * Is change enabled. Disabled changes do not allow changing selected state.
155 public boolean enabled() {
159 public void setEnabled(boolean enabled) {
160 this.enabled = enabled;
164 public void apply(WriteGraph g) throws DatabaseException {
173 * Applies the changes into the database.
176 * @throws DatabaseException
178 protected abstract void _apply(WriteGraph g) throws DatabaseException;
181 * Returns resource that this operation is changing.
184 public abstract Resource getResource();
187 * Returns resource that this operation is changing.
190 public abstract Statement getStatement();
193 * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
196 public abstract Resource getCreatedResource();
199 public String toString() {
200 String s = this.getClass().getSimpleName();