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.nodes;
14 import java.awt.Graphics2D;
15 import java.awt.geom.AffineTransform;
16 import java.awt.geom.Rectangle2D;
18 import org.simantics.scenegraph.ILookupService;
19 import org.simantics.scenegraph.g2d.IG2DNode;
22 * A node that delegates {@link #render(Graphics2D)} and
23 * {@link #getBoundsInLocal()} methods to a delegate {@link IG2DNode} it
27 * <b>CAUTION:</b> <em>this node must be used with extreme care</em>! It can be
28 * used to generate cyclic scene graphs which may cause rendering to crash due
29 * to infinite recursion and in any case rendering will not work as intended.
30 * E.g. a scene graph could have a {@link NavigationNode} under its root node
31 * and under which a <code>LinkNode</code> could link back to the navigation
32 * node, which would cause everything to be rendered twice and with double
33 * transformations. As a safety measure against cyclic cases, this node contains
34 * state that prevents it from being invoked recursively.
37 * <b>CAVEAT 1:</b> Nodes with internal state that is updated during rendering.
38 * Such nodes should not be used with this.
41 * <b>CAVEAT 2:</b> Only intended for local rendering. Will not work with remote
42 * rendering. For remote support, use {@link ILookupService} and
43 * {@link LinkNode} instead.
45 * @author Tuukka Lehtonen
50 public class LocalDelegateNode extends StateMaskNode {
52 private static final long serialVersionUID = -7465071303188585400L;
62 public void setDelegate(IG2DNode delegate) {
63 this.delegate = delegate;
66 public IG2DNode getDelegate() {
71 public void render(Graphics2D g2d) {
72 // Safety against cyclic cases.
73 if (hasFlags(IN_RENDER))
80 AffineTransform oldTransform = null;
81 if (transform != null && !transform.isIdentity()) {
82 g2d.transform(transform);
83 oldTransform = g2d.getTransform();
88 if (oldTransform != null)
89 g2d.setTransform(oldTransform);
90 clearFlags(IN_RENDER);
95 public Rectangle2D getBoundsInLocal() {
96 // Safety against cyclic cases.
97 if (hasFlags(IN_GET_BOUNDS))
98 return new Rectangle2D.Double();
100 if (delegate == null)
101 return new Rectangle2D.Double();
103 setFlags(IN_GET_BOUNDS);
105 Rectangle2D bounds = delegate.getBoundsInLocal();
106 if (transform != null && !transform.isIdentity())
107 bounds = transform.createTransformedShape(bounds).getBounds2D();
110 clearFlags(IN_GET_BOUNDS);