Skip to content

Commit 5d4ed59

Browse files
committed
Refactor up() and down() methods to be less messy
1 parent a0d6863 commit 5d4ed59

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/main/java/org/scijava/ui/swing/search/SwingSearchBar.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,31 +391,29 @@ private void update(final SearchEvent event) {
391391
}
392392

393393
private void up() {
394-
assertDispatchThread();
395-
final int rowCount = resultsList.getModel().getSize();
396-
if (rowCount == 0) return;
397-
// Move upward in the list one element at a time, skipping headers.
398-
int index = resultsList.getSelectedIndex();
399-
do {
400-
index = (index + rowCount - 1) % rowCount;
401-
}
402-
while (isHeader(result(index)));
403-
select(index);
394+
select(index -> (index + rowCount() - 1) % rowCount());
404395
}
405396

406397
private void down() {
398+
select(index -> (index + 1) % rowCount());
399+
}
400+
401+
private void select(final Function<Integer, Integer> stepper) {
407402
assertDispatchThread();
408-
final int rowCount = resultsList.getModel().getSize();
409-
if (rowCount == 0) return;
410-
// Move downward in the list one element at a time, skipping headers.
403+
if (rowCount() == 0) return;
404+
// Step through the list, skipping headers.
411405
int index = resultsList.getSelectedIndex();
412406
do {
413-
index = (index + 1) % rowCount;
407+
index = stepper.apply(index);
414408
}
415409
while (isHeader(result(index)));
416410
select(index);
417411
}
418412

413+
private int rowCount() {
414+
return resultsList.getModel().getSize();
415+
}
416+
419417
/** Executes the default search action. */
420418
private void execute() {
421419
assertDispatchThread();
@@ -491,7 +489,7 @@ private SearchResult result(final int index) {
491489

492490
private int firstResultIndex() {
493491
assertDispatchThread();
494-
for (int i = 0; i < resultsList.getModel().getSize(); i++) {
492+
for (int i = 0; i < rowCount(); i++) {
495493
if (!isHeader(result(i))) return i;
496494
}
497495
return -1;

0 commit comments

Comments
 (0)