From 224ab4fcf9cfa535cec0e548f0e1a111072c7597 Mon Sep 17 00:00:00 2001 From: leewyatt Date: Thu, 24 Oct 2024 17:52:51 +0900 Subject: [PATCH] Add showPopup property handling to SelectionBoxSkin Implemented the SHOW_POPUP_PROPERTY to manage the visibility of the popup during initialization and property changes. Added a button in the demo application to trigger the popup display, enhancing the control's interactivity. --- .../com/dlsc/gemsfx/demo/SelectionBoxApp.java | 8 ++++++++ .../com/dlsc/gemsfx/skins/SelectionBoxSkin.java | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SelectionBoxApp.java b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SelectionBoxApp.java index 7aad0df7..5ad26deb 100644 --- a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SelectionBoxApp.java +++ b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SelectionBoxApp.java @@ -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()); @@ -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); @@ -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 selectionModeComboBox = new ComboBox<>(); selectionModeComboBox.getItems().addAll(SelectionMode.SINGLE, SelectionMode.MULTIPLE); @@ -204,6 +211,7 @@ public List 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), diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SelectionBoxSkin.java b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SelectionBoxSkin.java index 3e3712f6..372a021b 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SelectionBoxSkin.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/SelectionBoxSkin.java @@ -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; @@ -49,6 +50,7 @@ public class SelectionBoxSkin extends SkinBase> { 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 control; @@ -96,6 +98,16 @@ public SelectionBoxSkin(SelectionBox 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() { @@ -150,12 +162,13 @@ private void addListenerToControl() { control.getProperties().addListener((MapChangeListener) 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); } } });