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

Last change on this file since 2277 was 2260, checked in by Gubaer, 15 years ago

added documentation, fixed initialization of enabled state of JosmAction

  • Property svn:eol-style set to native
File size: 7.6 KB
RevLine 
[298]1// License: GPL. Copyright 2007 by Immanuel Scholz and others
[30]2package org.openstreetmap.josm.actions;
3
[1182]4import static org.openstreetmap.josm.tools.I18n.tr;
5
[1820]6import java.util.Collection;
7
[30]8import javax.swing.AbstractAction;
[68]9import javax.swing.JComponent;
[30]10
[43]11import org.openstreetmap.josm.Main;
[1820]12import org.openstreetmap.josm.data.SelectionChangedListener;
[558]13import org.openstreetmap.josm.data.osm.DataSet;
[1820]14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.gui.layer.Layer;
[1814]16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[1820]17import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
[208]18import org.openstreetmap.josm.tools.Destroyable;
[71]19import org.openstreetmap.josm.tools.ImageProvider;
[1084]20import org.openstreetmap.josm.tools.Shortcut;
[30]21
22/**
23 * Base class helper for all Actions in JOSM. Just to make the life easier.
[1820]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 *
[208]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).
[1023]34 *
[30]35 * @author imi
36 */
[208]37abstract public class JosmAction extends AbstractAction implements Destroyable {
[30]38
[1169]39 protected Shortcut sc;
[1820]40 private LayerChangeAdapter layerChangeAdapter;
41 private SelectionChangeAdapter selectionChangeAdapter;
[208]42
[1169]43 public Shortcut getShortcut() {
44 if (sc == null) {
[1182]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"
[1169]49 }
50 return sc;
51 }
[1023]52
[1169]53 /**
54 * The new super for all actions.
55 *
56 * Use this super constructor to setup your action. It takes 5 parameters:
57 *
[1935]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,
[1169]65 * the user CANNOT configure a shortcut for your action.
[1935]66 * @param register register this action for the toolbar preferences?
[1169]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.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
74 Main.contentPane.getActionMap().put(name, this);
75 }
76 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
77 putValue("toolbar", iconName);
[1814]78 if (register) {
[1335]79 Main.toolbar.register(this);
[1814]80 }
[1820]81 installAdapters();
[1169]82 }
[1023]83
[1820]84 public JosmAction() {
85 setHelpId();
86 installAdapters();
87 }
88
[1169]89 public void destroy() {
[1180]90 if (sc != null) {
[1169]91 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(sc.getKeyStroke());
92 Main.contentPane.getActionMap().remove(sc.getKeyStroke());
93 }
[1820]94 if (Layer.listeners != null) {
95 Layer.listeners.remove(layerChangeAdapter);
96 }
97 if (DataSet.selListeners != null) {
98 DataSet.selListeners.remove(selectionChangeAdapter);
99 }
[1169]100 }
[1023]101
[155]102
[1820]103
[1169]104 /**
105 * needs to be overridden to be useful
106 */
107 public void pasteBufferChanged(DataSet newPasteBuffer) {
108 return;
109 }
[1023]110
[1169]111 /**
112 * needs to be overridden to be useful
113 */
114 public void addListener(JosmAction a) {
115 return;
116 }
[1023]117
[1169]118 private void setHelpId() {
119 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
[1814]120 if (helpId.endsWith("Action")) {
[1169]121 helpId = helpId.substring(0, helpId.length()-6);
[1814]122 }
[1169]123 putValue("help", helpId);
124 }
[1814]125
126 /**
127 * Replies the current edit layer
128 *
129 * @return the current edit layer. null, if no edit layer exists
130 */
131 protected OsmDataLayer getEditLayer() {
132 return Main.main.getEditLayer();
133 }
134
135 /**
136 * Replies the current dataset
137 *
138 * @return the current dataset. null, if no current dataset exists
139 */
140 protected DataSet getCurrentDataSet() {
141 return Main.main.getCurrentDataSet();
142 }
[1820]143
144 private void installAdapters() {
145 // make this action listen to layer change and selection change events
146 //
147 layerChangeAdapter = new LayerChangeAdapter();
148 selectionChangeAdapter = new SelectionChangeAdapter();
149 Layer.listeners.add(layerChangeAdapter);
150 DataSet.selListeners.add(selectionChangeAdapter);
[2256]151 initEnabledState();
[1820]152 }
153
[2260]154 /**
155 * Override in subclasses to init the enabled state of an action when it is
156 * created. Default behaviour is to call {@see #updateEnabledState()}
157 *
158 * @see #updateEnabledState()
159 * @see #updateEnabledState(Collection)
160 */
[2256]161 protected void initEnabledState() {
[2260]162 updateEnabledState();
[2256]163 }
164
[2260]165 /**
166 * Override in subclasses to update the enabled state of the action when
167 * something in the JOSM state changes, i.e. when a layer is removed or added.
168 *
169 * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
170 * of selected primitives.
171 *
172 * Default behavior is empty.
173 *
174 * @see #updateEnabledState(Collection)
175 * @see #initEnabledState()
176 */
[1820]177 protected void updateEnabledState() {
178 }
179
[2260]180 /**
181 * Override in subclasses to update the enabled state of the action if the
182 * collection of selected primitives changes. This method is called with the
183 * new selection. Avoid calling getCurrentDataSet().getSelected() because this
184 * loops over the complete data set.
185 *
186 * @param selection the collection of selected primitives
187 *
188 * @see #updateEnabledState()
189 * @see #initEnabledState()
190 */
[2256]191 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
192 }
193
[1820]194 /**
195 * Adapter for layer change events
196 *
197 */
198 private class LayerChangeAdapter implements LayerChangeListener {
199 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
200 updateEnabledState();
201 }
202
203 public void layerAdded(Layer newLayer) {
204 updateEnabledState();
205 }
206
207 public void layerRemoved(Layer oldLayer) {
208 updateEnabledState();
209 }
210 }
211
212 /**
213 * Adapter for selection change events
214 *
215 */
216 private class SelectionChangeAdapter implements SelectionChangedListener {
217 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
[2256]218 updateEnabledState(newSelection);
[1820]219 }
220 }
[30]221}
Note: See TracBrowser for help on using the repository browser.