|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.jidesoft.swing.Searchable
public abstract class Searchable
JList, JTable and JTree are three data-rich components. They can be used to display a huge amount of data so
searching function will be very a useful feature in those components. Searchable
is such a class that
can make JList, JTable and JTree searchable. User can simply type in any string they want to search for and use arrow
keys to navigate to next or previous occurrence.
Searchable
is a base abstract class. ListSearchable
, TableSearchable
and
TreeSearchable
are implementations to make JList, JTable and JTree searchable respectively. For each
implementation, there are five methods need to be implemented. protected abstract int
getSelectedIndex()
protected abstract void setSelectedIndex(int index, boolean incremental)
protected abstract int getElementCount()
protected abstract Object getElementAt(int
index)
protected abstract String convertElementToString(Object element)
isActivateKey(java.awt.event.KeyEvent)
, isDeactivateKey(java.awt.event.KeyEvent)
, isFindFirstKey(java.awt.event.KeyEvent)
,isFindLastKey(java.awt.event.KeyEvent)
, isFindNextKey(java.awt.event.KeyEvent)
, isFindPreviousKey(java.awt.event.KeyEvent)
to provide its own set
of keys.
In addition to press up/down arrow to find next occurrence or previous occurrence of particular string, there are
several other features that are very handy.
Multiple selection feature - If you press CTRL key and hold it while pressing up and down arrow, it will find
next/previous occurrence while keeping existing selections. Searchable
. All you need to do is to override compare(String, String)
method and implement by
yourself.
As this is an abstract class, please refer to to javadoc of ListSearchable
,TreeSearchable
, and TableSearchable
to find out how to use it with JList, JTree and JTable respectively.
This component has a timer. If user types very fast, it will accumulate them together and generate only one searching
action. The timer can be controlled by setSearchingDelay(int)
.
By default we will use lightweight popup for the sake of performance. But if you use heavyweight component which
could obscure the lightweight popup, you can call setHeavyweightComponentEnabled(boolean)
to true so that
heavyweight popup will be used.
When a Searchable
is installed on a component, component.getClientProperty(Searchable.CLIENT_PROPERTY_SEARCHABLE)
will give you the Searchable instance. You can use static method getSearchable(javax.swing.JComponent)
to
get it too.
Last but not the least, only one Searchable is allowed on a component. If you install another one, it will remove the
first one and then install the new one.
Nested Class Summary | |
---|---|
protected class |
Searchable.SearchField
A text field for searching text. |
class |
Searchable.SearchPopup
|
Field Summary | |
---|---|
protected JComponent |
_component
|
protected ComponentListener |
_componentListener
|
protected FocusListener |
_focusListener
|
protected KeyListener |
_keyListener
|
protected int |
_matchCount
|
static String |
CLIENT_PROPERTY_SEARCHABLE
The client property for Searchable instance. |
protected EventListenerList |
listenerList
A list of event listeners for this component. |
static String |
PROPERTY_SEARCH_TEXT
|
Constructor Summary | |
---|---|
Searchable(JComponent component)
Creates a Searchable. |
|
Searchable(JComponent component,
SearchableProvider searchableProvider)
Creates a Searchable. |
Method Summary | |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener propertychangelistener)
Adds the property change listener. |
void |
addSearchableListener(SearchableListener l)
Adds the specified listener to receive searchable events from this searchable. |
void |
adjustSelectedIndex(int index,
boolean incremental)
Sets the selected index. |
protected void |
cancelHighlightAll()
Cancel highlight all. |
protected boolean |
compare(Object element,
String searchingText)
Checks if the element matches the searching text. |
protected boolean |
compare(String text,
String searchingText)
Checks if the element string matches the searching text. |
protected abstract String |
convertElementToString(Object element)
Converts the element that returns from getElementAt() to string. |
String |
convertToString(Object element)
Converts the element to String. |
protected ComponentListener |
createComponentListener()
Creates a component listener that updates the popup when component is hidden, moved or resized. |
protected FocusListener |
createFocusListener()
Creates a FocusListener. |
protected KeyListener |
createKeyListener()
Creates the KeyListener and listen to key typed in the component. |
protected Searchable.SearchPopup |
createSearchPopup(String searchingText)
Creates the popup to hold the searching text. |
List<Integer> |
findAll(String s)
findAll uses the Searchable to find all the element indices that match the searching string. |
int |
findFirst(String s)
Finds the first element that matches the searching text. |
int |
findFromCursor(String s)
Finds the next matching index from the cursor. |
int |
findLast(String s)
Finds the last element that matches the searching text. |
int |
findNext(String s)
Finds the next matching index from the cursor. |
int |
findPrevious(String s)
Finds the previous matching index from the cursor. |
void |
firePropertyChangeEvent(String searchingText)
|
protected void |
fireSearchableEvent(SearchableEvent e)
Fires a searchable event. |
Color |
getBackground()
Gets the background color used inn the search popup. |
Component |
getComponent()
Gets the actual component which installed this Searchable. |
protected int |
getCurrentIndex()
|
int |
getCursor()
Gets the cursor which is the index of current location when searching. |
protected abstract Object |
getElementAt(int index)
Gets the element at the specified index. |
String |
getElementAtAsString(int index)
Gets the element at the specified index as string using convertElementToString(Object) method. |
protected abstract int |
getElementCount()
Gets the total element count in the component. |
Color |
getForeground()
Gets the foreground color used inn the search popup. |
Color |
getMismatchForeground()
Gets the foreground color when the searching text doesn't match with any of the elements in the component. |
int |
getPopupLocation()
Gets the popup location. |
Component |
getPopupLocationRelativeTo()
Gets the component that the location of the popup relative to. |
int |
getPopupTimeout()
Gets the timeout for showing the popup. |
protected String |
getResourceString(String key)
Gets the localized string from resource bundle. |
static Searchable |
getSearchable(JComponent component)
Gets the Searchable installed on the component. |
SearchableListener[] |
getSearchableListeners()
Returns an array of all the SearchableListener s added to this SearchableGroup with
addSearchableListener . |
SearchableProvider |
getSearchableProvider()
|
int |
getSearchingDelay()
If it returns a positive number, it will wait for that many ms before doing the search. |
String |
getSearchingText()
Gets the searching text. |
String |
getSearchLabel()
Gets the current text that appears in the search popup. |
protected abstract int |
getSelectedIndex()
Gets the selected index in the component. |
WildcardSupport |
getWildcardSupport()
Gets the WildcardSupport. |
void |
hidePopup()
Hides the popup. |
protected void |
highlightAll()
Highlight all matching cases in the target. |
void |
installListeners()
Installs necessary listeners to the component. |
protected boolean |
isActivateKey(KeyEvent e)
Checks if the key in KeyEvent should activate the search popup. |
boolean |
isCaseSensitive()
Checks if the case is sensitive during searching. |
boolean |
isCountMatch()
Gets the flag indicating if the Searchable should count all matches for every search. |
protected boolean |
isDeactivateKey(KeyEvent e)
Checks if the key in KeyEvent should hide the search popup. |
protected boolean |
isFindFirstKey(KeyEvent e)
Checks if the key is used as a key to find the first occurrence. |
protected boolean |
isFindLastKey(KeyEvent e)
Checks if the key is used as a key to find the last occurrence. |
protected boolean |
isFindNextKey(KeyEvent e)
Checks if the key is used as a key to find the next occurrence. |
protected boolean |
isFindPreviousKey(KeyEvent e)
Checks if the key is used as a key to find the previous occurrence. |
boolean |
isFromStart()
This is a property of how to compare searching text with the data. |
boolean |
isHeavyweightComponentEnabled()
|
boolean |
isHideSearchPopupOnEvent()
Get the flag indicating if the search popup should be hidden on the component's event. |
protected boolean |
isIncrementalSelectKey(KeyEvent e)
Checks if the key will trigger incremental selection. |
protected boolean |
isNavigationKey(KeyEvent e)
Checks if the key is used as a navigation key. |
boolean |
isPopupVisible()
Check if the searchable popup is visible. |
boolean |
isProcessModelChangeEvent()
Get the flag if we should process model change event. |
boolean |
isRepeats()
Checks if restart from the beginning when searching reaches the end or restart from the end when reaches beginning. |
boolean |
isReverseOrder()
Checks the searching order. |
boolean |
isSearchableListenerInstalled(SearchableListener l)
Returns if a given listener is already installed. |
protected boolean |
isSelectAllKey(KeyEvent e)
Checks if the key will trigger selecting all. |
boolean |
isWildcardEnabled()
Checks if it supports wildcard in searching text. |
protected void |
keyTypedOrPressed(KeyEvent e)
This method is called when a key is typed or pressed. |
void |
removePropertyChangeListener(PropertyChangeListener propertychangelistener)
Removes the property change listener. |
void |
removeSearchableListener(SearchableListener l)
Removes the specified searchable listener so that it no longer receives searchable events. |
int |
reverseFindFromCursor(String s)
Finds the previous matching index from the cursor. |
protected void |
searchingTextEmpty()
Actions to take on searching text empty scenario |
protected void |
select(int index,
String searchingText)
Select the index for the searching text. |
void |
setBackground(Color background)
Sets the background color used by popup. |
void |
setCaseSensitive(boolean caseSensitive)
Sets the case sensitive flag. |
void |
setCountMatch(boolean countMatch)
Sets the flag indicating if the Searchable should count all matches for every search. |
void |
setCursor(int cursor)
Sets the cursor which is the index of current location when searching. |
void |
setCursor(int cursor,
boolean incremental)
Sets the cursor which is the index of current location when searching. |
void |
setForeground(Color foreground)
Sets the foreground color used by popup. |
void |
setFromStart(boolean fromStart)
Sets the fromStart property. |
void |
setHeavyweightComponentEnabled(boolean heavyweightComponentEnabled)
|
void |
setHideSearchPopupOnEvent(boolean hideSearchPopupOnEvent)
Set the flag indicating if the search popup should be hidden on the component's event. |
void |
setMismatchForeground(Color mismatchForeground)
Sets the foreground for mismatch. |
void |
setPopupLocation(int popupLocation)
Sets the popup location. |
void |
setPopupLocationRelativeTo(Component popupLocationRelativeTo)
Sets the location of the popup relative to the specified component. |
void |
setPopupTimeout(int popupTimeout)
Sets the timeout for showing the popup. |
void |
setProcessModelChangeEvent(boolean processModelChangeEvent)
Set the flag if we should process model change event. |
void |
setRepeats(boolean repeats)
Sets the repeat flag. |
void |
setReverseOrder(boolean reverseOrder)
Sets the searching order. |
void |
setSearchableProvider(SearchableProvider searchableProvider)
|
void |
setSearchingDelay(int searchingDelay)
If this flag is set to a positive number, it will wait for that many ms before doing the search. |
void |
setSearchLabel(String searchLabel)
Sets the text that appears in the search popup. |
protected abstract void |
setSelectedIndex(int index,
boolean incremental)
Sets the selected index. |
void |
setWildcardEnabled(boolean wildcardEnabled)
Enable or disable the usage of wildcard. |
void |
setWildcardSupport(WildcardSupport wildcardSupport)
Sets the WildcardSupport. |
void |
showPopup(String searchingText)
Shows the search popup. |
protected void |
textChanged(String text)
|
void |
uninstallListeners()
Uninstall the listeners that installed before. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final JComponent _component
protected int _matchCount
protected ComponentListener _componentListener
protected KeyListener _keyListener
protected FocusListener _focusListener
public static final String PROPERTY_SEARCH_TEXT
protected EventListenerList listenerList
public static final String CLIENT_PROPERTY_SEARCHABLE
Constructor Detail |
---|
public Searchable(JComponent component)
component
- component where the Searchable will be installed.public Searchable(JComponent component, SearchableProvider searchableProvider)
component
- component where the Searchable will be installed.searchableProvider
- the Searchable Provider.Method Detail |
---|
protected abstract int getSelectedIndex()
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).
protected abstract void setSelectedIndex(int index, boolean incremental)
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.public void adjustSelectedIndex(int index, boolean incremental)
setSelectedIndex(int, boolean)
.
Please do NOT try to override this method. Always override setSelectedIndex(int, boolean)
instead.
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.protected abstract int getElementCount()
protected abstract Object getElementAt(int index)
index
- the index
protected abstract String convertElementToString(Object element)
element
- the element to be converted
public String convertToString(Object element)
convertElementToString(Object)
only. This method is added to provide a public
method for ShrinkSearchSupport without breaking the existing code of the customers.
element
- the element to be converted
public boolean isHideSearchPopupOnEvent()
public void setHideSearchPopupOnEvent(boolean hideSearchPopupOnEvent)
hideSearchPopupOnEvent
- the flagisHideSearchPopupOnEvent()
public void hidePopup()
public SearchableProvider getSearchableProvider()
public void setSearchableProvider(SearchableProvider searchableProvider)
public void installListeners()
protected ComponentListener createComponentListener()
protected KeyListener createKeyListener()
protected FocusListener createFocusListener()
public void uninstallListeners()
public void addPropertyChangeListener(PropertyChangeListener propertychangelistener)
propertychangelistener
- the listenerpublic void removePropertyChangeListener(PropertyChangeListener propertychangelistener)
propertychangelistener
- the listenerpublic void firePropertyChangeEvent(String searchingText)
protected void searchingTextEmpty()
protected boolean compare(Object element, String searchingText)
element
- the element to be checkedsearchingText
- the searching text
protected boolean compare(String text, String searchingText)
compare(Object, String)
, this
method is after the element has been converted to string using convertElementToString(Object)
.
text
- the text to be checkedsearchingText
- the searching text
public int getCursor()
public void setCursor(int cursor)
cursor
- the new position of the cursor.public void setCursor(int cursor, boolean incremental)
setSelectedIndex(int, boolean)
method from your code, you should call this method with the same parameters.
cursor
- the new position of the cursor.incremental
- 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.protected void highlightAll()
protected void cancelHighlightAll()
highlightAll()
, you may want to override
this method to notify your Searchable that the highlightAll button is to be released.
protected void select(int index, String searchingText)
index
- the start offsetsearchingText
- the searching text presented in the searchable event to be fired here.public int findNext(String s)
s
- the searching text
protected int getCurrentIndex()
public int findPrevious(String s)
s
- the searching text
public int findFromCursor(String s)
s
- the searching text
public int reverseFindFromCursor(String s)
s
- the searching text
public int findFirst(String s)
s
- the searching text
public int findLast(String s)
s
- the searching text
protected void keyTypedOrPressed(KeyEvent e)
e
- the KeyEvent.public void showPopup(String searchingText)
searchingText
- the searching textprotected Searchable.SearchPopup createSearchPopup(String searchingText)
searchingText
- the searching text
public String getSearchingText()
protected boolean isFindFirstKey(KeyEvent e)
e
- the key event
protected boolean isFindLastKey(KeyEvent e)
e
- the key event
protected boolean isFindPreviousKey(KeyEvent e)
e
- the key event
protected boolean isFindNextKey(KeyEvent e)
e
- the key event
protected boolean isNavigationKey(KeyEvent e)
e
- the key event
protected boolean isActivateKey(KeyEvent e)
e
- the key event
protected boolean isDeactivateKey(KeyEvent e)
isNavigationKey(java.awt.event.KeyEvent)
return false), the popup will be
hidden.
e
- the key event
protected boolean isSelectAllKey(KeyEvent e)
e
- the key event
protected boolean isIncrementalSelectKey(KeyEvent e)
e
- the key event
public Color getMismatchForeground()
setMismatchForeground(java.awt.Color)
. red
color will be used.public void setMismatchForeground(Color mismatchForeground)
mismatchForeground
- mismatch forgroundpublic boolean isCaseSensitive()
public void setCaseSensitive(boolean caseSensitive)
caseSensitive
- the flag if searching is case sensitivepublic int getSearchingDelay()
public void setSearchingDelay(int searchingDelay)
searchingDelay
- the number of ms delay before searching start.public boolean isRepeats()
public void setRepeats(boolean repeats)
repeats
- the repeat flagpublic Color getForeground()
public void setForeground(Color foreground)
foreground
- the foregroundpublic Color getBackground()
public void setBackground(Color background)
background
- the backgroundpublic boolean isWildcardEnabled()
public void setWildcardEnabled(boolean wildcardEnabled)
wildcardEnabled
- the flag if wildcard is enabledisWildcardEnabled()
public WildcardSupport getWildcardSupport()
DefaultWildcardSupport
will be used.
public void setWildcardSupport(WildcardSupport wildcardSupport)
wildcardSupport
- the new WildCardSupport.public String getSearchLabel()
public void setSearchLabel(String searchLabel)
searchLabel
- the search labelpublic void addSearchableListener(SearchableListener l)
l
- the searchable listenerpublic void removeSearchableListener(SearchableListener l)
l
- the searchable listenerpublic SearchableListener[] getSearchableListeners()
SearchableListener
s added to this SearchableGroup
with
addSearchableListener
.
SearchableListener
s added or an empty array if no listeners have been addedaddSearchableListener(com.jidesoft.swing.event.SearchableListener)
public boolean isSearchableListenerInstalled(SearchableListener l)
l
- the listener
protected void fireSearchableEvent(SearchableEvent e)
e
- the eventpublic Component getComponent()
public int getPopupLocation()
SwingConstants.TOP
or SwingConstants.BOTTOM
.
public void setPopupLocation(int popupLocation)
popupLocation
- the popup location. The valid values are either SwingConstants.TOP
or SwingConstants.BOTTOM
.public boolean isReverseOrder()
public void setReverseOrder(boolean reverseOrder)
reverseOrder
- the flag if searching from top to bottom or from bottom to topprotected String getResourceString(String key)
key
- the resource string key
public boolean isPopupVisible()
public boolean isHeavyweightComponentEnabled()
public void setHeavyweightComponentEnabled(boolean heavyweightComponentEnabled)
public Component getPopupLocationRelativeTo()
public void setPopupLocationRelativeTo(Component popupLocationRelativeTo)
getPopupLocation()
. If you never set, we will use the searchable component or its scroll pane (if exists) as
the popupLocationRelativeTo component.
popupLocationRelativeTo
- the relative componentpublic boolean isFromStart()
String.startsWith(String)
to do the comparison. Otherwise, it will use String.indexOf(String)
to do the
comparison.
public void setFromStart(boolean fromStart)
fromStart
- true if the comparison matches from the start of the text only. Otherwise false. The difference
is if true, it will use String's startWith
method to match. If false, it will use
indedxOf
method.public static Searchable getSearchable(JComponent component)
component
- the component
public boolean isProcessModelChangeEvent()
ListShrinkSearchableSupport
case, since we will fire this event while applying filters. This flag
will be switched to false before we fire the event and set it back to true.
In normal case, please do not set this flag.
public void setProcessModelChangeEvent(boolean processModelChangeEvent)
processModelChangeEvent
- the flagisProcessModelChangeEvent()
public int getPopupTimeout()
setPopupTimeout(int)
public void setPopupTimeout(int popupTimeout)
popupTimeout
- the timeout in millisecondspublic boolean isCountMatch()
setCountMatch(boolean)
public void setCountMatch(boolean countMatch)
countMatch
- the flagpublic List<Integer> findAll(String s)
findAll
uses the Searchable to find all the element indices that match the searching string.
s
- the searching string.
public String getElementAtAsString(int index)
convertElementToString(Object)
method.
index
- the index.
protected void textChanged(String text)
|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |