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 (+152 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 java.lang.reflect.InvocationTargetException;
34
import java.lang.reflect.Method;
35
import javax.swing.text.BadLocationException;
36
import javax.swing.undo.CompoundEdit;
37
import javax.swing.undo.UndoManager;
38
import javax.swing.undo.UndoableEdit;
39
import org.netbeans.junit.NbTestCase;
40
import org.openide.util.Exceptions;
41
42
/**
43
 * @author Jan Becicka
44
 */
45
public class IntegratedUndoTest extends NbTestCase  {
46
47
    private static final String TEXT = "test";
48
49
    public IntegratedUndoTest(String testName) {
50
        super(testName);
51
    }
52
53
    public void testUndoManager() throws Exception {
54
        final BaseDocument doc = new BaseDocument(BaseKit.class, false);
55
        UndoManagerImpl undoManager = new UndoManagerImpl();
56
        doc.addUndoableEditListener(undoManager);
57
        doc.putProperty(UndoManager.class, undoManager);
58
59
        final UndoableWrapper edit = new UndoableWrapper(doc);
60
        doc.runAtomic(new Runnable() {
61
62
            @Override
63
            public void run() {
64
                doc.addUndoableEdit(edit.first());
65
                try {
66
                    //edit 
67
                    doc.insertString(0, TEXT, null);
68
                } catch (BadLocationException ex) {
69
                    Exceptions.printStackTrace(ex);
70
                }
71
                doc.addUndoableEdit(edit.last());
72
            }
73
        });
74
        
75
        assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim());
76
        //undo is available
77
        //assertEquals(true, undoManager.canUndoOrRedo());
78
        assertEquals(true, undoManager.canUndo());
79
        
80
        //redo is not
81
        assertEquals(false, undoManager.canRedo());
82
        
83
        //last edit is mine
84
        assertEquals(edit.last(), getLast((CompoundEdit) undoManager.editToBeUndone()));
85
        
86
        assertEquals("MyUndo", undoManager.getUndoPresentationName());
87
        undoManager.undo();
88
        
89
        
90
        assertEquals("", org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim());
91
        //undo is not available
92
        //assertEquals(true, undoManager.canUndoOrRedo());
93
        assertEquals(false, undoManager.canUndo());
94
        
95
        //redo is available
96
        assertEquals(true, undoManager.canRedo());
97
        
98
        //now, there should not be any edit
99
        assertEquals(null, undoManager.editToBeUndone());
100
        assertEquals(edit.last(), getLast((CompoundEdit)undoManager.editToBeRedone()));
101
        
102
        assertEquals("MyRedo", undoManager.getUndoOrRedoPresentationName());
103
        undoManager.redo();
104
        
105
        assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim());
106
        //undo is available
107
        assertEquals(true, undoManager.canUndoOrRedo());
108
        assertEquals(true, undoManager.canUndo());
109
        
110
        //redo is not
111
        assertEquals(false, undoManager.canRedo());
112
        
113
        //last edit is mine
114
        assertEquals(edit.last(), getLast((CompoundEdit) undoManager.editToBeUndone()));
115
        assertEquals("MyUndo", undoManager.getUndoPresentationName());
116
        
117
    }
118
119
    private class UndoManagerImpl extends UndoManager {
120
121
        @Override
122
        public UndoableEdit editToBeRedone() {
123
            return super.editToBeRedone();
124
        }
125
126
        @Override
127
        public UndoableEdit editToBeUndone() {
128
            return super.editToBeUndone();
129
        }
130
    }
131
    
132
    UndoableEdit getLast(CompoundEdit orig) {
133
        try {
134
            Method m = CompoundEdit.class.getDeclaredMethod("lastEdit");
135
            m.setAccessible(true);
136
            Object edit = m.invoke(orig);
137
            return (UndoableEdit) edit;
138
        } catch (IllegalAccessException ex) {
139
            Exceptions.printStackTrace(ex);
140
        } catch (IllegalArgumentException ex) {
141
            Exceptions.printStackTrace(ex);
142
        } catch (InvocationTargetException ex) {
143
            Exceptions.printStackTrace(ex);
144
        } catch (NoSuchMethodException ex) {
145
            Exceptions.printStackTrace(ex);
146
        } catch (SecurityException ex) {
147
            Exceptions.printStackTrace(ex);
148
        }
149
        return null;
150
    }
151
}
152
    
(-)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