Package com.jidesoft.swing
Class TextComponentSearchable
java.lang.Object
com.jidesoft.swing.Searchable
com.jidesoft.swing.TextComponentSearchable
- All Implemented Interfaces:
PropertyChangeListener
,EventListener
,DocumentListener
public class TextComponentSearchable
extends Searchable
implements DocumentListener, PropertyChangeListener
TextComponentSearchable
is an concrete implementation of Searchable
that enables the search
function in JTextComponent. It's very simple to use it. Assuming you have a JTextComponent, all you need to do is
to call
Now the JTextComponent will have the search function.
JTextComponent textComponent = ....;
TextComponentSearchable searchable = new TextComponentSearchable(textComponent);
JTextComponent textComponent = ....;
TextComponentSearchable searchable = new ListSearchable(textComponent) {
protected String convertElementToString(Object object) {
...
}
protected boolean isActivateKey(KeyEvent e) { // change to a different activation key
return (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_F &&
(KeyEvent.CTRL_MASK & e.getModifiers()) != 0);
}
};
Additional customization can be done on the base Searchable class such as background and foreground color,
keystrokes, case sensitivity. TextComponentSearchable also has a special attribute called highlightColor. You can
change it using setHighlightColor(java.awt.Color)
.
Due to the special case of JTextComponent, the searching doesn't support wild card '*' or '?' as in other
Searchables. The other difference is JTextComponent will keep the highlights after search popup hides. If you want to
hide the highlights, just press ESC again (the first ESC will hide popup; the second ESC will hide all highlights if
any).-
Nested Class Summary
Nested classes/interfaces inherited from class com.jidesoft.swing.Searchable
Searchable.DefaultSearchPopup, Searchable.SearchField, Searchable.SearchPopup
-
Field Summary
Fields inherited from class com.jidesoft.swing.Searchable
_component, _componentListener, _focusListener, _keyListener, _matchCount, CLIENT_PROPERTY_SEARCHABLE, listenerList, PROPERTY_SEARCH_TEXT
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addHighlight
(int index, String text, boolean incremental) Adds highlight to text component at specified index and text.void
protected String
convertElementToString
(Object object) Converts the element in JTextComponent to string.int
Finds the first element that matches the searching text.int
Finds the next matching index from the cursor.int
Finds the last element that matches the searching text.int
Finds the next matching index from the cursor.int
Finds the previous matching index from the cursor.protected Object
getElementAt
(int index) Gets the element at the specified index.protected int
Gets the total element count in the component.Gets the highlight color.protected int
Gets the selected index in the component.void
Hides the popup.void
void
Installs the handler for ESC key to remove all highlightsvoid
Installs necessary listeners to the component.protected boolean
Checks if the key in KeyEvent should activate the search popup.void
protected void
Removes all highlights from the text component.void
int
Finds the previous matching index from the cursor.protected void
Actions to take on searching text empty scenariovoid
setHighlightColor
(Color highlightColor) Changes the highlight color.protected void
setSelectedIndex
(int index, boolean incremental) Sets the selected index.void
Uninstalls the handler for ESC key to remove all highlightsvoid
Uninstall the listeners that installed before.Methods inherited from class com.jidesoft.swing.Searchable
addPropertyChangeListener, addSearchableListener, adjustSelectedIndex, cancelHighlightAll, compare, compare, convertToString, createComponentListener, createFocusListener, createKeyListener, createSearchPopup, findAll, findFirstExactly, firePropertyChangeEvent, fireSearchableEvent, getBackground, getComponent, getCurrentIndex, getCursor, getElementAtAsString, getForeground, getMismatchForeground, getPopupLocation, getPopupLocationRelativeTo, getPopupTimeout, getResourceString, getSearchable, getSearchableListeners, getSearchableProvider, getSearchingDelay, getSearchingText, getSearchLabel, getWildcardSupport, highlightAll, isCaseSensitive, isCountMatch, isDeactivateKey, isFindFirstKey, isFindLastKey, isFindNextKey, isFindPreviousKey, isFromStart, isHeavyweightComponentEnabled, isHideSearchPopupOnEvent, isIncrementalSelectKey, isNavigationKey, isPopupVisible, isProcessModelChangeEvent, isRepeats, isReverseOrder, isSearchableListenerInstalled, isSelectAllKey, isWildcardEnabled, keyTypedOrPressed, removePropertyChangeListener, removeSearchableListener, select, setBackground, setCaseSensitive, setCountMatch, setCursor, setCursor, setForeground, setFromStart, setHeavyweightComponentEnabled, setHideSearchPopupOnEvent, setMismatchForeground, setPopupLocation, setPopupLocationRelativeTo, setPopupTimeout, setProcessModelChangeEvent, setRepeats, setReverseOrder, setSearchableProvider, setSearchingDelay, setSearchLabel, setWildcardEnabled, setWildcardSupport, showPopup, textChanged
-
Constructor Details
-
TextComponentSearchable
-
-
Method Details
-
uninstallHighlightsRemover
public void uninstallHighlightsRemover()Uninstalls the handler for ESC key to remove all highlights -
installHighlightsRemover
public void installHighlightsRemover()Installs the handler for ESC key to remove all highlights -
installListeners
public void installListeners()Description copied from class:Searchable
Installs necessary listeners to the component. This method will be called automatically when Searchable is created.- Overrides:
installListeners
in classSearchable
-
uninstallListeners
public void uninstallListeners()Description copied from class:Searchable
Uninstall the listeners that installed before. This method is never called because we don't have the control of the life cycle of the component. However you can call this method if you don't want the component to be searchable any more.- Overrides:
uninstallListeners
in classSearchable
-
setSelectedIndex
protected void setSelectedIndex(int index, boolean incremental) Description copied from class:Searchable
Sets the selected index. The concrete implementation should call methods on the component to select the element at the specified index. The incremental flag is used to do multiple select. If the flag is true, the element at the index should be added to current selection. If false, you should clear previous selection and then select the element.- Specified by:
setSelectedIndex
in classSearchable
- Parameters:
index
- the index to be selectedincremental
- a flag to enable multiple selection. If the flag is true, the element at the index should be added to current selection. If false, you should clear previous selection and then select the element.
-
addHighlight
protected void addHighlight(int index, String text, boolean incremental) throws BadLocationException Adds highlight to text component at specified index and text.- Parameters:
index
- the index of the text to be highlightedtext
- the text to be highlightedincremental
- if this is an incremental adding highlight- Throws:
BadLocationException
-
removeAllHighlights
protected void removeAllHighlights()Removes all highlights from the text component. -
getSelectedIndex
protected int getSelectedIndex()Description copied from class:Searchable
Gets the selected index in the component. The concrete implementation should call methods on the component to retrieve the current selected index. If the component supports multiple selection, it's OK just return the index of the first selection.Here are some examples. In the case of JList, the index is the row index. In the case of JTree, the index is the row index too. In the case of JTable, depending on the selection mode, the index could be row index (in row selection mode), could be column index (in column selection mode) or could the cell index (in cell selection mode).
- Specified by:
getSelectedIndex
in classSearchable
- Returns:
- the selected index.
-
getElementAt
Description copied from class:Searchable
Gets the element at the specified index. The element could be any data structure that internally used in the component. The convertElementToString method will give you a chance to convert the element to string which is used to compare with the string that user types in.- Specified by:
getElementAt
in classSearchable
- Parameters:
index
- the index- Returns:
- the element at the specified index.
-
getElementCount
protected int getElementCount()Description copied from class:Searchable
Gets the total element count in the component. Different concrete implementation could have different interpretation of the count. This is totally OK as long as it's consistent in all the methods. For example, the index parameter in other methods should be always a valid value within the total count.- Specified by:
getElementCount
in classSearchable
- Returns:
- the total element count.
-
convertElementToString
Converts the element in JTextComponent to string. The returned value will be thetoString()
of whatever element that returned fromlist.getModel().getElementAt(i)
.- Specified by:
convertElementToString
in classSearchable
- Parameters:
object
-- Returns:
- the string representing the element in the JTextComponent.
-
propertyChange
- Specified by:
propertyChange
in interfacePropertyChangeListener
-
insertUpdate
- Specified by:
insertUpdate
in interfaceDocumentListener
-
removeUpdate
- Specified by:
removeUpdate
in interfaceDocumentListener
-
changedUpdate
- Specified by:
changedUpdate
in interfaceDocumentListener
-
isActivateKey
Description copied from class:Searchable
Checks if the key in KeyEvent should activate the search popup.- Overrides:
isActivateKey
in classSearchable
- Parameters:
e
- the key event- Returns:
- true if the keyChar is visible except space and tab.
-
getHighlightColor
Gets the highlight color.- Returns:
- the highlight color.
-
setHighlightColor
Changes the highlight color.- Parameters:
highlightColor
-
-
findLast
Description copied from class:Searchable
Finds the last element that matches the searching text.- Overrides:
findLast
in classSearchable
- Parameters:
s
- the searching text- Returns:
- the last element that matches the searching text.
-
findFirst
Description copied from class:Searchable
Finds the first element that matches the searching text.- Overrides:
findFirst
in classSearchable
- Parameters:
s
- the searching text- Returns:
- the first element that matches with the searching text.
-
findFromCursor
Description copied from class:Searchable
Finds the next matching index from the cursor. If it reaches the end, it will restart from the beginning. However is the reverseOrder flag is true, it will finds the previous matching index from the cursor. If it reaches the beginning, it will restart from the end.- Overrides:
findFromCursor
in classSearchable
- Parameters:
s
- the searching text- Returns:
- the next index that the element matches the searching text.
-
reverseFindFromCursor
Description copied from class:Searchable
Finds the previous matching index from the cursor. If it reaches the beginning, it will restart from the end.- Overrides:
reverseFindFromCursor
in classSearchable
- Parameters:
s
- the searching text- Returns:
- the next index that the element matches the searching text.
-
findNext
Description copied from class:Searchable
Finds the next matching index from the cursor.- Overrides:
findNext
in classSearchable
- Parameters:
s
- the searching text- Returns:
- the next index that the element matches the searching text.
-
findPrevious
Description copied from class:Searchable
Finds the previous matching index from the cursor.- Overrides:
findPrevious
in classSearchable
- Parameters:
s
- the searching text- Returns:
- the previous index that the element matches the searching text.
-
hidePopup
public void hidePopup()Description copied from class:Searchable
Hides the popup.- Overrides:
hidePopup
in classSearchable
-
searchingTextEmpty
protected void searchingTextEmpty()Description copied from class:Searchable
Actions to take on searching text empty scenario- Overrides:
searchingTextEmpty
in classSearchable
-