1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
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;
22 import org.simantics.scenegraph.g2d.G2DNode;
23 import org.simantics.scenegraph.utils.GeometryUtils;
26 * @author Tuukka Lehtonen
28 public class PageBorderNode extends G2DNode {
30 private static final long serialVersionUID = -5969105979396122487L;
32 private static final Paint MARGIN_COLOR = new Color(192, 128, 128);
34 protected Rectangle2D border = null;
35 protected Rectangle2D margin = null;
36 protected Boolean enabled = null;
38 @SyncField( { "border", "margin", "enabled" })
39 public void init(Rectangle2D border, Rectangle2D margin, Boolean enabled) {
40 //System.out.println("PageBorderNode.init(" + border + ", " + margin + ", " + enabled);
43 this.enabled = enabled;
47 public void setEnabled(Boolean enabled) {
48 this.enabled = enabled;
52 public Rectangle2D getBounds() {
61 public void render(Graphics2D g) {
62 if (Boolean.FALSE.equals(enabled))
65 boolean borderPaintable = isPaintable(border);
66 boolean marginPaintable = isPaintable(margin);
67 if (!borderPaintable && !marginPaintable)
70 double scaleRecip = 1 / GeometryUtils.getScale(g.getTransform());
71 Stroke s = new BasicStroke((float) (1 * scaleRecip));
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);
80 if (marginPaintable && (!borderPainted || !margin.equals(border))) {
81 paintRectangle(g, scaleRecip, margin, s, MARGIN_COLOR, null, null);
85 boolean isPaintable(Rectangle2D r) {
86 return r != null && !r.isEmpty() && r.getWidth() != Double.POSITIVE_INFINITY
87 && r.getHeight() != Double.POSITIVE_INFINITY;
90 void paintRectangle(Graphics2D g, double scaleRecip, Rectangle2D r, Stroke stroke, Paint strokePaint,
91 Stroke shadowStroke, Paint shadowPaint) {
92 g.setPaint(strokePaint);
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));
112 public Rectangle2D getBoundsInLocal() {
117 public String toString() {
118 return super.toString() + " [enabled=" + enabled + ", border=" + border + ", margin=" + margin + "]";