1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import org.simantics.db.Resource;
8 import org.simantics.db.Statement;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.interop.test.GraphChanges;
14 * Base class for update operations (adding and deleting objects)
16 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
19 public abstract class UpdateOp {
21 private GraphChanges changes;
23 private boolean selected = false;
24 private boolean manualSelection = false;
25 protected boolean applied = false;
26 protected boolean visible = true;
27 protected boolean enabled = true;
29 private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
30 private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
33 public UpdateOp(GraphChanges changes) {
34 this.changes = changes;
37 public Collection<UpdateOp> getParentOps() {
41 public <T extends UpdateOp> Collection<T> getParentOpsWithClass(Class<T> cls) {
42 List<T> ops = new ArrayList<T>(parentOps.size());
43 for (UpdateOp op : parentOps)
44 if (cls.isAssignableFrom(op.getClass()))
49 public Collection<UpdateOp> getSubOps() {
53 public <T extends UpdateOp> Collection<T> getSubOpsWithClass(Class<T> cls) {
54 List<T> ops = new ArrayList<T>(subOps.size());
55 for (UpdateOp op : subOps)
56 if (cls.isAssignableFrom(op.getClass()))
61 public void addParentOp(UpdateOp op) {
62 assert (!op.equals(this));
66 public void addSubOp(UpdateOp op) {
67 assert (!op.equals(this));
71 public void removeParentOp(UpdateOp op) {
75 public void removeSubOp(UpdateOp op) {
79 public GraphChanges getChanges() {
83 public abstract boolean isAdd();
84 public abstract boolean isDelete();
86 public boolean isChange() {
87 return isAdd() || isDelete();
91 * Should given operation to be applied before this operation.
95 public boolean requiresOp(UpdateOp op) {
100 * Should selection state to be propagated to given op.
101 * @param op parent or sub op of this.
102 * @param select selection flag.
105 public boolean selectOp(UpdateOp op, boolean select) {
106 return requiresOp(op);
109 public boolean select(boolean select) {
114 boolean b = _select(select);
116 manualSelection = true;
120 private boolean _select(boolean select) {
121 if (select == selected)
127 manualSelection = false;
128 for (UpdateOp op : parentOps) {
129 if (selectOp(op,true))
132 for (UpdateOp op : subOps) {
133 if (selectOp(op,true))
139 manualSelection = false;
140 for (UpdateOp op : subOps) {
141 if (selectOp(op, false))
143 else if (!op.manualSelection)
146 for (UpdateOp op : parentOps)
147 if (selectOp(op, false))
153 public boolean selected() {
155 // Non change operations are not really selected, but the selected flag may be used for selection propagation
160 public boolean applied() {
164 public boolean isVisible() {
169 * Is change enabled. Disabled changes do not allow changing selected state.
172 public boolean enabled() {
176 public void setEnabled(boolean enabled) {
177 this.enabled = enabled;
181 public void apply(WriteGraph g) throws DatabaseException {
190 * Applies the changes into the database.
193 * @throws DatabaseException
195 protected abstract void _apply(WriteGraph g) throws DatabaseException;
198 * Returns resource that this operation is changing.
201 public abstract Resource getResource();
204 * Returns resource that this operation is changing.
207 public abstract Statement getStatement();
210 * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
213 public abstract Resource getCreatedResource();
216 public String toString() {
217 String s = this.getClass().getSimpleName();