java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow Program that matches bracket as user edits document
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3946
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Program that matches bracket as user edits document E-mail
User Rating: / 3
PoorBest 

This Java tip illustrates a method of matches bracket as user edits document. This tip extends parenthesis matcher tip and extends its functionality. The tip continuously colors as the user edits the document. This tip may also be termed as live parenthesis matcher.


Image

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;

public class LiveParenMatcher extends ParenMatcher implements DocumentListener {

  public LiveParenMatcher() {
    super();
    getDocument().addDocumentListener(this);
  }

  public void changedUpdate(DocumentEvent de) {
    // no insertion or deletion, so do nothing
  }

  public void insertUpdate(DocumentEvent de) {
    SwingUtilities.invokeLater(this)// will call run()
  }

  public void removeUpdate(DocumentEvent de) {
    SwingUtilities.invokeLater(this)// will call run()
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("LiveParenMatcher");
    frame.setContentPane(new JScrollPane(new LiveParenMatcher()));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300200);
    frame.setVisible(true);
  }

  // ---- finished from "The DocumentListener Interface" ----

  // ---- begin from "The DocumentEvent Interface" ----
  
  public void insertUpdate_2(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
      }
    }
  }

  // ---- begin from "The Segment Class" ----

  public void insertUpdate_3(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
      }
  }

  // ---- begin from "The ElementIterator Class" ----

  public void removeUpdate_2(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()
  }

}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.