-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in 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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
package org.simantics.scenegraph.g2d.nodes;
-\r
-import java.awt.BasicStroke;\r
-import java.awt.Color;\r
-import java.awt.Graphics2D;\r
-import java.awt.Stroke;\r
-import java.awt.geom.Point2D;\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import org.simantics.scenegraph.g2d.G2DNode;\r
-import org.simantics.scenegraph.g2d.events.EventTypes;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
-import org.simantics.scenegraph.utils.GeometryUtils;\r
-import org.simantics.scenegraph.utils.NodeUtil;\r
-
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Stroke;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+
+import org.simantics.scenegraph.g2d.G2DNode;
+import org.simantics.scenegraph.g2d.events.EventTypes;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
+import org.simantics.scenegraph.utils.GeometryUtils;
+import org.simantics.scenegraph.utils.NodeUtil;
+
+/**
+ * @author Tuukka Lehtonen
+ */
public class BoxSelectionNode extends G2DNode {
private static final long serialVersionUID = 8508750881358776559L;
- protected Stroke stroke = new BasicStroke(2);\r
- protected Color color = Color.BLACK;\r
- protected boolean scaleStroke = false;\r
- protected int mouseButton = 0;\r
+ protected Stroke stroke = new BasicStroke(2);
+ protected Color color = Color.BLACK;
+ protected boolean scaleStroke = false;
+ protected int mouseButton = 0;
+
+ protected Point2D start = new Point2D.Double(0, 0);
+ protected Point2D end = new Point2D.Double(0, 0);
+
+ protected SelectionListener listener = null;
- protected Point2D start = new Point2D.Double(0, 0);\r
- protected Point2D end = new Point2D.Double(0, 0);\r
-\r
- protected SelectionListener listener = null;\r
-\r
public static interface SelectionListener {
public void onSelect(Rectangle2D rect, int modifiers);
}
-\r
- @Override\r
- public void init() {\r
- super.init();\r
- addEventHandler(this);\r
- }\r
-\r
- @Override\r
- public void cleanup() {\r
- removeEventHandler(this);\r
- super.cleanup();\r
- }\r
+
+ @Override
+ public void init() {
+ super.init();
+ addEventHandler(this);
+ }
+
+ @Override
+ public void cleanup() {
+ removeEventHandler(this);
+ super.cleanup();
+ }
@SyncField("stroke")
public void setStroke(Stroke stroke) {
public void setEnd(Point2D end) {
this.end = end;
}
-\r
+
@SyncField("mouseButton")
- public void setMouseButton(int mouseButton) {\r
- this.mouseButton = mouseButton;\r
- }\r
-\r
+ public void setMouseButton(int mouseButton) {
+ this.mouseButton = mouseButton;
+ }
+
@Override
public void render(Graphics2D g2d) {
- Rectangle2D bounds = getBoundsInLocal();\r
- if (bounds == null || bounds.isEmpty())\r
- return;\r
-\r
- if (color != null)\r
- g2d.setColor(color);\r
- if (stroke != null) {\r
- if (scaleStroke && stroke instanceof BasicStroke) {\r
+ Rectangle2D bounds = getBoundsInLocal();
+ if (bounds == null || bounds.isEmpty())
+ return;
+
+ if (color != null)
+ g2d.setColor(color);
+ if (stroke != null) {
+ if (scaleStroke && stroke instanceof BasicStroke) {
BasicStroke bs = GeometryUtils.scaleStroke(stroke, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
g2d.setStroke(bs);
} else {
g2d.setStroke(stroke);
}
}
-\r
+
g2d.draw(bounds);
}
@ServerSide
private void onSelect(Rectangle2D selection, int modifiers) {
- if (listener != null)\r
- listener.onSelect(selection, modifiers);\r
+ if (listener != null)
+ listener.onSelect(selection, modifiers);
}
@Override
- public Rectangle2D getBoundsInLocal() {\r
- // Empty bounds indicate no rendering.\r
- if (start == null || end == null)\r
- return new Rectangle2D.Double();\r
+ public Rectangle2D getBoundsInLocal() {
+ // Empty bounds indicate no rendering.
+ if (start == null || end == null)
+ return new Rectangle2D.Double();
Rectangle2D rect = new Rectangle2D.Double(Math.min(start.getX(), end.getX()),
Math.min(start.getY(), end.getY()),
Math.max(start.getX(), end.getX())-Math.min(start.getX(), end.getX()),
- Math.max(start.getY(), end.getY())-Math.min(start.getY(), end.getY()));\r
+ Math.max(start.getY(), end.getY())-Math.min(start.getY(), end.getY()));
return rect;
- }\r
-\r
- @Override\r
- public int getEventMask() {\r
- return EventTypes.MouseButtonReleasedMask | EventTypes.MouseDragBeginMask\r
- | EventTypes.MouseMovedMask;\r
- }\r
-\r
- @Override\r
- protected boolean mouseButtonReleased(MouseButtonReleasedEvent e) {\r
- if (e.button == mouseButton) {\r
- onSelect(getBoundsInLocal(), e.stateMask);\r
- start = null;\r
- end = null;\r
- }\r
- return false;\r
- }\r
-\r
- @Override\r
- protected boolean mouseMoved(MouseMovedEvent e) {\r
- if (end != null) {\r
- NodeUtil.worldToLocal(this, e.controlPosition, end);\r
- repaint();\r
- }\r
- return false;\r
- }\r
-\r
+ }
+
+ @Override
+ public int getEventMask() {
+ return EventTypes.MouseButtonReleasedMask | EventTypes.MouseDragBeginMask
+ | EventTypes.MouseMovedMask;
+ }
+
+ @Override
+ protected boolean mouseButtonReleased(MouseButtonReleasedEvent e) {
+ if (e.button == mouseButton) {
+ onSelect(getBoundsInLocal(), e.stateMask);
+ start = null;
+ end = null;
+ }
+ return false;
+ }
+
+ @Override
+ protected boolean mouseMoved(MouseMovedEvent e) {
+ if (end != null) {
+ NodeUtil.worldToLocal(this, e.controlPosition, end);
+ repaint();
+ }
+ return false;
+ }
+
}