X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Fmodel%2FUpdateOp.java;h=2bd3718f6a39cc96d5741611e8cb85f3b5f59771;hb=961c51d828eace5c9ab9db9499f0af9a78f16192;hp=907741c23300b71a60b2504f26676363c05b61df;hpb=2665fb66a324e6d08f28215a4734b95b001927f0;p=simantics%2Finterop.git diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java index 907741c..2bd3718 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java @@ -2,11 +2,15 @@ package org.simantics.interop.update.model; import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.interop.test.GraphChanges; +import org.simantics.layer0.Layer0; /** * Base class for update operations (adding and deleting objects) @@ -21,6 +25,8 @@ public abstract class UpdateOp { private boolean selected = false; private boolean manualSelection = false; protected boolean applied = false; + protected boolean visible = true; + protected boolean enabled = true; private Collection parentOps = new ArrayList(); private Collection subOps = new ArrayList(); @@ -34,17 +40,37 @@ public abstract class UpdateOp { return parentOps; } + public Collection getParentOpsWithClass(Class cls) { + List ops = new ArrayList(parentOps.size()); + for (UpdateOp op : parentOps) + if (cls.isAssignableFrom(op.getClass())) + ops.add((T)op); + return ops; + } + public Collection getSubOps() { return subOps; } + public Collection getSubOpsWithClass(Class cls) { + List ops = new ArrayList(subOps.size()); + for (UpdateOp op : subOps) + if (cls.isAssignableFrom(op.getClass())) + ops.add((T)op); + return ops; + } + public void addParentOp(UpdateOp op) { assert (!op.equals(this)); + if (parentOps.contains(op)) + return; parentOps.add(op); } public void addSubOp(UpdateOp op) { assert (!op.equals(this)); + if (subOps.contains(op)) + return; subOps.add(op); } @@ -63,55 +89,101 @@ public abstract class UpdateOp { public abstract boolean isAdd(); public abstract boolean isDelete(); - public abstract boolean requiresParentOps(); - public abstract boolean requiresSubOps(); + public boolean isChange() { + return isAdd() || isDelete(); + } + + /** + * Should given operation to be applied before this operation. + * @param op + * @return + */ + public boolean requiresOp(UpdateOp op) { + return false; + } + + /** + * Should selection state to be propagated to given op. + * @param op parent or sub op of this. + * @param select selection flag. + * @return + */ + public boolean selectOp(UpdateOp op, boolean select) { + return requiresOp(op); + } public boolean select(boolean select) { + if (!enabled) + return false; + if (!isChange()) + return false; boolean b = _select(select); if (b) manualSelection = true; return b; } - + private boolean _select(boolean select) { if (select == selected) return true; + if (applied) + return false; if (select) { - if (requiresParentOps()) { - for (UpdateOp op : parentOps) - op._select(true); - } - - selected = true; - manualSelection = false; - if (requiresSubOps()) { - for (UpdateOp op : subOps) - op._select(true); - } - + selected = true; + manualSelection = false; + for (UpdateOp op : parentOps) { + if (selectOp(op,true)) + op._select(true); + } + for (UpdateOp op : subOps) { + if (selectOp(op,true)) + op._select(true); + } + return true; } else { selected = false; manualSelection = false; for (UpdateOp op : subOps) { - if (op.requiresParentOps()) - op._select(false); - else if (!op.manualSelection) - op._select(false); - } + if (selectOp(op, false)) + op._select(false); + else if (!op.manualSelection) + op._select(false); + } for (UpdateOp op : parentOps) - if (op.requiresSubOps()) - op._select(false); + if (selectOp(op, false)) + op._select(false); return true; } - return false; } + public boolean selected() { + if (!isChange()) + // Non change operations are not really selected, but the selected flag may be used for selection propagation + return false; return selected; } public boolean applied() { return applied; } + + public boolean isVisible() { + return visible; + } + + /** + * Is change enabled. Disabled changes do not allow changing selected state. + * @return + */ + public boolean enabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public void apply(WriteGraph g) throws DatabaseException { if (applied) return; @@ -120,6 +192,12 @@ public abstract class UpdateOp { } + /** + * Applies the changes into the database. + * + * @param g + * @throws DatabaseException + */ protected abstract void _apply(WriteGraph g) throws DatabaseException; /** @@ -128,10 +206,36 @@ public abstract class UpdateOp { */ public abstract Resource getResource(); + /** + * Returns resource that this operation is changing. + * @return + */ + public abstract Statement getStatement(); + /** * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null. * @return */ public abstract Resource getCreatedResource(); + + public Resource getParentResource(ReadGraph g) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(g); + return g.getPossibleObject(getResource(), l0.PartOf); + } + + + @Override + public String toString() { + String s = this.getClass().getSimpleName(); + if (selected) + s += " selected"; + if (enabled) + s += " enabled"; + if (visible) + s += " visible"; + if (applied) + s += " applied"; + return s; + } }