]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/chassis/ImageChassis.java
Testing SonarQube with Simantics Platform SDK
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / chassis / ImageChassis.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.g2d.chassis;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Graphics2D;\r
16 import java.awt.Image;\r
17 import java.awt.geom.Point2D;\r
18 import java.awt.geom.Rectangle2D;\r
19 \r
20 import org.simantics.g2d.canvas.ICanvasContext;\r
21 import org.simantics.g2d.canvas.IContentContext;\r
22 import org.simantics.g2d.canvas.IContentContext.IContentListener;\r
23 import org.simantics.scenegraph.g2d.G2DRenderingHints;\r
24 \r
25 /**\r
26  * Renders canvas to an image.\r
27  * \r
28  * @author Toni Kalajainen\r
29  */\r
30 public class ImageChassis extends AbstractChassis {\r
31 \r
32     public static final Color WIPE_COLOR = new Color(.0f, 0.f, 0.f, .0f);\r
33 \r
34     private Color backgroundColor;\r
35     \r
36     Image image;\r
37 \r
38     boolean dirty = false;\r
39 \r
40     public ImageChassis(Image image) {\r
41         this.image = image;\r
42     }\r
43     \r
44     public void setBackgroundColor(Color color) {\r
45         backgroundColor = color;\r
46     }\r
47 \r
48     public Point2D getSize() {\r
49         return new Point2D.Double(image.getWidth(null), image.getHeight(null));\r
50     }\r
51 \r
52     public Rectangle2D getBounds() {\r
53         return new Rectangle2D.Double(0,0,image.getWidth(null), image.getHeight(null));\r
54     }\r
55 \r
56     @Override\r
57     public void setCanvasContext(ICanvasContext canvasContext) {\r
58         super.setCanvasContext(canvasContext);\r
59 \r
60         canvasContext.getContentContext().addPaintableContextListener(new IContentListener() {\r
61             @Override\r
62             public void onDirty(IContentContext sender) {\r
63                 if (dirty) return;\r
64                 dirty = true;\r
65                 draw();\r
66             }});\r
67 \r
68         draw();\r
69     }\r
70 \r
71     public void draw() {\r
72         if (canvasContext.isLocked())\r
73             throw new IllegalStateException("cannot draw image, canvas context is locked: " + canvasContext);\r
74 \r
75         dirty = false;\r
76         Graphics2D g2d = (Graphics2D) image.getGraphics();\r
77         try {\r
78             g2d.setRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS, getBounds());\r
79                 if(backgroundColor != null) {\r
80                         Rectangle2D bounds = getBounds();\r
81                     g2d.setBackground(backgroundColor);\r
82                     g2d.clearRect(0, 0, (int)bounds.getWidth(), (int)bounds.getHeight() );\r
83                 }\r
84             canvasContext.getSceneGraph().render(g2d);\r
85         } finally {\r
86             g2d.dispose();\r
87         }\r
88     }\r
89 \r
90 }\r