An example that shows lots of little UndoManager details : Undo Redo « Swing JFC « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing JFC » Undo RedoScreenshots 
An example that shows lots of little UndoManager details
An example that shows lots of little UndoManager details
 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// UndoManagerDetails.java
//An example that shows lots of little UndoManager details
//

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import javax.swing.undo.UndoableEdit;

public class UndoManagerDetails {
  public static void main(String[] args) {
    UndoManager mgr = new UndoManager();

    // Show how insignificant edits are skipped over.
    //
    //                                 # adds? sig? replace?
    mgr.addEdit(new SampleUndoableEdit(1, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(2, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(3, false, false, false));
    mgr.addEdit(new SampleUndoableEdit(4, false, false, false));

    System.out.println("--------------------------");
    System.out.println("Insignificant edit example");
    System.out.println("--------------------------");
    mgr.undo();
    mgr.redo();
    System.out.println(mgr.canRedo())// No more sig. edits

    // Show how edits that call add/replace are used.
    //
    //                                 # adds? sig? replace?
    mgr.addEdit(new SampleUndoableEdit(5, true, true, false));
    mgr.addEdit(new SampleUndoableEdit(6, false, true, false));
    System.out.println("----------------------------------");
    System.out.println("Absorbed (by addEdit) edit example");
    System.out.println("----------------------------------");
    mgr.undo();
    mgr.discardAllEdits();

    //                                 # adds? sig? replace?
    mgr.addEdit(new SampleUndoableEdit(1, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(2, false, true, true));
    System.out.println("--------------------------------------");
    System.out.println("Absorbed (by replaceEdit) edit example");
    System.out.println("--------------------------------------");
    mgr.undo();
    System.out.println(mgr.canUndo());

    // Show how changing limit works.
    mgr.discardAllEdits();

    //                                 # adds? sig? replace?
    mgr.addEdit(new SampleUndoableEdit(1, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(2, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(3, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(4, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(5, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(6, false, true, false));
    System.out.println("----------------------");
    System.out.println("Changing limit example");
    System.out.println("----------------------");
    mgr.undo();
    mgr.undo();
    mgr.undo()// Now 3 undoable, 3 redoable
    mgr.setLimit(4)// Now 2 undoable, 2 redoable!
    while (mgr.canUndo())
      mgr.undo();
    while (mgr.canRedo())
      mgr.redo();

    // undoOrRedo example
    mgr.discardAllEdits();
    mgr.setLimit(1);

    //                                 # adds? sig? replace?
    mgr.addEdit(new SampleUndoableEdit(1, false, true, false));
    System.out.println("------------------");
    System.out.println("undoOrRedo example");
    System.out.println("------------------");
    System.out.println(mgr.getUndoOrRedoPresentationName());
    mgr.undoOrRedo();
    System.out.println(mgr.getUndoOrRedoPresentationName());
    mgr.undoOrRedo();

    // Show how UndoManager becomes a CompositeEdit.
    mgr.discardAllEdits();
    mgr.setLimit(100);

    //                                 # adds? sig? replace?
    mgr.addEdit(new SampleUndoableEdit(1, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(2, false, true, false));
    mgr.addEdit(new SampleUndoableEdit(3, false, true, false));
    System.out.println("------------------------------");
    System.out.println("Transform to composite example");
    System.out.println("------------------------------");
    mgr.end();
    mgr.undo();
    mgr.redo();

    // Show that adds are no longer allowed. Note that addEdit() returns
    // true in
    // pre-JDK 1.2 Swing releases. This is fixed in JDK 1.2.
    System.out.println(mgr.addEdit(new SampleUndoableEdit(4, false, true,
        false)));
    mgr.undo()// note that edit 4 is not there
  }
}

//SampleUndoableEdit.java
//A simple (?) example of an undoable edit.
//

class SampleUndoableEdit extends AbstractUndoableEdit {

  private boolean isSignificant;

  private boolean isReplacer;

  private int number;

  private boolean allowAdds;

  private Vector addedEdits;

  private UndoableEdit replaced;

  // Create a new edit with an identifying number. The boolean arguments
  // define
  // the edit's behavior.
  public SampleUndoableEdit(int number, boolean allowAdds,
      boolean isSignificant, boolean isReplacer) {
    this.number = number;
    this.allowAdds = allowAdds;
    if (allowAdds)
      addedEdits = new Vector();
    this.isSignificant = isSignificant;
    this.isReplacer = isReplacer;
  }

  // "Undo" the edit by printing a message to the screen.
  public void undo() throws CannotUndoException {
    super.undo();
    System.out.print("Undo " + number);
    dumpState();
  }

  // "Redo" the edit by printing a message to the screen.
  public void redo() throws CannotRedoException {
    super.redo();
    System.out.print("Redo " + number);
    dumpState();
  }

  // If allowAdds is true, we store the input edit. If not, just return false.
  public boolean addEdit(UndoableEdit anEdit) {
    if (allowAdds) {
      addedEdits.addElement(anEdit);
      return true;
    else
      return false;
  }

  // If isReplacer is true, we store the edit we are replacing.
  public boolean replaceEdit(UndoableEdit anEdit) {
    if (isReplacer) {
      replaced = anEdit;
      return true;
    else
      return false;
  }

  // Significance is based on constructor parameter.
  public boolean isSignificant() {
    return isSignificant;
  }

  // Just return our identifier.
  public String toString() {
    return "<" + number + ">";
  }

  // Debug output.
  public void dumpState() {
    if (allowAdds && addedEdits.size() 0) {
      Enumeration e = addedEdits.elements();
      System.out.print(" (absorbed: ");
      while (e.hasMoreElements()) {
        System.out.print(e.nextElement());
      }
      System.out.print(")");
    }
    if (isReplacer && replaced != null) {
      System.out.print(" (replaced: " + replaced + ")");
    }
    System.out.println();
  }
}

           
         
  
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. Undo redo textareaUndo redo textarea
6. Undo managerUndo manager
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.