Undo manager : Undo Redo « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Undo Redo 




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(400300);
    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 = (Pointpoints.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.