Ignore:
Timestamp:
2016-06-11T21:34:38+02:00 (4 years ago)
Author:
stoecker
Message:

gsoc-core - patch by Michael Zangl - fix #12943 - cleanup josm action, allow actions to prevent listener registration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/JosmAction.java

    r10345 r10353  
    1616import org.openstreetmap.josm.data.osm.DataSet;
    1717import org.openstreetmap.josm.data.osm.OsmPrimitive;
     18import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
    1819import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
     20import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
     21import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
     22import org.openstreetmap.josm.gui.layer.MainLayerManager;
    1923import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
    2024import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
     
    2832 * Base class helper for all Actions in JOSM. Just to make the life easier.
    2933 *
    30  * A JosmAction is a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
     34 * This action allows you to set up an icon, a tooltip text, a globally registered shortcut, register it in the main toolbar and set up
     35 * layer/selection listeners that call {@link #updateEnabledState()} whenever the global context is changed.
     36 *
     37 * A JosmAction can register a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
    3138 * a layer change event or a selection change event it invokes {@link #updateEnabledState()}.
    3239 * Subclasses can override {@link #updateEnabledState()} in order to update the {@link #isEnabled()}-state
     
    4451    protected transient Shortcut sc;
    4552    private transient LayerChangeAdapter layerChangeAdapter;
     53    private transient ActiveLayerChangeAdapter activeLayerChangeAdapter;
    4654    private transient SelectionChangeAdapter selectionChangeAdapter;
    47 
    48     /**
    49      * Returns the shortcut for this action.
    50      * @return the shortcut for this action, or "No shortcut" if none is defined
    51      */
    52     public Shortcut getShortcut() {
    53         if (sc == null) {
    54             sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
    55             // as this shortcut is shared by all action that don't want to have a shortcut,
    56             // we shouldn't allow the user to change it...
    57             // this is handled by special name "core:none"
    58         }
    59         return sc;
    60     }
    6155
    6256    /**
     
    178172    }
    179173
     174
     175    /**
     176     * Installs the listeners to this action.
     177     * <p>
     178     * This should either never be called or only called in the constructor of this action.
     179     * <p>
     180     * All registered adapters should be removed in {@link #destroy()}
     181     */
     182    protected void installAdapters() {
     183        // make this action listen to layer change and selection change events
     184        if (listenToLayerChange()) {
     185            layerChangeAdapter = new LayerChangeAdapter();
     186            activeLayerChangeAdapter = new ActiveLayerChangeAdapter();
     187            getLayerManager().addLayerChangeListener(layerChangeAdapter);
     188            getLayerManager().addActiveLayerChangeListener(activeLayerChangeAdapter);
     189        }
     190        if (listenToSelectionChange()) {
     191            selectionChangeAdapter = new SelectionChangeAdapter();
     192            DataSet.addSelectionListener(selectionChangeAdapter);
     193        }
     194        initEnabledState();
     195    }
     196
     197    /**
     198     * Overwrite this if {@link #updateEnabledState()} should be called when the active / availabe layers change. Default is true.
     199     * @return <code>true</code> if a {@link LayerChangeListener} and a {@link ActiveLayerChangeListener} should be registered.
     200     * @since 10352
     201     */
     202    protected boolean listenToLayerChange() {
     203        return true;
     204    }
     205
     206    /**
     207     * Overwrite this if {@link #updateEnabledState()} should be called when the selection changed. Default is true.
     208     * @return <code>true</code> if a {@link SelectionChangedListener} should be registered.
     209     * @since 10352
     210     */
     211    protected boolean listenToSelectionChange() {
     212        return true;
     213    }
     214
    180215    @Override
    181216    public void destroy() {
     
    184219        }
    185220        if (layerChangeAdapter != null) {
    186             Main.getLayerManager().removeActiveLayerChangeListener(layerChangeAdapter);
    187         }
    188         DataSet.removeSelectionListener(selectionChangeAdapter);
     221            getLayerManager().removeLayerChangeListener(layerChangeAdapter);
     222            getLayerManager().removeActiveLayerChangeListener(activeLayerChangeAdapter);
     223        }
     224        if (selectionChangeAdapter != null) {
     225            DataSet.removeSelectionListener(selectionChangeAdapter);
     226        }
    189227    }
    190228
     
    198236
    199237    /**
     238     * Returns the shortcut for this action.
     239     * @return the shortcut for this action, or "No shortcut" if none is defined
     240     */
     241    public Shortcut getShortcut() {
     242        if (sc == null) {
     243            sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
     244            // as this shortcut is shared by all action that don't want to have a shortcut,
     245            // we shouldn't allow the user to change it...
     246            // this is handled by special name "core:none"
     247        }
     248        return sc;
     249    }
     250
     251    /**
    200252     * Sets the tooltip text of this action.
    201253     * @param tooltip The text to display in tooltip. Can be {@code null}
     
    208260
    209261    /**
     262     * Gets the layer manager used for this action. Defaults to the main layer manager but you can overwrite this.
     263     * <p>
     264     * The layer manager must be available when {@link #installAdapters()} is called and must not change.
     265     *
     266     * @return The layer manager.
     267     * @since 10352
     268     */
     269    public MainLayerManager getLayerManager() {
     270        return Main.getLayerManager();
     271    }
     272
     273    /**
    210274     * Replies the current edit layer
    211275     *
    212276     * @return the current edit layer. null, if no edit layer exists
    213      */
     277     * @deprecated Use {@link #getLayerManager()}.getEditLayer() instead. To be removed in end of 2016.
     278     */
     279    @Deprecated
    214280    public static OsmDataLayer getEditLayer() {
    215         return Main.main != null ? Main.main.getEditLayer() : null;
     281        return Main.getLayerManager().getEditLayer();
    216282    }
    217283
     
    220286     *
    221287     * @return the current dataset. null, if no current dataset exists
    222      */
     288     * @deprecated Use {@link #getLayerManager()}.getEditDataSet() instead. To be removed in end of 2016.
     289     */
     290    @Deprecated
    223291    public static DataSet getCurrentDataSet() {
    224         return Main.main != null ? Main.main.getCurrentDataSet() : null;
    225     }
    226 
    227     protected void installAdapters() {
    228         // make this action listen to layer change and selection change events
    229         layerChangeAdapter = new LayerChangeAdapter();
    230         selectionChangeAdapter = new SelectionChangeAdapter();
    231         Main.getLayerManager().addActiveLayerChangeListener(layerChangeAdapter);
    232         DataSet.addSelectionListener(selectionChangeAdapter);
    233         initEnabledState();
     292        return Main.getLayerManager().getEditDataSet();
    234293    }
    235294
     
    273332     * @see #updateEnabledState(Collection)
    274333     * @see #initEnabledState()
     334     * @see #listenToLayerChange()
    275335     */
    276336    protected void updateEnabledState() {
     
    286346     * @see #updateEnabledState()
    287347     * @see #initEnabledState()
     348     * @see #listenToSelectionChange()
    288349     */
    289350    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
     
    293354     * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed.
    294355     */
    295     protected class LayerChangeAdapter implements ActiveLayerChangeListener {
     356    protected class LayerChangeAdapter implements LayerChangeListener {
     357        @Override
     358        public void layerAdded(LayerAddEvent e) {
     359            updateEnabledState();
     360        }
     361
     362        @Override
     363        public void layerRemoving(LayerRemoveEvent e) {
     364            updateEnabledState();
     365        }
     366
     367        @Override
     368        public void layerOrderChanged(LayerOrderChangeEvent e) {
     369            updateEnabledState();
     370        }
     371
     372        @Override
     373        public String toString() {
     374            return "LayerChangeAdapter [" + JosmAction.this.toString() + ']';
     375        }
     376    }
     377
     378    /**
     379     * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed.
     380     */
     381    protected class ActiveLayerChangeAdapter implements ActiveLayerChangeListener {
    296382        @Override
    297383        public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
     
    301387        @Override
    302388        public String toString() {
    303             return "LayerChangeAdapter [" + JosmAction.this.toString() + ']';
     389            return "ActiveLayerChangeAdapter [" + JosmAction.this.toString() + ']';
    304390        }
    305391    }
Note: See TracChangeset for help on using the changeset viewer.