]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/BoundsNode.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / BoundsNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.g2d.nodes;\r
13 \r
14 import java.awt.Graphics2D;\r
15 import java.awt.Rectangle;\r
16 import java.awt.geom.Rectangle2D;\r
17 \r
18 import org.simantics.scenegraph.g2d.G2DNode;\r
19 import org.simantics.scenegraph.g2d.G2DRenderingHints;\r
20 \r
21 /**\r
22  * @author Tuukka Lehtonen\r
23  */\r
24 public class BoundsNode extends G2DNode {\r
25 \r
26     public interface ResizeListener {\r
27         void controlResized(Rectangle2D bounds);\r
28     }\r
29 \r
30     private static final long serialVersionUID = -3215912248617879834L;\r
31 \r
32     private Rectangle2D       controlbounds    = new Rectangle();\r
33 \r
34     @SyncField("controlbounds")\r
35     protected void setControlBounds(Rectangle2D r) {\r
36         this.controlbounds = r.getBounds2D();\r
37     }\r
38 \r
39     @Override\r
40     public Rectangle2D getBoundsInLocal() {\r
41         // This node performs no real rendering but returning null\r
42         // guarantees that its render method will get called.\r
43         return null;\r
44     }\r
45 \r
46     public Rectangle2D getControlBounds() {\r
47         return controlbounds;\r
48     }\r
49 \r
50     @Override\r
51     public void render(Graphics2D g2d) {\r
52         Rectangle2D r = (Rectangle2D) g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS);\r
53         if (r != null) {\r
54             if (!r.equals(controlbounds)) {\r
55                 //System.out.println("new control bounds: " + r);\r
56                 setControlBounds(r);\r
57                 controlResized(r);\r
58             }\r
59         }\r
60     }\r
61 \r
62     @Override\r
63     public String toString() {\r
64         return super.toString() + " [controlbounds=" + controlbounds + "]";\r
65     }\r
66 \r
67     private ResizeListener resizeListener = null;\r
68 \r
69     public void setResizeListener(ResizeListener listener) {\r
70         this.resizeListener = listener;\r
71     }\r
72 \r
73     @ServerSide\r
74     public void controlResized(Rectangle2D bounds) {\r
75         if (resizeListener != null && bounds != null)\r
76             resizeListener.controlResized(bounds);\r
77     }\r
78 \r
79 }