Groovy Documentation

org.codehaus.griffon.runtime.search
[Java] Class JTextComponentSearcher

java.lang.Object
  org.codehaus.griffon.runtime.search.JTextComponentSearcher
All Implemented Interfaces:
Searcher

public class JTextComponentSearcher
extends Object

Implementation of the Searcher interface that targets JTextComponent.

Heavily based in SearchEngine.

Authors:
Robert Futrell (RTextArea 1.0)
Andres Almiray


Constructor Summary
JTextComponentSearcher()

Private constructor to prevent instantiation.

 
Method Summary
boolean find(JComponent subject, String text, boolean forward, boolean matchCase, boolean wholeWord, boolean regex)

Finds the next instance of the string/regular expression specified from the caret position.

protected String getFindInText(JTextComponent component, int start, boolean forward)

Returns the text in which to search, as a string.

protected List getMatches(Matcher m, String replaceStr)

This method is called internally by getNextMatchPosRegExImpl and is used to get the locations of all regular-expression matches, and possibly their replacement strings.

int getNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord)

Searches searchIn for an occurrence of searchFor either forwards or backwards, matching case or not.

protected int getNextMatchPosImpl(String searchFor, String searchIn, boolean goForward, boolean matchCase, boolean wholeWord)

Actually does the work of matching; assumes searchFor and searchIn are already upper/lower-cased appropriately.

Point getNextMatchPosRegEx(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord)

Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.

protected Object getNextMatchPosRegExImpl(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replaceStr)

Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.

protected RegExReplaceInfo getRegExReplaceInfo(String regEx, String searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replacement)

Returns information on how to implement a regular expression "replace" action in the specified text with the specified replacement string.

String getReplacementText(Matcher m, CharSequence template)

Called internally by getMatches().

private boolean isWholeWord(CharSequence searchIn, int offset, int len)

Returns whether the characters on either side of substr(searchIn,startPos,startPos+searchStringLength) are whitespace.

protected int makeMarkAndDotEqual(JTextComponent component, boolean forward)

Makes the caret's dot and mark the same location so that, for the next search in the specified direction, a match will be found even if it was within the original dot and mark's selection.

protected boolean regexReplace(JComponent subject, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord)

Finds the next instance of the regular expression specified from the caret position.

boolean replace(JComponent subject, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord, boolean regex)

Finds the next instance of the text/regular expression specified from the caret position.

int replaceAll(JComponent subject, String toFind, String replaceWith, boolean matchCase, boolean wholeWord, boolean regex)

Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.

private void selectAndPossiblyCenter(JTextComponent component, int start, int end)

Selects a range of text in a text component.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Constructor Detail

JTextComponentSearcher

public JTextComponentSearcher()
Private constructor to prevent instantiation.


 
Method Detail

find

public boolean find(JComponent subject, String text, boolean forward, boolean matchCase, boolean wholeWord, boolean regex)
Finds the next instance of the string/regular expression specified from the caret position. If a match is found, it is selected in this text area.
throws:
PatternSyntaxException If regex is true but text is not a valid regular expression.
Parameters:
subject - The text area in which to search.
text - The string literal or regular expression to search for.
forward - Whether to search forward from the caret position or backward from it.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
regex - Whether text is a Java regular expression to search for.
Returns:
Whether a match was found (and thus selected).
See Also:
replace
regexReplace


getFindInText

protected String getFindInText(JTextComponent component, int start, boolean forward)
Returns the text in which to search, as a string. This is used internally to grab the smallest buffer possible in which to search.


getMatches

protected List getMatches(Matcher m, String replaceStr)
This method is called internally by getNextMatchPosRegExImpl and is used to get the locations of all regular-expression matches, and possibly their replacement strings.

Returns either:

If replacement is null, this method call is assumed to be part of a "find" operation and points are returned. If if is non-null, it is assumed to be part of a "replace" operation and the RegExReplaceInfos are returned.

throws:
IndexOutOfBoundsException If replaceStr references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
m - The matcher.
replaceStr - The string to replace matches with. This is a "template" string and can contain captured group references in the form "\${digit}".
Returns:
A list of result objects.


getNextMatchPos

public final int getNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord)
Searches searchIn for an occurrence of searchFor either forwards or backwards, matching case or not.
Parameters:
searchFor - The string to look for.
searchIn - The string to search in.
forward - Whether to search forward or backward in searchIn.
matchCase - If true, do a case-sensitive search for searchFor.
wholeWord - If true, searchFor occurrences embedded in longer words in searchIn don't count as matches.
Returns:
The starting position of a match, or -1 if no match was found.
See Also:
getNextMatchPosImpl
getNextMatchPosRegEx


getNextMatchPosImpl

protected final int getNextMatchPosImpl(String searchFor, String searchIn, boolean goForward, boolean matchCase, boolean wholeWord)
Actually does the work of matching; assumes searchFor and searchIn are already upper/lower-cased appropriately.
The reason this method is here is to attempt to speed up FindInFilesDialog; since it repeatedly calls this method instead of getNextMatchPos, it gets better performance as it no longer has to allocate a lower-cased string for every call.
Parameters:
searchFor - The string to search for.
searchIn - The string to search in.
goForward - Whether the search is forward or backward.
matchCase - Whether the search is case-sensitive.
wholeWord - Whether only whole words should be matched.
Returns:
The location of the next match, or -1 if no match was found.


getNextMatchPosRegEx

public Point getNextMatchPosRegEx(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord)
Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.
throws:
PatternSyntaxException If regEx is an invalid regular expression.
Parameters:
regEx - The regular expression to look for.
searchIn - The string to search in.
goForward - Whether to search forward. If false, search backward.
matchCase - Whether or not to do a case-sensitive search for regEx.
wholeWord - If true, regEx occurrences embedded in longer words in searchIn don't count as matches.
Returns:
A Point representing the starting and ending position of the match, or null if no match was found.
See Also:
getNextMatchPos


getNextMatchPosRegExImpl

protected Object getNextMatchPosRegExImpl(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replaceStr)
Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.
throws:
PatternSyntaxException If regEx is an invalid regular expression.
throws:
IndexOutOfBoundsException If replaceStr references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
regEx - The regular expression to look for.
searchIn - The string to search in.
goForward - Whether to search forward. If false, search backward.
matchCase - Whether or not to do a case-sensitive search for regEx.
wholeWord - If true, regEx occurrences embedded in longer words in searchIn don't count as matches.
replaceStr - The string that will replace the match found (if a match is found). The object returned will contain the replacement string with matched groups substituted. If this value is null, it is assumed this call is part of a "find" instead of a "replace" operation.
Returns:
If replaceStr is null, a Point representing the starting and ending points of the match. If it is non-null, an object with information about the match and the morphed string to replace it with. If no match is found, null is returned.
See Also:
getNextMatchPos


getRegExReplaceInfo

protected RegExReplaceInfo getRegExReplaceInfo(String regEx, String searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replacement)
Returns information on how to implement a regular expression "replace" action in the specified text with the specified replacement string.
throws:
PatternSyntaxException If regEx is an invalid regular expression.
throws:
IndexOutOfBoundsException If replacement references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
regEx - The regular expression to look for.
searchIn - The string to search in.
goForward - Whether to search forward. If false, search backward.
matchCase - Whether or not to do a case-sensitive search for regEx.
wholeWord - If true, regEx occurrences embedded in longer words in searchIn don't count as matches.
replacement - A template for the replacement string (e.g., this can contain \\t and \\n to mean tabs and newlines, respectively, as well as group references \$n).
Returns:
A RegExReplaceInfo object describing how to implement the replace.
See Also:
getNextMatchPos


getReplacementText

public String getReplacementText(Matcher m, CharSequence template)
Called internally by getMatches(). This method assumes that the specified matcher has just found a match, and that you want to get the string with which to replace that match.
throws:
IndexOutOfBoundsException If template references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
m - The matcher.
template - The template for the replacement string. For example, "foo" would yield the replacement string "foo", while "\$1 is the greatest" would yield different values depending on the value of the first captured group in the match.
Returns:
The string to replace the match with.


isWholeWord

private final boolean isWholeWord(CharSequence searchIn, int offset, int len)
Returns whether the characters on either side of substr(searchIn,startPos,startPos+searchStringLength) are whitespace. While this isn't the best definition of "whole word", it's the one we're going to use for now.


makeMarkAndDotEqual

protected int makeMarkAndDotEqual(JTextComponent component, boolean forward)
Makes the caret's dot and mark the same location so that, for the next search in the specified direction, a match will be found even if it was within the original dot and mark's selection.
Parameters:
component - The text area.
forward - Whether the search will be forward through the document (false means backward).
Returns:
The new dot and mark position.


regexReplace

protected boolean regexReplace(JComponent subject, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord)
Finds the next instance of the regular expression specified from the caret position. If a match is found, it is replaced with the specified replacement string.
throws:
PatternSyntaxException If toFind is not a valid regular expression.
throws:
IndexOutOfBoundsException If replaceWith references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
subject - The text area in which to search.
toFind - The regular expression to search for.
replaceWith - The string to replace the found regex with.
forward - Whether to search forward from the caret position or backward from it.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
Returns:
Whether a match was found (and thus replaced).
See Also:
replace
find


replace

public boolean replace(JComponent subject, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord, boolean regex)
Finds the next instance of the text/regular expression specified from the caret position. If a match is found, it is replaced with the specified replacement string.
throws:
PatternSyntaxException If regex is true but toFind is not a valid regular expression.
throws:
IndexOutOfBoundsException If regex is true and replaceWith references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
subject - The text area in which to search.
toFind - The text/regular expression to search for.
replaceWith - The string to replace the found text with.
forward - Whether to search forward from the caret position or backward from it.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
regex - Whether or not this is a regular expression search.
Returns:
Whether a match was found (and thus replaced).
See Also:
regexReplace
find


replaceAll

public int replaceAll(JComponent subject, String toFind, String replaceWith, boolean matchCase, boolean wholeWord, boolean regex)
Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.
throws:
PatternSyntaxException If regex is true and toFind is an invalid regular expression.
throws:
IndexOutOfBoundsException If replaceWith references an invalid group (less than zero or greater than the number of groups matched).
Parameters:
subject - The text area in which to search.
toFind - The text/regular expression to search for.
replaceWith - The string to replace the found text with.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
regex - Whether or not this is a regular expression search.
Returns:
The number of replacements done.
See Also:
replace
regexReplace
find


selectAndPossiblyCenter

private void selectAndPossiblyCenter(JTextComponent component, int start, int end)
Selects a range of text in a text component. If the new selection is outside of the previous viewable rectangle, then the view is centered around the new selection.
Parameters:
component - The text component whose selection is to be centered.
start - The start of the range to select.
end - The end of the range to select.


 

Groovy Documentation