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

Last change on this file since 10010 was 10000, checked in by Don-vip, 8 years ago

sonar - fix various issues

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