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

Last change on this file since 8469 was 8324, checked in by Don-vip, 9 years ago

squid:S1133 - remove deprecated code

  • 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, String toolbarId, boolean installAdapters) {
76 super(name);
77 if(icon != null)
78 icon.getResource().getImageIcon(this);
79 setHelpId();
80 sc = shortcut;
81 if (sc != null) {
82 Main.registerActionShortcut(this, sc);
83 }
84 setTooltip(tooltip);
85 if (getValue("toolbar") == null) {
86 putValue("toolbar", toolbarId);
87 }
88 if (registerInToolbar && Main.toolbar != null) {
89 Main.toolbar.register(this);
90 }
91 if (installAdapters) {
92 installAdapters();
93 }
94 }
95
96 /**
97 * The new super for all actions.
98 *
99 * Use this super constructor to setup your action.
100 *
101 * @param name the action's text as displayed on the menu (if it is added to a menu)
102 * @param iconName the filename of the icon to use
103 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
104 * that html is not supported for menu actions on some platforms.
105 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
106 * do want a shortcut, remember you can always register it with group=none, so you
107 * won't be assigned a shortcut unless the user configures one. If you pass null here,
108 * the user CANNOT configure a shortcut for your action.
109 * @param registerInToolbar register this action for the toolbar preferences?
110 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
111 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
112 */
113 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
114 this(name, iconName == null ? null : new ImageProvider(iconName), tooltip, shortcut, registerInToolbar,
115 toolbarId == null ? iconName : toolbarId, installAdapters);
116 }
117
118 /**
119 * Constructs a new {@code JosmAction}.
120 *
121 * Use this super constructor to setup your action.
122 *
123 * @param name the action's text as displayed on the menu (if it is added to a menu)
124 * @param iconName the filename of the icon to use
125 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
126 * that html is not supported for menu actions on some platforms.
127 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
128 * do want a shortcut, remember you can always register it with group=none, so you
129 * won't be assigned a shortcut unless the user configures one. If you pass null here,
130 * the user CANNOT configure a shortcut for your action.
131 * @param registerInToolbar register this action for the toolbar preferences?
132 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
133 */
134 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar, boolean installAdapters) {
135 this(name, iconName, tooltip, shortcut, registerInToolbar, null, installAdapters);
136 }
137
138 /**
139 * Constructs a new {@code JosmAction}.
140 *
141 * Use this super constructor to setup your action.
142 *
143 * @param name the action's text as displayed on the menu (if it is added to a menu)
144 * @param iconName the filename of the icon to use
145 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
146 * that html is not supported for menu actions on some platforms.
147 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
148 * do want a shortcut, remember you can always register it with group=none, so you
149 * won't be assigned a shortcut unless the user configures one. If you pass null here,
150 * the user CANNOT configure a shortcut for your action.
151 * @param registerInToolbar register this action for the toolbar preferences?
152 */
153 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
154 this(name, iconName, tooltip, shortcut, registerInToolbar, null, true);
155 }
156
157 /**
158 * Constructs a new {@code JosmAction}.
159 */
160 public JosmAction() {
161 this(true);
162 }
163
164 /**
165 * Constructs a new {@code JosmAction}.
166 *
167 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
168 */
169 public JosmAction(boolean installAdapters) {
170 setHelpId();
171 if (installAdapters) {
172 installAdapters();
173 }
174 }
175
176 @Override
177 public void destroy() {
178 if (sc != null) {
179 Main.unregisterActionShortcut(this);
180 }
181 MapView.removeLayerChangeListener(layerChangeAdapter);
182 DataSet.removeSelectionListener(selectionChangeAdapter);
183 }
184
185 private void setHelpId() {
186 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
187 if (helpId.endsWith("Action")) {
188 helpId = helpId.substring(0, helpId.length()-6);
189 }
190 putValue("help", helpId);
191 }
192
193 /**
194 * Sets the tooltip text of this action.
195 * @param tooltip The text to display in tooltip. Can be {@code null}
196 */
197 public final void setTooltip(String tooltip) {
198 if (tooltip != null) {
199 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
200 }
201 }
202
203 /**
204 * Replies the current edit layer
205 *
206 * @return the current edit layer. null, if no edit layer exists
207 */
208 protected static OsmDataLayer getEditLayer() {
209 return Main.main != null ? Main.main.getEditLayer() : null;
210 }
211
212 /**
213 * Replies the current dataset
214 *
215 * @return the current dataset. null, if no current dataset exists
216 */
217 protected static DataSet getCurrentDataSet() {
218 return Main.main != null ? Main.main.getCurrentDataSet() : null;
219 }
220
221 protected void installAdapters() {
222 // make this action listen to layer change and selection change events
223 //
224 layerChangeAdapter = new LayerChangeAdapter();
225 selectionChangeAdapter = new SelectionChangeAdapter();
226 MapView.addLayerChangeListener(layerChangeAdapter);
227 DataSet.addSelectionListener(selectionChangeAdapter);
228 initEnabledState();
229 }
230
231 /**
232 * Override in subclasses to init the enabled state of an action when it is
233 * created. Default behaviour is to call {@link #updateEnabledState()}
234 *
235 * @see #updateEnabledState()
236 * @see #updateEnabledState(Collection)
237 */
238 protected void initEnabledState() {
239 updateEnabledState();
240 }
241
242 /**
243 * Override in subclasses to update the enabled state of the action when
244 * something in the JOSM state changes, i.e. when a layer is removed or added.
245 *
246 * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
247 * of selected primitives.
248 *
249 * Default behavior is empty.
250 *
251 * @see #updateEnabledState(Collection)
252 * @see #initEnabledState()
253 */
254 protected void updateEnabledState() {
255 }
256
257 /**
258 * Override in subclasses to update the enabled state of the action if the
259 * collection of selected primitives changes. This method is called with the
260 * new selection.
261 *
262 * @param selection the collection of selected primitives; may be empty, but not null
263 *
264 * @see #updateEnabledState()
265 * @see #initEnabledState()
266 */
267 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
268 }
269
270 /**
271 * Adapter for layer change events
272 *
273 */
274 private class LayerChangeAdapter implements MapView.LayerChangeListener {
275 private void updateEnabledStateInEDT() {
276 GuiHelper.runInEDT(new Runnable() {
277 @Override public void run() {
278 updateEnabledState();
279 }
280 });
281 }
282 @Override
283 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
284 updateEnabledStateInEDT();
285 }
286
287 @Override
288 public void layerAdded(Layer newLayer) {
289 updateEnabledStateInEDT();
290 }
291
292 @Override
293 public void layerRemoved(Layer oldLayer) {
294 updateEnabledStateInEDT();
295 }
296 }
297
298 /**
299 * Adapter for selection change events
300 */
301 private class SelectionChangeAdapter implements SelectionChangedListener {
302 @Override
303 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
304 updateEnabledState(newSelection);
305 }
306 }
307}
Note: See TracBrowser for help on using the repository browser.