util
Clone or download
Modified Files
package com.StreamPi.Util.ComboBox;
package com.StreamPi.Util.ComboBox;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import com.StreamPi.Util.FormHelper.SpaceFiller;
import com.StreamPi.Util.FormHelper.SpaceFiller;
import com.StreamPi.Util.FormHelper.SpaceFiller.FillerType;
import com.StreamPi.Util.FormHelper.SpaceFiller.FillerType;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.javafx.FontIcon;
import javafx.application.Platform;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBox;
public class StreamPiComboBox<T> extends HBox
public class StreamPiComboBox<T> extends HBox
{
{
private List<T> options;
private List<T> options;
private static StackPane stackPaneParent;
private static StackPane stackPaneParent;
public static void setParent(StackPane parent) {
public static void setParent(StackPane parent) {
stackPaneParent = parent;
stackPaneParent = parent;
stackPaneParent.getStyleClass().add("combobox_pane_parent");
stackPaneParent.getStyleClass().add("combobox_pane_parent");
stackPaneParent.getChildren().addListener(new ListChangeListener<Node>()
stackPaneParent.getChildren().addListener(new ListChangeListener<Node>()
{
{
@Override
@Override
public void onChanged(Change<? extends Node> c) {
public void onChanged(Change<? extends Node> c) {
if(stackPaneParent.getChildren().size() > 0)
if(stackPaneParent.getChildren().size() > 0)
{
{
stackPaneParent.setVisible(true);
stackPaneParent.setVisible(true);
stackPaneParent.toFront();
stackPaneParent.toFront();
}
}
else
else
{
{
stackPaneParent.setVisible(false);
stackPaneParent.setVisible(false);
stackPaneParent.toBack();
stackPaneParent.toBack();
}
}
}
}
});
});
}
}
public StreamPiComboBox(List<T> options)
public StreamPiComboBox(List<T> options)
{
{
setup();
setup();
setOptions(options);
setOptions(options);
}
}
public List<T> getOptions()
{
return options;
}
public StreamPiComboBox()
public StreamPiComboBox()
{
{
setup();
setup();
}
}
private Label currentSelectedLabel;
private Label currentSelectedLabel;
private void setup()
private void setup()
{
{
buttons = new ArrayList<>();
buttons = new ArrayList<>();
getStyleClass().add("combo_box");
getStyleClass().add("combo_box");
setOnMouseClicked(event->show());
setOnMouseClicked(event->show());
currentSelectedLabel = new Label();
currentSelectedLabel = new Label();
FontIcon fontIcon = new FontIcon();
FontIcon fontIcon = new FontIcon();
fontIcon.getStyleClass().add("combo_box_drop_down_icon");
fontIcon.getStyleClass().add("combo_box_drop_down_icon");
getChildren().addAll(
getChildren().addAll(
currentSelectedLabel,
currentSelectedLabel,
new SpaceFiller(FillerType.HBox),
new SpaceFiller(FillerType.HBox),
fontIcon
fontIcon
);
);
}
}
public void setOptions(List<T> options)
public void setOptions(List<T> options)
{
{
this.options = options;
this.options = options;
setCurrentSelectedItemIndex(0);
setCurrentSelectedItemIndex(0);
}
}
private int currentIndex = 0;
private int currentIndex = 0;
private List<ToggleButton> buttons;
private List<ToggleButton> buttons;
public ScrollPane getPopupScrollPane()
public ScrollPane getPopupScrollPane()
{
{
buttons.clear();
buttons.clear();
ScrollPane scrollPane = new ScrollPane();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.getStyleClass().add("combo_box_popup");
scrollPane.getStyleClass().add("combo_box_popup");
VBox vBox = new VBox();
VBox vBox = new VBox();
vBox.getStyleClass().add("combo_box_popup_vbox");
vBox.getStyleClass().add("combo_box_popup_vbox");
vBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
vBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10));
scrollPane.setContent(vBox);
scrollPane.setContent(vBox);
for(int i = 0;i<options.size();i++)
for(int i = 0;i<options.size();i++)
{
{
T eachOptionObj = options.get(i);
T eachOptionObj = options.get(i);
String displayText = streamPiComboBoxFactory.getOptionDisplayText(eachOptionObj);
String displayText = streamPiComboBoxFactory.getOptionDisplayText(eachOptionObj);
ToggleButton optionButton = new ToggleButton(displayText);
ToggleButton optionButton = new ToggleButton(displayText);
optionButton.setSelected(i == currentIndex);
optionButton.setSelected(i == currentIndex);
optionButton.setUserData(i);
optionButton.setUserData(i);
optionButton.setOnAction(event->
optionButton.setOnAction(event->
{
{
setCurrentSelectedItemIndex((int) optionButton.getUserData());
setCurrentSelectedItemIndex((int) optionButton.getUserData());
if(streamPiComboBoxListener != null)
if(streamPiComboBoxListener != null)
streamPiComboBoxListener.onNewItemSelected(options.get(currentIndex));
streamPiComboBoxListener.onNewItemSelected(options.get(currentIndex));
destroy();
destroy();
});
});
vBox.getChildren().addAll(optionButton);
vBox.getChildren().addAll(optionButton);
buttons.add(optionButton);
buttons.add(optionButton);
}
}
return scrollPane;
return scrollPane;
}
}
public int getCurrentIndex()
public int getCurrentIndex()
{
{
return currentIndex;
return currentIndex;
}
}
public T getCurrentSelectedItem()
public T getCurrentSelectedItem()
{
{
return options.get(currentIndex);
return options.get(currentIndex);
}
}
public void setCurrentSelectedItemIndex(int index)
public void setCurrentSelectedItemIndex(int index)
{
{
this.currentIndex = index;
this.currentIndex = index;
setCurrentSelectedLabelText(streamPiComboBoxFactory.getOptionDisplayText(options.get(index)));
setCurrentSelectedLabelText(streamPiComboBoxFactory.getOptionDisplayText(options.get(index)));
}
}
public void setCurrentSelectedItem(T object)
{
setCurrentSelectedItemIndex(options.indexOf(object));
}
private void setCurrentSelectedLabelText(String text)
private void setCurrentSelectedLabelText(String text)
{
{
currentSelectedLabel.setText(text);
currentSelectedLabel.setText(text);
}
}
private StreamPiComboBoxListener<T> streamPiComboBoxListener = null;
private StreamPiComboBoxListener<T> streamPiComboBoxListener = null;
public void setStreamPiComboBoxListener(StreamPiComboBoxListener<T> streamPiComboBoxListener)
public void setStreamPiComboBoxListener(StreamPiComboBoxListener<T> streamPiComboBoxListener)
{
{
this.streamPiComboBoxListener = streamPiComboBoxListener;
this.streamPiComboBoxListener = streamPiComboBoxListener;
}
}
private Node popupNode;
private Node popupNode;
public void show()
public void show()
{
{
Platform.runLater(()->{
Platform.runLater(()->{
popupNode = getPopupScrollPane();
popupNode = getPopupScrollPane();
stackPaneParent.getChildren().add(popupNode);
stackPaneParent.getChildren().add(popupNode);
});
});
}
}
public void destroy()
public void destroy()
{
{
Platform.runLater(()->{
Platform.runLater(()->{
stackPaneParent.getChildren().remove(popupNode);
stackPaneParent.getChildren().remove(popupNode);
});
});
}
}
private StreamPiComboBoxFactory<T> streamPiComboBoxFactory;
private StreamPiComboBoxFactory<T> streamPiComboBoxFactory;
public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory<T> streamPiComboBoxFactory)
public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory<T> streamPiComboBoxFactory)
{
{
this.streamPiComboBoxFactory = streamPiComboBoxFactory;
this.streamPiComboBoxFactory = streamPiComboBoxFactory;
}
}
}
}