Fix SCL SceneGraph is omitting graphical elements
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / BackgroundNode.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.Color;
15 import java.awt.Graphics2D;
16 import java.awt.Paint;
17 import java.awt.geom.Rectangle2D;
18
19 import org.simantics.scenegraph.g2d.G2DNode;
20
21 public class BackgroundNode extends G2DNode {
22     /**
23      * 
24      */
25     private static final long serialVersionUID = 2805713128639560702L;
26
27     protected Color color = null;
28     protected Paint paint = null;
29     protected Rectangle2D bounds = null;
30
31     @SyncField("color")
32     public void setColor(Color color) {
33         this.color = color;
34     }
35
36     @SyncField("paint")
37     public void setPaint(Paint paint) {
38         this.paint = paint;
39     }
40
41     @SyncField("bounds")
42     public void setBounds(Rectangle2D bounds) {
43         this.bounds = bounds;
44     }
45
46     @Override
47     public void render(Graphics2D g) {
48         Rectangle2D drawBounds = bounds;
49         if(drawBounds == null) {
50                 if (g.getClipBounds() != null)
51                         drawBounds = (Rectangle2D)g.getClipBounds().clone(); // Fill whole clip area if bounds is not defined
52                 else
53                         return;
54         }
55         
56         if (color!=null) {
57             g.setColor(color);
58             g.fill(drawBounds);
59         } else
60             if (paint!=null) {
61                 g.setPaint(paint);
62                 g.fill(drawBounds);
63             }
64     }
65
66     @Override
67     public String toString() {
68         return super.toString() + " [color=" + color + "]";
69     }
70
71         @Override
72         public Rectangle2D getBoundsInLocal() {
73                 return bounds;
74         }
75 }