Skip to content

Commit 7c263e8

Browse files
frauzufallctrueden
authored andcommitted
searchbar: layout updates, imagej forum result improvements
1 parent dc31013 commit 7c263e8

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
package org.scijava.search.web;
3+
4+
import java.time.Instant;
5+
import java.time.LocalDateTime;
6+
import java.time.ZoneId;
7+
import java.time.ZoneOffset;
8+
import java.time.format.DateTimeFormatter;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* @author Deborah Schmidt
14+
*/
15+
public class ImageJForumSearchResult extends WebSearchResult {
16+
17+
final HashMap<String, String> metaInfo;
18+
19+
public ImageJForumSearchResult(final String name, final String iconPath,
20+
final String url, HashMap< String, String > metaInfo, final String details)
21+
{
22+
super(name, iconPath, url, details);
23+
this.metaInfo = metaInfo;
24+
}
25+
26+
@Override
27+
public Map<String, String> properties() {
28+
final HashMap<String, String> properties = new HashMap<>();
29+
// properties.put("name", name);
30+
// properties.put("iconpath", iconPath);
31+
properties.put("url", url);
32+
properties.put("tags", metaInfo.get("tags"));
33+
properties.put("created / last posted", formatDate(metaInfo.get("created_at")) + " / " + formatDate(metaInfo.get("last_posted_at")));
34+
return properties;
35+
}
36+
37+
public String formatDate(String datestr) {
38+
Instant instant = Instant.parse(datestr);
39+
LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId()));
40+
return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
41+
}
42+
}

src/main/java/org/scijava/search/web/ImageJForumSearcher.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,30 @@ public List<SearchResult> search(final String text, final boolean fuzzy) {
8686
"created_at") + "<br />" + "Last posted: " + metaInfo.get(
8787
"last_posted_at");
8888

89-
addResult(metaInfo.get("title"), "", forumPostUrl, details);
89+
addResult(metaInfo.get("title"), "", forumPostUrl, metaInfo, details);
9090
}
9191
}
9292
catch (final IOException e) {
9393
log.debug(e);
9494
}
9595
return getSearchResults();
9696
}
97+
98+
/**
99+
* @param name Resulting website title / name
100+
* @param iconPath path to an image representing the results
101+
* @param url URL of the found website
102+
* @param metaInfo
103+
* @param details some text from the website representing its content
104+
*/
105+
protected void addResult(final String name, final String iconPath,
106+
final String url, HashMap< String, String > metaInfo, final String details)
107+
{
108+
getSearchResults().add(new ImageJForumSearchResult(name, //
109+
iconPath == null || iconPath.isEmpty() ? "/icons/world_link.png"
110+
: iconPath, url, metaInfo, details));
111+
}
112+
97113

98114
HashMap<String, String> parseForumSearchResult(String content) {
99115
content = content + ",";

src/main/java/org/scijava/search/web/WebSearchResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*/
4444
public class WebSearchResult implements SearchResult {
4545

46-
private final String details;
46+
protected final String details;
4747
String name;
4848
String iconPath;
4949
String url;

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
import java.awt.event.KeyAdapter;
4343
import java.awt.event.KeyEvent;
4444
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;
4550
import java.util.ArrayList;
4651
import java.util.Collections;
4752
import java.util.Comparator;
@@ -349,11 +354,17 @@ public SwingSearchPanel() {
349354
final JPanel detailsPane = new JPanel();
350355
final JLabel detailsTitle = new JLabel();
351356
final JPanel detailsProps = new JPanel();
357+
final JScrollPane detailsScrollPane = new JScrollPane(detailsProps);
352358
final JPanel detailsButtons = new JPanel();
353359

354-
detailsProps.setLayout(new MigLayout("fill, wrap 1, ins 0, wmin 10"));
355-
detailsButtons.setLayout(new MigLayout("fill, ins " + PAD + " 0 " + PAD + " 0"));
356-
detailsPane.setLayout(new MigLayout("wrap, ins 0 " + PAD + " " + PAD + " " + PAD + ", fill, wmin 100, hmin 100","[grow]", "[fill][fill]push[fill]"));
360+
detailsScrollPane.setHorizontalScrollBarPolicy(
361+
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
362+
detailsScrollPane.setBorder(null);
363+
364+
detailsProps.setLayout(new MigLayout("wrap 1, ins 0, wmin 0, hmin 0", "[grow]", ""));
365+
detailsButtons.setLayout(new MigLayout("fill, ins " + PAD + " 0 0 0"));
366+
detailsPane.setLayout(new MigLayout("wrap, ins 0 " + PAD + " " + PAD + " " + PAD + ", fill, wmin 0, hmin 0, hmax 100%, wmax 100%",
367+
"[grow]", "[fill][fill,grow][fill]"));
357368

358369
resultsList.addListSelectionListener(lse -> {
359370
if (lse.getValueIsAdjusting()) return;
@@ -373,7 +384,6 @@ public SwingSearchPanel() {
373384
else {
374385
// populate details pane
375386
detailsTitle.setText("<html><h2>" + highlightSearchUnderline(escapeHtml(result.name()), searchTerm) + "</h2>");
376-
detailsTitle.repaint();
377387
detailsProps.removeAll();
378388
result.properties().forEach((k, v) -> {
379389
if(v != "") {
@@ -422,10 +432,7 @@ public SwingSearchPanel() {
422432
});
423433
button.addKeyListener(new SearchBarKeyAdapter());
424434
if(first){
425-
JPanel mainBtnPane = new JPanel();
426-
mainBtnPane.setLayout(new MigLayout("insets 0", "[fill,grow]", "[fill,grow]"));
427-
mainBtnPane.add(button, "growx");
428-
detailsButtons.add(mainBtnPane, "south");
435+
detailsButtons.add(button, "grow, spanx");
429436
JRootPane rootPane = this.getRootPane();
430437
if(rootPane != null){
431438
rootPane.setDefaultButton(button);
@@ -440,7 +447,7 @@ public SwingSearchPanel() {
440447

441448
detailsPane.add(exitBtn, "pos n 0 100% n");
442449
detailsPane.add(detailsTitle,"growx, pad 0 0 0 -20");
443-
detailsPane.add(detailsProps, "growx");
450+
detailsPane.add(detailsScrollPane, "growx, hmin 0, wmin 0");
444451
detailsPane.add(detailsButtons, "growx");
445452

446453
resultsList.addKeyListener(new SearchBarKeyAdapter());
@@ -460,7 +467,7 @@ public SwingSearchPanel() {
460467
public void search(final String text) {
461468
assertDispatchThread();
462469
searchTerm = text;
463-
operation.search(text);
470+
operation.search(text.toLowerCase());
464471
}
465472

466473
// -- Helper methods --
@@ -655,6 +662,7 @@ public String escapeHtml(String s) {
655662
}
656663
return out.toString();
657664
}
665+
658666
}
659667

660668
private class SearchBarKeyAdapter extends KeyAdapter {

0 commit comments

Comments
 (0)