]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/canvas/CanvasPainter.java
Merge "Added Simantics/Diagram function positionToVector."
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / canvas / CanvasPainter.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.g2d.elementclass.canvas;
13
14 import java.awt.geom.Rectangle2D;
15
16 import org.simantics.g2d.canvas.ICanvasContext;
17 import org.simantics.g2d.element.ElementUtils;
18 import org.simantics.g2d.element.IElement;
19 import org.simantics.g2d.element.SceneGraphNodeKey;
20 import org.simantics.g2d.element.handler.SceneGraph;
21 import org.simantics.scenegraph.Node;
22 import org.simantics.scenegraph.g2d.G2DParentNode;
23 import org.simantics.scenegraph.g2d.nodes.ClippingNode;
24 import org.simantics.utils.datastructures.hints.IHintContext.Key;
25
26 /**
27  * An element handler that "paints" a canvas bound to an element.
28  * 
29  * Keys: CanvasClass.KEY_CANVAS
30  * 
31  * @author Toni Kalajainen
32  */
33 public class CanvasPainter implements SceneGraph {
34
35     private static final long         serialVersionUID = -182067379834789790L;
36
37     public static final CanvasPainter INSTANCE         = new CanvasPainter();
38
39     public static final Key           SG_NODE          = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
40
41     @Override
42     public void cleanup(IElement e) {
43         ElementUtils.removePossibleNode(e, SG_NODE);
44     }
45
46     @Override
47     public void init(IElement e, G2DParentNode parent) {
48         ICanvasContext ctx = e.getHint(CanvasClass.KEY_CANVAS);
49         if (ctx == null) {
50             ElementUtils.removePossibleNode(e, SG_NODE);
51             return;
52         }
53
54         ClippingNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, ClippingNode.class);
55
56         Rectangle2D bounds = ElementUtils.getElementBounds(e);
57         node.setClip(bounds);
58     }
59
60 }