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

Last change on this file since 2602 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

  • Property svn:eol-style set to native
File size: 7.6 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.layer.Layer;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
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(), name);
75 Main.contentPane.getActionMap().put(name, 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 if (Layer.listeners != null) {
96 Layer.listeners.remove(layerChangeAdapter);
97 }
98 if (DataSet.selListeners != null) {
99 DataSet.selListeners.remove(selectionChangeAdapter);
100 }
101 }
102
103 /**
104 * needs to be overridden to be useful
105 */
106 public void pasteBufferChanged(PrimitiveDeepCopy newPasteBuffer) {
107 return;
108 }
109
110 /**
111 * needs to be overridden to be useful
112 */
113 public void addListener(JosmAction a) {
114 return;
115 }
116
117 private void setHelpId() {
118 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
119 if (helpId.endsWith("Action")) {
120 helpId = helpId.substring(0, helpId.length()-6);
121 }
122 putValue("help", helpId);
123 }
124
125 /**
126 * Replies the current edit layer
127 *
128 * @return the current edit layer. null, if no edit layer exists
129 */
130 protected OsmDataLayer getEditLayer() {
131 return Main.main.getEditLayer();
132 }
133
134 /**
135 * Replies the current dataset
136 *
137 * @return the current dataset. null, if no current dataset exists
138 */
139 protected DataSet getCurrentDataSet() {
140 return Main.main.getCurrentDataSet();
141 }
142
143 private void installAdapters() {
144 // make this action listen to layer change and selection change events
145 //
146 layerChangeAdapter = new LayerChangeAdapter();
147 selectionChangeAdapter = new SelectionChangeAdapter();
148 Layer.listeners.add(layerChangeAdapter);
149 DataSet.selListeners.add(selectionChangeAdapter);
150 initEnabledState();
151 }
152
153 /**
154 * Override in subclasses to init the enabled state of an action when it is
155 * created. Default behaviour is to call {@see #updateEnabledState()}
156 *
157 * @see #updateEnabledState()
158 * @see #updateEnabledState(Collection)
159 */
160 protected void initEnabledState() {
161 updateEnabledState();
162 }
163
164 /**
165 * Override in subclasses to update the enabled state of the action when
166 * something in the JOSM state changes, i.e. when a layer is removed or added.
167 *
168 * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
169 * of selected primitives.
170 *
171 * Default behavior is empty.
172 *
173 * @see #updateEnabledState(Collection)
174 * @see #initEnabledState()
175 */
176 protected void updateEnabledState() {
177 }
178
179 /**
180 * Override in subclasses to update the enabled state of the action if the
181 * collection of selected primitives changes. This method is called with the
182 * new selection. Avoid calling getCurrentDataSet().getSelected() because this
183 * loops over the complete data set.
184 *
185 * @param selection the collection of selected primitives
186 *
187 * @see #updateEnabledState()
188 * @see #initEnabledState()
189 */
190 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
191 }
192
193 /**
194 * Adapter for layer change events
195 *
196 */
197 private class LayerChangeAdapter implements LayerChangeListener {
198 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
199 updateEnabledState();
200 }
201
202 public void layerAdded(Layer newLayer) {
203 updateEnabledState();
204 }
205
206 public void layerRemoved(Layer oldLayer) {
207 updateEnabledState();
208 }
209 }
210
211 /**
212 * Adapter for selection change events
213 *
214 */
215 private class SelectionChangeAdapter implements SelectionChangedListener {
216 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
217 updateEnabledState(newSelection);
218 }
219 }
220}
Note: See TracBrowser for help on using the repository browser.