]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/StyleBaseData.java
ca97fea784d3a35c3449fb8598fe611dbf4c5b6e
[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     private static StyleBaseData INSTANCE;
20
21     protected final Map<Tuple, Object> values   = new ConcurrentHashMap<>();
22
23     private Map<Tuple3, ObserverGroupListener> listeners = new HashMap<>();
24
25     private final Map<Style, List<Resource>> removals = new HashMap<>();
26
27     private StyleBaseData() {
28     }
29
30     public static StyleBaseData getInstance() {
31         if (INSTANCE == null) {
32             synchronized (StyleBaseData.class) {
33                 if (INSTANCE == null) {
34                     INSTANCE = new StyleBaseData();
35                 }
36             }
37         }
38         return INSTANCE;
39     }
40
41     public void removeValue(Tuple t) {
42         values.remove(t);
43     }
44
45     public void putValue(Tuple t, Object o) {
46         values.put(t, o);
47     }
48
49     public <T> T getValue(Tuple t) {
50         return (T) values.get(t);
51     }
52
53     public synchronized void removeItem(Style s, Resource r) {
54         List<Resource> l = removals.get(s);
55         if (l == null) {
56             l = new ArrayList<>();
57             removals.put(s, l);
58         }
59         l.add(r);
60     }
61
62     public void putListener(Tuple3 key, ObserverGroupListener listener) {
63         listeners.put(key, listener);
64     }
65
66     public void removeListener(Tuple3 key) {
67         listeners.remove(key);
68     }
69
70     public ObserverGroupListener getListener(Tuple3 key) {
71         return listeners.get(key);
72     }
73
74     public synchronized void applyRemovals(EvaluationContext evaluationContext, StyleBase s) {
75         List<Resource> rs = removals.remove(s);
76         if (rs == null)
77             return;
78
79         DataNodeMap map = evaluationContext.getConstant(ProfileKeys.NODE_MAP);
80
81         for (Resource item : rs) {
82             s.cleanupStyleForItem(evaluationContext, map, item);
83         }
84     }
85
86 }