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

Last change on this file since 4982 was 4982, checked in by stoecker, 12 years ago

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

  • Property svn:eol-style set to native
File size: 7.9 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;
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.layer.Layer;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.tools.Destroyable;
19import org.openstreetmap.josm.tools.ImageProvider;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * Base class helper for all Actions in JOSM. Just to make the life easier.
24 *
25 * A JosmAction is a {@see LayerChangeListener} and a {@see SelectionChangedListener}. Upon
26 * a layer change event or a selection change event it invokes {@see #updateEnabled()}.
27 * Subclasses can override {@see #updateEnabled()} in order to update the {@see #isEnabled()}-state
28 * of a JosmAction depending on the {@see #getCurrentDataSet()} and the current layers
29 * (see also {@see #getEditLayer()}).
30 *
31 * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
32 * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
33 * be called (currently).
34 *
35 * @author imi
36 */
37abstract public class JosmAction extends AbstractAction implements Destroyable {
38
39 protected Shortcut sc;
40 private LayerChangeAdapter layerChangeAdapter;
41 private SelectionChangeAdapter selectionChangeAdapter;
42
43 public Shortcut getShortcut() {
44 if (sc == null) {
45 sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
46 // as this shortcut is shared by all action that don't want to have a shortcut,
47 // we shouldn't allow the user to change it...
48 // this is handled by special name "core:none"
49 }
50 return sc;
51 }
52
53 /**
54 * The new super for all actions.
55 *
56 * Use this super constructor to setup your action.
57 *
58 * @param name the action's text as displayed on the menu (if it is added to a menu)
59 * @param iconName the filename of 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 register 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, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
71 super(name, iconName == null ? null : ImageProvider.get(iconName));
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 == null ? iconName : toolbarId);
80 }
81 if (register) {
82 Main.toolbar.register(this);
83 }
84 if (installAdapters) {
85 installAdapters();
86 }
87 }
88
89 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, boolean installAdapters) {
90 this(name, iconName, tooltip, shortcut, register, null, installAdapters);
91 }
92
93 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
94 this(name, iconName, tooltip, shortcut, register, null, true);
95 }
96
97 public JosmAction() {
98 this(true);
99 }
100
101 public JosmAction(boolean installAdapters) {
102 setHelpId();
103 if (installAdapters) {
104 installAdapters();
105 }
106 }
107
108 public void destroy() {
109 if (sc != null) {
110 Main.unregisterActionShortcut(this);
111 }
112 MapView.removeLayerChangeListener(layerChangeAdapter);
113 DataSet.removeSelectionListener(selectionChangeAdapter);
114 }
115
116 private void setHelpId() {
117 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
118 if (helpId.endsWith("Action")) {
119 helpId = helpId.substring(0, helpId.length()-6);
120 }
121 putValue("help", helpId);
122 }
123
124 public void setTooltip(String tooltip) {
125 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
126 }
127
128 /**
129 * Replies the current edit layer
130 *
131 * @return the current edit layer. null, if no edit layer exists
132 */
133 protected static OsmDataLayer getEditLayer() {
134 return Main.main.getEditLayer();
135 }
136
137 /**
138 * Replies the current dataset
139 *
140 * @return the current dataset. null, if no current dataset exists
141 */
142 protected static DataSet getCurrentDataSet() {
143 return Main.main.getCurrentDataSet();
144 }
145
146 protected void installAdapters() {
147 // make this action listen to layer change and selection change events
148 //
149 layerChangeAdapter = new LayerChangeAdapter();
150 selectionChangeAdapter = new SelectionChangeAdapter();
151 MapView.addLayerChangeListener(layerChangeAdapter);
152 DataSet.addSelectionListener(selectionChangeAdapter);
153 initEnabledState();
154 }
155
156 /**
157 * Override in subclasses to init the enabled state of an action when it is
158 * created. Default behaviour is to call {@see #updateEnabledState()}
159 *
160 * @see #updateEnabledState()
161 * @see #updateEnabledState(Collection)
162 */
163 protected void initEnabledState() {
164 updateEnabledState();
165 }
166
167 /**
168 * Override in subclasses to update the enabled state of the action when
169 * something in the JOSM state changes, i.e. when a layer is removed or added.
170 *
171 * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
172 * of selected primitives.
173 *
174 * Default behavior is empty.
175 *
176 * @see #updateEnabledState(Collection)
177 * @see #initEnabledState()
178 */
179 protected void updateEnabledState() {
180 }
181
182 /**
183 * Override in subclasses to update the enabled state of the action if the
184 * collection of selected primitives changes. This method is called with the
185 * new selection.
186 *
187 * @param selection the collection of selected primitives; may be empty, but not null
188 *
189 * @see #updateEnabledState()
190 * @see #initEnabledState()
191 */
192 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
193 }
194
195 /**
196 * Adapter for layer change events
197 *
198 */
199 private class LayerChangeAdapter implements MapView.LayerChangeListener {
200 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
201 updateEnabledState();
202 }
203
204 public void layerAdded(Layer newLayer) {
205 updateEnabledState();
206 }
207
208 public void layerRemoved(Layer oldLayer) {
209 updateEnabledState();
210 }
211 }
212
213 /**
214 * Adapter for selection change events
215 *
216 */
217 private class SelectionChangeAdapter implements SelectionChangedListener {
218 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
219 updateEnabledState(newSelection);
220 }
221 }
222}
Note: See TracBrowser for help on using the repository browser.