Changeset 2652 in josm for trunk


Ignore:
Timestamp:
2009-12-18T08:40:15+01:00 (15 years ago)
Author:
jttt
Message:

Added listener for edit layer change (not just active layer)

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/event/DataChangedEvent.java

    r2645 r2652  
    22package org.openstreetmap.josm.data.osm.event;
    33
     4import java.util.Collections;
    45import java.util.List;
    56
     
    2122    @Override
    2223    public List<OsmPrimitive> getPrimitives() {
    23         return dataSet.allPrimitives();
     24        if (dataSet == null)
     25            return Collections.emptyList();
     26        else
     27            return dataSet.allPrimitives();
    2428    }
    2529
  • trunk/src/org/openstreetmap/josm/data/osm/event/DatasetEventManager.java

    r2625 r2652  
    99import javax.swing.SwingUtilities;
    1010
    11 import org.openstreetmap.josm.Main;
    1211import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter.Listener;
    1312import org.openstreetmap.josm.gui.MapView;
    14 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    15 import org.openstreetmap.josm.gui.layer.Layer;
    1613import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1714
     
    2623 *
    2724 */
    28 public class DatasetEventManager implements LayerChangeListener, Listener {
     25public class DatasetEventManager implements MapView.EditLayerChangeListener, Listener {
    2926
    3027    private static final DatasetEventManager instance = new DatasetEventManager();
     
    4037
    4138    public DatasetEventManager() {
    42         MapView.addLayerChangeListener(this);
     39        MapView.addEditLayerChangeListener(this);
    4340    }
    4441
     
    6259    }
    6360
    64     /* --------------------------------------------------------------------------- */
    65     /* interface LayerChangeListener                                               */
    66     /* --------------------------------------------------------------------------- */
    67     public void activeLayerChange(Layer a, Layer b) {
    68         if (a != null && a instanceof OsmDataLayer) {
    69             ((OsmDataLayer)a).data.removeDataSetListener(myListener);
     61    public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
     62        if (oldLayer != null) {
     63            oldLayer.data.removeDataSetListener(myListener);
    7064        }
    71         if (b != null && b instanceof OsmDataLayer) {
    72             ((OsmDataLayer)b).data.addDataSetListener(myListener);
    73         }
    74         if (Main.main.getEditLayer() != null) {
    75             processDatasetEvent(new DataChangedEvent(Main.main.getEditLayer().data));
     65
     66        if (newLayer != null) {
     67            newLayer.data.addDataSetListener(myListener);
     68            processDatasetEvent(new DataChangedEvent(newLayer.data));
     69        } else {
     70            processDatasetEvent(new DataChangedEvent(null));
    7671        }
    7772    }
    78     public void layerRemoved(Layer a) {/* irrelevant in this context */}
    79     public void layerAdded(Layer a) {/* irrelevant in this context */}
    8073
    8174    public void processDatasetEvent(AbstractDatasetChangedEvent event) {
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r2626 r2652  
    7676    }
    7777
     78    public interface EditLayerChangeListener {
     79        void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer);
     80    }
     81
    7882    /**
    7983     * the layer listeners
    8084     */
    8185    private static final Collection<MapView.LayerChangeListener> layerChangeListeners = new CopyOnWriteArrayList<MapView.LayerChangeListener>();
     86    private static final Collection<EditLayerChangeListener> editLayerChangeListeners = new CopyOnWriteArrayList<EditLayerChangeListener>();
    8287
    8388    /**
     
    9297    }
    9398
     99    public static void removeEditLayerChangeListener(EditLayerChangeListener listener) {
     100        if (listener != null && editLayerChangeListeners.contains(listener)) {
     101            editLayerChangeListeners.remove(listener);
     102        }
     103    }
     104
     105
     106
    94107    /**
    95108     * Adds a layer change listener
     
    103116    }
    104117
     118    public static void addEditLayerChangeListener(EditLayerChangeListener listener) {
     119        if (listener != null && ! layerChangeListeners.contains(listener)) {
     120            editLayerChangeListeners.add(listener);
     121        }
     122    }
     123
    105124    protected static void fireActiveLayerChanged(Layer oldLayer, Layer newLayer) {
    106125        for (LayerChangeListener l : layerChangeListeners) {
     
    121140    }
    122141
     142    protected static void fireEditLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
     143        for (EditLayerChangeListener l : editLayerChangeListeners) {
     144            l.editLayerChanged(oldLayer, newLayer);
     145        }
     146    }
     147
    123148    /**
    124149     * A list of all layers currently loaded.
     
    134159     */
    135160    private Layer activeLayer;
     161
     162    private OsmDataLayer editLayer;
    136163
    137164    /**
     
    255282    @Override
    256283    protected DataSet getCurrentDataSet() {
    257         if(activeLayer != null && activeLayer instanceof OsmDataLayer)
    258             return ((OsmDataLayer)activeLayer).data;
    259         return null;
     284        if (editLayer != null)
     285            return editLayer.data;
     286        else
     287            return null;
    260288    }
    261289
     
    266294     */
    267295    public boolean isActiveLayerDrawable() {
    268         return activeLayer != null && activeLayer instanceof OsmDataLayer;
     296        return editLayer != null;
    269297    }
    270298
     
    275303     */
    276304    public boolean isActiveLayerVisible() {
    277         return isActiveLayerDrawable() && activeLayer.isVisible();
     305        return isActiveLayerDrawable() && editLayer.isVisible();
    278306    }
    279307
     
    290318     * @return the next active data layer
    291319     */
    292     protected Layer determineNextActiveLayer() {
    293         if (layers.isEmpty()) return null;
    294         // if possible, activate the first data layer
    295         //
    296         List<OsmDataLayer> dataLayers = getLayersOfType(OsmDataLayer.class);
    297         if (!dataLayers.isEmpty())
    298             return dataLayers.get(0);
    299 
    300         // else the first layer of any type
    301         //
    302         return layers.get(0);
     320    protected Layer determineNextActiveLayer(Layer ignoredLayer) {
     321        // First look for data layer
     322        for (Layer layer:layers) {
     323            if (layer instanceof OsmDataLayer && layer != ignoredLayer)
     324                return layer;
     325        }
     326
     327        // Then any layer
     328        for (Layer layer:layers) {
     329            if (layer != ignoredLayer)
     330                return layer;
     331        }
     332
     333        // and then give up
     334        return null;
     335
    303336    }
    304337
     
    308341     */
    309342    public void removeLayer(Layer layer) {
    310         boolean deletedLayerWasActiveLayer = false;
    311 
    312343        if (layer == activeLayer) {
    313             activeLayer = null;
    314             deletedLayerWasActiveLayer = true;
    315             fireActiveLayerChanged(layer, null);
     344            setActiveLayer(determineNextActiveLayer(activeLayer));
    316345        }
    317346        if (layers.remove(layer)) {
     
    321350        layer.destroy();
    322351        AudioPlayer.reset();
    323         if (deletedLayerWasActiveLayer) {
    324             Layer l = determineNextActiveLayer();
    325             if (l != null) {
    326                 activeLayer = l;
    327                 fireActiveLayerChanged(null, l);
    328             }
    329         }
    330352        repaint();
    331353    }
     
    362384            layers.add(pos, layer);
    363385        }
     386        setEditLayer();
    364387        AudioPlayer.reset();
    365388        repaint();
     
    561584    }
    562585
     586    private void setEditLayer() {
     587        OsmDataLayer newEditLayer = null;
     588        OsmDataLayer oldEditLayer = editLayer;
     589
     590        // Find new edit layer
     591        if (activeLayer != editLayer) {
     592            if (activeLayer instanceof OsmDataLayer) {
     593                newEditLayer = (OsmDataLayer) activeLayer;
     594            } else {
     595                for (Layer layer:layers) {
     596                    if (layer instanceof OsmDataLayer) {
     597                        newEditLayer = (OsmDataLayer) layer;
     598                        break;
     599                    }
     600                }
     601            }
     602        }
     603
     604        // Set new edit layer
     605        if (newEditLayer != editLayer) {
     606            if (newEditLayer == null) {
     607                getCurrentDataSet().setSelected();
     608            }
     609
     610            editLayer = newEditLayer;
     611            fireEditLayerChanged(oldEditLayer, newEditLayer);
     612            refreshTitle();
     613        }
     614
     615    }
     616
    563617    /**
    564618     * Sets the active layer to <code>layer</code>. If <code>layer</code> is an instance
     
    571625        if (!layers.contains(layer))
    572626            throw new IllegalArgumentException(tr("Layer ''{0}'' must be in list of layers", layer.toString()));
    573         if (! (layer instanceof OsmDataLayer)) {
    574             if (getCurrentDataSet() != null) {
    575                 getCurrentDataSet().setSelected();
    576             }
    577         }
     627
     628        if (layer == activeLayer)
     629            return;
     630
    578631        Layer old = activeLayer;
    579632        activeLayer = layer;
    580         if (old != layer) {
    581             fireActiveLayerChanged(old, layer);
    582         }
    583         if (layer instanceof OsmDataLayer) {
    584             refreshTitle((OsmDataLayer)layer);
    585         }
     633        fireActiveLayerChanged(old, layer);
     634
     635        setEditLayer();
    586636
    587637        /* This only makes the buttons look disabled. Disabling the actions as well requires
     
    612662     */
    613663    public OsmDataLayer getEditLayer() {
    614         if (activeLayer instanceof OsmDataLayer)
    615             return (OsmDataLayer)activeLayer;
    616 
    617         // the first OsmDataLayer is the edit layer
    618         //
    619         for (Layer layer : layers) {
    620             if (layer instanceof OsmDataLayer)
    621                 return (OsmDataLayer)layer;
    622         }
    623         return null;
     664        return editLayer;
    624665    }
    625666
     
    678719            OsmDataLayer layer = (OsmDataLayer)evt.getSource();
    679720            if (layer == getEditLayer()) {
    680                 refreshTitle(layer);
    681             }
    682         }
    683     }
    684 
    685     protected void refreshTitle(OsmDataLayer layer) {
    686         boolean dirty = layer.requiresSaveToFile() || layer.requiresUploadToServer();
     721                refreshTitle();
     722            }
     723        }
     724    }
     725
     726    protected void refreshTitle() {
     727        boolean dirty = editLayer != null && (editLayer.requiresSaveToFile() || editLayer.requiresUploadToServer());
    687728        if (dirty) {
    688729            JOptionPane.getFrameForComponent(Main.parent).setTitle("* " + tr("Java OpenStreetMap Editor"));
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetsInActiveDataLayerListModel.java

    r2621 r2652  
    66import org.openstreetmap.josm.Main;
    77import org.openstreetmap.josm.gui.MapView;
    8 import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    98import org.openstreetmap.josm.gui.layer.DataChangeListener;
    109import org.openstreetmap.josm.gui.layer.Layer;
Note: See TracChangeset for help on using the changeset viewer.