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

Last change on this file since 3262 was 3252, checked in by jttt, 16 years ago

Fix #2234: Translation can cause JosmActions to illegally handle shortcuts

  • Property svn:eol-style set to native
File size: 7.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.util.Collection;
7
8import javax.swing.AbstractAction;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.SelectionChangedListener;
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
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"), 0, Shortcut.GROUP_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. It takes 5 parameters:
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 */
68 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
69 super(name, iconName == null ? null : ImageProvider.get(iconName));
70 setHelpId();
71 sc = shortcut;
72 if (sc != null) {
73 Main.registerActionShortcut(this, sc);
74 }
75 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
76 putValue("toolbar", iconName);
77 if (register) {
78 Main.toolbar.register(this);
79 }
80 installAdapters();
81 }
82
83 public JosmAction() {
84 setHelpId();
85 installAdapters();
86 }
87
88 public void destroy() {
89 if (sc != null) {
90 Main.unregisterActionShortcut(sc);
91 }
92 MapView.removeLayerChangeListener(layerChangeAdapter);
93 DataSet.selListeners.remove(selectionChangeAdapter);
94 }
95
96 /**
97 * needs to be overridden to be useful
98 */
99 public void pasteBufferChanged(PrimitiveDeepCopy newPasteBuffer) {
100 return;
101 }
102
103 /**
104 * needs to be overridden to be useful
105 */
106 public void addListener(JosmAction a) {
107 return;
108 }
109
110 private void setHelpId() {
111 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
112 if (helpId.endsWith("Action")) {
113 helpId = helpId.substring(0, helpId.length()-6);
114 }
115 putValue("help", helpId);
116 }
117
118 /**
119 * Replies the current edit layer
120 *
121 * @return the current edit layer. null, if no edit layer exists
122 */
123 protected OsmDataLayer getEditLayer() {
124 return Main.main.getEditLayer();
125 }
126
127 /**
128 * Replies the current dataset
129 *
130 * @return the current dataset. null, if no current dataset exists
131 */
132 protected DataSet getCurrentDataSet() {
133 return Main.main.getCurrentDataSet();
134 }
135
136 protected void installAdapters() {
137 // make this action listen to layer change and selection change events
138 //
139 layerChangeAdapter = new LayerChangeAdapter();
140 selectionChangeAdapter = new SelectionChangeAdapter();
141 MapView.addLayerChangeListener(layerChangeAdapter);
142 DataSet.selListeners.add(selectionChangeAdapter);
143 initEnabledState();
144 }
145
146 /**
147 * Override in subclasses to init the enabled state of an action when it is
148 * created. Default behaviour is to call {@see #updateEnabledState()}
149 *
150 * @see #updateEnabledState()
151 * @see #updateEnabledState(Collection)
152 */
153 protected void initEnabledState() {
154 updateEnabledState();
155 }
156
157 /**
158 * Override in subclasses to update the enabled state of the action when
159 * something in the JOSM state changes, i.e. when a layer is removed or added.
160 *
161 * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
162 * of selected primitives.
163 *
164 * Default behavior is empty.
165 *
166 * @see #updateEnabledState(Collection)
167 * @see #initEnabledState()
168 */
169 protected void updateEnabledState() {
170 }
171
172 /**
173 * Override in subclasses to update the enabled state of the action if the
174 * collection of selected primitives changes. This method is called with the
175 * new selection. Avoid calling getCurrentDataSet().getSelected() because this
176 * loops over the complete data set.
177 *
178 * @param selection the collection of selected primitives
179 *
180 * @see #updateEnabledState()
181 * @see #initEnabledState()
182 */
183 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
184 }
185
186 /**
187 * Adapter for layer change events
188 *
189 */
190 private class LayerChangeAdapter implements MapView.LayerChangeListener {
191 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
192 updateEnabledState();
193 }
194
195 public void layerAdded(Layer newLayer) {
196 updateEnabledState();
197 }
198
199 public void layerRemoved(Layer oldLayer) {
200 updateEnabledState();
201 }
202 }
203
204 /**
205 * Adapter for selection change events
206 *
207 */
208 private class SelectionChangeAdapter implements SelectionChangedListener {
209 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
210 updateEnabledState(newSelection);
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.