*******************************************************************************/
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;
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;
/**
*/
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>();
+ @SuppressWarnings("unchecked")
+ 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
*/
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();
}
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);
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);
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!
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);
}
}
private ObserverGroupListener getListener(Resource runtime, Group group) {
- return listeners.get(Pair.make(runtime, group));
+ return StyleBaseData.getInstance().getListener(new Tuple3(this, runtime, group));
}
}