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