Changeset 9537 in osm for applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java
- Timestamp:
- 2008-08-07T15:33:50+02:00 (17 years ago)
- Location:
- applications/editors/josm/plugins/slippy_map_chooser
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/slippy_map_chooser
-
Property svn:ignore
set to
build
-
Property svn:ignore
set to
-
applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java
r7343 r9537 2 2 // License: GPL. Copyright 2007 by Tim Haussmann 3 3 4 import java.awt.Cursor;5 4 import java.awt.Point; 6 import java.awt.event.ActionEvent;7 5 import java.awt.event.KeyEvent; 8 6 import java.awt.event.MouseAdapter; 9 7 import java.awt.event.MouseEvent; 8 import java.awt.event.MouseListener; 10 9 import java.awt.event.MouseMotionListener; 11 import java.awt.event.MouseWheelEvent;12 import java.awt.event.MouseWheelListener;13 10 14 import javax.swing.AbstractAction;15 11 import javax.swing.JComponent; 16 12 import javax.swing.JPanel; 17 13 import javax.swing.KeyStroke; 18 import static org.openstreetmap.josm.tools.I18n.tr;19 14 20 15 /** 21 * This class controls the user input by listening to mouse and key events. Currently implemented is:22 * - zooming in and out with scrollwheel 23 * - zooming inand centering by double clicking24 * - selecting an area by clicking and draggingthe mouse16 * This class controls the user input by listening to mouse and key events. 17 * Currently implemented is: - zooming in and out with scrollwheel - zooming in 18 * and centering by double clicking - selecting an area by clicking and dragging 19 * the mouse 25 20 * 26 21 * @author Tim Haussmann 27 22 */ 28 public class OsmMapControl extends MouseAdapter implements MouseMotionListener, Mouse WheelListener {23 public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseListener { 29 24 30 31 //positions while moving the map 32 private int iLastX = 0; 33 private int iLastY = 0; 34 35 //start and end point of selection rectangle 25 // start and end point of selection rectangle 36 26 private Point iStartSelectionPoint; 37 27 private Point iEndSelectionPoint; 38 28 39 //the SlippyMapChooserComponent 29 // the SlippyMapChooserComponent 40 30 private final SlippyMapChooser iSlippyMapChooser; 41 42 //The old cursor when we changed it to movement cursor.43 private Cursor iOldCursor;44 31 45 private boolean isMovementInPlace = false;46 47 32 private SizeButton iSizeButton = null; 48 33 49 50 private final class ZoomerAction extends AbstractAction {51 private final String action;52 public ZoomerAction(String action) {53 this.action = action;54 }55 public void actionPerformed(ActionEvent e) {56 if (action.equals(".") || action.equals(",")) {57 Point mouse = iSlippyMapChooser.getMousePosition();58 if (mouse == null)59 mouse = new Point((int)iSlippyMapChooser.getBounds().getCenterX(), (int)iSlippyMapChooser.getBounds().getCenterY());60 MouseWheelEvent we = new MouseWheelEvent(iSlippyMapChooser, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, action.equals(",") ? -1 : 1);61 mouseWheelMoved(we);62 } else {63 if (action.equals(tr("left")))64 iSlippyMapChooser.moveMap(-30, 0);65 else if (action.equals("right"))66 iSlippyMapChooser.moveMap(30, 0);67 else if (action.equals("up"))68 iSlippyMapChooser.moveMap(0, -30);69 else if (action.equals("down"))70 iSlippyMapChooser.moveMap(0, 30);71 }72 }73 }74 75 76 77 34 /** 78 35 * Create a new OsmMapControl … … 82 39 iSlippyMapChooser.addMouseListener(this); 83 40 iSlippyMapChooser.addMouseMotionListener(this); 84 iSlippyMapChooser.addMouseWheelListener(this); 85 86 String[] n = {",",".","up","right","down","left"}; 87 int[] k = {KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT}; 41 42 String[] n = { ",", ".", "up", "right", "down", "left" }; 43 int[] k = 44 { KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, 45 KeyEvent.VK_DOWN, KeyEvent.VK_LEFT }; 88 46 89 47 if (contentPane != null) { 90 48 for (int i = 0; i < n.length; ++i) { 91 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), "MapMover.Zoomer."+n[i]); 92 contentPane.getActionMap().put("MapMover.Zoomer."+n[i], new ZoomerAction(n[i])); 49 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( 50 KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), 51 "MapMover.Zoomer." + n[i]); 93 52 } 94 53 } 95 96 54 iSizeButton = sizeButton; 97 55 } 98 56 99 57 /** 100 * If the right mousebuttonis pressed and moved while holding, move the map.101 * If the left mouse button is pressed start the selection rectangle and stop when left mouse button is released58 * Start drawing the selection rectangle if it was the 1st button (left 59 * button) 102 60 */ 103 public void mouseDragged(MouseEvent e) { 104 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK; 105 //Moving the map 106 if ((e.getModifiersEx() & (MouseEvent.BUTTON3_DOWN_MASK | offMask)) == MouseEvent.BUTTON3_DOWN_MASK) { 107 108 startMovement(e); 109 110 //calc the moved distance since the last drag event 111 int distX = e.getX()-iLastX; 112 int distY = e.getY()-iLastY; 113 114 //change the offset for the origin of the map 115 iSlippyMapChooser.moveMap(-distX, -distY); 116 117 //update the last position of the mouse for the next dragging event 118 iLastX = e.getX(); 119 iLastY = e.getY(); 120 } 121 //selecting 122 else if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK){ 123 iEndSelectionPoint = e.getPoint(); 124 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint()); 125 } 126 //end moving of the map 127 else 128 endMovement(); 129 } 130 131 /** 132 * Start moving the map, if it was the 3rd button (right button). 133 * Start drawing the selection rectangle if it was the 1st button (left button) 134 */ 135 @Override public void mousePressed(MouseEvent e) { 136 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK; 137 if (e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0) 138 startMovement(e); 139 else if (e.getButton() == MouseEvent.BUTTON1){ 140 if(!iSizeButton.hit(e.getPoint())){ 61 @Override 62 public void mousePressed(MouseEvent e) { 63 if (e.getButton() == MouseEvent.BUTTON1) { 64 if (!iSizeButton.hit(e.getPoint())) { 141 65 iStartSelectionPoint = e.getPoint(); 142 i SlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);66 iEndSelectionPoint = e.getPoint(); 143 67 } 144 68 } 145 69 } 146 147 148 /**149 * When dragging the map change the cursor back to it's pre-move cursor.150 * If a double-click occurs center and zoom the map on the clicked location.151 */152 @Override public void mouseReleased(MouseEvent e) {153 if (e.getButton() == MouseEvent.BUTTON3)154 endMovement();155 else if (e.getButton() == MouseEvent.BUTTON1){156 if(iSizeButton.hit(e.getPoint())){157 iSizeButton.toggle();158 iSlippyMapChooser.resizeSlippyMap();159 }else{160 if(e.getClickCount() == 1){161 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());162 163 //reset the selections start and end164 iEndSelectionPoint = null;165 iStartSelectionPoint = null;166 }167 else if(e.getClickCount() == 2){168 iSlippyMapChooser.zoomIn(e.getPoint());169 iSlippyMapChooser.centerOnScreenPoint(e.getPoint());170 }171 }172 }173 }174 70 175 /** 176 * Start movement by setting a new cursor and remember the current mouse 177 * position. 178 */ 179 private void startMovement(MouseEvent e) { 180 if (isMovementInPlace) 181 return; 182 isMovementInPlace = true; 183 iLastX = e.getX(); 184 iLastY = e.getY(); 185 iOldCursor = iSlippyMapChooser.getCursor(); 186 iSlippyMapChooser.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 187 } 188 189 /** 190 * End the movement. Setting back the cursor and clear the movement variables 191 */ 192 private void endMovement() { 193 if (!isMovementInPlace) 194 return; 195 isMovementInPlace = false; 196 if (iOldCursor != null) 197 iSlippyMapChooser.setCursor(iOldCursor); 198 else 199 iSlippyMapChooser.setCursor(Cursor.getDefaultCursor()); 200 iOldCursor = null; 201 } 202 203 /** 204 * Zoom the map in and out. 205 * @param e The wheel event. 206 */ 207 public void mouseWheelMoved(MouseWheelEvent e) { 208 int rot = e.getWheelRotation(); 209 //scroll wheel rotated away from user 210 if(rot < 0){ 211 iSlippyMapChooser.zoomIn(e.getPoint()); 212 } 213 //scroll wheel rotated towards the user 214 else if(rot > 0){ 215 iSlippyMapChooser.zoomOut(e.getPoint()); 71 @Override 72 public void mouseDragged(MouseEvent e) { 73 if (iStartSelectionPoint != null) { 74 iEndSelectionPoint = e.getPoint(); 75 iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint); 216 76 } 217 77 } 218 78 219 79 /** 220 * Does nothing. Only to satisfy MouseMotionListener 80 * When dragging the map change the cursor back to it's pre-move cursor. If 81 * a double-click occurs center and zoom the map on the clicked location. 221 82 */ 222 public void mouseMoved(MouseEvent e) {} 83 @Override 84 public void mouseReleased(MouseEvent e) { 85 if (e.getButton() == MouseEvent.BUTTON1) { 86 if (iSizeButton.hit(e.getPoint())) { 87 iSizeButton.toggle(); 88 iSlippyMapChooser.resizeSlippyMap(); 89 } else { 90 if (e.getClickCount() == 1) { 91 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint()); 92 93 // reset the selections start and end 94 iEndSelectionPoint = null; 95 iStartSelectionPoint = null; 96 } 97 } 98 } 99 } 100 223 101 }
Note:
See TracChangeset
for help on using the changeset viewer.