1 19 20 21 package org.openide.text; 22 23 24 import java.lang.reflect.*; 25 import javax.swing.event.UndoableEditEvent ; 26 import javax.swing.text.StyledDocument ; 27 import javax.swing.undo.CompoundEdit ; 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 40 public class UndoRedoWrappingExampleTest extends NbTestCase implements CloneableEditorSupport.Env { 41 42 private Method method; 43 44 private CES support; 45 private String content = ""; 47 private boolean valid = true; 48 private boolean modified = false; 49 private java.util.Date date = new java.util.Date (); 50 private java.util.List propL = new java.util.ArrayList (); 51 private java.beans.VetoableChangeListener vetoL; 52 53 54 55 public UndoRedoWrappingExampleTest(String 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 { 64 StringBuffer sb = new StringBuffer (); 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 doc = support.openDocument(); 74 assertEquals("200 chars there", 200, doc.getLength()); 75 76 CompoundEdit bigEdit = new CompoundEdit (); 77 support.getUndoRedo().undoableEditHappened(new UndoableEditEvent (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 { 114 115 StyledDocument doc = support.openDocument(); 116 StringBuffer sb = new StringBuffer (); 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 bigEdit = new CompoundEdit (); 127 support.getUndoRedo().undoableEditHappened(new UndoableEditEvent (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 167 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 168 propL.add (l); 169 } 170 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 171 propL.remove (l); 172 } 173 174 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 175 assertNull ("This is the first veto listener", vetoL); 176 vetoL = l; 177 } 178 public void removeVetoableChangeListener(java.beans.VetoableChangeListener 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 getMimeType() { 188 return "text/plain"; 189 } 190 191 public java.util.Date getTime() { 192 return date; 193 } 194 195 public java.io.InputStream inputStream() throws java.io.IOException { 196 return new java.io.ByteArrayInputStream (content.getBytes ()); 197 } 198 public java.io.OutputStream outputStream() throws java.io.IOException { 199 class ContentStream extends java.io.ByteArrayOutputStream { 200 public void close () throws java.io.IOException { 201 super.close (); 202 content = new String (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 { 218 modified = true; 219 } 220 221 public void unmarkModified() { 222 modified = false; 223 } 224 225 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 messageName() { 235 return "Name"; 236 } 237 238 protected String messageOpened() { 239 return "Opened"; 240 } 241 242 protected String messageOpening() { 243 return "Opening"; 244 } 245 246 protected String messageSave() { 247 return "Save"; 248 } 249 250 protected String messageToolTip() { 251 return "ToolTip"; 252 } 253 254 protected javax.swing.text.EditorKit createEditorKit() { 255 if (plain) { 256 return super.createEditorKit (); 257 } else { 258 return new NbLikeEditorKit (); 259 } 260 } 261 } 263 private static final class FakeEdit implements javax.swing.undo.UndoableEdit { 264 public boolean addEdit(javax.swing.undo.UndoableEdit 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 getPresentationName() { 280 return ""; 281 } 282 283 public java.lang.String getRedoPresentationName() { 284 return ""; 285 } 286 287 public java.lang.String getUndoPresentationName() { 288 return ""; 289 } 290 291 public boolean isSignificant() { 292 return false; 293 } 294 295 public void redo() throws javax.swing.undo.CannotRedoException { 296 } 297 298 public boolean replaceEdit(javax.swing.undo.UndoableEdit anEdit) { 299 return true; 300 } 301 302 public void undo() throws javax.swing.undo.CannotUndoException { 303 } 304 305 } } 307 | Popular Tags |