util
Clone or download
Modified Files
/*
/*
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad
Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel Quiñones (SamuelQuinones)
This program is free software: you can redistribute it and/or modify
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
(at your option) any later version.
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
GNU General Public License for more details.
Originally Written by : Debayan Sutradhar (rnayabed)
Originally Written by : Debayan Sutradhar (rnayabed)
*/
*/
package com.stream_pi.util.combobox;
package com.stream_pi.util.combobox;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import com.stream_pi.util.uihelper.SpaceFiller;
import com.stream_pi.util.uihelper.SpaceFiller;
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;
/**
/**
* Custom Combo Box for Server and Client
* Custom Combo Box for Server and Client
* @param <T> Combo Box List Type
* @param <T> Combo Box List Type
*/
*/
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;
/**
/**
* Sets the parent where the Combo Box will be shown
* Sets the parent where the Combo Box will be shown
* @param parent StackPane where the Combo Box dialog will be shown
* @param parent StackPane where the Combo Box dialog will be shown
*/
*/
public static void setParent(StackPane parent) {
public static void setParent(StackPane parent) {
stackPaneParent = parent;
stackPaneParent = parent;
stackPaneParent.getStyleClass().add("combo_box_pane_parent");
stackPaneParent.getStyleClass().add("combo_box_pane_parent");
stackPaneParent.getChildren().addListener((ListChangeListener<Node>) c ->
stackPaneParent.getChildren().addListener((ListChangeListener<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();
}
}
});
});
}
}
/**
/**
* Constructor to create Combo Box with all the options
* Constructor to create Combo Box with all the options
* @param options List of Options
* @param options List of Options
*/
*/
public StreamPiComboBox(List<T> options)
public StreamPiComboBox(List<T> options)
{
{
setup();
setup();
setOptions(options);
setOptions(options);
}
}
/**
/**
* @return List of Combo Box Options
* @return List of Combo Box Options
*/
*/
public List<T> getOptions()
public List<T> getOptions()
{
{
return options;
return options;
}
}
/**
/**
* Constructor to create Combo Box with empty list
* Constructor to create Combo Box with empty list
*/
*/
public StreamPiComboBox()
public StreamPiComboBox()
{
{
setup();
setup();
}
}
private Label currentSelectedLabel;
private Label currentSelectedLabel;
/**
/**
* init of Combo Box
* init of Combo Box
*/
*/
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();
currentSelectedLabel.getStyleClass().add("combo_box_current_selected_label");
currentSelectedLabel.getStyleClass().add("combo_box_current_selected_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(SpaceFiller.FillerType.HBox),
new SpaceFiller(SpaceFiller.FillerType.HBox),
fontIcon
fontIcon
);
);
}
}
/**
/**
* Set Combo Box options
* Set Combo Box options
* @param options Combo Box Options
* @param options Combo Box Options
*/
*/
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;
/**
/**
* @return Final Scroll Pane
* @return Final Scroll Pane
*/
*/
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.maxHeightProperty().bind(vBox.heightProperty().add(20));
scrollPane.setContent(vBox);
scrollPane.setContent(vBox);
//scrollPane.setMaxHeight(Double.NEGATIVE_INFINITY);
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.getStyleClass().add("combo_box_list_option_button");
optionButton.getStyleClass().add("combo_box_list_option_button");
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;
}
}
/**
/**
* @return Current Selected Option Index
* @return Current Selected Option Index
*/
*/
public int getCurrentIndex()
public int getCurrentIndex()
{
{
return currentIndex;
return currentIndex;
}
}
/**
/**
* @return Current Selected Item
* @return Current Selected Item
*/
*/
public T getCurrentSelectedItem()
public T getCurrentSelectedItem()
{
{
return options.get(currentIndex);
return options.get(currentIndex);
}
}
/**
/**
* @param index Current item index to be selected
* @param index Current item index to be selected
*/
*/
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)));
}
}
/**
/**
* @param text Name of the option
* @param text Name of the option
*/
*/
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;
/**
/**
* Set on click Listener
* Set on click Listener
* @param streamPiComboBoxListener Combo Box Listener, triggered when an option is clicked
* @param streamPiComboBoxListener Combo Box Listener, triggered when an option is clicked
*/
*/
public void setStreamPiComboBoxListener(StreamPiComboBoxListener<T> streamPiComboBoxListener)
public void setStreamPiComboBoxListener(StreamPiComboBoxListener<T> streamPiComboBoxListener)
{
{
this.streamPiComboBoxListener = streamPiComboBoxListener;
this.streamPiComboBoxListener = streamPiComboBoxListener;
}
}
private Node popupNode;
private Node popupNode;
/**
/**
* Adds the Combo Box to the parent node
* Adds the Combo Box to the parent node
*/
*/
public void show()
public void show()
{
{
Platform.runLater(()->{
Platform.runLater(()->{
popupNode = getPopupScrollPane();
popupNode = getPopupScrollPane();
stackPaneParent.getChildren().add(popupNode);
stackPaneParent.getChildren().add(popupNode);
});
});
}
}
/**
/**
* Removes the combo box from the parent pane
* Removes the combo box from the parent pane
*/
*/
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;
/**
/**
* @param streamPiComboBoxFactory Factory for the Combo Box
* @param streamPiComboBoxFactory Factory for the Combo Box
*/
*/
public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory<T> streamPiComboBoxFactory)
public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory<T> streamPiComboBoxFactory)
{
{
this.streamPiComboBoxFactory = streamPiComboBoxFactory;
this.streamPiComboBoxFactory = streamPiComboBoxFactory;
}
}
}
}