Class AbstractSearchable
- java.lang.Object
-
- org.jdesktop.swingx.search.AbstractSearchable
-
- All Implemented Interfaces:
Searchable
- Direct Known Subclasses:
ListSearchable,TableSearchable,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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractSearchable.SearchResultA convenience class to hold search state.
-
Field Summary
Fields Modifier and Type Field Description protected AbstractSearchable.SearchResultlastSearchResultstores the result of the previous search.static java.lang.StringMATCH_HIGHLIGHTERkey for client property to use SearchHighlighter as match marker.private AbstractHighlightermatchHighlighter
-
Constructor Summary
Constructors Constructor Description AbstractSearchable()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidaddHighlighter(Highlighter highlighter)Adds the highlighter to the target.protected intadjustStartPosition(int startIndex, boolean backwards)Called ifstartIndexis different from last search and make sure a backwards/forwards search starts at last/first row, respectively.protected intconvertColumnIndexToModel(int viewColumn)Converts and returns the given column index from view coordinates to model coordinates.protected AbstractHighlightercreateMatchHighlighter()Creates and returns the Highlighter used as match marker.protected HighlightPredicatecreateMatchPredicate()Creates and returns a HighlightPredicate appropriate for the current search result.protected AbstractSearchable.SearchResultcreateSearchResult(java.util.regex.Matcher matcher, int row, int column)Factory method to create a SearchResult from the given parameters.protected intdoSearch(java.util.regex.Pattern pattern, int startIndex, boolean backwards)Performs a search starting at the given startIndex using the pattern;Pattern.protected voidensureInsertedSearchHighlighters(Highlighter highlighter)Ensure that the given Highlighter is the last in the list of the highlighters registered on the target.protected abstract AbstractSearchable.SearchResultfindExtendedMatch(java.util.regex.Pattern pattern, int row)Matches the cell at row/lastFoundColumn against the pattern.protected abstract voidfindMatchAndUpdateState(java.util.regex.Pattern pattern, int startRow, boolean backwards)Loops through the searchable until a match is found or the end is reached.protected AbstractHighlightergetConfiguredMatchHighlighter()Configures and returns the match highlighter for the current match.protected abstract Highlighter[]getHighlighters()Returns the highlighters registered on the search target.protected AbstractHighlightergetMatchHighlighter()Returns the Hihglighter to use as match marker, lazyly created if null.protected abstract intgetSize()Returns the size of this searchable.abstract javax.swing.JComponentgetTarget()It's the responsibility of subclasses to covariant override.protected booleanhasMatch()Returns a boolean indicating whether the current search result is a match.private booleanhasMatch(AbstractSearchable.SearchResult result)protected booleanisEmpty(java.lang.String searchString)Checks if the searchString should be interpreted as empty.protected booleanisEqualPattern(java.util.regex.Pattern pattern)Checks if the given Pattern should be considered as the same as in a previous search.protected booleanisEqualStartIndex(int startIndex)Checks if the startIndex should be considered as the same as in the previous search.private booleanisInPipeline(Highlighter searchHighlighter)Returns a flag indicating if the given highlighter is last in the list of highlighters registered on the target.protected booleanisTrivialNoMatch(java.util.regex.Pattern pattern, int startIndex)Returns a boolean indicating if it can be trivially decided to not match.protected booleanisValidIndex(int index)Checks if index is in range: 0 <= index < getSize().protected booleanmarkByHighlighter()Returns a boolean indicating whether a match should be marked with a Highlighter.protected abstract voidmoveMatchMarker()Moves the match marker according to current found state.protected intmoveStartPosition(int startIndex, boolean backwards)Moves the internal start position for matching as appropriate and returns the new startIndex to use.protected abstract voidremoveHighlighter(Highlighter searchHighlighter)Removes the highlighter.intsearch(java.lang.String searchString)Performs a forward search starting at the beginning across the Searchable using String that represents a regex pattern;Pattern.intsearch(java.lang.String searchString, int startIndex)Performs a forward search starting at the given startIndex using String that represents a regex pattern;Pattern.intsearch(java.lang.String searchString, int startIndex, boolean backward)Performs a search starting at the given startIndex using String that represents a regex pattern;Pattern.intsearch(java.util.regex.Pattern pattern)Performs a forward search starting at the beginning across the Searchable using the pattern;Pattern.intsearch(java.util.regex.Pattern pattern, int startIndex)Performs a forward search starting at the given startIndex using the Pattern;Pattern.intsearch(java.util.regex.Pattern pattern, int startIndex, boolean backwards)Performs a search starting at the given startIndex using the pattern;Pattern.voidsetMatchHighlighter(AbstractHighlighter hl)Sets the AbstractHighlighter to use as match marker, if enabled.protected voidupdateState(AbstractSearchable.SearchResult searchResult)Updates inner searchable state based on provided search result
-
-
-
Field Detail
-
lastSearchResult
protected final AbstractSearchable.SearchResult lastSearchResult
stores the result of the previous search.
-
matchHighlighter
private AbstractHighlighter matchHighlighter
-
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
-
-
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:
searchin interfaceSearchable- Parameters:
searchString-Stringthat we will try to locate- 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:
searchin interfaceSearchable- Parameters:
searchString-Stringthat we will try to locatestartIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start 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:
searchin interfaceSearchable- Parameters:
searchString-Stringthat we will try to locatestartIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginningbackward-trueif we should perform search towards 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)
Performs a forward search starting at the beginning across the Searchable using the pattern;Pattern.- Specified by:
searchin interfaceSearchable- Parameters:
pattern-Patternthat we will try to locate- 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:
searchin interfaceSearchable- Parameters:
pattern-Patternthat we will try to locatestartIndex- position in the document in the appropriate coordinates from which we will start search or -1 to 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:
searchin interfaceSearchable- Parameters:
pattern-Patternthat we will try to locatestartIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginningbackwards-trueif we should perform search towards the beginning- 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-Patternthat we will try to locatestartIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginningbackwards-trueif we should perform search towards the beginning- 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-Patternthat we will try to locatestartRow- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginningbackwards-trueif we should perform search towards the beginning
-
isTrivialNoMatch
protected boolean isTrivialNoMatch(java.util.regex.Pattern pattern, int startIndex)Returns a boolean indicating if it can be trivially decided to not match.This implementation returns true if pattern is null or startIndex exceeds the upper size limit.
- Parameters:
pattern-Patternthat we will try to locatestartIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginning- Returns:
- true if we can say ahead that no match will be found with given search criteria
-
adjustStartPosition
protected int adjustStartPosition(int startIndex, boolean backwards)Called ifstartIndexis different from last search and make sure a backwards/forwards search starts at last/first row, respectively.- Parameters:
startIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginningbackwards-trueif we should perform search from towards the beginning- Returns:
- adjusted
startIndex
-
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.This implementation returns a by 1 decremented/incremented startIndex depending on backwards true/false, respectively.
- Parameters:
startIndex- position in the document in the appropriate coordinates from which we will start search or -1 to start from the beginningbackwards-trueif we should perform search towards the beginning- Returns:
- adjusted
startIndex
-
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.This implementation compares the patterns' regex.
- Parameters:
pattern-Patternthat we will compare with last request- Returns:
- if provided
Patternis the same as the stored from the previous search attempt
-
isEqualStartIndex
protected boolean isEqualStartIndex(int startIndex)
Checks if the startIndex should be considered as the same as in the previous search.- Parameters:
startIndex-startIndexthat we will compare with the index stored by the previous search request- 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.This implementation returns true if string is null or has zero length.
- Parameters:
searchString-Stringthat we should evaluate- Returns:
- true if the provided
Stringshould be interpreted as empty
-
findExtendedMatch
protected abstract AbstractSearchable.SearchResult findExtendedMatch(java.util.regex.Pattern pattern, int row)
Matches the cell at row/lastFoundColumn against the pattern. Called if sameRowIndex && !hasEqualRegEx. PRE: lastFoundColumn valid.- Parameters:
pattern-Patternthat we will try to matchrow- position at which we will get the value to match with the providedPattern- Returns:
- result of the match;
AbstractSearchable.SearchResult
-
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 indexcolumn- the found column- Returns:
- newly created
SearchResult
-
isValidIndex
protected boolean isValidIndex(int index)
Checks if index is in range: 0 <= index < getSize().- Parameters:
index- possible start position that we will check for validity- Returns:
trueif given parameter is valid index
-
getSize
protected abstract int getSize()
Returns the size of this searchable.- Returns:
- size of this searchable
-
updateState
protected void updateState(AbstractSearchable.SearchResult searchResult)
Updates inner searchable state based on provided search result- Parameters:
searchResult-SearchResultthat represents the new state of thisAbstractSearchable
-
moveMatchMarker
protected abstract void moveMatchMarker()
Moves the match marker according to current found state.
-
getTarget
public abstract javax.swing.JComponent getTarget()
It's the responsibility of subclasses to covariant override.- Returns:
- the target component
-
removeHighlighter
protected abstract void removeHighlighter(Highlighter searchHighlighter)
Removes the highlighter.- Parameters:
searchHighlighter- the Highlighter to remove.
-
getHighlighters
protected abstract Highlighter[] getHighlighters()
Returns the highlighters registered on the search target.- Returns:
- all registered highlighters
-
addHighlighter
protected abstract void addHighlighter(Highlighter highlighter)
Adds the highlighter to the target.- Parameters:
highlighter- the Highlighter to add.
-
ensureInsertedSearchHighlighters
protected void ensureInsertedSearchHighlighters(Highlighter highlighter)
Ensure that the given Highlighter is the last in the list of the highlighters registered on the target.- Parameters:
highlighter- the Highlighter to be inserted as last.
-
isInPipeline
private boolean isInPipeline(Highlighter searchHighlighter)
Returns a flag indicating if the given highlighter is last in the list of highlighters registered on the target. If so returns true. If not, it has the side-effect of removing the highlighter and returns false.- Parameters:
searchHighlighter- the highlighter to check for being last- Returns:
- a boolean indicating whether the highlighter is last.
-
convertColumnIndexToModel
protected int convertColumnIndexToModel(int viewColumn)
Converts and returns the given column index from view coordinates to model coordinates.This implementation returns the view coordinate, that is assumes that both coordinate systems are the same.
- Parameters:
viewColumn- the column index in view coordinates, must be a valid index in that system.- Returns:
- the column index in model coordinates.
-
hasMatch
private boolean hasMatch(AbstractSearchable.SearchResult result)
- Parameters:
result-- Returns:
trueif theresultcontains a match;falseotherwise
-
hasMatch
protected boolean hasMatch()
Returns a boolean indicating whether the current search result is a match.PENDING JW: move to SearchResult?
- Returns:
- a boolean indicating whether the current search result is a match.
-
markByHighlighter
protected boolean markByHighlighter()
Returns a boolean indicating whether a match should be marked with a Highlighter. Typically, if true, the match highlighter is used, otherwise a match is indicated by selection.This implementation returns true if the target component has a client property for key MATCH_HIGHLIGHTER with value Boolean.TRUE, false otherwise. The SearchFactory sets that client property in incremental search mode, that is when triggering a search via the JXFindBar as installed by the factory.
- Returns:
- a boolean indicating whether a match should be marked by a using a Highlighter.
- See Also:
SearchFactory
-
setMatchHighlighter
public void setMatchHighlighter(AbstractHighlighter hl)
Sets the AbstractHighlighter to use as match marker, if enabled. A null value will re-install the default.- Parameters:
hl- the Highlighter to use as match marker.
-
getMatchHighlighter
protected AbstractHighlighter getMatchHighlighter()
Returns the Hihglighter to use as match marker, lazyly created if null.- Returns:
- a highlighter used for matching, guaranteed to be not null.
-
createMatchHighlighter
protected AbstractHighlighter createMatchHighlighter()
Creates and returns the Highlighter used as match marker.- Returns:
- a highlighter used for matching
-
getConfiguredMatchHighlighter
protected AbstractHighlighter getConfiguredMatchHighlighter()
Configures and returns the match highlighter for the current match.- Returns:
- a highlighter configured for matching
-
createMatchPredicate
protected HighlightPredicate createMatchPredicate()
Creates and returns a HighlightPredicate appropriate for the current search result.- Returns:
- a HighlightPredicate appropriate for the current search result.
-
-