Changeset 10508 in josm


Ignore:
Timestamp:
2016-07-03T18:26:16+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #13078 - Add new listener that fires before first layer is added and after last one is removed (patch by michael2402) - gsoc-core

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
3 edited

Legend:

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

    r10467 r10508  
    397397        I18n.setupLanguageFonts();
    398398
    399         // Can only be called after preferences are initialized.
    400         // We can move this to MainPanel constructor as soon as noone depends on Main#panel any more.
    401         GuiHelper.runInEDTAndWait(new Runnable() {
    402             @Override
    403             public void run() {
    404                 mainPanel.updateContent();
    405             }
    406         });
    407 
    408399        WindowGeometry geometry = WindowGeometry.mainWindow("gui.geometry",
    409400                args.containsKey(Option.GEOMETRY) ? args.get(Option.GEOMETRY).iterator().next() : null,
  • trunk/src/org/openstreetmap/josm/gui/MainPanel.java

    r10444 r10508  
    77
    88import javax.swing.JPanel;
    9 import javax.swing.SwingUtilities;
    109
    1110import org.openstreetmap.josm.Main;
    1211import org.openstreetmap.josm.actions.mapmode.MapMode;
    1312import org.openstreetmap.josm.gui.layer.Layer;
    14 import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
    15 import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
    16 import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
    17 import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
    1813import org.openstreetmap.josm.gui.layer.MainLayerManager;
     14import org.openstreetmap.josm.gui.layer.MainLayerManager.LayerAvailabilityEvent;
     15import org.openstreetmap.josm.gui.layer.MainLayerManager.LayerAvailabilityListener;
    1916import org.openstreetmap.josm.gui.util.GuiHelper;
    2017
     
    4542    /**
    4643     * Update the content of this {@link MainFrame} to either display the map or display the welcome screen.
     44     * @param showMap If the map should be displayed.
    4745     */
    48     protected void updateContent() {
     46    protected void updateContent(boolean showMap) {
    4947        GuiHelper.assertCallFromEdt();
    5048        MapFrame old = map;
    51         boolean showMap = !layerManager.getLayers().isEmpty();
    5249        if (old != null && showMap) {
    5350            // no state change
     
    155152     */
    156153    public void reAddListeners() {
    157         layerManager.addLayerChangeListener(new LayerChangeListener() {
     154        layerManager.addLayerAvailabilityListener(new LayerAvailabilityListener() {
    158155            @Override
    159             public void layerAdded(LayerAddEvent e) {
    160                 updateContent();
     156            public void beforeFirstLayerAdded(LayerAvailabilityEvent e) {
     157                updateContent(true);
    161158            }
    162159
    163160            @Override
    164             public void layerRemoving(final LayerRemoveEvent e) {
    165                 // Delay main.map removal until after all listeners are finished.
    166                 // Some components rely on this and e.g. get the MapView that way.
    167                 SwingUtilities.invokeLater(new Runnable() {
    168                     @Override
    169                     public void run() {
    170                         updateContent();
    171                     }
    172                 });
     161            public void afterLastLayerRemoved(LayerAvailabilityEvent e) {
     162                updateContent(false);
    173163            }
    174 
     164        });
     165        GuiHelper.runInEDTAndWait(new Runnable() {
    175166            @Override
    176             public void layerOrderChanged(LayerOrderChangeEvent e) {
    177                 // ignored
     167            public void run() {
     168                updateContent(!layerManager.getLayers().isEmpty());
    178169            }
    179170        });
  • trunk/src/org/openstreetmap/josm/gui/layer/MainLayerManager.java

    r10507 r10508  
    9696
    9797    /**
     98     * This event is fired for {@link LayerAvailabilityListener}
     99     * @author Michael Zangl
     100     * @since 10508
     101     */
     102    public class LayerAvailabilityEvent extends LayerManagerEvent {
     103        private final boolean hasLayers;
     104
     105        LayerAvailabilityEvent(LayerManager source, boolean hasLayers) {
     106            super(source);
     107            this.hasLayers = hasLayers;
     108        }
     109
     110        /**
     111         * Checks if this layer manager will have layers afterwards
     112         * @return true if layers will be added.
     113         */
     114        public boolean hasLayers() {
     115            return hasLayers;
     116        }
     117    }
     118
     119    /**
     120     * A listener that gets informed before any layer is displayed and after all layers are removed.
     121     * @author Michael Zangl
     122     * @since 10508
     123     */
     124    public interface LayerAvailabilityListener {
     125        /**
     126         * This method is called in the UI thread right before the first layer is added.
     127         * @param e The event.
     128         */
     129        void beforeFirstLayerAdded(LayerAvailabilityEvent e);
     130
     131        /**
     132         * This method is called in the UI thread after the last layer was removed.
     133         * @param e The event.
     134         */
     135        void afterLastLayerRemoved(LayerAvailabilityEvent e);
     136    }
     137
     138    /**
    98139     * The layer from the layers list that is currently active.
    99140     */
     
    106147
    107148    private final List<ActiveLayerChangeListener> activeLayerChangeListeners = new CopyOnWriteArrayList<>();
     149    private final List<LayerAvailabilityListener> layerAvailabilityListeners = new CopyOnWriteArrayList<>();
    108150
    109151    /**
     
    154196        }
    155197        activeLayerChangeListeners.remove(listener);
     198    }
     199
     200    /**
     201     * Add a new {@link LayerAvailabilityListener}.
     202     * @param listener The listener
     203     * @since 10508
     204     */
     205    public synchronized void addLayerAvailabilityListener(LayerAvailabilityListener listener) {
     206        if (!layerAvailabilityListeners.add(listener)) {
     207            throw new IllegalArgumentException("Attempted to add listener that was already in list: " + listener);
     208        }
     209    }
     210
     211    /**
     212     * Remove an {@link LayerAvailabilityListener}.
     213     * @param listener The listener
     214     * @since 10508
     215     */
     216    public synchronized void removeLayerAvailabilityListener(LayerAvailabilityListener listener) {
     217        if (!layerAvailabilityListeners.remove(listener)) {
     218            throw new IllegalArgumentException("Attempted to remove listener that was not in list: " + listener);
     219        }
    156220    }
    157221
     
    199263    @Override
    200264    protected synchronized void realAddLayer(Layer layer) {
     265        if (getLayers().isEmpty()) {
     266            LayerAvailabilityEvent e = new LayerAvailabilityEvent(this, true);
     267            for (LayerAvailabilityListener l : layerAvailabilityListeners) {
     268                l.beforeFirstLayerAdded(e);
     269            }
     270        }
    201271        super.realAddLayer(layer);
    202272
     
    214284        }
    215285
    216         return super.realRemoveSingleLayer(layer);
     286        Collection<Layer> toDelete = super.realRemoveSingleLayer(layer);
     287        if (getLayers().isEmpty()) {
     288            LayerAvailabilityEvent e = new LayerAvailabilityEvent(this, false);
     289            for (LayerAvailabilityListener l : layerAvailabilityListeners) {
     290                l.afterLastLayerRemoved(e);
     291            }
     292        }
     293        return toDelete;
    217294    }
    218295
     
    320397
    321398        activeLayerChangeListeners.clear();
     399        layerAvailabilityListeners.clear();
    322400    }
    323401}
Note: See TracChangeset for help on using the changeset viewer.