1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.housenumbertool;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.io.File;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * An action for opening the {@link TagDialog} editor
|
---|
19 | */
|
---|
20 | public class LaunchAction extends JosmAction implements DataSelectionListener {
|
---|
21 |
|
---|
22 | private static final long serialVersionUID = -2017126466206457986L;
|
---|
23 | private OsmPrimitive selection;
|
---|
24 |
|
---|
25 | private final File pluginDir;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code LaunchAction}.
|
---|
29 | * @param pluginDir plugin directory
|
---|
30 | */
|
---|
31 | public LaunchAction(File pluginDir) {
|
---|
32 | super(tr("HouseNumberTaggingTool"),
|
---|
33 | "home-icon32",
|
---|
34 | tr("Launches the HouseNumberTaggingTool dialog"),
|
---|
35 | Shortcut.registerShortcut("edit:housenumbertaggingtool", tr("Data: {0}", "HouseNumberTaggingTool"),
|
---|
36 | KeyEvent.VK_K, Shortcut.DIRECT),
|
---|
37 | true);
|
---|
38 |
|
---|
39 | this.pluginDir = pluginDir;
|
---|
40 | SelectionEventManager.getInstance().addSelectionListener(this);
|
---|
41 | setEnabled(false);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * launch the editor
|
---|
46 | */
|
---|
47 | protected void launchEditor() {
|
---|
48 | if (!isEnabled()) {
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | TagDialog dialog = new TagDialog(pluginDir, selection);
|
---|
53 | dialog.showDialog();
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void actionPerformed(ActionEvent e) {
|
---|
58 | launchEditor();
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
63 | Set<OsmPrimitive> newSelection = event.getSelection();
|
---|
64 | if (newSelection != null && newSelection.size() == 1) {
|
---|
65 | setEnabled(true);
|
---|
66 | selection = newSelection.iterator().next();
|
---|
67 | } else {
|
---|
68 | setEnabled(false);
|
---|
69 | selection = null;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|