Skip to content

Commit

Permalink
Merge pull request #2 from scijava/liststyle
Browse files Browse the repository at this point in the history
Search result list style improved
  • Loading branch information
imagejan authored Jan 3, 2018
2 parents 33833ec + 70368e5 commit 6105bdd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/scijava/search/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ default String identifier() {
return name();
}

default String context() {
return "";
}

String iconPath();

Map<String, String> properties();
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/scijava/search/module/ModuleSearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ModuleSearchResult(final ModuleInfo info, final String baseDir) {
props = new LinkedHashMap<>();
final MenuPath menuPath = info.getMenuPath();
if (menuPath != null && !menuPath.isEmpty()) {
props.put("Menu path", getMenuPath(false));
props.put("Menu path", getMenuPath(true));
final MenuEntry menuLeaf = menuPath.getLeaf();
if (menuLeaf != null) {
final Accelerator accelerator = menuLeaf.getAccelerator();
Expand All @@ -78,8 +78,12 @@ public String name() {

@Override
public String identifier() {
final String menuPath = getMenuPath(true);
return menuPath.isEmpty() ? name() : menuPath;
return name();
}

@Override
public String context() {
return "/" + getMenuPath(name() != info.getMenuPath().getLeaf().toString(), "/");
}

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

private String getMenuPath(boolean includeLeaf) {
return getMenuPath(includeLeaf, " \u203a ");
}

private String getMenuPath(boolean includeLeaf, String separator) {
final MenuPath menuPath = info.getMenuPath();
if (menuPath == null) return "";
return menuPath.getMenuString(includeLeaf).replace(">", "\u203a");
return menuPath.getMenuString(includeLeaf).replace(" > ", separator);
}
}
30 changes: 23 additions & 7 deletions src/main/java/org/scijava/ui/swing/search/SwingSearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public class SwingSearchBar extends JTextField {
private static final String DEFAULT_MESSAGE = "Click here to search";
private static final Color ACTIVE_FONT_COLOR = new Color(0, 0, 0);
private static final Color INACTIVE_FONT_COLOR = new Color(150, 150, 150);
private static final Color SELECTED_RESULT_COLOR = new Color(70, 152, 251);
private static final Color SELECTED_RESULT_COLOR = new Color(186, 218, 255);
private static final String CONTEXT_COLOR = "#8C745E";
private static final int ICON_SIZE = 16;
private static final int PAD = 5;

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

final Container parent = getParent();

String resultSizeStr = "";
final int resCount = ((SearchResultHeader) value).resultCount();
if(resCount > resultLimit) {
resultSizeStr += " <span style='color: " + CONTEXT_COLOR + ";'>(" + resultLimit + "/" + resCount + ")";
}

final JCheckBox headerBox = //
new JCheckBox(searcher.title(), searchService.enabled(searcher));
new JCheckBox("<html>" + searcher.title() + resultSizeStr, searchService.enabled(searcher));
headerBox.setFont(smaller(headerBox.getFont(), 2));
if (parent != null) headerBox.setBackground(parent.getBackground());
headerCheckboxes.put(searcher.getClass(), headerBox);
Expand All @@ -373,9 +380,10 @@ public SwingSearchPanel(final Context context) {
item.setBorder(new EmptyBorder(1, PAD, 0, PAD));
item.add(icon(value.iconPath()));
item.add(Box.createHorizontalStrut(3));
final JTextArea name = new JTextArea();
name.setText(value.identifier());
name.setEditable(false);
final JLabel name = new JLabel();
Font f = name.getFont();
name.setFont(f.deriveFont(f.getStyle() & ~Font.BOLD));
name.setText("<html>" + value.identifier() + "&nbsp;&nbsp;<span style='color: " + CONTEXT_COLOR + ";'>" + value.context() + "</span>");
name.setBackground(null);
item.add(name);
item.setBackground(isSelected ? SELECTED_RESULT_COLOR : list.getBackground());
Expand Down Expand Up @@ -642,8 +650,10 @@ private void rebuild() {

if (completeResults == null) continue;

int resultCount = completeResults.size();

// Add section header.
listModel.addElement(new SearchResultHeader(searcher));
listModel.addElement(new SearchResultHeader(searcher, resultCount));

if (completeResults.isEmpty()) continue;

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

private final Searcher searcher;
private final int resultCount;

public SearchResultHeader(final Searcher searcher) {
public SearchResultHeader(final Searcher searcher, int resultCount) {
this.searcher = searcher;
this.resultCount = resultCount;
}

public int resultCount() {
return resultCount;
}

public Searcher searcher() {
Expand Down

0 comments on commit 6105bdd

Please sign in to comment.