Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/gui/MapView.java

    r17 r30  
    1717import javax.swing.event.ChangeListener;
    1818
     19import org.openstreetmap.josm.Main;
    1920import org.openstreetmap.josm.data.Bounds;
    2021import org.openstreetmap.josm.data.GeoPoint;
     
    2627import org.openstreetmap.josm.data.projection.Projection;
    2728import org.openstreetmap.josm.gui.layer.Layer;
     29import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2830
    2931/**
    3032 * This is a component used in the MapFrame for browsing the map. It use is to
    3133 * provide the MapMode's enough capabilities to operate.
    32  *
    33  * MapView holds the map data, organize it, convert it, provide access to it.
    3434 *
    3535 * MapView hold meta-data about the data set currently displayed, as scale level,
     
    7373        private ArrayList<Layer> layers = new ArrayList<Layer>();
    7474        /**
     75         * Direct link to the edit layer (if any) in the layers list.
     76         */
     77        private OsmDataLayer editLayer;
     78        /**
    7579         * The layer from the layers list that is currently active.
    7680         */
     
    8690         */
    8791        public MapView(Layer layer) {
    88                 if (layer.getDataSet() == null)
    89                         throw new IllegalArgumentException("Initial layer must have a dataset.");
    90 
    9192                addComponentListener(new ComponentAdapter(){
    9293                        @Override
     
    102103                addLayer(layer);
    103104                Main.pref.addPropertyChangeListener(this);
    104 
    105                 // init screen
    106                 recalculateCenterScale();
    107105        }
    108106
     
    112110         */
    113111        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
    114132                layers.add(0,layer);
    115 
    116                 DataSet ds = layer.getDataSet();
    117 
    118                 if (ds != null) {
    119                         // initialize the projection if it was the first layer
    120                         if (layers.size() == 1)
    121                                 Main.pref.getProjection().init(ds);
    122                        
    123                         // initialize the dataset in the new layer
    124                         for (Node n : ds.nodes)
    125                                 Main.pref.getProjection().latlon2xy(n.coor);
    126                 }
    127133
    128134                for (LayerChangeListener l : listeners)
    129135                        l.layerAdded(layer);
    130136
     137                // autoselect the new layer
    131138                setActiveLayer(layer);
    132139        }
    133        
     140
    134141        /**
    135142         * Remove the layer from the mapview. If the layer was in the list before,
     
    140147                        for (LayerChangeListener l : listeners)
    141148                                l.layerRemoved(layer);
     149                if (layer == editLayer)
     150                        editLayer = null;
    142151        }
    143152
     
    232241                OsmPrimitive minPrimitive = null;
    233242
    234                 // calculate the object based on the current active dataset.
    235                 DataSet ds = getActiveDataSet();
    236                
    237243                // nodes
    238                 for (Node n : ds.nodes) {
     244                for (Node n : Main.main.ds.nodes) {
    239245                        Point sp = getScreenPoint(n.coor);
    240246                        double dist = p.distanceSq(sp);
     
    248254               
    249255                // 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);
    253259                        double c = A.distanceSq(B);
    254260                        double a = p.distanceSq(B);
     
    263269                // tracks & line segments
    264270                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);
    269275                                double c = A.distanceSq(B);
    270276                                double a = p.distanceSq(B);
     
    320326                for (int i = layers.size()-1; i >= 0; --i) {
    321327                        Layer l = layers.get(i);
    322                         if (l.isVisible())
     328                        if (l.visible)
    323329                                l.paint(g, this);
    324330                }
     
    332338                // reset all datasets.
    333339                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);
    340342                recalculateCenterScale();
    341343        }
     
    375377
    376378       
    377         /**
    378          * Return the dataSet for the current selected layer. If the active layer
    379          * 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 
    394379        /**
    395380         * Change to the new projection. Recalculate the dataset and zoom, if autoZoom
     
    425410                                h = 20;
    426411                       
    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
    429423                        boolean oldAutoScale = autoScale;
    430424                        GeoPoint oldCenter = center;
     
    479473        /**
    480474         * 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.
    481476         */
    482477        public void setActiveLayer(Layer layer) {
     
    485480                Layer old = activeLayer;
    486481                activeLayer = layer;
     482                if (layer instanceof OsmDataLayer)
     483                        Main.main.ds = ((OsmDataLayer)layer).data;
    487484                if (old != layer) {
    488                         if (old != null && old.getDataSet() != null)
    489                                 old.getDataSet().clearSelection();
    490485                        for (LayerChangeListener l : listeners)
    491486                                l.activeLayerChange(old, layer);
     
    500495                return activeLayer;
    501496        }
     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        }
    502507}
Note: See TracChangeset for help on using the changeset viewer.