Changeset 9 in josm for src/org


Ignore:
Timestamp:
2005-10-04T00:09:32+02:00 (19 years ago)
Author:
imi
Message:
  • added support for DataReaders
  • added a nice status line and a tooltip when holding middle mouse button
Location:
src/org/openstreetmap/josm
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/OpenGpxAction.java

    r7 r9  
    1313import javax.swing.filechooser.FileFilter;
    1414
    15 import org.jdom.JDOMException;
    1615import org.openstreetmap.josm.data.osm.DataSet;
    1716import org.openstreetmap.josm.gui.Main;
    1817import org.openstreetmap.josm.gui.MapFrame;
    1918import org.openstreetmap.josm.io.GpxReader;
     19import org.openstreetmap.josm.io.DataReader.ConnectionException;
     20import org.openstreetmap.josm.io.DataReader.ParseException;
    2021
    2122/**
     
    5354               
    5455                try {
    55                         DataSet dataSet = new GpxReader().parse(new FileReader(gpxFile));
     56                        DataSet dataSet = new GpxReader(new FileReader(gpxFile)).parse();
    5657                        MapFrame map = new MapFrame(dataSet);
    5758                        Main.main.setMapFrame(gpxFile.getName(), map);
    58                 } catch (JDOMException x) {
     59                } catch (ParseException x) {
    5960                        x.printStackTrace();
    60                         JOptionPane.showMessageDialog(Main.main, "Illegal GPX document:\n"+x.getMessage());
     61                        JOptionPane.showMessageDialog(Main.main, x.getMessage());
    6162                } catch (IOException x) {
    6263                        x.printStackTrace();
    63                         JOptionPane.showMessageDialog(Main.main, "Could not read '"+gpxFile.getName()+"':\n"+x.getMessage());
     64                        JOptionPane.showMessageDialog(Main.main, "Could not read '"+gpxFile.getName()+"'\n"+x.getMessage());
     65                } catch (ConnectionException x) {
     66                        x.printStackTrace();
     67                        JOptionPane.showMessageDialog(Main.main, x.getMessage());
    6468                }
    6569        }
  • src/org/openstreetmap/josm/data/GeoPoint.java

    r7 r9  
    4848
    4949        /**
    50          * GeoPoints are equal, if their lat/lon are equal or, if lat or lon are NaN,
    51          * if their x/y are equal.
    52          */
    53         @Override
    54         public boolean equals(Object obj) {
    55                 if (!(obj instanceof GeoPoint))
    56                         return false;
    57                 GeoPoint gp = (GeoPoint)obj;
    58                
    59                 if (Double.isNaN(lat) || Double.isNaN(lon))
    60                         return x == gp.x && y == gp.y;
    61                 return lat == gp.lat && lon == gp.lon;
    62         }
    63 
    64         @Override
    65         public int hashCode() {
    66                 return super.hashCode();
    67         }
    68 
    69         /**
    7050         * Return the squared distance of the northing/easting values between
    7151         * this and the argument.
  • src/org/openstreetmap/josm/data/osm/Key.java

    r8 r9  
    5757        }
    5858
    59         /**
    60          * Keys are equal, when their name is equal, regardless of their other keys.
    61          */
    62         @Override
    63         public boolean equals(Object obj) {
    64                 if (!(obj instanceof Key))
    65                         return false;
    66                 return name.equals(((Key)obj).name);
    67         }
    68 
    69         @Override
    70         public int hashCode() {
    71                 return name.hashCode();
    72         }
    73 
    7459        @Override
    7560        public void visit(Visitor visitor) {
  • src/org/openstreetmap/josm/data/osm/LineSegment.java

    r8 r9  
    8585        }
    8686
    87         /**
    88          * Line segments are equal, if their starting and ending node and their
    89          * keys are equal.
    90          */
    91         @Override
    92         public boolean equals(Object obj) {
    93                 if (!(obj instanceof LineSegment))
    94                         return false;
    95                 return super.equals(obj) &&
    96                         getStart().equals(((LineSegment)obj).getStart()) &&
    97                         getEnd().equals(((LineSegment)obj).getEnd());
    98         }
    99 
    100         @Override
    101         public int hashCode() {
    102                 return super.hashCode() + getStart().hashCode() + getEnd().hashCode();
    103         }
    104 
    10587        @Override
    10688        public void visit(Visitor visitor) {
  • src/org/openstreetmap/josm/data/osm/Node.java

    r8 r9  
    3535       
    3636        /**
    37          * Nodes are equal when their coordinates are equal.
    38          */
    39         @Override
    40         public boolean equals(Object obj) {
    41                 if (!(obj instanceof Node))
    42                         return false;
    43                 Node n = (Node)obj;
    44                 if (coor == null)
    45                         return n.coor == null;
    46                 return coor.equals(n.coor) && super.equals(obj);
    47         }
    48 
    49         /**
    50          * Compute the hashcode from the OsmPrimitive's hash and the coor's hash.
    51          */
    52         @Override
    53         public int hashCode() {
    54                 return (coor == null ? 0 : coor.hashCode()) + super.hashCode();
    55         }
    56 
    57         /**
    5837         * Return a list only this added.
    5938         */
  • src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r8 r9  
    6868       
    6969        /**
    70          * Osm primitives are equal, when their keys are equal.
    71          */
    72         @Override
    73         public boolean equals(Object obj) {
    74                 if (obj == null)
    75                         return false;
    76                 if (!(obj instanceof OsmPrimitive))
    77                         return false;
    78                 OsmPrimitive osm = (OsmPrimitive)obj;
    79                 if (keys == null)
    80                         return osm.keys == null;
    81                 return keys.equals(osm.keys);
    82         }
    83 
    84         /**
    85          * Compute the hashCode from the keys.
    86          */
    87         @Override
    88         public int hashCode() {
    89                 return keys == null ? 0 : keys.hashCode();
    90         }
    91 
    92         /**
    9370         * Mark the primitive as selected or not selected and fires a selection
    9471         * changed later, if the value actualy changed.
  • src/org/openstreetmap/josm/data/osm/Track.java

    r8 r9  
    9090
    9191        /**
    92          * Tracks are equal, when all segments and the keys are equal
    93          */
    94         @Override
    95         public boolean equals(Object obj) {
    96                 if (!(obj instanceof Track))
    97                         return false;
    98                 if (!super.equals(obj))
    99                         return false;
    100                 Track t = (Track)obj;
    101                 int size = segments.size();
    102                 if (size != t.segments.size())
    103                         return false;
    104                 for (int i = 0; i < size; ++i)
    105                         if (!segments.get(i).equals(t.segments.get(i)))
    106                                 return false;
    107                 return true;
    108         }
    109 
    110         @Override
    111         public int hashCode() {
    112                 int hash = super.hashCode();
    113                 for (LineSegment ls : segments)
    114                         hash += ls.hashCode();
    115                 return hash;
    116         }
    117 
    118         /**
    11992         * Return the last node in the track. This is the node, which no line segment
    12093         * has as start, but at least one has it as end. If there are not exact one
  • src/org/openstreetmap/josm/gui/Main.java

    r7 r9  
    142142                panel.removeAll();
    143143                panel.add(mapFrame, BorderLayout.CENTER);
    144                 panel.add(mapFrame.getToolBarActions(), BorderLayout.WEST);
     144                panel.add(mapFrame.toolBarActions, BorderLayout.WEST);
     145                panel.add(mapFrame.statusLine, BorderLayout.SOUTH);
    145146                panel.setVisible(true);
    146147        }
  • src/org/openstreetmap/josm/gui/MapFrame.java

    r8 r9  
    4747         * The toolbar with the action icons
    4848         */
    49         private JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
     49        public JToolBar toolBarActions = new JToolBar(JToolBar.VERTICAL);
     50        /**
     51         * The status line below the map
     52         */
     53        public MapStatus statusLine;
    5054
    5155        /**
     
    7781                toolGroup.setSelected(((AbstractButton)toolBarActions.getComponent(0)).getModel(), true);
    7882                selectMapMode((MapMode)((AbstractButton)toolBarActions.getComponent(0)).getAction());
    79                
     83
    8084                // autoScale
    8185                toolBarActions.addSeparator();
     
    9397                // properties
    9498                toolBarActions.add(new IconToggleButton(this, new PropertiesDialog(this)));
    95                
     99
    96100                // selection dialog
    97101                SelectionListDialog selectionList = new SelectionListDialog(dataSet);
     
    104108                });
    105109                toolBarActions.add(buttonSelection);
     110
     111                // status line below the map
     112                statusLine = new MapStatus(mapView);
    106113        }
    107114
     
    117124                mapMode.registerListener();
    118125        }
    119 
    120         /**
    121          * @return Returns the toolBarActions.
    122          */
    123         public JToolBar getToolBarActions() {
    124                 return toolBarActions;
    125         }
    126126}
  • src/org/openstreetmap/josm/gui/MapView.java

    r8 r9  
    8484       
    8585        /**
    86          * Construct a MapView and attach it to a frame.
     86         * Construct a MapView.
    8787         */
    8888        public MapView(DataSet dataSet) {
    8989                this.dataSet = dataSet;
    9090                addComponentListener(this);
    91                
     91
    9292                // initialize the movement listener
    9393                new MapMover(this);
    94                
     94
    9595                // initialize the projection
    9696                setProjection(Main.pref.projection.clone());
    97                
    98                 // initialize the engine
     97
     98                // initialize the drawing engine
    9999                engine = new SimpleEngine(this);
    100100        }
     
    222222        }
    223223
     224       
    224225        /**
    225226         * Zoom to the given coordinate.
     
    247248       
    248249        /**
     250         * Draw the component.
     251         */
     252        @Override
     253        public void paint(Graphics g) {
     254                engine.init(g);
     255                engine.drawBackground(getPoint(0,0,true), getPoint(getWidth(), getHeight(), true));
     256
     257                for (Track t : dataSet.tracks())
     258                        engine.drawTrack(t);
     259                for (LineSegment ls : dataSet.pendingLineSegments())
     260                        engine.drawPendingLineSegment(ls);
     261                for (Node n : dataSet.nodes)
     262                        engine.drawNode(n);
     263        }
     264
     265        /**
     266         * Notify from the projection, that something has changed.
     267         * @param e
     268         */
     269        public void stateChanged(ChangeEvent e) {
     270                projection.init(dataSet);
     271                recalculateCenterScale();
     272        }
     273
     274        /**
     275         * Set a new projection method. This call is not cheap, as it will
     276         * transform the whole underlying dataSet and repaint itself.
     277         *
     278         * @param projection The new projection method to set.
     279         */
     280        public void setProjection(Projection projection) {
     281                if (projection == this.projection)
     282                        return;
     283
     284                Projection oldProjection = this.projection;
     285               
     286                if (this.projection != null)
     287                        this.projection.removeChangeListener(this);
     288                this.projection = projection;
     289                projection.addChangeListener(this);
     290               
     291                stateChanged(new ChangeEvent(this));
     292                firePropertyChange("projection", oldProjection, projection);
     293        }
     294
     295        /**
     296         * Return the projection method for this map view.
     297         * @return The projection method.
     298         */
     299        public Projection getProjection() {
     300                return projection;
     301        }
     302
     303        /**
    249304         * Return the current scale value.
    250305         * @return The scale value currently used in display
     
    254309        }
    255310
     311        /**
     312         * @return Returns the autoScale.
     313         */
     314        public boolean isAutoScale() {
     315                return autoScale;
     316        }
     317
     318        /**
     319         * @param autoScale The autoScale to set.
     320         */
     321        public void setAutoScale(boolean autoScale) {
     322                if (this.autoScale != autoScale) {
     323                        this.autoScale = autoScale;
     324                        firePropertyChange("autoScale", !autoScale, autoScale);
     325                }
     326        }
     327        /**
     328         * @return Returns the center point. A copy is returned, so users cannot
     329         *              change the center by accessing the return value. Use zoomTo instead.
     330         */
     331        public GeoPoint getCenter() {
     332                return center.clone();
     333        }
     334
     335       
     336       
    256337        /**
    257338         * Set the new dimension to the projection class. Also adjust the components
     
    285366                                scale = Math.max(scaleX, scaleY); // minimum scale to see all of the screen
    286367                        }
    287 
     368       
    288369                        firePropertyChange("center", oldCenter, center);
    289370                        if (oldAutoScale != autoScale)
     
    303384
    304385        /**
    305          * Draw the component.
    306          */
    307         @Override
    308         public void paint(Graphics g) {
    309                 engine.init(g);
    310                 engine.drawBackground(getPoint(0,0,true), getPoint(getWidth(), getHeight(), true));
    311 
    312                 for (Track t : dataSet.tracks())
    313                         engine.drawTrack(t);
    314                 for (LineSegment ls : dataSet.pendingLineSegments())
    315                         engine.drawPendingLineSegment(ls);
    316                 for (Node n : dataSet.nodes)
    317                         engine.drawNode(n);
    318         }
    319 
    320         /**
    321          * Notify from the projection, that something has changed.
    322          * @param e
    323          */
    324         public void stateChanged(ChangeEvent e) {
    325                 projection.init(dataSet);
    326                 recalculateCenterScale();
    327         }
    328 
    329         /**
    330          * Set a new projection method. This call is not cheap, as it will
    331          * transform the whole underlying dataSet and repaint itself.
    332          *
    333          * @param projection The new projection method to set.
    334          */
    335         public void setProjection(Projection projection) {
    336                 if (projection == this.projection)
    337                         return;
    338 
    339                 Projection oldProjection = this.projection;
    340                
    341                 if (this.projection != null)
    342                         this.projection.removeChangeListener(this);
    343                 this.projection = projection;
    344                 projection.addChangeListener(this);
    345                
    346                 stateChanged(new ChangeEvent(this));
    347                 firePropertyChange("projection", oldProjection, projection);
    348         }
    349 
    350         /**
    351          * Return the projection method for this map view.
    352          * @return The projection method.
    353          */
    354         public Projection getProjection() {
    355                 return projection;
    356         }
    357 
    358         /**
    359          * @return Returns the autoScale.
    360          */
    361         public boolean isAutoScale() {
    362                 return autoScale;
    363         }
    364 
    365         /**
    366          * @param autoScale The autoScale to set.
    367          */
    368         public void setAutoScale(boolean autoScale) {
    369                 if (this.autoScale != autoScale) {
    370                         this.autoScale = autoScale;
    371                         firePropertyChange("autoScale", !autoScale, autoScale);
    372                 }
    373         }
    374         /**
    375          * @return Returns the center point. A copy is returned, so users cannot
    376          *              change the center by accessing the return value. Use zoomTo instead.
    377          */
    378         public GeoPoint getCenter() {
    379                 return center.clone();
    380         }
    381 
    382         /**
    383386         * Does nothing. Just to satisfy ComponentListener.
    384387         */
  • src/org/openstreetmap/josm/io/GpxReader.java

    r8 r9  
    33import java.io.IOException;
    44import java.io.Reader;
     5import java.util.HashMap;
    56
    67import org.jdom.Element;
     
    1011import org.openstreetmap.josm.data.GeoPoint;
    1112import org.openstreetmap.josm.data.osm.DataSet;
     13import org.openstreetmap.josm.data.osm.Key;
    1214import org.openstreetmap.josm.data.osm.LineSegment;
    1315import org.openstreetmap.josm.data.osm.Node;
     16import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1417import org.openstreetmap.josm.data.osm.Track;
    1518import org.openstreetmap.josm.gui.Main;
    1619
    1720/**
    18  * Reads an gpx stream and construct a DataSet out of it. Some information may not be
    19  * imported, since JOSM does not fully support GPX.
     21 * Reads an gpx stream and construct a DataSet out of it.
     22 * Some information may not be imported, but GpxReader tries its best to load
     23 * all data possible in the key/value structure.
    2024 *
    2125 * @author imi
    2226 */
    23 public class GpxReader {
    24 
    25         public static final Namespace XSD = Namespace.getNamespace("http://www.w3.org/2001/XMLSchema");
    26         public static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
     27public class GpxReader implements DataReader {
     28
     29        /**
     30         * The GPX namespace used.
     31         */
     32        private static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
     33        /**
     34         * The OSM namespace used (for extensions).
     35         */
     36        private static final Namespace OSM = Namespace.getNamespace("osm");
     37
     38        /**
     39         * The data source from this reader.
     40         */
     41        public Reader source;
    2742       
    2843        /**
     44         * Construct a parser from a specific data source.
     45         * @param source The data source, as example a FileReader to read from a file.
     46         */
     47        public GpxReader(Reader source) {
     48                this.source = source;
     49        }
     50       
     51        /**
    2952         * Read the input stream and return a DataSet from the stream.
    30          *
    31          * @param in
    32          * @throws IOException          An error with the provided stream occoured.
    33          * @throws JDOMException        An parse error occoured.
    34          */
    35         public DataSet parse(Reader in) throws JDOMException, IOException {
     53         */
     54        public DataSet parse() throws ParseException, ConnectionException {
    3655                try {
    3756                        final SAXBuilder builder = new SAXBuilder();
    38                         Element root = builder.build(in).getRootElement();
     57                        Element root = builder.build(source).getRootElement();
    3958                        return parseDataSet(root);
    4059                } catch (NullPointerException npe) {
    41                         throw new JDOMException("NullPointerException. Probably a tag name mismatch.", npe);
     60                        throw new ParseException("NullPointerException. Probably a tag name mismatch.", npe);
    4261                } catch (ClassCastException cce) {
    43                         throw new JDOMException("ClassCastException. Probably a tag does not contain the correct type.", cce);
     62                        throw new ParseException("ClassCastException. Probably a tag does not contain the correct type.", cce);
     63                } catch (JDOMException e) {
     64                        throw new ParseException("The data could not be parsed. Reason: "+e.getMessage(), e);
     65                } catch (IOException e) {
     66                        throw new ConnectionException("The data could not be retrieved. Reason: "+e.getMessage(), e);
    4467                }
    4568        }
     
    5679                        Float.parseFloat(e.getAttributeValue("lat")),
    5780                        Float.parseFloat(e.getAttributeValue("lon")));
     81                for (Object o : e.getChildren()) {
     82                        Element child = (Element)o;
     83                        if (child.getName().equals("extensions"))
     84                                parseKeyValueExtensions(data, child);
     85                        else if (child.getName().equals("link"))
     86                                parseKeyValueLink(data, child);
     87                        else
     88                                parseKeyValueTag(data, child);
     89                }
    5890                return data;
     91        }
     92
     93        /**
     94         * Parse the extensions tag and add all properties found as key/value.
     95         * <code>osm.keys</code> may be <code>null</code>, in which case it is
     96         * created first. If <code>e</code> is <code>null</code>, nothing
     97         * happens.
     98         *
     99         * @param osm   The primitive to store the properties.
     100         * @param e             The extensions element to read the properties from.
     101         */
     102        private void parseKeyValueExtensions(OsmPrimitive osm, Element e) {
     103                if (e != null) {
     104                        if (osm.keys == null)
     105                                osm.keys = new HashMap<Key, String>();
     106                        for (Object o : e.getChildren("property", OSM)) {
     107                                Element child = (Element)o;
     108                                Key key = Key.get(child.getAttributeValue("name"));
     109                                osm.keys.put(key, child.getAttributeValue("value"));
     110                        }
     111                }
     112        }
     113
     114        /**
     115         * If the element is not <code>null</code>, read the data from it and put
     116         * it as the key with the name of the elements name in the given object.
     117         *
     118         * The <code>keys</code> - field of the element could be <code>null</code>,
     119         * in which case it is created first.
     120         *
     121         * @param osm     The osm primitive to put the key into.
     122         * @param e               The element to look for data.
     123         */
     124        private void parseKeyValueTag(OsmPrimitive osm, Element e) {
     125                if (e != null) {
     126                        if (osm.keys == null)
     127                                osm.keys = new HashMap<Key, String>();
     128                        osm.keys.put(Key.get(e.getName()), e.getValue());
     129                }
     130        }
     131
     132        /**
     133         * Parse the GPX linkType data information and store it as value in the
     134         * primitives <i>link</i> key. <code>osm.keys</code> may be
     135         * <code>null</code>, in which case it is created first. If
     136         * <code>e</code> is <code>null</code>, nothing happens.
     137         *
     138         * The format stored is: mimetype;url
     139         * Example: text/html;http://www.openstreetmap.org
     140         * @param osm   The osm primitive to store the data in.
     141         * @param e             The element in gpx:linkType - format.
     142         */
     143        private void parseKeyValueLink(Node osm, Element e) {
     144                if (e != null) {
     145                        if (osm.keys == null)
     146                                osm.keys = new HashMap<Key, String>();
     147                        String link = e.getChildText("type") + ";" + e.getChildText("text");
     148                        osm.keys.put(Key.get("link"), link);
     149                }
    59150        }
    60151
     
    105196                if (Main.pref.mergeNodes)
    106197                        for (Node n : data.nodes)
    107                                 if (node.equals(n))
     198                                if (node.coor.lat == n.coor.lat && node.coor.lon == n.coor.lon)
    108199                                        return n;
    109200                data.nodes.add(node);
Note: See TracChangeset for help on using the changeset viewer.