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

Last change on this file since 6099 was 6099, checked in by akks, 11 years ago

see #8921: avoid setTooltipText(null) (gives NPE when tooltip is shown on some JVMs)

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