Skip to content

Commit

Permalink
Add "Copy All" functionality to TextView and fixed size calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Aug 14, 2024
1 parent 82e93d2 commit dc2655a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TextViewApp extends Application {
@Override
public void start(Stage stage) {
TextView textView = new TextView("Lorem ipsum dolor sit amet consectetur adipiscing elit nunc hendrerit purus, nisi dapibus primis nibh volutpat fringilla ad nisl urna posuere, cubilia sagittis egestas pharetra sociis montes nullam netus erat. Fusce mauris condimentum neque morbi nunc ligula pretium vehicula nulla, platea dictum mus sapien pulvinar eget porta mi praesent, orci hac dignissim suscipit imperdiet sem per a. Mauris pellentesque dui vitae velit netus venenatis diam felis urna ultrices, potenti pretium sociosqu eros dictumst dis aenean nibh cursus, leo sagittis integer nullam malesuada aliquet et metus vulputate. Interdum facilisis congue ac proin libero mus ullamcorper mauris leo imperdiet eleifend porta, posuere dignissim erat tincidunt vehicula habitant taciti porttitor scelerisque laoreet neque. Habitant etiam cubilia tempor inceptos ad aptent est et varius, vitae imperdiet phasellus feugiat class purus curabitur ullamcorper maecenas, venenatis mollis fusce cras leo eros metus proin. Fusce aenean sociosqu dis habitant mi sapien inceptos, orci lacinia nisi nascetur convallis at erat sociis, purus integer arcu feugiat sollicitudin libero.");

textView.setPrefWidth(400);
HBox.setHgrow(textView, Priority.ALWAYS);

Expand All @@ -32,8 +33,8 @@ public void start(Stage stage) {
Button clear = new Button("Clear Selection");
clear.setOnAction(evt -> textView.clearSelection());

Button copy = new Button("Copy Selection to Clipboard");
copy.setOnAction(evt -> textView.copySelection());
Button copyAll = new Button("Copy entire text to clipboard");
copyAll.setOnAction(evt -> textView.copyAll());

ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(25);
Expand All @@ -57,7 +58,7 @@ public void start(Stage stage) {
controls.add(selectedText, 1, 1);

controls.add(clear, 1, 2);
controls.add(copy, 1, 3);
controls.add(copyAll, 1, 3);

HBox hBox = new HBox(10, textView, controls);
hBox.setPadding(new Insets(20));
Expand Down
50 changes: 35 additions & 15 deletions gemsfx/src/main/java/com/dlsc/gemsfx/TextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.MapChangeListener;
import javafx.geometry.Orientation;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Control;
import javafx.scene.control.MenuItem;
Expand All @@ -31,7 +32,11 @@ public class TextView extends Control {
public TextView() {
getStyleClass().add("text-view");

listenPropertySelectedTextChanged();
getProperties().addListener((MapChangeListener<Object, Object>) change -> {
if (change.getKey().equals("selected.text")) {
selectedText.set((String) change.getValueAdded());
}
});

addEventHandler(KeyEvent.KEY_PRESSED, evt -> {
if (KeyCodeCombination.keyCombination("shortcut+c").match(evt)) {
Expand All @@ -51,10 +56,13 @@ public TextView() {

setOnContextMenuRequested(evt -> {
if (getContextMenu() == null) {
// i18n approach copied from TextInputControlBehavior
MenuItem copyItem = new MenuItem("Copy");
copyItem.setOnAction(e -> copySelection());
ContextMenu contextMenu = new ContextMenu(copyItem);
MenuItem copySelectionItem = new MenuItem("Copy Selection");
copySelectionItem.setOnAction(e -> copySelection());

MenuItem copyAllItem = new MenuItem("Copy All");
copyAllItem.setOnAction(e -> copyAll());

ContextMenu contextMenu = new ContextMenu(copyAllItem, copySelectionItem);
setContextMenu(contextMenu);
contextMenu.show(this, evt.getScreenX(), evt.getScreenY());
}
Expand All @@ -71,6 +79,11 @@ public TextView(String text) {
setText(text);
}

@Override
public Orientation getContentBias() {
return Orientation.HORIZONTAL;
}

@Override
protected Skin<?> createDefaultSkin() {
return new TextViewSkin(this);
Expand All @@ -86,9 +99,21 @@ public String getUserAgentStylesheet() {
* that applications can implement their own logic.
*/
public void copySelection() {
doCopy(getSelectedText());
}

/**
* Copy the entire text to the clipboard. This method is intentionally non-final so
* that applications can implement their own logic.
*/
public void copyAll() {
doCopy(getText());
}

private void doCopy(String text) {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putString(getSelectedText());
content.putString(text);
clipboard.setContent(content);
}

Expand All @@ -99,6 +124,8 @@ public final void clearSelection() {
selectedText.set(null);
}

// text

private final StringProperty text = new SimpleStringProperty(this, "text");

/**
Expand All @@ -118,6 +145,8 @@ public final void setText(String text) {
textProperty().set(text);
}

// selected text

private final ReadOnlyStringWrapper selectedText = new ReadOnlyStringWrapper(this, "selectedText");

/**
Expand All @@ -132,13 +161,4 @@ public final String getSelectedText() {
public final ReadOnlyStringProperty selectedTextProperty() {
return selectedText.getReadOnlyProperty();
}

// listeners
private void listenPropertySelectedTextChanged() {
getProperties().addListener((MapChangeListener<Object, Object>) change -> {
if (change.getKey().equals("selected.text")) {
selectedText.set((String) change.getValueAdded());
}
});
}
}
6 changes: 2 additions & 4 deletions gemsfx/src/main/java/com/dlsc/gemsfx/skins/TextViewSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ public TextViewSkin(TextView control) {
getChildren().addAll(selectionContainer, textsContainer);
}


@Override
protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) {
textsContainer.resizeRelocate(contentX, contentY, contentWidth, contentHeight);
selectionContainer.resizeRelocate(contentX, contentY, contentWidth, contentHeight);
protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
return textsContainer.prefHeight(width - leftInset - rightInset);
}

/**
Expand Down

0 comments on commit dc2655a

Please sign in to comment.