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.g2d.diagram.participant.pointertool;
\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
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
43 * @author Toni Kalajainen
\r
45 public class BoxSelectionMode extends AbstractMode {
\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
53 public final static BasicStroke STROKE = new BasicStroke(1.0f);
\r
55 public static final int PAINT_PRIORITY = 30;
\r
57 Point2D startingPoint;
\r
58 Point2D currentPoint;
\r
60 PickPolicy boxSelectMode;
\r
61 BoxSelectionNode node;
\r
64 public void init(G2DParentNode parent) {
\r
65 if (quality != null)
\r
66 quality.setStaticQuality(Quality.LOW);
\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
78 public void onSelect(Rectangle2D rect, int modifiers) {
\r
83 boolean toggle = (modifiers & MouseEvent.CTRL_MASK) != 0;
\r
84 boolean accumulate = (modifiers & MouseEvent.SHIFT_MASK) != 0;
\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
91 int selectionId = mouseId;
\r
93 selection.toggle(selectionId, boxSelection);
\r
94 } else if (accumulate) {
\r
95 for (IElement elem : boxSelection)
\r
96 selection.add(selectionId, elem);
\r
98 selection.setSelection(selectionId, boxSelection);
\r
101 if (node != null) {
\r
114 public void cleanup() {
\r
115 if (quality != null)
\r
116 quality.setStaticQuality(null);
\r
117 if (node != null) {
\r
123 public BoxSelectionMode(Point2D startingPoint, Point2D currentPoint, final int mouseId, final int mouseButton, final PickPolicy boxSelectMode) {
\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
132 public synchronized Color getToolColor() {
\r
133 Color c = getHint(DiagramHints.KEY_SELECTION_FRAME_COLOR);
\r
136 return Color.BLACK;
\r
140 * Allows user to cancel box selection operations by invoking the CANCEL
\r
141 * command (pressing ESC).
\r
143 @EventHandler(priority = 100)
\r
144 public boolean handleCancel(CommandEvent e) {
\r
145 if (e.command.equals( Commands.CANCEL ) ) {
\r