]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/ClippingNode.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / ClippingNode.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.Shape;
16 import java.awt.geom.Rectangle2D;
17
18 import org.simantics.scenegraph.g2d.G2DParentNode;
19
20 /**
21  * @author Tuukka Lehtonen
22  */
23 public class ClippingNode extends G2DParentNode {
24
25     private static final long serialVersionUID = -1064240485806430485L;
26
27     protected Shape clipShape;
28
29     @SyncField({"clipShape"})
30     public void setClip(Shape shape) {
31         this.clipShape = shape;
32     }
33
34     public Shape getClip() {
35         return clipShape;
36     }
37
38     @Override
39     public void render(Graphics2D g2d) {
40         Shape prevClip = g2d.getClip();
41         try {
42             g2d.setClip(clipShape);
43             super.render(g2d);
44         } finally {
45             g2d.setClip(prevClip);
46         }
47     }
48
49     @Override
50     public String toString() {
51         return super.toString() + "[clip shape=" + clipShape + "]";
52     }
53
54     @Override
55     public Rectangle2D getBoundsInLocal() {
56         if (clipShape == null)
57             return null;
58        return clipShape.getBounds2D();
59     }
60
61 }