1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.beans.PropertyChangeListener;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
---|
7 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
---|
8 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
9 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Abstract super-class of all upload actions.
|
---|
13 | * Listens to layer change events to update its enabled state.
|
---|
14 | * @since 15513
|
---|
15 | */
|
---|
16 | public abstract class AbstractUploadAction extends JosmAction {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a new {@code AbstractUploadAction}.
|
---|
20 | *
|
---|
21 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
22 | * @param iconName the filename of the icon to use
|
---|
23 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
24 | * that html is not supported for menu actions on some platforms.
|
---|
25 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
26 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
27 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
28 | * the user CANNOT configure a shortcut for your action.
|
---|
29 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
30 | */
|
---|
31 | protected AbstractUploadAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
---|
32 | boolean registerInToolbar) {
|
---|
33 | super(name, iconName, tooltip, shortcut, registerInToolbar);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private final PropertyChangeListener updateOnRequireUploadChange = evt -> {
|
---|
37 | if (OsmDataLayer.REQUIRES_UPLOAD_TO_SERVER_PROP.equals(evt.getPropertyName())) {
|
---|
38 | updateEnabledState();
|
---|
39 | }
|
---|
40 | };
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | protected LayerChangeAdapter buildLayerChangeAdapter() {
|
---|
44 | return new LayerChangeAdapter() {
|
---|
45 | @Override
|
---|
46 | public void layerAdded(LayerAddEvent e) {
|
---|
47 | if (e.getAddedLayer() instanceof OsmDataLayer) {
|
---|
48 | e.getAddedLayer().addPropertyChangeListener(updateOnRequireUploadChange);
|
---|
49 | }
|
---|
50 | super.layerAdded(e);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void layerRemoving(LayerRemoveEvent e) {
|
---|
55 | if (e.getRemovedLayer() instanceof OsmDataLayer) {
|
---|
56 | e.getRemovedLayer().removePropertyChangeListener(updateOnRequireUploadChange);
|
---|
57 | }
|
---|
58 | super.layerRemoving(e);
|
---|
59 | }
|
---|
60 | };
|
---|
61 | }
|
---|
62 | }
|
---|