# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /Users/beci/source/jet-main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: editor.lib/test/unit/src/org/netbeans/editor/IntegratedUndoTest.java --- editor.lib/test/unit/src/org/netbeans/editor/IntegratedUndoTest.java Base (BASE) +++ editor.lib/test/unit/src/org/netbeans/editor/IntegratedUndoTest.java Locally New @@ -0,0 +1,152 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2007 Sun Microsystems, Inc. + */ +package org.netbeans.editor; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import javax.swing.text.BadLocationException; +import javax.swing.undo.CompoundEdit; +import javax.swing.undo.UndoManager; +import javax.swing.undo.UndoableEdit; +import org.netbeans.junit.NbTestCase; +import org.openide.util.Exceptions; + +/** + * @author Jan Becicka + */ +public class IntegratedUndoTest extends NbTestCase { + + private static final String TEXT = "test"; + + public IntegratedUndoTest(String testName) { + super(testName); + } + + public void testUndoManager() throws Exception { + final BaseDocument doc = new BaseDocument(BaseKit.class, false); + UndoManagerImpl undoManager = new UndoManagerImpl(); + doc.addUndoableEditListener(undoManager); + doc.putProperty(UndoManager.class, undoManager); + + final UndoableWrapper edit = new UndoableWrapper(doc); + doc.runAtomic(new Runnable() { + + @Override + public void run() { + doc.addUndoableEdit(edit.first()); + try { + //edit + doc.insertString(0, TEXT, null); + } catch (BadLocationException ex) { + Exceptions.printStackTrace(ex); + } + doc.addUndoableEdit(edit.last()); + } + }); + + assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim()); + //undo is available + //assertEquals(true, undoManager.canUndoOrRedo()); + assertEquals(true, undoManager.canUndo()); + + //redo is not + assertEquals(false, undoManager.canRedo()); + + //last edit is mine + assertEquals(edit.last(), getLast((CompoundEdit) undoManager.editToBeUndone())); + + assertEquals("MyUndo", undoManager.getUndoPresentationName()); + undoManager.undo(); + + + assertEquals("", org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim()); + //undo is not available + //assertEquals(true, undoManager.canUndoOrRedo()); + assertEquals(false, undoManager.canUndo()); + + //redo is available + assertEquals(true, undoManager.canRedo()); + + //now, there should not be any edit + assertEquals(null, undoManager.editToBeUndone()); + assertEquals(edit.last(), getLast((CompoundEdit)undoManager.editToBeRedone())); + + assertEquals("MyRedo", undoManager.getUndoOrRedoPresentationName()); + undoManager.redo(); + + assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim()); + //undo is available + assertEquals(true, undoManager.canUndoOrRedo()); + assertEquals(true, undoManager.canUndo()); + + //redo is not + assertEquals(false, undoManager.canRedo()); + + //last edit is mine + assertEquals(edit.last(), getLast((CompoundEdit) undoManager.editToBeUndone())); + assertEquals("MyUndo", undoManager.getUndoPresentationName()); + + } + + private class UndoManagerImpl extends UndoManager { + + @Override + public UndoableEdit editToBeRedone() { + return super.editToBeRedone(); + } + + @Override + public UndoableEdit editToBeUndone() { + return super.editToBeUndone(); + } + } + + UndoableEdit getLast(CompoundEdit orig) { + try { + Method m = CompoundEdit.class.getDeclaredMethod("lastEdit"); + m.setAccessible(true); + Object edit = m.invoke(orig); + return (UndoableEdit) edit; + } catch (IllegalAccessException ex) { + Exceptions.printStackTrace(ex); + } catch (IllegalArgumentException ex) { + Exceptions.printStackTrace(ex); + } catch (InvocationTargetException ex) { + Exceptions.printStackTrace(ex); + } catch (NoSuchMethodException ex) { + Exceptions.printStackTrace(ex); + } catch (SecurityException ex) { + Exceptions.printStackTrace(ex); + } + return null; + } +} + \ No newline at end of file Index: editor.lib/test/unit/src/org/netbeans/editor/UndoableWrapper.java --- editor.lib/test/unit/src/org/netbeans/editor/UndoableWrapper.java Base (BASE) +++ editor.lib/test/unit/src/org/netbeans/editor/UndoableWrapper.java Locally New @@ -0,0 +1,139 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.editor; + +import javax.swing.text.BadLocationException; +import javax.swing.undo.CannotRedoException; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoableEdit; +import org.openide.util.Exceptions; + +/** + * + * @author Jan Becicka + */ +public class UndoableWrapper { + + private final BaseDocument doc; + private UndoableEdit last; + private UndoableEdit first; + + public UndoableWrapper(BaseDocument doc) { + this.doc = doc; + } + + public UndoableEdit first() { + if (first==null) { + first = new UndoableEditImpl(); + } + return first; + } + + public UndoableEdit last() { + if (last==null) { + last = new UndoableEditImpl(); + } + return last; + } + + class UndoableEditImpl implements UndoableEdit { + + private boolean canUndo = true; + private boolean canRedo = false; + + @Override + public void undo() throws CannotUndoException { + canUndo = false; + canRedo = true; + } + + @Override + public boolean canUndo() { + return canUndo; + } + + @Override + public void redo() throws CannotRedoException { + canUndo = true; + canRedo = false; + } + + @Override + public boolean canRedo() { + return canRedo; + } + + @Override + public void die() { + } + + @Override + public boolean addEdit(UndoableEdit ue) { + return false; + } + + @Override + public boolean replaceEdit(UndoableEdit ue) { + return false; + } + + @Override + public boolean isSignificant() { + return true; + } + + @Override + public String getPresentationName() { + return "MyUndoManager"; + } + + @Override + public String getUndoPresentationName() { + return "MyUndo"; + } + + @Override + public String getRedoPresentationName() { + return "MyRedo"; + } + } +}