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

Last change on this file since 1084 was 1084, checked in by framm, 15 years ago
  • cosmetics: rename ShortCut to Shortcut, and shortCut to shortcut
  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import java.awt.event.InputEvent;
5
6import javax.swing.AbstractAction;
7import javax.swing.JComponent;
8import javax.swing.KeyStroke;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.osm.DataSet;
12import org.openstreetmap.josm.tools.Destroyable;
13import org.openstreetmap.josm.tools.ImageProvider;
14import 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 */
25abstract public class JosmAction extends AbstractAction implements Destroyable {
26
27 @Deprecated
28 public KeyStroke shortcut;
29 protected Shortcut sc;
30
31 public Shortcut getShortcut() {
32 if (sc == null) {
33 sc = Shortcut.registerShortcut("core:none", "No Shortcut", 0, Shortcut.GROUP_NONE);
34 sc.setAutomatic(); // 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 }
37 return sc;
38 }
39
40 @Deprecated
41 public JosmAction(String name, String iconName, String tooltip, int shortcut, int modifier, boolean register) {
42 super(name, iconName == null ? null : ImageProvider.get(iconName));
43 setHelpId();
44 if (shortcut != 0) {
45 int group = Shortcut.GROUP_LAYER; //GROUP_NONE;
46 if (((modifier & InputEvent.CTRL_MASK) != 0) || ((modifier & InputEvent.CTRL_DOWN_MASK) != 0)) {
47 group = Shortcut.GROUP_MENU;
48 } else if (modifier == 0) {
49 group = Shortcut.GROUP_EDIT;
50 }
51 sc = Shortcut.registerShortcut("auto:"+name, name, shortcut, group);
52 this.shortcut = sc.getKeyStroke();
53 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
54 Main.contentPane.getActionMap().put(name, this);
55 }
56 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
57 putValue("toolbar", iconName);
58 if (register)
59 Main.toolbar.register(this);
60 }
61
62 /**
63 * The new super for all actions.
64 *
65 * Use this super constructor to setup your action. It takes 5 parameters:
66 *
67 * name - the action's text as displayed on the menu (if it is added to a menu)
68 * iconName - the filename of the icon to use
69 * tooltip - a longer description of the action that will be displayed in the tooltip. Please note
70 * that html is not supported for menu action on some platforms
71 * shortcut - a ready-created shortcut object or null if you don't want a shortcut. But you always
72 * do want a shortcut, remember you can alway register it with group=none, so you
73 * won't be assigned a shurtcut unless the user configures one. If you pass null here,
74 * the user CANNOT configure a shortcut for your action.
75 * register - register this action for the toolbar preferences?
76 */
77 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
78 super(name, iconName == null ? null : ImageProvider.get(iconName));
79 setHelpId();
80 sc = shortcut;
81 if (sc != null) {
82 this.shortcut = sc.getKeyStroke();
83 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
84 Main.contentPane.getActionMap().put(name, this);
85 }
86 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
87 putValue("toolbar", iconName);
88 if (register)
89 Main.toolbar.register(this);
90 }
91
92 public void destroy() {
93 if (shortcut != null) {
94 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(sc.getKeyStroke());
95 Main.contentPane.getActionMap().remove(sc.getKeyStroke());
96 }
97 }
98
99 public JosmAction() {
100 setHelpId();
101 }
102
103 /**
104 * needs to be overridden to be useful
105 */
106 public void pasteBufferChanged(DataSet newPasteBuffer) {
107 return;
108 }
109
110 /**
111 * needs to be overridden to be useful
112 */
113 public void addListener(JosmAction a) {
114 return;
115 }
116
117 private void setHelpId() {
118 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
119 if (helpId.endsWith("Action"))
120 helpId = helpId.substring(0, helpId.length()-6);
121 putValue("help", helpId);
122 }
123}
Note: See TracBrowser for help on using the repository browser.