From b0205b09bbd81755eb69452eae0142d5a93f95a4 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Tue, 24 Oct 2017 17:26:17 +0300 Subject: [PATCH] PropertyChange class (instead of Pair) --- .../scl/Interop/Update.scl | 48 ++++++++-- .../update/editor/ModelUpdateEditor.java | 18 ++-- .../interop/update/model/ModelUpdate.java | 44 +++------ .../interop/update/model/PropertyChange.java | 91 +++++++++++++++++++ .../interop/update/model/UpdateList.java | 79 ++++++++-------- .../interop/update/model/UpdateOp.java | 2 + 6 files changed, 198 insertions(+), 84 deletions(-) create mode 100644 org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java diff --git a/org.simantics.interop.update/scl/Interop/Update.scl b/org.simantics.interop.update/scl/Interop/Update.scl index 8397844..d405033 100644 --- a/org.simantics.interop.update/scl/Interop/Update.scl +++ b/org.simantics.interop.update/scl/Interop/Update.scl @@ -54,19 +54,25 @@ importJava "org.simantics.interop.update.model.UpdateList" where data UpdateList @JavaName getChanges - getListChanges :: UpdateList -> [Pair] + getListChanges :: UpdateList -> [PropertyChange] @JavaName getSelected - getListSelected :: UpdateList -> MSet.T Pair + getListSelected :: UpdateList -> [PropertyChange] - @JavaName addSelected - addListSelected :: UpdateList -> Pair -> () + @JavaName getChanges + getResChanges :: UpdateList -> Resource -> [PropertyChange] + + @JavaName getChange + getStmChange :: UpdateList -> Statement -> Maybe PropertyChange - @JavaName removeSelected - removeListSelected :: UpdateList -> Pair -> () + //@JavaName addSelected + //addListSelected :: UpdateList -> Pair -> () - @JavaName isSelected - isListSelected :: UpdateList -> Pair -> Boolean + //@JavaName removeSelected + //removeListSelected :: UpdateList -> Pair -> () + + //@JavaName isSelected + //isListSelected :: UpdateList -> Pair -> Boolean importJava "org.simantics.interop.test.GraphChanges" where data GraphChanges @@ -82,7 +88,29 @@ importJava "org.simantics.interop.test.GraphChanges" where @JavaName getModifications getModifications :: GraphChanges -> [Pair] + +importJava "org.simantics.interop.update.model.PropertyChange" where + data PropertyChange + + @JavaName "" + createPropertyChange :: GraphChanges -> Maybe Statement -> Maybe Statement -> PropertyChange + + @JavaName applied + pcApplied :: PropertyChange -> Boolean + @JavaName selected + pcSelected :: PropertyChange -> Boolean + + @JavaName "select" + pcSelect :: PropertyChange -> Boolean -> Boolean + + @JavaName getFirst + getFirst :: PropertyChange -> Maybe Statement + + @JavaName getSecond + getSecond :: PropertyChange -> Maybe Statement + + importJava "org.simantics.interop.update.model.UpdateNode" where data UpdateNode @@ -160,10 +188,10 @@ importJava "org.simantics.utils.datastructures.Pair" where createPair :: Maybe a -> Maybe b -> Pair @JavaName first - getFirst :: Pair -> Maybe a + getPairFirst :: Pair -> Maybe a @JavaName second - getSecond :: Pair -> Maybe b + getPairSecond :: Pair -> Maybe b importJava "org.simantics.utils.datastructures.BijectionMap" where data BijectionMap diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java b/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java index 5a6646e..1779b8f 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java @@ -51,6 +51,7 @@ import org.simantics.db.request.Read; import org.simantics.interop.test.GraphChanges; import org.simantics.interop.update.Activator; import org.simantics.interop.update.model.ModelUpdate; +import org.simantics.interop.update.model.PropertyChange; import org.simantics.interop.update.model.ModelUpdate.WarningListener; import org.simantics.interop.update.model.UpdateList; import org.simantics.interop.update.model.UpdateNode; @@ -216,8 +217,8 @@ public abstract class ModelUpdateEditor extends Composite implements WarningList if (update.getUpdateList().getSelected().size() > 0) { update.getUpdateList().clearSelected(); } else { - for (Pair nr : update.getUpdateList().getChanges()) - update.getUpdateList().addSelected(nr); + for (PropertyChange nr : update.getUpdateList().getChanges()) + nr.select(true); } changeViewer.refresh(); } @@ -539,9 +540,8 @@ public abstract class ModelUpdateEditor extends Composite implements WarningList public Image getImage(Object element) { if (update == null || !update.isInit()) return null; -// if (update.getUpdateList() == null) -// return null; - if (update.getUpdateList().isSelected((Pair) element)) + PropertyChange pc = (PropertyChange)element; + if (pc.selected()) return checked; else return unchecked; @@ -629,7 +629,8 @@ public abstract class ModelUpdateEditor extends Composite implements WarningList protected Object getValue(Object element) { if (update == null || !update.isInit()) return false; - return update.getUpdateList().isSelected((Pair) element); + PropertyChange pc = (PropertyChange)element; + return pc.selected(); } @SuppressWarnings("unchecked") @@ -637,10 +638,11 @@ public abstract class ModelUpdateEditor extends Composite implements WarningList protected void setValue(Object element, Object value) { if (update == null || !update.isInit()) return; + PropertyChange pc = (PropertyChange)element; if (Boolean.TRUE.equals(value)) - update.getUpdateList().addSelected((Pair) element); + pc.select(true); else - update.getUpdateList().removeSelected((Pair) element); + pc.select(false); getViewer().refresh(element); } diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java index bf391d4..46bf79e 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java @@ -1,7 +1,6 @@ package org.simantics.interop.update.model; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map.Entry; @@ -136,7 +135,7 @@ public abstract class ModelUpdate { protected abstract Pair getChanges(Resource r1, Resource r2) throws DatabaseException; protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException; protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException { - return new UpdateList(changes.getModifications()); + return new UpdateList(changes, changes.getModifications()); } protected void addFilters(List filters) { @@ -198,10 +197,9 @@ public abstract class ModelUpdate { public void applyAll(WriteGraph graph) throws DatabaseException { Layer0Utils.addCommentMetadata(graph, "Apply all model updates"); graph.markUndoPoint(); - for (Pair mod : updateList.getChanges()) { - applyLiteralChange(graph, mod); + for (PropertyChange mod : updateList.getChanges()) { + mod.apply(graph); } - updateList.clear(); updateTree.getUpdateOps().applyAll(graph); } @@ -209,29 +207,15 @@ public abstract class ModelUpdate { public void applySelected(WriteGraph graph) throws DatabaseException { Layer0Utils.addCommentMetadata(graph, "Apply selected model updates"); graph.markUndoPoint(); - HashSet> changes = new HashSet<>(updateList.getSelected()); - for (Pair mod : changes) { - updateList.removeChange(mod); - applyLiteralChange(graph, mod); + for (PropertyChange mod : updateList.getChanges()) { + if (mod.selected()) + mod.apply(graph); } updateTree.getUpdateOps().applySelected(graph); } - protected void applyLiteralChange(WriteGraph graph, Pair mod) throws DatabaseException { - if (mod.second == null) { - graph.deny(mod.first); - return; - } - Resource s = changes.getComparable().getLeft(mod.second.getSubject()); - Resource pred = mod.second.getPredicate(); - if (graph.hasValue(mod.second.getObject())) { - Object value = graph.getValue(mod.second.getObject()); - graph.claimLiteral(s, pred, value); - } else { - graph.deny(s,pred); - } - } + protected Session getSession() { @@ -341,8 +325,8 @@ public abstract class ModelUpdate { } - for (Pair pair : updateList.getChanges()) { - updateList.addSelected(pair); + for (PropertyChange pair : updateList.getChanges()) { + pair.select(true); } // preserve user-made changes (by removing selections) @@ -358,17 +342,17 @@ public abstract class ModelUpdate { } } - for (Pair pair : updateList.getChanges()) { - if (pair.first != null) { + for (PropertyChange pair : updateList.getChanges()) { + if (pair.getFirst() != null) { boolean found = false; - for (Pair pair2 : updateList2.getChanges()) { - if (pair.first.equals(pair2.second)) { + for (PropertyChange pair2 : updateList2.getChanges()) { + if (pair.getFirst().equals(pair2.getSecond())) { found = true; break; } } if (found) { - updateList.removeSelected(pair); + pair.select(false); } } } 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 new file mode 100644 index 0000000..9867a39 --- /dev/null +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java @@ -0,0 +1,91 @@ +package org.simantics.interop.update.model; + +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.utils.datastructures.Pair; + +public class PropertyChange { + protected GraphChanges changes; + protected Pair pair; + protected boolean applied = false; + protected boolean selected = false; + + + public PropertyChange(GraphChanges changes, Statement first, Statement second) { + this.pair = new Pair(first, second); + this.changes = changes; + } + + public PropertyChange(GraphChanges changes, Pair change) { + this.pair = change; + this.changes = changes; + } + + public Statement getFirst() { + return pair.first; + } + + public Statement getSecond() { + return pair.second; + } + + public GraphChanges getChanges() { + return changes; + } + + @Override + public int hashCode() { + return pair.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + if (obj.getClass() != this.getClass()) + return false; + PropertyChange c = (PropertyChange)obj; + return pair.equals(c.pair); + } + + public void apply(WriteGraph graph) throws DatabaseException { + if (applied) + return; + if (pair.second == null) { + graph.deny(pair.first); + 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); + } + applied = true; + + } + + public boolean select(boolean select) { + if (applied) + return false; + this.selected = select; + return true; + } + + + public boolean selected() { + return selected; + } + + public boolean applied() { + return applied; + } + + +} diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java index 747cd78..6f66ba3 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java @@ -9,79 +9,86 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; import org.simantics.db.exception.DatabaseException; +import org.simantics.interop.test.GraphChanges; import org.simantics.utils.datastructures.Pair; public class UpdateList { - private HashSet> changes; - private HashSet> selected; + private HashSet changes; public UpdateList() { changes = new HashSet<>(); - selected = new HashSet<>(); } - public UpdateList(Collection> changes) { + public UpdateList(Collection changes) { this.changes = new HashSet<>(changes); - this.selected = new HashSet<>(); } - public Collection> getChanges() { - return changes; + public UpdateList(GraphChanges graphChanges, Collection> coll) { + this.changes = new HashSet<>(); + for (Pair p : coll) { + changes.add(create(graphChanges, p)); + } } - public HashSet> getSelected() { - return selected; + protected PropertyChange create(GraphChanges changes, Pair change) { + return new PropertyChange(changes, change); } - public void addChange(Pair change) { + public Collection getChanges() { + return changes; + } + + public void addChange(PropertyChange change) { changes.add(change); } - public void removeChange(Pair change) { + public void removeChange(PropertyChange change) { changes.remove(change); - selected.remove(change); - } - - public void addSelected(Pair change) { - selected.add(change); } - public void removeSelected(Pair change) { - selected.remove(change); - } - - public boolean isSelected(Pair change) { - return selected.contains(change); + public Collection getSelected() { + HashSet selected = new HashSet<>(); + for (PropertyChange c : selected) { + if (c.selected()) + selected.add(c); + } + return selected; } - public void clear() { - changes.clear(); - selected.clear(); + public void clearSelected() { + for (PropertyChange c : changes) + c.select(false); } - public void clearSelected() { - selected.clear(); + public PropertyChange getChange(ReadGraph g, Statement s) throws DatabaseException { + for (PropertyChange c : changes) { + if (s.equals(c.pair.first)) + return c; + if (s.equals(c.pair.second)) + return c; + } + return null; } - public Collection> getChanges(ReadGraph g, Resource r) throws DatabaseException{ - List> list = new ArrayList<>(); - for (Pair pair : changes) { - if (pair.first != null) { - if (pair.first.getSubject().equals(r)) { + public Collection getChanges(ReadGraph g, Resource r) throws DatabaseException{ + List list = new ArrayList<>(); + for (PropertyChange pair : changes) { + if (pair.getFirst() != null) { + if (pair.getFirst().getSubject().equals(r)) { list.add(pair); continue; } - if (pair.first.getObject().equals(r)) { + if (pair.getFirst().getObject().equals(r)) { list.add(pair); continue; } } - if (pair.second != null) { - if (pair.second.getSubject().equals(r)) { + if (pair.getSecond() != null) { + if (pair.getSecond().getSubject().equals(r)) { list.add(pair); continue; } - if (pair.second.getObject().equals(r)) { + if (pair.getSecond().getObject().equals(r)) { list.add(pair); continue; } 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 72e1785..6809ea7 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 @@ -104,6 +104,8 @@ public abstract class UpdateOp { private boolean _select(boolean select) { if (select == selected) return true; + if (applied) + return false; if (select) { if (selectParentOps()) { for (UpdateOp op : parentOps) -- 2.45.1