From 8cc8a50d89c33ff539e54352e067079cebd32c51 Mon Sep 17 00:00:00 2001 From: Leo Leplat <60394504+Rylern@users.noreply.github.com> Date: Fri, 21 Mar 2025 15:45:42 +0000 Subject: [PATCH 1/5] Update selected URI after search suggestion clicked --- .../gui/viewer/JavadocViewer.java | 46 +++++++++++++++---- .../gui/viewer/javadoc_viewer.fxml | 2 +- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/javadocviewer/src/main/java/qupath/ui/javadocviewer/gui/viewer/JavadocViewer.java b/javadocviewer/src/main/java/qupath/ui/javadocviewer/gui/viewer/JavadocViewer.java index 6b63bdf..c9dc173 100644 --- a/javadocviewer/src/main/java/qupath/ui/javadocviewer/gui/viewer/JavadocViewer.java +++ b/javadocviewer/src/main/java/qupath/ui/javadocviewer/gui/viewer/JavadocViewer.java @@ -45,8 +45,6 @@ public class JavadocViewer extends BorderPane { @FXML private ComboBox uris; @FXML - private HBox searchContainer; - @FXML private AutoCompletionTextField autoCompletionTextField; /** @@ -95,7 +93,7 @@ protected void updateItem(URI item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { - setGraphic(null); + setText(null); } else { setText(getName(item)); } @@ -107,7 +105,7 @@ protected void updateItem(URI item, boolean empty) { super.updateItem(item, empty); if (item == null || empty) { - setGraphic(null); + setText(null); } else { setText(getName(item)); } @@ -141,7 +139,10 @@ protected void updateItem(URI item, boolean empty) { .flatMap(List::stream) .map(javadocElement -> new JavadocEntry( javadocElement, - () -> webView.getEngine().load(javadocElement.uri().toString()) + () -> { + updateSelectedUri(javadocElement.uri()); + webView.getEngine().load(javadocElement.uri().toString()); + } )) .filter(javadocEntry -> !CATEGORIES_TO_SKIP.contains(javadocEntry.getCategory())) .toList()); @@ -155,13 +156,10 @@ private void setUpListeners() { )); uris.getSelectionModel().selectedItemProperty().addListener((p, o, n) -> { - if (n != null) { + if (n != null && !webView.getEngine().getLocation().equals(n.toString())) { webView.getEngine().load(n.toString()); } }); - if (uris.getSelectionModel().getSelectedItem() != null) { - webView.getEngine().load(uris.getSelectionModel().getSelectedItem().toString()); - } // Sometimes, redirection is not automatically performed // (see https://github.com/qupath/qupath/pull/1513#issuecomment-2095553840) @@ -204,6 +202,24 @@ private static String getName(URI uri) { return name; } + private void updateSelectedUri(URI uri) { + int maxIndex = Integer.MIN_VALUE; + URI closestUri = null; + + for (URI sd: uris.getItems()) { + int index = getNumberOfCommonCharactersFromBeginning(uri.toString(), sd.toString()); + + if (index > maxIndex) { + maxIndex = index; + closestUri = sd; + } + } + + if (closestUri != null) { + uris.getSelectionModel().select(closestUri); + } + } + private static Optional changeLocation(String currentLocation, String newLocation) { int index = currentLocation.lastIndexOf("/"); @@ -213,4 +229,16 @@ private static Optional changeLocation(String currentLocation, String ne return Optional.of(currentLocation.substring(0, currentLocation.lastIndexOf("/")) + "/" + newLocation); } } + + private static int getNumberOfCommonCharactersFromBeginning(String text1, String text2) { + int n = Math.min(text1.length(), text2.length()); + + for (int i=0; i - +