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

Last change on this file since 5299 was 5275, checked in by bastiK, 12 years ago

doc improvements

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