From 5f3131019050027f9ed8fae4f2397f90b826bc6a Mon Sep 17 00:00:00 2001 From: leewyatt Date: Mon, 29 Apr 2024 18:16:43 +0900 Subject: [PATCH] Refactor and Enhance Copy Functionality for Web Compatibility - Consolidate multiple `selectedProperty().addListener` into a single listener for cleaner and more efficient code. - Introduce `onCopyAction` property to support custom copy actions in JPro web environments, enhancing functionality when running as a web application. --- .../java/com/dlsc/gemsfx/EnhancedLabel.java | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedLabel.java b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedLabel.java index c4debc46..581c5698 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedLabel.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedLabel.java @@ -1,8 +1,5 @@ package com.dlsc.gemsfx; -import java.util.Objects; -import java.util.function.Supplier; - import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleBooleanProperty; @@ -10,6 +7,8 @@ import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.css.PseudoClass; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.control.ContextMenu; import javafx.scene.control.Label; @@ -21,6 +20,9 @@ import javafx.scene.input.KeyCombination; import javafx.scene.input.MouseButton; +import java.util.Objects; +import java.util.function.Supplier; + /** * An enhanced label that allows for selecting the (whole) label and copying to the clipboard * either via keyboard shortcut or via context menu. @@ -53,8 +55,6 @@ private void init() { setFocusTraversable(true); - selectedProperty().addListener(it -> pseudoClassStateChanged(SELECTED_PSEUDO_CLASS, isSelected())); - KeyCombination copy = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN); setOnKeyPressed(evt -> { @@ -70,6 +70,7 @@ private void init() { }); selectedProperty().addListener(it -> { + pseudoClassStateChanged(SELECTED_PSEUDO_CLASS, isSelected()); if (isSelected() && !isFocused()) { requestFocus(); } @@ -83,7 +84,12 @@ private void init() { MenuItem copyItem = new MenuItem(); copyItem.textProperty().bind(copyMenuItemTextProperty()); - copyItem.setOnAction(evt -> copyText()); + copyItem.setOnAction(event -> { + EventHandler handler = getOnCopyAction(); + if (handler != null) { + handler.handle(event); + } + }); ContextMenu contextMenu = new ContextMenu(); contextMenu.getItems().add(copyItem); @@ -100,6 +106,35 @@ private void copyText() { clipboard.setContent(content); } + private void copyText(ActionEvent actionEvent) { + copyText(); + actionEvent.consume(); + } + + private ObjectProperty> onCopyAction; + + public final void setOnCopyAction(EventHandler value) { + onCopyActionProperty().set(value); + } + + public final EventHandler getOnCopyAction() { + return onCopyAction == null ? this::copyText : onCopyAction.get(); + } + + /** + * The action that is executed when the user copies the label text. The default action + * is to copy the text to the clipboard. This action can be overridden by setting a new + * event handler. + * + * @return the action that is executed when the user copies the label text + */ + public final ObjectProperty> onCopyActionProperty() { + if (onCopyAction == null) { + onCopyAction = new SimpleObjectProperty<>(this, "onCopyAction", this::copyText); + } + return onCopyAction; + } + private final BooleanProperty selected = new SimpleBooleanProperty(this, "selected"); public final boolean isSelected() {