1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d;
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;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
26 import javax.swing.JComponent;
27 import javax.swing.RepaintManager;
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;
38 * The root node of a 2D scene graph.
40 * Implements {@link ILookupService} according to the reference implementation
41 * {@link LookupService}.
45 @SuppressWarnings("deprecation")
46 public class G2DSceneGraph extends G2DParentNode implements ILookupService, INodeEventHandlerProvider {
48 private static final long serialVersionUID = -7066146333849901429L;
50 public static final String IGNORE_FOCUS = "ignoreFocus";
51 public static final String PICK_DISTANCE = "pickDistance";
53 protected transient Container rootPane = null;
54 // TODO: swing dependency in here might not be a good idea
56 // This variable is actually used in remote use, when rendering is not performed locally
57 private transient final Object treeLock = new Object();
59 private HashMap<Object, Integer> pending = new HashMap<Object, Integer>();
60 private HashMap<String, Object> globalProperties = new HashMap<String, Object>();
63 * For preventing duplicates on the nodesToRemove queue.
65 protected transient Set<INode> nodesToRemoveSet = new HashSet<INode>();
66 protected Deque<INode> nodesToRemove = new ArrayDeque<INode>();
68 private transient EventDelegator eventDelegator = new EventDelegator(this);
69 private transient NodeEventHandler eventHandler = new NodeEventHandler(this);
72 * The node that has input focus in the scene graph. The input node will
73 * receive key and command events.
75 private transient IG2DNode focusNode;
78 * The custom repaint manager of this scene graph.
80 private transient G2DRepaintManager repaintManager;
83 * Returns the event delegator, that is responsible for delegating events to nodes in the sg tree
85 * @return EventDelegator instance, always not null
87 public EventDelegator getEventDelegator() {
88 return eventDelegator;
92 * Returns the node event handler, that is responsible for delegating events
93 * to nodes in the sg tree.
95 * @return NodeEventHandler instance for this scene graph, always non-null
97 public NodeEventHandler getEventHandler() {
101 public void setFocusNode(IG2DNode focusNode) {
102 this.focusNode = focusNode;
105 public IG2DNode getFocusNode() {
110 public void render(Graphics2D g2d) {
112 Component rootPane = getRootPane();
113 if (rootPane != null)
114 g2d.setRenderingHint(G2DRenderingHints.KEY_COMPONENT, rootPane);
115 synchronized(treeLock) {
121 public void refresh() {
127 * Util method for executing updates to scenegraph tree
128 * NOTE: You should really consider performance issues when using this
130 * @param r Runnable to be executed while rendering is not performed
132 public void syncExec(Runnable r) {
133 synchronized(treeLock) {
139 * Set rootpane for swing components. This is used as parent for the components created by ComponentNode.
141 * @param rootPane Component that is used as a parent for the swing component (This shouldn't be visible)
143 public void setRootPane(JComponent rootPane) {
144 synchronized (RepaintManager.class) {
145 RepaintManager old = RepaintManager.currentManager(rootPane);
146 old = findProperRepaintManager(old);
147 this.repaintManager = new G2DRepaintManager(rootPane.getClass(), old);
148 RepaintManager.setCurrentManager(repaintManager);
150 this.rootPane = rootPane;
154 * Set rootpane for swing components. This is used as parent for the components created by ComponentNode.
155 * Supports separate component that is responsible for repainting the scenegraph.
157 * @param rootPane Component that is used as a parent for the swing component (This shouldn't be visible)
158 * @param paintContext Component that is responsible for repainting the scenegraph
160 public void setRootPane(Container rootPane, Component paintContext) {
161 synchronized (RepaintManager.class) {
162 RepaintManager old = RepaintManager.currentManager(paintContext);
163 old = findProperRepaintManager(old);
164 this.repaintManager = new G2DRepaintManager(paintContext.getClass(), old);
165 RepaintManager.setCurrentManager(repaintManager);
167 this.rootPane = rootPane;
170 private RepaintManager findProperRepaintManager(RepaintManager old) {
171 while (old instanceof G2DRepaintManager) {
172 G2DRepaintManager g2drm = (G2DRepaintManager) old;
173 old = g2drm.getDelegate();
178 public G2DRepaintManager getRepaintManager() {
179 return repaintManager;
183 * Put the node to the remove queue
186 public void asyncRemoveNode(INode node) {
187 synchronized(nodesToRemove) {
188 // Prevent nodes from winding up twice on the nodesToRemove queue
189 if (nodesToRemoveSet.add(node)) {
190 nodesToRemove.add(node); // This is performed when called inside the render
196 * Perform the actual removal of the nodes in the nodesToRemove list
198 public void performCleanup() {
199 synchronized(nodesToRemove) {
200 while(nodesToRemove.size() > 0) {
201 INode node = nodesToRemove.removeFirst();
202 ParentNode<?> parent = node.getParent();
203 // This works around issue #2071
205 parent.removeNode(node);
207 if (!nodesToRemoveSet.isEmpty())
208 nodesToRemoveSet.clear();
213 public void cleanup() {
215 nodesToRemove.clear();
216 nodesToRemove = null;
217 nodesToRemoveSet.clear();
218 nodesToRemoveSet = null;
219 eventHandler.dispose();
221 eventDelegator.dispose();
222 eventDelegator = null;
225 public Container getRootPane() {
226 return (Container) this.rootPane;
230 public String toString() {
231 return super.toString() + " [root pane=" + rootPane + "]";
235 public ParentNode<?> getRootNode() {
236 // This is a root node!
240 // ILookupService implementation
242 private final Object lookupLock = new Object();
243 private final Map<String, INode> toNode = new HashMap<String, INode>();
244 private final Map<INode, String> toId = new HashMap<INode, String>();
246 transient Logger logger = Logger.getLogger(getClass().getName());
249 public INode map(String id, INode node) {
251 throw new NullPointerException("null id");
253 throw new NullPointerException("null node");
257 synchronized (lookupLock) {
258 oldNode = toNode.put(id, node);
259 oldId = toId.put(node, id);
261 // Keep the mapping a consistent bijection:
262 // If ID => INode mapping is removed, the INode => ID mappings must
265 if (oldNode != null && !oldNode.equals(node)) {
266 String removedId = toId.remove(oldNode);
267 if (!id.equals(removedId))
268 toNode.remove(removedId);
270 if (oldId != null && !oldId.equals(id)) {
271 INode removedNode = toNode.remove(oldId);
272 if (removedNode != node)
273 toId.remove(removedNode);
276 if (logger.isLoggable(Level.FINER))
277 logger.fine("map(" + id + ", " + node + ")");
278 if (oldNode != null || oldId != null) {
279 if (logger.isLoggable(Level.FINE)) {
280 logger.info("replaced mappings for ID " + oldId + " and node " + oldNode);
287 public INode unmap(String id) {
290 synchronized (lookupLock) {
291 node = toNode.remove(id);
294 mappedId = toId.remove(node);
296 if (logger.isLoggable(Level.FINER))
297 logger.fine("unmap(" + id + "): " + node);
298 if (mappedId != null && !mappedId.equals(id)) {
299 if (logger.isLoggable(Level.WARNING))
300 logger.log(Level.WARNING, "mapping was out-of-sync: " + id + " => " + node + " & " + mappedId + " => " + node, new Exception("trace"));
306 public String unmap(INode node) {
309 synchronized (lookupLock) {
310 id = toId.remove(node);
313 mappedNode = toNode.remove(id);
315 if (logger.isLoggable(Level.FINER))
316 logger.fine("unmap(" + node + "): " + id);
317 if (mappedNode != null && node != mappedNode) {
318 if (logger.isLoggable(Level.WARNING))
319 logger.log(Level.WARNING, "mapping was out-of-sync: " + node + " => " + id + " & " + id + " => " + mappedNode, new Exception("trace"));
325 public INode lookupNode(String id) {
326 synchronized (lookupLock) {
327 return toNode.get(id);
332 public String lookupId(INode node) {
333 synchronized (lookupLock) {
334 return toId.get(node);
338 public boolean isPending() {
339 return !pending.isEmpty();
342 synchronized public void increasePending(Object object) {
343 Integer ref = pending.get(object);
344 if (ref == null) pending.put(object, 1);
345 else pending.put(object, ref+1);
348 synchronized public void setPending(Object object) {
349 pending.put(object, 1);
352 synchronized public void clearPending(Object object) {
353 pending.remove(object);
356 synchronized public void decreasePending(Object object) {
357 Integer ref = pending.get(object);
360 //throw new IllegalStateException("Ref count in unregister was 0 for " + object);
362 if (ref > 1) pending.put(object, ref-1);
363 else if (ref==1) pending.remove(object);
366 //throw new IllegalStateException("Ref count in unregister was 0 for " + object);
370 synchronized public void setGlobalProperty(String key, Object value) {
371 globalProperties.put(key, value);
374 @SuppressWarnings("unchecked")
375 synchronized public <T> T getGlobalProperty(String key, T defaultValue) {
376 T t = (T)globalProperties.get(key);
377 if(t == null) return defaultValue;