001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.gui.layer.OsmDataLayer;
012import org.openstreetmap.josm.tools.Shortcut;
013
014/**
015 * Undoes the last command.
016 *
017 * @author imi
018 */
019public 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    @Override
032    public void actionPerformed(ActionEvent e) {
033        if (Main.map == null)
034            return;
035        Main.map.repaint();
036        Main.main.undoRedo.undo();
037    }
038
039    @Override
040    protected void updateEnabledState() {
041        setEnabled(Main.main != null && !Main.main.undoRedo.commands.isEmpty());
042    }
043
044    @Override
045    public void commandChanged(int queueSize, int redoSize) {
046        if (Main.main.undoRedo.commands.isEmpty()) {
047            putValue(NAME, tr("Undo"));
048            setTooltip(tr("Undo the last action."));
049        } else {
050            putValue(NAME, tr("Undo ..."));
051            setTooltip(tr("Undo {0}",
052                    Main.main.undoRedo.commands.getLast().getDescriptionText()));
053        }
054    }
055}