Package org.jdesktop.swingx.search
Class RecentSearches
- java.lang.Object
-
- org.jdesktop.swingx.search.RecentSearches
-
- All Implemented Interfaces:
java.awt.event.ActionListener,java.util.EventListener
public class RecentSearches extends java.lang.Object implements java.awt.event.ActionListenerMaintains a list of recent searches and persists this list automatically usingPreferences. A recent searches popup menu can be installed on aJXSearchFieldusing#install(JXSearchField).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRecentSearches.RecentSearchesPopupThe popup menu returned byRecentSearches#createPopupMenu(JXSearchField).
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<javax.swing.event.ChangeListener>listenersprivate intmaxRecentsprivate java.util.prefs.PreferencesprefsNodeprivate java.util.List<java.lang.String>recentSearches
-
Constructor Summary
Constructors Constructor Description RecentSearches(java.lang.String saveName)Creates a list of recent searches and usessaveNameto persist this list under thePreferencesuser root node.RecentSearches(java.util.prefs.Preferences prefs, java.lang.String saveName)Creates a list of recent searches and usessaveNameto persist this list under theprefsnode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactionPerformed(java.awt.event.ActionEvent e)Callsput(String)with theActionEvents action command as the search string.voidaddChangeListener(javax.swing.event.ChangeListener l)Add a change listener.protected javax.swing.JPopupMenucreatePopupMenu(javax.swing.JTextField searchField)Creates the recent searches popup menu which will be used by#install(JXSearchField)to set a search popup menu onsearchField.private voidfireChangeEvent()javax.swing.event.ChangeListener[]getChangeListeners()Returns all registeredChangeListeners.intgetLength()The number of recent searches.intgetMaxRecents()Returns the maximum number of recent searches.java.lang.String[]getRecentSearches()Returns all recent searches in this list.voidinstall(javax.swing.JTextField searchField)Install a recent the searches popup menu returned by#createPopupMenu(JXSearchField)onsearchField.private voidload()voidput(java.lang.String searchString)Add a search string as the first element.voidremoveAll()Remove all recent searches.voidremoveChangeListener(javax.swing.event.ChangeListener l)Remove a change listener.private voidsave()voidsetMaxRecents(int maxRecents)Set the maximum number of recent searches.voiduninstall(JXSearchField searchField)Remove the recent searches popup fromsearchFieldwhen installed and stop listening forActionEvents fired by the search field.
-
-
-
Constructor Detail
-
RecentSearches
public RecentSearches(java.lang.String saveName)
Creates a list of recent searches and usessaveNameto persist this list under thePreferencesuser root node. Existing entries will be loaded automatically.- Parameters:
saveName- a unique name for saving this list of recent searches
-
RecentSearches
public RecentSearches(java.util.prefs.Preferences prefs, java.lang.String saveName)Creates a list of recent searches and usessaveNameto persist this list under theprefsnode. Existing entries will be loaded automatically.- Parameters:
prefsNode- the preferences node under which this list will be persisted. If prefsNode isnullthe preferences node will be set to the user root nodesaveName- a unique name for saving this list of recent searches. If saveName isnull, the list will not be persisted
-
-
Method Detail
-
load
private void load()
-
save
private void save()
-
put
public void put(java.lang.String searchString)
Add a search string as the first element. If the search string isnullor empty nothing will be added. If the search string already exists, the old element will be removed. The modified list will automatically be persisted. If the number of elements exceeds the maximum number of entries, the last entry will be removed.- Parameters:
searchString- the search string to add- See Also:
getMaxRecents()
-
getRecentSearches
public java.lang.String[] getRecentSearches()
Returns all recent searches in this list.- Returns:
- the recent searches
-
getLength
public int getLength()
The number of recent searches.- Returns:
- number of recent searches
-
removeAll
public void removeAll()
Remove all recent searches.
-
getMaxRecents
public int getMaxRecents()
Returns the maximum number of recent searches.- Returns:
- the maximum number of recent searches
- See Also:
put(String)
-
setMaxRecents
public void setMaxRecents(int maxRecents)
Set the maximum number of recent searches.- Parameters:
maxRecents- maximum number of recent searches- See Also:
put(String)
-
addChangeListener
public void addChangeListener(javax.swing.event.ChangeListener l)
Add a change listener. AChangeEventwill be fired whenever a search is added or removed.- Parameters:
l- theChangeListener
-
removeChangeListener
public void removeChangeListener(javax.swing.event.ChangeListener l)
Remove a change listener.- Parameters:
l- a registeredChangeListener
-
getChangeListeners
public javax.swing.event.ChangeListener[] getChangeListeners()
Returns all registeredChangeListeners.- Returns:
- all registered
ChangeListeners
-
fireChangeEvent
private void fireChangeEvent()
-
createPopupMenu
protected javax.swing.JPopupMenu createPopupMenu(javax.swing.JTextField searchField)
Creates the recent searches popup menu which will be used by#install(JXSearchField)to set a search popup menu onsearchField. Override to return a custom popup menu.- Parameters:
searchField- the search field the returned popup menu will be installed on- Returns:
- the recent searches popup menu
-
install
public void install(javax.swing.JTextField searchField)
Install a recent the searches popup menu returned by#createPopupMenu(JXSearchField)onsearchField. Also registers anActionListeneronsearchFieldand adds the search string to the list of recent searches whenever aActionEventis received. UsesNativeSearchFieldSupportto achieve compatibility with the native search field support provided by the Mac Look And Feel since Mac OS 10.5.- Parameters:
searchField- the search field to install a recent searches popup menu on
-
uninstall
public void uninstall(JXSearchField searchField)
Remove the recent searches popup fromsearchFieldwhen installed and stop listening forActionEvents fired by the search field.- Parameters:
searchField- uninstall recent searches popup menu
-
actionPerformed
public void actionPerformed(java.awt.event.ActionEvent e)
Callsput(String)with theActionEvents action command as the search string.- Specified by:
actionPerformedin interfacejava.awt.event.ActionListener
-
-