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

Last change on this file since 1335 was 1335, checked in by stoecker, 16 years ago

fix #1988. Patch by Igor Shubovych

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