It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes About Undo! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "About Undo!" Watch "About Undo!" New topic
Author

About Undo!

eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
Can you pls help me do undo...

I already have a complete and working code of undo and undoAction but the problem is it is so confusing and I can't understand the importance of each code. Maybe you can give me the steps on how to do it and I will just compare it to the codes I have...

Please help me...

Thanks!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Originally posted by eLL Pascual:
Maybe you can give me the steps on how to do it and I will just compare it to the codes I have...


Maybe you can post your code and we can walk you through it.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
eLL Pascual
Greenhorn

Joined: May 22, 2008
Posts: 21
This is my code... but it is not yet a SSCCE...

code:
----------------------------------------------------------------------------

public class notepad
{

HashMap<Object, Action> actions;

protected UndoAction undoAction;
protected RedoAction redoAction;
protected UndoManager undo = new UndoManager();

public constructor(){
actions = createActionTable(textArea);
}

class UndoAction extends AbstractAction {
public UndoAction() {
super("Undo");
setEnabled(true);
}

public void actionPerformed(ActionEvent e) {
try {
undo.undo();
}
catch (CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
updateUndoState();
redoAction.updateRedoState();
}

protected void updateUndoState() {
if (undo.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}

protected class MyUndoableEditListener implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent e) {

undo.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
}

private HashMap<Object, Action> createActionTable(JTextComponent textComponent) {
HashMap<Object, Action> actions = new HashMap<Object, Action>();
Action[] actionsArray = textComponent.getActions();
for (int i = 0; i < actionsArray.length; i++) {
Action a = actionsArray[i];
actions.put(a.getValue(Action.NAME), a);
}
return actions;
}

private Action getActionByName(String name) {
return actions.get(name);
}

}
----------------------------------------------------------------------------
[ June 11, 2008: Message edited by: eLL Pascual ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

1) Always use CODE tags. It makes your code a lot easier to read and understand.
2) Please use proper naming conventions in your code. it should be
Thisis a good place to pick up pointers on coding styles.
3) What is ? Is that your constructor or a method?
4)
I already have a complete and working code of undo and undoAction
Is the code you posted part of your working code? I doubt.
5)
but the problem is it is so confusing and I can't understand the importance of each code
Which part did you not understand
[ June 11, 2008: Message edited by: Maneesh Godbole ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Undo!
 
Similar Threads
How to add Undo and Redo actions to JPanel
maintain action history of drag and drop in jtree
how to undo the last action printed on an applet using undo button
Using cell editors in JTable
how to undo the last action printed on an applet using undo button