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

Last change on this file since 1814 was 1814, checked in by Gubaer, 15 years ago

removed dependencies to Main.ds, removed Main.ds
removed AddVisitor, NameVisitor, DeleteVisitor - unnecessary double dispatching for these simple cases

  • Property svn:eol-style set to native
File size: 4.1 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 javax.swing.AbstractAction;
7import javax.swing.JComponent;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.gui.layer.OsmDataLayer;
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 protected Shortcut sc;
28
29 public Shortcut getShortcut() {
30 if (sc == null) {
31 sc = Shortcut.registerShortcut("core:none", tr("No Shortcut"), 0, Shortcut.GROUP_NONE);
32 // as this shortcut is shared by all action that don't want to have a shortcut,
33 // we shouldn't allow the user to change it...
34 // this is handled by special name "core:none"
35 }
36 return sc;
37 }
38
39 /**
40 * The new super for all actions.
41 *
42 * Use this super constructor to setup your action. It takes 5 parameters:
43 *
44 * name - the action's text as displayed on the menu (if it is added to a menu)
45 * iconName - the filename of the icon to use
46 * tooltip - a longer description of the action that will be displayed in the tooltip. Please note
47 * that html is not supported for menu action on some platforms
48 * shortcut - a ready-created shortcut object or null if you don't want a shortcut. But you always
49 * do want a shortcut, remember you can alway register it with group=none, so you
50 * won't be assigned a shurtcut unless the user configures one. If you pass null here,
51 * the user CANNOT configure a shortcut for your action.
52 * register - register this action for the toolbar preferences?
53 */
54 public JosmAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean register) {
55 super(name, iconName == null ? null : ImageProvider.get(iconName));
56 setHelpId();
57 sc = shortcut;
58 if (sc != null) {
59 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), name);
60 Main.contentPane.getActionMap().put(name, this);
61 }
62 putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
63 putValue("toolbar", iconName);
64 if (register) {
65 Main.toolbar.register(this);
66 }
67 }
68
69 public void destroy() {
70 if (sc != null) {
71 Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(sc.getKeyStroke());
72 Main.contentPane.getActionMap().remove(sc.getKeyStroke());
73 }
74 }
75
76 public JosmAction() {
77 setHelpId();
78 }
79
80 /**
81 * needs to be overridden to be useful
82 */
83 public void pasteBufferChanged(DataSet newPasteBuffer) {
84 return;
85 }
86
87 /**
88 * needs to be overridden to be useful
89 */
90 public void addListener(JosmAction a) {
91 return;
92 }
93
94 private void setHelpId() {
95 String helpId = "Action/"+getClass().getName().substring(getClass().getName().lastIndexOf('.')+1);
96 if (helpId.endsWith("Action")) {
97 helpId = helpId.substring(0, helpId.length()-6);
98 }
99 putValue("help", helpId);
100 }
101
102 /**
103 * Replies the current edit layer
104 *
105 * @return the current edit layer. null, if no edit layer exists
106 */
107 protected OsmDataLayer getEditLayer() {
108 return Main.main.getEditLayer();
109 }
110
111 /**
112 * Replies the current dataset
113 *
114 * @return the current dataset. null, if no current dataset exists
115 */
116 protected DataSet getCurrentDataSet() {
117 return Main.main.getCurrentDataSet();
118 }
119}
Note: See TracBrowser for help on using the repository browser.