KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > UndoRedoWrappingExampleTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.openide.text;
22
23
24 import java.lang.reflect.*;
25 import javax.swing.event.UndoableEditEvent JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27 import javax.swing.undo.CompoundEdit JavaDoc;
28
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31
32 import org.openide.awt.UndoRedo;
33 import org.openide.util.RequestProcessor;
34
35
36 /** Example of an edit that encapsulates more edits into one.
37  *
38  * @author Jaroslav Tulach
39  */

40 public class UndoRedoWrappingExampleTest extends NbTestCase implements CloneableEditorSupport.Env {
41     /** the method of manager that we are testing */
42     private Method method;
43     
44     private CES support;
45     // Env variables
46
private String JavaDoc content = "";
47     private boolean valid = true;
48     private boolean modified = false;
49     private java.util.Date JavaDoc date = new java.util.Date JavaDoc ();
50     private java.util.List JavaDoc/*<java.beans.PropertyChangeListener>*/ propL = new java.util.ArrayList JavaDoc ();
51     private java.beans.VetoableChangeListener JavaDoc vetoL;
52     
53     
54     /** Creates new UndoRedoTest */
55     public UndoRedoWrappingExampleTest(String JavaDoc s) {
56         super(s);
57     }
58
59     protected void setUp () {
60         support = new CES (this, org.openide.util.Lookup.EMPTY);
61     }
62     
63     public void testDeleteEachTenthCharFromDocument() throws Exception JavaDoc {
64         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
65         for (int i = 0; i < 20; i++) {
66             for (int j = 0; j < 10; j++) {
67                 sb.append((char)('0' + j));
68             }
69         }
70         
71         content = sb.toString();
72         
73         StyledDocument JavaDoc doc = support.openDocument();
74         assertEquals("200 chars there", 200, doc.getLength());
75         
76         CompoundEdit JavaDoc bigEdit = new CompoundEdit JavaDoc();
77         support.getUndoRedo().undoableEditHappened(new UndoableEditEvent JavaDoc(doc, bigEdit));
78         
79         assertTrue("Big edit will consume other edits", bigEdit.isInProgress());
80         
81         for (int i = 199; i >= 0; i -= 10) {
82             doc.remove(i, 1);
83         }
84         
85         assertTrue("Big edit was still in consume mode", bigEdit.isInProgress());
86         bigEdit.end();
87         assertFalse("Big edit is over", bigEdit.isInProgress());
88         assertTrue("Document is modified", modified);
89         assertTrue("We can undo", support.getUndoRedo().canUndo());
90         
91         if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
92             fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
93         }
94         
95         support.getUndoRedo().undo();
96         
97         assertEquals("Again 200", 200, doc.getLength());
98         assertFalse("Not modified anymore", modified);
99
100         
101         assertTrue("We can redo", support.getUndoRedo().canRedo());
102         support.getUndoRedo().redo();
103         
104         assertTrue("Document is modified", modified);
105         assertTrue("We can undo", support.getUndoRedo().canUndo());
106         
107         if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
108             fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
109         }
110         
111     }
112
113     public void testDeleteEachTenthCharOnModifiedDocument() throws Exception JavaDoc {
114         
115         StyledDocument JavaDoc doc = support.openDocument();
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117         for (int i = 0; i < 20; i++) {
118             for (int j = 0; j < 10; j++) {
119                 sb.append((char)('0' + j));
120             }
121         }
122         assertEquals("empty", 0, doc.getLength());
123         doc.insertString(0, sb.toString(), null);
124         assertEquals("200 chars there", 200, doc.getLength());
125         
126         CompoundEdit JavaDoc bigEdit = new CompoundEdit JavaDoc();
127         support.getUndoRedo().undoableEditHappened(new UndoableEditEvent JavaDoc(doc, bigEdit));
128         
129         assertTrue("Big edit will consume other edits", bigEdit.isInProgress());
130         
131         for (int i = 199; i >= 0; i -= 10) {
132             doc.remove(i, 1);
133         }
134         
135         assertTrue("Big edit was still in consume mode", bigEdit.isInProgress());
136         bigEdit.end();
137         assertFalse("Big edit is over", bigEdit.isInProgress());
138         assertTrue("Document is modified", modified);
139         assertTrue("We can undo", support.getUndoRedo().canUndo());
140         
141         if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
142             fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
143         }
144         
145         support.getUndoRedo().undo();
146         
147         assertEquals("Again 200", 200, doc.getLength());
148         assertTrue("Still modified", modified);
149
150         
151         assertTrue("We can redo", support.getUndoRedo().canRedo());
152         support.getUndoRedo().redo();
153         
154         assertTrue("Document is modified", modified);
155         assertTrue("We can undo", support.getUndoRedo().canUndo());
156         
157         if (doc.getText(0, doc.getLength()).indexOf('9') != -1) {
158             fail("There should be no 9 in the doc:\n" + doc.getText(0, doc.getLength()));
159         }
160         
161     }
162     
163     //
164
// Implementation of the CloneableEditorSupport.Env
165
//
166

167     public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
168         propL.add (l);
169     }
170     public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
171         propL.remove (l);
172     }
173     
174     public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
175         assertNull ("This is the first veto listener", vetoL);
176         vetoL = l;
177     }
178     public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc l) {
179         assertEquals ("Removing the right veto one", vetoL, l);
180         vetoL = null;
181     }
182     
183     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
184         return support;
185     }
186     
187     public String JavaDoc getMimeType() {
188         return "text/plain";
189     }
190     
191     public java.util.Date JavaDoc getTime() {
192         return date;
193     }
194     
195     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
196         return new java.io.ByteArrayInputStream JavaDoc (content.getBytes ());
197     }
198     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
199         class ContentStream extends java.io.ByteArrayOutputStream JavaDoc {
200             public void close () throws java.io.IOException JavaDoc {
201                 super.close ();
202                 content = new String JavaDoc (toByteArray ());
203             }
204         }
205         
206         return new ContentStream ();
207     }
208     
209     public boolean isValid() {
210         return valid;
211     }
212     
213     public boolean isModified() {
214         return modified;
215     }
216
217     public void markModified() throws java.io.IOException JavaDoc {
218         modified = true;
219     }
220     
221     public void unmarkModified() {
222         modified = false;
223     }
224
225     /** Implementation of the CES */
226     private final class CES extends CloneableEditorSupport {
227         public boolean plain;
228         
229         
230         public CES (Env env, org.openide.util.Lookup l) {
231             super (env, l);
232         }
233         
234         protected String JavaDoc messageName() {
235             return "Name";
236         }
237         
238         protected String JavaDoc messageOpened() {
239             return "Opened";
240         }
241         
242         protected String JavaDoc messageOpening() {
243             return "Opening";
244         }
245         
246         protected String JavaDoc messageSave() {
247             return "Save";
248         }
249         
250         protected String JavaDoc messageToolTip() {
251             return "ToolTip";
252         }
253
254         protected javax.swing.text.EditorKit JavaDoc createEditorKit() {
255             if (plain) {
256                 return super.createEditorKit ();
257             } else {
258                 return new NbLikeEditorKit ();
259             }
260         }
261     } // end of CES
262

263     private static final class FakeEdit implements javax.swing.undo.UndoableEdit JavaDoc {
264         public boolean addEdit(javax.swing.undo.UndoableEdit JavaDoc anEdit) {
265             return false;
266         }
267
268         public boolean canRedo() {
269             return true;
270         }
271
272         public boolean canUndo() {
273             return true;
274         }
275
276         public void die() {
277         }
278
279         public java.lang.String JavaDoc getPresentationName() {
280             return "";
281         }
282
283         public java.lang.String JavaDoc getRedoPresentationName() {
284             return "";
285         }
286
287         public java.lang.String JavaDoc getUndoPresentationName() {
288             return "";
289         }
290
291         public boolean isSignificant() {
292             return false;
293         }
294
295         public void redo() throws javax.swing.undo.CannotRedoException JavaDoc {
296         }
297
298         public boolean replaceEdit(javax.swing.undo.UndoableEdit JavaDoc anEdit) {
299             return true;
300         }
301
302         public void undo() throws javax.swing.undo.CannotUndoException JavaDoc {
303         }
304         
305     } // end of UndoableEdit
306
}
307
Popular Tags