]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fix diagram profiles to work with latest DB changes
authorAntti Villberg <antti.villberg@semantum.fi>
Wed, 26 Sep 2018 04:53:54 +0000 (06:53 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 26 Sep 2018 09:17:47 +0000 (11:17 +0200)
gitlab #96

Change-Id: I799c1f22422126290aed0868d6fe4ceed6bccaa9

bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/TypeGroup.java
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/ResourceSCLTextGridStyle.java
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/SCLTextGridStyle.java
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBase.java
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBaseData.java [new file with mode: 0644]
bundles/org.simantics.diagram/src/org/simantics/diagram/profile/TextGridStyle.java

index e025fb2ddf16927783a2fc353817b87c9c068155..522c0b9d5b73661051370fa6cecdb446ffe656a5 100644 (file)
@@ -47,6 +47,31 @@ public class TypeGroup implements Group {
         this.name = name;
     }
 
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((types == null) ? 0 : types.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        TypeGroup other = (TypeGroup) obj;
+        if (types == null) {
+            if (other.types != null)
+                return false;
+        } else if (!types.equals(other.types))
+            return false;
+        return true;
+    }
+
     /**
      * Initialize TypeGroup from a DIAGRAM.Group instance.
      * 
index 3bbbf8081dea77cfbb7792d0c1ecbd97d3e2a1ed..224e767c2792956f83cb52399a54a2b8e16fce6c 100644 (file)
@@ -23,11 +23,10 @@ import org.simantics.utils.datastructures.Pair;
  */
 public class ResourceSCLTextGridStyle extends TextGridStyle {
 
-       final Resource style;
        final Font font;
 
        public ResourceSCLTextGridStyle(ReadGraph graph, Resource style) throws DatabaseException {
-               this.style = style;
+           super(style);
                G2DResource G2D = G2DResource.getInstance(graph);
         Resource fontR = graph.getPossibleObject(style, G2D.HasFont);
                if(fontR != null) {
@@ -51,7 +50,7 @@ public class ResourceSCLTextGridStyle extends TextGridStyle {
 
        @Override
        protected Object getIdentity(Resource entry) {
-               return new Pair<Resource, Resource>(style, entry);
+               return new Pair<Resource, Resource>(getResource(), entry);
        }
 
        @Override
@@ -63,7 +62,7 @@ public class ResourceSCLTextGridStyle extends TextGridStyle {
        public MonitorTextGridResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {
                DiagramResource DIA = DiagramResource.getInstance(graph);
 
-               Variable styleVariable = Variables.getVariable(graph, style);
+               Variable styleVariable = Variables.getVariable(graph, getResource());
                Function1<Resource,Tuple3> function = styleVariable.getPossiblePropertyValue(graph, DIA.ResourceSCLTextGridStyle_texts);
                Tuple3 result = Simantics.applySCLRead(graph, function, element);
                
@@ -79,7 +78,7 @@ public class ResourceSCLTextGridStyle extends TextGridStyle {
 
        @Override
        public String getNodeName() {
-               return "" + style.getResourceId();
+               return "" + getResource().getResourceId();
        }
 
 }
index 4401f2213c51aa0a58b0b0c8170ab5e9636d281c..f084799fdddf0d71dec97a3a7b940f3a17a886d9 100644 (file)
@@ -25,11 +25,10 @@ import org.simantics.utils.datastructures.Pair;
  */
 public class SCLTextGridStyle extends TextGridStyle {
 
-       final Resource style;
        final Font font;
 
        public SCLTextGridStyle(ReadGraph graph, Resource style) throws DatabaseException {
-               this.style = style;
+           super(style);
                G2DResource G2D = G2DResource.getInstance(graph);
         Resource fontR = graph.getPossibleObject(style, G2D.HasFont);
                if(fontR != null) {
@@ -53,7 +52,7 @@ public class SCLTextGridStyle extends TextGridStyle {
 
        @Override
        protected Object getIdentity(Resource entry) {
-               return new Pair<Resource, Resource>(style, entry);
+               return new Pair<Resource, Resource>(getResource(), entry);
        }
 
        @Override
@@ -81,7 +80,7 @@ public class SCLTextGridStyle extends TextGridStyle {
                if (moduleVariable == null)
                        return null;
 
-               Variable styleVariable = Variables.getVariable(graph, style);
+               Variable styleVariable = Variables.getVariable(graph, getResource());
                Function1<Variable,Tuple3> function = styleVariable.getPossiblePropertyValue(graph, DIA.SCLTextGridStyle_texts);
                Tuple3 result = Simantics.applySCLRead(graph, function, moduleVariable);
                
@@ -97,7 +96,7 @@ public class SCLTextGridStyle extends TextGridStyle {
 
        @Override
        public String getNodeName() {
-               return "" + style.getResourceId();
+               return "" + getResource().getResourceId();
        }
 
 }
index 21071ba74bd9453e9c3bdf166c0240bd46156c54..28b03a1f66797c03e240d6896eeb0941ec57a955 100644 (file)
  *******************************************************************************/
 package org.simantics.diagram.profile;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
@@ -44,8 +39,7 @@ import org.simantics.scenegraph.profile.Style;
 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
 import org.simantics.scenegraph.profile.common.ObserverGroupValueListener;
 import org.simantics.scenegraph.profile.impl.DebugPolicy;
-import org.simantics.scl.runtime.tuple.Tuple;
-import org.simantics.scl.runtime.tuple.Tuple2;
+import org.simantics.scl.runtime.tuple.Tuple3;
 import org.simantics.utils.datastructures.Pair;
 
 /**
@@ -72,14 +66,48 @@ import org.simantics.utils.datastructures.Pair;
  */
 public abstract class StyleBase<Result> implements Style {
 
-    protected final Map<Tuple, Result> values   = new ConcurrentHashMap<Tuple, Result>();
-
-//    private Map<Resource,ObserverGroupListener>         listeners = new ConcurrentHashMap<Resource, ObserverGroupListener>();
+    private Object identity;
+    
+    public StyleBase(Object identity) {
+        this.identity = identity;
+    }
 
-    private Map<Pair<Resource, Group>, ObserverGroupListener> listeners = new HashMap<Pair<Resource, Group>, ObserverGroupListener>();
+    public StyleBase() {
+        this.identity = getClass();
+    }
     
+    protected <T> T getIdentity() {
+        return (T)identity;
+    }
+    
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((identity == null) ? 0 : identity.hashCode());
+        return result;
+    }
 
-    private final List<Resource>                removals = new ArrayList<Resource>();
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        StyleBase other = (StyleBase) obj;
+        if (identity == null) {
+            if (other.identity != null)
+                return false;
+        } else if (!identity.equals(other.identity))
+            return false;
+        return true;
+    }
+
+    protected Resource getResource() {
+        return getIdentity();
+    }
 
     /**
      * For caching this simple base request that is done in every
@@ -175,9 +203,9 @@ public abstract class StyleBase<Result> implements Style {
      */
     public void styleResultChanged(Observer observer, Resource runtimeDiagram, Resource object, Result result) {
         if (result == null)
-            values.remove(new Tuple2(runtimeDiagram, object));
+            StyleBaseData.getInstance().removeValue(new Tuple3(this, runtimeDiagram, object));
         else
-            values.put(new Tuple2(runtimeDiagram, object), result);
+            StyleBaseData.getInstance().putValue(new Tuple3(this, runtimeDiagram, object), result);
         observer.update();
     }
 
@@ -283,12 +311,11 @@ public abstract class StyleBase<Result> implements Style {
               if (DebugPolicy.DEBUG_PROFILE_STYLE_GROUP_TRACKING)
                   System.out.println(style + ": removed from group " + group + ": " + item);
 
-              synchronized (style.removals) {
-                 style.removals.add(item);
-              }
+              StyleBaseData.getInstance().removeItem(style, item);
 
               // TODO: do something here to dispose of ObserverGroupValueListeners?
               super.remove(item);
+              
           }  
           
     }
@@ -308,7 +335,7 @@ public abstract class StyleBase<Result> implements Style {
 
             listener = new GroupListener<Result>(backend.getSession(), runtimeDiagram, entry, this, group, observer);
 
-            listeners.put(Pair.make(runtimeDiagram, group), listener);
+            StyleBaseData.getInstance().putListener(new Tuple3(this, runtimeDiagram, group), listener);
 
             group.trackItems(backend, runtimeDiagram, listener);
 
@@ -390,7 +417,7 @@ public abstract class StyleBase<Result> implements Style {
             listener.removeEntry(entry);
             if (!listener.hasEntries()) {
                 listener.dispose();
-                listeners.remove(Pair.make(runtimeDiagram, group));
+                StyleBaseData.getInstance().removeListener(new Tuple3(this, runtimeDiagram, group));
             }
 
             // This was too eager when multiple groups were tracked!
@@ -425,19 +452,13 @@ public abstract class StyleBase<Result> implements Style {
         if (DebugPolicy.DEBUG_PROFILE_STYLE_APPLICATION)
             System.out.println(StyleBase.this + ": applying style for items: " + listener.getItems());
 
-        if (!removals.isEmpty()) {
-            Resource[] removed;
-            synchronized (removals) {
-                removed = removals.toArray(Resource.NONE);
-                removals.clear();
-            }
-            for (Resource item : removed) {
-                cleanupStyleForItem(evaluationContext, map, item);
-            }
-        }
+        
+        StyleBaseData data = StyleBaseData.getInstance();
+        
+        data.applyRemovals(evaluationContext, this);
         
         for (Object item : listener.getItems()) {
-            Result value = values.get(new Tuple2(evaluationContext.getResource(), item));
+            Result value = data.getValue(new Tuple3(this, evaluationContext.getResource(), item));
             applyStyleForItem(evaluationContext, map, item, value);
         }
         
@@ -480,7 +501,7 @@ public abstract class StyleBase<Result> implements Style {
     }
     
     private ObserverGroupListener getListener(Resource runtime, Group group) {
-        return listeners.get(Pair.make(runtime, group));
+        return StyleBaseData.getInstance().getListener(new Tuple3(this, runtime, group));
     }
 
 }
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBaseData.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBaseData.java
new file mode 100644 (file)
index 0000000..5bd51b2
--- /dev/null
@@ -0,0 +1,84 @@
+package org.simantics.diagram.profile;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.simantics.db.Resource;
+import org.simantics.scenegraph.profile.DataNodeMap;
+import org.simantics.scenegraph.profile.EvaluationContext;
+import org.simantics.scenegraph.profile.Style;
+import org.simantics.scenegraph.profile.common.ObserverGroupListener;
+import org.simantics.scl.runtime.tuple.Tuple;
+import org.simantics.scl.runtime.tuple.Tuple3;
+
+public class StyleBaseData {
+
+    protected final Map<Tuple, Object> values   = new ConcurrentHashMap<>();
+
+    private Map<Tuple3, ObserverGroupListener> listeners = new HashMap<>();
+
+    private final Map<Style, List<Resource>> removals = new HashMap<>();
+
+    private StyleBaseData() {
+        
+    }
+    
+    private static StyleBaseData INSTANCE;
+    
+    public static StyleBaseData getInstance() {
+        if(INSTANCE == null) {
+            INSTANCE = new StyleBaseData();
+        }
+        return INSTANCE;
+    }
+    
+    public void removeValue(Tuple t) {
+        values.remove(t);
+    }
+    
+    public void putValue(Tuple t, Object o) {
+        values.put(t, o);
+    }
+    
+    public <T> T getValue(Tuple t) {
+        return (T)values.get(t);
+    }
+
+    public synchronized void removeItem(Style s, Resource r) {
+        List<Resource> l = removals.get(s);
+        if(l == null) {
+            l = new ArrayList<>();
+            removals.put(s, l);
+        }
+        l.add(r);
+    }
+    
+    public void putListener(Tuple3 key, ObserverGroupListener listener) {
+        listeners.put(key, listener);
+    }
+    
+    public void removeListener(Tuple3 key) {
+        listeners.remove(key);
+    }
+
+    public ObserverGroupListener getListener(Tuple3 key) {
+        return listeners.get(key);
+    }
+
+    public synchronized void applyRemovals(EvaluationContext evaluationContext, StyleBase s) {
+        
+        List<Resource> rs = removals.remove(s);
+        if(rs == null) return;
+        
+        DataNodeMap map = evaluationContext.getConstant(ProfileKeys.NODE_MAP);
+
+        for (Resource item : rs) {
+            s.cleanupStyleForItem(evaluationContext, map, item);
+        }
+        
+    }
+
+}
index 54972155d8d69579294e4d6e49718a453e14e66d..60e8fa6da7763767cd4a041ab9195d7c35078533 100644 (file)
@@ -38,17 +38,18 @@ public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
        private final Color BACKGROUND_COLOR = new Color(255, 255, 255, 192);
        private static final Rectangle2D EMPTY_BOUNDS = new Rectangle2D.Double(0, 0, 0, 0);
 
-       // NOTE: this is a hack
-       String id;
+//     // NOTE: this is a hack
+//     String id;
 
        protected double xOffset;
        protected double yOffset;
 
-       public TextGridStyle() {
-               this(0.0, 2.1);
+       public TextGridStyle(Resource r) {
+               this(r, 0.0, 2.1);
        }
 
-       public TextGridStyle(double xOffset, double yOffset) {
+       public TextGridStyle(Resource r, double xOffset, double yOffset) {
+           super(r);
                this.xOffset = xOffset;
                this.yOffset = yOffset;
        }
@@ -175,29 +176,33 @@ public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
 
                        // This assumes that this TextGridStyle instance will be devoted to
                        // this row ID until the end of its life.
-                       String id = result.getRowId();
-                       //System.out.println(this + " ID: " + id);
-                       if (!id.equals(this.id)) {
-                               //System.out.println(this + " SET ID: " + this.id + " -> " + id);
-                               this.id = id;
-                       }
+//                     String id = result.getRowId();
+//                     System.out.println(this + " ID: " + id);
+//                     if (!id.equals(this.id)) {
+//                             System.out.println(this + " SET ID: " + this.id + " -> " + id);
+//                             this.id = id;
+//                     }
+                       
 
                        Integer newRow = observer.getTemporaryProperty(_node, "location");
                        if (newRow == null)
                                newRow = 1;
 
-                       // Remove from existing row to add to another row if necessary.
-                       Integer row = observer.getProperty(_node, id);
+            // Remove from existing row to add to another row if necessary.
+            Integer row = getCurrentRowNumber(observer, _node);
                        if (row != null && row != newRow) {
                                String actualId = node.getRowId(row);
+                               String id = observer.getProperty(_node, rowIdKey());
                                if (id.equals(actualId)) {
                                        node.removeRow(row);
                                }
                        }
                        row = newRow;
 
-                       node.setRowId(row, id);
-                       observer.setProperty(_node, id, row);
+                       node.setRowId(row, result.getRowId());
+                       
+                       setCurrentRowNumber(observer, _node, result.getRowId(), row);
+                       
                        observer.setTemporaryProperty(_node, "location", row + 1);
 
                        node.setText(2, row, value2);
@@ -361,18 +366,45 @@ public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
 
        @Override
        protected void cleanupStyleForNode(EvaluationContext observer, INode _node) {
-               Integer row = observer.getProperty(_node, id);
+           Integer row = getCurrentRowNumber(observer, _node);
                //System.out.println(this + " cleanup(" + id + ", " + row + ")");
                //System.out.println(element);
                if (row == null)
                        return;
-               observer.setProperty(_node, id, null);
+               clearCurrentRowNumber(observer, _node);
                TextGridNode node = ProfileVariables.browseChild(_node, "TextGridStyle");
                if (node != null)
                        node.removeRow(row);
        }
+       
+       private Integer getCurrentRowNumber(EvaluationContext observer, INode _node) {
+        String rowId = observer.getProperty(_node, rowIdKey());
+        return observer.getProperty(_node, rowId);
+       }
+
+    private void setCurrentRowNumber(EvaluationContext observer, INode _node, String rowId, int row) {
+        // Mapping style identity -> rowId (resourceId)
+        observer.setProperty(_node, rowIdKey(), rowId);
+        // Mapping rowId (resourceId) -> row number
+        observer.setProperty(_node, rowId, row);
+    }
+
+       private void clearCurrentRowNumber(EvaluationContext observer, INode _node) {
+        String rowId = observer.getProperty(_node, rowIdKey());
+        if(rowId != null) {
+            observer.setProperty(_node, rowIdKey(), null);
+            Integer row = observer.getProperty(_node, rowId);
+            if(row != null) {
+                observer.setProperty(_node, rowId, null);
+            }
+        }
+    }
 
        protected void postProcessNode(TextGridNode node, int row) {
        }
+       
+       private String rowIdKey() {
+        return "style" + getIdentity().toString();
+       }
 
 }