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 83963 - IllegalStateException in Redo
Summary: IllegalStateException in Redo
Status: VERIFIED FIXED
Alias: None
Product: editor
Classification: Unclassified
Component: -- Other -- (show other bugs)
Version: 5.x
Hardware: PC Windows XP
: P1 blocker (vote)
Assignee: Ayub Khan
URL:
Keywords:
Depends on:
Blocks: 81655
  Show dependency tree
 
Reported: 2006-08-31 20:10 UTC by htt
Modified: 2007-11-05 13:44 UTC (History)
4 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
source view (56.34 KB, application/octet-stream)
2006-09-11 20:05 UTC, Ayub Khan
Details
source view updated (56.00 KB, application/octet-stream)
2006-09-11 20:05 UTC, Ayub Khan
Details
twisted contents in source view (55.44 KB, application/octet-stream)
2006-09-12 08:55 UTC, Ayub Khan
Details
Patch for better exception diagnostics (721 bytes, application/octet-stream)
2006-09-13 13:31 UTC, Miloslav Metelka
Details
Patch for better exception diagnostics (721 bytes, patch)
2006-09-13 13:32 UTC, Miloslav Metelka
Details | Diff
Extra debugging for BaseDocumentEvent in release55 branch (2.52 KB, patch)
2006-09-15 13:36 UTC, Miloslav Metelka
Details | Diff
Log with the extra debug information from the described reproduction case (7.09 KB, text/plain)
2006-09-15 13:38 UTC, Miloslav Metelka
Details
Stacktraces of the problematic modifications; the IDE run with -J-Dnetbeans.debug.editor.document.stack=true (8.72 KB, text/plain)
2006-09-15 18:30 UTC, Miloslav Metelka
Details

Note You need to log in before you can comment on or make changes to this bug.
Description htt 2006-08-31 20:10:40 UTC
With trunk build 060831_1, jdk1.5.0_07...

o Open sample LoanApp schema.
o In Schema view, right-click Complex Types and choose Add Complex Type; name it
"borrow"
o In Source view, 
  - add simple type "lend" by entering the followings just before the
</xs:schema> line:
   <xs:simpleType name="lend">
     <xs:list>
       <xs:simpleType>
         <xs:restriction base="xs:string"/>
       </xs:simpleType>
     </xs:list>
   </xs:simpleType>
  - click on Validate XML icon to make sure the code is clean.

o In Schema view, 
  - click Undo or CTRL-Z -> "lend" is gone from Simple Types
  - click Undo or CTRL-Z again -> "borrow" disappears from Complex Types
  - click Redo or CTRL-Y -> "borrow" is back under Complex Types
  - click Redo or CTRL-Y again
    => 1. NPE below (*)
       2. Schema & Design views blank out with "Schema is not well-formed" msg
       3. in Source view, the section of code involving "borrow" and "lend"
looks rather messed up.  Click on Validate XML icon:
XML validation started.
C:/z/JavaApplication1/src/newLoanApplication.xsd:266,1
Element type "x" must be followed by either attribute specifications, ">" or "/>".
C:/z/JavaApplication1/src/newLoanApplication.xsd:266,1
Element type "x" must be followed by either attribute specifications, ">" or "/>".
2 Error(s),  0 Warning(s).
XML validation finished.

          
(*)
java.lang.IllegalStateException: javax.swing.text.BadLocationException: Length
must be positive
        at
org.netbeans.editor.FixLineSyntaxState.fixSyntaxStateInfos(FixLineSyntaxState.java:492)
        at
org.netbeans.editor.FixLineSyntaxState.update(FixLineSyntaxState.java:275)
        at
org.netbeans.editor.FixLineSyntaxState$AfterLineUndo.redo(FixLineSyntaxState.java:589)
        at org.netbeans.editor.BaseDocumentEvent.redo(BaseDocumentEvent.java:322)
        at
org.netbeans.editor.GuardedDocumentEvent.redo(GuardedDocumentEvent.java:56)
        at javax.swing.undo.CompoundEdit.redo(CompoundEdit.java:59)
        at
org.netbeans.editor.BaseDocument$AtomicCompoundEdit.redo(BaseDocument.java:1699)
        at
org.netbeans.editor.BaseDocument$AtomicCompoundEdit.redo(BaseDocument.java:1694)
        at javax.swing.undo.UndoManager.redoTo(UndoManager.java:222)
        at javax.swing.undo.UndoManager.redo(UndoManager.java:311)
        at
org.openide.text.CloneableEditorSupport$CESUndoRedoManager.access$2301(CloneableEditorSupport.java:2637)
        at
org.openide.text.CloneableEditorSupport$CESUndoRedoManager$RenderUndo.run(CloneableEditorSupport.java:2772)
        at org.netbeans.editor.GuardedDocument.runAtomic(GuardedDocument.java:226)
        at
org.openide.text.CloneableEditorSupport$CESUndoRedoManager$RenderUndo.<init>(CloneableEditorSupport.java:2757)
        at
org.openide.text.CloneableEditorSupport$CESUndoRedoManager$RenderUndo.<init>(CloneableEditorSupport.java:2749)
        at
org.openide.text.CloneableEditorSupport$CESUndoRedoManager.redo(CloneableEditorSupport.java:2653)
        at
org.netbeans.modules.xml.xam.ui.undo.FilterUndoManager.redo(FilterUndoManager.java:116)
        at
org.netbeans.modules.xml.xam.ui.undo.CompoundUndoManager.redo(CompoundUndoManager.java:184)
        at
org.netbeans.modules.xml.xam.ui.undo.QuietUndoManager.redo(QuietUndoManager.java:154)
        at org.openide.actions.RedoAction.performAction(RedoAction.java:71)
        at
org.openide.util.actions.CallableSystemAction$1.run(CallableSystemAction.java:100)
        at
org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(ActionsBridge.java:53)
        at
org.openide.util.actions.CallableSystemAction.actionPerformed(CallableSystemAction.java:96)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
        at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
        at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
        at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
        at java.awt.Component.processMouseEvent(Component.java:5488)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
        at org.openide.awt.ToolbarButton.processMouseEvent(ToolbarButton.java:39)
        at java.awt.Component.processEvent(Component.java:5253)
        at java.awt.Container.processEvent(Container.java:1966)
        at java.awt.Component.dispatchEventImpl(Component.java:3955)
        at java.awt.Container.dispatchEventImpl(Container.java:2024)
        at java.awt.Component.dispatchEvent(Component.java:3803)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
        at java.awt.Container.dispatchEventImpl(Container.java:2010)
        at java.awt.Window.dispatchEventImpl(Window.java:1774)
        at java.awt.Component.dispatchEvent(Component.java:3803)
[catch] at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
        at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
        at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Comment 1 Chris Webster 2006-09-09 00:33:00 UTC
The unit tests are in both xamui and schema/core. Just open QuietUndoManager and
uncomment the last redo() line in the testIssue83963() method.

Note that:

1. The document edit must start on the last line of the model edit change.

2. The document edit must be non-trivial.

The document text is intact, it is the FixLineSyntaxState class in NetBeans that
is causing a BadLocationException and throwing up. I need a debug build of
NetBeans to see what is going on, but my one attempt at building from source
failed because the file layout changed, according to the "golden" file. 
Comment 2 Ayub Khan 2006-09-11 20:04:22 UTC
I did the above scenario, except I used "newComplexType" instead of "borrow".
These are the findings.

1. After doing 2 Redo's as mentioned in the scenario. I see XDM/Schema/AXI model
all valid. However in the source view, the text before </xsd:schema> looks wierd.
See attachment issue83963.png.

2. Eventhough the source looks wierd, I can add complextypes. As I make changes
to the "newComplexType", like add a new element, then the source gets updated.
See attachment issue83963_2.png. 
Comment 3 Ayub Khan 2006-09-11 20:05:19 UTC
Created attachment 33788 [details]
source view
Comment 4 Ayub Khan 2006-09-11 20:05:48 UTC
Created attachment 33789 [details]
source view updated
Comment 5 Ayub Khan 2006-09-12 08:54:03 UTC
After debugging further by adding a document listener to document and printing
the offsets and length of insert/remove, final document length after each
undo/redo operation. I do not see any issue in XDM/XAM/Schema/AXI codes. This
looks like a NB55 Editor issue in FixLineSyntaxState.java code.

See attachment that contains the Source view, with contents twisted, and the
line number 8 duplicated. Lines 9-15 are missing. But document contents seems to
be correct.(using  Ctl+A, Ctl+C)
Comment 6 Ayub Khan 2006-09-12 08:55:00 UTC
Created attachment 33803 [details]
twisted contents in source view
Comment 7 Ayub Khan 2006-09-12 17:44:28 UTC
Assigning to xml/text-editor team, since all the debugging informatin suggests
FixLineSyntaxState.java.
Comment 8 Petr Jiricka 2006-09-13 08:08:24 UTC
> With trunk build 060831_1, jdk1.5.0_07...

Did you really mean trunk? These features are not a part of trunk codebase, only
in NetBeans 5.5.
Comment 9 Marek Fukala 2006-09-13 10:42:47 UTC
It looks like either a bad usage of the undo/redo or a bug in
FixLineSyntaxState. I do not see any dependency of the problem on xml editor.
Reassigning to editor - Mila Metelka.
Comment 10 Miloslav Metelka 2006-09-13 13:31:13 UTC
Please apply the attached patch and re-run the test so that we see where the
exception really occurred. Thanks.
Comment 11 Miloslav Metelka 2006-09-13 13:31:59 UTC
Created attachment 33872 [details]
Patch for better exception diagnostics
Comment 12 Miloslav Metelka 2006-09-13 13:32:10 UTC
Created attachment 33873 [details]
Patch for better exception diagnostics
Comment 13 Miloslav Metelka 2006-09-13 13:34:45 UTC
IIRC there were no similar problems experienced with FixLineSyntaxState recently
so I suppose this occurred after you've started to use your special undo manager
(removal of the checking for last event to be undone in the BaseDocument by
issue #83740 allowed you to do that).

 I have read again the http://xml.netbeans.org/specs/undo/ and I'm still not
sure that I understand your undo manager solution. However please keep in mind
that regarding the document undos/redos you must always apply them in the proper
order, you cannot skip any of them or insert extra edits between them. Otherwise
the things would get seriously broken because the edits use absolute offsets of
where the changes are performed so the edits would be applied at wrong places in
the document.
Comment 14 Miloslav Metelka 2006-09-13 13:50:35 UTC
If I could suggest you a solution regarding special undo manager then please
override

    protected UndoRedo.Manager createUndoRedoManager() {
        return new CESUndoRedoManager(this);
    }

and make your own UndoRedo.Manager that will mostly delegate to the UM obtained
from super.createUndoRedoManager() except the undoableEditHappened() method.
That method will normally obtain the undoable edit performed in the document. At
that time you should already know the changes in your visual editor and have
them ready as an undoable edit (your undo manager should be able to access them).
The undoableEditHappened() method should then be implemented in the following way.

    public void undoableEditHappened(final UndoableEditEvent ue) {
        if (visualEdit != null) { // any visual changes performed?
            UndoableEdit origEdit = ue.getEdit();
            WrapperCompoundEdit wrapperEdit = new WrapperCompoundEdit(origEdit,
visualEdit);
            ue = new UndoableEditEvent(ue.getSource(), wrapperEdit);
        }
        delegateCESUndoManager.undoableEditHappened(ue);
    }

Regarding the fact that you can have multiple document edits for a single change
- if you execute your changes wrapped by NbDocument.runAtomic() then all your
document changes within that transaction will appear as a single undoable edit.
Comment 15 Miloslav Metelka 2006-09-13 13:55:44 UTC
Or if you want to merge multiple visual changes into just one undo edit you may
use UndoableEdit.replaceEdit() mechanism for merging.
Comment 16 Nathan Fiedler 2006-09-13 21:39:50 UTC
The issue to which you are referring (issue 83740) was fixed a week after this
bug was entered, so no, that had nothing to do with this problem.

Our undo manager is, in fact, delegating to the CloneableEditorSupport undo
manager, and we are keeping the undoable edits in the order in which they
arrive, and we are not introducing any edits that modify the document. This is
all explained in the undo spec to which you referred.

Regarding your other questions/comments, I believe the undo spec should explain
everything. The schema model is generating undoable edits of its own, and our
undo manager is collecting those (and passing them to CESUndoRedoManager).
Likewise, the undo manager is collecting the document edits coming from the
source editor. The only thing our undo manager does is undo/redo several edits
at once, depending on the state of the undo manager.

You can see the code for yourself in release55 branch, in xml/xamui/src/.../undo
(CompoundUndoManager and QuietUndoManager classes).
Comment 17 Nathan Fiedler 2006-09-13 21:49:45 UTC
By the way, using the attached patch, I was only able to get the following extra
information from the exception stack trace:

Caused by: javax.swing.text.BadLocationException: Length must be positive
        at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:815)
        at
org.netbeans.editor.FixLineSyntaxState.fixSyntaxStateInfos(FixLineSyntaxState.java:348)
        ... 50 more

That did not seem to help very much, as we already knew where the exception was
occurring by using the debugger and stepping through the code.
Comment 18 Miloslav Metelka 2006-09-15 13:35:12 UTC
I have debugged the problem with the following scenario similar to yours:
1) Have empty xsd
2) In Schema view Add Complex Type name it "a"
3) In Source view I type Enter then "<xsd"; choose "<xsd:simpleType" from code
completion; type " name="b">"; choose "</xsd:simpleType>" from completion (I'm
describing it in a detail because in slightly different case the exception was
not thrown but this one always worked). So I end up with

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xml.netbeans.org/schema/xmlSchema"
            xmlns:tns="http://xml.netbeans.org/schema/xmlSchema"
            elementFormDefault="qualified">
    <xsd:complexType name="a">
        <xsd:sequence/>
    </xsd:complexType>
    <xsd:simpleType name="b"></xsd:simpleType>
</xsd:schema>

4) In schema I do Undo; Undo; Redo and the following Redo will throw an exception.

I have patched the class BaseDocumentEvent in editor library to show the
creation of the document modification events and their undo and redo. I have
added a static counter to verify the proper order of the events in undo/redo.
The order was correct but there is another problem - there are document
modifications (document.insertString()/remove()) performed and followed by redo
of edits that were done *before* the just performed modifications. That cannot work.
In fact I don't understand why the undo/redo should do any extra modifications.

Regarding the attached log - at the end of the second undo the document
modifications are performed:
UNDO of id=66
    UNDO of id=66 finished
UNDO of id=65
    UNDO of id=65 finished
UNDO of id=64
    UNDO of id=63 after this undo ...
UNDO of id=63
    UNDO of id=63 finished
    UNDO of id=64 finished
CREATED id=82, type=REMOVE, offset=277, length=77
    removed text='    <xsd:complexType name="a">
        <xsd:sequence/>
    </xsd:complexType>'
CREATED id=83, type=INSERT, offset=277, length=17


The first redo:
CREATED id=84, type=REMOVE, offset=277, length=17
    removed text='
                '
CREATED id=85, type=INSERT, offset=277, length=77


The second redo:
REDO of id=64
    REDO of id=63 before this redo ...
REDO of id=63
    REDO of id=63 finished
    REDO of id=64 finished
REDO of id=65
    REDO of id=65 finished
REDO of id=66
    REDO of id=66 finished
REDO of id=67
    REDO of id=67 finished
REDO of id=68
    REDO of id=68 finished
REDO of id=69
*********** Exception occurred ************ at 2:32 PM on Sep 15, 2006
java.lang.IllegalStateException: javax.swing.text.BadLocationException: Length
must be positive
        at
org.netbeans.editor.FixLineSyntaxState.fixSyntaxStateInfos(FixLineSyntaxState.java:492)
        at
org.netbeans.editor.FixLineSyntaxState.update(FixLineSyntaxState.java:275)
...
Comment 19 Miloslav Metelka 2006-09-15 13:36:10 UTC
Created attachment 34005 [details]
Extra debugging for BaseDocumentEvent in release55 branch
Comment 20 Miloslav Metelka 2006-09-15 13:38:02 UTC
Created attachment 34006 [details]
Log with the extra debug information from the described reproduction case
Comment 21 Miloslav Metelka 2006-09-15 13:43:11 UTC
Marking invalid for the editor (please reopen and reassign if you want to
continue to solve the problem on your side in terms of this issue).
Comment 22 Miloslav Metelka 2006-09-15 18:30:19 UTC
Created attachment 34030 [details]
Stacktraces of the problematic modifications; the IDE run with -J-Dnetbeans.debug.editor.document.stack=true
Comment 24 Ayub Khan 2006-09-19 02:10:25 UTC
Assigning to myself, since we have a solution that will avoid the Exception.
Comment 25 Ayub Khan 2006-09-19 02:11:28 UTC
Reviewer comments:

Hi Ayub the change looks safe to me.
-Nam 

QE comments:

using trunk build 060915, with xdm.xml & xdm.jar replaced
per Ayub's instructions: issue is fixed... no more NPE and
undo/redo work fine in this scenario.

thanks,
hao.

Files changed:

/cvs/xml/schema/api/test/unit/src/org/netbeans/modules/xml/schema/model/impl/xdm/Attic/UndoRedoTest.java
new revision: 1.1.4.1; previous revision: 1.1

/cvs/xml/xdm/src/org/netbeans/modules/xml/xdm/XDMModel.java
new revision: 1.2.2.46; previous revision: 1.2.2.45

/cvs/xml/schema/api/test/unit/src/org/netbeans/modules/xml/schema/model/resources/Attic/undoredo.xsd
new revision: 1.1.4.1; previous revision: 1.1
Comment 26 htt 2006-09-20 20:59:33 UTC
Verified in CB 060920_11.