FileDocCategorySizeDatePackage
LiveParenMatcher.javaAPI DocExample3285Thu Oct 24 20:14:26 BST 2002None

LiveParenMatcher

public class LiveParenMatcher extends ParenMatcher implements DocumentListener

Fields Summary
Constructors Summary
public LiveParenMatcher()

    super();
    getDocument().addDocumentListener(this);
  
Methods Summary
public voidchangedUpdate(javax.swing.event.DocumentEvent de)

    // no insertion or deletion, so do nothing
  
public voidinsertUpdate(javax.swing.event.DocumentEvent de)

    SwingUtilities.invokeLater(this); // will call run()
  
public voidinsertUpdate_2(javax.swing.event.DocumentEvent de)

    Document doc = de.getDocument();
    int offset = de.getOffset();
    int length = de.getLength();
    String inserted = "";
    try {
      inserted = doc.getText(offset, length);
    } catch (BadLocationException ble) { }

    for (int j=0; j < inserted.length(); j+=1) {
      char ch = inserted.charAt(j);
      if (ch == '(" || ch == '[" || ch == '{" ||
          ch == ')" || ch == ']" || ch == '}"  ) {
        SwingUtilities.invokeLater(this); // will call run()
        return; // no need to check further
      }
    }
  
public voidinsertUpdate_3(javax.swing.event.DocumentEvent de)

    Document doc = de.getDocument();
    int offset = de.getOffset();
    int length = de.getLength();
    Segment seg = new Segment();
    try {
      doc.getText(offset, length, seg); // text placed in Segment
    } catch (BadLocationException ble) { }

    // iterate through the Segment
    for (char ch = seg.first(); ch != seg.DONE; ch = seg.next())
      if (ch == '(" || ch == '[" || ch == '{" ||
          ch == ')" || ch == ']" || ch == '}"  ) {
        SwingUtilities.invokeLater(this); // will call run()
        return; // no need to check further
      }
  
public static voidmain(java.lang.String[] args)

    JFrame frame = new JFrame("LiveParenMatcher");
    frame.setContentPane(new JScrollPane(new LiveParenMatcher()));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
  
public voidremoveUpdate(javax.swing.event.DocumentEvent de)

    SwingUtilities.invokeLater(this); // will call run()
  
public voidremoveUpdate_2(javax.swing.event.DocumentEvent de)

    // print some debugging information before matching the parens
    ElementIterator iter = new ElementIterator(de.getDocument());

    for (Element elem = iter.first(); elem != null; elem = iter.next()) {
      DocumentEvent.ElementChange change = de.getChange(elem);
      if (change != null) { // null means there was no change in elem
        System.out.println("Element "+elem.getName() + " (depth " +
            iter.depth()+") changed its children: " +
            change.getChildrenRemoved().length+" children removed, " +
            change.getChildrenAdded().length+" children added.\n");
      }
    }
    SwingUtilities.invokeLater(this); // will call run()