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

Last change on this file since 9276 was 8957, checked in by simon04, 8 years ago

see #10350 - Allow subclasses of JosmAction access its inner classes

  • Property svn:eol-style set to native
File size: 12.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.KeyEvent;
7import java.util.Collection;
8
9import javax.swing.AbstractAction;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.SelectionChangedListener;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.gui.MapView;
16import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
17import org.openstreetmap.josm.gui.layer.Layer;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20import org.openstreetmap.josm.tools.Destroyable;
21import org.openstreetmap.josm.tools.ImageProvider;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * Base class helper for all Actions in JOSM. Just to make the life easier.
26 *
27 * A JosmAction is a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
28 * a layer change event or a selection change event it invokes {@link #updateEnabledState()}.
29 * Subclasses can override {@link #updateEnabledState()} in order to update the {@link #isEnabled()}-state
30 * of a JosmAction depending on the {@link #getCurrentDataSet()} and the current layers
31 * (see also {@link #getEditLayer()}).
32 *
33 * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
34 * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
35 * be called (currently).
36 *
37 * @author imi
38 */
39public abstract class JosmAction extends AbstractAction implements Destroyable {
40
41 protected transient Shortcut sc;
42 private transient LayerChangeAdapter layerChangeAdapter;
43 private transient SelectionChangeAdapter selectionChangeAdapter;
44
45 /**
46 * Returns the shortcut for this action.
47 * @return the shortcut for this action, or "No shortcut" if none is defined
48 */
49 public Shortcut getShortcut() {
50 if (sc == null) {
51 sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
52 // as this shortcut is shared by all action that don't want to have a shortcut,
53 // we shouldn't allow the user to change it...
54 // this is handled by special name "core:none"
55 }
56 return sc;
57 }
58
59 /**
60 * Constructs a {@code JosmAction}.
61 *
62 * @param name the action's text as displayed on the menu (if it is added to a menu)
63 * @param icon the icon to use
64 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
65 * that html is not supported for menu actions on some platforms.
66 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
67 * do want a shortcut, remember you can always register it with group=none, so you
68 * won't be assigned a shortcut unless the user configures one. If you pass null here,
69 * the user CANNOT configure a shortcut for your action.
70 * @param registerInToolbar register this action for the toolbar preferences?
71 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
72 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
73 * TODO: do not pass Icon, pass ImageProvider instead
74 */
75 public JosmAction(String name, ImageProvider icon, String tooltip, Shortcut shortcut, boolean registerInToolbar,
76 String toolbarId, boolean installAdapters) {
77 super(name);
78 if (icon != null)
79 icon.getResource().getImageIcon(this);
80 setHelpId();
81 sc = shortcut;
82 if (sc != null) {
83 Main.registerActionShortcut(this, sc);
84 }
85 setTooltip(tooltip);
86 if (getValue("toolbar") == null) {
87 putValue("toolbar", toolbarId);
88 }
89 if (registerInToolbar && Main.toolbar != null) {
90 Main.toolbar.register(this);
91 }
92 if (installAdapters) {
93 installAdapters();
94 }
95 }
96
97 /**
98 * The new super for all actions.
99 *
100 * Use this super constructor to setup your action.
101 *
102 * @param name the action's text as displayed on the menu (if it is added to a menu)
103 * @param iconName the filename of the icon to use
104 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
105 * that html is not supported for menu actions on some platforms.
106 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
107 * do want a shortcut, remember you can always register it with group=none, so you
108 * won't be assigned a shortcut unless the user configures one. If you pass null here,
109 * the user CANNOT configure a shortcut for your action.
110 * @param registerInToolbar register this action for the toolbar preferences?
111 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
112 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
113 */
114 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar,
115 String toolbarId, boolean installAdapters) {
116 this(name, iconName == null ? null : new ImageProvider(iconName), tooltip, shortcut, registerInToolbar,
117 toolbarId == null ? iconName : toolbarId, installAdapters);
118 }
119
120 /**
121 * Constructs a new {@code JosmAction}.
122 *
123 * Use this super constructor to setup your action.
124 *
125 * @param name the action's text as displayed on the menu (if it is added to a menu)
126 * @param iconName the filename of the icon to use
127 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
128 * that html is not supported for menu actions on some platforms.
129 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
130 * do want a shortcut, remember you can always register it with group=none, so you
131 * won't be assigned a shortcut unless the user configures one. If you pass null here,
132 * the user CANNOT configure a shortcut for your action.
133 * @param registerInToolbar register this action for the toolbar preferences?
134 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
135 */
136 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, boolean installAdapters) {
137 this(name, iconName, tooltip, shortcut, registerInToolbar, null, installAdapters);
138 }
139
140 /**
141 * Constructs a new {@code JosmAction}.
142 *
143 * Use this super constructor to setup your action.
144 *
145 * @param name the action's text as displayed on the menu (if it is added to a menu)
146 * @param iconName the filename of the icon to use
147 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
148 * that html is not supported for menu actions on some platforms.
149 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
150 * do want a shortcut, remember you can always register it with group=none, so you
151 * won't be assigned a shortcut unless the user configures one. If you pass null here,
152 * the user CANNOT configure a shortcut for your action.
153 * @param registerInToolbar register this action for the toolbar preferences?
154 */
155 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
156 this(name, iconName, tooltip, shortcut, registerInToolbar, null, true);
157 }
158
159 /**
160 * Constructs a new {@code JosmAction}.
161 */
162 public JosmAction() {
163 this(true);
164 }
165
166 /**
167 * Constructs a new {@code JosmAction}.
168 *
169 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
170 */
171 public JosmAction(boolean installAdapters) {
172 setHelpId();
173 if (installAdapters) {
174 installAdapters();
175 }
176 }
177
178 @Override
179 public void destroy() {
180 if (sc != null) {
181 Main.unregisterActionShortcut(this);
182 }
183 MapView.removeLayerChangeListener(layerChangeAdapter);
184 DataSet.removeSelectionListener(selectionChangeAdapter);
185 }
186
187 private void setHelpId() {
188 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
189 if (helpId.endsWith("Action")) {
190 helpId = helpId.substring(0, helpId.length()-6);
191 }
192 putValue("help", helpId);
193 }
194
195 /**
196 * Sets the tooltip text of this action.
197 * @param tooltip The text to display in tooltip. Can be {@code null}
198 */
199 public final void setTooltip(String tooltip) {
200 if (tooltip != null) {
201 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
202 }
203 }
204
205 /**
206 * Replies the current edit layer
207 *
208 * @return the current edit layer. null, if no edit layer exists
209 */
210 protected static OsmDataLayer getEditLayer() {
211 return Main.main != null ? Main.main.getEditLayer() : null;
212 }
213
214 /**
215 * Replies the current dataset
216 *
217 * @return the current dataset. null, if no current dataset exists
218 */
219 protected static DataSet getCurrentDataSet() {
220 return Main.main != null ? Main.main.getCurrentDataSet() : null;
221 }
222
223 protected void installAdapters() {
224 // make this action listen to layer change and selection change events
225 //
226 layerChangeAdapter = new LayerChangeAdapter();
227 selectionChangeAdapter = new SelectionChangeAdapter();
228 MapView.addLayerChangeListener(layerChangeAdapter);
229 DataSet.addSelectionListener(selectionChangeAdapter);
230 initEnabledState();
231 }
232
233 /**
234 * Override in subclasses to init the enabled state of an action when it is
235 * created. Default behaviour is to call {@link #updateEnabledState()}
236 *
237 * @see #updateEnabledState()
238 * @see #updateEnabledState(Collection)
239 */
240 protected void initEnabledState() {
241 updateEnabledState();
242 }
243
244 /**
245 * Override in subclasses to update the enabled state of the action when
246 * something in the JOSM state changes, i.e. when a layer is removed or added.
247 *
248 * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
249 * of selected primitives.
250 *
251 * Default behavior is empty.
252 *
253 * @see #updateEnabledState(Collection)
254 * @see #initEnabledState()
255 */
256 protected void updateEnabledState() {
257 }
258
259 /**
260 * Override in subclasses to update the enabled state of the action if the
261 * collection of selected primitives changes. This method is called with the
262 * new selection.
263 *
264 * @param selection the collection of selected primitives; may be empty, but not null
265 *
266 * @see #updateEnabledState()
267 * @see #initEnabledState()
268 */
269 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
270 }
271
272 /**
273 * Adapter for layer change events
274 *
275 */
276 protected class LayerChangeAdapter implements MapView.LayerChangeListener {
277 private void updateEnabledStateInEDT() {
278 GuiHelper.runInEDT(new Runnable() {
279 @Override public void run() {
280 updateEnabledState();
281 }
282 });
283 }
284
285 @Override
286 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
287 updateEnabledStateInEDT();
288 }
289
290 @Override
291 public void layerAdded(Layer newLayer) {
292 updateEnabledStateInEDT();
293 }
294
295 @Override
296 public void layerRemoved(Layer oldLayer) {
297 updateEnabledStateInEDT();
298 }
299 }
300
301 /**
302 * Adapter for selection change events
303 */
304 protected class SelectionChangeAdapter implements SelectionChangedListener {
305 @Override
306 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
307 updateEnabledState(newSelection);
308 }
309 }
310}
Note: See TracBrowser for help on using the repository browser.