]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/PageBorderNode.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / PageBorderNode.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.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Graphics2D;
17 import java.awt.Paint;
18 import java.awt.Stroke;
19 import java.awt.geom.Line2D;
20 import java.awt.geom.Rectangle2D;
21
22 import org.simantics.scenegraph.g2d.G2DNode;
23 import org.simantics.scenegraph.utils.GeometryUtils;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class PageBorderNode extends G2DNode {
29
30     private static final long  serialVersionUID = -5969105979396122487L;
31
32     private static final Paint MARGIN_COLOR     = new Color(192, 128, 128);
33
34     protected Rectangle2D      border           = null;
35     protected Rectangle2D      margin           = null;
36     protected Boolean          enabled          = null;
37
38     @SyncField( { "border", "margin", "enabled" })
39     public void init(Rectangle2D border, Rectangle2D margin, Boolean enabled) {
40         //System.out.println("PageBorderNode.init(" + border + ", " + margin + ", " + enabled);
41         this.border = border;
42         this.margin = margin;
43         this.enabled = enabled;
44     }
45
46     @SyncField("enabled")
47     public void setEnabled(Boolean enabled) {
48         this.enabled = enabled;
49     }
50
51     @Override
52     public Rectangle2D getBounds() {
53         if (border != null)
54             return border;
55         if (margin != null)
56             return margin;
57         return null;
58     }
59
60     @Override
61     public void render(Graphics2D g) {
62         if (Boolean.FALSE.equals(enabled))
63             return;
64
65         boolean borderPaintable = isPaintable(border);
66         boolean marginPaintable = isPaintable(margin);
67         if (!borderPaintable && !marginPaintable)
68             return;
69
70         double scaleRecip = 1 / GeometryUtils.getScale(g.getTransform());
71         Stroke s = new BasicStroke((float) (1 * scaleRecip));
72
73         boolean borderPainted = false;
74         if (borderPaintable) {
75             Stroke ss = new BasicStroke((float) (2 * scaleRecip));
76             paintRectangle(g, scaleRecip, border, s, Color.BLACK, ss, Color.GRAY);
77             borderPainted = true;
78         }
79
80         if (marginPaintable && (!borderPainted || !margin.equals(border))) {
81             paintRectangle(g, scaleRecip, margin, s, MARGIN_COLOR, null, null);
82         }
83     }
84
85     boolean isPaintable(Rectangle2D r) {
86         return r != null && !r.isEmpty() && r.getWidth() != Double.POSITIVE_INFINITY
87         && r.getHeight() != Double.POSITIVE_INFINITY;
88     }
89
90     void paintRectangle(Graphics2D g, double scaleRecip, Rectangle2D r, Stroke stroke, Paint strokePaint,
91             Stroke shadowStroke, Paint shadowPaint) {
92         g.setPaint(strokePaint);
93         g.setStroke(stroke);
94         g.draw(r);
95
96         if (shadowStroke != null) {
97             g.setPaint(shadowPaint);
98             g.setStroke(shadowStroke);
99             double offset = 2 * scaleRecip;
100             double w = r.getWidth();
101             double h = r.getHeight();
102             double x = r.getMaxX() + offset;
103             double x1 = r.getMinX() + offset * 2;
104             double y1 = r.getMinY() + offset * 2;
105             g.draw(new Line2D.Double(x, y1, x, y1 + h - offset));
106             double y = r.getMaxY() + offset;
107             g.draw(new Line2D.Double(x1, y, x1 + w - offset, y));
108         }
109     }
110
111     @Override
112     public Rectangle2D getBoundsInLocal() {
113         return border;
114     }
115
116     @Override
117     public String toString() {
118         return super.toString() + " [enabled=" + enabled + ", border=" + border + ", margin=" + margin + "]";
119     }
120
121 }