/** * Creates the repeat button. By default it will return a JCheckBox. Subclass class can override it to return your * own button or customize the button created by default as long as it can set underlying Searchable's repeats * property. * * @return the repeat button. */ protected AbstractButton createRepeatsButton() { AbstractButton button = new JCheckBox(getResourceString("SearchableBar.repeats")); button.setMnemonic(getResourceString("SearchableBar.repeats.mnemonic").charAt(0)); button.setRequestFocusEnabled(false); button.setFocusable(false); button.setSelected(getSearchable().isRepeats()); button.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getSource() instanceof AbstractButton) { getSearchable().setRepeats(((AbstractButton) e.getSource()).isSelected()); } } }); button.setOpaque(false); return button; }
/** * Finds the previous matching index from the cursor. * * @param s the searching text * @return the previous index that the element matches the searching text. */ public int findPrevious(String s) { String str = isCaseSensitive() ? s : s.toLowerCase(); int count = getElementCount(); if (count == 0) return s.length() > 0 ? -1 : 0; int selectedIndex = getCurrentIndex(); for (int i = selectedIndex - 1; i >= 0; i--) { Object element = getElementAt(i); if (compare(element, str)) return i; } if (isRepeats()) { for (int i = count - 1; i >= selectedIndex; i--) { Object element = getElementAt(i); if (compare(element, str)) return i; } } return selectedIndex == -1 ? -1 : (compare(getElementAt(selectedIndex), str) ? selectedIndex : -1); }
/** * Finds the next matching index from the cursor. * * @param s the searching text * @return the next index that the element matches the searching text. */ public int findNext(String s) { String str = isCaseSensitive() ? s : s.toLowerCase(); int count = getElementCount(); if (count == 0) return s.length() > 0 ? -1 : 0; int selectedIndex = getCurrentIndex(); for (int i = selectedIndex + 1; i < count; i++) { Object element = getElementAt(i); if (compare(element, str)) return i; } if (isRepeats()) { for (int i = 0; i < selectedIndex; i++) { Object element = getElementAt(i); if (compare(element, str)) return i; } } return selectedIndex == -1 ? -1 : (compare(getElementAt(selectedIndex), str) ? selectedIndex : -1); }
public void actionPerformed(ActionEvent e) { _highlightsButton.setSelected(false); String text = getSearchingText(); addSearchingTextToHistory(text); int cursor = _searchable.getSelectedIndex(); _searchable.setCursor(cursor); int found = _searchable.findPrevious(text); if (found == cursor) { select(found, text, false); clearStatus(); } else if (found != -1 && _searchable.isRepeats() && found >= cursor) { select(found, text, false); setStatus(getResourceString("SearchableBar.reachedTopRepeat"), getImageIcon(SearchableBarIconsFactory.Buttons.REPEAT)); } else if (!_searchable.isRepeats() && found == -1) { setStatus(getResourceString("SearchableBar.reachedTop"), getImageIcon(SearchableBarIconsFactory.Buttons.ERROR)); } else if (found != -1) { select(found, text, false); clearStatus(); } } };
public void actionPerformed(ActionEvent e) { _highlightsButton.setSelected(false); String text = getSearchingText(); addSearchingTextToHistory(text); int cursor = _searchable.getSelectedIndex(); _searchable.setCursor(cursor); int found = _searchable.findNext(text); if (found == cursor) { select(found, text, false); clearStatus(); } else if (found != -1 && _searchable.isRepeats() && found <= cursor) { select(found, text, false); setStatus(getResourceString("SearchableBar.reachedBottomRepeat"), getImageIcon(SearchableBarIconsFactory.Buttons.REPEAT)); } else if (!_searchable.isRepeats() && found == -1) { setStatus(getResourceString("SearchableBar.reachedBottom"), getImageIcon(SearchableBarIconsFactory.Buttons.ERROR)); } else if (found != -1) { select(found, text, false); clearStatus(); if (_searchable.getSearchingDelay() < 0) { // never updated the count so we update here highlightAllOrNext(); } } } };
boolean oldRepeats = isRepeats(); // set repeats to false and set it back later. if (oldRepeats) { setRepeats(false);
return; boolean old = _searchable.isRepeats(); _searchable.setRepeats(false); int index = _searchable.findFirst(text);