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.awt.event.KeyEvent;
009    
010    import org.openstreetmap.josm.Main;
011    import org.openstreetmap.josm.gui.layer.OsmDataLayer;
012    import org.openstreetmap.josm.tools.Shortcut;
013    
014    /**
015     * Undoes the last command.
016     *
017     * @author imi
018     */
019    public class UndoAction extends JosmAction implements OsmDataLayer.CommandQueueListener {
020    
021        /**
022         * Construct the action with "Undo" as label.
023         */
024        public UndoAction() {
025            super(tr("Undo"), "undo", tr("Undo the last action."),
026                    Shortcut.registerShortcut("system:undo", tr("Edit: {0}", tr("Undo")), KeyEvent.VK_Z, Shortcut.CTRL), true);
027            setEnabled(false);
028            putValue("help", ht("/Action/Undo"));
029        }
030    
031        public void actionPerformed(ActionEvent e) {
032            if (Main.map == null)
033                return;
034            Main.map.repaint();
035            Main.main.undoRedo.undo();
036        }
037    
038        @Override
039        protected void updateEnabledState() {
040            setEnabled(Main.main != null && !Main.main.undoRedo.commands.isEmpty());
041        }
042    
043        @Override
044        public void commandChanged(int queueSize, int redoSize) {
045            if (Main.main.undoRedo.commands.isEmpty()) {
046                putValue(NAME, tr("Undo"));
047                setTooltip(tr("Undo the last action."));
048            } else {
049                putValue(NAME, tr("Undo ..."));
050                setTooltip(tr("Undo {0}",
051                        Main.main.undoRedo.commands.getLast().getDescriptionText()));
052            }
053        }
054    }