org.jdesktop.swingx
Class AbstractSearchable

java.lang.Object
  extended by org.jdesktop.swingx.AbstractSearchable
All Implemented Interfaces:
Searchable
Direct Known Subclasses:
JXList.ListSearchable, JXTable.TableSearchable, JXTree.TreeSearchable

public abstract class AbstractSearchable
extends java.lang.Object
implements Searchable

An abstract implementation of Searchable supporting incremental search. Keeps internal state to represent the previous search result. For all methods taking a string as parameter: compiles the String to a Pattern as-is and routes to the central method taking a Pattern.

Author:
Jeanette Winzenburg

Nested Class Summary
static class AbstractSearchable.SearchResult
          A convenience class to hold search state.
 
Field Summary
protected  AbstractSearchable.SearchResult lastSearchResult
          stores the result of the previous search.
static java.lang.String MATCH_HIGHLIGHTER
          key for client property to use SearchHighlighter as match marker.
static AbstractSearchable.SearchResult NO_MATCH
          a constant representing not-found state.
 
Constructor Summary
AbstractSearchable()
           
 
Method Summary
protected  int adjustStartPosition(int startIndex, boolean backwards)
          Called if startIndex is different from last search and make sure a backwards/forwards search starts at last/first row, respectively.
protected  AbstractSearchable.SearchResult createSearchResult(java.util.regex.Matcher matcher, int row, int column)
          Factory method to create a SearchResult from the given parameters.
protected  int doSearch(java.util.regex.Pattern pattern, int startIndex, boolean backwards)
          Performs a search starting at the given startIndex using the pattern; Pattern.
protected abstract  AbstractSearchable.SearchResult findExtendedMatch(java.util.regex.Pattern pattern, int row)
          called if sameRowIndex && !hasEqualRegEx.
protected abstract  void findMatchAndUpdateState(java.util.regex.Pattern pattern, int startRow, boolean backwards)
          loops through the searchable until a match is found or the end is reached.
protected abstract  int getSize()
          returns the size of this searchable.
protected  boolean isEmpty(java.lang.String searchString)
          checks if the searchString should be interpreted as empty.
protected  boolean isEqualPattern(java.util.regex.Pattern pattern)
          Checks if the given Pattern should be considered as the same as in a previous search.
protected  boolean isEqualStartIndex(int startIndex)
          Checks if the startIndex should be considered as the same as in the previous search.
protected  boolean isTrivialNoMatch(java.util.regex.Pattern pattern, int startIndex)
          Checks and returns if it can be trivially decided to not match.
protected  boolean isValidIndex(int index)
          checks if index is in range: 0 <= index < getSize().
protected abstract  void moveMatchMarker()
          Moves the match marker according to current found state.
protected  int moveStartPosition(int startIndex, boolean backwards)
          Moves the internal start position for matching as appropriate and returns the new startIndex to use.
 int search(java.util.regex.Pattern pattern)
          Performs a forward search starting at the beginning across the Searchable using the pattern; Pattern.
 int search(java.util.regex.Pattern pattern, int startIndex)
          Performs a forward search starting at the given startIndex using the Pattern; Pattern.
 int search(java.util.regex.Pattern pattern, int startIndex, boolean backwards)
          Performs a search starting at the given startIndex using the pattern; Pattern.
 int search(java.lang.String searchString)
          Performs a forward search starting at the beginning across the Searchable using String that represents a regex pattern; Pattern.
 int search(java.lang.String searchString, int startIndex)
          Performs a forward search starting at the given startIndex using String that represents a regex pattern; Pattern.
 int search(java.lang.String searchString, int startIndex, boolean backward)
          Performs a search starting at the given startIndex using String that represents a regex pattern; Pattern.
protected  void updateState(AbstractSearchable.SearchResult searchResult)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_MATCH

public static final AbstractSearchable.SearchResult NO_MATCH
a constant representing not-found state.


lastSearchResult

protected AbstractSearchable.SearchResult lastSearchResult
stores the result of the previous search.


MATCH_HIGHLIGHTER

public static final java.lang.String MATCH_HIGHLIGHTER
key for client property to use SearchHighlighter as match marker.

See Also:
Constant Field Values
Constructor Detail

AbstractSearchable

public AbstractSearchable()
Method Detail

search

public int search(java.lang.String searchString)
Performs a forward search starting at the beginning across the Searchable using String that represents a regex pattern; Pattern.

Specified by:
search in interface Searchable
Parameters:
searchString - String we should find in a document.
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

search

public int search(java.lang.String searchString,
                  int startIndex)
Performs a forward search starting at the given startIndex using String that represents a regex pattern; Pattern.

Specified by:
search in interface Searchable
Parameters:
searchString - String we should find in a document.
startIndex - Start position in a document or -1 if we want to search from the beginning.
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

search

public int search(java.lang.String searchString,
                  int startIndex,
                  boolean backward)
Performs a search starting at the given startIndex using String that represents a regex pattern; Pattern. The search direction depends on the boolean parameter: forward/backward if false/true, respectively.

Specified by:
search in interface Searchable
Parameters:
searchString - String we should find in a document.
startIndex - Start position in a document or -1 if we want to search from the beginning.
backward - Indicates search direction, will search from the given position towards the beginning of a document if this parameter is true.
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

search

public int search(java.util.regex.Pattern pattern)
Performs a forward search starting at the beginning across the Searchable using the pattern; Pattern.

Specified by:
search in interface Searchable
Parameters:
pattern - Pattern for search
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

search

public int search(java.util.regex.Pattern pattern,
                  int startIndex)
Performs a forward search starting at the given startIndex using the Pattern; Pattern.

Specified by:
search in interface Searchable
Parameters:
pattern - Pattern for search
startIndex - starting index of search. If -1 then start from the beginning
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

search

public int search(java.util.regex.Pattern pattern,
                  int startIndex,
                  boolean backwards)
Performs a search starting at the given startIndex using the pattern; Pattern. The search direction depends on the boolean parameter: forward/backward if false/true, respectively. Updates visible and internal search state.

Specified by:
search in interface Searchable
Parameters:
startIndex - the index to start search
backwards - flag for search direction
pattern -
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

doSearch

protected int doSearch(java.util.regex.Pattern pattern,
                       int startIndex,
                       boolean backwards)
Performs a search starting at the given startIndex using the pattern; Pattern. The search direction depends on the boolean parameter: forward/backward if false/true, respectively. Updates internal search state.

Parameters:
pattern -
startIndex -
backwards -
Returns:
the position of the match in appropriate coordinates or -1 if no match found.

findMatchAndUpdateState

protected abstract void findMatchAndUpdateState(java.util.regex.Pattern pattern,
                                                int startRow,
                                                boolean backwards)
loops through the searchable until a match is found or the end is reached. Updates internal search state.

Parameters:
pattern -
startRow -
backwards -

isTrivialNoMatch

protected boolean isTrivialNoMatch(java.util.regex.Pattern pattern,
                                   int startIndex)
Checks and returns if it can be trivially decided to not match. Here: pattern is null or startIndex exceeds the upper size limit.

Parameters:
pattern -
startIndex -
Returns:

adjustStartPosition

protected int adjustStartPosition(int startIndex,
                                  boolean backwards)
Called if startIndex is different from last search and make sure a backwards/forwards search starts at last/first row, respectively.

Parameters:
startIndex -
backwards -
Returns:

moveStartPosition

protected int moveStartPosition(int startIndex,
                                boolean backwards)
Moves the internal start position for matching as appropriate and returns the new startIndex to use. Called if search was messaged with the same startIndex as previously.

Parameters:
startIndex -
backwards -
Returns:

isEqualPattern

protected boolean isEqualPattern(java.util.regex.Pattern pattern)
Checks if the given Pattern should be considered as the same as in a previous search. Here: compares the patterns' regex.

Parameters:
pattern -
Returns:

isEqualStartIndex

protected boolean isEqualStartIndex(int startIndex)
Checks if the startIndex should be considered as the same as in the previous search.

Parameters:
startIndex -
Returns:
true if the startIndex should be re-matched, false if not.

isEmpty

protected boolean isEmpty(java.lang.String searchString)
checks if the searchString should be interpreted as empty. here: returns true if string is null or has zero length.

Parameters:
searchString -
Returns:

findExtendedMatch

protected abstract AbstractSearchable.SearchResult findExtendedMatch(java.util.regex.Pattern pattern,
                                                                     int row)
called if sameRowIndex && !hasEqualRegEx. Matches the cell at row/lastFoundColumn against the pattern. PRE: lastFoundColumn valid.

Parameters:
pattern -
row -
Returns:

createSearchResult

protected AbstractSearchable.SearchResult createSearchResult(java.util.regex.Matcher matcher,
                                                             int row,
                                                             int column)
Factory method to create a SearchResult from the given parameters.

Parameters:
matcher - the matcher after a successful find. Must not be null.
row - the found index
column - the found column
Returns:

isValidIndex

protected boolean isValidIndex(int index)
checks if index is in range: 0 <= index < getSize().

Parameters:
index -
Returns:

getSize

protected abstract int getSize()
returns the size of this searchable.

Returns:

updateState

protected void updateState(AbstractSearchable.SearchResult searchResult)

moveMatchMarker

protected abstract void moveMatchMarker()
Moves the match marker according to current found state.



Copyright © 2005 Sun Microsystems All Rights Reserved.