/** * Add a search string as the first element. If the search string is * <code>null</code> or 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. * * @see #getMaxRecents() * @param searchString * the search string to add */ public void put(String searchString) { if (searchString == null || searchString.trim().length() == 0) { return; } int lastIndex = recentSearches.indexOf(searchString); if (lastIndex != -1) { recentSearches.remove(lastIndex); } recentSearches.add(0, searchString); if (getLength() > getMaxRecents()) { recentSearches.remove(recentSearches.size() - 1); } save(); fireChangeEvent(); }
/** * Calls {@link #put(String)} with the {@link ActionEvent}s action command * as the search string. */ @Override public void actionPerformed(ActionEvent e) { put(e.getActionCommand()); }
/** * Remove all recent searches. */ public void removeAll() { recentSearches.clear(); save(); fireChangeEvent(); }
/** * Installs and manages a recent searches popup menu as the find popup menu, * if <code>recentSearchesSaveKey</code> is not null. Otherwise, removes * the popup menu and stops managing recent searches. * * @see #setFindAction(ActionListener) * @see #isManagingRecentSearches() * @see RecentSearches * * @param recentSearchesSaveKey * this key is used to persist the recent searches. */ public void setRecentSearchesSaveKey(String recentSearchesSaveKey) { String oldName = getRecentSearchesSaveKey(); this.recentSearchesSaveKey = recentSearchesSaveKey; if (recentSearches != null) { // set null before uninstalling. otherwise the popup menu is not // allowed to be changed. RecentSearches rs = recentSearches; recentSearches = null; rs.uninstall(this); } if (isValidRecentSearchesKey(recentSearchesSaveKey)) { recentSearches = new RecentSearches(recentSearchesSaveKey); recentSearches.install(this); } firePropertyChange("recentSearchesSaveKey", oldName, this.recentSearchesSaveKey); }
/** * Rebuilds the menu according to the recent searches. */ private void buildMenu() { setVisible(false); removeAll(); if (recentSearches.getLength() == 0) { JMenuItem noRecent = new JMenuItem(UIManagerExt.getString("SearchField.noRecentsText")); noRecent.setEnabled(false); add(noRecent); } else { JMenuItem recent = new JMenuItem(UIManagerExt.getString("SearchField.recentsMenuTitle")); recent.setEnabled(false); add(recent); for (String searchString : recentSearches.getRecentSearches()) { JMenuItem mi = new JMenuItem(searchString); mi.addActionListener(this); add(mi); } addSeparator(); clear = new JMenuItem(UIManagerExt.getString("SearchField.clearRecentsText")); clear.addActionListener(this); add(clear); } }
/** * Creates a new popup menu based on the given {@link RecentSearches} * and {@link JXSearchField}. * * @param recentSearches * @param searchField */ public RecentSearchesPopup(RecentSearches recentSearches, JTextField searchField) { this.searchField = searchField; this.recentSearches = recentSearches; recentSearches.addChangeListener(this); buildMenu(); }
/** * Install a recent the searches popup menu returned by * {@link #createPopupMenu(JXSearchField)} on <code>searchField</code>. * Also registers an {@link ActionListener} on <code>searchField</code> * and adds the search string to the list of recent searches whenever a * {@link ActionEvent} is received. * * Uses {@link NativeSearchFieldSupport} to achieve compatibility with the native * search field support provided by the Mac Look And Feel since Mac OS 10.5. * * @param searchField * the search field to install a recent searches popup menu on */ public void install(JTextField searchField) { searchField.addActionListener(this); NativeSearchFieldSupport.setFindPopupMenu(searchField, createPopupMenu(searchField)); }
/** * Creates a list of recent searches and uses <code>saveName</code> to * persist this list under the <code>prefs</code> node. Existing entries * will be loaded automatically. * * @param prefsNode * the preferences node under which this list will be persisted. * If prefsNode is <code>null</code> the preferences node will * be set to the user root node * @param saveName * a unique name for saving this list of recent searches. If * saveName is <code>null</code>, the list will not be * persisted */ public RecentSearches(Preferences prefs, String saveName) { if (prefs == null) { try { prefs = Preferences.userRoot(); } catch (AccessControlException ace) { // disable persistency, if we aren't allowed to access // preferences. Logger.getLogger(getClass().getName()).warning("cannot acces preferences. persistency disabled."); } } if (prefs != null && saveName != null) { this.prefsNode = prefs.node(saveName); load(); } }
/** * Installs and manages a recent searches popup menu as the find popup menu, * if <code>recentSearchesSaveKey</code> is not null. Otherwise, removes * the popup menu and stops managing recent searches. * * @see #setFindAction(ActionListener) * @see #isManagingRecentSearches() * @see RecentSearches * * @param recentSearchesSaveKey * this key is used to persist the recent searches. */ public void setRecentSearchesSaveKey(String recentSearchesSaveKey) { String oldName = getRecentSearchesSaveKey(); this.recentSearchesSaveKey = recentSearchesSaveKey; if (recentSearches != null) { // set null before uninstalling. otherwise the popup menu is not // allowed to be changed. RecentSearches rs = recentSearches; recentSearches = null; rs.uninstall(this); } if (isValidRecentSearchesKey(recentSearchesSaveKey)) { recentSearches = new RecentSearches(recentSearchesSaveKey); recentSearches.install(this); } firePropertyChange("recentSearchesSaveKey", oldName, this.recentSearchesSaveKey); }
/** * Remove all recent searches. */ public void removeAll() { recentSearches.clear(); save(); fireChangeEvent(); }
/** * Rebuilds the menu according to the recent searches. */ private void buildMenu() { setVisible(false); removeAll(); if (recentSearches.getLength() == 0) { JMenuItem noRecent = new JMenuItem(UIManagerExt.getString("SearchField.noRecentsText")); noRecent.setEnabled(false); add(noRecent); } else { JMenuItem recent = new JMenuItem(UIManagerExt.getString("SearchField.recentsMenuTitle")); recent.setEnabled(false); add(recent); for (String searchString : recentSearches.getRecentSearches()) { JMenuItem mi = new JMenuItem(searchString); mi.addActionListener(this); add(mi); } addSeparator(); clear = new JMenuItem(UIManagerExt.getString("SearchField.clearRecentsText")); clear.addActionListener(this); add(clear); } }
/** * Creates a new popup menu based on the given {@link RecentSearches} * and {@link JXSearchField}. * * @param recentSearches * @param searchField */ public RecentSearchesPopup(RecentSearches recentSearches, JTextField searchField) { this.searchField = searchField; this.recentSearches = recentSearches; recentSearches.addChangeListener(this); buildMenu(); }
/** * Install a recent the searches popup menu returned by * {@link #createPopupMenu(JXSearchField)} on <code>searchField</code>. * Also registers an {@link ActionListener} on <code>searchField</code> * and adds the search string to the list of recent searches whenever a * {@link ActionEvent} is received. * * Uses {@link NativeSearchFieldSupport} to achieve compatibility with the native * search field support provided by the Mac Look And Feel since Mac OS 10.5. * * @param searchField * the search field to install a recent searches popup menu on */ public void install(JTextField searchField) { searchField.addActionListener(this); NativeSearchFieldSupport.setFindPopupMenu(searchField, createPopupMenu(searchField)); }
/** * Creates a list of recent searches and uses <code>saveName</code> to * persist this list under the <code>prefs</code> node. Existing entries * will be loaded automatically. * * @param prefsNode * the preferences node under which this list will be persisted. * If prefsNode is <code>null</code> the preferences node will * be set to the user root node * @param saveName * a unique name for saving this list of recent searches. If * saveName is <code>null</code>, the list will not be * persisted */ public RecentSearches(Preferences prefs, String saveName) { if (prefs == null) { try { prefs = Preferences.userRoot(); } catch (AccessControlException ace) { // disable persistency, if we aren't allowed to access // preferences. Logger.getLogger(getClass().getName()).warning("cannot acces preferences. persistency disabled."); } } if (prefs != null && saveName != null) { this.prefsNode = prefs.node(saveName); load(); } }
/** * Add a search string as the first element. If the search string is * <code>null</code> or 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. * * @see #getMaxRecents() * @param searchString * the search string to add */ public void put(String searchString) { if (searchString == null || searchString.trim().length() == 0) { return; } int lastIndex = recentSearches.indexOf(searchString); if (lastIndex != -1) { recentSearches.remove(lastIndex); } recentSearches.add(0, searchString); if (getLength() > getMaxRecents()) { recentSearches.remove(recentSearches.size() - 1); } save(); fireChangeEvent(); }
/** * Installs and manages a recent searches popup menu as the find popup menu, * if <code>recentSearchesSaveKey</code> is not null. Otherwise, removes * the popup menu and stops managing recent searches. * * @see #setFindAction(ActionListener) * @see #isManagingRecentSearches() * @see RecentSearches * * @param recentSearchesSaveKey * this key is used to persist the recent searches. */ public void setRecentSearchesSaveKey(String recentSearchesSaveKey) { String oldName = getRecentSearchesSaveKey(); this.recentSearchesSaveKey = recentSearchesSaveKey; if (recentSearches != null) { // set null before uninstalling. otherwise the popup menu is not // allowed to be changed. RecentSearches rs = recentSearches; recentSearches = null; rs.uninstall(this); } if (isValidRecentSearchesKey(recentSearchesSaveKey)) { recentSearches = new RecentSearches(recentSearchesSaveKey); recentSearches.install(this); } firePropertyChange("recentSearchesSaveKey", oldName, this.recentSearchesSaveKey); }
/** * Remove all recent searches. */ public void removeAll() { recentSearches.clear(); save(); fireChangeEvent(); }
/** * Rebuilds the menu according to the recent searches. */ private void buildMenu() { setVisible(false); removeAll(); if (recentSearches.getLength() == 0) { JMenuItem noRecent = new JMenuItem(UIManagerExt.getString("SearchField.noRecentsText")); noRecent.setEnabled(false); add(noRecent); } else { JMenuItem recent = new JMenuItem(UIManagerExt.getString("SearchField.recentsMenuTitle")); recent.setEnabled(false); add(recent); for (String searchString : recentSearches.getRecentSearches()) { JMenuItem mi = new JMenuItem(searchString); mi.addActionListener(this); add(mi); } addSeparator(); clear = new JMenuItem(UIManagerExt.getString("SearchField.clearRecentsText")); clear.addActionListener(this); add(clear); } }
/** * Calls {@link #put(String)} with the {@link ActionEvent}s action command * as the search string. */ @Override public void actionPerformed(ActionEvent e) { put(e.getActionCommand()); }
/** * Creates a new popup menu based on the given {@link RecentSearches} * and {@link JXSearchField}. * * @param recentSearches * @param searchField */ public RecentSearchesPopup(RecentSearches recentSearches, JTextField searchField) { this.searchField = searchField; this.recentSearches = recentSearches; recentSearches.addChangeListener(this); buildMenu(); }