tornadofx.control.NaviSelect Maven / Gradle / Ivy
package tornadofx.control;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import java.util.function.Function;
import static javafx.beans.binding.Bindings.createStringBinding;
public class NaviSelect extends HBox {
private ComboBox editButton = new ComboBox<>();
private Button gotoButton = new Button();
private Function defaultVisualConverter = t -> { String value = t == null ? null : t.toString(); return value == null ? "" : value; };
private ObjectProperty> visualConverter = new SimpleObjectProperty>(defaultVisualConverter) {
public void set(Function newValue) {
super.set(newValue);
visualBinding.invalidate();
}
};
private ObjectProperty value = new SimpleObjectProperty<>();
private StringBinding visualBinding = createStringBinding(() -> getVisualConverter().apply(getValue()), valueProperty());
public NaviSelect() {
getStyleClass().add("navi-select");
editButton.getStyleClass().add("edit-button");
editButton.valueProperty().bind(visualBinding);
HBox.setHgrow(editButton, Priority.ALWAYS);
editButton.setTooltip(new Tooltip("Edit"));
Pane gotoButtonGraphic = new Pane();
gotoButtonGraphic.getStyleClass().add("icon");
gotoButton.setGraphic(gotoButtonGraphic);
gotoButton.setTooltip(new Tooltip("Goto"));
gotoButton.getStyleClass().add("goto-button");
getChildren().addAll(editButton, gotoButton);
}
public void setOnEdit(EventHandler editHandler) {
editButton.setOnMouseClicked(editHandler);
}
public void setOnGoto(EventHandler gotoHandler) {
gotoButton.setOnAction(gotoHandler);
}
public ObjectProperty valueProperty() { return value; }
public T getValue() { return value.get(); }
public void setValue(T value) { this.value.set(value); }
public ComboBox getEditButton() {
return editButton;
}
public void setEditButton(ComboBox editButton) {
this.editButton = editButton;
}
public Button getGotoButton() {
return gotoButton;
}
public void setGotoButton(Button gotoButton) {
this.gotoButton = gotoButton;
}
public String getUserAgentStylesheet() {
return ListMenu.class.getResource("naviselect.css").toExternalForm();
}
public Function getVisualConverter() {
return visualConverter.get();
}
public ObjectProperty> visualConverterProperty() {
return visualConverter;
}
public void setVisualConverter(Function visualConverter) {
this.visualConverter.set(visualConverter);
}
}