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