com.wolfram.jlink.ui
Class BracketMatcher

java.lang.Object
  extended by com.wolfram.jlink.ui.BracketMatcher

public class BracketMatcher
extends java.lang.Object

A utility class that detects matching open/close bracket pairs in Mathematica input. This class is used by MathSessionPane to implement its bracket-matching (also called bracket balancing) feature, but you can also use it directly in your own programs.

BracketMatcher detects matching bracket pairs of the following types: (), {}, [], (**). It ignores brackets within strings and within Mathematica comments. It can acommodate nested comments. It searches in the typical way--expanding the current selection left and right to find the first enclosing matching brackets. If this description is not clear enough, simply create a MathSessionPane and investigate how its bracket-matching feature behaves.

To use it, create an instance and supply the input text either in the constructor or by using the setText() method. Then call the balance() method, supplying the character position of the left end of the current selection (or the position of the caret if there is no selection) and the length of the selection in characters (0 if there is no selected region). The balance() method returns a Point, which acts simply as a container for two integers: the x value is the character position of the left-most matching bracket and the y value is the character position just to the right of the rightmost bracket. That means that these numbers mark the beginning and end of a selection region that encompasses a matched bracket pair. Null is returned if there is no block larger than the current selection that is enclosed by a matched pair and has no unclosed brackets within it.

A single BracketMatcher instance can be reused with different text by calling the setText() method each time.

Since:
2.0
See Also:
MathSessionPane

Constructor Summary
BracketMatcher()
           
BracketMatcher(java.lang.String text)
           
 
Method Summary
 java.awt.Point balance(int leftEndOfSelection, int selectionLength)
          Performs the search for matching brackets.
 void setText(java.lang.String text)
          Sets the string of input that will be searched for matches.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BracketMatcher

public BracketMatcher()

BracketMatcher

public BracketMatcher(java.lang.String text)
Parameters:
text - the string of input that will be searched for matches
Method Detail

setText

public void setText(java.lang.String text)
Sets the string of input that will be searched for matches.

Parameters:
text -

balance

public java.awt.Point balance(int leftEndOfSelection,
                              int selectionLength)
Performs the search for matching brackets.

Parameters:
leftEndOfSelection - the character position of the left end of the current selection (or the position of the caret if there is no selection)
selectionLength - the length of the selection in characters (0 if there is no selected region)
Returns:
a Point whose x value is the character position of the left-most matching bracket and whose y value is the character position just to the right of the rightmost bracket. Null if there is no matching set that encloses the current selection.


J/Link is Copyright (c) 1999-2008, Wolfram Research, Inc. All rights reserved.