Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Undo

Reply
 
 
Adam
Guest
Posts: n/a
 
      09-17-2004
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


 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      09-17-2004
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>
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      09-17-2004
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/...c/Notepad.java

--
Thomas<dot>Fritsch<squiggle>ops<dot>de

 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      09-17-2004
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


 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      09-20-2004
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

 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      09-21-2004
Yes, this seems to be what I need.
I'll try that out.

Thanks againg,
Adam


 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      09-22-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to "undo" a database delete Morgan Bachu ASP .Net 13 08-31-2005 03:08 AM
Undo formatting tshad ASP .Net 5 04-13-2005 09:24 PM
How to open a "closed TAB" ( is there an UNDO command?) bd Firefox 5 01-05-2005 03:43 AM
How do I undo WebRequest.RegisterPrefix john farrow ASP .Net 0 04-13-2004 09:21 PM
Undo in TextBox Marco Liedekerken ASP .Net 0 06-26-2003 10:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57