Changeset 42 in josm


Ignore:
Timestamp:
2006-01-21T19:02:06+01:00 (18 years ago)
Author:
imi
Message:

fixed fancy download world chooser

Files:
8 edited
1 moved

Legend:

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

    r41 r42  
    110110                        }
    111111                });
     112                wc.addListMarker(bookmarks);
     113                wc.addLatLonInputField(latlon);
    112114                dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
    113115
  • src/org/openstreetmap/josm/data/Preferences.java

    r41 r42  
    1818import org.jdom.output.Format;
    1919import org.jdom.output.XMLOutputter;
    20 import org.openstreetmap.josm.data.projection.LatitudeLongitude;
     20import org.openstreetmap.josm.data.projection.Epsg4263;
    2121import org.openstreetmap.josm.data.projection.Mercator;
    2222import org.openstreetmap.josm.data.projection.Projection;
     
    8080                new Mercator(),
    8181                new UTM(),
    82                 new LatitudeLongitude()
     82                new Epsg4263()
    8383        };
    8484
  • src/org/openstreetmap/josm/data/projection/Epsg4263.java

    r41 r42  
    1010 * @author imi
    1111 */
    12 public class LatitudeLongitude extends Projection {
     12public class Epsg4263 extends Projection {
    1313
    1414        @Override
     
    2626        @Override
    2727        public String toString() {
    28                 return "Latitude/Longitude";
     28                return "EPSG:4263";
    2929        }
    3030
  • src/org/openstreetmap/josm/gui/MapMover.java

    r41 r42  
    3232        private Cursor oldCursor;
    3333
     34       
    3435        /**
    3536         * Create a new MapMover
    36          * @param mapView The map that should be moved.
    3737         */
    38         public MapMover(NavigatableComponent mapView) {
    39                 this.nc = mapView;
     38        MapMover(NavigatableComponent navComp) {
     39                this.nc = navComp;
    4040                nc.addMouseListener(this);
    4141                nc.addMouseMotionListener(this);
  • src/org/openstreetmap/josm/gui/MapView.java

    r41 r42  
    1111import java.util.Collection;
    1212import java.util.Collections;
    13 import java.util.HashSet;
    1413import java.util.LinkedList;
    1514
     
    2120import org.openstreetmap.josm.data.GeoPoint;
    2221import org.openstreetmap.josm.data.osm.DataSet;
    23 import org.openstreetmap.josm.data.osm.LineSegment;
    2422import org.openstreetmap.josm.data.osm.Node;
    25 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    26 import org.openstreetmap.josm.data.osm.Track;
    2723import org.openstreetmap.josm.data.projection.Projection;
    2824import org.openstreetmap.josm.gui.layer.Layer;
     
    10399                // initialize the projection if it is the first layer
    104100                if (layers.isEmpty())
    105                         Main.pref.getProjection().init(layer.getBoundsLatLon());
     101                        getProjection().init(layer.getBoundsLatLon());
    106102
    107103                // reinitialize layer's data
    108                 layer.init(Main.pref.getProjection());
     104                layer.init(getProjection());
    109105
    110106                if (layer instanceof OsmDataLayer) {
     
    161157        }
    162158
    163         /**
    164          * Return the object, that is nearest to the given screen point.
    165          *
    166          * First, a node will be searched. If a node within 10 pixel is found, the
    167          * nearest node is returned.
    168          *
    169          * If no node is found, search for pending line segments.
    170          *
    171          * If no such line segment is found, and a non-pending line segment is
    172          * within 10 pixel to p, this segment is returned, except when
    173          * <code>wholeTrack</code> is <code>true</code>, in which case the
    174          * corresponding Track is returned.
    175          *
    176          * If no line segment is found and the point is within an area, return that
    177          * area.
    178          *
    179          * If no area is found, return <code>null</code>.
    180          *
    181          * @param p                              The point on screen.
    182          * @param lsInsteadTrack Whether the line segment (true) or only the whole
    183          *                                               track should be returned.
    184          * @return      The primitive, that is nearest to the point p.
    185          */
    186         public OsmPrimitive getNearest(Point p, boolean lsInsteadTrack) {
    187                 double minDistanceSq = Double.MAX_VALUE;
    188                 OsmPrimitive minPrimitive = null;
    189 
    190                 // nodes
    191                 for (Node n : Main.main.ds.nodes) {
    192                         if (n.isDeleted())
    193                                 continue;
    194                         Point sp = getScreenPoint(n.coor);
    195                         double dist = p.distanceSq(sp);
    196                         if (minDistanceSq > dist && dist < 100) {
    197                                 minDistanceSq = p.distanceSq(sp);
    198                                 minPrimitive = n;
    199                         }
    200                 }
    201                 if (minPrimitive != null)
    202                         return minPrimitive;
    203                
    204                 // for whole tracks, try the tracks first
    205                 minDistanceSq = Double.MAX_VALUE;
    206                 if (!lsInsteadTrack) {
    207                         for (Track t : Main.main.ds.tracks) {
    208                                 if (t.isDeleted())
    209                                         continue;
    210                                 for (LineSegment ls : t.segments) {
    211                                         if (ls.isDeleted())
    212                                                 continue;
    213                                         Point A = getScreenPoint(ls.start.coor);
    214                                         Point B = getScreenPoint(ls.end.coor);
    215                                         double c = A.distanceSq(B);
    216                                         double a = p.distanceSq(B);
    217                                         double b = p.distanceSq(A);
    218                                         double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared
    219                                         if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {
    220                                                 minDistanceSq = perDist;
    221                                                 minPrimitive = t;
    222                                         }
    223                                 }                       
    224                         }
    225                         if (minPrimitive != null)
    226                                 return minPrimitive;
    227                 }
    228                
    229                 minDistanceSq = Double.MAX_VALUE;
    230                 // line segments
    231                 for (LineSegment ls : Main.main.ds.lineSegments) {
    232                         if (ls.isDeleted())
    233                                 continue;
    234                         Point A = getScreenPoint(ls.start.coor);
    235                         Point B = getScreenPoint(ls.end.coor);
    236                         double c = A.distanceSq(B);
    237                         double a = p.distanceSq(B);
    238                         double b = p.distanceSq(A);
    239                         double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared
    240                         if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {
    241                                 minDistanceSq = perDist;
    242                                 minPrimitive = ls;
    243                         }
    244                 }
    245 
    246                 return minPrimitive;
    247         }
    248 
    249         /**
    250          * @return A list of all objects that are nearest to
    251          * the mouse. To do this, first the nearest object is
    252          * determined.
    253          *
    254          * If its a node, return all line segments and
    255          * streets the node is part of, as well as all nodes
    256          * (with their line segments and tracks) with the same
    257          * location.
    258          *
    259          * If its a line segment, return all tracks this segment
    260          * belongs to as well as all line segments that are between
    261          * the same nodes (in both direction) with all their tracks.
    262          *
    263          * @return A collection of all items or <code>null</code>
    264          *              if no item under or near the point. The returned
    265          *              list is never empty.
    266          */
    267         public Collection<OsmPrimitive> getAllNearest(Point p) {
    268                 OsmPrimitive osm = getNearest(p, true);
    269                 if (osm == null)
    270                         return null;
    271                 Collection<OsmPrimitive> c = new HashSet<OsmPrimitive>();
    272                 c.add(osm);
    273                 if (osm instanceof Node) {
    274                         Node node = (Node)osm;
    275                         for (Node n : Main.main.ds.nodes)
    276                                 if (!n.isDeleted() && n.coor.equalsLatLon(node.coor))
    277                                         c.add(n);
    278                         for (LineSegment ls : Main.main.ds.lineSegments)
    279                                 // line segments never match nodes, so they are skipped by contains
    280                                 if (!ls.isDeleted() && (c.contains(ls.start) || c.contains(ls.end)))
    281                                         c.add(ls);
    282                 }
    283                 if (osm instanceof LineSegment) {
    284                         LineSegment line = (LineSegment)osm;
    285                         for (LineSegment ls : Main.main.ds.lineSegments)
    286                                 if (!ls.isDeleted() && ls.equalPlace(line))
    287                                         c.add(ls);
    288                 }
    289                 if (osm instanceof Node || osm instanceof LineSegment) {
    290                         for (Track t : Main.main.ds.tracks) {
    291                                 if (t.isDeleted())
    292                                         continue;
    293                                 for (LineSegment ls : t.segments) {
    294                                         if (!ls.isDeleted() && c.contains(ls)) {
    295                                                 c.add(t);
    296                                                 break;
    297                                         }
    298                                 }
    299                         }
    300                 }
    301                 return c;
    302         }
    303        
    304159        /**
    305160         * Draw the component.
     
    378233                                // no bounds means standard scale and center
    379234                                center = new GeoPoint(51.526447, -0.14746371);
    380                                 Main.pref.getProjection().latlon2xy(center);
     235                                getProjection().latlon2xy(center);
    381236                                scale = 10;
    382237                        } else {
    383238                                center = bounds.centerXY();
    384                                 Main.pref.getProjection().xy2latlon(center);
     239                                getProjection().xy2latlon(center);
    385240                                double scaleX = (bounds.max.x-bounds.min.x)/w;
    386241                                double scaleY = (bounds.max.y-bounds.min.y)/h;
     
    483338        public void stateChanged(ChangeEvent e) {
    484339                // reset all datasets.
    485                 Projection p = Main.pref.getProjection();
     340                Projection p = getProjection();
    486341                for (Node n : Main.main.ds.nodes)
    487342                        p.latlon2xy(n.coor);
  • src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r41 r42  
    22
    33import java.awt.Point;
     4import java.util.Collection;
     5import java.util.HashSet;
    46
    57import javax.swing.JComponent;
     
    79import org.openstreetmap.josm.Main;
    810import org.openstreetmap.josm.data.GeoPoint;
     11import org.openstreetmap.josm.data.osm.LineSegment;
     12import org.openstreetmap.josm.data.osm.Node;
     13import org.openstreetmap.josm.data.osm.OsmPrimitive;
     14import org.openstreetmap.josm.data.osm.Track;
     15import org.openstreetmap.josm.data.projection.Projection;
    916
    1017/**
     
    1724
    1825        /**
    19          * The scale factor in meter per pixel.
     26         * The scale factor in x or y-units per pixel. This means, if scale = 10,
     27         * every physical pixel on screen are 10 x or 10 y units in the
     28         * northing/easting space of the projection.
    2029         */
    2130        protected double scale;
     
    5867        public GeoPoint getPoint(int x, int y, boolean latlon) {
    5968                GeoPoint p = new GeoPoint();
    60                 p.x = (x - getWidth()/2.0)*scale + center.x;
    61                 p.y = (getHeight()/2.0 - y)*scale + center.y;
     69                p.x = center.x + (x - getWidth()/2.0)*scale;
     70                p.y = center.y - (y - getHeight()/2.0)*scale;
    6271                if (latlon)
    63                         Main.pref.getProjection().xy2latlon(p);
     72                        getProjection().xy2latlon(p);
    6473                return p;
    6574        }
     
    7988                                throw new IllegalArgumentException("point: Either lat/lon or x/y must be set.");
    8089                        p = point.clone();
    81                         Main.pref.getProjection().latlon2xy(p);
    82                 }
    83                 int x = ((int)Math.round((p.x-center.x) / scale + getWidth()/2));
    84                 int y = ((int)Math.round((center.y-p.y) / scale + getHeight()/2));
    85                 return new Point(x,y);
     90                        getProjection().latlon2xy(p);
     91                }
     92                double x = (p.x-center.x)/scale + getWidth()/2;
     93                double y = (center.y-p.y)/scale + getHeight()/2;
     94                return new Point((int)x,(int)y);
    8695        }
    8796
     
    93102         */
    94103        public void zoomTo(GeoPoint newCenter, double scale) {
    95                 System.out.println(scale);
    96104                center = newCenter.clone();
    97                 Main.pref.getProjection().xy2latlon(center);
     105                getProjection().xy2latlon(center);
    98106                this.scale = scale;
    99107                repaint();
    100108        }
     109
     110        /**
     111         * Return the object, that is nearest to the given screen point.
     112         *
     113         * First, a node will be searched. If a node within 10 pixel is found, the
     114         * nearest node is returned.
     115         *
     116         * If no node is found, search for pending line segments.
     117         *
     118         * If no such line segment is found, and a non-pending line segment is
     119         * within 10 pixel to p, this segment is returned, except when
     120         * <code>wholeTrack</code> is <code>true</code>, in which case the
     121         * corresponding Track is returned.
     122         *
     123         * If no line segment is found and the point is within an area, return that
     124         * area.
     125         *
     126         * If no area is found, return <code>null</code>.
     127         *
     128         * @param p                              The point on screen.
     129         * @param lsInsteadTrack Whether the line segment (true) or only the whole
     130         *                                               track should be returned.
     131         * @return      The primitive, that is nearest to the point p.
     132         */
     133        public OsmPrimitive getNearest(Point p, boolean lsInsteadTrack) {
     134                double minDistanceSq = Double.MAX_VALUE;
     135                OsmPrimitive minPrimitive = null;
     136       
     137                // nodes
     138                for (Node n : Main.main.ds.nodes) {
     139                        if (n.isDeleted())
     140                                continue;
     141                        Point sp = getScreenPoint(n.coor);
     142                        double dist = p.distanceSq(sp);
     143                        if (minDistanceSq > dist && dist < 100) {
     144                                minDistanceSq = p.distanceSq(sp);
     145                                minPrimitive = n;
     146                        }
     147                }
     148                if (minPrimitive != null)
     149                        return minPrimitive;
     150               
     151                // for whole tracks, try the tracks first
     152                minDistanceSq = Double.MAX_VALUE;
     153                if (!lsInsteadTrack) {
     154                        for (Track t : Main.main.ds.tracks) {
     155                                if (t.isDeleted())
     156                                        continue;
     157                                for (LineSegment ls : t.segments) {
     158                                        if (ls.isDeleted())
     159                                                continue;
     160                                        Point A = getScreenPoint(ls.start.coor);
     161                                        Point B = getScreenPoint(ls.end.coor);
     162                                        double c = A.distanceSq(B);
     163                                        double a = p.distanceSq(B);
     164                                        double b = p.distanceSq(A);
     165                                        double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared
     166                                        if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {
     167                                                minDistanceSq = perDist;
     168                                                minPrimitive = t;
     169                                        }
     170                                }                       
     171                        }
     172                        if (minPrimitive != null)
     173                                return minPrimitive;
     174                }
     175               
     176                minDistanceSq = Double.MAX_VALUE;
     177                // line segments
     178                for (LineSegment ls : Main.main.ds.lineSegments) {
     179                        if (ls.isDeleted())
     180                                continue;
     181                        Point A = getScreenPoint(ls.start.coor);
     182                        Point B = getScreenPoint(ls.end.coor);
     183                        double c = A.distanceSq(B);
     184                        double a = p.distanceSq(B);
     185                        double b = p.distanceSq(A);
     186                        double perDist = a-(a-b+c)*(a-b+c)/4/c; // perpendicular distance squared
     187                        if (perDist < 100 && minDistanceSq > perDist && a < c+100 && b < c+100) {
     188                                minDistanceSq = perDist;
     189                                minPrimitive = ls;
     190                        }
     191                }
     192       
     193                return minPrimitive;
     194        }
     195
     196        /**
     197         * @return A list of all objects that are nearest to
     198         * the mouse. To do this, first the nearest object is
     199         * determined.
     200         *
     201         * If its a node, return all line segments and
     202         * streets the node is part of, as well as all nodes
     203         * (with their line segments and tracks) with the same
     204         * location.
     205         *
     206         * If its a line segment, return all tracks this segment
     207         * belongs to as well as all line segments that are between
     208         * the same nodes (in both direction) with all their tracks.
     209         *
     210         * @return A collection of all items or <code>null</code>
     211         *              if no item under or near the point. The returned
     212         *              list is never empty.
     213         */
     214        public Collection<OsmPrimitive> getAllNearest(Point p) {
     215                OsmPrimitive osm = getNearest(p, true);
     216                if (osm == null)
     217                        return null;
     218                Collection<OsmPrimitive> c = new HashSet<OsmPrimitive>();
     219                c.add(osm);
     220                if (osm instanceof Node) {
     221                        Node node = (Node)osm;
     222                        for (Node n : Main.main.ds.nodes)
     223                                if (!n.isDeleted() && n.coor.equalsLatLon(node.coor))
     224                                        c.add(n);
     225                        for (LineSegment ls : Main.main.ds.lineSegments)
     226                                // line segments never match nodes, so they are skipped by contains
     227                                if (!ls.isDeleted() && (c.contains(ls.start) || c.contains(ls.end)))
     228                                        c.add(ls);
     229                }
     230                if (osm instanceof LineSegment) {
     231                        LineSegment line = (LineSegment)osm;
     232                        for (LineSegment ls : Main.main.ds.lineSegments)
     233                                if (!ls.isDeleted() && ls.equalPlace(line))
     234                                        c.add(ls);
     235                }
     236                if (osm instanceof Node || osm instanceof LineSegment) {
     237                        for (Track t : Main.main.ds.tracks) {
     238                                if (t.isDeleted())
     239                                        continue;
     240                                for (LineSegment ls : t.segments) {
     241                                        if (!ls.isDeleted() && c.contains(ls)) {
     242                                                c.add(t);
     243                                                break;
     244                                        }
     245                                }
     246                        }
     247                }
     248                return c;
     249        }
     250       
     251        /**
     252         * @return The projection to be used in calculating stuff.
     253         */
     254        protected Projection getProjection() {
     255                return Main.pref.getProjection();
     256        }
    101257}
  • src/org/openstreetmap/josm/gui/SelectionManager.java

    r30 r42  
    8989        private Point mousePos;
    9090        /**
    91          * The MapView, the selection rectangle is drawn onto.
    92          */
    93         private final MapView mv;
     91         * The Component, the selection rectangle is drawn onto.
     92         */
     93        private final NavigatableComponent nc;
    9494        /**
    9595         * Whether the selection rectangle must obtain the aspect ratio of the
     
    105105         * @param aspectRatio If true, the selection window must obtain the aspect
    106106         *              ratio of the drawComponent.
    107          * @param mapView The view, the rectangle is drawn onto.
    108          */
    109         public SelectionManager(SelectionEnded selectionEndedListener, boolean aspectRatio, MapView mapView) {
     107         * @param navComp The component, the rectangle is drawn onto.
     108         */
     109        public SelectionManager(SelectionEnded selectionEndedListener, boolean aspectRatio, NavigatableComponent navComp) {
    110110                this.selectionEndedListener = selectionEndedListener;
    111111                this.aspectRatio = aspectRatio;
    112                 this.mv = mapView;
     112                this.nc = navComp;
    113113        }
    114114       
     
    196196                if (mousePos == null || mousePosStart == null || mousePos == mousePosStart)
    197197                        return;
    198                 Graphics g = mv.getGraphics();
     198                Graphics g = nc.getGraphics();
    199199                g.setColor(Color.BLACK);
    200200                g.setXORMode(Color.WHITE);
     
    224224                if (aspectRatio) {
    225225                        // keep the aspect ration by shrinking the rectangle
    226                         double aspectRatio = (double)mv.getWidth()/mv.getHeight();
     226                        double aspectRatio = (double)nc.getWidth()/nc.getHeight();
    227227                        if ((double)w/h > aspectRatio) {
    228228                                int neww = (int)(h*aspectRatio);
     
    267267
    268268                if (clicked) {
    269                         OsmPrimitive osm = mv.getNearest(center, alt);
     269                        OsmPrimitive osm = nc.getNearest(center, alt);
    270270                        if (osm != null)
    271271                                selection.add(osm);
     
    273273                        // nodes
    274274                        for (Node n : Main.main.ds.nodes) {
    275                                 if (r.contains(mv.getScreenPoint(n.coor)))
     275                                if (r.contains(nc.getScreenPoint(n.coor)))
    276276                                        selection.add(n);
    277277                        }
     
    310310        private boolean rectangleContainLineSegment(Rectangle r, boolean alt, LineSegment ls) {
    311311                if (alt) {
    312                         Point p1 = mv.getScreenPoint(ls.start.coor);
    313                         Point p2 = mv.getScreenPoint(ls.end.coor);
     312                        Point p1 = nc.getScreenPoint(ls.start.coor);
     313                        Point p2 = nc.getScreenPoint(ls.end.coor);
    314314                        if (r.intersectsLine(p1.x, p1.y, p2.x, p2.y))
    315315                                return true;
    316316                } else {
    317                         if (r.contains(mv.getScreenPoint(ls.start.coor))
    318                                         && r.contains(mv.getScreenPoint(ls.end.coor)))
     317                        if (r.contains(nc.getScreenPoint(ls.start.coor))
     318                                        && r.contains(nc.getScreenPoint(ls.end.coor)))
    319319                                return true;
    320320                }
  • src/org/openstreetmap/josm/gui/WorldChooser.java

    r41 r42  
    44import java.awt.Dimension;
    55import java.awt.Graphics;
     6import java.awt.Point;
     7import java.awt.Rectangle;
     8import java.awt.event.KeyAdapter;
     9import java.awt.event.KeyEvent;
     10import java.awt.event.KeyListener;
     11import java.beans.PropertyChangeListener;
    612import java.net.URL;
    713
    814import javax.swing.ImageIcon;
     15import javax.swing.JComponent;
     16import javax.swing.JTextField;
     17import javax.swing.SwingUtilities;
    918import javax.swing.event.ListSelectionEvent;
    1019import javax.swing.event.ListSelectionListener;
    1120
    1221import org.openstreetmap.josm.Main;
     22import org.openstreetmap.josm.data.Bounds;
    1323import org.openstreetmap.josm.data.GeoPoint;
     24import org.openstreetmap.josm.data.projection.Projection;
    1425import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
     26import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
    1527
    1628/**
     
    3042
    3143        /**
     44         * Maximum scale level
     45         */
     46        private double scaleMax;
     47       
     48        /**
    3249         * Mark this rectangle (lat/lon values) when painting.
    3350         */
    34         protected double[] markerRect;
    35 
     51        protected Bounds marker;
     52
     53        private Projection projection;
     54       
    3655        /**
    3756         * Create the chooser component.
     
    4059                URL path = Main.class.getResource("/images/world.jpg");
    4160                world = new ImageIcon(path);
    42                 center = new GeoPoint(0,0,30,165);
     61                center = new GeoPoint(0,0,world.getIconWidth()/2, world.getIconHeight()/2);
    4362                setPreferredSize(new Dimension(200, 100));
    44                 //new MapMover(this);
     63                new MapMover(this);
     64                projection = new Projection(){
     65                        @Override
     66                        public void latlon2xy(GeoPoint p) {
     67                                p.x = (p.lon+180) / 360 * world.getIconWidth();
     68                                p.y = (p.lat+90) / 180 * world.getIconHeight();
     69                        }
     70                        @Override
     71                        public void xy2latlon(GeoPoint p) {
     72                                p.lon = p.x*360/world.getIconWidth() - 180;
     73                                p.lat = p.y*180/world.getIconHeight() - 90;
     74                        }
     75                        @Override
     76                        public String toString() {
     77                                return "WorldChooser";
     78                        }
     79                        @Override
     80                        public JComponent getConfigurationPanel() {
     81                                return null;
     82                        }
     83                        @Override
     84                        public void commitConfigurationPanel() {
     85                        }
     86                };
    4587        }
    4688
     
    5294        public void setPreferredSize(Dimension preferredSize) {
    5395                super.setPreferredSize(preferredSize);
    54                 scale = 60/preferredSize.getWidth();
     96                scale = world.getIconWidth()/preferredSize.getWidth();
     97                scaleMax = scale;
    5598        }
    5699
     
    61104        @Override
    62105        public void paint(Graphics g) {
    63                 int x1 = getScreenPoint(new GeoPoint(0,0,0,180)).x;
    64                 int y1 = getScreenPoint(new GeoPoint(0,0,0,180)).y;
    65                 int x2 = getScreenPoint(new GeoPoint(0,0,360,0)).x;
    66                 int y2 = getScreenPoint(new GeoPoint(0,0,360,0)).y;
    67                 System.out.println(x1+" "+y1+" "+x2+" "+y2);
    68                 System.out.println(center.x+" "+center.y+" "+(scale*getWidth()));
    69                 g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),x1,y1,x2,y2, null);
    70                 g.setColor(Color.WHITE);
    71                
     106                GeoPoint tl = getPoint(0,0,false);
     107                GeoPoint br = getPoint(getWidth(),getHeight(),false);
     108                g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),(int)tl.x,(int)tl.y,(int)br.x,(int)br.y, null);
     109
    72110                // draw marker rect
    73                 //TODO
     111                if (marker != null) {
     112                        Point p1 = getScreenPoint(marker.min);
     113                        Point p2 = getScreenPoint(marker.max);
     114                        double x = Math.min(p1.x, p2.x);
     115                        double y = Math.min(p1.y, p2.y);
     116                        double w = Math.max(p1.x, p2.x) - x;
     117                        double h = Math.max(p1.y, p2.y) - y;
     118                        if (w < 1)
     119                                w = 1;
     120                        if (h < 1)
     121                                h = 1;
     122                        g.setColor(Color.YELLOW);
     123                        g.drawRect((int)x, (int)y, (int)w, (int)h);
     124                }
    74125        }
    75126
     
    77128        @Override
    78129        public void zoomTo(GeoPoint newCenter, double scale) {
    79                 if (getWidth() != 0 && scale < 60.0/getWidth())
    80                         scale = 60.0/getWidth();
     130                if (getWidth() != 0 && scale > scaleMax)
     131                        scale = scaleMax;
    81132                super.zoomTo(newCenter, scale);
    82133        }
    83        
     134
    84135        /**
    85136         * Show the selection bookmark in the world.
     
    89140                        public void valueChanged(ListSelectionEvent e) {
    90141                                Bookmark b = (Bookmark)list.getSelectedValue();
    91                                 if (b != null)
    92                                         markerRect = b.latlon;
    93                                 else
    94                                         markerRect = null;
     142                                if (b != null) {
     143                                        marker = new Bounds(new GeoPoint(b.latlon[0],b.latlon[1]),
     144                                                        new GeoPoint(b.latlon[2],b.latlon[3]));
     145                                } else
     146                                        marker = null;
    95147                                repaint();
    96148                        }
    97149                });
    98150        }
     151
     152        /**
     153         * Update edit fields and react upon changes.
     154         * @param field Must have exactly 4 entries (min lat to max lon)
     155         */
     156        public void addLatLonInputField(final JTextField[] field) {
     157                if (field.length != 4)
     158                        throw new IllegalArgumentException();
     159
     160                // listener that invokes updateMarkerFromTextField after all
     161                // messages are dispatched and so text fields are updated.
     162                KeyListener listener = new KeyAdapter(){
     163                        @Override
     164                        public void keyTyped(KeyEvent e) {
     165                                SwingUtilities.invokeLater(new Runnable(){
     166                                        public void run() {
     167                                                updateMarkerFromTextFields(field);
     168                                        }
     169                                });
     170                        }
     171                };
     172
     173                for (JTextField f : field)
     174                        f.addKeyListener(listener);
     175
     176                SelectionEnded selListener = new SelectionEnded(){
     177                        public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
     178                                GeoPoint min = getPoint(r.x, r.y+r.height, true);
     179                                GeoPoint max = getPoint(r.x+r.width, r.y, true);
     180                                marker = new Bounds(min, max);
     181                                field[0].setText(""+min.lat);
     182                                field[1].setText(""+min.lon);
     183                                field[2].setText(""+max.lat);
     184                                field[3].setText(""+max.lon);
     185                                for (JTextField f : field)
     186                                        f.setCaretPosition(0);
     187                                repaint();
     188                        }
     189                        public void addPropertyChangeListener(PropertyChangeListener listener) {}
     190                        public void removePropertyChangeListener(PropertyChangeListener listener) {}
     191                };
     192                SelectionManager sm = new SelectionManager(selListener, false, this);
     193                sm.register(this);
     194                updateMarkerFromTextFields(field);
     195        }
     196
     197        /**
     198         * Update the marker field from the values of the given textfields
     199         */
     200        private void updateMarkerFromTextFields(JTextField[] field) {
     201                // try to read all values
     202                double v[] = new double[field.length];
     203                for (int i = 0; i < field.length; ++i) {
     204                        try {
     205                                v[i] = Double.parseDouble(field[i].getText());
     206                        } catch (NumberFormatException nfe) {
     207                                return;
     208                        }
     209                }
     210
     211                marker = new Bounds(new GeoPoint(v[0], v[1]), new GeoPoint(v[2], v[3]));
     212                repaint();
     213        }
     214
     215        /**
     216         * Always use our image projection mode.
     217         */
     218        @Override
     219        protected Projection getProjection() {
     220                return projection;
     221        }
    99222}
Note: See TracChangeset for help on using the changeset viewer.