这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions gemsfx/src/main/java/com/dlsc/gemsfx/SearchField.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class SearchField<T> extends Control {

private final SearchFieldPopup<T> popup;
private final HistoryButton<String> historyButton;


/**
* Constructs a new spotlight field. The field will set defaults for the
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,23 +67,25 @@ private Comparator<T> createInnerComparator() {
}
}

// prefer the suggestions that start with the search term
StringConverter<T> converter = searchField.getConverter();
String searchText = searchField.getText().toLowerCase();
if(searchField.searchResultsOnTop.get()) {
// prefer the suggestions that start with the search term
StringConverter<T> 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;
Expand Down