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

Last change on this file since 5459 was 5459, checked in by Don-vip, 12 years ago

fix #2961 - Improve usability of WMS Layer Saving/Loading

  • Replaced the unconventional method of creating a blank layer, then loading a .wms file to a standard File->Open approach
  • Fixed memory leaks with some actions registered as listeners but never destroyed
  • Layer interface modified to allow a generic approach of layer saving in SaveActionBase rather than the previous one restricted to OSM and GPX data
  • FileImporters and FileExporters can now be enabled/disabled at runtime, for example when the active layer changes
  • Property svn:eol-style set to native
File size: 9.4 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.awt.event.KeyEvent;
7import java.util.Collection;
8
9import javax.swing.AbstractAction;
10import javax.swing.Icon;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.SelectionChangedListener;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.gui.MapView;
17import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.tools.Destroyable;
21import org.openstreetmap.josm.tools.ImageProvider;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * Base class helper for all Actions in JOSM. Just to make the life easier.
26 *
27 * A JosmAction is a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon
28 * a layer change event or a selection change event it invokes {@link #updateEnabledState()}.
29 * Subclasses can override {@link #updateEnabledState()} in order to update the {@link #isEnabled()}-state
30 * of a JosmAction depending on the {@link #getCurrentDataSet()} and the current layers
31 * (see also {@link #getEditLayer()}).
32 *
33 * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
34 * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
35 * be called (currently).
36 *
37 * @author imi
38 */
39abstract public class JosmAction extends AbstractAction implements Destroyable {
40
41 protected Shortcut sc;
42 private LayerChangeAdapter layerChangeAdapter;
43 private SelectionChangeAdapter selectionChangeAdapter;
44
45 public Shortcut getShortcut() {
46 if (sc == null) {
47 sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
48 // as this shortcut is shared by all action that don't want to have a shortcut,
49 // we shouldn't allow the user to change it...
50 // this is handled by special name "core:none"
51 }
52 return sc;
53 }
54
55 /**
56 * Constructs a {@code JosmAction}.
57 *
58 * @param name the action's text as displayed on the menu (if it is added to a menu)
59 * @param icon 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 registerInToolbar register this action for the toolbar preferences?
67 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
68 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
69 */
70 public JosmAction(String name, Icon icon, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
71 super(name, icon);
72 setHelpId();
73 sc = shortcut;
74 if (sc != null) {
75 Main.registerActionShortcut(this, sc);
76 }
77 setTooltip(tooltip);
78 if (getValue("toolbar") == null) {
79 putValue("toolbar", toolbarId);
80 }
81 if (registerInToolbar) {
82 Main.toolbar.register(this);
83 }
84 if (installAdapters) {
85 installAdapters();
86 }
87 }
88
89 /**
90 * The new super for all actions.
91 *
92 * Use this super constructor to setup your action.
93 *
94 * @param name the action's text as displayed on the menu (if it is added to a menu)
95 * @param iconName the filename of the icon to use
96 * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
97 * that html is not supported for menu actions on some platforms.
98 * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
99 * do want a shortcut, remember you can always register it with group=none, so you
100 * won't be assigned a shortcut unless the user configures one. If you pass null here,
101 * the user CANNOT configure a shortcut for your action.
102 * @param register register this action for the toolbar preferences?
103 * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
104 * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
105 */
106 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, String toolbarId, boolean installAdapters) {
107 this(name, iconName == null ? null : ImageProvider.get(iconName), tooltip, shortcut, register,
108 toolbarId == null ? iconName : toolbarId, installAdapters);
109 }
110
111 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register, boolean installAdapters) {
112 this(name, iconName, tooltip, shortcut, register, null, installAdapters);
113 }
114
115 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
116 this(name, iconName, tooltip, shortcut, register, null, true);
117 }
118
119 public JosmAction() {
120 this(true);
121 }
122
123 public JosmAction(boolean installAdapters) {
124 setHelpId();
125 if (installAdapters) {
126 installAdapters();
127 }
128 }
129
130 @Override
131 public void destroy() {
132 if (sc != null) {
133 Main.unregisterActionShortcut(this);
134 }
135 MapView.removeLayerChangeListener(layerChangeAdapter);
136 DataSet.removeSelectionListener(selectionChangeAdapter);
137 }
138
139 private void setHelpId() {
140 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
141 if (helpId.endsWith("Action")) {
142 helpId = helpId.substring(0, helpId.length()-6);
143 }
144 putValue("help", helpId);
145 }
146
147 public void setTooltip(String tooltip) {
148 if (tooltip != null) {
149 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
150 }
151 }
152
153 /**
154 * Replies the current edit layer
155 *
156 * @return the current edit layer. null, if no edit layer exists
157 */
158 protected static OsmDataLayer getEditLayer() {
159 return Main.main.getEditLayer();
160 }
161
162 /**
163 * Replies the current dataset
164 *
165 * @return the current dataset. null, if no current dataset exists
166 */
167 protected static DataSet getCurrentDataSet() {
168 return Main.main.getCurrentDataSet();
169 }
170
171 protected void installAdapters() {
172 // make this action listen to layer change and selection change events
173 //
174 layerChangeAdapter = new LayerChangeAdapter();
175 selectionChangeAdapter = new SelectionChangeAdapter();
176 MapView.addLayerChangeListener(layerChangeAdapter);
177 DataSet.addSelectionListener(selectionChangeAdapter);
178 initEnabledState();
179 }
180
181 /**
182 * Override in subclasses to init the enabled state of an action when it is
183 * created. Default behaviour is to call {@link #updateEnabledState()}
184 *
185 * @see #updateEnabledState()
186 * @see #updateEnabledState(Collection)
187 */
188 protected void initEnabledState() {
189 updateEnabledState();
190 }
191
192 /**
193 * Override in subclasses to update the enabled state of the action when
194 * something in the JOSM state changes, i.e. when a layer is removed or added.
195 *
196 * See {@link #updateEnabledState(Collection)} to respond to changes in the collection
197 * of selected primitives.
198 *
199 * Default behavior is empty.
200 *
201 * @see #updateEnabledState(Collection)
202 * @see #initEnabledState()
203 */
204 protected void updateEnabledState() {
205 }
206
207 /**
208 * Override in subclasses to update the enabled state of the action if the
209 * collection of selected primitives changes. This method is called with the
210 * new selection.
211 *
212 * @param selection the collection of selected primitives; may be empty, but not null
213 *
214 * @see #updateEnabledState()
215 * @see #initEnabledState()
216 */
217 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
218 }
219
220 /**
221 * Adapter for layer change events
222 *
223 */
224 private class LayerChangeAdapter implements MapView.LayerChangeListener {
225 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
226 updateEnabledState();
227 }
228
229 public void layerAdded(Layer newLayer) {
230 updateEnabledState();
231 }
232
233 public void layerRemoved(Layer oldLayer) {
234 updateEnabledState();
235 }
236 }
237
238 /**
239 * Adapter for selection change events
240 *
241 */
242 private class SelectionChangeAdapter implements SelectionChangedListener {
243 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
244 updateEnabledState(newSelection);
245 }
246 }
247}
Note: See TracBrowser for help on using the repository browser.