Skip to content

Commit

Permalink
Merge pull request #7 from qupath/search-improvements
Browse files Browse the repository at this point in the history
Scrollable search + title
  • Loading branch information
Rylern authored Aug 6, 2024
2 parents 18adc74 + e27bce8 commit 6fc69d4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public interface AutoCompleteTextFieldEntry {
*/
String getName();

/**
* @return the text to use when filtering this entry
*/
String getSearchableText();

/**
* @return the category this entry belongs to
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.Region;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;

Expand All @@ -21,14 +23,16 @@
*/
public class AutoCompletionTextField<T extends AutoCompleteTextFieldEntry> extends TextField {

private static final int MAX_ENTRIES = 10;
private static final int MAX_ENTRIES = 50;
private static final int MAX_POPUP_HEIGHT = 300;
private final ContextMenu entriesPopup = new ContextMenu();
private final List<T> suggestions = new ArrayList<>();

/**
* Create the auto-completion text field
*/
public AutoCompletionTextField() {
setUpUI();
setUpListeners();
}

Expand All @@ -40,6 +44,19 @@ public List<T> getSuggestions() {
return suggestions;
}

private void setUpUI() {
entriesPopup.setMaxHeight(MAX_POPUP_HEIGHT);

// Make context menu respect max height
// See https://stackoverflow.com/a/58542568
entriesPopup.addEventHandler(Menu.ON_SHOWING, e -> {
Node content = entriesPopup.getSkin().getNode();
if (content instanceof Region region) {
region.setMaxHeight(entriesPopup.getMaxHeight());
}
});
}

private void setUpListeners() {
textProperty().addListener((p, o, n) -> {
String enteredText = getText();
Expand All @@ -51,7 +68,7 @@ private void setUpListeners() {

populatePopup(
suggestions.stream()
.filter(entry -> entry.getName().toLowerCase().contains(loweredCaseEnteredText))
.filter(entry -> entry.getSearchableText().toLowerCase().contains(loweredCaseEnteredText))
.limit(MAX_ENTRIES)
.toList(),
enteredText
Expand All @@ -66,7 +83,7 @@ private void populatePopup(List<T> entries, String filter) {
if (entries.isEmpty()) {
entriesPopup.hide();
} else {
entriesPopup.getItems().setAll(entries.stream()
List<MenuItem> items = entries.stream()
.map(AutoCompleteTextFieldEntry::getCategory)
.distinct()
.flatMap(category -> Stream.concat(
Expand All @@ -85,10 +102,16 @@ private void populatePopup(List<T> entries, String filter) {
return menuItem;
})
))
.toList()
);
.toList();

entriesPopup.getItems().clear();

// Add first item, show popup, and then add other items
// This is used to avoid the popup to ignore the anchor position
// See https://stackoverflow.com/a/58542568
entriesPopup.getItems().add(items.get(0));
entriesPopup.show(this, Side.BOTTOM, 0, 0);
entriesPopup.getItems().addAll(items.stream().skip(1).toList());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public String getName() {
return javadocElement.name();
}

@Override
public String getSearchableText() {
int parenthesisIndex = javadocElement.name().indexOf("(");

if (parenthesisIndex > -1) {
return javadocElement.name().substring(0, parenthesisIndex);
} else {
return javadocElement.name();
}
}

@Override
public String getCategory() {
return javadocElement.category();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebHistory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,38 @@

import java.io.IOException;
import java.net.URI;
import java.util.ResourceBundle;

/**
* A command to start a {@link JavadocViewer} in a standalone window.
* Only one instance of the viewer will be created.
*/
public class JavadocViewerCommand implements Runnable {

private static final ResourceBundle resources = ResourceBundle.getBundle("qupath.ui.javadocviewer.strings");
private final Stage owner;
private final ReadOnlyStringProperty stylesheet;
private final URI[] urisToSearch;
private Stage stage;
private JavadocViewer javadocViewer;

/**
* Get a reference to the singleton {@link JavadocViewer}
* Create the command. This will not create the viewer yet.
*
* @param owner the stage that should own the viewer window. Can be null
* @param stylesheet a property containing a link to a stylesheet which should
* be applied to the viewer. Can be null
* @param urisToSearch URIs to search for Javadocs. See {@link JavadocViewer#JavadocViewer(ReadOnlyStringProperty, URI...)}
*/
public JavadocViewerCommand(Stage owner, ReadOnlyStringProperty stylesheet, URI... urisToSearch) {
this.owner = owner;
this.stylesheet = stylesheet;
this.urisToSearch = urisToSearch;
}

/**
* Get a reference to the singleton {@link JavadocViewer}.
*
* @return A JavadocViewer, unless the constructor fails, in which case
* a {@link RuntimeException} is thrown.
*/
Expand All @@ -35,28 +52,14 @@ public JavadocViewer getJavadocViewer() {
return javadocViewer;
}

/**
* Create the command. This will not create the viewer yet.
*
* @param owner the stage that should own the viewer window. Can be null
* @param stylesheet a property containing a link to a stylesheet which should
* be applied to the viewer. Can be null
* @param urisToSearch URIs to search for Javadocs. See {@link JavadocViewer#JavadocViewer(ReadOnlyStringProperty, URI...)}
*/
public JavadocViewerCommand(Stage owner, ReadOnlyStringProperty stylesheet, URI... urisToSearch) {
this.owner = owner;
this.stylesheet = stylesheet;
this.urisToSearch = urisToSearch;
}

@Override
public void run() {
if (stage == null) {

stage = new Stage();
if (owner != null) {
stage.initOwner(owner);
}
stage.setTitle(resources.getString("JavadocViewer.title"));

javadocViewer = getJavadocViewer();

Expand All @@ -66,7 +69,6 @@ public void run() {

stage.setMinWidth(javadocViewer.getWidth());
stage.setMinHeight(javadocViewer.getHeight());

}

stage.show();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
JavadocViewer.title = Javadoc Viewer
JavadocViewer.back = Back
JavadocViewer.forward = Forward
JavadocViewer.javadocSource = Javadoc source
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
JavadocViewer.title = Visionneuse de Javadoc
JavadocViewer.back = Précédent
JavadocViewer.forward = Suivant
JavadocViewer.javadocSource = Source
Expand Down

0 comments on commit 6fc69d4

Please sign in to comment.