Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with popup visibility in SelectionBox: handling early show/hide calls and property cleanup #210

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class SelectionBoxApp extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
selectionBox.show();

SplitPane splitPane = new SplitPane();
splitPane.setDividerPositions(0.7);
splitPane.getItems().addAll(createControl(), getControlPanel());
Expand All @@ -40,6 +42,7 @@ public void start(Stage primaryStage) throws Exception {
private Region createControl() {
selectionBox.setPrefWidth(220);
selectionBox.getItems().addAll("Item 1", "Item 2", "Item 3", "Option A", "Option B", "Option C", "Option D");
selectionBox.getSelectionModel().selectFirst();
// selectionBox.setItemConverter(new SimpleStringConverter<>(s -> ">>" +s));

StackPane wrapper = new StackPane(selectionBox);
Expand All @@ -48,6 +51,10 @@ private Region createControl() {
}

private Node getControlPanel() {
// show popup button
Button showButton = new Button("Show Popup");
showButton.setOnAction(e -> selectionBox.show());

// selection mode
ComboBox<SelectionMode> selectionModeComboBox = new ComboBox<>();
selectionModeComboBox.getItems().addAll(SelectionMode.SINGLE, SelectionMode.MULTIPLE);
Expand Down Expand Up @@ -204,6 +211,7 @@ public List<String> fromString(String string) {

return new SimpleControlPane(
"SelectionBox",
new SimpleControlPane.ControlItem("Show Popup", showButton),
new SimpleControlPane.ControlItem("Selection Mode", selectionModeComboBox),
new SimpleControlPane.ControlItem("Show Extra Buttons", visibleExtraButtonsCheckBox),
new SimpleControlPane.ControlItem("Change Extra Buttons", changeExtraButtonsButton),
Expand Down
15 changes: 14 additions & 1 deletion gemsfx/src/main/java/com/dlsc/gemsfx/skins/SelectionBoxSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dlsc.gemsfx.CustomPopupControl;
import com.dlsc.gemsfx.SelectionBox;
import com.dlsc.gemsfx.util.UIUtil;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class SelectionBoxSkin<T> extends SkinBase<SelectionBox<T>> {
private static final String UPDATE_POPUP_CONTENT = "updatePopupContent";
private static final String UPDATE_SELECTION_IN_POPUP = "updateSelectionInPopup";
private static final String UPDATE_EXTRA_BUTTONS_POSITION = "updateExtraButtonsPosition";
private static final String SHOW_POPUP_PROPERTY = "showPopup";

private final SelectionBox<T> control;

Expand Down Expand Up @@ -96,6 +98,16 @@ public SelectionBoxSkin(SelectionBox<T> control) {
addListenerToControl();

getChildren().addAll(displayLabel, arrowButton);

// Check during skin initialization if the popup should be displayed
if (control.getProperties().containsKey(SHOW_POPUP_PROPERTY)) {
if (Boolean.TRUE.equals(control.getProperties().get(SHOW_POPUP_PROPERTY))) {
Platform.runLater(this::showPopup);
} else {
hidePopup();
}
control.getProperties().remove(SHOW_POPUP_PROPERTY);
}
}

private void addListenerToControl() {
Expand Down Expand Up @@ -150,12 +162,13 @@ private void addListenerToControl() {

control.getProperties().addListener((MapChangeListener<Object, Object>) change -> {
if (change.wasAdded()) {
if (change.getKey().equals("showPopup")) {
if (change.getKey().equals(SHOW_POPUP_PROPERTY)) {
if (Boolean.TRUE.equals(change.getValueAdded())) {
showPopup();
} else {
hidePopup();
}
control.getProperties().remove(SHOW_POPUP_PROPERTY);
}
}
});
Expand Down