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

(-)editor.lib/test/unit/src/org/netbeans/editor/IntegratedUndoTest.java (+129 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
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
30
 */
31
package org.netbeans.editor;
32
33
import javax.swing.text.BadLocationException;
34
import javax.swing.undo.UndoManager;
35
import javax.swing.undo.UndoableEdit;
36
import org.netbeans.junit.NbTestCase;
37
import org.openide.util.Exceptions;
38
39
/**
40
 * @author Jan Becicka
41
 */
42
public class IntegratedUndoTest extends NbTestCase  {
43
44
    private static final String TEXT = "test";
45
46
    public IntegratedUndoTest(String testName) {
47
        super(testName);
48
    }
49
50
    public void testUndoManager() throws Exception {
51
        final BaseDocument doc = new BaseDocument(BaseKit.class, false);
52
        UndoManagerImpl undoManager = new UndoManagerImpl();
53
        doc.addUndoableEditListener(undoManager);
54
        doc.putProperty(UndoManager.class, undoManager);
55
56
        final UndoableWrapper edit = new UndoableWrapper(doc);
57
        doc.runAtomic(new Runnable() {
58
59
            @Override
60
            public void run() {
61
                doc.addUndoableEdit(edit.first());
62
                try {
63
                    //edit 
64
                    doc.insertString(0, TEXT, null);
65
                } catch (BadLocationException ex) {
66
                    Exceptions.printStackTrace(ex);
67
                }
68
                doc.addUndoableEdit(edit.last());
69
            }
70
        });
71
        
72
        assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim());
73
        //undo is available
74
        //assertEquals(true, undoManager.canUndoOrRedo());
75
        assertEquals(true, undoManager.canUndo());
76
        
77
        //redo is not
78
        assertEquals(false, undoManager.canRedo());
79
        
80
        //last edit is mine
81
        assertEquals(edit.last(), undoManager.editToBeUndone());
82
        
83
        assertEquals("MyUndo", undoManager.getUndoPresentationName());
84
        undoManager.undo();
85
        
86
        
87
        assertEquals("", org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim());
88
        //undo is not available
89
        //assertEquals(true, undoManager.canUndoOrRedo());
90
        assertEquals(false, undoManager.canUndo());
91
        
92
        //redo is available
93
        assertEquals(true, undoManager.canRedo());
94
        
95
        //now, there should not be any edit
96
        assertEquals(null, undoManager.editToBeUndone());
97
        assertEquals(edit.first(), undoManager.editToBeRedone());
98
        
99
        assertEquals("MyRedo", undoManager.getUndoOrRedoPresentationName());
100
        undoManager.redo();
101
        
102
        assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim());
103
        //undo is available
104
        assertEquals(true, undoManager.canUndoOrRedo());
105
        assertEquals(true, undoManager.canUndo());
106
        
107
        //redo is not
108
        assertEquals(false, undoManager.canRedo());
109
        
110
        //last edit is mine
111
        assertEquals(edit.last(), undoManager.editToBeUndone());
112
        assertEquals("MyUndo", undoManager.getUndoPresentationName());
113
        
114
    }
115
116
    private class UndoManagerImpl extends UndoManager {
117
118
        @Override
119
        public UndoableEdit editToBeRedone() {
120
            return super.editToBeRedone();
121
        }
122
123
        @Override
124
        public UndoableEdit editToBeUndone() {
125
            return super.editToBeUndone();
126
        }
127
    }
128
}
129
    
(-)editor.lib/test/unit/src/org/netbeans/editor/UndoableWrapper.java (+139 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.editor;
43
44
import javax.swing.text.BadLocationException;
45
import javax.swing.undo.CannotRedoException;
46
import javax.swing.undo.CannotUndoException;
47
import javax.swing.undo.UndoableEdit;
48
import org.openide.util.Exceptions;
49
50
/**
51
 *
52
 * @author Jan Becicka
53
 */
54
public class UndoableWrapper {
55
56
    private final BaseDocument doc;
57
    private UndoableEdit last;
58
    private UndoableEdit first;
59
60
    public UndoableWrapper(BaseDocument doc) {
61
        this.doc = doc;
62
    }
63
    
64
    public UndoableEdit first() {
65
        if (first==null) {
66
            first = new UndoableEditImpl();
67
        }
68
        return first;
69
    }
70
    
71
    public UndoableEdit last() {
72
        if (last==null) {
73
            last = new UndoableEditImpl();
74
        }
75
        return last;
76
    }
77
78
    class UndoableEditImpl implements UndoableEdit {
79
80
        private boolean canUndo = true;
81
        private boolean canRedo = false;
82
83
        @Override
84
        public void undo() throws CannotUndoException {
85
            canUndo = false;
86
            canRedo = true;
87
        }
88
89
        @Override
90
        public boolean canUndo() {
91
            return canUndo;
92
        }
93
94
        @Override
95
        public void redo() throws CannotRedoException {
96
            canUndo = true;
97
            canRedo = false;
98
        }
99
100
        @Override
101
        public boolean canRedo() {
102
            return canRedo;
103
        }
104
105
        @Override
106
        public void die() {
107
        }
108
109
        @Override
110
        public boolean addEdit(UndoableEdit ue) {
111
            return false;
112
        }
113
114
        @Override
115
        public boolean replaceEdit(UndoableEdit ue) {
116
            return false;
117
        }
118
119
        @Override
120
        public boolean isSignificant() {
121
            return true;
122
        }
123
124
        @Override
125
        public String getPresentationName() {
126
            return "MyUndoManager";
127
        }
128
129
        @Override
130
        public String getUndoPresentationName() {
131
            return "MyUndo";
132
        }
133
134
        @Override
135
        public String getRedoPresentationName() {
136
            return "MyRedo";
137
        }
138
    }
139
}

Return to bug 204828