diff --git a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/TextViewApp.java b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/TextViewApp.java index c35a4293..da45eac5 100644 --- a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/TextViewApp.java +++ b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/TextViewApp.java @@ -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); @@ -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); @@ -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)); diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/TextView.java b/gemsfx/src/main/java/com/dlsc/gemsfx/TextView.java index 034a4d28..88f0b62c 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/TextView.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/TextView.java @@ -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; @@ -31,7 +32,11 @@ public class TextView extends Control { public TextView() { getStyleClass().add("text-view"); - listenPropertySelectedTextChanged(); + getProperties().addListener((MapChangeListener) change -> { + if (change.getKey().equals("selected.text")) { + selectedText.set((String) change.getValueAdded()); + } + }); addEventHandler(KeyEvent.KEY_PRESSED, evt -> { if (KeyCodeCombination.keyCombination("shortcut+c").match(evt)) { @@ -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()); } @@ -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); @@ -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); } @@ -99,6 +124,8 @@ public final void clearSelection() { selectedText.set(null); } + // text + private final StringProperty text = new SimpleStringProperty(this, "text"); /** @@ -118,6 +145,8 @@ public final void setText(String text) { textProperty().set(text); } + // selected text + private final ReadOnlyStringWrapper selectedText = new ReadOnlyStringWrapper(this, "selectedText"); /** @@ -132,13 +161,4 @@ public final String getSelectedText() { public final ReadOnlyStringProperty selectedTextProperty() { return selectedText.getReadOnlyProperty(); } - - // listeners - private void listenPropertySelectedTextChanged() { - getProperties().addListener((MapChangeListener) change -> { - if (change.getKey().equals("selected.text")) { - selectedText.set((String) change.getValueAdded()); - } - }); - } } \ No newline at end of file diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/TextViewSkin.java b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/TextViewSkin.java index af2208f5..f1ffe3b5 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/TextViewSkin.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/TextViewSkin.java @@ -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); } /**