diff --git a/src/org/openstreetmap/josm/actions/JosmAction.java b/src/org/openstreetmap/josm/actions/JosmAction.java
index 1441c88..d2ef11d 100644
|
a
|
b
|
import org.openstreetmap.josm.Main;
|
| 15 | 15 | import org.openstreetmap.josm.data.SelectionChangedListener; |
| 16 | 16 | import org.openstreetmap.josm.data.osm.DataSet; |
| 17 | 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 18 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent; |
| 18 | 19 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener; |
| | 20 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent; |
| | 21 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent; |
| | 22 | import org.openstreetmap.josm.gui.layer.MainLayerManager; |
| 19 | 23 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent; |
| 20 | 24 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener; |
| 21 | 25 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| … |
… |
import org.openstreetmap.josm.tools.Shortcut;
|
| 27 | 31 | /** |
| 28 | 32 | * Base class helper for all Actions in JOSM. Just to make the life easier. |
| 29 | 33 | * |
| 30 | | * A JosmAction is a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon |
| | 34 | * This action allows you to set up an icon, a tooltip text, a globally registered shortcut, register it in the main toolbar and set up |
| | 35 | * layer/selection listeners that call {@link #updateEnabledState()} whenever the global context is changed. |
| | 36 | * |
| | 37 | * A JosmAction can register a {@link LayerChangeListener} and a {@link SelectionChangedListener}. Upon |
| 31 | 38 | * a layer change event or a selection change event it invokes {@link #updateEnabledState()}. |
| 32 | 39 | * Subclasses can override {@link #updateEnabledState()} in order to update the {@link #isEnabled()}-state |
| 33 | 40 | * of a JosmAction depending on the {@link #getCurrentDataSet()} and the current layers |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 43 | 50 | |
| 44 | 51 | protected transient Shortcut sc; |
| 45 | 52 | private transient LayerChangeAdapter layerChangeAdapter; |
| | 53 | private transient ActiveLayerChangeAdapter activeLayerChangeAdapter; |
| 46 | 54 | private transient SelectionChangeAdapter selectionChangeAdapter; |
| 47 | 55 | |
| 48 | 56 | /** |
| 49 | | * Returns the shortcut for this action. |
| 50 | | * @return the shortcut for this action, or "No shortcut" if none is defined |
| 51 | | */ |
| 52 | | public Shortcut getShortcut() { |
| 53 | | if (sc == null) { |
| 54 | | sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE); |
| 55 | | // as this shortcut is shared by all action that don't want to have a shortcut, |
| 56 | | // we shouldn't allow the user to change it... |
| 57 | | // this is handled by special name "core:none" |
| 58 | | } |
| 59 | | return sc; |
| 60 | | } |
| 61 | | |
| 62 | | /** |
| 63 | 57 | * Constructs a {@code JosmAction}. |
| 64 | 58 | * |
| 65 | 59 | * @param name the action's text as displayed on the menu (if it is added to a menu) |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 177 | 171 | } |
| 178 | 172 | } |
| 179 | 173 | |
| | 174 | |
| | 175 | /** |
| | 176 | * Installs the listeners to this action. |
| | 177 | * <p> |
| | 178 | * This should either never be called or only called in the constructor of this action. |
| | 179 | * <p> |
| | 180 | * All registered adapters should be removed in {@link #destroy()} |
| | 181 | */ |
| | 182 | protected void installAdapters() { |
| | 183 | // make this action listen to layer change and selection change events |
| | 184 | if (listenToLayerChange()) { |
| | 185 | layerChangeAdapter = new LayerChangeAdapter(); |
| | 186 | activeLayerChangeAdapter = new ActiveLayerChangeAdapter(); |
| | 187 | getLayerManager().addLayerChangeListener(layerChangeAdapter); |
| | 188 | getLayerManager().addActiveLayerChangeListener(activeLayerChangeAdapter); |
| | 189 | } |
| | 190 | if (listenToSelectionChange()) { |
| | 191 | selectionChangeAdapter = new SelectionChangeAdapter(); |
| | 192 | DataSet.addSelectionListener(selectionChangeAdapter); |
| | 193 | } |
| | 194 | initEnabledState(); |
| | 195 | } |
| | 196 | |
| | 197 | /** |
| | 198 | * Overwrite this if {@link #updateEnabledState()} should be called when the active / availabe layers change. Default is true. |
| | 199 | * @return <code>true</code> if a {@link LayerChangeListener} and a {@link ActiveLayerChangeListener} should be registered. |
| | 200 | * @since xxx |
| | 201 | */ |
| | 202 | protected boolean listenToLayerChange() { |
| | 203 | return true; |
| | 204 | } |
| | 205 | |
| | 206 | /** |
| | 207 | * Overwrite this if {@link #updateEnabledState()} should be called when the selection changed. Default is true. |
| | 208 | * @return <code>true</code> if a {@link SelectionChangedListener} should be registered. |
| | 209 | * @since xxx |
| | 210 | */ |
| | 211 | protected boolean listenToSelectionChange() { |
| | 212 | return true; |
| | 213 | } |
| | 214 | |
| 180 | 215 | @Override |
| 181 | 216 | public void destroy() { |
| 182 | 217 | if (sc != null) { |
| 183 | 218 | Main.unregisterActionShortcut(this); |
| 184 | 219 | } |
| 185 | 220 | if (layerChangeAdapter != null) { |
| 186 | | Main.getLayerManager().removeActiveLayerChangeListener(layerChangeAdapter); |
| | 221 | getLayerManager().removeLayerChangeListener(layerChangeAdapter); |
| | 222 | getLayerManager().removeActiveLayerChangeListener(activeLayerChangeAdapter); |
| | 223 | } |
| | 224 | if (selectionChangeAdapter != null) { |
| | 225 | DataSet.removeSelectionListener(selectionChangeAdapter); |
| 187 | 226 | } |
| 188 | | DataSet.removeSelectionListener(selectionChangeAdapter); |
| 189 | 227 | } |
| 190 | 228 | |
| 191 | 229 | private void setHelpId() { |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 197 | 235 | } |
| 198 | 236 | |
| 199 | 237 | /** |
| | 238 | * Returns the shortcut for this action. |
| | 239 | * @return the shortcut for this action, or "No shortcut" if none is defined |
| | 240 | */ |
| | 241 | public Shortcut getShortcut() { |
| | 242 | if (sc == null) { |
| | 243 | sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE); |
| | 244 | // as this shortcut is shared by all action that don't want to have a shortcut, |
| | 245 | // we shouldn't allow the user to change it... |
| | 246 | // this is handled by special name "core:none" |
| | 247 | } |
| | 248 | return sc; |
| | 249 | } |
| | 250 | |
| | 251 | /** |
| 200 | 252 | * Sets the tooltip text of this action. |
| 201 | 253 | * @param tooltip The text to display in tooltip. Can be {@code null} |
| 202 | 254 | */ |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 207 | 259 | } |
| 208 | 260 | |
| 209 | 261 | /** |
| | 262 | * Gets the layer manager used for this action. Defaults to the main layer manager but you can overwrite this. |
| | 263 | * <p> |
| | 264 | * The layer manager must be available when {@link #installAdapters()} is called and must not change. |
| | 265 | * |
| | 266 | * @return The layer manager. |
| | 267 | * @since xxx |
| | 268 | */ |
| | 269 | public MainLayerManager getLayerManager() { |
| | 270 | return Main.getLayerManager(); |
| | 271 | } |
| | 272 | |
| | 273 | /** |
| 210 | 274 | * Replies the current edit layer |
| 211 | 275 | * |
| 212 | 276 | * @return the current edit layer. null, if no edit layer exists |
| | 277 | * @deprecated Use {@link #getLayerManager()}.getEditLayer() instead. To be removed in end of 2016. |
| 213 | 278 | */ |
| | 279 | @Deprecated |
| 214 | 280 | public static OsmDataLayer getEditLayer() { |
| 215 | | return Main.main != null ? Main.main.getEditLayer() : null; |
| | 281 | return Main.getLayerManager().getEditLayer(); |
| 216 | 282 | } |
| 217 | 283 | |
| 218 | 284 | /** |
| 219 | 285 | * Replies the current dataset. |
| 220 | 286 | * |
| 221 | 287 | * @return the current dataset. null, if no current dataset exists |
| | 288 | * @deprecated Use {@link #getLayerManager()}.getEditDataSet() instead. To be removed in end of 2016. |
| 222 | 289 | */ |
| | 290 | @Deprecated |
| 223 | 291 | public static DataSet getCurrentDataSet() { |
| 224 | | return Main.main != null ? Main.main.getCurrentDataSet() : null; |
| 225 | | } |
| 226 | | |
| 227 | | protected void installAdapters() { |
| 228 | | // make this action listen to layer change and selection change events |
| 229 | | layerChangeAdapter = new LayerChangeAdapter(); |
| 230 | | selectionChangeAdapter = new SelectionChangeAdapter(); |
| 231 | | Main.getLayerManager().addActiveLayerChangeListener(layerChangeAdapter); |
| 232 | | DataSet.addSelectionListener(selectionChangeAdapter); |
| 233 | | initEnabledState(); |
| | 292 | return Main.getLayerManager().getEditDataSet(); |
| 234 | 293 | } |
| 235 | 294 | |
| 236 | 295 | protected static void waitFuture(final Future<?> future, final PleaseWaitProgressMonitor monitor) { |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 272 | 331 | * |
| 273 | 332 | * @see #updateEnabledState(Collection) |
| 274 | 333 | * @see #initEnabledState() |
| | 334 | * @see #listenToLayerChange() |
| 275 | 335 | */ |
| 276 | 336 | protected void updateEnabledState() { |
| 277 | 337 | } |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 285 | 345 | * |
| 286 | 346 | * @see #updateEnabledState() |
| 287 | 347 | * @see #initEnabledState() |
| | 348 | * @see #listenToSelectionChange() |
| 288 | 349 | */ |
| 289 | 350 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
| 290 | 351 | } |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 292 | 353 | /** |
| 293 | 354 | * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed. |
| 294 | 355 | */ |
| 295 | | protected class LayerChangeAdapter implements ActiveLayerChangeListener { |
| | 356 | protected class LayerChangeAdapter implements LayerChangeListener { |
| 296 | 357 | @Override |
| 297 | | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) { |
| | 358 | public void layerAdded(LayerAddEvent e) { |
| | 359 | updateEnabledState(); |
| | 360 | } |
| | 361 | |
| | 362 | @Override |
| | 363 | public void layerRemoving(LayerRemoveEvent e) { |
| | 364 | updateEnabledState(); |
| | 365 | } |
| | 366 | |
| | 367 | @Override |
| | 368 | public void layerOrderChanged(LayerOrderChangeEvent e) { |
| 298 | 369 | updateEnabledState(); |
| 299 | 370 | } |
| 300 | 371 | |
| … |
… |
public abstract class JosmAction extends AbstractAction implements Destroyable {
|
| 305 | 376 | } |
| 306 | 377 | |
| 307 | 378 | /** |
| | 379 | * Adapter for layer change events. Runs updateEnabledState() whenever the active layer changed. |
| | 380 | */ |
| | 381 | protected class ActiveLayerChangeAdapter implements ActiveLayerChangeListener { |
| | 382 | @Override |
| | 383 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) { |
| | 384 | updateEnabledState(); |
| | 385 | } |
| | 386 | |
| | 387 | @Override |
| | 388 | public String toString() { |
| | 389 | return "ActiveLayerChangeAdapter [" + JosmAction.this.toString() + ']'; |
| | 390 | } |
| | 391 | } |
| | 392 | |
| | 393 | /** |
| 308 | 394 | * Adapter for selection change events. Runs updateEnabledState() whenever the selection changed. |
| 309 | 395 | */ |
| 310 | 396 | protected class SelectionChangeAdapter implements SelectionChangedListener { |