]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/G2DSceneGraph.java
Minor refactorings related to SCL constructors
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / G2DSceneGraph.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.g2d;
13
14 import java.awt.Component;
15 import java.awt.Container;
16 import java.awt.Graphics2D;
17 import java.util.ArrayDeque;
18 import java.util.Deque;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import javax.swing.JComponent;
27 import javax.swing.RepaintManager;
28
29 import org.simantics.scenegraph.ILookupService;
30 import org.simantics.scenegraph.INode;
31 import org.simantics.scenegraph.LookupService;
32 import org.simantics.scenegraph.ParentNode;
33 import org.simantics.scenegraph.g2d.events.EventDelegator;
34 import org.simantics.scenegraph.g2d.events.INodeEventHandlerProvider;
35 import org.simantics.scenegraph.g2d.events.NodeEventHandler;
36
37 /**
38  * The root node of a 2D scene graph.
39  * 
40  * Implements {@link ILookupService} according to the reference implementation
41  * {@link LookupService}.
42  * 
43  * @author J-P Laine
44  */
45 @SuppressWarnings("deprecation")
46 public class G2DSceneGraph extends G2DParentNode implements ILookupService, INodeEventHandlerProvider {
47
48     private static final long          serialVersionUID = -7066146333849901429L;
49     
50     public static final String        IGNORE_FOCUS = "ignoreFocus";
51
52     protected transient Container     rootPane         = null;
53     // TODO: swing dependency in here might not be a good idea
54
55     // This variable is actually used in remote use, when rendering is not performed locally
56     private transient final Object     treeLock         = new Object();
57
58     private HashMap<Object, Integer>   pending          = new HashMap<Object, Integer>();
59     private HashMap<String, Object>    globalProperties = new HashMap<String, Object>();
60
61     /**
62      * For preventing duplicates on the nodesToRemove queue.
63      */
64     protected transient Set<INode>     nodesToRemoveSet = new HashSet<INode>();
65     protected Deque<INode>             nodesToRemove    = new ArrayDeque<INode>();
66
67     private transient EventDelegator   eventDelegator   = new EventDelegator(this);
68     private transient NodeEventHandler eventHandler     = new NodeEventHandler(this);
69
70     /**
71      * The node that has input focus in the scene graph. The input node will
72      * receive key and command events.
73      */
74     private transient IG2DNode         focusNode;
75
76     /**
77      * The custom repaint manager of this scene graph.
78      */
79     private transient G2DRepaintManager repaintManager;
80
81     /**
82      * Returns the event delegator, that is responsible for delegating events to nodes in the sg tree
83      * 
84      * @return EventDelegator instance, always not null
85      */
86     public EventDelegator getEventDelegator() {
87         return eventDelegator;
88     }
89
90     /**
91      * Returns the node event handler, that is responsible for delegating events
92      * to nodes in the sg tree.
93      * 
94      * @return NodeEventHandler instance for this scene graph, always non-null
95      */
96     public NodeEventHandler getEventHandler() {
97         return eventHandler;
98     }
99
100     public void setFocusNode(IG2DNode focusNode) {
101         this.focusNode = focusNode;
102     }
103
104     public IG2DNode getFocusNode() {
105         return focusNode;
106     }
107
108     @Override
109     public void render(Graphics2D g2d) {
110         performCleanup();
111         Component rootPane = getRootPane();
112         if (rootPane != null)
113             g2d.setRenderingHint(G2DRenderingHints.KEY_COMPONENT, rootPane);
114         synchronized(treeLock) {
115             super.render(g2d);
116         }
117     }
118
119     /**
120      * Util method for executing updates to scenegraph tree
121      * NOTE: You should really consider performance issues when using this
122      * 
123      * @param r  Runnable to be executed while rendering is not performed
124      */
125     public void syncExec(Runnable r) {
126         synchronized(treeLock) {
127             r.run();
128         }
129     }
130
131     /**
132      * Set rootpane for swing components. This is used as parent for the components created by ComponentNode.
133      * 
134      * @param rootPane Component that is used as a parent for the swing component (This shouldn't be visible)
135      */
136     public void setRootPane(JComponent rootPane) {
137         synchronized (RepaintManager.class) {
138             RepaintManager old = RepaintManager.currentManager(rootPane);
139             old = findProperRepaintManager(old);
140             this.repaintManager = new G2DRepaintManager(rootPane.getClass(), old);
141             RepaintManager.setCurrentManager(repaintManager);
142         }
143         this.rootPane = rootPane;
144     }
145
146     /**
147      * Set rootpane for swing components. This is used as parent for the components created by ComponentNode.
148      * Supports separate component that is responsible for repainting the scenegraph.
149      * 
150      * @param rootPane     Component that is used as a parent for the swing component (This shouldn't be visible)
151      * @param paintContext Component that is responsible for repainting the scenegraph
152      */
153     public void setRootPane(Container rootPane, Component paintContext) {
154         synchronized (RepaintManager.class) {
155             RepaintManager old = RepaintManager.currentManager(paintContext);
156             old = findProperRepaintManager(old);
157             this.repaintManager = new G2DRepaintManager(paintContext.getClass(), old);
158             RepaintManager.setCurrentManager(repaintManager);
159         }
160         this.rootPane = rootPane;
161         eventHandler.setRootPane(rootPane);
162     }
163
164     private RepaintManager findProperRepaintManager(RepaintManager old) {
165         while (old instanceof G2DRepaintManager) {
166             G2DRepaintManager g2drm = (G2DRepaintManager) old;
167             old = g2drm.getDelegate();
168         }
169         return old;
170     }
171
172     public G2DRepaintManager getRepaintManager() {
173         return repaintManager;
174     }
175
176     /**
177      * Put the node to the remove queue
178      */
179     @Override
180     public void asyncRemoveNode(INode node) {
181         synchronized(nodesToRemove) {
182             // Prevent nodes from winding up twice on the nodesToRemove queue
183             if (nodesToRemoveSet.add(node)) {
184                 nodesToRemove.add(node); // This is performed when called inside the render
185             }
186         }
187     }
188
189     /**
190      * Perform the actual removal of the nodes in the nodesToRemove list
191      */
192     public void performCleanup() {
193         synchronized(nodesToRemove) {
194             while(nodesToRemove.size() > 0) {
195                 INode node = nodesToRemove.removeFirst();
196                 ParentNode<?> parent = node.getParent();
197                 // This works around issue #2071
198                 if (parent != null)
199                     parent.removeNode(node);
200             }
201             if (!nodesToRemoveSet.isEmpty())
202                 nodesToRemoveSet.clear();
203         }
204     }
205
206     @Override
207     public void cleanup() {
208         super.cleanup();
209         nodesToRemove.clear();
210         nodesToRemove = null;
211         nodesToRemoveSet.clear();
212         nodesToRemoveSet = null;
213         eventHandler.dispose();
214         eventHandler = null;
215         eventDelegator.dispose();
216         eventDelegator = null;
217     }
218
219     public Container getRootPane() {
220         return (Container) this.rootPane;
221     }
222
223     @Override
224     public String toString() {
225         return super.toString() + " [root pane=" + rootPane + "]";
226     }
227
228     @Override
229     public ParentNode<?> getRootNode() {
230         // This is a root node!
231         return this;
232     }
233
234     // ILookupService implementation
235
236     private final Object             lookupLock = new Object();
237     private final Map<String, INode> toNode     = new HashMap<String, INode>();
238     private final Map<INode, String> toId       = new HashMap<INode, String>();
239
240     transient Logger                 logger     = Logger.getLogger(getClass().getName());
241
242     @Override
243     public INode map(String id, INode node) {
244         if (id == null)
245             throw new NullPointerException("null id");
246         if (node == null)
247             throw new NullPointerException("null node");
248
249         INode oldNode;
250         String oldId;
251         synchronized (lookupLock) {
252             oldNode = toNode.put(id, node);
253             oldId = toId.put(node, id);
254
255             // Keep the mapping a consistent bijection:
256             // If ID => INode mapping is removed, the INode => ID mappings must
257             // removed also.
258
259             if (oldNode != null && !oldNode.equals(node)) {
260                 String removedId = toId.remove(oldNode);
261                 if (!id.equals(removedId))
262                     toNode.remove(removedId);
263             }
264             if (oldId != null && !oldId.equals(id)) {
265                 INode removedNode = toNode.remove(oldId);
266                 if (removedNode != node)
267                     toId.remove(removedNode);
268             }
269         }
270         if (logger.isLoggable(Level.FINER))
271             logger.fine("map(" + id + ", " + node + ")");
272         if (oldNode != null || oldId != null) {
273             if (logger.isLoggable(Level.FINE)) {
274                 logger.info("replaced mappings for ID " + oldId + " and node " + oldNode);
275             }
276         }
277         return oldNode;
278     }
279
280     @Override
281     public INode unmap(String id) {
282         INode node;
283         String mappedId;
284         synchronized (lookupLock) {
285             node = toNode.remove(id);
286             if (node == null)
287                 return null;
288             mappedId = toId.remove(node);
289         }
290         if (logger.isLoggable(Level.FINER))
291             logger.fine("unmap(" + id + "): " + node);
292         if (mappedId != null && !mappedId.equals(id)) {
293             if (logger.isLoggable(Level.WARNING))
294                 logger.log(Level.WARNING, "mapping was out-of-sync: " + id + " => " + node + " & " + mappedId + " => " + node, new Exception("trace"));
295         }
296         return node;
297     }
298
299     @Override
300     public String unmap(INode node) {
301         String id;
302         INode mappedNode;
303         synchronized (lookupLock) {
304             id = toId.remove(node);
305             if (node == null)
306                 return null;
307             mappedNode = toNode.remove(id);
308         }
309         if (logger.isLoggable(Level.FINER))
310             logger.fine("unmap(" + node + "): " + id);
311         if (mappedNode != null && node != mappedNode) {
312             if (logger.isLoggable(Level.WARNING))
313                 logger.log(Level.WARNING, "mapping was out-of-sync: " + node + " => " + id + " & " + id + " => " + mappedNode, new Exception("trace"));
314         }
315         return id;
316     }
317
318     @Override
319     public INode lookupNode(String id) {
320         synchronized (lookupLock) {
321             return toNode.get(id);
322         }
323     }
324
325     @Override
326     public String lookupId(INode node) {
327         synchronized (lookupLock) {
328             return toId.get(node);
329         }
330     }
331
332     public boolean isPending() {
333         return !pending.isEmpty();
334     }
335
336     synchronized public void increasePending(Object object) {
337         Integer ref = pending.get(object);
338         if (ref == null) pending.put(object, 1);
339         else pending.put(object, ref+1);
340     }
341
342     synchronized public void setPending(Object object) {
343         pending.put(object, 1);
344     }
345
346     synchronized public void clearPending(Object object) {
347         pending.remove(object);
348     }
349
350     synchronized public void decreasePending(Object object) {
351         Integer ref = pending.get(object);
352         if (ref == null) {
353             return;
354             //throw new IllegalStateException("Ref count in unregister was 0 for " + object);
355         }
356         if (ref > 1) pending.put(object, ref-1);
357         else if (ref==1) pending.remove(object);
358         else {
359             return;
360             //throw new IllegalStateException("Ref count in unregister was 0 for " + object);
361         }
362     }
363     
364     synchronized public void setGlobalProperty(String key, Object value) {
365         globalProperties.put(key, value);
366     }
367     
368     @SuppressWarnings("unchecked")
369         synchronized public <T> T getGlobalProperty(String key, T defaultValue) {
370         T t = (T)globalProperties.get(key);
371         if(t == null) return defaultValue;
372         return t;
373     }
374
375 }