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

Last change on this file since 3227 was 3227, checked in by jttt, 14 years ago

Fix #2234 Translation can cause JosmActions to illegally handle shortcuts

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