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 204828
Collapse All | Expand All

(-)a/editor.lib/nbproject/project.xml (+5 lines)
Lines 202-207 Link Here
202
                        <recursive/>
202
                        <recursive/>
203
                    </test-dependency>
203
                    </test-dependency>
204
                    <test-dependency>
204
                    <test-dependency>
205
                        <code-name-base>org.netbeans.modules.editor.mimelookup</code-name-base>
206
                        <compile-dependency/>
207
                        <test/>
208
                    </test-dependency>
209
                    <test-dependency>
205
                        <code-name-base>org.netbeans.modules.editor.settings.storage</code-name-base>
210
                        <code-name-base>org.netbeans.modules.editor.settings.storage</code-name-base>
206
                        <recursive/>
211
                        <recursive/>
207
                        <compile-dependency/>
212
                        <compile-dependency/>
(-)a/editor.lib/src/org/netbeans/editor/BaseDocument.java (-3 / +18 lines)
Lines 45-51 Link Here
45
package org.netbeans.editor;
45
package org.netbeans.editor;
46
46
47
import java.awt.Font;
47
import java.awt.Font;
48
import java.beans.PropertyVetoException;
49
import java.beans.VetoableChangeListener;
48
import java.beans.VetoableChangeListener;
50
import java.util.Hashtable;
49
import java.util.Hashtable;
51
import java.util.Dictionary;
50
import java.util.Dictionary;
Lines 56-61 Link Here
56
import java.beans.PropertyChangeListener;
55
import java.beans.PropertyChangeListener;
57
import java.beans.PropertyChangeEvent;
56
import java.beans.PropertyChangeEvent;
58
import java.beans.PropertyChangeSupport;
57
import java.beans.PropertyChangeSupport;
58
import java.util.Collection;
59
import java.util.EventListener;
59
import java.util.EventListener;
60
import java.util.HashSet;
60
import java.util.HashSet;
61
import java.util.Set;
61
import java.util.Set;
Lines 112-122 Link Here
112
import org.netbeans.modules.editor.lib2.document.ReadWriteBuffer;
112
import org.netbeans.modules.editor.lib2.document.ReadWriteBuffer;
113
import org.netbeans.modules.editor.lib2.document.ReadWriteUtils;
113
import org.netbeans.modules.editor.lib2.document.ReadWriteUtils;
114
import org.netbeans.modules.editor.lib2.document.StableCompoundEdit;
114
import org.netbeans.modules.editor.lib2.document.StableCompoundEdit;
115
import org.netbeans.spi.editor.document.UndoableEditWrapper;
115
import org.netbeans.spi.lexer.MutableTextInput;
116
import org.netbeans.spi.lexer.MutableTextInput;
116
import org.netbeans.spi.lexer.TokenHierarchyControl;
117
import org.netbeans.spi.lexer.TokenHierarchyControl;
117
import org.openide.filesystems.FileObject;
118
import org.openide.filesystems.FileObject;
118
import org.openide.util.RequestProcessor;
119
import org.openide.util.RequestProcessor.Task;
120
import org.openide.util.WeakListeners;
119
import org.openide.util.WeakListeners;
121
120
122
/**
121
/**
Lines 345-350 Link Here
345
    private CharSequence text;
344
    private CharSequence text;
346
    
345
    
347
    private UndoableEdit removeUpdateLineUndo;
346
    private UndoableEdit removeUpdateLineUndo;
347
    
348
    private Collection<? extends UndoableEditWrapper> undoEditWrappers;
348
349
349
    private DocumentFilter.FilterBypass filterBypass;
350
    private DocumentFilter.FilterBypass filterBypass;
350
351
Lines 564-569 Link Here
564
        findSupportChange(null); // update doc by find settings
565
        findSupportChange(null); // update doc by find settings
565
566
566
        TrailingWhitespaceRemove.install(this);
567
        TrailingWhitespaceRemove.install(this);
568
        
569
        undoEditWrappers = MimeLookup.getLookup(mimeType).lookupAll(UndoableEditWrapper.class);
567
570
568
        if (weakPrefsListener == null) {
571
        if (weakPrefsListener == null) {
569
            // the listening could have already been initialized from setMimeType(), which
572
            // the listening could have already been initialized from setMimeType(), which
Lines 1571-1576 Link Here
1571
    }
1574
    }
1572
1575
1573
    protected @Override void fireUndoableEditUpdate(UndoableEditEvent e) {
1576
    protected @Override void fireUndoableEditUpdate(UndoableEditEvent e) {
1577
        // Possibly wrap contained edit
1578
        if (undoEditWrappers != null) {
1579
            UndoableEdit origEdit = e.getEdit();
1580
            UndoableEdit edit = origEdit;
1581
            for (UndoableEditWrapper wrapper : undoEditWrappers) {
1582
                edit = wrapper.wrap(edit, this);
1583
            }
1584
            if (edit != origEdit) {
1585
                e = new UndoableEditEvent(this, edit);
1586
            }
1587
        }
1588
        
1574
	// Fire to the list of listeners that was used before the atomic lock started
1589
	// Fire to the list of listeners that was used before the atomic lock started
1575
        // This fixes issue #47881 and appears to be somewhat more logical
1590
        // This fixes issue #47881 and appears to be somewhat more logical
1576
        // than the default approach to fire all the current listeners
1591
        // than the default approach to fire all the current listeners
(-)a/editor.lib/test/unit/src/org/netbeans/editor/TestingUndoableEditWrapper.java (+72 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.editor;
43
44
import javax.swing.text.Document;
45
import javax.swing.undo.CompoundEdit;
46
import javax.swing.undo.UndoableEdit;
47
import org.netbeans.spi.editor.document.UndoableEditWrapper;
48
49
/**
50
 *
51
 * @author mmetelka
52
 */
53
//@MimeRegistration(mimeType="", service=UndoableEditWrapper.class)
54
public class TestingUndoableEditWrapper implements UndoableEditWrapper {
55
56
    @Override
57
    public UndoableEdit wrap(UndoableEdit edit, Document doc) {
58
        WrapCompoundEdit wrapEdit = new WrapCompoundEdit();
59
        wrapEdit.addEdit(edit);
60
        wrapEdit.end();
61
        return wrapEdit;
62
    }
63
    
64
    static final class WrapCompoundEdit extends CompoundEdit {
65
        
66
        WrapCompoundEdit() {
67
        }
68
        
69
    }
70
71
    
72
}
(-)a/editor.lib/test/unit/src/org/netbeans/editor/UndoableEditWrapperTest.java (+80 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * Contributor(s):
28
 *
29
 * The Original Software is NetBeans. The Initial Developer of the Original
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
33
 * If you wish your version of this file to be governed by only the CDDL
34
 * or only the GPL Version 2, indicate your decision by adding
35
 * "[Contributor] elects to include this software in this distribution
36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37
 * single choice of license, a recipient has the option to distribute
38
 * your version of this file under either the CDDL, the GPL Version 2 or
39
 * to extend the choice of license to its licensees as provided above.
40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
41
 * Version 2 license, then the option applies only if the new code is
42
 * made subject to such option by the copyright holder.
43
 */
44
45
package org.netbeans.editor;
46
47
import javax.swing.event.UndoableEditEvent;
48
import javax.swing.event.UndoableEditListener;
49
import javax.swing.undo.UndoableEdit;
50
import org.netbeans.api.editor.mimelookup.MimePath;
51
import org.netbeans.api.editor.mimelookup.test.MockMimeLookup;
52
import org.netbeans.junit.NbTestCase;
53
54
/**
55
 *
56
 * @author Miloslav Metelka
57
 */
58
public class UndoableEditWrapperTest extends NbTestCase {
59
    
60
    /** Creates a new instance of ZOrderTest */
61
    public UndoableEditWrapperTest(String name) {
62
        super(name);
63
    }
64
    
65
    public void testWrapping() throws Exception {
66
        MimePath mimePath = MimePath.EMPTY;
67
        MockMimeLookup.setInstances(mimePath, new TestingUndoableEditWrapper());
68
        BaseDocument bDoc = new BaseDocument(false, "");
69
        bDoc.addUndoableEditListener(new UndoableEditListener() {
70
            @Override
71
            public void undoableEditHappened(UndoableEditEvent e) {
72
                UndoableEdit edit = e.getEdit();
73
                assertEquals("Expected WrapCompoundEdit.class",
74
                        TestingUndoableEditWrapper.WrapCompoundEdit.class, edit.getClass());
75
            }
76
        });
77
        bDoc.insertString(0, "Test", null);
78
    }
79
80
}
(-)a/editor.lib2/manifest.mf (-1 / +1 lines)
Lines 1-6 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.editor.lib2/1
2
OpenIDE-Module: org.netbeans.modules.editor.lib2/1
3
OpenIDE-Module-Implementation-Version: 26
3
OpenIDE-Module-Implementation-Version: 27
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/lib2/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/lib2/Bundle.properties
5
OpenIDE-Module-Layer: org/netbeans/modules/editor/lib2/resources/layer.xml
5
OpenIDE-Module-Layer: org/netbeans/modules/editor/lib2/resources/layer.xml
6
OpenIDE-Module-Needs: org.netbeans.modules.editor.actions
6
OpenIDE-Module-Needs: org.netbeans.modules.editor.actions
(-)a/editor.lib2/nbproject/project.properties (-1 / +1 lines)
Lines 43-49 Link Here
43
is.autoload=true
43
is.autoload=true
44
javac.source=1.6
44
javac.source=1.6
45
javac.compilerargs=-Xlint:unchecked
45
javac.compilerargs=-Xlint:unchecked
46
spec.version.base=1.55.0
46
spec.version.base=1.56.0
47
47
48
javadoc.arch=${basedir}/arch.xml
48
javadoc.arch=${basedir}/arch.xml
49
javadoc.apichanges=${basedir}/apichanges.xml
49
javadoc.apichanges=${basedir}/apichanges.xml
(-)a/editor.lib2/nbproject/project.xml (+1 lines)
Lines 188-193 Link Here
188
            <public-packages>
188
            <public-packages>
189
                <package>org.netbeans.api.editor</package>
189
                <package>org.netbeans.api.editor</package>
190
                <package>org.netbeans.spi.editor.codegen</package>
190
                <package>org.netbeans.spi.editor.codegen</package>
191
                <package>org.netbeans.spi.editor.document</package>
191
                <package>org.netbeans.spi.editor.highlighting</package>
192
                <package>org.netbeans.spi.editor.highlighting</package>
192
                <package>org.netbeans.spi.editor.highlighting.support</package>
193
                <package>org.netbeans.spi.editor.highlighting.support</package>
193
                <package>org.netbeans.spi.editor.typinghooks</package>
194
                <package>org.netbeans.spi.editor.typinghooks</package>
(-)a/editor.lib2/src/org/netbeans/spi/editor/document/UndoableEditWrapper.java (+68 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.spi.editor.document;
43
44
import javax.swing.text.Document;
45
import javax.swing.undo.UndoableEdit;
46
import org.netbeans.api.annotations.common.NonNull;
47
48
/**
49
 * Wrap undoable edits generated by document implementation into a custom undoable edit.
50
 * <br/>
51
 * Instances should be registered by using @MimeRegistration.
52
 *
53
 * @author Miloslav Metelka
54
 */
55
public interface UndoableEditWrapper {
56
    
57
    /**
58
     * Wrap given undoable edit by a custom undoable edit implementation
59
     * (or leave it as it is).
60
     *
61
     * @param edit original undoable edit generated by document (or previous wrapper).
62
     * @param doc document which generated the original undoable edit.
63
     * @return wrap edit or original edit.
64
     * @since 1.56
65
     */
66
    @NonNull UndoableEdit wrap(@NonNull UndoableEdit edit, @NonNull Document doc);
67
    
68
}
(-)a/openide.text/src/org/openide/text/NbDocument.java (+25 lines)
Lines 51-56 Link Here
51
import javax.swing.JToolBar;
51
import javax.swing.JToolBar;
52
import javax.swing.SwingUtilities;
52
import javax.swing.SwingUtilities;
53
import javax.swing.text.*;
53
import javax.swing.text.*;
54
import javax.swing.undo.UndoableEdit;
55
import org.openide.awt.UndoRedo;
54
import org.openide.cookies.EditorCookie;
56
import org.openide.cookies.EditorCookie;
55
57
56
58
Lines 519-524 Link Here
519
        ((Annotatable) doc).removeAnnotation(annotation);
521
        ((Annotatable) doc).removeAnnotation(annotation);
520
    }
522
    }
521
523
524
    public static <T> T getEditToBeUndoneOfType(EditorCookie ec, Class<T> type) {
525
        return getEditToBeUndoneRedoneOfType(ec, type, false);
526
    }
527
    
528
    public static <T> T getEditToBeRedoneOfType(EditorCookie ec, Class<T> type) {
529
        return getEditToBeUndoneRedoneOfType(ec, type, true);
530
    }
531
    
532
    private static <T> T getEditToBeUndoneRedoneOfType(EditorCookie ec, Class<T> type, boolean redone) {
533
        UndoRedo ur;
534
        if (ec instanceof CloneableEditorSupport &&
535
                ((ur = ((CloneableEditorSupport)ec).getUndoRedo()) instanceof UndoRedoManager))
536
        {
537
            UndoRedoManager urManager = (UndoRedoManager) ur;
538
            UndoableEdit edit = urManager.editToBeUndoneRedone(redone);
539
            if (type.isInstance(edit)) {
540
                @SuppressWarnings("unchecked") T inst = (T) edit;
541
                return inst;
542
            }
543
        }
544
        return null;
545
    }
546
522
    /** Specialized version of document that knows how to lock the document
547
    /** Specialized version of document that knows how to lock the document
523
    * for complex modifications.
548
    * for complex modifications.
524
    */
549
    */
(-)a/openide.text/src/org/openide/text/UndoRedoManager.java (+5 lines)
Lines 564-569 Link Here
564
        undoGroup = null;
564
        undoGroup = null;
565
    }
565
    }
566
566
567
    UndoableEdit editToBeUndoneRedone(boolean redone) { // Access for NbDocument
568
        WrapUndoEdit wrapEdit = (WrapUndoEdit) (redone ? editToBeRedone() : editToBeUndone());
569
        return wrapEdit.delegate();
570
    }
571
567
    static String editToString(UndoableEdit edit) {
572
    static String editToString(UndoableEdit edit) {
568
        if (edit instanceof WrapUndoEdit) {
573
        if (edit instanceof WrapUndoEdit) {
569
            return toStringTerse(edit) + "->" + toStringTerse(((WrapUndoEdit)edit).delegate()); // NOI18N
574
            return toStringTerse(edit) + "->" + toStringTerse(((WrapUndoEdit)edit).delegate()); // NOI18N
(-)a/openide.text/test/unit/src/org/openide/text/EditToBeUndoneRedoneTest.java (+285 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * Contributor(s):
28
 *
29
 * The Original Software is NetBeans. The Initial Developer of the Original
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
33
 * If you wish your version of this file to be governed by only the CDDL
34
 * or only the GPL Version 2, indicate your decision by adding
35
 * "[Contributor] elects to include this software in this distribution
36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37
 * single choice of license, a recipient has the option to distribute
38
 * your version of this file under either the CDDL, the GPL Version 2 or
39
 * to extend the choice of license to its licensees as provided above.
40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
41
 * Version 2 license, then the option applies only if the new code is
42
 * made subject to such option by the copyright holder.
43
 */
44
45
package org.openide.text;
46
47
import java.beans.PropertyChangeListener;
48
import java.beans.PropertyChangeSupport;
49
import java.beans.VetoableChangeListener;
50
import java.io.ByteArrayInputStream;
51
import java.io.ByteArrayOutputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54
import java.io.OutputStream;
55
import java.io.Serializable;
56
import java.util.Date;
57
import javax.swing.event.UndoableEditEvent;
58
import javax.swing.text.AttributeSet;
59
import javax.swing.text.BadLocationException;
60
import javax.swing.text.Document;
61
import javax.swing.text.EditorKit;
62
import javax.swing.text.Position;
63
import javax.swing.text.StyledDocument;
64
import javax.swing.undo.AbstractUndoableEdit;
65
import javax.swing.undo.CannotRedoException;
66
import javax.swing.undo.CannotUndoException;
67
import javax.swing.undo.CompoundEdit;
68
import javax.swing.undo.UndoableEdit;
69
import org.netbeans.junit.NbTestCase;
70
import org.openide.awt.UndoRedo;
71
import org.openide.cookies.EditorCookie;
72
import org.openide.text.CloneableEditorSupport.Env;
73
import org.openide.util.Exceptions;
74
import org.openide.util.Lookup;
75
import org.openide.util.RequestProcessor;
76
import org.openide.util.RequestProcessor.Task;
77
import org.openide.windows.CloneableOpenSupport;
78
import org.openide.windows.CloneableTopComponent;
79
80
/**
81
 * Deadlock of a thread simulating reloading of document and another thread trying to close the file.
82
 * 
83
 * @author Miloslav Metelka
84
 */
85
public class EditToBeUndoneRedoneTest extends NbTestCase
86
implements CloneableEditorSupport.Env {
87
    static {
88
        System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false");
89
    }
90
    /** the support to work with */
91
    private transient CES support;
92
93
    // Env variables
94
    private transient String content = "";
95
    private transient boolean valid = true;
96
    private transient boolean modified = false;
97
    /** if not null contains message why this document cannot be modified */
98
    private transient String cannotBeModified;
99
    private transient Date date = new Date ();
100
    private transient final PropertyChangeSupport pcl;
101
    private transient VetoableChangeListener vetoL;
102
    
103
    private transient volatile boolean inReloadBeforeSupportLock;
104
    private transient volatile boolean closing;
105
    
106
    private static EditToBeUndoneRedoneTest RUNNING;
107
    
108
    public EditToBeUndoneRedoneTest(String s) {
109
        super(s);
110
        pcl = new PropertyChangeSupport(this);
111
    }
112
    
113
    protected void setUp () {
114
        support = new CES (this, Lookup.EMPTY);
115
        RUNNING = this;
116
    }
117
    
118
    protected boolean runInEQ() {
119
        return false;
120
    }
121
122
    @Override
123
    protected int timeOut() {
124
        return 15000;
125
    }
126
    
127
    private Object writeReplace () {
128
        return new Replace ();
129
    }
130
    
131
    public void testUndoRedoEdits() throws Exception {
132
        Document doc = support.openDocument();
133
        doc.insertString(0, "a", null);
134
        UndoRedo.Manager ur = support.getUndoRedo();
135
        MyEdit myEdit = NbDocument.getEditToBeUndoneOfType(support, MyEdit.class);
136
        assertNotNull("Expected valid myEdit", myEdit);
137
        ur.undo();
138
        myEdit = NbDocument.getEditToBeRedoneOfType(support, MyEdit.class);
139
        assertNotNull("Expected valid myEdit", myEdit);
140
    }
141
    
142
    //
143
    // Implementation of the CloneableEditorSupport.Env
144
    //
145
    
146
    public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
147
        pcl.addPropertyChangeListener(l);
148
    }    
149
    public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
150
        pcl.removePropertyChangeListener(l);
151
    }
152
    
153
    public synchronized void addVetoableChangeListener(VetoableChangeListener l) {
154
        assertNull ("This is the first veto listener", vetoL);
155
        vetoL = l;
156
    }
157
    public void removeVetoableChangeListener(VetoableChangeListener l) {
158
        assertEquals ("Removing the right veto one", vetoL, l);
159
        vetoL = null;
160
    }
161
    
162
    public CloneableOpenSupport findCloneableOpenSupport() {
163
        return RUNNING.support;
164
    }
165
    
166
    public String getMimeType() {
167
        return "text/plain";
168
    }
169
    
170
    public Date getTime() {
171
        return date;
172
    }
173
    
174
    public InputStream inputStream() throws IOException {
175
        return new ByteArrayInputStream (content.getBytes ());
176
    }
177
    public OutputStream outputStream() throws IOException {
178
        class ContentStream extends ByteArrayOutputStream {
179
            public void close () throws IOException {
180
                super.close ();
181
                content = new String (toByteArray ());
182
            }
183
        }
184
        
185
        return new ContentStream ();
186
    }
187
    
188
    public boolean isValid() {
189
        return valid;
190
    }
191
    
192
    public boolean isModified() {
193
        return modified;
194
    }
195
196
    public void markModified() throws IOException {
197
        if (cannotBeModified != null) {
198
            final String notify = cannotBeModified;
199
            IOException e = new IOException () {
200
                public String getLocalizedMessage () {
201
                    return notify;
202
                }
203
            };
204
            Exceptions.attachLocalizedMessage(e, cannotBeModified);
205
            throw e;
206
        }
207
        
208
        modified = true;
209
    }
210
    
211
    public void unmarkModified() {
212
        modified = false;
213
    }
214
    
215
    /** Implementation of the CES */
216
    private static final class CES extends CloneableEditorSupport implements EditorCookie {
217
        public CES (Env env, Lookup l) {
218
            super (env, l);
219
        }
220
        
221
        @Override
222
        protected EditorKit createEditorKit () {
223
            // Important to use NbLikeEditorKit since otherwise FilterDocument
224
            // would be created with improper runAtomic()
225
            return new MyKit ();
226
        }
227
        public CloneableTopComponent.Ref getRef () {
228
            return allEditors;
229
        }
230
        
231
        protected String messageName() {
232
            return "Name";
233
        }
234
        
235
        protected String messageOpened() {
236
            return "Opened";
237
        }
238
        
239
        protected String messageOpening() {
240
            return "Opening";
241
        }
242
        
243
        protected String messageSave() {
244
            return "Save";
245
        }
246
        
247
        protected String messageToolTip() {
248
            return "ToolTip";
249
        }
250
        
251
    }
252
253
    private static final class Replace implements Serializable {
254
        public Object readResolve () {
255
            return RUNNING;
256
        }
257
    }
258
259
    private static final class MyEdit extends CompoundEdit { // Marker custom undo edit
260
        
261
    }
262
    
263
    private static final class MyKit extends NbLikeEditorKit {
264
265
        @Override
266
        public Document createDefaultDocument() {
267
            return new Doc() {
268
269
                @Override
270
                protected void fireUndoableEditUpdate(UndoableEditEvent e) {
271
                    UndoableEdit edit = e.getEdit();
272
                    MyEdit wrapEdit = new MyEdit();
273
                    wrapEdit.addEdit(edit);
274
                    wrapEdit.end();
275
                    e = new UndoableEditEvent(e.getSource(), wrapEdit);
276
277
                    super.fireUndoableEditUpdate(e);
278
                }
279
280
            };
281
        }
282
        
283
        
284
    }
285
}

Return to bug 204828