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