-1

I'm developing a basic editor within my application, which allows to view several components within a JTabbedPane. Each tab holds a JScrollPane which contains a JEditorPane.

Now I would like to implement an undo and redo functionality. So if I press the undo or redo button in my toolbar or use the defined keystroke, I would like the UndoManager to undo or redo my last edit in the currently viewed document.

There are several Tutorials, like this one, on how to do this for a single document or text component but not for a full document stack. Do you have any ideas.

I tried the following. I implemented the UndoHandler, Undo and RedoAction like this, ...

class UndoHandler implements UndoableEditListener {

    public void undoableEditHappened(UndoableEditEvent e) {
        undoManager.addEdit(e.getEdit());
        undoAction.update();
        redoAction.update();
    }
}

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

    public void actionPerformed(ActionEvent e) {
        try {
            undoManager.undo();
        } catch (CannotUndoException ex) {
            ex.printStackTrace();
        }

        update();
        redoAction.update();
    }

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

class RedoAction extends AbstractAction {
    public RedoAction() {
        super("Redo");
        setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
        try {
            undoManager.redo();
        } catch (CannotRedoException ex) {
            ex.printStackTrace();
        }

        update();
        undoAction.update();
    }

    protected void update() {
        if (undoManager.canRedo()) {
            setEnabled(true);
            putValue(Action.NAME, undoManager.getRedoPresentationName());
        } else {
            setEnabled(false);
            putValue(Action.NAME, "Redo");
        }
    }
}

... and assigned it to each document like this:

        // Fetch the under laying document ...
        ObjDocument doc = (ObjDocument) editor.getDocument();

        // ... and assign the undo listener.
        doc.addUndoableEditListener(undoHandler);

Thanks & Best regards

Andreas

Phidelux
  • 2,043
  • 1
  • 32
  • 50
  • 2
    An [SSCCE](http://sscce.org) would help greatly. – Hovercraft Full Of Eels Jan 21 '12 at 17:47
  • 1
    I don't understand, it would be the same for a bunch of documents. Each would get its own `UndoManager` and `UndoHandler`. – stryba Jan 21 '12 at 17:55
  • I added my recent tests. But I don't understand what you mean. Could you make a short example? – Phidelux Jan 21 '12 at 18:28
  • 1
    @Avedo: exactly, ***could you make a short example***? an [SSCCE](http://sscce.org)? That we would know exactly where your problem lies. – Hovercraft Full Of Eels Jan 21 '12 at 23:50
  • 1
    It's hard to present an short example if I'm locking for a solution. I don't have code, that is not doing what it is expected for. I need an Idea how to implement undo and redo functionality for more than one Document. All examples show just how to implement this for one single document. – Phidelux Jan 22 '12 at 15:42

1 Answers1

0

Usualy to get undo functionality you can use command pattern.

Check this out:

http://www.javaworld.com/javaworld/jw-06-1998/jw-06-undoredo.html

http://www.javaworld.com/javaworld/javatips/jw-javatip68.html

http://twit88.com/blog/2008/01/26/design-pattern-in-java-101-command-pattern-behavioral-pattern/

Matjaz Muhic
  • 5,328
  • 2
  • 16
  • 34
  • Thanks for this links, but I already know the Command pattern and it doesn't help in this case, because I just would like to undo and redo basic text operations. This functionality is already implemented in the text components. I just don't know how to use it with more than one document at the same time. – Phidelux Jan 21 '12 at 18:27