]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
PropertyChange class (instead of Pair)
authorMarko Luukkainen <marko.luukkainen@vtt.fi>
Tue, 24 Oct 2017 14:26:17 +0000 (17:26 +0300)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Tue, 24 Oct 2017 14:26:17 +0000 (17:26 +0300)
org.simantics.interop.update/scl/Interop/Update.scl
org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java
org.simantics.interop.update/src/org/simantics/interop/update/model/ModelUpdate.java
org.simantics.interop.update/src/org/simantics/interop/update/model/PropertyChange.java [new file with mode: 0644]
org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java
org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java

index 8397844a1a42ce6743f76ff1af4c9c9b8ff3b93a..d4050330da1dcc37c467d0980de8ddabbdcfc7a2 100644 (file)
@@ -54,19 +54,25 @@ importJava "org.simantics.interop.update.model.UpdateList" where
   data UpdateList
   
   @JavaName getChanges
-  getListChanges :: UpdateList -> <Proc> [Pair]
+  getListChanges :: UpdateList -> <Proc> [PropertyChange]
   
   @JavaName getSelected
-  getListSelected :: UpdateList -> <Proc> MSet.T Pair
+  getListSelected :: UpdateList -> <Proc> [PropertyChange]
   
-  @JavaName addSelected
-  addListSelected :: UpdateList -> Pair -> <Proc>()
+  @JavaName getChanges
+  getResChanges :: UpdateList -> Resource -> <ReadGraph> [PropertyChange]
+  
+  @JavaName getChange
+  getStmChange :: UpdateList -> Statement -> <ReadGraph> Maybe PropertyChange
   
-  @JavaName removeSelected
-  removeListSelected :: UpdateList -> Pair -> <Proc>()
+  //@JavaName addSelected
+  //addListSelected :: UpdateList -> Pair -> <Proc>()
   
-  @JavaName isSelected
-  isListSelected :: UpdateList -> Pair -> <Proc> Boolean
+  //@JavaName removeSelected
+  //removeListSelected :: UpdateList -> Pair -> <Proc>()
+  
+  //@JavaName isSelected
+  //isListSelected :: UpdateList -> Pair -> <Proc> 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 -> <Proc> [Pair]
+
+importJava "org.simantics.interop.update.model.PropertyChange" where
+  data PropertyChange
+  
+  @JavaName "<init>"
+  createPropertyChange :: GraphChanges -> Maybe Statement -> Maybe Statement -> <Proc> PropertyChange
+  
+  @JavaName applied
+  pcApplied :: PropertyChange -> <Proc> Boolean
   
+  @JavaName selected
+  pcSelected :: PropertyChange -> <Proc> Boolean
+  
+  @JavaName "select"
+  pcSelect :: PropertyChange -> Boolean -> <Proc> Boolean
+  
+  @JavaName getFirst
+  getFirst :: PropertyChange -> <Proc> Maybe Statement
+  
+  @JavaName getSecond
+  getSecond :: PropertyChange -> <Proc> 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 -> <Proc> Pair
   
   @JavaName first
-  getFirst :: Pair -> <Proc> Maybe a
+  getPairFirst :: Pair -> <Proc> Maybe a
   
   @JavaName second
-  getSecond :: Pair -> <Proc> Maybe b
+  getPairSecond :: Pair -> <Proc> Maybe b
   
 importJava "org.simantics.utils.datastructures.BijectionMap" where
   data BijectionMap 
index 5a6646edc66521422f6170404960de1dc861a7d3..1779b8f6bbdaf188b1556a2c61d69669a3035374 100644 (file)
@@ -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<Statement, Statement> 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<Statement, Statement>) 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<Statement, Statement>) 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<Statement, Statement>) element);
+                               pc.select(true);
                        else
-                               update.getUpdateList().removeSelected((Pair<Statement, Statement>) element);
+                               pc.select(false);
                        
                        getViewer().refresh(element);
                }
index bf391d49e52e8a1fd367f1fbbfac29758b696314..46bf79ecbcdeccd3679b684e924ca13e0214a5b1 100644 (file)
@@ -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<GraphComparator,String> 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<ChangeFilter> 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<Statement, Statement> 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<Pair<Statement, Statement>> changes = new HashSet<>(updateList.getSelected());
-               for (Pair<Statement, Statement> 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<Statement, Statement> 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<Statement, Statement> 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<Statement, Statement> pair : updateList.getChanges()) {
-                       if (pair.first != null) {
+               for (PropertyChange pair : updateList.getChanges()) {
+                       if (pair.getFirst() != null) {
                                boolean found = false;
-                               for (Pair<Statement, Statement> 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 (file)
index 0000000..9867a39
--- /dev/null
@@ -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<Statement, Statement> pair;
+       protected boolean applied = false;
+       protected boolean selected = false;
+       
+       
+       public PropertyChange(GraphChanges changes, Statement first, Statement second) {
+               this.pair = new Pair<Statement, Statement>(first, second);
+               this.changes = changes;
+       }
+       
+       public PropertyChange(GraphChanges changes, Pair<Statement, Statement> 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;
+       }
+       
+       
+}
index 747cd78c7c2a5a4381677f288a4782cd789eac51..6f66ba399326f0eb4ffa480d3b9d3874c54841f4 100644 (file)
@@ -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<Pair<Statement, Statement>> changes;
-       private HashSet<Pair<Statement, Statement>> selected;
+       private HashSet<PropertyChange> changes;
        
        public UpdateList() {
                changes = new HashSet<>();
-               selected = new HashSet<>();
        }
        
-       public UpdateList(Collection<Pair<Statement, Statement>> changes) {
+       public UpdateList(Collection<PropertyChange> changes) {
                this.changes = new HashSet<>(changes);
-               this.selected = new HashSet<>();
        }
        
-       public Collection<Pair<Statement, Statement>> getChanges() {
-               return changes;
+       public UpdateList(GraphChanges graphChanges, Collection<Pair<Statement, Statement>> coll) {
+               this.changes = new HashSet<>();
+               for (Pair<Statement, Statement> p : coll) {
+                       changes.add(create(graphChanges, p));
+               }
        }
        
-       public HashSet<Pair<Statement, Statement>> getSelected() {
-               return selected;
+       protected PropertyChange create(GraphChanges changes, Pair<Statement, Statement> change) {
+               return new PropertyChange(changes, change);
        }
        
-       public void addChange(Pair<Statement, Statement> change) {
+       public Collection<PropertyChange> getChanges() {
+               return changes;
+       }
+
+       public void addChange(PropertyChange change) {
                changes.add(change);
        }
        
-       public void removeChange(Pair<Statement, Statement> change) {
+       public void removeChange(PropertyChange change) {
                changes.remove(change);
-               selected.remove(change);
-       }
-       
-       public void addSelected(Pair<Statement, Statement> change) {
-               selected.add(change);
        }
        
-       public void removeSelected(Pair<Statement, Statement> change) {
-               selected.remove(change);
-       }
-       
-       public boolean isSelected(Pair<Statement, Statement> change) {
-               return selected.contains(change);
+       public Collection<PropertyChange> getSelected() {
+               HashSet<PropertyChange> 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<Pair<Statement, Statement>> getChanges(ReadGraph g, Resource r) throws DatabaseException{
-               List<Pair<Statement, Statement>> list = new ArrayList<>();
-               for (Pair<Statement, Statement> pair : changes) {
-                       if (pair.first != null) {
-                               if (pair.first.getSubject().equals(r)) {
+       public Collection<PropertyChange> getChanges(ReadGraph g, Resource r) throws DatabaseException{
+               List<PropertyChange> 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;
                                }
index 72e17856a4a28589a02b09e21795097ea1da93b5..6809ea75811de08cf0b2d9013a454a8be484e1dc 100644 (file)
@@ -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)