Add ComboBox with search option

This commit is contained in:
Manfred Karrer 2016-05-22 20:49:10 +02:00
parent 1adc040d11
commit a0090a185d
4 changed files with 69 additions and 5 deletions

View File

@ -0,0 +1,42 @@
package io.bitsquare.gui.components;
import io.bitsquare.common.UserThread;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.control.ComboBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SearchComboBox<T> extends ComboBox<T> {
private static final Logger log = LoggerFactory.getLogger(SearchComboBox.class);
private FilteredList<T> filteredList;
public SearchComboBox() {
this(FXCollections.<T>observableArrayList());
}
public SearchComboBox(final ObservableList<T> items) {
super(new FilteredList<>(items));
filteredList = new FilteredList<>(items);
setEditable(true);
itemsProperty().addListener(new WeakChangeListener<>((observable, oldValue, newValue) -> {
filteredList = new FilteredList<>(newValue);
setItems(filteredList);
}));
getEditor().textProperty().addListener(new WeakChangeListener<>((observable, oldValue, newValue) -> {
if (!filteredList.stream().filter(item -> getConverter().toString(item).equals(newValue)).
findAny().isPresent()) {
UserThread.execute(() -> {
filteredList.setPredicate(item -> newValue.isEmpty() ||
getConverter().toString(item).toLowerCase().startsWith(newValue.toLowerCase()));
hide();
setVisibleRowCount(Math.min(20, filteredList.size()));
show();
});
}
}));
}
}

View File

@ -40,6 +40,8 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
import static io.bitsquare.gui.util.FormBuilder.*;
public class BlockChainForm extends PaymentMethodForm {
@ -127,19 +129,25 @@ public class BlockChainForm extends PaymentMethodForm {
@Override
protected void addTradeCurrencyComboBox() {
currencyComboBox = addLabelComboBox(gridPane, ++gridRow, "Cryptocurrency:", Layout.FIRST_ROW_AND_GROUP_DISTANCE).second;
currencyComboBox.setPromptText("Select cryptocurrency");
currencyComboBox = addLabelSearchComboBox(gridPane, ++gridRow, "Cryptocurrency:", Layout.FIRST_ROW_AND_GROUP_DISTANCE).second;
currencyComboBox.setPromptText("Select or search cryptocurrency");
currencyComboBox.setItems(FXCollections.observableArrayList(CurrencyUtil.getAllSortedCryptoCurrencies()));
currencyComboBox.setVisibleRowCount(Math.min(currencyComboBox.getItems().size(), 20));
currencyComboBox.setConverter(new StringConverter<TradeCurrency>() {
@Override
public String toString(TradeCurrency tradeCurrency) {
return tradeCurrency.getNameAndCode();
return tradeCurrency != null ? tradeCurrency.getNameAndCode() : "";
}
@Override
public TradeCurrency fromString(String s) {
return null;
Optional<TradeCurrency> tradeCurrencyOptional = currencyComboBox.getItems().stream().
filter(tradeCurrency -> tradeCurrency.getNameAndCode().equals(s)).
findAny();
if (tradeCurrencyOptional.isPresent())
return tradeCurrencyOptional.get();
else
return null;
}
});
currencyComboBox.setOnAction(e -> {

View File

@ -224,7 +224,7 @@ public class FiatAccountsView extends ActivatableViewAndModel<GridPane, FiatAcco
paymentMethodsComboBox.setConverter(new StringConverter<PaymentMethod>() {
@Override
public String toString(PaymentMethod paymentMethod) {
return BSResources.get(paymentMethod.getId());
return paymentMethod != null ? BSResources.get(paymentMethod.getId()) : "";
}
@Override

View File

@ -558,6 +558,20 @@ public class FormBuilder {
return new Tuple2<>(label, comboBox);
}
public static Tuple2<Label, SearchComboBox> addLabelSearchComboBox(GridPane gridPane, int rowIndex, String title, double top) {
Label label = null;
if (title != null)
label = addLabel(gridPane, rowIndex, title, top);
SearchComboBox comboBox = new SearchComboBox();
GridPane.setRowIndex(comboBox, rowIndex);
GridPane.setColumnIndex(comboBox, 1);
GridPane.setMargin(comboBox, new Insets(top, 0, 0, 0));
gridPane.getChildren().add(comboBox);
return new Tuple2<>(label, comboBox);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Label + ComboBox + ComboBox