Fix diagram profiles to work with latest DB changes
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / StyleBase.java
index 21071ba74bd9453e9c3bdf166c0240bd46156c54..e1d8b1fbf7e67e043c8af93202f03d95747d9216 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 Object identity;
 
-//    private Map<Resource,ObserverGroupListener>         listeners = new ConcurrentHashMap<Resource, ObserverGroupListener>();
+    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();
+    }
 
-    private final List<Resource>                removals = new ArrayList<Resource>();
+    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;
+    }
+
+    @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,9 +311,7 @@ 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 +334,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 +416,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 +451,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 +500,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));
     }
 
 }