+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.trend.impl;\r
+\r
+import java.awt.AlphaComposite;\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Composite;\r
+import java.awt.Graphics2D;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+\r
+public class SelectionNode extends G2DNode {\r
+ \r
+ private static final long serialVersionUID = -8803833805847402592L;\r
+\r
+ public static AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);\r
+ public static final Color COLOR = new Color(.5f, .5f, 1.f);\r
+ public static final BasicStroke STROKE =\r
+ new BasicStroke(1.f,\r
+ BasicStroke.CAP_SQUARE,\r
+ BasicStroke.CAP_SQUARE,\r
+ 10.0f, null, 0.0f);\r
+ \r
+ double x1, x2, y1, y2;\r
+ Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, 1, 1);\r
+ Rectangle2D.Double rect2 = new Rectangle2D.Double(0, 0, 1, 1);\r
+ boolean binaryZoom = false, timeZoom = false;\r
+ \r
+ public void start(Point2D pt, boolean binaryZoom, boolean timeZoom)\r
+ {\r
+ x1 = x2 = pt.getX();\r
+ y1 = y2 = pt.getY();\r
+ rect.setFrame(x1, y1, 0, 0); \r
+ rect2.setFrame(x1, y1, 0, 0);\r
+ this.binaryZoom = binaryZoom;\r
+ this.timeZoom = timeZoom;\r
+ }\r
+ \r
+ public void setEndPoint(Point2D pt)\r
+ {\r
+ x2 = pt.getX();\r
+ y2 = pt.getY();\r
+ layout();\r
+ }\r
+ \r
+ public void layout() {\r
+ TrendNode trend = (TrendNode) getParent();\r
+ Plot plot = trend.plot;\r
+ \r
+ if (binaryZoom) {\r
+ // Binary Zoom\r
+ double px = plot.getX();\r
+ double py = plot.getY()+plot.analogAreaHeight;\r
+ double pw = plot.getWidth();\r
+ double ph = plot.binaryAreaHeight;\r
+ \r
+ rect2.setFrame( Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2) ); \r
+ \r
+ double _x1 = Math.min( Math.max( x1, px ), px+pw);\r
+ double _x2 = Math.min( Math.max( x2, px ), px+pw);\r
+ \r
+ rect.setFrame( Math.min(_x1, _x2), py, Math.abs(_x1-_x2), ph ); \r
+ } else {\r
+ // Analog Zoom\r
+ double px = plot.getX();\r
+ double py = plot.getY();\r
+ double pw = plot.getWidth();\r
+ double ph = plot.analogAreaHeight;\r
+ \r
+ rect2.setFrame( Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2) ); \r
+ \r
+ double _x1 = Math.min( Math.max( x1, px ), px+pw);\r
+ double _x2 = Math.min( Math.max( x2, px ), px+pw);\r
+ double _y1 = Math.min( Math.max( y1, py ), py+ph);\r
+ double _y2 = Math.min( Math.max( y2, py ), py+ph);\r
+ \r
+ if (timeZoom) {\r
+ rect.setFrame( Math.min(_x1, _x2), py, Math.abs(_x1-_x2), plot.analogAreaHeight ); \r
+ } else {\r
+ rect.setFrame( Math.min(_x1, _x2), Math.min(_y1, _y2), Math.abs(_x1-_x2), Math.abs(_y1-_y2) ); \r
+ }\r
+ } \r
+ \r
+ }\r
+\r
+ @Override\r
+ public void render(Graphics2D g2d) { \r
+ Composite c = g2d.getComposite();\r
+ g2d.setComposite( composite );\r
+ g2d.setColor( COLOR );\r
+ g2d.fill( rect );\r
+ g2d.setComposite( c );\r
+ g2d.setStroke( STROKE );\r
+ g2d.draw( rect );\r
+ }\r
+ \r
+ @Override\r
+ public Rectangle2D getBoundsInLocal() { \r
+ return rect;\r
+ }\r
+\r
+}\r