1 2 package org.netbeans.modules.editor; 3 4 import javax.swing.event.ChangeEvent ; 5 import javax.swing.event.ChangeListener ; 6 import javax.swing.event.DocumentEvent ; 7 import javax.swing.event.DocumentListener ; 8 import javax.swing.event.EventListenerList ; 9 import javax.swing.text.BadLocationException ; 10 import javax.swing.text.Caret ; 11 import javax.swing.text.Document ; 12 import javax.swing.text.EditorKit ; 13 import org.netbeans.editor.BaseDocument; 14 import org.netbeans.editor.BaseKit; 15 import org.netbeans.junit.NbTestCase; 16 17 22 public class BaseDocumentUnitTestCase extends NbTestCase { 23 24 private EditorKit editorKit; 25 26 private String loadDocumentText; 27 28 private BaseDocument doc; 29 30 private Caret caret; 31 32 private int loadCaretOffset = -1; 33 34 35 public BaseDocumentUnitTestCase(String testMethodName) { 36 super(testMethodName); 37 38 } 39 40 51 protected void setLoadDocumentText(String loadDocumentText) { 52 loadCaretOffset = loadDocumentText.indexOf('|'); 54 if (loadCaretOffset != -1) { 55 loadDocumentText = loadDocumentText.substring(0, loadCaretOffset) 56 + loadDocumentText.substring(loadCaretOffset + 1); 57 } 58 59 this.loadDocumentText = loadDocumentText; 60 } 61 62 69 protected final String getLoadDocumentText() { 70 return loadDocumentText; 71 } 72 73 80 protected final int getLoadCaretOffset() { 81 return loadCaretOffset; 82 } 83 84 92 protected synchronized final Caret getCaret () { 93 if (caret == null) { 94 caret = new CaretImpl(getDocument(), loadCaretOffset); 95 } 96 return caret; 97 } 98 99 102 protected final int getCaretOffset() { 103 return getCaret().getDot(); 104 } 105 106 112 protected synchronized BaseDocument getDocument() { 113 if (doc == null) { 114 doc = createAndLoadDocument(); 115 } 116 return doc; 117 } 118 119 124 protected String getDocumentText() { 125 try { 126 Document d = getDocument(); 127 return d.getText(0, d.getLength()); 128 } catch (BadLocationException ex) { 129 ex.printStackTrace(getLog()); 130 fail(ex.getMessage()); 131 return null; } 133 } 134 135 139 protected void assertDocumentText(String msg, String expectedText) { 140 String docText = getDocumentText(); 141 if (!docText.equals(expectedText)) { 142 StringBuffer sb = new StringBuffer (); 143 sb.append(msg); 144 sb.append("\n----- expected text: -----\n"); 145 appendDebugText(sb, expectedText); 146 sb.append("\n----- document text: -----\n"); 147 appendDebugText(sb, docText); 148 sb.append("\n-----\n"); 149 150 fail(sb.toString()); 151 } 152 } 153 154 protected final void appendDebugChar(StringBuffer sb, char ch) { 155 switch (ch) { 156 case '\n': 157 sb.append("\\n\n"); 158 break; 159 case '\t': 160 sb.append("\\t"); 161 break; 162 163 default: 164 sb.append(ch); 165 break; 166 } 167 } 168 169 protected final void appendDebugText(StringBuffer sb, String text) { 170 for (int i = 0; i < text.length(); i++) { 171 appendDebugChar(sb, text.charAt(i)); 172 } 173 } 174 175 protected final String debugText(String text) { 176 StringBuffer sb = new StringBuffer (); 177 appendDebugText(sb, text); 178 return sb.toString(); 179 } 180 181 187 protected void assertDocumentTextAndCaret(String msg, String expectedTextAndCaretPipe) { 188 int expectedCaretOffset = expectedTextAndCaretPipe.indexOf('|'); 190 if (expectedCaretOffset == -1) { fail("Caret position not indicated in '" + expectedTextAndCaretPipe + "'"); 192 } 193 194 String expectedDocumentText= expectedTextAndCaretPipe.substring(0, expectedCaretOffset) 195 + expectedTextAndCaretPipe.substring(expectedCaretOffset + 1); 196 197 assertDocumentText(msg, expectedDocumentText); 198 if (expectedCaretOffset != getCaretOffset()) { 199 fail("caretOffset=" + getCaretOffset() 200 + " but expectedCaretOffset=" + expectedCaretOffset 201 + " in '" + expectedTextAndCaretPipe + "'" 202 ); 203 } 204 } 205 206 214 protected EditorKit createEditorKit() { 215 return BaseKit.getKit(BaseKit.class); 216 } 217 218 227 public final EditorKit getEditorKit() { 228 if (editorKit == null) { 229 editorKit = createEditorKit(); 230 } 231 return editorKit; 232 } 233 234 private BaseDocument createAndLoadDocument() { 235 BaseDocument bd = (BaseDocument)getEditorKit().createDefaultDocument(); 236 237 if (loadDocumentText != null) { 238 try { 239 bd.insertString(0, loadDocumentText, null); 240 } catch (BadLocationException e) { 241 e.printStackTrace(getLog()); 242 fail(); 243 } 244 } 245 return bd; 246 } 247 248 class CaretImpl implements Caret , DocumentListener { 249 250 private Document doc; 251 252 private int dot; 253 254 private int mark; 255 256 private boolean visible = true; 257 258 private boolean selectionVisible; 259 260 private int blinkRate = 300; 261 262 private EventListenerList listenerList = new EventListenerList (); 263 264 private ChangeEvent changeEvent; 265 266 CaretImpl(Document doc, int dot) { 267 this.doc = doc; 268 doc.addDocumentListener(this); 269 setDot(dot); 270 } 271 272 public void deinstall (javax.swing.text.JTextComponent c) { 273 fail("Not yet implemented"); 274 } 275 276 public void install (javax.swing.text.JTextComponent c) { 277 fail("Not yet implemented"); 278 } 279 280 public java.awt.Point getMagicCaretPosition () { 281 fail("Not yet implemented"); 282 return null; 283 } 284 285 public void setMagicCaretPosition (java.awt.Point p) { 286 fail("Not yet implemented"); 287 } 288 289 public int getDot () { 290 return dot; 291 } 292 293 public int getMark () { 294 return mark; 295 } 296 297 public void setDot (int dot) { 298 this.mark = this.dot; 299 changeCaretPosition(dot); 300 } 301 302 public void moveDot (int dot) { 303 changeCaretPosition(dot); 304 } 305 306 public int getBlinkRate () { 307 return blinkRate; 308 } 309 310 public void setBlinkRate (int rate) { 311 this.blinkRate = blinkRate; 312 } 313 314 public boolean isVisible () { 315 return visible; 316 } 317 318 public void setVisible (boolean v) { 319 this.visible = visible; 320 } 321 322 public boolean isSelectionVisible () { 323 return selectionVisible; 324 } 325 326 public void setSelectionVisible (boolean v) { 327 this.selectionVisible = v; 328 } 329 330 public void addChangeListener (ChangeListener l) { 331 listenerList.add(ChangeListener .class, l); 332 } 333 334 public void removeChangeListener (ChangeListener l) { 335 listenerList.remove(ChangeListener .class, l); 336 } 337 338 public void fireChangeListener() { 339 if (changeEvent == null) { 341 changeEvent = new ChangeEvent (this); 342 } 343 344 Object [] listeners = listenerList.getListenerList(); 345 for (int i = 0; i < listeners.length; i++) { 346 ((ChangeListener )listeners[i + 1]).stateChanged(changeEvent); 347 } 348 } 349 350 public void paint (java.awt.Graphics g) { 351 } 352 353 public void insertUpdate(DocumentEvent e) { 354 int offset = e.getOffset(); 355 int length = e.getLength(); 356 int newDot = dot; 357 short changed = 0; 358 if (newDot >= offset) { 359 newDot += length; 360 changed |= 1; 361 } 362 int newMark = mark; 363 if (newMark >= offset) { 364 newMark += length; 365 changed |= 2; 366 } 367 368 if (changed != 0) { 369 if (newMark == newDot) { 370 setDot(newDot); 371 ensureValidPosition(); 372 } else { 373 setDot(newMark); 374 if (getDot() == newMark) { 375 moveDot(newDot); 376 } 377 ensureValidPosition(); 378 } 379 380 } 381 } 382 383 public void removeUpdate(DocumentEvent e) { 384 int offs0 = e.getOffset(); 385 int offs1 = offs0 + e.getLength(); 386 int newDot = dot; 387 if (newDot >= offs1) { 388 newDot -= (offs1 - offs0); 389 } else if (newDot >= offs0) { 390 newDot = offs0; 391 } 392 int newMark = mark; 393 if (newMark >= offs1) { 394 newMark -= (offs1 - offs0); 395 } else if (newMark >= offs0) { 396 newMark = offs0; 397 } 398 if (newMark == newDot) { 399 setDot(newDot); 400 ensureValidPosition(); 401 } else { 402 setDot(newMark); 403 if (getDot() == newMark) { 404 moveDot(newDot); 405 } 406 ensureValidPosition(); 407 } 408 } 409 410 public void changedUpdate(DocumentEvent e) { 411 412 } 413 414 private void changeCaretPosition(int dot) { 415 if (this.dot != dot) { 416 this.dot = dot; 417 fireChangeListener(); 418 } 419 } 420 421 private void ensureValidPosition() { 422 int length = doc.getLength(); 423 if (dot > length || mark > length) { 424 setDot(length); 425 } 426 } 427 428 } 429 430 } 431 | Popular Tags |