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

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

refactor duplicated code

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