Changes in src/org/openstreetmap/josm/gui/MapView.java [17:30] in josm
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/gui/MapView.java
r17 r30 17 17 import javax.swing.event.ChangeListener; 18 18 19 import org.openstreetmap.josm.Main; 19 20 import org.openstreetmap.josm.data.Bounds; 20 21 import org.openstreetmap.josm.data.GeoPoint; … … 26 27 import org.openstreetmap.josm.data.projection.Projection; 27 28 import org.openstreetmap.josm.gui.layer.Layer; 29 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 28 30 29 31 /** 30 32 * This is a component used in the MapFrame for browsing the map. It use is to 31 33 * provide the MapMode's enough capabilities to operate. 32 *33 * MapView holds the map data, organize it, convert it, provide access to it.34 34 * 35 35 * MapView hold meta-data about the data set currently displayed, as scale level, … … 73 73 private ArrayList<Layer> layers = new ArrayList<Layer>(); 74 74 /** 75 * Direct link to the edit layer (if any) in the layers list. 76 */ 77 private OsmDataLayer editLayer; 78 /** 75 79 * The layer from the layers list that is currently active. 76 80 */ … … 86 90 */ 87 91 public MapView(Layer layer) { 88 if (layer.getDataSet() == null)89 throw new IllegalArgumentException("Initial layer must have a dataset.");90 91 92 addComponentListener(new ComponentAdapter(){ 92 93 @Override … … 102 103 addLayer(layer); 103 104 Main.pref.addPropertyChangeListener(this); 104 105 // init screen106 recalculateCenterScale();107 105 } 108 106 … … 112 110 */ 113 111 public void addLayer(Layer layer) { 112 // initialize the projection if it is the first layer 113 if (layers.isEmpty()) 114 Main.pref.getProjection().init(layer.getBoundsLatLon()); 115 116 // reinitialize layer's data 117 layer.init(Main.pref.getProjection()); 118 119 if (layer instanceof OsmDataLayer) { 120 if (editLayer != null) { 121 // merge the layer into the existing one 122 if (!editLayer.isMergable(layer)) 123 throw new IllegalArgumentException("Cannot merge argument"); 124 editLayer.mergeFrom(layer); 125 repaint(); 126 return; 127 } 128 editLayer = (OsmDataLayer)layer; 129 } 130 131 // add as a new layer 114 132 layers.add(0,layer); 115 116 DataSet ds = layer.getDataSet();117 118 if (ds != null) {119 // initialize the projection if it was the first layer120 if (layers.size() == 1)121 Main.pref.getProjection().init(ds);122 123 // initialize the dataset in the new layer124 for (Node n : ds.nodes)125 Main.pref.getProjection().latlon2xy(n.coor);126 }127 133 128 134 for (LayerChangeListener l : listeners) 129 135 l.layerAdded(layer); 130 136 137 // autoselect the new layer 131 138 setActiveLayer(layer); 132 139 } 133 140 134 141 /** 135 142 * Remove the layer from the mapview. If the layer was in the list before, … … 140 147 for (LayerChangeListener l : listeners) 141 148 l.layerRemoved(layer); 149 if (layer == editLayer) 150 editLayer = null; 142 151 } 143 152 … … 232 241 OsmPrimitive minPrimitive = null; 233 242 234 // calculate the object based on the current active dataset.235 DataSet ds = getActiveDataSet();236 237 243 // nodes 238 for (Node n : ds.nodes) {244 for (Node n : Main.main.ds.nodes) { 239 245 Point sp = getScreenPoint(n.coor); 240 246 double dist = p.distanceSq(sp); … … 248 254 249 255 // pending line segments 250 for (LineSegment ls : ds.pendingLineSegments()) {251 Point A = getScreenPoint(ls. getStart().coor);252 Point B = getScreenPoint(ls. getEnd().coor);256 for (LineSegment ls : Main.main.ds.lineSegments) { 257 Point A = getScreenPoint(ls.start.coor); 258 Point B = getScreenPoint(ls.end.coor); 253 259 double c = A.distanceSq(B); 254 260 double a = p.distanceSq(B); … … 263 269 // tracks & line segments 264 270 minDistanceSq = Double.MAX_VALUE; 265 for (Track t : ds.tracks()) {266 for (LineSegment ls : t.segments ()) {267 Point A = getScreenPoint(ls. getStart().coor);268 Point B = getScreenPoint(ls. getEnd().coor);271 for (Track t : Main.main.ds.tracks) { 272 for (LineSegment ls : t.segments) { 273 Point A = getScreenPoint(ls.start.coor); 274 Point B = getScreenPoint(ls.end.coor); 269 275 double c = A.distanceSq(B); 270 276 double a = p.distanceSq(B); … … 320 326 for (int i = layers.size()-1; i >= 0; --i) { 321 327 Layer l = layers.get(i); 322 if (l. isVisible())328 if (l.visible) 323 329 l.paint(g, this); 324 330 } … … 332 338 // reset all datasets. 333 339 Projection p = Main.pref.getProjection(); 334 for (Layer l : layers) { 335 DataSet ds = l.getDataSet(); 336 if (ds != null) 337 for (Node n : ds.nodes) 338 p.latlon2xy(n.coor); 339 } 340 for (Node n : Main.main.ds.nodes) 341 p.latlon2xy(n.coor); 340 342 recalculateCenterScale(); 341 343 } … … 375 377 376 378 377 /**378 * Return the dataSet for the current selected layer. If the active layer379 * does not have a dataset, return the DataSet from the next layer a.s.o.380 *381 * @return The DataSet of the current active layer.382 */383 public DataSet getActiveDataSet() {384 if (activeLayer.getDataSet() != null)385 return activeLayer.getDataSet();386 for (Layer l : layers) {387 DataSet ds = l.getDataSet();388 if (ds != null)389 return ds;390 }391 throw new IllegalStateException("No dataset found.");392 }393 394 379 /** 395 380 * Change to the new projection. Recalculate the dataset and zoom, if autoZoom … … 425 410 h = 20; 426 411 427 Bounds bounds = getActiveDataSet().getBoundsXY(); 428 412 Bounds bounds = null; 413 for (Layer l : layers) { 414 if (bounds == null) 415 bounds = l.getBoundsXY(); 416 else { 417 Bounds lb = l.getBoundsXY(); 418 if (lb != null) 419 bounds = bounds.mergeXY(lb); 420 } 421 } 422 429 423 boolean oldAutoScale = autoScale; 430 424 GeoPoint oldCenter = center; … … 479 473 /** 480 474 * Set the active selection to the given value and raise an layerchange event. 475 * Also, swap the active dataset in Main.main if it is a datalayer. 481 476 */ 482 477 public void setActiveLayer(Layer layer) { … … 485 480 Layer old = activeLayer; 486 481 activeLayer = layer; 482 if (layer instanceof OsmDataLayer) 483 Main.main.ds = ((OsmDataLayer)layer).data; 487 484 if (old != layer) { 488 if (old != null && old.getDataSet() != null)489 old.getDataSet().clearSelection();490 485 for (LayerChangeListener l : listeners) 491 486 l.activeLayerChange(old, layer); … … 500 495 return activeLayer; 501 496 } 497 498 /** 499 * @return The current edit layer. If no edit layer exist, one is created. 500 * So editLayer does never return <code>null</code>. 501 */ 502 public OsmDataLayer editLayer() { 503 if (editLayer == null) 504 addLayer(new OsmDataLayer(new DataSet(), "unnamed")); 505 return editLayer; 506 } 502 507 }
Note:
See TracChangeset
for help on using the changeset viewer.