Skip to content

Commit

Permalink
Merge pull request controlsfx#1185 from piegamesde/master
Browse files Browse the repository at this point in the history
Auto completion improvements
  • Loading branch information
Maxoudela authored Apr 10, 2020
2 parents 7136fd9 + 8f41caf commit 4c855b1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,40 @@
*/
package org.controlsfx.samples.textfields;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.controlsfx.ControlsFXSample;
import org.controlsfx.control.textfield.AutoCompletionBinding;
import org.controlsfx.control.textfield.TextFields;
import org.controlsfx.samples.Utils;

import impl.org.controlsfx.skin.AutoCompletePopup;
import impl.org.controlsfx.skin.AutoCompletePopupSkin;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import org.controlsfx.ControlsFXSample;
import org.controlsfx.control.textfield.AutoCompletionBinding;
import org.controlsfx.control.textfield.TextFields;
import org.controlsfx.samples.Utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class HelloAutoComplete extends ControlsFXSample {

private AutoCompletionBinding<String> autoCompletionBinding;
private String[] _possibleSuggestions = {"Hey", "Hello", "Hello World", "Apple", "Cool", "Costa", "Cola", "Coca Cola"};
private Set<String> possibleSuggestions = new HashSet<>(Arrays.asList(_possibleSuggestions));
private Map<String, Color> colorSuggestions = allColorsWithName();

private TextField learningTextField;

Expand All @@ -71,7 +81,10 @@ public class HelloAutoComplete extends ControlsFXSample {
+ "\n\n"
+ "The 'Learning TextField' will add whatever words are typed "
+ "to the auto-complete popup, as long as you press Enter once "
+ "you've finished typing the word.";
+ "you've finished typing the word."
+ "\n\n"
+ "The Color TextField will suggest different colors when you type "
+ "in their name.";
}

@Override public Node getPanel(final Stage stage) {
Expand Down Expand Up @@ -116,9 +129,33 @@ public void handle(KeyEvent ke) {
}
});

grid.add(new Label("Learning TextField"), 0, 1);
grid.add(learningTextField, 1, 1);
GridPane.setHgrow(learningTextField, Priority.ALWAYS);
grid.add(new Label("Learning TextField"), 0, 1);
grid.add(learningTextField, 1, 1);
GridPane.setHgrow(learningTextField, Priority.ALWAYS);

//
// TextField with custom cell factory
// Completes color names
//
TextField customTextField = new TextField();
AutoCompletePopup<String> colorCompletionPopup = TextFields.bindAutoCompletion(customTextField, colorSuggestions.keySet()).getAutoCompletionPopup();
colorCompletionPopup.setSkin(new AutoCompletePopupSkin<String>(colorCompletionPopup, param -> new ListCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
setGraphic(new Rectangle(32, 32, colorSuggestions.get(item)));
setText(item);
}
}
}));

grid.add(new Label("Color TextField with custom CellFactory"), 0, 2);
grid.add(customTextField, 1, 2);
GridPane.setHgrow(customTextField, Priority.ALWAYS);

root.setTop(grid);
return root;
Expand Down Expand Up @@ -146,6 +183,23 @@ private void autoCompletionLearnWord(String newWord){
return grid;
}

/* Modified from https://stackoverflow.com/a/17465261/6094756 */
private Map<String, Color> allColorsWithName() {
Map<String, Color> map = new HashMap<>();
try {
for (Field f : Color.class.getFields()) {
Object obj = f.get(null);
if (obj instanceof Color) {
map.put(f.getName(), (Color) obj);
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
map.put("red", Color.RED);
map.put("green", Color.GREEN);
map.put("blue", Color.BLUE);
}
return map;
}

public static void main(String[] args) {
launch(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2016 ControlsFX
* Copyright (c) 2014, 2016, 2020 ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -30,9 +30,12 @@
import javafx.event.Event;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import javafx.scene.control.ListCell;
import javafx.scene.control.Skin;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.input.MouseButton;
import javafx.util.StringConverter;
import javafx.util.Callback;
import org.controlsfx.control.textfield.AutoCompletionBinding;


Expand All @@ -42,7 +45,30 @@ public class AutoCompletePopupSkin<T> implements Skin<AutoCompletePopup<T>> {
private final ListView<T> suggestionList;
final int LIST_CELL_HEIGHT = 24;

public AutoCompletePopupSkin(AutoCompletePopup<T> control){
public AutoCompletePopupSkin(AutoCompletePopup<T> control) {
this(control, control.getConverter());
}

/**
* @param control
* The popup to be skinned
* @param displayConverter
* An alternate {@link StringConverter} to use. This way, you can show autocomplete suggestions
* that when applied will fill in a different text than displayed. For example, you may preview
* {@code Files.newBufferedReader(Path: path) - Bufferedreader} but only fill in
* {@code Files.newBufferedReader(}
*/
public AutoCompletePopupSkin(AutoCompletePopup<T> control, StringConverter<T> displayConverter) {
this(control, TextFieldListCell.forListView(displayConverter));
}

/**
* @param control
* The popup to be skinned
* @param cellFactory
* Set a custom cell factory for the suggestions.
*/
public AutoCompletePopupSkin(AutoCompletePopup<T> control, Callback<ListView<T>, ListCell<T>> cellFactory) {
this.control = control;
suggestionList = new ListView<>(control.getSuggestions());

Expand All @@ -59,7 +85,7 @@ public AutoCompletePopupSkin(AutoCompletePopup<T> control){
suggestionList.prefHeightProperty().bind(
Bindings.min(control.visibleRowCountProperty(), Bindings.size(suggestionList.getItems()))
.multiply(LIST_CELL_HEIGHT).add(18));
suggestionList.setCellFactory(TextFieldListCell.forListView(control.getConverter()));
suggestionList.setCellFactory(cellFactory);

//Allowing the user to control ListView width.
suggestionList.prefWidthProperty().bind(control.prefWidthProperty());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2016 ControlsFX
* Copyright (c) 2014, 2016, 2020 ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -268,6 +268,19 @@ public final double getMaxWidth() {
public final DoubleProperty maxWidthProperty() {
return autoCompletionPopup.maxWidthProperty();
}

/**
* Get the {@link AutoCompletePopup} used by this binding. Note that this gives access to the
* internal API and should be used with great care (and in the expectation that things may break in
* the future). All relevant methods of the popup are already exposed in this class.
* <p/>
* The only reason this is exposed is to allow custom skins for the popup.
*
* @return the {@link AutoCompletePopup} used by this binding
*/
public AutoCompletePopup<T> getAutoCompletionPopup() {
return autoCompletionPopup;
}

/***************************************************************************
* *
Expand Down

0 comments on commit 4c855b1

Please sign in to comment.