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