X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Fmodel%2FPropertyChange.java;h=de28055996ef41f8c1ed7368fd5d6fffb6ccc651;hb=c2a5b7657db18e6de715f9559ddc4335b953f203;hp=25d43592f7491662b349674d0bc80d5d61ce69a8;hpb=2e5e6f18c3d1182386e69f86b78e5a4179dde18a;p=simantics%2Finterop.git diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java index 25d4359..de28055 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java @@ -5,34 +5,66 @@ 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; import org.simantics.utils.datastructures.Pair; public class PropertyChange { protected GraphChanges changes; + protected Resource leftSubject; + protected Resource rightSubject; protected Pair pair; protected boolean applied = false; protected boolean selected = false; protected boolean visible = true; + protected boolean enabled = true; + protected Object customValue = null; - public PropertyChange(GraphChanges changes, Statement first, Statement second) { + public PropertyChange(GraphChanges changes, Resource left, Statement first, Resource right, Statement second) { + if (first == null && second == null) + throw new IllegalArgumentException("At least one of the stamenents must be non null."); + if (left == null || right == null) + throw new IllegalArgumentException("Subject resources cannot be null."); this.pair = new Pair(first, second); this.changes = changes; + this.leftSubject = left; + this.rightSubject = right; } - public PropertyChange(GraphChanges changes, Pair change) { + public PropertyChange(GraphChanges changes, Resource left, Resource right, Pair change) { + if (change == null || (change.first == null && change.second == null)) + throw new IllegalArgumentException("At least one of the stamenents must be non null."); + if (left == null || right == null) + throw new IllegalArgumentException("Subject resources cannot be null."); this.pair = change; this.changes = changes; + this.leftSubject = left; + this.rightSubject = right; + } + + + public Resource getFirstSubject() { + return leftSubject; } public Statement getFirst() { return pair.first; } + public Resource getSecondSubject() { + return rightSubject; + } + public Statement getSecond() { return pair.second; } + public Resource getPredicate() { + if (pair.first != null) + return pair.first.getPredicate(); + return pair.second.getPredicate(); + } + public GraphChanges getChanges() { return changes; } @@ -49,45 +81,88 @@ public class PropertyChange { if (obj.getClass() != this.getClass()) return false; PropertyChange c = (PropertyChange)obj; - return pair.equals(c.pair); + if (!leftSubject.equals(c.leftSubject)) + return false; + if (!rightSubject.equals(c.rightSubject)) + return false; + if (pair.first != null && pair.first.equals(c.pair.first)) + return true; + if (pair.second != null && pair.second.equals(c.pair.second)) + return true; + return false; } public void apply(WriteGraph graph) throws DatabaseException { if (applied) return; if (pair.second == null) { - graph.deny(pair.first); + graph.deny(leftSubject, pair.first.getPredicate(),pair.first.getObject()); return; } - Resource s = changes.getComparable().getLeft(pair.second.getSubject()); - //Resource s = pair.first.getSubject(); - Resource pred = pair.second.getPredicate(); - if (graph.hasValue(pair.second.getObject())) { - Object value = graph.getValue(pair.second.getObject()); - graph.claimLiteral(s, pred, value); - } else { - graph.deny(s,pred); + Layer0 L0 = Layer0.getInstance(graph); + Resource s = leftSubject; + if (graph.isInstanceOf(pair.second.getObject(), L0.Literal)) { + Object value = null; + if (customValue != null) + value = customValue; + else if (graph.hasValue(pair.second.getObject())) { + value = graph.getValue(pair.second.getObject()); + } + Resource pred = pair.second.getPredicate(); + if (getChanges().getComparable().containsRight(pred)) + pred = getChanges().getComparable().getLeft(pred); + + if (value != null) { + graph.deny(s, pred); + graph.claimLiteral(s, pred, value); + } else { + graph.deny(s,pred); + } + } else if (graph.isInstanceOf(pair.second.getObject(), L0.SCLValue)) { + Resource pred = pair.second.getPredicate(); + graph.deny(s, pred); + Resource valueResource = graph.newResource(); + graph.claim(valueResource, L0.InstanceOf, graph.getSingleObject(pair.second.getObject(), L0.InstanceOf)); + AddDeleteUpdateOp.copyProperties(graph, pair.second.getObject(), valueResource); + graph.claim(s, pred, valueResource); } applied = true; - } + /** + * Sets selected state. + * @param select + * @return true if selection state was changed + */ public boolean select(boolean select) { + if (!enabled) + return false; if (applied) return false; this.selected = select; return true; } - + /** + * Is change selected. + * @return + */ public boolean selected() { return selected; } + /** + * Has change been applied + * @return + */ public boolean applied() { return applied; } + /** + * Is change visible + * @return + */ public boolean isVisible() { return visible; } @@ -96,4 +171,42 @@ public class PropertyChange { this.visible = 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; + } + + @Override + public String toString() { + String s = "PropertyChange"; + if (pair.first != null) + s += " L(" + (leftSubject) + " , " + pair.first.getPredicate() + " , " + pair.first.getObject() + ")"; + if (pair.second != null) + s += " R(" + (rightSubject) + " , " + pair.second.getPredicate() + " , " + pair.second.getObject() + ")"; + if (selected) + s += " selected"; + if (enabled) + s += " enabled"; + if (visible) + s += " visible"; + if (applied) + s += " applied"; + return s; + } + + public Object getCustomValue() { + return customValue; + } + + public void setCustomValue(Object customValue) { + this.customValue = customValue; + } + }