Undo manager : Undo Redo « Swing JFC « Java






Undo manager

Undo manager
 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

public class UndoManagerDemo extends JFrame {
  protected Vector pointVector = new Vector();

  protected PaintCanvas canvas = new PaintCanvas(pointVector);

  protected UndoManager undoManager = new UndoManager();

  protected JButton undoButton = new JButton("Undo");

  protected JButton redoButton = new JButton("Redo");

  public UndoManagerDemo() {
    super("Undo/Redo Demo");

    undoButton.setEnabled(false);
    redoButton.setEnabled(false);

    JPanel buttonPanel = new JPanel(new GridLayout());
    buttonPanel.add(undoButton);
    buttonPanel.add(redoButton);

    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(canvas, BorderLayout.CENTER);

    canvas.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        Point point = new Point(e.getX(), e.getY());
        pointVector.addElement(point);

        undoManager.undoableEditHappened(new UndoableEditEvent(
            canvas, new UndoablePaintSquare(point, pointVector)));

        undoButton.setText(undoManager.getUndoPresentationName());
        redoButton.setText(undoManager.getRedoPresentationName());
        undoButton.setEnabled(undoManager.canUndo());
        redoButton.setEnabled(undoManager.canRedo());
        canvas.repaint();
      }
    });

    undoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          undoManager.undo();
        } catch (CannotRedoException cre) {
          cre.printStackTrace();
        }
        canvas.repaint();
        undoButton.setEnabled(undoManager.canUndo());
        redoButton.setEnabled(undoManager.canRedo());
      }
    });

    redoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          undoManager.redo();
        } catch (CannotRedoException cre) {
          cre.printStackTrace();
        }
        canvas.repaint();
        undoButton.setEnabled(undoManager.canUndo());
        redoButton.setEnabled(undoManager.canRedo());
      }
    });

    setSize(400, 300);
    setVisible(true);
  }

  public static void main(String argv[]) {
    new UndoManagerDemo();
  }
  class PaintCanvas extends JPanel {
    private Vector points;

    protected int width = 50;

    protected int height = 50;

    public PaintCanvas(Vector v) {
      super();
      points = v;
      setOpaque(true);
      setBackground(Color.white);
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.black);
      
      for(int i=0;i<points.size();i++){
        Point point = (Point) points.get(i);
        g.drawRect(point.x, point.y, width, height);
      }
    }
  }

  class UndoablePaintSquare extends AbstractUndoableEdit {
    protected Vector points;

    protected Point point;

    public UndoablePaintSquare(Point p, Vector v) {
      points = v;
      point = p;
    }

    public String getPresentationName() {
      return "Square Addition";
    }

    public void undo() {
      super.undo();
      points.remove(point);
    }

    public void redo() {
      super.redo();
      points.add(point);
    }
  }
}

           
         
  








Related examples in the same category

1.The use of UndoableToggleEditThe use of UndoableToggleEdit
2.The use of StateEdit(able)The use of StateEdit(able)
3.The use of UndoManagerThe use of UndoManager
4.A sample app showing the use of UndoableToggleEdit and CompoundEditA sample app showing the use of UndoableToggleEdit and CompoundEdit
5.An example that shows lots of little UndoManager detailsAn example that shows lots of little UndoManager details
6.Undo redo textareaUndo redo textarea
7.Simple GUI demo of UndoManager and friendsSimple GUI demo of UndoManager and friends
8.Undo Example 1Undo Example 1
9.Undo Example 2Undo Example 2
10.Undo Example 3Undo Example 3
11.Undo Example 4Undo Example 4
12.Undo Example 5Undo Example 5
13.Undo Example 6Undo Example 6
14.Undoable Drawing Panel 2Undoable Drawing Panel 2
15.Undo DrawingUndo Drawing
16.Undo Example 7Undo Example 7
17.Creating TextArea with Undo, Redo Capabilities
18.Add Undo and Redo to a text component
19.Add undo support to the StyleFrame exampleAdd undo support to the StyleFrame example
20.Adding Undo and Redo to a Text Component
21.Create a redo action and add it to the text component (JTextComponent)
22.Listen for undo and redo events
23.Create an undo action and add it to the text component
24.Bind the undo action to ctl-Z