source: josm/trunk/src/org/openstreetmap/josm/actions/JosmAction.java@ 4851

Last change on this file since 4851 was 4733, checked in by bastiK, 12 years ago

fixed #7174 - identifier for toolbar actions is icon name

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7
8import javax.swing.AbstractAction;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.SelectionChangedListener;
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.gui.MapView;
15import org.openstreetmap.josm.gui.layer.Layer;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.tools.Destroyable;
18import org.openstreetmap.josm.tools.ImageProvider;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * Base class helper for all Actions in JOSM. Just to make the life easier.
23 *
24 * A JosmAction is a {@see LayerChangeListener} and a {@see SelectionChangedListener}. Upon
25 * a layer change event or a selection change event it invokes {@see #updateEnabled()}.
26 * Subclasses can override {@see #updateEnabled()} in order to update the {@see #isEnabled()}-state
27 * of a JosmAction depending on the {@see #getCurrentDataSet()} and the current layers
28 * (see also {@see #getEditLayer()}).
29 *
30 * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
31 * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
32 * be called (currently).
33 *
34 * @author imi
35 */
36abstract public class JosmAction extends AbstractAction implements Destroyable {
37
38 protected Shortcut sc;
39 private LayerChangeAdapter layerChangeAdapter;
40 private SelectionChangeAdapter selectionChangeAdapter;
41
42 public Shortcut getShortcut() {
43 if (sc == null) {
44 sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), 0, Shortcut.GROUP_NONE);
45 // as this shortcut is shared by all action that don't want to have a shortcut,
46 // we shouldn't allow the user to change it...
47 // this is handled by special name "core:none"
48 }
49 return sc;
50 }
51
52 /**
53 * The new super for all actions.
54 *
55 * Use this super constructor to setup your action.
56 *
57 * @param name the action's text as displayed on the menu (if it is added to a menu)
58 * @param iconName the filename of the icon to use
59 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
60 * that html is not supported for menu actions on some platforms.
61 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
62 * do want a shortcut, remember you can always register it with group=none, so you
63 * won't be assigned a shortcut unless the user configures one. If you pass null here,
64 * the user CANNOT configure a shortcut for your action.
65 * @param register register this action for the toolbar preferences?
66 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
67 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
68 */
69 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
70 super(name, iconName == null ? null : ImageProvider.get(iconName));
71 setHelpId();
72 sc = shortcut;
73 if (sc != null) {
74 Main.registerActionShortcut(this, sc);
75 }
76 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
77 if (getValue("toolbar") == null) {
78 putValue("toolbar", toolbarId == null ? iconName : toolbarId);
79 }
80 if (register) {
81 Main.toolbar.register(this);
82 }
83 if (installAdapters) {
84 installAdapters();
85 }
86 }
87
88 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, boolean installAdapters) {
89 this(name, iconName, tooltip, shortcut, register, null, installAdapters);
90 }
91
92 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
93 this(name, iconName, tooltip, shortcut, register, null, true);
94 }
95
96 public JosmAction() {
97 this(true);
98 }
99
100 public JosmAction(boolean installAdapters) {
101 setHelpId();
102 if (installAdapters) {
103 installAdapters();
104 }
105 }
106
107 public void destroy() {
108 if (sc != null) {
109 Main.unregisterActionShortcut(this);
110 }
111 MapView.removeLayerChangeListener(layerChangeAdapter);
112 DataSet.removeSelectionListener(selectionChangeAdapter);
113 }
114
115 private void setHelpId() {
116 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
117 if (helpId.endsWith("Action")) {
118 helpId = helpId.substring(0, helpId.length()-6);
119 }
120 putValue("help", helpId);
121 }
122
123 /**
124 * Replies the current edit layer
125 *
126 * @return the current edit layer. null, if no edit layer exists
127 */
128 protected static OsmDataLayer getEditLayer() {
129 return Main.main.getEditLayer();
130 }
131
132 /**
133 * Replies the current dataset
134 *
135 * @return the current dataset. null, if no current dataset exists
136 */
137 protected static DataSet getCurrentDataSet() {
138 return Main.main.getCurrentDataSet();
139 }
140
141 protected void installAdapters() {
142 // make this action listen to layer change and selection change events
143 //
144 layerChangeAdapter = new LayerChangeAdapter();
145 selectionChangeAdapter = new SelectionChangeAdapter();
146 MapView.addLayerChangeListener(layerChangeAdapter);
147 DataSet.addSelectionListener(selectionChangeAdapter);
148 initEnabledState();
149 }
150
151 /**
152 * Override in subclasses to init the enabled state of an action when it is
153 * created. Default behaviour is to call {@see #updateEnabledState()}
154 *
155 * @see #updateEnabledState()
156 * @see #updateEnabledState(Collection)
157 */
158 protected void initEnabledState() {
159 updateEnabledState();
160 }
161
162 /**
163 * Override in subclasses to update the enabled state of the action when
164 * something in the JOSM state changes, i.e. when a layer is removed or added.
165 *
166 * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
167 * of selected primitives.
168 *
169 * Default behavior is empty.
170 *
171 * @see #updateEnabledState(Collection)
172 * @see #initEnabledState()
173 */
174 protected void updateEnabledState() {
175 }
176
177 /**
178 * Override in subclasses to update the enabled state of the action if the
179 * collection of selected primitives changes. This method is called with the
180 * new selection.
181 *
182 * @param selection the collection of selected primitives; may be empty, but not null
183 *
184 * @see #updateEnabledState()
185 * @see #initEnabledState()
186 */
187 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
188 }
189
190 /**
191 * Adapter for layer change events
192 *
193 */
194 private class LayerChangeAdapter implements MapView.LayerChangeListener {
195 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
196 updateEnabledState();
197 }
198
199 public void layerAdded(Layer newLayer) {
200 updateEnabledState();
201 }
202
203 public void layerRemoved(Layer oldLayer) {
204 updateEnabledState();
205 }
206 }
207
208 /**
209 * Adapter for selection change events
210 *
211 */
212 private class SelectionChangeAdapter implements SelectionChangedListener {
213 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
214 updateEnabledState(newSelection);
215 }
216 }
217}
Note: See TracBrowser for help on using the repository browser.