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