This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 120218 - A way to listen to UndoableEdits (was: UndoRedo - No ChangeEvent published to listeners)
Summary: A way to listen to UndoableEdits (was: UndoRedo - No ChangeEvent published to...
Status: STARTED
Alias: None
Product: platform
Classification: Unclassified
Component: Window System (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: issues@platform
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-10-26 18:21 UTC by prunand
Modified: 2010-08-11 13:31 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Example of UndoRedo ChangeListener bug. (14.80 KB, application/octet-stream)
2007-10-31 16:16 UTC, prunand
Details
Includes a modified UndoRedoManager that issues ChangeEvents when undoable edits are added. (16.72 KB, application/octet-stream)
2007-11-02 23:44 UTC, prunand
Details

Note You need to log in before you can comment on or make changes to this bug.
Description prunand 2007-10-26 18:21:34 UTC
I have implemented custom Undo Redo actions in order to support Undo/Redo outside of my own TopComponents.  My
implementation attempted to use UndoRedo.addChangeListener() in order to capture changes in the state of the current
TopComponent's UndoRedo.  The docs state for addChangeListener(): "The listener will be notified every time the
undo/redo ability of this object changes."  At no point has my listener's ChangeListener.stateChanged() method been called.

As a work-around, I have overriden isEnabled() in my Undo/Redo actions in the meantime.
Comment 1 Jiri Rechtacek 2007-10-27 16:56:00 UTC
UndoRedo interface&impl is part of o.o.awt.
Comment 2 _ tboudreau 2007-10-28 21:49:39 UTC
FYI, the code that publishes the events is really weird and complicated (it looked like it might work but probably someone could subclass and break it).  It 
looks like it was once intended to be multi-threaded (creates a runnable, etc.), and someone then changed it to run inline.  Probably should be rewritten to do 
the obvious thing of just firing an event when an undo event is added/removed.
Comment 3 David Simonek 2007-10-29 09:24:47 UTC
Reporter, could you attach runnable source code of the module so that I can reproduce, test and debug? Strip any
unnecessary code which is irrelevant for this bug, of course. Thanks.
Comment 4 David Simonek 2007-10-31 11:28:23 UTC
prunand, please attach requested info and then reopen, thanks.
Comment 5 prunand 2007-10-31 15:30:11 UTC
I plan to create you a concise example today.  I'll post it shortly.
Comment 6 prunand 2007-10-31 16:16:51 UTC
Created attachment 52140 [details]
Example of UndoRedo ChangeListener bug.
Comment 7 prunand 2007-10-31 16:19:24 UTC
Sorry for the delay.  Here is an example.  Click on "Undoable editor" under Window.  There is a text field that adds
UndoableEdits to the UndoRedo.Manager for the topcomponent.  There is also a textarea where events are logged to.

As UndoableEdits are added to the topcomponents UndoRedo, they are logged to std out, and to the text area.  The same
should happen as the state of the UndoRedo.Manager is changed, but this never happens.
Comment 8 David Simonek 2007-10-31 16:56:37 UTC
x
Comment 9 Jaroslav Tulach 2007-11-01 07:32:04 UTC
I am not sure if my comment is relevant, but
void UndoRedo.addChangeListener(ChangeListener l) javadoc says:
Add a change listener. The listener will be notified every time the undo/redo ability of this object changes.

Just by adding some UndoableEdit to the manager, there is often no change as canUndo and canRedo still return the same 
values. That is why there is no reason for any change event to be fired.
Comment 10 _ tboudreau 2007-11-01 15:49:53 UTC
This is probably my misunderstanding then.  What is needed is a way to be notified when a new undo event appears - in order to support global undo-redo 
(not tied to TopComponents) and allow components with their own existing UndoRedo.Manager to have their undoable events published into a global undo-
redo manager (which some custom Undo/Redo actions would work with).
Comment 11 David Simonek 2007-11-01 16:05:42 UTC
So do I understand correctly that we need new API, something like new interface UndoRedoEdits with
add/removeUndoableEditListener? Or isn't it possible to use javax.swing.undo.UndoManager and its
addUndoableEditListener(...) somehow?

Changing to enhancement and updating summary then, please correct me if I'm wrong.
Comment 12 prunand 2007-11-02 16:55:42 UTC
This still seems broken to me.  From the Class description of UndoRedo.Manager: "An undo manager which fires a change
event each time it consumes a new undoable edit."  This is of course in addition to the addChangeListener() description.
 The UndoRedo.Manager is not issuing any change events when its state changes.

A new API may be what you have to implement, since there may be some backward compatibility problems, but then the old
API should be marked as deprectaed and commented as broken.  Also, I don't think UndoableEditListener is the right API
to model after, since this is a listener to register to consume UndoableEdits from objects that may produce them (such
as Document).

I'm going to pull in the UndoRedo.Manager code into my example, and see if I can make it bend to my will.  I'll report
back with what I find.
Comment 13 prunand 2007-11-02 18:02:57 UTC
The source for this is actually very small.  It looks like the sole purpose for this class is to support the addition of
changeListeners.  The code seems to make the assumption that users of this class will be issuing UndoableEditEvents in
order to modify the Undo/Redo stack.  My code was directly adding UndoableEdits via the addEdit method.  This seems like
merely an oversight in UndoRedo.Manager.  I made a small change to fire change events if addEdit is called.  The method
I added is simply:
    
    @Override
    public synchronized boolean addEdit(final UndoableEdit anEdit) {
        final boolean retval = super.addEdit(anEdit);
        
        cs.fireChange();
        
        return retval;
    }

It may be more appropriate for me to issue UndoableEditEvents than my current implementation.  I'll do some more
testing, and post a new example with the change later today.
Comment 14 prunand 2007-11-02 23:44:15 UTC
Created attachment 52425 [details]
Includes a modified UndoRedoManager that issues ChangeEvents when undoable edits are added.
Comment 15 David Simonek 2008-10-23 16:14:31 UTC
Oops, thanks very much for the contribution, I somehow overlooked it. I think it could be integrated as it is (your
proposed fix), we just need to note the change in api changes for the module.

prunand, could you make a patch (diff) for me? I think I understand what needs to be done, but it will be safer this
way, as I'm not familiar with UndoRedomanager code, thanks.