source: josm/trunk/src/org/openstreetmap/josm/actions/AddNodeAction.java@ 2277

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

JosmAction is now a LayerChangeListener and a SelectionChangeListener
updated all JosmActions
fixed #3018: Make sure tools menu entries (and actions) are deactivated when no layer

File size: 2.9 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 java.awt.GridBagLayout;
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9
10import javax.swing.JLabel;
11import javax.swing.JOptionPane;
12import javax.swing.JPanel;
13import javax.swing.JTextField;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.AddCommand;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.tools.GBC;
20import org.openstreetmap.josm.tools.Shortcut;
21
22/**
23 * This action displays a dialog where the user can enter a latitude and longitude,
24 * and when ok is pressed, a new node is created at the specified position.
25 */
26public final class AddNodeAction extends JosmAction {
27
28 public AddNodeAction() {
29 super(tr("Add Node..."), "addnode", tr("Add a node by entering latitude and longitude."),
30 Shortcut.registerShortcut("addnode", tr("Edit: {0}", tr("Add Node...")), KeyEvent.VK_D, Shortcut.GROUP_EDIT,
31 Shortcut.SHIFT_DEFAULT), true);
32 }
33
34 public void actionPerformed(ActionEvent e) {
35 // we abort if we are not in the context of an OsmDataLayer
36 //
37 if (!isEnabled())
38 return;
39
40 JPanel p = new JPanel(new GridBagLayout());
41 p.add(new JLabel("<html>"+
42 tr("Enter the coordinates for the new node.") +
43 "<br>" + tr("Use decimal degrees.") +
44 "<br>" + tr("Negative values denote Western/Southern hemisphere.")),
45 GBC.eol());
46
47 p.add(new JLabel(tr("Latitude")), GBC.std().insets(0,10,5,0));
48 final JTextField lat = new JTextField(12);
49 p.add(lat, GBC.eol().insets(0,10,0,0));
50 p.add(new JLabel(tr("Longitude")), GBC.std().insets(0,0,5,10));
51 final JTextField lon = new JTextField(12);
52 p.add(lon, GBC.eol().insets(0,0,0,10));
53
54 Node nnew = null;
55
56 while(nnew == null) {
57 JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
58 pane.createDialog(Main.parent, tr("Add Node...")).setVisible(true);
59 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
60 return;
61 try {
62 LatLon ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
63 if (!ll.isOutSideWorld()) {
64 nnew = new Node(ll);
65 }
66 } catch (Exception ex) { }
67 }
68
69 /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
70 Main.main.undoRedo.add(new AddCommand(nnew));
71 getCurrentDataSet().setSelected(nnew);
72 Main.map.mapView.repaint();
73 }
74
75 @Override
76 protected void updateEnabledState() {
77 setEnabled(getEditLayer() != null);
78 }
79
80}
81
Note: See TracBrowser for help on using the repository browser.