Skip to content

Commit 63b8ac4

Browse files
frauzufallctrueden
authored andcommitted
searchbar: listener and layout updates
1 parent 7c263e8 commit 63b8ac4

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

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

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@
4141
import java.awt.event.FocusListener;
4242
import java.awt.event.KeyAdapter;
4343
import java.awt.event.KeyEvent;
44+
import java.awt.event.MouseAdapter;
45+
import java.awt.event.MouseEvent;
46+
import java.awt.event.MouseMotionAdapter;
4447
import java.net.URL;
45-
import java.time.Instant;
46-
import java.time.LocalDateTime;
47-
import java.time.ZoneId;
48-
import java.time.ZoneOffset;
49-
import java.time.format.DateTimeFormatter;
5048
import java.util.ArrayList;
5149
import java.util.Collections;
5250
import java.util.Comparator;
@@ -296,6 +294,8 @@ private class SwingSearchPanel extends JPanel {
296294
private final Map<Class<?>, SearchEvent> allResults;
297295
private final Map<Class<?>, JCheckBox> headerCheckboxes;
298296
private final JList<SearchResult> resultsList;
297+
298+
private SearchResult selected = null;
299299

300300
public SwingSearchPanel() {
301301

@@ -317,13 +317,13 @@ public SwingSearchPanel() {
317317
final JCheckBox headerBox = //
318318
new JCheckBox(searcher.title(), searchService.enabled(searcher));
319319
headerBox.setFont(smaller(headerBox.getFont(), 2));
320-
headerBox.setBackground(HEADER_COLOR);
320+
headerBox.setBackground(parent.getBackground());
321321
headerCheckboxes.put(searcher.getClass(), headerBox);
322322

323323
final JPanel headerInnerPane = new JPanel();
324324
headerInnerPane.setLayout(new GridLayout(1, 1));
325325
headerInnerPane.add(headerBox);
326-
headerInnerPane.setBackground(HEADER_COLOR);
326+
headerInnerPane.setBackground(parent.getBackground());
327327

328328
final JPanel headerOuterPane = new JPanel();
329329
headerOuterPane.setLayout(new GridLayout(1, 1));
@@ -349,33 +349,65 @@ public SwingSearchPanel() {
349349
final JScrollPane resultsPane = new JScrollPane(resultsList);
350350
resultsPane.setHorizontalScrollBarPolicy(
351351
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
352+
// uncomment to move scrollbar to the left
353+
// resultsPane.setVerticalScrollBarPolicy(
354+
// ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
355+
// resultsPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
352356
resultsPane.setBorder(null);
353357

354358
final JPanel detailsPane = new JPanel();
355359
final JLabel detailsTitle = new JLabel();
356360
final JPanel detailsProps = new JPanel();
357361
final JScrollPane detailsScrollPane = new JScrollPane(detailsProps);
358362
final JPanel detailsButtons = new JPanel();
359-
363+
360364
detailsScrollPane.setHorizontalScrollBarPolicy(
361365
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
362366
detailsScrollPane.setBorder(null);
363-
367+
364368
detailsProps.setLayout(new MigLayout("wrap 1, ins 0, wmin 0, hmin 0", "[grow]", ""));
365369
detailsButtons.setLayout(new MigLayout("fill, ins " + PAD + " 0 0 0"));
366370
detailsPane.setLayout(new MigLayout("wrap, ins 0 " + PAD + " " + PAD + " " + PAD + ", fill, wmin 0, hmin 0, hmax 100%, wmax 100%",
367371
"[grow]", "[fill][fill,grow][fill]"));
368372

373+
resultsList.addMouseListener(new MouseAdapter() {
374+
@Override
375+
public void mouseClicked( MouseEvent e ) {
376+
final SearchResult result = resultsList.getSelectedValue();
377+
if (result == null || isHeader(result)) {
378+
if (result != null) {
379+
final Searcher searcher = ((SearchResultHeader) result).searcher();
380+
searchService.setEnabled(searcher, !searchService.enabled(searcher));
381+
SwingSearchBar.this.search();
382+
}
383+
}
384+
}
385+
});
386+
387+
resultsList.addMouseMotionListener(new MouseMotionAdapter() {
388+
@Override
389+
public void mouseMoved( MouseEvent e ) {
390+
int index = resultsList.locationToIndex(e.getPoint());
391+
SearchResult _selected = resultsList.getModel().getElementAt(index);
392+
if(selected != _selected) {
393+
selected = _selected;
394+
if(selected != null && !isHeader(selected)) {
395+
resultsList.setSelectedValue(selected, false);
396+
}
397+
}
398+
}
399+
});
400+
369401
resultsList.addListSelectionListener(lse -> {
370402
if (lse.getValueIsAdjusting()) return;
371403
final SearchResult result = resultsList.getSelectedValue();
372404
if (result == null || isHeader(result)) {
373-
if (result != null) {
374-
final Searcher searcher = ((SearchResultHeader) result).searcher();
375-
searchService.setEnabled(searcher, !searchService.enabled(searcher));
376-
SwingSearchBar.this.search();
405+
if(result != null){
406+
threadService.queue(() -> {
407+
down();
408+
});
409+
return;
377410
}
378-
379411
// clear details pane
380412
detailsTitle.setText("");
381413
detailsProps.removeAll();
@@ -490,7 +522,7 @@ private void down() {
490522

491523
private void select(final Function<Integer, Integer> stepper) {
492524
assertDispatchThread();
493-
if (rowCount() == 0) return;
525+
if (resultCount() == 0) return;
494526
// Step through the list, skipping headers.
495527
int index = resultsList.getSelectedIndex();
496528
do {
@@ -503,6 +535,15 @@ private void select(final Function<Integer, Integer> stepper) {
503535
private int rowCount() {
504536
return resultsList.getModel().getSize();
505537
}
538+
539+
private int resultCount() {
540+
int count = 0;
541+
for(int i = 0; i < resultsList.getModel().getSize(); i++){
542+
SearchResult result = resultsList.getModel().getElementAt(i);
543+
if(!isHeader(result)) count++;
544+
}
545+
return count;
546+
}
506547

507548
private Font smaller(Font font, int decrement) {
508549
return new Font(font.getFontName(), font.getStyle(), font.getSize() - decrement);

0 commit comments

Comments
 (0)