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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.scenegraph.g2d.nodes;
\r
14 import java.awt.BasicStroke;
\r
15 import java.awt.Color;
\r
16 import java.awt.Graphics2D;
\r
17 import java.awt.geom.AffineTransform;
\r
18 import java.awt.geom.Rectangle2D;
\r
20 import org.simantics.scenegraph.g2d.G2DNode;
\r
21 import org.simantics.scenegraph.utils.GeometryUtils;
\r
23 public class SelectionNode extends G2DNode implements Decoration {
\r
27 private static final long serialVersionUID = -2879575230419873230L;
\r
29 public transient static final BasicStroke SELECTION_STROKE = new BasicStroke(1.0f,
\r
30 BasicStroke.CAP_SQUARE, BasicStroke.CAP_SQUARE, 10.0f,
\r
31 new float[] { 5.0f, 5.0f }, 0.0f);
\r
33 protected Rectangle2D bounds = null;
\r
34 protected Color color = null;
\r
35 protected transient Rectangle2D rect;
\r
36 protected transient BasicStroke scaledStroke;
\r
37 protected transient double previousScaleRecip = Double.NaN;
\r
39 @SyncField({"transform", "bounds", "color"})
\r
40 public void init(AffineTransform transform, Rectangle2D bounds, Color color) {
\r
41 this.transform = transform;
\r
42 this.bounds = bounds;
\r
47 public void render(Graphics2D g) {
\r
48 if (bounds == null) return;
\r
50 // Prevent exceptions during rendering.
\r
51 if (transform.getDeterminant() == 0)
\r
54 AffineTransform ot = g.getTransform();
\r
57 g.transform(transform);
\r
59 AffineTransform tx = g.getTransform();
\r
60 //System.out.println("tx: " + tx);
\r
61 double scale = GeometryUtils.getScale(tx);
\r
62 //System.out.println("scale: " + scale);
\r
63 double scaleRecip = 1.0 / scale;
\r
64 //System.out.println("scale: " + scaleRecip);
\r
66 // Prevent stroke reallocation while panning.
\r
67 // Zooming will trigger reallocation.
\r
68 if (scaledStroke == null || scaleRecip != previousScaleRecip) {
\r
69 scaledStroke = GeometryUtils.scaleStroke( SELECTION_STROKE, (float) scaleRecip);
\r
70 previousScaleRecip = scaleRecip;
\r
72 g.setStroke(scaledStroke);
\r
74 double padding = 5.0 * scaleRecip;
\r
75 double paddingX = padding;
\r
76 double paddingY = padding;
\r
79 rect = new Rectangle2D.Double();
\r
80 rect.setFrame(bounds.getMinX() - paddingX, bounds.getMinY() - paddingY,
\r
81 bounds.getWidth() + 2.0*paddingX, bounds.getHeight() + 2.0*paddingY);
\r
89 public Rectangle2D getBoundsInLocal() {
\r