1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 |
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JTextField;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.command.AddCommand;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener;
|
---|
21 | import org.openstreetmap.josm.tools.GBC;
|
---|
22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This action displays a dialog where the user can enter a latitude and longitude,
|
---|
26 | * and when ok is pressed, a new node is created at the specified position.
|
---|
27 | */
|
---|
28 | public final class AddNodeAction extends JosmAction implements LayerChangeListener {
|
---|
29 |
|
---|
30 | public AddNodeAction() {
|
---|
31 | super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude and longitude."),
|
---|
32 | Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")), KeyEvent.VK_D, Shortcut.GROUP_EDIT,
|
---|
33 | Shortcut.SHIFT_DEFAULT), true);
|
---|
34 | Layer.listeners.add(this);
|
---|
35 | refreshEnabled();
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected void refreshEnabled() {
|
---|
39 | setEnabled(Main.map != null
|
---|
40 | && Main.map.mapView != null
|
---|
41 | && Main.map.mapView.getEditLayer() != null);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
45 | // we abort if we are not in the context of an OsmDataLayer
|
---|
46 | //
|
---|
47 | if (!isEnabled())
|
---|
48 | return;
|
---|
49 |
|
---|
50 | JPanel p = new JPanel(new GridBagLayout());
|
---|
51 | p.add(new JLabel("<html>"+
|
---|
52 | tr("Enter the coordinates for the new node.") +
|
---|
53 | "<br>" + tr("Use decimal degrees.") +
|
---|
54 | "<br>" + tr("Negative values denote Western/Southern hemisphere.")),
|
---|
55 | GBC.eol());
|
---|
56 |
|
---|
57 | p.add(new JLabel(tr("Latitude")), GBC.std().insets(0,10,5,0));
|
---|
58 | final JTextField lat = new JTextField(12);
|
---|
59 | p.add(lat, GBC.eol().insets(0,10,0,0));
|
---|
60 | p.add(new JLabel(tr("Longitude")), GBC.std().insets(0,0,5,10));
|
---|
61 | final JTextField lon = new JTextField(12);
|
---|
62 | p.add(lon, GBC.eol().insets(0,0,0,10));
|
---|
63 |
|
---|
64 | Node nnew = null;
|
---|
65 |
|
---|
66 | while(nnew == null) {
|
---|
67 | JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
|
---|
68 | pane.createDialog(Main.parent, tr("Add Node...")).setVisible(true);
|
---|
69 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
70 | return;
|
---|
71 | try {
|
---|
72 | LatLon ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
---|
73 | if (!ll.isOutSideWorld()) {
|
---|
74 | nnew = new Node(ll);
|
---|
75 | }
|
---|
76 | } catch (Exception ex) { }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
|
---|
80 | Main.main.undoRedo.add(new AddCommand(nnew));
|
---|
81 | Main.ds.setSelected(nnew);
|
---|
82 | Main.map.mapView.repaint();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
86 | refreshEnabled();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void layerAdded(Layer newLayer) {
|
---|
90 | refreshEnabled();
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | public void layerRemoved(Layer oldLayer) {
|
---|
95 | refreshEnabled();
|
---|
96 | }
|
---|
97 | }
|
---|