From dd6dcd7e748c0386a543d3bfb84af9b6596136a9 Mon Sep 17 00:00:00 2001 From: Jody Date: Tue, 30 Sep 2025 11:44:44 -0400 Subject: [PATCH] making search results at the top optional and trusting the user's sorting algo where possible --- .../java/com/dlsc/gemsfx/SearchField.java | 21 ++++++++++++++ .../gemsfx/skins/SearchFieldPopupSkin.java | 29 ++++++++++--------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/SearchField.java b/gemsfx/src/main/java/com/dlsc/gemsfx/SearchField.java index 088e07f6..5de89a0a 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/SearchField.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/SearchField.java @@ -112,6 +112,7 @@ public class SearchField extends Control { private final SearchFieldPopup popup; private final HistoryButton historyButton; + /** * Constructs a new spotlight field. The field will set defaults for the @@ -538,6 +539,26 @@ public final void setBusyGraphic(Node busyGraphic) { public final boolean isSearching() { return searching.get(); } + + public final BooleanProperty searchResultsOnTop = new SimpleBooleanProperty(this, "searchResultsOnTop", true); + + public final boolean isSearchResultsOnTop() { + return searchResultsOnTop.get(); + } + + /** + * Shows results matching searched text at the top of the search results, this may be turned off when search item + * order has special requirements. The default is "true". + * + * @return true if the popup showing the list of suggestions will show search matched terms first when available + */ + public final BooleanProperty searchResultsOnTopProperty() { + return searchResultsOnTop; + } + + public final void setSearchResultsOnTop(boolean searchResultsOnTop) { + this.searchResultsOnTop.set(searchResultsOnTop); + } private final BooleanProperty hidePopupWithSingleChoice = new SimpleBooleanProperty(this, "hidePopupWithSingleChoice", false); diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SearchFieldPopupSkin.java b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SearchFieldPopupSkin.java index ed6c79a5..2a24079c 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SearchFieldPopupSkin.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SearchFieldPopupSkin.java @@ -6,6 +6,7 @@ package com.dlsc.gemsfx.skins; import com.dlsc.gemsfx.SearchField; +import javafx.beans.property.SimpleBooleanProperty; import javafx.collections.transformation.SortedList; import javafx.scene.Node; import javafx.scene.control.ListView; @@ -66,23 +67,25 @@ private Comparator createInnerComparator() { } } - // prefer the suggestions that start with the search term - StringConverter converter = searchField.getConverter(); - String searchText = searchField.getText().toLowerCase(); + if(searchField.searchResultsOnTop.get()) { + // prefer the suggestions that start with the search term + StringConverter converter = searchField.getConverter(); + String searchText = searchField.getText().toLowerCase(); - String text1 = converter.toString(o1).toLowerCase(); - String text2 = converter.toString(o2).toLowerCase(); + String text1 = converter.toString(o1).toLowerCase(); + String text2 = converter.toString(o2).toLowerCase(); - if (text1.startsWith(searchText) && text2.startsWith(searchText)) { - return text1.compareTo(text2); - } + if (text1.startsWith(searchText) && text2.startsWith(searchText)) { + return result;//Both items are on equal footing, trust the custom comparator + } - if (text1.startsWith(searchText)) { - result = -1; - } + if (text1.startsWith(searchText)) { + result = -1; + } - if (text2.startsWith(searchText)) { - result = 1; + if (text2.startsWith(searchText)) { + result = 1; + } } return result;