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.

View | Details | Raw Unified | Return to bug 42005
Collapse All | Expand All

(-)src/org/openide/awt/UndoRedo.java (-10 / +16 lines)
Lines 164-188 Link Here
164
            /* The following task is finished when there are no
164
            /* The following task is finished when there are no
165
             * undoable edits waiting to be added to undoredo.
165
             * undoable edits waiting to be added to undoredo.
166
             */
166
             */
167
            //Use internal not default RequestProcessor to solve deadlock #10826
167
            
168
            task = internalRequestProcessor.post (new Runnable () {
168
            class R implements Runnable {
169
                public void run () {
169
                public void run () {
170
                    for (;;) {
170
                    for (;;) {
171
			UndoableEditEvent ue;
171
                        UndoableEditEvent ue;
172
                        synchronized (runus) {
172
                        synchronized (runus) {
173
                            if (runus.isEmpty ()) break;
173
                            if (runus.isEmpty ()) break;
174
                            ue = (UndoableEditEvent)runus.removeFirst ();
174
                            ue = (UndoableEditEvent)runus.removeFirst ();
175
                        }
175
                        }
176
                        
176
                        
177
			if (ue == null) {
177
                        if (ue == null) {
178
                	    superDiscardAllEdits();
178
                            superDiscardAllEdits ();
179
			} else {
179
                        } else {
180
                    	    superUndoableEditHappened (ue);
180
                            superUndoableEditHappened (ue);
181
			}
181
                        }
182
                	fireChange();
182
                        fireChange ();
183
                    }
183
                    }
184
                }
184
                }
185
            }, 0, Thread.MAX_PRIORITY);
185
            }
186
            
187
            R r = new R ();
188
            r.run ();
189
            
190
            //Use internal not default RequestProcessor to solve deadlock #10826
191
            //task = internalRequestProcessor.post (r, 0, Thread.MAX_PRIORITY);
186
        }
192
        }
187
        
193
        
188
        /* Attaches change listener to the this object.
194
        /* Attaches change listener to the this object.
(-)src/org/openide/text/CloneableEditorSupport.java (-1 / +2 lines)
Lines 1893-1899 Link Here
1893
         * @see #revertUpcomingUndo */
1893
         * @see #revertUpcomingUndo */
1894
        public void stateChanged(ChangeEvent evt) {
1894
        public void stateChanged(ChangeEvent evt) {
1895
            getUndoRedo().removeChangeListener(this);
1895
            getUndoRedo().removeChangeListener(this);
1896
            SwingUtilities.invokeLater(undoTask);
1896
            undoTask.run ();
1897
            //SwingUtilities.invokeLater(undoTask);
1897
            undoTask = null;
1898
            undoTask = null;
1898
        }
1899
        }
1899
        
1900
        
(-)test/unit/src/org/openide/text/CloneableEditorSupportTest.java (-15 / +8 lines)
Lines 129-149 Link Here
129
        doc.insertString (0, "Kuk", null);
129
        doc.insertString (0, "Kuk", null);
130
        
130
        
131
        String modifiedForAWhile = doc.getText (0, 3);
131
        String modifiedForAWhile = doc.getText (0, 3);
132
        assertEquals ("For a while the test really starts with Kuk", "Kuk", doc.getText (0, 3));
132
        //assertEquals ("For a while the test really starts with Kuk", "Kuk", doc.getText (0, 3));
133
        
133
        
134
        // this waits for undo redo to be performed (it runs in AWT)
134
        assertFalse ("The document cannot be modified", support.getUndoRedo ().canUndo ());
135
        // small problem is that right now the undoredo manager adds its edit
136
        // asynchronously and it might happen that the change will not be there
137
        // a this point, if that happens consider synchronous notification of the
138
        // UndoRedoManager
139
        {
140
            int max = 5;
141
            while (max-- > 0 && support.getUndoRedo ().canUndo ()) {
142
                Thread.sleep (100);
143
            }
144
            
145
            assertFalse ("The document cannot be modified", support.getUndoRedo ().canUndo ());
146
        }
147
        
135
        
148
        String s = doc.getText (0, doc.getLength ());
136
        String s = doc.getText (0, doc.getLength ());
149
        assertEquals ("The document is now the same as at the begining", content, s);
137
        assertEquals ("The document is now the same as at the begining", content, s);
Lines 234-240 Link Here
234
222
235
    public void markModified() throws java.io.IOException {
223
    public void markModified() throws java.io.IOException {
236
        if (cannotBeModified != null) {
224
        if (cannotBeModified != null) {
237
            IOException e = new IOException ();
225
            final String notify = cannotBeModified;
226
            IOException e = new IOException () {
227
                public String getLocalizedMessage () {
228
                    return notify;
229
                }
230
            };
238
            org.openide.ErrorManager.getDefault ().annotate (e, cannotBeModified);
231
            org.openide.ErrorManager.getDefault ().annotate (e, cannotBeModified);
239
            throw e;
232
            throw e;
240
        }
233
        }
(-)test/unit/src/org/openide/text/UndoRedoCooperationTest.java (+203 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
15
package org.openide.text;
16
17
18
import java.io.File;
19
import java.io.IOException;
20
import javax.swing.text.*;
21
import javax.swing.text.BadLocationException;
22
import javax.swing.text.Document;
23
import javax.swing.text.Position;
24
import javax.swing.text.StyledDocument;
25
26
import junit.textui.TestRunner;
27
28
import org.netbeans.junit.NbTestCase;
29
import org.netbeans.junit.NbTestSuite;
30
31
import org.openide.filesystems.FileObject;
32
import org.openide.filesystems.LocalFileSystem;
33
import org.openide.text.CloneableEditorSupport;
34
import org.openide.text.FilterDocument;
35
import org.openide.text.NbDocument;
36
import org.openide.util.RequestProcessor;
37
38
/**
39
 * Emulating old UndoRedo manager deadlock.
40
 *
41
 * @author  Jaroslav Tulach
42
 */
43
public class UndoRedoCooperationTest extends NbTestCase implements CloneableEditorSupport.Env {
44
    /** the support to work with */
45
    private CES support;
46
    // Env variables
47
    private String content = "Hello";
48
    private boolean valid = true;
49
    private boolean modified = false;
50
    /** if not null contains message why this document cannot be modified */
51
    private String cannotBeModified;
52
    private java.util.Date date = new java.util.Date ();
53
    private java.util.List/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList ();
54
    private java.beans.VetoableChangeListener vetoL;
55
    
56
    /** Creates new TextTest */
57
    public UndoRedoCooperationTest (String s) {
58
        super(s);
59
    }
60
    
61
    public static void main(String[] args) {
62
        TestRunner.run(new NbTestSuite(UndoRedoCooperationTest.class));
63
    }
64
65
    protected void setUp () {
66
        support = new CES (this, org.openide.util.Lookup.EMPTY);
67
    }
68
69
    
70
    public void testDeadlock8692 () throws Exception {
71
        doTest (0);
72
    }
73
    
74
    public void testUndoRedo () throws Exception {
75
        doTest (1000);
76
    }
77
    
78
    private void doTest (int sleep) throws Exception {
79
        final StyledDocument d = support.openDocument ();
80
        d.insertString (0, "Ahoj\n", null);
81
        
82
        cannotBeModified = "My reason";
83
        
84
        class R implements Runnable {
85
            private Exception ex;
86
            
87
            public void run () {
88
                try {
89
                    d.remove (0, 2);
90
                } catch (BadLocationException ex) {
91
                    this.ex = ex;
92
                }
93
            }
94
        }
95
96
        R r = new R ();
97
        NbDocument.runAtomic (d, r);
98
99
        if (sleep > 0) {
100
            Thread.sleep (sleep);
101
        }
102
        
103
        assertEquals ("Text contains orignal version", "Aho", d.getText (0, 3));
104
    }
105
    
106
    
107
    //
108
    // Implementation of the CloneableEditorSupport.Env
109
    //
110
    
111
    public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
112
        propL.add (l);
113
    }    
114
    public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
115
        propL.remove (l);
116
    }
117
    
118
    public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
119
        assertNull ("This is the first veto listener", vetoL);
120
        vetoL = l;
121
    }
122
    public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
123
        assertEquals ("Removing the right veto one", vetoL, l);
124
        vetoL = null;
125
    }
126
    
127
    public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
128
        return support;
129
    }
130
    
131
    public String getMimeType() {
132
        return "text/plain";
133
    }
134
    
135
    public java.util.Date getTime() {
136
        return date;
137
    }
138
    
139
    public java.io.InputStream inputStream() throws java.io.IOException {
140
        return new java.io.ByteArrayInputStream (content.getBytes ());
141
    }
142
    public java.io.OutputStream outputStream() throws java.io.IOException {
143
        class ContentStream extends java.io.ByteArrayOutputStream {
144
            public void close () throws java.io.IOException {
145
                super.close ();
146
                content = new String (toByteArray ());
147
            }
148
        }
149
        
150
        return new ContentStream ();
151
    }
152
    
153
    public boolean isValid() {
154
        return valid;
155
    }
156
    
157
    public boolean isModified() {
158
        return modified;
159
    }
160
161
    public void markModified() throws java.io.IOException {
162
        if (cannotBeModified != null) {
163
            IOException e = new IOException ();
164
            org.openide.ErrorManager.getDefault ().annotate (e, cannotBeModified);
165
            throw e;
166
        }
167
        
168
        modified = true;
169
    }
170
    
171
    public void unmarkModified() {
172
        modified = false;
173
    }
174
175
    /** Implementation of the CES */
176
    private final class CES extends CloneableEditorSupport {
177
        
178
        public CES (Env env, org.openide.util.Lookup l) {
179
            super (env, l);
180
        }
181
        
182
        protected String messageName() {
183
            return "Name";
184
        }
185
        
186
        protected String messageOpened() {
187
            return "Opened";
188
        }
189
        
190
        protected String messageOpening() {
191
            return "Opening";
192
        }
193
        
194
        protected String messageSave() {
195
            return "Save";
196
        }
197
        
198
        protected String messageToolTip() {
199
            return "ToolTip";
200
        }        
201
        
202
    } // end of CES
203
}

Return to bug 42005