001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.actions;
003    
004    import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005    import static org.openstreetmap.josm.tools.I18n.tr;
006    
007    import java.awt.event.ActionEvent;
008    import java.util.Collection;
009    
010    import org.openstreetmap.josm.Main;
011    import org.openstreetmap.josm.command.MoveCommand;
012    import org.openstreetmap.josm.data.coor.LatLon;
013    import org.openstreetmap.josm.data.osm.Node;
014    import org.openstreetmap.josm.data.osm.OsmPrimitive;
015    import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
016    
017    /**
018     * This action displays a dialog with the coordinates of a node where the user can change them,
019     * and when ok is pressed, the node is relocated to the specified position.
020     */
021    public final class MoveNodeAction extends JosmAction {
022    
023        public MoveNodeAction() {
024            super(tr("Move Node..."), "movenode", tr("Edit latitude and longitude of a node."),
025                    null, /* no shortcut */
026                    true);
027            putValue("help", ht("/Action/MoveNode"));
028        }
029    
030        public void actionPerformed(ActionEvent e) {
031            if (!isEnabled() || (getCurrentDataSet().getSelectedNodes().size() != 1))
032                return;
033    
034            LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Move Node..."), ht("/Action/MoveNode"));
035            Node n = (Node) getCurrentDataSet().getSelectedNodes().toArray()[0];
036            dialog.setCoordinates(n.getCoor());
037            dialog.showDialog();
038            if (dialog.getValue() != 1)
039                return;
040    
041            LatLon coordinates = dialog.getCoordinates();
042            if (coordinates == null)
043                return;
044    
045            // move the node
046            Main.main.undoRedo.add(new MoveCommand(n, coordinates));
047            Main.map.mapView.repaint();
048        }
049    
050        @Override
051        protected void updateEnabledState() {
052            if (getCurrentDataSet() == null) {
053                setEnabled(false);
054            } else {
055                updateEnabledState(getCurrentDataSet().getSelected());
056            }
057        }
058    
059        @Override
060        protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
061            if (selection == null || selection.isEmpty()) {
062                setEnabled(false);
063                return;
064            }
065            if ((selection.size()) == 1 && (selection.toArray()[0] instanceof Node) ) {
066                setEnabled(true);
067            } else {
068                setEnabled(false);
069            }
070        }
071    }