| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.event.InputEvent;
|
|---|
| 5 |
|
|---|
| 6 | import javax.swing.AbstractAction;
|
|---|
| 7 | import javax.swing.JComponent;
|
|---|
| 8 | import javax.swing.KeyStroke;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.Main;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 12 | import org.openstreetmap.josm.tools.Destroyable;
|
|---|
| 13 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 14 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Base class helper for all Actions in JOSM. Just to make the life easier.
|
|---|
| 18 | *
|
|---|
| 19 | * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
|
|---|
| 20 | * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
|
|---|
| 21 | * be called (currently).
|
|---|
| 22 | *
|
|---|
| 23 | * @author imi
|
|---|
| 24 | */
|
|---|
| 25 | abstract public class JosmAction extends AbstractAction implements Destroyable {
|
|---|
| 26 |
|
|---|
| 27 | protected Shortcut sc;
|
|---|
| 28 |
|
|---|
| 29 | public Shortcut getShortcut() {
|
|---|
| 30 | if (sc == null) {
|
|---|
| 31 | sc = Shortcut.registerShortcut("core:none", "No Shortcut", 0, Shortcut.GROUP_NONE);
|
|---|
| 32 | sc.setAutomatic(); // as this shortcut is shared by all action that don't want to have a shortcut,
|
|---|
| 33 | // we shouldn't allow the user to change it...
|
|---|
| 34 | }
|
|---|
| 35 | return sc;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * The new super for all actions.
|
|---|
| 40 | *
|
|---|
| 41 | * Use this super constructor to setup your action. It takes 5 parameters:
|
|---|
| 42 | *
|
|---|
| 43 | * name - the action's text as displayed on the menu (if it is added to a menu)
|
|---|
| 44 | * iconName - the filename of the icon to use
|
|---|
| 45 | * tooltip - a longer description of the action that will be displayed in the tooltip. Please note
|
|---|
| 46 | * that html is not supported for menu action on some platforms
|
|---|
| 47 | * shortcut - a ready-created shortcut object or null if you don't want a shortcut. But you always
|
|---|
| 48 | * do want a shortcut, remember you can alway register it with group=none, so you
|
|---|
| 49 | * won't be assigned a shurtcut unless the user configures one. If you pass null here,
|
|---|
| 50 | * the user CANNOT configure a shortcut for your action.
|
|---|
| 51 | * register - register this action for the toolbar preferences?
|
|---|
| 52 | */
|
|---|
| 53 | public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
|
|---|
| 54 | super(name, iconName == null ? null : ImageProvider.get(iconName));
|
|---|
| 55 | setHelpId();
|
|---|
| 56 | sc = shortcut;
|
|---|
| 57 | if (sc != null) {
|
|---|
| 58 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
|
|---|
| 59 | Main.contentPane.getActionMap().put(name, this);
|
|---|
| 60 | }
|
|---|
| 61 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
|---|
| 62 | putValue("toolbar", iconName);
|
|---|
| 63 | if (register)
|
|---|
| 64 | Main.toolbar.register(this);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | public void destroy() {
|
|---|
| 68 | if (sc != null) {
|
|---|
| 69 | Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(sc.getKeyStroke());
|
|---|
| 70 | Main.contentPane.getActionMap().remove(sc.getKeyStroke());
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | public JosmAction() {
|
|---|
| 75 | setHelpId();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * needs to be overridden to be useful
|
|---|
| 80 | */
|
|---|
| 81 | public void pasteBufferChanged(DataSet newPasteBuffer) {
|
|---|
| 82 | return;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * needs to be overridden to be useful
|
|---|
| 87 | */
|
|---|
| 88 | public void addListener(JosmAction a) {
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | private void setHelpId() {
|
|---|
| 93 | String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
|
|---|
| 94 | if (helpId.endsWith("Action"))
|
|---|
| 95 | helpId = helpId.substring(0, helpId.length()-6);
|
|---|
| 96 | putValue("help", helpId);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|