jhomenet.gui.wizard
Class BracketMatcher

java.lang.Object
  extended by jhomenet.gui.wizard.BracketMatcher
All Implemented Interfaces:
java.util.EventListener, javax.swing.event.CaretListener

public class BracketMatcher
extends java.lang.Object
implements javax.swing.event.CaretListener

A class to support highlighting of parenthesis. To use it, add it as a caret listener to your text component. It listens for the location of the dot. If the character before the dot is a close paren, it finds the matching start paren and highlights both of them. Otherwise it clears the highlighting. This object can be shared among multiple components. It will only highlight one at a time.


Method Summary
 void caretUpdate(javax.swing.event.CaretEvent e)
          Called whenever the caret moves, it updates the highlights
 void clearHighlights()
           
static int findMatchingParen(javax.swing.text.Document d, int paren)
          Returns the position of the matching parenthesis (bracket, whatever) for the character at paren.
static char getCharAt(javax.swing.text.Document doc, int p)
          Returns the character at position p in the document
static void main(java.lang.String[] a)
          A demo main
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clearHighlights

public void clearHighlights()

getCharAt

public static char getCharAt(javax.swing.text.Document doc,
                             int p)
                      throws javax.swing.text.BadLocationException
Returns the character at position p in the document

Throws:
javax.swing.text.BadLocationException

findMatchingParen

public static int findMatchingParen(javax.swing.text.Document d,
                                    int paren)
                             throws javax.swing.text.BadLocationException
Returns the position of the matching parenthesis (bracket, whatever) for the character at paren. It counts all kinds of brackets, so the "matching" parenthesis might be a bad one. For this demo, we're not going to take quotes or comments into account since that's not the point. It's assumed that paren is the position of some parenthesis character

Returns:
the position of the matching paren, or -1 if none is found
Throws:
javax.swing.text.BadLocationException

caretUpdate

public void caretUpdate(javax.swing.event.CaretEvent e)
Called whenever the caret moves, it updates the highlights

Specified by:
caretUpdate in interface javax.swing.event.CaretListener

main

public static void main(java.lang.String[] a)
A demo main



Copyright © 2004-2005 David Irwin. All Rights Reserved.