Changeset 2 in josm


Ignore:
Timestamp:
Sep 29, 2005 12:33:19 AM (8 years ago)
Author:
imi
Message:

Added selection

Files:
10 edited

Legend:

Unmodified
Added
Removed
  •  

    • Property svn:ignore set to

      bin
  • src/org/openstreetmap/josm/actions/DebugAction.java

    r1 r2  
    2525 
    2626        public DebugAction(MapFrame mapFrame) { 
    27                 super("Debug Zones", "images/debug.png", KeyEvent.VK_D, mapFrame); 
     27                super("Debug Zones", "debug", KeyEvent.VK_D, mapFrame); 
    2828        } 
    2929         
  • src/org/openstreetmap/josm/actions/MapMode.java

    r1 r2  
    3131         */ 
    3232        public MapMode(String name, String iconName, int mnemonic, MapFrame mapFrame) { 
    33                 super(name, new ImageIcon(iconName)); 
     33                super(name, new ImageIcon("images/"+iconName+".png")); 
    3434                putValue(MNEMONIC_KEY, mnemonic); 
    3535                this.mapFrame = mapFrame; 
  • src/org/openstreetmap/josm/actions/OpenGpxAction.java

    r1 r2  
    5454                try { 
    5555                        DataSet dataSet = new GpxReader().parse(new FileReader(gpxFile)); 
    56                         if (dataSet.name == null) 
    57                                 dataSet.name = gpxFile.getName(); 
    5856                        MapFrame map = new MapFrame(dataSet); 
    5957                        Main.main.setMapFrame(gpxFile.getName(), map); 
  • src/org/openstreetmap/josm/actions/ZoomAction.java

    r1 r2  
    11package org.openstreetmap.josm.actions; 
    22 
    3 import static java.awt.event.MouseEvent.BUTTON1; 
    4 import static java.awt.event.MouseEvent.BUTTON3; 
    5  
    6 import java.awt.Color; 
    7 import java.awt.Cursor; 
    8 import java.awt.Graphics; 
    9 import java.awt.Point; 
    103import java.awt.Rectangle; 
    114import java.awt.event.KeyEvent; 
    12 import java.awt.event.MouseEvent; 
    13 import java.awt.event.MouseListener; 
    14 import java.awt.event.MouseMotionListener; 
    15 import java.awt.event.MouseWheelEvent; 
    16 import java.awt.event.MouseWheelListener; 
    175 
     6import org.openstreetmap.josm.actions.SelectionManager.SelectionEnded; 
    187import org.openstreetmap.josm.data.GeoPoint; 
    198import org.openstreetmap.josm.gui.MapFrame; 
     
    2110 
    2211/** 
    23  * Enable the zoom mode within the MapFrame. 
     12 * Enable the zoom mode within the MapFrame.  
     13 *  
     14 * Holding down the left mouse button select a rectangle with the same aspect  
     15 * ratio than the current map view. 
     16 * Holding down left and right let the user move the former selected rectangle. 
     17 * Releasing the left button zoom to the selection. 
    2418 *  
    2519 * @author imi 
    2620 */ 
    27 public class ZoomAction extends MapMode  
    28         implements MouseListener, MouseMotionListener, MouseWheelListener { 
     21public class ZoomAction extends MapMode implements SelectionEnded { 
    2922 
    30         /** 
    31          * Point, the mouse was when pressing the button. 
    32          */ 
    33         private Point mousePosStart; 
    34         /** 
    35          * Point to the actual mouse position. 
    36          */ 
    37         private Point mousePos; 
    38         /** 
    39          * The point in the map that was the under the mouse point 
    40          * when moving around started. 
    41          */ 
    42         private GeoPoint mousePosMove; 
    4323        /** 
    4424         * Shortcut to the mapview. 
     
    4626        private final MapView mv; 
    4727        /** 
    48          * Whether currently an zooming rectangle is visible or not 
     28         * Manager that manages the selection rectangle with the aspect ratio of the 
     29         * MapView. 
    4930         */ 
    50         private boolean zoomRectVisible = false; 
    51  
    52         /** 
    53          * For the current state. 
    54          */ 
    55         enum State {NOTHING, MOVE, ZOOM, ZOOM_POS}; 
    56         /** 
    57          * The current state, the ZoomAction is in. 
    58          */ 
    59         State state = State.NOTHING; 
    60          
     31        private final SelectionManager selectionManager; 
    6132         
    6233         
     
    6637         */ 
    6738        public ZoomAction(MapFrame mapFrame) { 
    68                 super("Zoom", "images/zoom.png", KeyEvent.VK_Z, mapFrame); 
     39                super("Zoom", "zoom", KeyEvent.VK_Z, mapFrame); 
    6940                mv = mapFrame.mapView; 
     41                selectionManager = new SelectionManager(this, true, mv); 
    7042        } 
    7143 
    7244        /** 
    73          * Just to keep track of mouse movements. 
     45         * Zoom to the rectangle on the map. 
    7446         */ 
    75         public void mouseMoved(MouseEvent e) { 
    76                 mousePos = e.getPoint(); 
    77         } 
    78  
    79         /** 
    80          * Initializing the mouse moving state. 
    81          */ 
    82         public void mousePressed(MouseEvent e) { 
    83                 switch (state) { 
    84                 case NOTHING: 
    85                         switch (e.getButton()) { 
    86                         case BUTTON1: 
    87                                 state = State.ZOOM; 
    88                                 mousePosStart = e.getPoint(); 
    89                                 paintZoomRect(true); 
    90                                 break; 
    91                         case BUTTON3: 
    92                                 state = State.MOVE; 
    93                                 mousePosMove = mv.getPoint(e.getX(), e.getY(), false); 
    94                                 mv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
    95                                 break; 
    96                         } 
    97                         break; 
    98                 case ZOOM: 
    99                         if (e.getButton() == BUTTON3) 
    100                                 state = State.ZOOM_POS; 
    101                         break; 
    102                 case MOVE: 
    103                         if (e.getButton() == BUTTON1) { 
    104                                 state = State.ZOOM_POS; 
    105                                 mousePosStart = e.getPoint(); 
    106                                 mousePos = e.getPoint(); 
    107                         } 
    108                 } 
    109         } 
    110  
    111         public void mouseReleased(MouseEvent e) { 
    112                 switch (state) { 
    113                 case ZOOM: 
    114                         if (e.getButton() == BUTTON1) { 
    115                                 state = State.NOTHING; 
    116                                 paintZoomRect(false); 
    117                                 Rectangle r = calculateZoomableRectangle(); 
    118                                 mousePosStart = null; 
    119                                 // zoom to the rectangle 
    120                                 if (r != null) { 
    121                                         double scale = mv.getScale() * r.getWidth()/mv.getWidth(); 
    122                                         GeoPoint newCenter = mv.getPoint(r.x+r.width/2, r.y+r.height/2, false); 
    123                                         mv.zoomTo(newCenter, scale); 
    124                                 } 
    125                         } 
    126                         break; 
    127                 case MOVE: 
    128                         if (e.getButton() == BUTTON3) { 
    129                                 state = State.NOTHING; 
    130                                 mousePosMove = null; 
    131                                 mv.setCursor(Cursor.getDefaultCursor()); 
    132                         } 
    133                         break; 
    134                 case ZOOM_POS: 
    135                         switch (e.getButton()) { 
    136                         case BUTTON1: 
    137                                 state = State.MOVE; 
    138                                 paintZoomRect(false); 
    139                                 mousePosStart = null; 
    140                                 mousePos = null; 
    141                                 mousePosMove = mv.getPoint(e.getX(), e.getY(), false); 
    142                                 mv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
    143                                 break; 
    144                         case BUTTON3: 
    145                                 state = State.ZOOM; 
    146                                 break; 
    147                         } 
    148                 } 
    149         } 
    150  
    151         public void mouseDragged(MouseEvent e) { 
    152                 switch (state) { 
    153                 case MOVE: 
    154                         if (mousePosMove == null) { 
    155                                 mousePosMove = mv.getCenter(); 
    156                                 mv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
    157                         } 
    158                         GeoPoint center = mv.getCenter(); 
    159                         GeoPoint mouseCenter = mv.getPoint(e.getX(), e.getY(), false); 
    160                         GeoPoint p = new GeoPoint(); 
    161                         p.x = mousePosMove.x + center.x - mouseCenter.x;   
    162                         p.y = mousePosMove.y + center.y - mouseCenter.y;   
    163                         mv.zoomTo(p, mv.getScale()); 
    164                         break; 
    165                 case ZOOM: 
    166                         if (mousePosStart == null) 
    167                                 mousePosStart = e.getPoint(); 
    168                         if (mousePos == null) 
    169                                 mousePos = e.getPoint(); 
    170                         paintZoomRect(false); 
    171                         mousePos = e.getPoint(); 
    172                         paintZoomRect(true); 
    173                         break; 
    174                 case ZOOM_POS: 
    175                         if (mousePosStart == null) 
    176                                 mousePosStart = e.getPoint(); 
    177                         if (mousePos == null) 
    178                                 mousePos = e.getPoint(); 
    179                         paintZoomRect(false); 
    180                         mousePosStart.x += e.getX()-mousePos.x; 
    181                         mousePosStart.y += e.getY()-mousePos.y; 
    182                         mousePos = e.getPoint(); 
    183                         paintZoomRect(true); 
    184                         break; 
    185                 } 
    186         } 
    187  
    188         public void mouseWheelMoved(MouseWheelEvent e) { 
    189                 boolean zoomRect = zoomRectVisible; 
    190                 paintZoomRect(false); 
    191                 double zoom = Math.max(0.1, 1 + e.getWheelRotation()/5.0); 
    192                 mv.zoomTo(mv.getCenter(), mv.getScale()*zoom); 
    193                 paintZoomRect(zoomRect); 
    194         } 
    195  
    196         /** 
    197          * Calculate the zoomable rectangle between mousePos and mousePosStart. 
    198          * Zoomable is the rectangle with a fix point at mousePosStart and the 
    199          * correct aspect ratio that fits into the current mapView's size. 
    200          * @return Rectangle which should be used to zoom. 
    201          */ 
    202         private Rectangle calculateZoomableRectangle() { 
    203                 if (mousePosStart == null || mousePos == null || mousePosStart == mousePos) 
    204                         return null; 
    205                 Rectangle r = new Rectangle(); 
    206                 r.x = mousePosStart.x; 
    207                 r.y = mousePosStart.y; 
    208                 r.width = mousePos.x - mousePosStart.x; 
    209                 r.height = mousePos.y - mousePosStart.y; 
    210                 if (r.width < 0) { 
    211                         r.x += r.width; 
    212                         r.width = -r.width; 
    213                 } 
    214                 if (r.height < 0) { 
    215                         r.y += r.height; 
    216                         r.height = -r.height; 
    217                 } 
    218                  
    219                 // keep the aspect ration by shrinking the rectangle 
    220                 double aspectRatio = (double)mv.getWidth()/mv.getHeight(); 
    221                 if ((double)r.width/r.height > aspectRatio) 
    222                         r.width = (int)(r.height*aspectRatio); 
    223                 else 
    224                         r.height = (int)(r.width/aspectRatio); 
    225  
    226                 return r; 
    227         } 
    228          
    229         /** 
    230          * Paint the mouse selection rectangle XOR'ed over the display. 
    231          * 
    232          * @param drawVisible True, to draw the rectangle or false to erase it. 
    233          */ 
    234         private void paintZoomRect(boolean drawVisible) { 
    235                 Rectangle r = calculateZoomableRectangle(); 
    236                 if (r != null && drawVisible != zoomRectVisible) { 
    237                         Graphics g = mv.getGraphics(); 
    238                         g.setColor(Color.BLACK); 
    239                         g.setXORMode(Color.WHITE); 
    240                         g.drawRect(r.x,r.y,r.width,r.height); 
    241                         zoomRectVisible = !zoomRectVisible; 
    242                 } 
     47        public void selectionEnded(Rectangle r, int modifier) { 
     48                double scale = mv.getScale() * r.getWidth()/mv.getWidth(); 
     49                GeoPoint newCenter = mv.getPoint(r.x+r.width/2, r.y+r.height/2, false); 
     50                mv.zoomTo(newCenter, scale); 
    24351        } 
    24452 
    24553        public void registerListener(MapView mapView) { 
    246                 mapView.addMouseListener(this); 
    247                 mapView.addMouseMotionListener(this); 
    248                 mapView.addMouseWheelListener(this); 
     54                selectionManager.register(mapView); 
    24955        } 
    25056 
    25157        public void unregisterListener(MapView mapView) { 
    252                 mapView.removeMouseListener(this); 
    253                 mapView.removeMouseMotionListener(this); 
    254                 mapView.removeMouseWheelListener(this); 
     58                selectionManager.unregister(mapView); 
    25559        } 
    256          
    257         /** 
    258          * Does nothing. Only to satisfy MouseListener 
    259          */ 
    260         public void mouseClicked(MouseEvent e) {} 
    261         /** 
    262          * Does nothing. Only to satisfy MouseListener 
    263          */ 
    264         public void mouseEntered(MouseEvent e) {} 
    265         /** 
    266          * Does nothing. Only to satisfy MouseListener 
    267          */ 
    268         public void mouseExited(MouseEvent e) {} 
    26960} 
  • src/org/openstreetmap/josm/data/osm/DataSet.java

    r1 r2  
    11package org.openstreetmap.josm.data.osm; 
    22 
     3import java.util.Collection; 
    34import java.util.List; 
    45 
     
    1617 */ 
    1718public class DataSet implements Cloneable { 
    18  
    19         /** 
    20          * human readable name of the data. 
    21          */ 
    22         public String name; 
    23  
    24         /** 
    25          * human readable description of what the data is about. Space for comments. 
    26          */ 
    27         public String desc; 
    28  
    29         /** 
    30          * Who recorded/created it. 
    31          */ 
    32         public String author; 
    3319 
    3420        /** 
     
    10995        } 
    11096 
     97        /** 
     98         * Remove the selection of the whole dataset. 
     99         */ 
     100        public void clearSelection() { 
     101                clearSelection(allNodes); 
     102                clearSelection(tracks); 
     103                for (Track t : tracks) 
     104                        clearSelection(t.segments); 
     105        } 
     106         
     107        /** 
     108         * Remove the selection from every value in the collection. 
     109         * @param list The collection to remove the selection from. 
     110         */ 
     111        private void clearSelection(Collection<? extends OsmPrimitive> list) { 
     112                if (list == null) 
     113                        return; 
     114                for (OsmPrimitive osm : list) { 
     115                        osm.selected = false; 
     116                        if (osm.keys != null) 
     117                                clearSelection(osm.keys.keySet()); 
     118                } 
     119        } 
     120         
    111121        public DataSet clone() { 
    112122                try {return (DataSet)super.clone();} catch (CloneNotSupportedException e) {} 
  • src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r1 r2  
    1616         */ 
    1717        public Map<Key, String> keys; 
     18         
     19        /** 
     20         * If set to true, this object has been modified in the current session. 
     21         */ 
     22        public boolean modified = false; 
     23         
     24        /** 
     25         * If set to true, this object is currently selected. 
     26         */ 
     27        public boolean selected = false; 
    1828} 
  • src/org/openstreetmap/josm/gui/MapFrame.java

    r1 r2  
    2727import org.openstreetmap.josm.actions.DebugAction; 
    2828import org.openstreetmap.josm.actions.MapMode; 
     29import org.openstreetmap.josm.actions.SelectionAction; 
    2930import org.openstreetmap.josm.actions.ZoomAction; 
    3031import org.openstreetmap.josm.data.Preferences; 
     
    125126                toolBarActions.setFloatable(false); 
    126127                toolBarActions.add(new IconToggleButton(new ZoomAction(this))); 
     128                toolBarActions.add(new IconToggleButton(new SelectionAction(this))); 
    127129                toolBarActions.add(new IconToggleButton(new DebugAction(this))); 
    128130 
  • src/org/openstreetmap/josm/gui/MapView.java

    r1 r2  
    44import java.awt.Component; 
    55import java.awt.Graphics; 
     6import java.awt.Point; 
    67import java.awt.event.ActionEvent; 
    78import java.awt.event.ComponentEvent; 
     
    126127                addComponentListener(this); 
    127128                 
     129                // initialize the movement listener 
     130                new MapMover(this); 
     131                 
    128132                // initialize the projection 
    129133                setProjection(Main.pref.projection.clone()); 
     
    152156                        getProjection().xy2latlon(p); 
    153157                return p; 
     158        } 
     159         
     160        /** 
     161         * Return the point on the screen where this GeoPoint would be. 
     162         * @param point The point, where this geopoint would be drawn. 
     163         * @return The point on screen where "point" would be drawn, relative 
     164         *              to the own top/left. 
     165         */ 
     166        public Point getScreenPoint(GeoPoint point) { 
     167                GeoPoint p; 
     168                if (!Double.isNaN(point.x) && !Double.isNaN(point.y)) 
     169                        p = point; 
     170                else { 
     171                        if (Double.isNaN(point.lat) || Double.isNaN(point.lon)) 
     172                                throw new IllegalArgumentException("point: Either lat/lon or x/y must be set."); 
     173                        p = point.clone(); 
     174                        projection.latlon2xy(p); 
     175                } 
     176                return new Point(toScreenX(p.x), toScreenY(p.y)); 
    154177        } 
    155178         
     
    225248                 
    226249                // draw tracks 
    227                 g.setColor(Color.RED); 
    228250                if (dataSet.tracks != null) 
    229251                        for (Track track : dataSet.tracks) 
    230                                 for (LineSegment ls : track.segments) 
     252                                for (LineSegment ls : track.segments) { 
     253                                        g.setColor(ls.selected ? Color.WHITE : Color.GRAY); 
    231254                                        g.drawLine(toScreenX(ls.start.coor.x), toScreenY(ls.start.coor.y), 
    232255                                                        toScreenX(ls.end.coor.x), toScreenY(ls.end.coor.y)); 
     256                                } 
    233257 
    234258                // draw nodes 
    235                 g.setColor(Color.YELLOW); 
    236259                for (Node w : dataSet.allNodes) { 
     260                        g.setColor(w.selected ? Color.WHITE : Color.RED); 
    237261                        g.drawArc(toScreenX(w.coor.x), toScreenY(w.coor.y), 3, 3, 0, 360); 
    238262                } 
  • src/org/openstreetmap/josm/io/GpxReader.java

    r1 r2  
    6767        private DataSet parseDataSet(Element e) { 
    6868                DataSet data = new DataSet(); 
    69                 // read global information 
    70                 data.author = e.getAttributeValue("creator"); 
    71                 Element metadata = e.getChild("metadata"); 
    72                 if (metadata != null) 
    73                 { 
    74                         data.desc = metadata.getChildText("desc", XSD); 
    75                         data.name = metadata.getChildText("name", XSD); 
    76                 } 
    77                  
    7869                // read waypoints not contained in tracks or areas 
    7970                data.allNodes = new LinkedList<Node>();  
Note: See TracChangeset for help on using the changeset viewer.