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

Last change on this file since 298 was 298, checked in by imi, 17 years ago
  • added license description to head of each source file
File size: 2.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4
5import javax.swing.AbstractAction;
6import javax.swing.JComponent;
7import javax.swing.KeyStroke;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.tools.Destroyable;
11import org.openstreetmap.josm.tools.ImageProvider;
12import org.openstreetmap.josm.tools.ShortCutLabel;
13
14/**
15 * Base class helper for all Actions in JOSM. Just to make the life easier.
16 *
17 * destroy() from interface Destroyable is called e.g. for MapModes, when the last layer has
18 * been removed and so the mapframe will be destroyed. For other JosmActions, destroy() may never
19 * be called (currently).
20 *
21 * @author imi
22 */
23abstract public class JosmAction extends AbstractAction implements Destroyable {
24
25 private KeyStroke shortCut;
26
27 public JosmAction(String name, String iconName, String tooltip, int shortCut, int modifier, boolean register) {
28 super(name, ImageProvider.get(iconName));
29 setHelpId();
30 String scl = ShortCutLabel.name(shortCut, modifier);
31 putValue(SHORT_DESCRIPTION, "<html>"+tooltip+" <font size='-2'>"+scl+"</font>"+(scl.equals("")?"":"&nbsp;")+"</html>");
32 if (shortCut != 0) {
33 this.shortCut = KeyStroke.getKeyStroke(shortCut, modifier);
34 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(this.shortCut, name);
35 Main.contentPane.getActionMap().put(name, this);
36 }
37 putValue("toolbar", iconName);
38 if (register)
39 Main.toolbar.register(this);
40 }
41
42 public void destroy() {
43 if (shortCut != null) {
44 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortCut);
45 Main.contentPane.getActionMap().remove(shortCut);
46 }
47 }
48
49 public JosmAction() {
50 setHelpId();
51 }
52
53
54 private void setHelpId() {
55 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
56 if (helpId.endsWith("Action"))
57 helpId = helpId.substring(0, helpId.length()-6);
58 putValue("help", helpId);
59 }
60}
Note: See TracBrowser for help on using the repository browser.