]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/pointertool/BoxSelectionMode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / participant / pointertool / BoxSelectionMode.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/pointertool/BoxSelectionMode.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/pointertool/BoxSelectionMode.java
new file mode 100644 (file)
index 0000000..8e47498
--- /dev/null
@@ -0,0 +1,152 @@
+/*******************************************************************************\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
+package org.simantics.g2d.diagram.participant.pointertool;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.handler.PickContext;\r
+import org.simantics.g2d.diagram.handler.PickRequest;\r
+import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.participant.KeyUtil;\r
+import org.simantics.g2d.participant.RenderingQualityInteractor;\r
+import org.simantics.g2d.participant.TransformUtil;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+import org.simantics.scenegraph.g2d.nodes.BoxSelectionNode;\r
+import org.simantics.scenegraph.utils.Quality;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ */\r
+public class BoxSelectionMode extends AbstractMode {\r
+\r
+    @Reference  RenderingQualityInteractor quality;\r
+    @Dependency TransformUtil util;\r
+    @Dependency Selection selection;\r
+    @Dependency PickContext pickContext;\r
+    @Dependency KeyUtil keyUtil;\r
+\r
+    public final static BasicStroke STROKE         = new BasicStroke(1.0f);\r
+\r
+    public static final int         PAINT_PRIORITY = 30;\r
+\r
+    Point2D                         startingPoint;\r
+    Point2D                         currentPoint;\r
+    int                             mouseButton;\r
+    PickPolicy                      boxSelectMode;\r
+    BoxSelectionNode                node;\r
+\r
+    @SGInit\r
+    public void init(G2DParentNode parent) {\r
+        if (quality != null)\r
+            quality.setStaticQuality(Quality.LOW);\r
+\r
+        node = parent.getOrCreateNode("" + hashCode(), BoxSelectionNode.class);\r
+        node.setZIndex(PAINT_PRIORITY);\r
+        node.setStart(startingPoint);\r
+        node.setEnd(currentPoint);\r
+        node.setStroke(STROKE);\r
+        node.setScaleStroke(true);\r
+        node.setColor(getToolColor());\r
+        node.setMouseButton(mouseButton);\r
+        node.setSelectionListener(new BoxSelectionNode.SelectionListener() {\r
+            @Override\r
+            public void onSelect(Rectangle2D rect, int modifiers) {\r
+                // Disposed?\r
+                if (isRemoved())\r
+                    return;\r
+\r
+                boolean toggle = (modifiers & MouseEvent.CTRL_MASK) != 0;\r
+                boolean accumulate = (modifiers & MouseEvent.SHIFT_MASK) != 0;\r
+\r
+                Set<IElement> boxSelection = new HashSet<IElement>();\r
+                PickRequest request = new PickRequest(rect);\r
+                request.pickPolicy = boxSelectMode;\r
+                pickContext.pick(diagram, request, boxSelection);\r
+\r
+                int selectionId = mouseId;\r
+                if (toggle) {\r
+                    selection.toggle(selectionId, boxSelection);\r
+                } else if (accumulate) {\r
+                    for (IElement elem : boxSelection)\r
+                        selection.add(selectionId, elem);\r
+                } else {\r
+                    selection.setSelection(selectionId, boxSelection);\r
+                }\r
+\r
+                if (node != null) {\r
+                    node.remove();\r
+                    node = null;\r
+                }\r
+\r
+                setDirty();\r
+                remove();\r
+            }\r
+        });\r
+\r
+    }\r
+\r
+    @SGCleanup\r
+    public void cleanup() {\r
+        if (quality != null)\r
+            quality.setStaticQuality(null);\r
+        if (node != null) {\r
+            node.remove();\r
+            node = null;\r
+        }\r
+    }\r
+\r
+    public BoxSelectionMode(Point2D startingPoint, Point2D currentPoint, final int mouseId, final int mouseButton, final PickPolicy boxSelectMode) {\r
+        super(mouseId);\r
+        this.startingPoint = startingPoint;\r
+        this.currentPoint = currentPoint;\r
+        this.mouseButton = mouseButton;\r
+        this.boxSelectMode = boxSelectMode;\r
+        //setHint(RenderingQualityInteractor.KEY_QUALITY_INTERACTOR_ENABLED, Boolean.FALSE);\r
+    }\r
+\r
+    public synchronized Color getToolColor() {\r
+        Color c = getHint(DiagramHints.KEY_SELECTION_FRAME_COLOR);\r
+        if (c != null)\r
+            return c;\r
+        return Color.BLACK;\r
+    }\r
+\r
+    /**\r
+     * Allows user to cancel box selection operations by invoking the CANCEL\r
+     * command (pressing ESC).\r
+     */\r
+    @EventHandler(priority = 100)\r
+    public boolean handleCancel(CommandEvent e) {\r
+        if (e.command.equals( Commands.CANCEL ) ) {\r
+            setDirty();\r
+            remove();\r
+        }\r
+        return false;\r
+    }\r
+\r
+}
\ No newline at end of file