1 19 20 package org.netbeans.test.j2ee.multiview; 21 22 import javax.swing.event.DocumentEvent ; 23 import javax.swing.event.DocumentListener ; 24 import javax.swing.text.Document ; 25 import junit.framework.AssertionFailedError; 26 import org.netbeans.junit.AssertionFailedErrorException; 27 28 32 public class TestDocumentListener implements DocumentListener { 33 private String findText; 34 35 public TestDocumentListener(String findText){ 36 this.findText = findText; 37 } 38 39 40 public void removeUpdate(javax.swing.event.DocumentEvent e) { 41 } 42 43 public void insertUpdate(javax.swing.event.DocumentEvent e) { 44 fireEvent(e); 45 } 46 47 public void changedUpdate(javax.swing.event.DocumentEvent e) { 48 fireEvent(e); 49 } 50 51 public void fireEvent(DocumentEvent e) throws RuntimeException { 52 try { 53 Document document = (Document )e.getDocument(); 54 String text = document.getText(0,document.getLength()); 55 int index = text.indexOf(findText); 56 if(index<0){ throw new AssertionFailedError("Cannot find correct element " + findText + "in XML view (editor document)"); 58 } 59 document.removeDocumentListener(this); 60 System.out.println("Found text: " + findText + ", index: " + index); 61 } catch (javax.swing.text.BadLocationException ex) { 62 throw new AssertionFailedErrorException("Failed to read the document: ",ex); 63 } 64 } 65 66 } 67 | Popular Tags |