]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/Updater.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / Updater.java
1 package org.simantics.diagram.profile;\r
2 \r
3 import java.util.HashMap;\r
4 import java.util.HashSet;\r
5 import java.util.Map;\r
6 import java.util.concurrent.TimeUnit;\r
7 import java.util.concurrent.atomic.AtomicBoolean;\r
8 \r
9 import org.simantics.diagram.elements.DiagramNodeUtil;\r
10 import org.simantics.g2d.canvas.ICanvasContext;\r
11 import org.simantics.scenegraph.INode;\r
12 import org.simantics.scenegraph.g2d.G2DNode;\r
13 import org.simantics.utils.threads.ThreadUtils;\r
14 \r
15 class Updater implements Runnable {\r
16 \r
17         private static Updater INSTANCE;\r
18         \r
19         Map<INode, ICanvasContext> requesters = new HashMap<INode, ICanvasContext>();\r
20         \r
21         AtomicBoolean state = new AtomicBoolean(false);\r
22         \r
23         private static long time = System.nanoTime();\r
24         \r
25         private Updater() {\r
26         }\r
27         \r
28         @Override\r
29         public void run() {\r
30 \r
31                 // Stop this if nothing is to be done, need synchonization since this is not AWT\r
32                 synchronized(requesters) {\r
33                         if(requesters.isEmpty()) {\r
34                                 state.set(false);\r
35                                 throw new RuntimeException();\r
36                         }\r
37                 }\r
38                 \r
39                 // TODO: in unit tests this should not be AWT\r
40                 ThreadUtils.AWT_EDT.execute(new Runnable() {\r
41 \r
42                         @Override\r
43                         public void run() {\r
44                                 \r
45                                 time = System.nanoTime();\r
46                                 \r
47                                 synchronized(requesters) {\r
48                                         HashSet<ICanvasContext> ctxSet = new HashSet<ICanvasContext>();\r
49                                         for(ICanvasContext ctx : requesters.values()) {\r
50                                                 if(ctx != null) {\r
51                                                         if(ctxSet.add(ctx)) ctx.getContentContext().setDirty();\r
52                                                 }\r
53                                         }\r
54                                 }\r
55                                                 \r
56                         }\r
57                         \r
58                 });\r
59                 \r
60         }\r
61         \r
62         public void register(INode node) {\r
63                 // We use ths size of this map to determine whether updates are needed, this is done in AWT thread\r
64                 synchronized(requesters) {\r
65                         if(requesters.size() == 0) {\r
66                                 if(state.compareAndSet(false, true)) {\r
67                                         ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(this, 0, 500, TimeUnit.MILLISECONDS);\r
68                                 }\r
69                         }\r
70                         ICanvasContext context = DiagramNodeUtil.getPossibleCanvasContext((G2DNode)node);\r
71                         requesters.put(node, context);\r
72                 }\r
73         }\r
74         \r
75         public void unregister(INode node) {\r
76                 synchronized(requesters) {\r
77                         requesters.remove(node);\r
78                 }\r
79         }\r
80         \r
81         public long getTime() {\r
82                 return time;\r
83         }\r
84         \r
85         public static Updater getInstance() {\r
86                 if(INSTANCE == null) {\r
87                         INSTANCE = new Updater();\r
88                 }\r
89                 return INSTANCE;\r
90         }\r
91         \r
92 }