]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/BoundsNode.java
Configurable connection crossing styles
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / BoundsNode.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.nodes;
13
14 import java.awt.Graphics2D;
15 import java.awt.Rectangle;
16 import java.awt.geom.Rectangle2D;
17
18 import org.simantics.scenegraph.g2d.G2DNode;
19 import org.simantics.scenegraph.g2d.G2DRenderingHints;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public class BoundsNode extends G2DNode {
25
26     public interface ResizeListener {
27         void controlResized(Rectangle2D bounds);
28     }
29
30     private static final long serialVersionUID = -3215912248617879834L;
31
32     private Rectangle2D       controlbounds    = new Rectangle();
33
34     @SyncField("controlbounds")
35     protected void setControlBounds(Rectangle2D r) {
36         this.controlbounds = r.getBounds2D();
37     }
38
39     @Override
40     public Rectangle2D getBoundsInLocal() {
41         // This node performs no real rendering but returning null
42         // guarantees that its render method will get called.
43         return null;
44     }
45
46     public Rectangle2D getControlBounds() {
47         return controlbounds;
48     }
49
50     @Override
51     public void render(Graphics2D g2d) {
52         Rectangle2D r = (Rectangle2D) g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS);
53         if (r != null) {
54             if (!r.equals(controlbounds)) {
55                 //System.out.println("new control bounds: " + r);
56                 setControlBounds(r);
57                 controlResized(r);
58             }
59         }
60     }
61
62     @Override
63     public String toString() {
64         return super.toString() + " [controlbounds=" + controlbounds + "]";
65     }
66
67     private ResizeListener resizeListener = null;
68
69     public void setResizeListener(ResizeListener listener) {
70         this.resizeListener = listener;
71     }
72
73     @ServerSide
74     public void controlResized(Rectangle2D bounds) {
75         if (resizeListener != null && bounds != null)
76             resizeListener.controlResized(bounds);
77     }
78
79 }