Changeset 4396 in josm for trunk/src/org/openstreetmap/josm/actions/mapmode
- Timestamp:
- 2011-09-02T14:04:22+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java
r4360 r4396 12 12 import java.awt.Toolkit; 13 13 import java.awt.event.AWTEventListener; 14 import java.awt.event.ActionEvent;15 14 import java.awt.event.InputEvent; 16 15 import java.awt.event.KeyEvent; … … 70 69 enum Mode { move, rotate, scale, select } 71 70 72 // contains all possible cases the cursor can be in the SelectAction except the 73 // the move pointer (latter is a system one and not an image) 74 private enum SelectActionCursor { 71 // contains all possible cases the cursor can be in the SelectAction 72 static private enum SelectActionCursor { 75 73 rect("normal", "selection"), 76 74 rect_add("normal", "select_add"), … … 84 82 virtual_node("normal", "addnode"), 85 83 scale("scale", null), 86 rotate("rotate", null); 84 rotate("rotate", null), 85 merge("crosshair", null), 86 merge_to_node("crosshair", "joinnode"), 87 move(Cursor.MOVE_CURSOR); 87 88 88 89 private final Cursor c; 89 90 private SelectActionCursor(String main, String sub) { 90 91 c = ImageProvider.getCursor(main, sub); 92 } 93 private SelectActionCursor(int systemCursor) { 94 c = Cursor.getPredefinedCursor(systemCursor); 91 95 } 92 96 public Cursor cursor() { … … 154 158 initialMoveThreshold = Main.pref.getInteger("edit.initial-move-threshold", 5); 155 159 drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true); 156 // This is required to update the cursors when ctrl/shift/alt is pressed157 try {158 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);159 } catch (SecurityException ex) {160 System.out.println(ex);161 }162 160 } 163 161 … … 169 167 mv.setVirtualNodesEnabled( 170 168 Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0); 169 // This is required to update the cursors when ctrl/shift/alt is pressed 170 try { 171 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK); 172 } catch (SecurityException ex) {} 171 173 } 172 174 … … 178 180 mv.removeMouseMotionListener(this); 179 181 mv.setVirtualNodesEnabled(false); 182 try { 183 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 184 } catch (SecurityException ex) {} 180 185 removeHighlighting(); 181 186 } … … 196 201 break; 197 202 } 198 199 // nearbyStuff cannot be empty as otherwise we would be in 200 // Move.select and not Move.move 201 OsmPrimitive osm = nearbyStuff.iterator().next(); 203 final Iterator<OsmPrimitive> it = nearbyStuff.iterator(); 204 final OsmPrimitive osm = it.hasNext() ? it.next() : null; 205 206 if(dragInProgress()) { 207 c = ctrl ? "merge" : "move"; 208 if(osm != null && osm instanceof Node) { 209 c += "_to_node"; 210 } 211 break; 212 } 202 213 203 214 c = (osm instanceof Node) ? "node" : c; 204 215 c = (osm instanceof Way) ? "way" : c; 205 206 216 if(shift) { 207 217 c += "_add"; … … 246 256 /** 247 257 * handles adding highlights and updating the cursor for the given mouse event. 258 * Please note that the highlighting for merging while moving is handled via mouseDragged. 248 259 * @param MouseEvent which should be used as base for the feedback 249 260 * @return true if repaint is required … … 255 266 /** 256 267 * handles adding highlights and updating the cursor for the given mouse event. 268 * Please note that the highlighting for merging while moving is handled via mouseDragged. 257 269 * @param MouseEvent which should be used as base for the feedback 258 270 * @param define custom keyboard modifiers if the ones from MouseEvent are outdated or similar … … 310 322 // We don't have a mouse event, so we pass the old mouse event but the 311 323 // new modifiers. 312 giveUserFeedback(oldEvent, ((InputEvent) e).getModifiers()); 324 if(giveUserFeedback(oldEvent, ((InputEvent) e).getModifiers())) { 325 mv.repaint(); 326 } 313 327 } 314 328 … … 338 352 339 353 if (mode == Mode.move) { 340 mv.setNewCursor(Cursor.MOVE_CURSOR, this); 354 // If ctrl is pressed we are in merge mode. Look for a nearby node, 355 // highlight it and adjust the cursor accordingly. 356 final OsmPrimitive p = ctrl ? (OsmPrimitive)findNodeToMergeTo(e) : null; 357 boolean needsRepaint = removeHighlighting(); 358 if(p != null) { 359 p.setHighlighted(true); 360 oldHighlights.add(p); 361 needsRepaint = true; 362 } 363 mv.setNewCursor(getCursor(MapView.asColl(p)), this); 364 if(needsRepaint) { 365 mv.repaint(); 366 } 341 367 } 342 368 … … 463 489 mv.repaint(); 464 490 } 491 } 492 493 /** returns true whenever elements have been grabbed and moved (i.e. the initial 494 * thresholds have been exceeded) and is still in progress (i.e. mouse button 495 * still pressed) 496 */ 497 final private boolean dragInProgress() { 498 return didMouseDrag && startingDraggingPos != null; 465 499 } 466 500 … … 595 629 } else if (alt && ctrl) { 596 630 mode = Mode.scale; 597 } else if (hasSelectionNearby) { 631 } else if (hasSelectionNearby || dragInProgress()) { 598 632 mode = Mode.move; 599 633 } else { … … 731 765 } 732 766 } else { 733 mergePrims( getCurrentDataSet().getSelectedNodes(),e);767 mergePrims(e); 734 768 } 735 769 getCurrentDataSet().fireSelectionChanged(); … … 815 849 } 816 850 817 private void mergePrims(Collection<Node> affectedNodes, MouseEvent e) { 818 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0; 819 820 if (ctrl && !affectedNodes.isEmpty()) { 821 Collection<Node> target = mv.getNearestNodes(e.getPoint(), affectedNodes, OsmPrimitive.isSelectablePredicate); 822 if (!target.isEmpty()) { 823 Collection<Node> nodesToMerge = new LinkedList<Node>(affectedNodes); 824 Node t = target.iterator().next(); 825 nodesToMerge.add(t); 826 Command cmd = MergeNodesAction.mergeNodes(Main.main.getEditLayer(), nodesToMerge, t); 827 if (cmd != null) { 828 Main.main.undoRedo.add(cmd); 829 getCurrentDataSet().setSelected(t); 830 } 831 } 832 } 851 /** Merges the selected nodes to the one closest to the given mouse position iff the control 852 * key is pressed. If there is no such node, no action will be done and no error will be 853 * reported. If there is, it will execute the merge and add it to the undo buffer. */ 854 final private void mergePrims(MouseEvent e) { 855 updateKeyModifiers(e); 856 Collection<Node> selNodes = getCurrentDataSet().getSelectedNodes(); 857 if (!ctrl || selNodes.isEmpty()) 858 return; 859 860 Node target = findNodeToMergeTo(e); 861 if (target == null) 862 return; 863 864 Collection<Node> nodesToMerge = new LinkedList<Node>(selNodes); 865 nodesToMerge.add(target); 866 Command cmd = MergeNodesAction.mergeNodes(Main.main.getEditLayer(), nodesToMerge, target); 867 if (cmd != null) { 868 Main.main.undoRedo.add(cmd); 869 getCurrentDataSet().setSelected(target); 870 } 871 } 872 873 /** tries to find a node to merge to when in move-merge mode for the current mouse 874 * position. Either returns the node or null, if no suitable one is nearby. */ 875 final private Node findNodeToMergeTo(MouseEvent e) { 876 Collection<Node> target = mv.getNearestNodes(e.getPoint(), 877 getCurrentDataSet().getSelectedNodes(), 878 OsmPrimitive.isSelectablePredicate); 879 return target.isEmpty() ? null : target.iterator().next(); 833 880 } 834 881
Note:
See TracChangeset
for help on using the changeset viewer.