]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/pointertool/BoxSelectionMode.java
Sync git svn branch with SVN repository r33324.
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / participant / pointertool / BoxSelectionMode.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.diagram.participant.pointertool;\r
13 \r
14 import java.awt.BasicStroke;\r
15 import java.awt.Color;\r
16 import java.awt.geom.Point2D;\r
17 import java.awt.geom.Rectangle2D;\r
18 import java.util.HashSet;\r
19 import java.util.Set;\r
20 \r
21 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
22 import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;\r
23 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
24 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
25 import org.simantics.g2d.diagram.DiagramHints;\r
26 import org.simantics.g2d.diagram.handler.PickContext;\r
27 import org.simantics.g2d.diagram.handler.PickRequest;\r
28 import org.simantics.g2d.diagram.handler.PickRequest.PickPolicy;\r
29 import org.simantics.g2d.diagram.participant.Selection;\r
30 import org.simantics.g2d.element.IElement;\r
31 import org.simantics.g2d.participant.KeyUtil;\r
32 import org.simantics.g2d.participant.RenderingQualityInteractor;\r
33 import org.simantics.g2d.participant.TransformUtil;\r
34 import org.simantics.scenegraph.g2d.G2DParentNode;\r
35 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
36 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
37 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
38 import org.simantics.scenegraph.g2d.events.command.Commands;\r
39 import org.simantics.scenegraph.g2d.nodes.BoxSelectionNode;\r
40 import org.simantics.scenegraph.utils.Quality;\r
41 \r
42 /**\r
43  * @author Toni Kalajainen\r
44  */\r
45 public class BoxSelectionMode extends AbstractMode {\r
46 \r
47     @Reference  RenderingQualityInteractor quality;\r
48     @Dependency TransformUtil util;\r
49     @Dependency Selection selection;\r
50     @Dependency PickContext pickContext;\r
51     @Dependency KeyUtil keyUtil;\r
52 \r
53     public final static BasicStroke STROKE         = new BasicStroke(1.0f);\r
54 \r
55     public static final int         PAINT_PRIORITY = 30;\r
56 \r
57     Point2D                         startingPoint;\r
58     Point2D                         currentPoint;\r
59     int                             mouseButton;\r
60     PickPolicy                      boxSelectMode;\r
61     BoxSelectionNode                node;\r
62 \r
63     @SGInit\r
64     public void init(G2DParentNode parent) {\r
65         if (quality != null)\r
66             quality.setStaticQuality(Quality.LOW);\r
67 \r
68         node = parent.getOrCreateNode("" + hashCode(), BoxSelectionNode.class);\r
69         node.setZIndex(PAINT_PRIORITY);\r
70         node.setStart(startingPoint);\r
71         node.setEnd(currentPoint);\r
72         node.setStroke(STROKE);\r
73         node.setScaleStroke(true);\r
74         node.setColor(getToolColor());\r
75         node.setMouseButton(mouseButton);\r
76         node.setSelectionListener(new BoxSelectionNode.SelectionListener() {\r
77             @Override\r
78             public void onSelect(Rectangle2D rect, int modifiers) {\r
79                 // Disposed?\r
80                 if (isRemoved())\r
81                     return;\r
82 \r
83                 boolean toggle = (modifiers & MouseEvent.CTRL_MASK) != 0;\r
84                 boolean accumulate = (modifiers & MouseEvent.SHIFT_MASK) != 0;\r
85 \r
86                 Set<IElement> boxSelection = new HashSet<IElement>();\r
87                 PickRequest request = new PickRequest(rect);\r
88                 request.pickPolicy = boxSelectMode;\r
89                 pickContext.pick(diagram, request, boxSelection);\r
90 \r
91                 int selectionId = mouseId;\r
92                 if (toggle) {\r
93                     selection.toggle(selectionId, boxSelection);\r
94                 } else if (accumulate) {\r
95                     for (IElement elem : boxSelection)\r
96                         selection.add(selectionId, elem);\r
97                 } else {\r
98                     selection.setSelection(selectionId, boxSelection);\r
99                 }\r
100 \r
101                 if (node != null) {\r
102                     node.remove();\r
103                     node = null;\r
104                 }\r
105 \r
106                 setDirty();\r
107                 remove();\r
108             }\r
109         });\r
110 \r
111     }\r
112 \r
113     @SGCleanup\r
114     public void cleanup() {\r
115         if (quality != null)\r
116             quality.setStaticQuality(null);\r
117         if (node != null) {\r
118             node.remove();\r
119             node = null;\r
120         }\r
121     }\r
122 \r
123     public BoxSelectionMode(Point2D startingPoint, Point2D currentPoint, final int mouseId, final int mouseButton, final PickPolicy boxSelectMode) {\r
124         super(mouseId);\r
125         this.startingPoint = startingPoint;\r
126         this.currentPoint = currentPoint;\r
127         this.mouseButton = mouseButton;\r
128         this.boxSelectMode = boxSelectMode;\r
129         //setHint(RenderingQualityInteractor.KEY_QUALITY_INTERACTOR_ENABLED, Boolean.FALSE);\r
130     }\r
131 \r
132     public synchronized Color getToolColor() {\r
133         Color c = getHint(DiagramHints.KEY_SELECTION_FRAME_COLOR);\r
134         if (c != null)\r
135             return c;\r
136         return Color.BLACK;\r
137     }\r
138 \r
139     /**\r
140      * Allows user to cancel box selection operations by invoking the CANCEL\r
141      * command (pressing ESC).\r
142      */\r
143     @EventHandler(priority = 100)\r
144     public boolean handleCancel(CommandEvent e) {\r
145         if (e.command.equals( Commands.CANCEL ) ) {\r
146             setDirty();\r
147             remove();\r
148         }\r
149         return false;\r
150     }\r
151 \r
152 }