Skip to content

Commit

Permalink
Merge pull request #162 from dlsc-software-consulting-gmbh/enhancemen…
Browse files Browse the repository at this point in the history
…t-EnhancedLabel

Refactor and Enhance Copy Functionality for Web Compatibility
  • Loading branch information
dlemmermann authored Apr 29, 2024
2 parents 2f48f32 + 5f31310 commit 87033ce
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions gemsfx/src/main/java/com/dlsc/gemsfx/EnhancedLabel.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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;
import javafx.beans.property.SimpleObjectProperty;
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;
Expand All @@ -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.
Expand Down Expand Up @@ -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 -> {
Expand All @@ -70,6 +70,7 @@ private void init() {
});

selectedProperty().addListener(it -> {
pseudoClassStateChanged(SELECTED_PSEUDO_CLASS, isSelected());
if (isSelected() && !isFocused()) {
requestFocus();
}
Expand All @@ -83,7 +84,12 @@ private void init() {

MenuItem copyItem = new MenuItem();
copyItem.textProperty().bind(copyMenuItemTextProperty());
copyItem.setOnAction(evt -> copyText());
copyItem.setOnAction(event -> {
EventHandler<ActionEvent> handler = getOnCopyAction();
if (handler != null) {
handler.handle(event);
}
});

ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().add(copyItem);
Expand All @@ -100,6 +106,35 @@ private void copyText() {
clipboard.setContent(content);
}

private void copyText(ActionEvent actionEvent) {
copyText();
actionEvent.consume();
}

private ObjectProperty<EventHandler<ActionEvent>> onCopyAction;

public final void setOnCopyAction(EventHandler<ActionEvent> value) {
onCopyActionProperty().set(value);
}

public final EventHandler<ActionEvent> 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<EventHandler<ActionEvent>> 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() {
Expand Down

0 comments on commit 87033ce

Please sign in to comment.