org.rapidpm.modul.javafx.textfield.autocomplete.AutoCompleteTextField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javafx-autocomplete-textfield Show documentation
Show all versions of javafx-autocomplete-textfield Show documentation
A JavaFX component: TextField with autocompletion-features
The newest version!
/*
* Copyright [2013] [www.rapidpm.org / Sven Ruppert ([email protected])]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rapidpm.modul.javafx.textfield.autocomplete;
import java.util.ArrayList;
import java.util.List;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Control;
import javafx.scene.control.ListView;
import org.rapidpm.commons.javafx.textfield.LimitedTextField;
/**
* This class is main Control class which extends from Control
* and also implements basic functions of the AutoCompleteTextFieldFactory
*
* You can easily utilize the AutoCompleteTextField in your application
*
* e.g
*
* //..codes
* AutoCompleteTextField autobox = new AutoCompleteTextField("hello","prefix","dog","city");
* autobox.setListLimit(7);
* //..add autobox to your scene then the output must be like this:
*
*
* @author Narayan G. Maharjan
* @author Sven Ruppert
*/
public class AutoCompleteTextField extends Control implements AutoCompleteTextFieldFactory {
public static final int DEFAULT_LIMIT = 6;
private boolean filterModeStartsWith = true;
private LimitedTextField textbox;
private ListView listview;
private ObservableList data = FXCollections.observableArrayList();
private boolean filterMode;
private int limit;
private List customActionsList = new ArrayList<>();
public AutoCompleteTextField(ObservableList data) {
init();
this.data = data;
}
public AutoCompleteTextField() {
init();
}
public AutoCompleteTextField(AutoCompleteTextField other) {
this.filterModeStartsWith = other.filterModeStartsWith;
this.textbox = other.textbox;
this.listview = other.listview;
this.data = other.data;
this.filterMode = other.filterMode;
this.limit = other.limit;
this.customActionsList = other.customActionsList;
}
private void init() {
getStyleClass().setAll("autofill-text");
textbox = new LimitedTextField();
textbox.setLimit(255);
listview = new ListView();
limit = DEFAULT_LIMIT;
filterMode = false;
listen();
}
public void requestFocus() {
super.requestFocus();
textbox.requestFocus();
}
public T getItem() {
return listview.getSelectionModel().getSelectedItem();
}
public String getText() {
return textbox.getText();
}
@Override
public void setData(ObservableList data) {
this.data = data;
}
@Override
public ObservableList getData() {
return data;
}
@Override
public ListView getListview() {
return listview;
}
@Override
public LimitedTextField getTextbox() {
return textbox;
}
@Override
public void setListLimit(int limit) {
this.limit = limit;
}
@Override
public int getListLimit() {
return limit;
}
@Override
public void setFilterMode(boolean filter) {
filterMode = filter;
}
@Override
public boolean getFilterMode() {
return filterMode;
}
@Override
public void setMinSize(double d, double d1) {
super.setMinSize(d, d1);
textbox.setMinSize(d, d1);
}
@Override
public void setPrefSize(double d, double d1) {
super.setPrefSize(d, d1);
textbox.setPrefSize(d, d1);
}
@Override
public void resize(double d, double d1) {
super.resize(d, d1);
textbox.resize(d, d1);
}
@Override
public void setMaxSize(double d, double d1) {
super.setMaxSize(d, d1);
textbox.setMaxSize(d, d1);
}
public boolean isFilterModeStartsWith() {
return filterModeStartsWith;
}
public void setFilterModeStartsWith(boolean filterModeStartsWith) {
this.filterModeStartsWith = filterModeStartsWith;
}
public List getCustomActionsList() {
return customActionsList;
}
private void listen() {
this.prefHeightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> ov, Number t, Number t1) {
textbox.setPrefHeight(t1.doubleValue());
}
});
this.prefWidthProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> ov, Number t, Number t1) {
textbox.setPrefWidth(t1.doubleValue());
}
});
this.minHeightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> ov, Number t, Number t1) {
textbox.setMinHeight(t1.doubleValue());
}
});
this.maxHeightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> ov, Number t, Number t1) {
textbox.setMaxHeight(t1.doubleValue());
}
});
this.minWidthProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> ov, Number t, Number t1) {
textbox.setMinWidth(t1.doubleValue());
}
});
this.maxWidthProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends Number> ov, Number t, Number t1) {
textbox.setMaxWidth(t1.doubleValue());
}
});
}
}