source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java@ 12969

Last change on this file since 12969 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

  • Property svn:eol-style set to native
File size: 50.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.Cursor;
9import java.awt.Point;
10import java.awt.Rectangle;
11import java.awt.event.KeyEvent;
12import java.awt.event.MouseEvent;
13import java.awt.geom.Point2D;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.HashSet;
17import java.util.Iterator;
18import java.util.LinkedList;
19import java.util.Optional;
20
21import javax.swing.JOptionPane;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.MergeNodesAction;
25import org.openstreetmap.josm.command.AddCommand;
26import org.openstreetmap.josm.command.ChangeCommand;
27import org.openstreetmap.josm.command.Command;
28import org.openstreetmap.josm.command.MoveCommand;
29import org.openstreetmap.josm.command.RotateCommand;
30import org.openstreetmap.josm.command.ScaleCommand;
31import org.openstreetmap.josm.command.SequenceCommand;
32import org.openstreetmap.josm.data.coor.EastNorth;
33import org.openstreetmap.josm.data.coor.LatLon;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.data.osm.Node;
36import org.openstreetmap.josm.data.osm.OsmPrimitive;
37import org.openstreetmap.josm.data.osm.Way;
38import org.openstreetmap.josm.data.osm.WaySegment;
39import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
40import org.openstreetmap.josm.data.osm.visitor.paint.WireframeMapRenderer;
41import org.openstreetmap.josm.gui.ExtendedDialog;
42import org.openstreetmap.josm.gui.MainApplication;
43import org.openstreetmap.josm.gui.MapFrame;
44import org.openstreetmap.josm.gui.MapView;
45import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
46import org.openstreetmap.josm.gui.SelectionManager;
47import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
48import org.openstreetmap.josm.gui.layer.Layer;
49import org.openstreetmap.josm.gui.layer.OsmDataLayer;
50import org.openstreetmap.josm.gui.util.GuiHelper;
51import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
52import org.openstreetmap.josm.gui.util.ModifierExListener;
53import org.openstreetmap.josm.spi.preferences.Config;
54import org.openstreetmap.josm.tools.ImageProvider;
55import org.openstreetmap.josm.tools.Logging;
56import org.openstreetmap.josm.tools.Pair;
57import org.openstreetmap.josm.tools.Shortcut;
58import org.openstreetmap.josm.tools.Utils;
59
60/**
61 * Move is an action that can move all kind of OsmPrimitives (except keys for now).
62 *
63 * If an selected object is under the mouse when dragging, move all selected objects.
64 * If an unselected object is under the mouse when dragging, it becomes selected
65 * and will be moved.
66 * If no object is under the mouse, move all selected objects (if any)
67 *
68 * On Mac OS X, Ctrl + mouse button 1 simulates right click (map move), so the
69 * feature "selection remove" is disabled on this platform.
70 */
71public class SelectAction extends MapMode implements ModifierExListener, KeyPressReleaseListener, SelectionEnded {
72
73 private static final String NORMAL = "normal";
74
75 /**
76 * Select action mode.
77 * @since 7543
78 */
79 public enum Mode {
80 /** "MOVE" means either dragging or select if no mouse movement occurs (i.e. just clicking) */
81 MOVE,
82 /** "ROTATE" allows to apply a rotation transformation on the selected object (see {@link RotateCommand}) */
83 ROTATE,
84 /** "SCALE" allows to apply a scaling transformation on the selected object (see {@link ScaleCommand}) */
85 SCALE,
86 /** "SELECT" means the selection rectangle */
87 SELECT
88 }
89
90 // contains all possible cases the cursor can be in the SelectAction
91 enum SelectActionCursor {
92
93 rect(NORMAL, "selection"),
94 rect_add(NORMAL, "select_add"),
95 rect_rm(NORMAL, "select_remove"),
96 way(NORMAL, "select_way"),
97 way_add(NORMAL, "select_way_add"),
98 way_rm(NORMAL, "select_way_remove"),
99 node(NORMAL, "select_node"),
100 node_add(NORMAL, "select_node_add"),
101 node_rm(NORMAL, "select_node_remove"),
102 virtual_node(NORMAL, "addnode"),
103 scale("scale", null),
104 rotate("rotate", null),
105 merge("crosshair", null),
106 lasso(NORMAL, "rope"),
107 merge_to_node("crosshair", "joinnode"),
108 move(Cursor.MOVE_CURSOR);
109
110 private final Cursor c;
111 SelectActionCursor(String main, String sub) {
112 c = ImageProvider.getCursor(main, sub);
113 }
114
115 SelectActionCursor(int systemCursor) {
116 c = Cursor.getPredefinedCursor(systemCursor);
117 }
118
119 /**
120 * Returns the action cursor.
121 * @return the cursor
122 */
123 public Cursor cursor() {
124 return c;
125 }
126 }
127
128 private boolean lassoMode;
129 private boolean repeatedKeySwitchLassoOption;
130
131 // Cache previous mouse event (needed when only the modifier keys are
132 // pressed but the mouse isn't moved)
133 private MouseEvent oldEvent;
134
135 private Mode mode;
136 private final transient SelectionManager selectionManager;
137 private boolean cancelDrawMode;
138 private boolean drawTargetHighlight;
139 private boolean didMouseDrag;
140 /**
141 * The component this SelectAction is associated with.
142 */
143 private final MapView mv;
144 /**
145 * The old cursor before the user pressed the mouse button.
146 */
147 private Point startingDraggingPos;
148 /**
149 * point where user pressed the mouse to start movement
150 */
151 private EastNorth startEN;
152 /**
153 * The last known position of the mouse.
154 */
155 private Point lastMousePos;
156 /**
157 * The time of the user mouse down event.
158 */
159 private long mouseDownTime;
160 /**
161 * The pressed button of the user mouse down event.
162 */
163 private int mouseDownButton;
164 /**
165 * The time of the user mouse down event.
166 */
167 private long mouseReleaseTime;
168 /**
169 * The time which needs to pass between click and release before something
170 * counts as a move, in milliseconds
171 */
172 private int initialMoveDelay;
173 /**
174 * The screen distance which needs to be travelled before something
175 * counts as a move, in pixels
176 */
177 private int initialMoveThreshold;
178 private boolean initialMoveThresholdExceeded;
179
180 /**
181 * elements that have been highlighted in the previous iteration. Used
182 * to remove the highlight from them again as otherwise the whole data
183 * set would have to be checked.
184 */
185 private transient Optional<OsmPrimitive> currentHighlight = Optional.empty();
186
187 /**
188 * Create a new SelectAction
189 * @param mapFrame The MapFrame this action belongs to.
190 */
191 public SelectAction(MapFrame mapFrame) {
192 super(tr("Select"), "move/move", tr("Select, move, scale and rotate objects"),
193 Shortcut.registerShortcut("mapmode:select", tr("Mode: {0}", tr("Select")), KeyEvent.VK_S, Shortcut.DIRECT),
194 ImageProvider.getCursor("normal", "selection"));
195 mv = mapFrame.mapView;
196 putValue("help", ht("/Action/Select"));
197 selectionManager = new SelectionManager(this, false, mv);
198 }
199
200 @Override
201 public void enterMode() {
202 super.enterMode();
203 mv.addMouseListener(this);
204 mv.addMouseMotionListener(this);
205 mv.setVirtualNodesEnabled(Config.getPref().getInt("mappaint.node.virtual-size", 8) != 0);
206 drawTargetHighlight = Config.getPref().getBoolean("draw.target-highlight", true);
207 initialMoveDelay = Config.getPref().getInt("edit.initial-move-delay", 200);
208 initialMoveThreshold = Config.getPref().getInt("edit.initial-move-threshold", 5);
209 repeatedKeySwitchLassoOption = Config.getPref().getBoolean("mappaint.select.toggle-lasso-on-repeated-S", true);
210 cycleManager.init();
211 virtualManager.init();
212 // This is required to update the cursors when ctrl/shift/alt is pressed
213 MapFrame map = MainApplication.getMap();
214 map.keyDetector.addModifierExListener(this);
215 map.keyDetector.addKeyListener(this);
216 }
217
218 @Override
219 public void exitMode() {
220 super.exitMode();
221 selectionManager.unregister(mv);
222 mv.removeMouseListener(this);
223 mv.removeMouseMotionListener(this);
224 mv.setVirtualNodesEnabled(false);
225 MapFrame map = MainApplication.getMap();
226 map.keyDetector.removeModifierExListener(this);
227 map.keyDetector.removeKeyListener(this);
228 removeHighlighting();
229 }
230
231 @Override
232 public void modifiersExChanged(int modifiers) {
233 if (!MainApplication.isDisplayingMapView() || oldEvent == null) return;
234 if (giveUserFeedback(oldEvent, modifiers)) {
235 mv.repaint();
236 }
237 }
238
239 /**
240 * handles adding highlights and updating the cursor for the given mouse event.
241 * Please note that the highlighting for merging while moving is handled via mouseDragged.
242 * @param e {@code MouseEvent} which should be used as base for the feedback
243 * @return {@code true} if repaint is required
244 */
245 private boolean giveUserFeedback(MouseEvent e) {
246 return giveUserFeedback(e, e.getModifiersEx());
247 }
248
249 /**
250 * handles adding highlights and updating the cursor for the given mouse event.
251 * Please note that the highlighting for merging while moving is handled via mouseDragged.
252 * @param e {@code MouseEvent} which should be used as base for the feedback
253 * @param modifiers define custom keyboard extended modifiers if the ones from MouseEvent are outdated or similar
254 * @return {@code true} if repaint is required
255 */
256 private boolean giveUserFeedback(MouseEvent e, int modifiers) {
257 Optional<OsmPrimitive> c = Optional.ofNullable(
258 mv.getNearestNodeOrWay(e.getPoint(), mv.isSelectablePredicate, true));
259
260 updateKeyModifiersEx(modifiers);
261 determineMapMode(c.isPresent());
262
263 Optional<OsmPrimitive> newHighlight = Optional.empty();
264
265 virtualManager.clear();
266 if (mode == Mode.MOVE && !dragInProgress() && virtualManager.activateVirtualNodeNearPoint(e.getPoint())) {
267 DataSet ds = getLayerManager().getEditDataSet();
268 if (ds != null && drawTargetHighlight) {
269 ds.setHighlightedVirtualNodes(virtualManager.virtualWays);
270 }
271 mv.setNewCursor(SelectActionCursor.virtual_node.cursor(), this);
272 // don't highlight anything else if a virtual node will be
273 return repaintIfRequired(newHighlight);
274 }
275
276 mv.setNewCursor(getCursor(c), this);
277
278 // return early if there can't be any highlights
279 if (!drawTargetHighlight || mode != Mode.MOVE || !c.isPresent())
280 return repaintIfRequired(newHighlight);
281
282 // CTRL toggles selection, but if while dragging CTRL means merge
283 final boolean isToggleMode = ctrl && !dragInProgress();
284 if (c.isPresent() && (isToggleMode || !c.get().isSelected())) {
285 // only highlight primitives that will change the selection
286 // when clicked. I.e. don't highlight selected elements unless
287 // we are in toggle mode.
288 newHighlight = c;
289 }
290 return repaintIfRequired(newHighlight);
291 }
292
293 /**
294 * works out which cursor should be displayed for most of SelectAction's
295 * features. The only exception is the "move" cursor when actually dragging
296 * primitives.
297 * @param nearbyStuff primitives near the cursor
298 * @return the cursor that should be displayed
299 */
300 private Cursor getCursor(Optional<OsmPrimitive> nearbyStuff) {
301 String c = "rect";
302 switch(mode) {
303 case MOVE:
304 if (virtualManager.hasVirtualNode()) {
305 c = "virtual_node";
306 break;
307 }
308 final OsmPrimitive osm = nearbyStuff.orElse(null);
309
310 if (dragInProgress()) {
311 // only consider merge if ctrl is pressed and there are nodes in
312 // the selection that could be merged
313 if (!ctrl || getLayerManager().getEditDataSet().getSelectedNodes().isEmpty()) {
314 c = "move";
315 break;
316 }
317 // only show merge to node cursor if nearby node and that node is currently
318 // not being dragged
319 final boolean hasTarget = osm instanceof Node && !osm.isSelected();
320 c = hasTarget ? "merge_to_node" : "merge";
321 break;
322 }
323
324 c = (osm instanceof Node) ? "node" : c;
325 c = (osm instanceof Way) ? "way" : c;
326 if (shift) {
327 c += "_add";
328 } else if (ctrl) {
329 c += osm == null || osm.isSelected() ? "_rm" : "_add";
330 }
331 break;
332 case ROTATE:
333 c = "rotate";
334 break;
335 case SCALE:
336 c = "scale";
337 break;
338 case SELECT:
339 if (lassoMode) {
340 c = "lasso";
341 } else {
342 c = "rect" + (shift ? "_add" : (ctrl && !Main.isPlatformOsx() ? "_rm" : ""));
343 }
344 break;
345 }
346 return SelectActionCursor.valueOf(c).cursor();
347 }
348
349 /**
350 * Removes all existing highlights.
351 * @return true if a repaint is required
352 */
353 private boolean removeHighlighting() {
354 boolean needsRepaint = false;
355 DataSet ds = getLayerManager().getEditDataSet();
356 if (ds != null && !ds.getHighlightedVirtualNodes().isEmpty()) {
357 needsRepaint = true;
358 ds.clearHighlightedVirtualNodes();
359 }
360 if (!currentHighlight.isPresent()) {
361 return needsRepaint;
362 } else {
363 currentHighlight.get().setHighlighted(false);
364 }
365 currentHighlight = Optional.empty();
366 return true;
367 }
368
369 private boolean repaintIfRequired(Optional<OsmPrimitive> newHighlight) {
370 if (!drawTargetHighlight || currentHighlight.equals(newHighlight))
371 return false;
372 currentHighlight.ifPresent(osm -> osm.setHighlighted(false));
373 newHighlight.ifPresent(osm -> osm.setHighlighted(true));
374 currentHighlight = newHighlight;
375 return true;
376 }
377
378 /**
379 * Look, whether any object is selected. If not, select the nearest node.
380 * If there are no nodes in the dataset, do nothing.
381 *
382 * If the user did not press the left mouse button, do nothing.
383 *
384 * Also remember the starting position of the movement and change the mouse
385 * cursor to movement.
386 */
387 @Override
388 public void mousePressed(MouseEvent e) {
389 mouseDownButton = e.getButton();
390 // return early
391 if (!mv.isActiveLayerVisible() || !(Boolean) this.getValue("active") || mouseDownButton != MouseEvent.BUTTON1)
392 return;
393
394 // left-button mouse click only is processed here
395
396 // request focus in order to enable the expected keyboard shortcuts
397 mv.requestFocus();
398
399 // update which modifiers are pressed (shift, alt, ctrl)
400 updateKeyModifiers(e);
401
402 // We don't want to change to draw tool if the user tries to (de)select
403 // stuff but accidentally clicks in an empty area when selection is empty
404 cancelDrawMode = shift || ctrl;
405 didMouseDrag = false;
406 initialMoveThresholdExceeded = false;
407 mouseDownTime = System.currentTimeMillis();
408 lastMousePos = e.getPoint();
409 startEN = mv.getEastNorth(lastMousePos.x, lastMousePos.y);
410
411 // primitives under cursor are stored in c collection
412
413 OsmPrimitive nearestPrimitive = mv.getNearestNodeOrWay(e.getPoint(), mv.isSelectablePredicate, true);
414
415 determineMapMode(nearestPrimitive != null);
416
417 switch(mode) {
418 case ROTATE:
419 case SCALE:
420 // if nothing was selected, select primitive under cursor for scaling or rotating
421 DataSet ds = getLayerManager().getEditDataSet();
422 if (ds.selectionEmpty()) {
423 ds.setSelected(asColl(nearestPrimitive));
424 }
425
426 // Mode.select redraws when selectPrims is called
427 // Mode.move redraws when mouseDragged is called
428 // Mode.rotate redraws here
429 // Mode.scale redraws here
430 break;
431 case MOVE:
432 // also include case when some primitive is under cursor and no shift+ctrl / alt+ctrl is pressed
433 // so this is not movement, but selection on primitive under cursor
434 if (!cancelDrawMode && nearestPrimitive instanceof Way) {
435 virtualManager.activateVirtualNodeNearPoint(e.getPoint());
436 }
437 OsmPrimitive toSelect = cycleManager.cycleSetup(nearestPrimitive, e.getPoint());
438 selectPrims(asColl(toSelect), false, false);
439 useLastMoveCommandIfPossible();
440 // Schedule a timer to update status line "initialMoveDelay+1" ms in the future
441 GuiHelper.scheduleTimer(initialMoveDelay+1, evt -> updateStatusLine(), false);
442 break;
443 case SELECT:
444 default:
445 if (!(ctrl && Main.isPlatformOsx())) {
446 // start working with rectangle or lasso
447 selectionManager.register(mv, lassoMode);
448 selectionManager.mousePressed(e);
449 break;
450 }
451 }
452 if (giveUserFeedback(e)) {
453 mv.repaint();
454 }
455 updateStatusLine();
456 }
457
458 @Override
459 public void mouseMoved(MouseEvent e) {
460 // Mac OSX simulates with ctrl + mouse 1 the second mouse button hence no dragging events get fired.
461 if (Main.isPlatformOsx() && (mode == Mode.ROTATE || mode == Mode.SCALE)) {
462 mouseDragged(e);
463 return;
464 }
465 oldEvent = e;
466 if (giveUserFeedback(e)) {
467 mv.repaint();
468 }
469 }
470
471 /**
472 * If the left mouse button is pressed, move all currently selected
473 * objects (if one of them is under the mouse) or the current one under the
474 * mouse (which will become selected).
475 */
476 @Override
477 public void mouseDragged(MouseEvent e) {
478 if (!mv.isActiveLayerVisible())
479 return;
480
481 // Swing sends random mouseDragged events when closing dialogs by double-clicking their top-left icon on Windows
482 // Ignore such false events to prevent issues like #7078
483 if (mouseDownButton == MouseEvent.BUTTON1 && mouseReleaseTime > mouseDownTime)
484 return;
485
486 cancelDrawMode = true;
487 if (mode == Mode.SELECT) {
488 // Unregisters selectionManager if ctrl has been pressed after mouse click on Mac OS X in order to move the map
489 if (ctrl && Main.isPlatformOsx()) {
490 selectionManager.unregister(mv);
491 // Make sure correct cursor is displayed
492 mv.setNewCursor(Cursor.MOVE_CURSOR, this);
493 }
494 return;
495 }
496
497 // do not count anything as a move if it lasts less than 100 milliseconds.
498 if ((mode == Mode.MOVE) && (System.currentTimeMillis() - mouseDownTime < initialMoveDelay))
499 return;
500
501 if (mode != Mode.ROTATE && mode != Mode.SCALE && (e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0) {
502 // button is pressed in rotate mode
503 return;
504 }
505
506 if (mode == Mode.MOVE) {
507 // If ctrl is pressed we are in merge mode. Look for a nearby node,
508 // highlight it and adjust the cursor accordingly.
509 final boolean canMerge = ctrl && !getLayerManager().getEditDataSet().getSelectedNodes().isEmpty();
510 final OsmPrimitive p = canMerge ? findNodeToMergeTo(e.getPoint()) : null;
511 boolean needsRepaint = removeHighlighting();
512 if (p != null) {
513 p.setHighlighted(true);
514 currentHighlight = Optional.of(p);
515 needsRepaint = true;
516 }
517 mv.setNewCursor(getCursor(Optional.ofNullable(p)), this);
518 // also update the stored mouse event, so we can display the correct cursor
519 // when dragging a node onto another one and then press CTRL to merge
520 oldEvent = e;
521 if (needsRepaint) {
522 mv.repaint();
523 }
524 }
525
526 if (startingDraggingPos == null) {
527 startingDraggingPos = new Point(e.getX(), e.getY());
528 }
529
530 if (lastMousePos == null) {
531 lastMousePos = e.getPoint();
532 return;
533 }
534
535 if (!initialMoveThresholdExceeded) {
536 int dp = (int) lastMousePos.distance(e.getX(), e.getY());
537 if (dp < initialMoveThreshold)
538 return; // ignore small drags
539 initialMoveThresholdExceeded = true; //no more ingnoring uintil nex mouse press
540 }
541 if (e.getPoint().equals(lastMousePos))
542 return;
543
544 EastNorth currentEN = mv.getEastNorth(e.getX(), e.getY());
545
546 if (virtualManager.hasVirtualWaysToBeConstructed()) {
547 virtualManager.createMiddleNodeFromVirtual(currentEN);
548 } else {
549 if (!updateCommandWhileDragging(currentEN)) return;
550 }
551
552 mv.repaint();
553 if (mode != Mode.SCALE) {
554 lastMousePos = e.getPoint();
555 }
556
557 didMouseDrag = true;
558 }
559
560 @Override
561 public void mouseExited(MouseEvent e) {
562 if (removeHighlighting()) {
563 mv.repaint();
564 }
565 }
566
567 @Override
568 public void mouseReleased(MouseEvent e) {
569 if (!mv.isActiveLayerVisible())
570 return;
571
572 startingDraggingPos = null;
573 mouseReleaseTime = System.currentTimeMillis();
574 MapFrame map = MainApplication.getMap();
575
576 if (mode == Mode.SELECT) {
577 if (e.getButton() != MouseEvent.BUTTON1) {
578 return;
579 }
580 selectionManager.endSelecting(e);
581 selectionManager.unregister(mv);
582
583 // Select Draw Tool if no selection has been made
584 if (!cancelDrawMode && getLayerManager().getEditDataSet().selectionEmpty()) {
585 map.selectDrawTool(true);
586 updateStatusLine();
587 return;
588 }
589 }
590
591 if (mode == Mode.MOVE && e.getButton() == MouseEvent.BUTTON1) {
592 if (!didMouseDrag) {
593 // only built in move mode
594 virtualManager.clear();
595 // do nothing if the click was to short too be recognized as a drag,
596 // but the release position is farther than 10px away from the press position
597 if (lastMousePos == null || lastMousePos.distanceSq(e.getPoint()) < 100) {
598 updateKeyModifiers(e);
599 selectPrims(cycleManager.cyclePrims(), true, false);
600
601 // If the user double-clicked a node, change to draw mode
602 Collection<OsmPrimitive> c = getLayerManager().getEditDataSet().getSelected();
603 if (e.getClickCount() >= 2 && c.size() == 1 && c.iterator().next() instanceof Node) {
604 // We need to do it like this as otherwise drawAction will see a double
605 // click and switch back to SelectMode
606 MainApplication.worker.execute(() -> map.selectDrawTool(true));
607 return;
608 }
609 }
610 } else {
611 confirmOrUndoMovement(e);
612 }
613 }
614
615 mode = null;
616
617 // simply remove any highlights if the middle click popup is active because
618 // the highlights don't depend on the cursor position there. If something was
619 // selected beforehand this would put us into move mode as well, which breaks
620 // the cycling through primitives on top of each other (see #6739).
621 if (e.getButton() == MouseEvent.BUTTON2) {
622 removeHighlighting();
623 } else {
624 giveUserFeedback(e);
625 }
626 updateStatusLine();
627 }
628
629 @Override
630 public void selectionEnded(Rectangle r, MouseEvent e) {
631 updateKeyModifiers(e);
632 selectPrims(selectionManager.getSelectedObjects(alt), true, true);
633 }
634
635 @Override
636 public void doKeyPressed(KeyEvent e) {
637 if (!repeatedKeySwitchLassoOption || !MainApplication.isDisplayingMapView() || !getShortcut().isEvent(e))
638 return;
639 if (Logging.isDebugEnabled()) {
640 Logging.debug("{0} consuming event {1}", getClass().getName(), e);
641 }
642 e.consume();
643 MapFrame map = MainApplication.getMap();
644 if (!lassoMode) {
645 map.selectMapMode(map.mapModeSelectLasso);
646 } else {
647 map.selectMapMode(map.mapModeSelect);
648 }
649 }
650
651 @Override
652 public void doKeyReleased(KeyEvent e) {
653 // Do nothing
654 }
655
656 /**
657 * sets the mapmode according to key modifiers and if there are any
658 * selectables nearby. Everything has to be pre-determined for this
659 * function; its main purpose is to centralize what the modifiers do.
660 * @param hasSelectionNearby {@code true} if some primitves are selectable nearby
661 */
662 private void determineMapMode(boolean hasSelectionNearby) {
663 if (shift && ctrl) {
664 mode = Mode.ROTATE;
665 } else if (alt && ctrl) {
666 mode = Mode.SCALE;
667 } else if (hasSelectionNearby || dragInProgress()) {
668 mode = Mode.MOVE;
669 } else {
670 mode = Mode.SELECT;
671 }
672 }
673
674 /**
675 * Determines whenever elements have been grabbed and moved (i.e. the initial
676 * thresholds have been exceeded) and is still in progress (i.e. mouse button still pressed)
677 * @return true if a drag is in progress
678 */
679 private boolean dragInProgress() {
680 return didMouseDrag && startingDraggingPos != null;
681 }
682
683 /**
684 * Create or update data modification command while dragging mouse - implementation of
685 * continuous moving, scaling and rotation
686 * @param currentEN - mouse position
687 * @return status of action (<code>true</code> when action was performed)
688 */
689 private boolean updateCommandWhileDragging(EastNorth currentEN) {
690 // Currently we support only transformations which do not affect relations.
691 // So don't add them in the first place to make handling easier
692 DataSet ds = getLayerManager().getEditDataSet();
693 Collection<OsmPrimitive> selection = ds.getSelectedNodesAndWays();
694 if (selection.isEmpty()) { // if nothing was selected to drag, just select nearest node/way to the cursor
695 OsmPrimitive nearestPrimitive = mv.getNearestNodeOrWay(mv.getPoint(startEN), mv.isSelectablePredicate, true);
696 ds.setSelected(nearestPrimitive);
697 }
698
699 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
700 // for these transformations, having only one node makes no sense - quit silently
701 if (affectedNodes.size() < 2 && (mode == Mode.ROTATE || mode == Mode.SCALE)) {
702 return false;
703 }
704 Command c = getLastCommandInDataset(ds);
705 if (mode == Mode.MOVE) {
706 if (startEN == null) return false; // fix #8128
707 ds.beginUpdate();
708 try {
709 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
710 ((MoveCommand) c).saveCheckpoint();
711 ((MoveCommand) c).applyVectorTo(currentEN);
712 } else if (!selection.isEmpty()) {
713 c = new MoveCommand(selection, startEN, currentEN);
714 MainApplication.undoRedo.add(c);
715 }
716 for (Node n : affectedNodes) {
717 LatLon ll = n.getCoor();
718 if (ll != null && ll.isOutSideWorld()) {
719 // Revert move
720 ((MoveCommand) c).resetToCheckpoint();
721 // TODO: We might use a simple notification in the lower left corner.
722 JOptionPane.showMessageDialog(
723 Main.parent,
724 tr("Cannot move objects outside of the world."),
725 tr("Warning"),
726 JOptionPane.WARNING_MESSAGE);
727 mv.setNewCursor(cursor, this);
728 return false;
729 }
730 }
731 } finally {
732 ds.endUpdate();
733 }
734 } else {
735 startEN = currentEN; // drag can continue after scaling/rotation
736
737 if (mode != Mode.ROTATE && mode != Mode.SCALE) {
738 return false;
739 }
740
741 ds.beginUpdate();
742 try {
743 if (mode == Mode.ROTATE) {
744 if (c instanceof RotateCommand && affectedNodes.equals(((RotateCommand) c).getTransformedNodes())) {
745 ((RotateCommand) c).handleEvent(currentEN);
746 } else {
747 MainApplication.undoRedo.add(new RotateCommand(selection, currentEN));
748 }
749 } else if (mode == Mode.SCALE) {
750 if (c instanceof ScaleCommand && affectedNodes.equals(((ScaleCommand) c).getTransformedNodes())) {
751 ((ScaleCommand) c).handleEvent(currentEN);
752 } else {
753 MainApplication.undoRedo.add(new ScaleCommand(selection, currentEN));
754 }
755 }
756
757 Collection<Way> ways = ds.getSelectedWays();
758 if (doesImpactStatusLine(affectedNodes, ways)) {
759 MainApplication.getMap().statusLine.setDist(ways);
760 }
761 } finally {
762 ds.endUpdate();
763 }
764 }
765 return true;
766 }
767
768 private static boolean doesImpactStatusLine(Collection<Node> affectedNodes, Collection<Way> selectedWays) {
769 for (Way w : selectedWays) {
770 for (Node n : w.getNodes()) {
771 if (affectedNodes.contains(n)) {
772 return true;
773 }
774 }
775 }
776 return false;
777 }
778
779 /**
780 * Adapt last move command (if it is suitable) to work with next drag, started at point startEN
781 */
782 private void useLastMoveCommandIfPossible() {
783 DataSet dataSet = getLayerManager().getEditDataSet();
784 if (dataSet == null) {
785 // It may happen that there is no edit layer.
786 return;
787 }
788 Command c = getLastCommandInDataset(dataSet);
789 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(dataSet.getSelected());
790 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
791 // old command was created with different base point of movement, we need to recalculate it
792 ((MoveCommand) c).changeStartPoint(startEN);
793 }
794 }
795
796 /**
797 * Obtain command in undoRedo stack to "continue" when dragging
798 * @param ds The data set the command needs to be in.
799 * @return last command
800 */
801 private static Command getLastCommandInDataset(DataSet ds) {
802 Command lastCommand = MainApplication.undoRedo.getLastCommand();
803 if (lastCommand instanceof SequenceCommand) {
804 lastCommand = ((SequenceCommand) lastCommand).getLastCommand();
805 }
806 if (lastCommand != null && ds.equals(lastCommand.getAffectedDataSet())) {
807 return lastCommand;
808 } else {
809 return null;
810 }
811 }
812
813 /**
814 * Present warning in the following cases and undo unwanted movements: <ul>
815 * <li>large and possibly unwanted movements</li>
816 * <li>movement of node with attached ways that are hidden by filters</li>
817 * </ul>
818 *
819 * @param e the mouse event causing the action (mouse released)
820 */
821 private void confirmOrUndoMovement(MouseEvent e) {
822 if (movesHiddenWay()) {
823 final ExtendedDialog ed = new ConfirmMoveDialog();
824 ed.setContent(tr("Are you sure that you want to move elements with attached ways that are hidden by filters?"));
825 ed.toggleEnable("movedHiddenElements");
826 ed.showDialog();
827 if (ed.getValue() != 1) {
828 MainApplication.undoRedo.undo();
829 }
830 }
831 int max = Config.getPref().getInt("warn.move.maxelements", 20), limit = max;
832 for (OsmPrimitive osm : getLayerManager().getEditDataSet().getSelected()) {
833 if (osm instanceof Way) {
834 limit -= ((Way) osm).getNodes().size();
835 }
836 if (--limit < 0) {
837 break;
838 }
839 }
840 if (limit < 0) {
841 final ExtendedDialog ed = new ConfirmMoveDialog();
842 ed.setContent(
843 /* for correct i18n of plural forms - see #9110 */
844 trn("You moved more than {0} element. " + "Moving a large number of elements is often an error.\n" + "Really move them?",
845 "You moved more than {0} elements. " + "Moving a large number of elements is often an error.\n" + "Really move them?",
846 max, max));
847 ed.toggleEnable("movedManyElements");
848 ed.showDialog();
849
850 if (ed.getValue() != 1) {
851 MainApplication.undoRedo.undo();
852 }
853 } else {
854 // if small number of elements were moved,
855 updateKeyModifiers(e);
856 if (ctrl) mergePrims(e.getPoint());
857 }
858 }
859
860 static class ConfirmMoveDialog extends ExtendedDialog {
861 ConfirmMoveDialog() {
862 super(Main.parent,
863 tr("Move elements"),
864 tr("Move them"), tr("Undo move"));
865 setButtonIcons("reorder", "cancel");
866 setCancelButton(2);
867 }
868 }
869
870 private boolean movesHiddenWay() {
871 DataSet ds = getLayerManager().getEditDataSet();
872 final Collection<OsmPrimitive> elementsToTest = new HashSet<>(ds.getSelected());
873 for (Way osm : ds.getSelectedWays()) {
874 elementsToTest.addAll(osm.getNodes());
875 }
876 for (OsmPrimitive node : Utils.filteredCollection(elementsToTest, Node.class)) {
877 for (Way ref : Utils.filteredCollection(node.getReferrers(), Way.class)) {
878 if (ref.isDisabledAndHidden()) {
879 return true;
880 }
881 }
882 }
883 return false;
884 }
885
886 /**
887 * Merges the selected nodes to the one closest to the given mouse position if the control
888 * key is pressed. If there is no such node, no action will be done and no error will be
889 * reported. If there is, it will execute the merge and add it to the undo buffer.
890 * @param p mouse position
891 */
892 private void mergePrims(Point p) {
893 DataSet ds = getLayerManager().getEditDataSet();
894 Collection<Node> selNodes = ds.getSelectedNodes();
895 if (selNodes.isEmpty())
896 return;
897
898 Node target = findNodeToMergeTo(p);
899 if (target == null)
900 return;
901
902 if (selNodes.size() == 1) {
903 // Move all selected primitive to preserve shape #10748
904 Collection<OsmPrimitive> selection = ds.getSelectedNodesAndWays();
905 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
906 Command c = getLastCommandInDataset(ds);
907 ds.beginUpdate();
908 try {
909 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
910 Node selectedNode = selNodes.iterator().next();
911 EastNorth selectedEN = selectedNode.getEastNorth();
912 EastNorth targetEN = target.getEastNorth();
913 ((MoveCommand) c).moveAgain(targetEN.getX() - selectedEN.getX(),
914 targetEN.getY() - selectedEN.getY());
915 }
916 } finally {
917 ds.endUpdate();
918 }
919 }
920
921 Collection<Node> nodesToMerge = new LinkedList<>(selNodes);
922 nodesToMerge.add(target);
923 mergeNodes(MainApplication.getLayerManager().getEditLayer(), nodesToMerge, target);
924 }
925
926 /**
927 * Merge nodes using {@code MergeNodesAction}.
928 * Can be overridden for testing purpose.
929 * @param layer layer the reference data layer. Must not be null
930 * @param nodes the collection of nodes. Ignored if null
931 * @param targetLocationNode this node's location will be used for the target node
932 */
933 public void mergeNodes(OsmDataLayer layer, Collection<Node> nodes,
934 Node targetLocationNode) {
935 MergeNodesAction.doMergeNodes(layer, nodes, targetLocationNode);
936 }
937
938 /**
939 * Tries to find a node to merge to when in move-merge mode for the current mouse
940 * position. Either returns the node or null, if no suitable one is nearby.
941 * @param p mouse position
942 * @return node to merge to, or null
943 */
944 private Node findNodeToMergeTo(Point p) {
945 Collection<Node> target = mv.getNearestNodes(p,
946 getLayerManager().getEditDataSet().getSelectedNodes(),
947 mv.isSelectablePredicate);
948 return target.isEmpty() ? null : target.iterator().next();
949 }
950
951 private void selectPrims(Collection<OsmPrimitive> prims, boolean released, boolean area) {
952 DataSet ds = getLayerManager().getEditDataSet();
953
954 // not allowed together: do not change dataset selection, return early
955 // Virtual Ways: if non-empty the cursor is above a virtual node. So don't highlight
956 // anything if about to drag the virtual node (i.e. !released) but continue if the
957 // cursor is only released above a virtual node by accident (i.e. released). See #7018
958 if (ds == null || (shift && ctrl) || (ctrl && !released) || (virtualManager.hasVirtualWaysToBeConstructed() && !released))
959 return;
960
961 if (!released) {
962 // Don't replace the selection if the user clicked on a
963 // selected object (it breaks moving of selected groups).
964 // Do it later, on mouse release.
965 shift |= ds.getSelected().containsAll(prims);
966 }
967
968 if (ctrl) {
969 // Ctrl on an item toggles its selection status,
970 // but Ctrl on an *area* just clears those items
971 // out of the selection.
972 if (area) {
973 ds.clearSelection(prims);
974 } else {
975 ds.toggleSelected(prims);
976 }
977 } else if (shift) {
978 // add prims to an existing selection
979 ds.addSelected(prims);
980 } else {
981 // clear selection, then select the prims clicked
982 ds.setSelected(prims);
983 }
984 }
985
986 /**
987 * Returns the current select mode.
988 * @return the select mode
989 * @since 7543
990 */
991 public final Mode getMode() {
992 return mode;
993 }
994
995 @Override
996 public String getModeHelpText() {
997 if (mouseDownButton == MouseEvent.BUTTON1 && mouseReleaseTime < mouseDownTime) {
998 if (mode == Mode.SELECT)
999 return tr("Release the mouse button to select the objects in the rectangle.");
1000 else if (mode == Mode.MOVE && (System.currentTimeMillis() - mouseDownTime >= initialMoveDelay)) {
1001 final DataSet ds = getLayerManager().getEditDataSet();
1002 final boolean canMerge = ds != null && !ds.getSelectedNodes().isEmpty();
1003 final String mergeHelp = canMerge ? (' ' + tr("Ctrl to merge with nearest node.")) : "";
1004 return tr("Release the mouse button to stop moving.") + mergeHelp;
1005 } else if (mode == Mode.ROTATE)
1006 return tr("Release the mouse button to stop rotating.");
1007 else if (mode == Mode.SCALE)
1008 return tr("Release the mouse button to stop scaling.");
1009 }
1010 return tr("Move objects by dragging; Shift to add to selection (Ctrl to toggle); Shift-Ctrl to rotate selected; " +
1011 "Alt-Ctrl to scale selected; or change selection");
1012 }
1013
1014 @Override
1015 public boolean layerIsSupported(Layer l) {
1016 return l instanceof OsmDataLayer;
1017 }
1018
1019 /**
1020 * Enable or diable the lasso mode
1021 * @param lassoMode true to enable the lasso mode, false otherwise
1022 */
1023 public void setLassoMode(boolean lassoMode) {
1024 this.selectionManager.setLassoMode(lassoMode);
1025 this.lassoMode = lassoMode;
1026 }
1027
1028 private final transient CycleManager cycleManager = new CycleManager();
1029 private final transient VirtualManager virtualManager = new VirtualManager();
1030
1031 private class CycleManager {
1032
1033 private Collection<OsmPrimitive> cycleList = Collections.emptyList();
1034 private boolean cyclePrims;
1035 private OsmPrimitive cycleStart;
1036 private boolean waitForMouseUpParameter;
1037 private boolean multipleMatchesParameter;
1038 /**
1039 * read preferences
1040 */
1041 private void init() {
1042 waitForMouseUpParameter = Config.getPref().getBoolean("mappaint.select.waits-for-mouse-up", false);
1043 multipleMatchesParameter = Config.getPref().getBoolean("selectaction.cycles.multiple.matches", false);
1044 }
1045
1046 /**
1047 * Determine primitive to be selected and build cycleList
1048 * @param nearest primitive found by simple method
1049 * @param p point where user clicked
1050 * @return OsmPrimitive to be selected
1051 */
1052 private OsmPrimitive cycleSetup(OsmPrimitive nearest, Point p) {
1053 OsmPrimitive osm = null;
1054
1055 if (nearest != null) {
1056 osm = nearest;
1057
1058 if (!(alt || multipleMatchesParameter)) {
1059 // no real cycling, just one element in cycle list
1060 cycleList = asColl(osm);
1061
1062 if (waitForMouseUpParameter) {
1063 // prefer a selected nearest node or way, if possible
1064 osm = mv.getNearestNodeOrWay(p, mv.isSelectablePredicate, true);
1065 }
1066 } else {
1067 // Alt + left mouse button pressed: we need to build cycle list
1068 cycleList = mv.getAllNearest(p, mv.isSelectablePredicate);
1069
1070 if (cycleList.size() > 1) {
1071 cyclePrims = false;
1072
1073 // find first already selected element in cycle list
1074 OsmPrimitive old = osm;
1075 for (OsmPrimitive o : cycleList) {
1076 if (o.isSelected()) {
1077 cyclePrims = true;
1078 osm = o;
1079 break;
1080 }
1081 }
1082
1083 // special case: for cycle groups of 2, we can toggle to the
1084 // true nearest primitive on mousePressed right away
1085 if (cycleList.size() == 2 && !waitForMouseUpParameter) {
1086 if (!(osm.equals(old) || osm.isNew() || ctrl)) {
1087 cyclePrims = false;
1088 osm = old;
1089 } // else defer toggling to mouseRelease time in those cases:
1090 /*
1091 * osm == old -- the true nearest node is the
1092 * selected one osm is a new node -- do not break
1093 * unglue ways in ALT mode ctrl is pressed -- ctrl
1094 * generally works on mouseReleased
1095 */
1096 }
1097 }
1098 }
1099 }
1100 return osm;
1101 }
1102
1103 /**
1104 * Modifies current selection state and returns the next element in a
1105 * selection cycle given by
1106 * <code>cycleList</code> field
1107 * @return the next element of cycle list
1108 */
1109 private Collection<OsmPrimitive> cyclePrims() {
1110 if (cycleList.size() <= 1) {
1111 // no real cycling, just return one-element collection with nearest primitive in it
1112 return cycleList;
1113 }
1114 // updateKeyModifiers() already called before!
1115
1116 DataSet ds = getLayerManager().getEditDataSet();
1117 OsmPrimitive first = cycleList.iterator().next(), foundInDS = null;
1118 OsmPrimitive nxt = first;
1119
1120 if (cyclePrims && shift) {
1121 for (Iterator<OsmPrimitive> i = cycleList.iterator(); i.hasNext();) {
1122 nxt = i.next();
1123 if (!nxt.isSelected()) {
1124 break; // take first primitive in cycleList not in sel
1125 }
1126 }
1127 // if primitives 1,2,3 are under cursor, [Alt-press] [Shift-release] gives 1 -> 12 -> 123
1128 } else {
1129 for (Iterator<OsmPrimitive> i = cycleList.iterator(); i.hasNext();) {
1130 nxt = i.next();
1131 if (nxt.isSelected()) {
1132 foundInDS = nxt;
1133 // first selected primitive in cycleList is found
1134 if (cyclePrims || ctrl) {
1135 ds.clearSelection(foundInDS); // deselect it
1136 nxt = i.hasNext() ? i.next() : first;
1137 // return next one in cycle list (last->first)
1138 }
1139 break; // take next primitive in cycleList
1140 }
1141 }
1142 }
1143
1144 // if "no-alt-cycling" is enabled, Ctrl-Click arrives here.
1145 if (ctrl) {
1146 // a member of cycleList was found in the current dataset selection
1147 if (foundInDS != null) {
1148 // mouse was moved to a different selection group w/ a previous sel
1149 if (!cycleList.contains(cycleStart)) {
1150 ds.clearSelection(cycleList);
1151 cycleStart = foundInDS;
1152 } else if (cycleStart.equals(nxt)) {
1153 // loop detected, insert deselect step
1154 ds.addSelected(nxt);
1155 }
1156 } else {
1157 // setup for iterating a sel group again or a new, different one..
1158 nxt = cycleList.contains(cycleStart) ? cycleStart : first;
1159 cycleStart = nxt;
1160 }
1161 } else {
1162 cycleStart = null;
1163 }
1164 // return one-element collection with one element to be selected (or added to selection)
1165 return asColl(nxt);
1166 }
1167 }
1168
1169 private class VirtualManager {
1170
1171 private Node virtualNode;
1172 private Collection<WaySegment> virtualWays = new LinkedList<>();
1173 private int nodeVirtualSize;
1174 private int virtualSnapDistSq2;
1175 private int virtualSpace;
1176
1177 private void init() {
1178 nodeVirtualSize = Config.getPref().getInt("mappaint.node.virtual-size", 8);
1179 int virtualSnapDistSq = Config.getPref().getInt("mappaint.node.virtual-snap-distance", 8);
1180 virtualSnapDistSq2 = virtualSnapDistSq*virtualSnapDistSq;
1181 virtualSpace = Config.getPref().getInt("mappaint.node.virtual-space", 70);
1182 }
1183
1184 /**
1185 * Calculate a virtual node if there is enough visual space to draw a
1186 * crosshair node and the middle of a way segment is clicked. If the
1187 * user drags the crosshair node, it will be added to all ways in
1188 * <code>virtualWays</code>.
1189 *
1190 * @param p the point clicked
1191 * @return whether
1192 * <code>virtualNode</code> and
1193 * <code>virtualWays</code> were setup.
1194 */
1195 private boolean activateVirtualNodeNearPoint(Point p) {
1196 if (nodeVirtualSize > 0) {
1197
1198 Collection<WaySegment> selVirtualWays = new LinkedList<>();
1199 Pair<Node, Node> vnp = null, wnp = new Pair<>(null, null);
1200
1201 for (WaySegment ws : mv.getNearestWaySegments(p, mv.isSelectablePredicate)) {
1202 Way w = ws.way;
1203
1204 wnp.a = w.getNode(ws.lowerIndex);
1205 wnp.b = w.getNode(ws.lowerIndex + 1);
1206 MapViewPoint p1 = mv.getState().getPointFor(wnp.a);
1207 MapViewPoint p2 = mv.getState().getPointFor(wnp.b);
1208 if (WireframeMapRenderer.isLargeSegment(p1, p2, virtualSpace)) {
1209 Point2D pc = new Point2D.Double((p1.getInViewX() + p2.getInViewX()) / 2, (p1.getInViewY() + p2.getInViewY()) / 2);
1210 if (p.distanceSq(pc) < virtualSnapDistSq2) {
1211 // Check that only segments on top of each other get added to the
1212 // virtual ways list. Otherwise ways that coincidentally have their
1213 // virtual node at the same spot will be joined which is likely unwanted
1214 Pair.sort(wnp);
1215 if (vnp == null) {
1216 vnp = new Pair<>(wnp.a, wnp.b);
1217 virtualNode = new Node(mv.getLatLon(pc.getX(), pc.getY()));
1218 }
1219 if (vnp.equals(wnp)) {
1220 // if mutiple line segments have the same points,
1221 // add all segments to be splitted to virtualWays list
1222 // if some lines are selected, only their segments will go to virtualWays
1223 (w.isSelected() ? selVirtualWays : virtualWays).add(ws);
1224 }
1225 }
1226 }
1227 }
1228
1229 if (!selVirtualWays.isEmpty()) {
1230 virtualWays = selVirtualWays;
1231 }
1232 }
1233
1234 return !virtualWays.isEmpty();
1235 }
1236
1237 private void createMiddleNodeFromVirtual(EastNorth currentEN) {
1238 DataSet ds = getLayerManager().getEditDataSet();
1239 Collection<Command> virtualCmds = new LinkedList<>();
1240 virtualCmds.add(new AddCommand(ds, virtualNode));
1241 for (WaySegment virtualWay : virtualWays) {
1242 Way w = virtualWay.way;
1243 Way wnew = new Way(w);
1244 wnew.addNode(virtualWay.lowerIndex + 1, virtualNode);
1245 virtualCmds.add(new ChangeCommand(ds, w, wnew));
1246 }
1247 virtualCmds.add(new MoveCommand(ds, virtualNode, startEN, currentEN));
1248 String text = trn("Add and move a virtual new node to way",
1249 "Add and move a virtual new node to {0} ways", virtualWays.size(),
1250 virtualWays.size());
1251 MainApplication.undoRedo.add(new SequenceCommand(text, virtualCmds));
1252 ds.setSelected(Collections.singleton((OsmPrimitive) virtualNode));
1253 clear();
1254 }
1255
1256 private void clear() {
1257 virtualWays.clear();
1258 virtualNode = null;
1259 }
1260
1261 private boolean hasVirtualNode() {
1262 return virtualNode != null;
1263 }
1264
1265 private boolean hasVirtualWaysToBeConstructed() {
1266 return !virtualWays.isEmpty();
1267 }
1268 }
1269
1270 /**
1271 * Returns {@code o} as collection of {@code o}'s type.
1272 * @param <T> object type
1273 * @param o any object
1274 * @return {@code o} as collection of {@code o}'s type.
1275 */
1276 protected static <T> Collection<T> asColl(T o) {
1277 return o == null ? Collections.emptySet() : Collections.singleton(o);
1278 }
1279}
Note: See TracBrowser for help on using the repository browser.