3 import java.awt.FlowLayout;
4 import java.awt.BorderLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.Stack;
8 import javax.swing.BorderFactory;
9 import javax.swing.JButton;
10 import javax.swing.JDialog;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JList;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.ListSelectionModel;
17 import javax.swing.event.ListSelectionEvent;
18 import javax.swing.event.ListSelectionListener;
21 import tim.prune.I18nManager;
22 import tim.prune.undo.UndoOperation;
25 * Class to manage the selection of actions to undo
27 public class UndoManager
30 private JDialog _dialog;
31 private JList _actionList;
37 public UndoManager(App inApp, JFrame inFrame)
40 _dialog = new JDialog(inFrame, I18nManager.getText("dialog.undo.title"), true);
41 _dialog.setLocationRelativeTo(inFrame);
42 JPanel mainPanel = new JPanel();
43 mainPanel.setLayout(new BorderLayout(3, 3));
44 mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
45 Stack undoStack = inApp.getUndoStack();
46 mainPanel.add(new JLabel(I18nManager.getText("dialog.undo.pretext")), BorderLayout.NORTH);
48 String[] undoActions = new String[undoStack.size()];
49 for (int i=0; i<undoStack.size(); i++)
51 undoActions[i] = ((UndoOperation) undoStack.elementAt(undoStack.size()-1-i)).getDescription();
53 _actionList = new JList(undoActions);
54 _actionList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
55 _actionList.setSelectedIndex(0);
56 _actionList.addListSelectionListener(new ListSelectionListener()
58 public void valueChanged(ListSelectionEvent e)
60 if (_actionList.getMinSelectionIndex() > 0)
62 _actionList.setSelectionInterval(0, _actionList.getMaxSelectionIndex());
66 mainPanel.add(new JScrollPane(_actionList), BorderLayout.CENTER);
68 JPanel buttonPanel = new JPanel();
69 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
70 JButton okButton = new JButton("OK");
71 okButton.addActionListener(new ActionListener()
73 public void actionPerformed(ActionEvent e)
75 _app.undoActions(_actionList.getMaxSelectionIndex() + 1);
79 buttonPanel.add(okButton);
80 JButton cancelButton = new JButton("Cancel");
81 cancelButton.addActionListener(new ActionListener()
83 public void actionPerformed(ActionEvent e)
88 buttonPanel.add(cancelButton);
89 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
90 _dialog.getContentPane().add(mainPanel);