Ignore:
Timestamp:
2008-08-07T15:33:50+02:00 (17 years ago)
Author:
stotz
Message:

Slippy-Map-Chooser now uses an extended version of JMapViewer instead of it's own implementation

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
  • applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java

    r7343 r9537  
    22// License: GPL. Copyright 2007 by Tim Haussmann
    33
    4 import java.awt.Cursor;
    54import java.awt.Point;
    6 import java.awt.event.ActionEvent;
    75import java.awt.event.KeyEvent;
    86import java.awt.event.MouseAdapter;
    97import java.awt.event.MouseEvent;
     8import java.awt.event.MouseListener;
    109import java.awt.event.MouseMotionListener;
    11 import java.awt.event.MouseWheelEvent;
    12 import java.awt.event.MouseWheelListener;
    1310
    14 import javax.swing.AbstractAction;
    1511import javax.swing.JComponent;
    1612import javax.swing.JPanel;
    1713import javax.swing.KeyStroke;
    18 import static org.openstreetmap.josm.tools.I18n.tr;
    1914
    2015/**
    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 in and centering by double clicking
    24  * - selecting an area by clicking and dragging the mouse
     16 * 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
    2520 *
    2621 * @author Tim Haussmann
    2722 */
    28 public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseWheelListener {
     23public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseListener {
    2924
    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
    3626        private Point iStartSelectionPoint;
    3727        private Point iEndSelectionPoint;
    3828
    39         //the SlippyMapChooserComponent
     29        // the SlippyMapChooserComponent
    4030        private final SlippyMapChooser iSlippyMapChooser;
    41        
    42         //The old cursor when we changed it to movement cursor.
    43         private Cursor iOldCursor;
    4431
    45         private boolean isMovementInPlace = false;
    46        
    4732        private SizeButton iSizeButton = null;
    4833
    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        
    7734        /**
    7835         * Create a new OsmMapControl
     
    8239                iSlippyMapChooser.addMouseListener(this);
    8340                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 };
    8846
    8947                if (contentPane != null) {
    9048                        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]);
    9352                        }
    9453                }
    95                
    9654                iSizeButton = sizeButton;
    9755        }
    9856
    9957        /**
    100          * If the right mouse button is 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 released
     58         * Start drawing the selection rectangle if it was the 1st button (left
     59         * button)
    10260         */
    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())) {
    14165                                iStartSelectionPoint = e.getPoint();
    142                                 iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
     66                                iEndSelectionPoint = e.getPoint();
    14367                        }
    14468                }
    14569        }
    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 end
    164                                         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         }
    17470
    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);
    21676                }
    21777        }
    21878
    21979        /**
    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.
    22182         */
    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
    223101}
Note: See TracChangeset for help on using the changeset viewer.