Skip to content

Commit 6105bdd

Browse files
authored
Merge pull request #2 from scijava/liststyle
Search result list style improved
2 parents 33833ec + 70368e5 commit 6105bdd

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/main/java/org/scijava/search/SearchResult.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ default String identifier() {
5050
return name();
5151
}
5252

53+
default String context() {
54+
return "";
55+
}
56+
5357
String iconPath();
5458

5559
Map<String, String> properties();

src/main/java/org/scijava/search/module/ModuleSearchResult.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ModuleSearchResult(final ModuleInfo info, final String baseDir) {
5454
props = new LinkedHashMap<>();
5555
final MenuPath menuPath = info.getMenuPath();
5656
if (menuPath != null && !menuPath.isEmpty()) {
57-
props.put("Menu path", getMenuPath(false));
57+
props.put("Menu path", getMenuPath(true));
5858
final MenuEntry menuLeaf = menuPath.getLeaf();
5959
if (menuLeaf != null) {
6060
final Accelerator accelerator = menuLeaf.getAccelerator();
@@ -78,8 +78,12 @@ public String name() {
7878

7979
@Override
8080
public String identifier() {
81-
final String menuPath = getMenuPath(true);
82-
return menuPath.isEmpty() ? name() : menuPath;
81+
return name();
82+
}
83+
84+
@Override
85+
public String context() {
86+
return "/" + getMenuPath(name() != info.getMenuPath().getLeaf().toString(), "/");
8387
}
8488

8589
@Override
@@ -95,8 +99,12 @@ public Map<String, String> properties() {
9599
// -- Helper methods --
96100

97101
private String getMenuPath(boolean includeLeaf) {
102+
return getMenuPath(includeLeaf, " \u203a ");
103+
}
104+
105+
private String getMenuPath(boolean includeLeaf, String separator) {
98106
final MenuPath menuPath = info.getMenuPath();
99107
if (menuPath == null) return "";
100-
return menuPath.getMenuString(includeLeaf).replace(">", "\u203a");
108+
return menuPath.getMenuString(includeLeaf).replace(" > ", separator);
101109
}
102110
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public class SwingSearchBar extends JTextField {
103103
private static final String DEFAULT_MESSAGE = "Click here to search";
104104
private static final Color ACTIVE_FONT_COLOR = new Color(0, 0, 0);
105105
private static final Color INACTIVE_FONT_COLOR = new Color(150, 150, 150);
106-
private static final Color SELECTED_RESULT_COLOR = new Color(70, 152, 251);
106+
private static final Color SELECTED_RESULT_COLOR = new Color(186, 218, 255);
107+
private static final String CONTEXT_COLOR = "#8C745E";
107108
private static final int ICON_SIZE = 16;
108109
private static final int PAD = 5;
109110

@@ -348,8 +349,14 @@ public SwingSearchPanel(final Context context) {
348349

349350
final Container parent = getParent();
350351

352+
String resultSizeStr = "";
353+
final int resCount = ((SearchResultHeader) value).resultCount();
354+
if(resCount > resultLimit) {
355+
resultSizeStr += " <span style='color: " + CONTEXT_COLOR + ";'>(" + resultLimit + "/" + resCount + ")";
356+
}
357+
351358
final JCheckBox headerBox = //
352-
new JCheckBox(searcher.title(), searchService.enabled(searcher));
359+
new JCheckBox("<html>" + searcher.title() + resultSizeStr, searchService.enabled(searcher));
353360
headerBox.setFont(smaller(headerBox.getFont(), 2));
354361
if (parent != null) headerBox.setBackground(parent.getBackground());
355362
headerCheckboxes.put(searcher.getClass(), headerBox);
@@ -373,9 +380,10 @@ public SwingSearchPanel(final Context context) {
373380
item.setBorder(new EmptyBorder(1, PAD, 0, PAD));
374381
item.add(icon(value.iconPath()));
375382
item.add(Box.createHorizontalStrut(3));
376-
final JTextArea name = new JTextArea();
377-
name.setText(value.identifier());
378-
name.setEditable(false);
383+
final JLabel name = new JLabel();
384+
Font f = name.getFont();
385+
name.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD));
386+
name.setText("<html>" + value.identifier() + "&nbsp;&nbsp;<span style='color: " + CONTEXT_COLOR + ";'>" + value.context() + "</span>");
379387
name.setBackground(null);
380388
item.add(name);
381389
item.setBackground(isSelected ? SELECTED_RESULT_COLOR : list.getBackground());
@@ -642,8 +650,10 @@ private void rebuild() {
642650

643651
if (completeResults == null) continue;
644652

653+
int resultCount = completeResults.size();
654+
645655
// Add section header.
646-
listModel.addElement(new SearchResultHeader(searcher));
656+
listModel.addElement(new SearchResultHeader(searcher, resultCount));
647657

648658
if (completeResults.isEmpty()) continue;
649659

@@ -809,9 +819,15 @@ public void keyPressed(final KeyEvent e) {
809819
private class SearchResultHeader implements SearchResult {
810820

811821
private final Searcher searcher;
822+
private final int resultCount;
812823

813-
public SearchResultHeader(final Searcher searcher) {
824+
public SearchResultHeader(final Searcher searcher, int resultCount) {
814825
this.searcher = searcher;
826+
this.resultCount = resultCount;
827+
}
828+
829+
public int resultCount() {
830+
return resultCount;
815831
}
816832

817833
public Searcher searcher() {

0 commit comments

Comments
 (0)