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

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

Use PrimitiveData for Copy, Paste and Paste tags actions

  • 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
105 /**
106 * needs to be overridden to be useful
107 */
108 public void pasteBufferChanged(PrimitiveDeepCopy newPasteBuffer) {
109 return;
110 }
111
112 /**
113 * needs to be overridden to be useful
114 */
115 public void addListener(JosmAction a) {
116 return;
117 }
118
119 private void setHelpId() {
120 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
121 if (helpId.endsWith("Action")) {
122 helpId = helpId.substring(0, helpId.length()-6);
123 }
124 putValue("help", helpId);
125 }
126
127 /**
128 * Replies the current edit layer
129 *
130 * @return the current edit layer. null, if no edit layer exists
131 */
132 protected OsmDataLayer getEditLayer() {
133 return Main.main.getEditLayer();
134 }
135
136 /**
137 * Replies the current dataset
138 *
139 * @return the current dataset. null, if no current dataset exists
140 */
141 protected DataSet getCurrentDataSet() {
142 return Main.main.getCurrentDataSet();
143 }
144
145 private void installAdapters() {
146 // make this action listen to layer change and selection change events
147 //
148 layerChangeAdapter = new LayerChangeAdapter();
149 selectionChangeAdapter = new SelectionChangeAdapter();
150 Layer.listeners.add(layerChangeAdapter);
151 DataSet.selListeners.add(selectionChangeAdapter);
152 initEnabledState();
153 }
154
155 /**
156 * Override in subclasses to init the enabled state of an action when it is
157 * created. Default behaviour is to call {@see #updateEnabledState()}
158 *
159 * @see #updateEnabledState()
160 * @see #updateEnabledState(Collection)
161 */
162 protected void initEnabledState() {
163 updateEnabledState();
164 }
165
166 /**
167 * Override in subclasses to update the enabled state of the action when
168 * something in the JOSM state changes, i.e. when a layer is removed or added.
169 *
170 * See {@see #updateEnabledState(Collection)} to respond to changes in the collection
171 * of selected primitives.
172 *
173 * Default behavior is empty.
174 *
175 * @see #updateEnabledState(Collection)
176 * @see #initEnabledState()
177 */
178 protected void updateEnabledState() {
179 }
180
181 /**
182 * Override in subclasses to update the enabled state of the action if the
183 * collection of selected primitives changes. This method is called with the
184 * new selection. Avoid calling getCurrentDataSet().getSelected() because this
185 * loops over the complete data set.
186 *
187 * @param selection the collection of selected primitives
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 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.