Undo

Discussion in 'Java' started by Adam, Sep 17, 2004.

  1. Adam

    Adam Guest

    Does anyone know any tutorials, guides, code samples
    on how to use javax.swing.undo package?
    I've been searching sun's site for last half of an hour
    and found nothing :(

    Thanks in advance,
    Adam
     
    Adam, Sep 17, 2004
    #1
    1. Advertising

  2. Adam wrote:
    > Does anyone know any tutorials, guides, code samples
    > on how to use javax.swing.undo package?
    > I've been searching sun's site for last half of an hour
    > and found nothing



    Google [ sun java javax.swing.undo ]
    <http://www.google.com/search?hl=en&ie=UTF-8&q=sun+java+javax.swing.undo&btnG=Google+Search>

    javax.swing.undo (Java 2 Platform SE v1.4.2)
    <http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/undo/package-summary.html>

    Description
    <http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/undo/package-summary.html#package_description>

    Implementing Undo and Redo
    <http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#undo>
     
    Thomas Schodt, Sep 17, 2004
    #2
    1. Advertising

  3. Adam wrote:
    > Does anyone know any tutorials, guides, code samples
    > on how to use javax.swing.undo package?
    > I've been searching sun's site for last half of an hour
    > and found nothing :(
    >
    > Thanks in advance,
    > Adam


    Take a look at Sun's Notepad sample application (with undo/redo-support)
    file:whereYourJdkIsInstalled/demo/jfc/Notepad/src/Notepad.java or
    http://www-uxsup.csx.cam.ac.uk/java/jdk-1.2.2/demo/jfc/Notepad/src/Notepad.java

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Sep 17, 2004
    #3
  4. Adam

    Adam Guest

    Thomas & Thomas,
    many thanks for the links.

    As I've skimmed the materials,
    it seems to me that... I don't know
    if the framework is suitable for my needs.

    Operations which I perform in my application
    (and want to be able to undo) have nothing
    to do with text or GUI in general.

    It's more like that I have an object of a 'business' class:

    SomeClass
    {
    private int state;
    public void setState(int newState)
    {
    state = newState;
    //code here to notify UndoManager about the change
    }
    }

    I would like to report each call to setState() to
    my UndoManager, and if then user presses undo button,
    I would like the UndoManager to restore the previuos
    state of the object.
    I'm wondering if these requirements are too general
    to be implemented with swing.undo, am I wrong?

    Before I came across swing.undo package I tried to
    design my own framework for that, it looks more or less like this:

    interface UndoAble
    {
    void undo() throws CantUndoException;
    }

    class UndoManager
    {
    undoLastAction();//called by the GUI when user clicks undo button
    addUndoAction(UndoAble action);
    }

    For each class (like SomeClass) i have a corresponding class
    implementing UndoAble interface, which is instantiated when
    setState is called and passed to UndoManager.addUndoAction().
    Each implementation of UndoAble knows exactly how to
    restore the state of its corresponding 'business' class.

    The problem is that I have so many of these classes.

    So I was wondering if using swing.undo would make my life easier
    in any way here.

    Any opinions?

    Adam
     
    Adam, Sep 17, 2004
    #4
  5. Adam schrieb:
    > Thomas & Thomas,
    > many thanks for the links.
    >
    > As I've skimmed the materials,
    > it seems to me that... I don't know
    > if the framework is suitable for my needs.
    >
    > Operations which I perform in my application
    > (and want to be able to undo) have nothing
    > to do with text or GUI in general.
    >
    > It's more like that I have an object of a 'business' class:
    >
    > SomeClass
    > {
    > private int state;
    > public void setState(int newState)
    > {
    > state = newState;
    > //code here to notify UndoManager about the change
    > }
    > }
    >
    > I would like to report each call to setState() to
    > my UndoManager, and if then user presses undo button,
    > I would like the UndoManager to restore the previuos
    > state of the object.
    > I'm wondering if these requirements are too general
    > to be implemented with swing.undo, am I wrong?
    >
    > Before I came across swing.undo package I tried to
    > design my own framework for that, it looks more or less like this:
    >
    > interface UndoAble
    > {
    > void undo() throws CantUndoException;
    > }
    >
    > class UndoManager
    > {
    > undoLastAction();//called by the GUI when user clicks undo button
    > addUndoAction(UndoAble action);
    > }
    >
    > For each class (like SomeClass) i have a corresponding class
    > implementing UndoAble interface, which is instantiated when
    > setState is called and passed to UndoManager.addUndoAction().
    > Each implementation of UndoAble knows exactly how to
    > restore the state of its corresponding 'business' class.
    >
    > The problem is that I have so many of these classes.
    >
    > So I was wondering if using swing.undo would make my life easier
    > in any way here.
    >
    > Any opinions?
    >
    > Adam
    >
    >

    OK, I understand. What you want, is not so much supporting undo/redo in
    a GUI, but rather to make your business object to have its state
    undone/redone.
    By the way: In the Notepad example you only see the GUI-part of the
    undo-thing. But there is also a data part under the hood (you may call
    it the 'business part'), where the real work is done. It is buried
    inside a javax.swing.text.PlainDocument, which holds the actual text
    data. You can see the peak of this iceberg in the Notepad-constructor:
    editor.getDocument().addUndoableEditListener(...);
    So I think your business document class has to interact with swing.undo
    in the right way, i.e. it needs a addUndoableEditListener()
    method and some logic for saving/restoring its internal state. This is
    much less painful than one would expect. And then you may or may not
    implement a GUI on top of it.

    // Stripped-down example for undo/redoable class:
    import java.util.Hashtable;
    import javax.swing.undo.*;
    import javax.swing.event.*;

    // Some kind of business class:
    public class BusinessObject implements StateEditable
    {
    // Our internal state:
    private String a;
    private int b;
    private double c;
    //...

    // Supporting listeners is straight forward:
    private UndoableEditSupport undoSupport = new UndoableEditSupport();
    public void addUndoableEditListener(UndoableEditListener l) {
    undoSupport.addUndoableEditListener(l);
    }
    public void removeUndoableEditListener(UndoableEditListener l) {
    undoSupport.removeUndoableEditListener(l);
    }

    // Implementation of StateEditable is simple again:
    public void storeState(Hashtable savedState) {
    if (a != null)
    savedState.put("a", a);
    savedState.put("b", new Integer(b));
    savedState.put("c", new Float(c));
    //...
    }
    public void restoreState(Hashtable savedState) {
    a = (String) savedState.get("a");
    b = ((Integer) savedState.get("b")).intValue();
    c = ((Double) savedState.get("c")).doubleValue();
    //...
    }

    // Whenever you change the internal state, use this pattern:
    void anyMethod() {
    StateEdit edit = new StateEdit(this);
    // or ... new StateEdit(this, "changing b and c");
    b = 42; c = 3.14;
    edit.end(); // will call storeState()
    undoSupport.postEdit(edit); // will notify our listeners
    }
    }

    // And somewhere in your GUI code:
    // BusinessObject businessObject = ...;
    // businessObject.addUndoableEditListener(...);
    // The rest of the GUI is essentially very much Notepad-like.


    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Sep 20, 2004
    #5
  6. Adam

    Adam Guest

    Yes, this seems to be what I need.
    I'll try that out.

    Thanks againg,
    Adam
     
    Adam, Sep 21, 2004
    #6
  7. Thomas Fritsch wrote:
    >
    > // Stripped-down example for undo/redoable class:

    ....
    > // Implementation of StateEditable is simple again

    // Implementation od StateEditable is not so simple ;-)
    > public void storeState(Hashtable savedState) {
    > if (a != null)
    > savedState.put("a", a);
    > savedState.put("b", new Integer(b));
    > savedState.put("c", new Float(c));
    > //...
    > }
    > public void restoreState(Hashtable savedState) {
    > a = (String) savedState.get("a");
    > b = ((Integer) savedState.get("b")).intValue();
    > c = ((Double) savedState.get("c")).doubleValue();
    > //...
    > }

    public void restoreState(Hashtable savedState) {
    // Be careful, because StateEdit may have optimized
    // the Hashtable by removing the redundant entries
    // which didn't change during the edit:
    if (savedState.containsKey("a"))
    a = (String) savedState.get("a");
    if (savedState.containsKey("b"))
    b = ((Integer) savedState.get("b")).intValue();
    if (savedState.containsKey("c"))
    c = ((Double) savedState.get("c")).doubleValue();
    }
    ....


    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Sep 22, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Liedekerken

    Undo in TextBox

    Marco Liedekerken, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,371
    Marco Liedekerken
    Jun 26, 2003
  2. john farrow

    How do I undo WebRequest.RegisterPrefix

    john farrow, Apr 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    858
    john farrow
    Apr 13, 2004
  3. tshad

    Undo formatting

    tshad, Apr 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    1,091
    tshad
    Apr 13, 2005
  4. Morgan Bachu

    How to "undo" a database delete

    Morgan Bachu, Aug 30, 2005, in forum: ASP .Net
    Replies:
    13
    Views:
    10,663
    James Doughty
    Aug 31, 2005
  5. Andreas Wachhold

    Undo/Redo Java?

    Andreas Wachhold, May 4, 2004, in forum: Java
    Replies:
    2
    Views:
    4,949
    Mykola Rabchevskiy
    May 4, 2004
Loading...

Share This Page