Changeset 42 in josm for src/org/openstreetmap
- Timestamp:
- 2006-01-21T19:02:06+01:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm
- Files:
-
- 7 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/actions/DownloadAction.java
r41 r42 110 110 } 111 111 }); 112 wc.addListMarker(bookmarks); 113 wc.addLatLonInputField(latlon); 112 114 dlg.add(new JScrollPane(bookmarks), GBC.eol().fill()); 113 115 -
src/org/openstreetmap/josm/data/Preferences.java
r41 r42 18 18 import org.jdom.output.Format; 19 19 import org.jdom.output.XMLOutputter; 20 import org.openstreetmap.josm.data.projection. LatitudeLongitude;20 import org.openstreetmap.josm.data.projection.Epsg4263; 21 21 import org.openstreetmap.josm.data.projection.Mercator; 22 22 import org.openstreetmap.josm.data.projection.Projection; … … 80 80 new Mercator(), 81 81 new UTM(), 82 new LatitudeLongitude()82 new Epsg4263() 83 83 }; 84 84 -
src/org/openstreetmap/josm/data/projection/Epsg4263.java
r41 r42 10 10 * @author imi 11 11 */ 12 public class LatitudeLongitudeextends Projection {12 public class Epsg4263 extends Projection { 13 13 14 14 @Override … … 26 26 @Override 27 27 public String toString() { 28 return " Latitude/Longitude";28 return "EPSG:4263"; 29 29 } 30 30 -
src/org/openstreetmap/josm/gui/MapMover.java
r41 r42 32 32 private Cursor oldCursor; 33 33 34 34 35 /** 35 36 * Create a new MapMover 36 * @param mapView The map that should be moved.37 37 */ 38 publicMapMover(NavigatableComponentmapView) {39 this.nc = mapView;38 MapMover(NavigatableComponent navComp) { 39 this.nc = navComp; 40 40 nc.addMouseListener(this); 41 41 nc.addMouseMotionListener(this); -
src/org/openstreetmap/josm/gui/MapView.java
r41 r42 11 11 import java.util.Collection; 12 12 import java.util.Collections; 13 import java.util.HashSet;14 13 import java.util.LinkedList; 15 14 … … 21 20 import org.openstreetmap.josm.data.GeoPoint; 22 21 import org.openstreetmap.josm.data.osm.DataSet; 23 import org.openstreetmap.josm.data.osm.LineSegment;24 22 import org.openstreetmap.josm.data.osm.Node; 25 import org.openstreetmap.josm.data.osm.OsmPrimitive;26 import org.openstreetmap.josm.data.osm.Track;27 23 import org.openstreetmap.josm.data.projection.Projection; 28 24 import org.openstreetmap.josm.gui.layer.Layer; … … 103 99 // initialize the projection if it is the first layer 104 100 if (layers.isEmpty()) 105 Main.pref.getProjection().init(layer.getBoundsLatLon());101 getProjection().init(layer.getBoundsLatLon()); 106 102 107 103 // reinitialize layer's data 108 layer.init( Main.pref.getProjection());104 layer.init(getProjection()); 109 105 110 106 if (layer instanceof OsmDataLayer) { … … 161 157 } 162 158 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, the167 * 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 is172 * within 10 pixel to p, this segment is returned, except when173 * <code>wholeTrack</code> is <code>true</code>, in which case the174 * corresponding Track is returned.175 *176 * If no line segment is found and the point is within an area, return that177 * 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 whole183 * 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 // nodes191 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 first205 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 squared219 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 segments231 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 squared240 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 to251 * the mouse. To do this, first the nearest object is252 * determined.253 *254 * If its a node, return all line segments and255 * streets the node is part of, as well as all nodes256 * (with their line segments and tracks) with the same257 * location.258 *259 * If its a line segment, return all tracks this segment260 * belongs to as well as all line segments that are between261 * 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 returned265 * 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 contains280 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 304 159 /** 305 160 * Draw the component. … … 378 233 // no bounds means standard scale and center 379 234 center = new GeoPoint(51.526447, -0.14746371); 380 Main.pref.getProjection().latlon2xy(center);235 getProjection().latlon2xy(center); 381 236 scale = 10; 382 237 } else { 383 238 center = bounds.centerXY(); 384 Main.pref.getProjection().xy2latlon(center);239 getProjection().xy2latlon(center); 385 240 double scaleX = (bounds.max.x-bounds.min.x)/w; 386 241 double scaleY = (bounds.max.y-bounds.min.y)/h; … … 483 338 public void stateChanged(ChangeEvent e) { 484 339 // reset all datasets. 485 Projection p = Main.pref.getProjection();340 Projection p = getProjection(); 486 341 for (Node n : Main.main.ds.nodes) 487 342 p.latlon2xy(n.coor); -
src/org/openstreetmap/josm/gui/NavigatableComponent.java
r41 r42 2 2 3 3 import java.awt.Point; 4 import java.util.Collection; 5 import java.util.HashSet; 4 6 5 7 import javax.swing.JComponent; … … 7 9 import org.openstreetmap.josm.Main; 8 10 import org.openstreetmap.josm.data.GeoPoint; 11 import org.openstreetmap.josm.data.osm.LineSegment; 12 import org.openstreetmap.josm.data.osm.Node; 13 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 import org.openstreetmap.josm.data.osm.Track; 15 import org.openstreetmap.josm.data.projection.Projection; 9 16 10 17 /** … … 17 24 18 25 /** 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. 20 29 */ 21 30 protected double scale; … … 58 67 public GeoPoint getPoint(int x, int y, boolean latlon) { 59 68 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; 62 71 if (latlon) 63 Main.pref.getProjection().xy2latlon(p);72 getProjection().xy2latlon(p); 64 73 return p; 65 74 } … … 79 88 throw new IllegalArgumentException("point: Either lat/lon or x/y must be set."); 80 89 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); 86 95 } 87 96 … … 93 102 */ 94 103 public void zoomTo(GeoPoint newCenter, double scale) { 95 System.out.println(scale);96 104 center = newCenter.clone(); 97 Main.pref.getProjection().xy2latlon(center);105 getProjection().xy2latlon(center); 98 106 this.scale = scale; 99 107 repaint(); 100 108 } 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 } 101 257 } -
src/org/openstreetmap/josm/gui/SelectionManager.java
r30 r42 89 89 private Point mousePos; 90 90 /** 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; 94 94 /** 95 95 * Whether the selection rectangle must obtain the aspect ratio of the … … 105 105 * @param aspectRatio If true, the selection window must obtain the aspect 106 106 * 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) { 110 110 this.selectionEndedListener = selectionEndedListener; 111 111 this.aspectRatio = aspectRatio; 112 this. mv = mapView;112 this.nc = navComp; 113 113 } 114 114 … … 196 196 if (mousePos == null || mousePosStart == null || mousePos == mousePosStart) 197 197 return; 198 Graphics g = mv.getGraphics();198 Graphics g = nc.getGraphics(); 199 199 g.setColor(Color.BLACK); 200 200 g.setXORMode(Color.WHITE); … … 224 224 if (aspectRatio) { 225 225 // keep the aspect ration by shrinking the rectangle 226 double aspectRatio = (double) mv.getWidth()/mv.getHeight();226 double aspectRatio = (double)nc.getWidth()/nc.getHeight(); 227 227 if ((double)w/h > aspectRatio) { 228 228 int neww = (int)(h*aspectRatio); … … 267 267 268 268 if (clicked) { 269 OsmPrimitive osm = mv.getNearest(center, alt);269 OsmPrimitive osm = nc.getNearest(center, alt); 270 270 if (osm != null) 271 271 selection.add(osm); … … 273 273 // nodes 274 274 for (Node n : Main.main.ds.nodes) { 275 if (r.contains( mv.getScreenPoint(n.coor)))275 if (r.contains(nc.getScreenPoint(n.coor))) 276 276 selection.add(n); 277 277 } … … 310 310 private boolean rectangleContainLineSegment(Rectangle r, boolean alt, LineSegment ls) { 311 311 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); 314 314 if (r.intersectsLine(p1.x, p1.y, p2.x, p2.y)) 315 315 return true; 316 316 } 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))) 319 319 return true; 320 320 } -
src/org/openstreetmap/josm/gui/WorldChooser.java
r41 r42 4 4 import java.awt.Dimension; 5 5 import java.awt.Graphics; 6 import java.awt.Point; 7 import java.awt.Rectangle; 8 import java.awt.event.KeyAdapter; 9 import java.awt.event.KeyEvent; 10 import java.awt.event.KeyListener; 11 import java.beans.PropertyChangeListener; 6 12 import java.net.URL; 7 13 8 14 import javax.swing.ImageIcon; 15 import javax.swing.JComponent; 16 import javax.swing.JTextField; 17 import javax.swing.SwingUtilities; 9 18 import javax.swing.event.ListSelectionEvent; 10 19 import javax.swing.event.ListSelectionListener; 11 20 12 21 import org.openstreetmap.josm.Main; 22 import org.openstreetmap.josm.data.Bounds; 13 23 import org.openstreetmap.josm.data.GeoPoint; 24 import org.openstreetmap.josm.data.projection.Projection; 14 25 import org.openstreetmap.josm.gui.BookmarkList.Bookmark; 26 import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded; 15 27 16 28 /** … … 30 42 31 43 /** 44 * Maximum scale level 45 */ 46 private double scaleMax; 47 48 /** 32 49 * Mark this rectangle (lat/lon values) when painting. 33 50 */ 34 protected double[] markerRect; 35 51 protected Bounds marker; 52 53 private Projection projection; 54 36 55 /** 37 56 * Create the chooser component. … … 40 59 URL path = Main.class.getResource("/images/world.jpg"); 41 60 world = new ImageIcon(path); 42 center = new GeoPoint(0,0, 30,165);61 center = new GeoPoint(0,0,world.getIconWidth()/2, world.getIconHeight()/2); 43 62 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 }; 45 87 } 46 88 … … 52 94 public void setPreferredSize(Dimension preferredSize) { 53 95 super.setPreferredSize(preferredSize); 54 scale = 60/preferredSize.getWidth(); 96 scale = world.getIconWidth()/preferredSize.getWidth(); 97 scaleMax = scale; 55 98 } 56 99 … … 61 104 @Override 62 105 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 72 110 // 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 } 74 125 } 75 126 … … 77 128 @Override 78 129 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; 81 132 super.zoomTo(newCenter, scale); 82 133 } 83 134 84 135 /** 85 136 * Show the selection bookmark in the world. … … 89 140 public void valueChanged(ListSelectionEvent e) { 90 141 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; 95 147 repaint(); 96 148 } 97 149 }); 98 150 } 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 } 99 222 }
Note:
See TracChangeset
for help on using the changeset viewer.