]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBaseData.java
Fix diagram profiles to work with latest DB changes
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / StyleBaseData.java
1 package org.simantics.diagram.profile;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.concurrent.ConcurrentHashMap;
8
9 import org.simantics.db.Resource;
10 import org.simantics.scenegraph.profile.DataNodeMap;
11 import org.simantics.scenegraph.profile.EvaluationContext;
12 import org.simantics.scenegraph.profile.Style;
13 import org.simantics.scenegraph.profile.common.ObserverGroupListener;
14 import org.simantics.scl.runtime.tuple.Tuple;
15 import org.simantics.scl.runtime.tuple.Tuple3;
16
17 public class StyleBaseData {
18
19     protected final Map<Tuple, Object> values   = new ConcurrentHashMap<>();
20
21     private Map<Tuple3, ObserverGroupListener> listeners = new HashMap<>();
22
23     private final Map<Style, List<Resource>> removals = new HashMap<>();
24
25     private StyleBaseData() {
26         
27     }
28     
29     private static StyleBaseData INSTANCE;
30     
31     public static StyleBaseData getInstance() {
32         if(INSTANCE == null) {
33             INSTANCE = new StyleBaseData();
34         }
35         return INSTANCE;
36     }
37     
38     public void removeValue(Tuple t) {
39         values.remove(t);
40     }
41     
42     public void putValue(Tuple t, Object o) {
43         values.put(t, o);
44     }
45     
46     public <T> T getValue(Tuple t) {
47         return (T)values.get(t);
48     }
49
50     public synchronized void removeItem(Style s, Resource r) {
51         List<Resource> l = removals.get(s);
52         if(l == null) {
53             l = new ArrayList<>();
54             removals.put(s, l);
55         }
56         l.add(r);
57     }
58     
59     public void putListener(Tuple3 key, ObserverGroupListener listener) {
60         listeners.put(key, listener);
61     }
62     
63     public void removeListener(Tuple3 key) {
64         listeners.remove(key);
65     }
66
67     public ObserverGroupListener getListener(Tuple3 key) {
68         return listeners.get(key);
69     }
70
71     public synchronized void applyRemovals(EvaluationContext evaluationContext, StyleBase s) {
72         
73         List<Resource> rs = removals.remove(s);
74         if(rs == null) return;
75         
76         DataNodeMap map = evaluationContext.getConstant(ProfileKeys.NODE_MAP);
77
78         for (Resource item : rs) {
79             s.cleanupStyleForItem(evaluationContext, map, item);
80         }
81         
82     }
83
84 }