Search in sources :

Example 1 with DataFormat

use of javafx.scene.input.DataFormat in project RichTextFX by FXMisc.

the class ClipboardHelper method copy.

/**
 * Transfers the currently selected text to the clipboard,
 * leaving the current selection.
 */
default void copy() {
    IndexRange selection = getSelection();
    if (selection.getLength() > 0) {
        ClipboardContent content = new ClipboardContent();
        content.putString(getSelectedText());
        getStyleCodecs().ifPresent(codecs -> {
            Codec<StyledDocument<PS, SEG, S>> codec = ReadOnlyStyledDocument.codec(codecs._1, codecs._2, getSegOps());
            DataFormat format = dataFormat(codec.getName());
            StyledDocument<PS, SEG, S> doc = subDocument(selection.getStart(), selection.getEnd());
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            try {
                codec.encode(dos, doc);
                content.put(format, os.toByteArray());
            } catch (IOException e) {
                System.err.println("Codec error: Exception in encoding '" + codec.getName() + "':");
                e.printStackTrace();
            }
        });
        Clipboard.getSystemClipboard().setContent(content);
    }
}
Also used : IndexRange(javafx.scene.control.IndexRange) ClipboardContent(javafx.scene.input.ClipboardContent) DataOutputStream(java.io.DataOutputStream) ReadOnlyStyledDocument(org.fxmisc.richtext.model.ReadOnlyStyledDocument) StyledDocument(org.fxmisc.richtext.model.StyledDocument) DataFormat(javafx.scene.input.DataFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with DataFormat

use of javafx.scene.input.DataFormat in project jgnash by ccavanaugh.

the class ExceptionDialog method initialize.

@FXML
private void initialize() {
    message.setGraphic(new MaterialDesignLabel(MaterialDesignLabel.MDIcon.EXCLAMATION_TRIANGLE, ThemeManager.getBaseTextHeight() * Alert.HEIGHT_MULTIPLIER, Color.DARKRED));
    closeButton.setOnAction(event -> ((Stage) parent.get().getWindow()).close());
    clipboardButton.setOnAction(event -> {
        Clipboard clipboard = Clipboard.getSystemClipboard();
        Map<DataFormat, Object> map = new HashMap<>();
        map.put(DataFormat.PLAIN_TEXT, stackTrace);
        clipboard.setContent(map);
    });
}
Also used : HashMap(java.util.HashMap) DataFormat(javafx.scene.input.DataFormat) MaterialDesignLabel(jgnash.uifx.resource.font.MaterialDesignLabel) Clipboard(javafx.scene.input.Clipboard) InjectFXML(jgnash.uifx.util.InjectFXML) FXML(javafx.fxml.FXML)

Example 3 with DataFormat

use of javafx.scene.input.DataFormat in project org.csstudio.display.builder by kasemir.

the class DragDropDemo method createScene.

public static Scene createScene() {
    final Text source = new Text(50, 100, "DRAG ME");
    source.setScaleX(2.0);
    source.setScaleY(2.0);
    final Text target = new Text(250, 100, "DROP HERE");
    target.setScaleX(2.0);
    target.setScaleY(2.0);
    DataFormat custom_format = new DataFormat("java:org.csstudio.trends.databrowser3.model.ArchiveDataSource");
    source.setOnDragDetected(e -> {
        final Dragboard db = source.startDragAndDrop(TransferMode.ANY);
        final ClipboardContent content = new ClipboardContent();
        content.putString("Hello!");
        content.put(custom_format, Instant.now());
        db.setContent(content);
        e.consume();
    });
    target.setOnDragOver(e -> {
        final Dragboard db = e.getDragboard();
        System.out.println("Somebody's dragging " + db.getContentTypes());
        e.acceptTransferModes(TransferMode.COPY);
        e.consume();
    });
    target.setOnDragDropped(e -> {
        final Dragboard db = e.getDragboard();
        System.out.println("Somebody dropped " + db.getContentTypes());
        if (db.hasString())
            System.out.println("-> String " + db.getString());
        if (db.hasContent(custom_format))
            System.out.println("-> custom " + db.getContent(custom_format));
        e.setDropCompleted(true);
        e.consume();
    });
    final Group widgets = new Group(source, target);
    final Scene scene = new Scene(widgets);
    return scene;
}
Also used : Group(javafx.scene.Group) ClipboardContent(javafx.scene.input.ClipboardContent) DataFormat(javafx.scene.input.DataFormat) Text(javafx.scene.text.Text) Scene(javafx.scene.Scene) Dragboard(javafx.scene.input.Dragboard)

Example 4 with DataFormat

use of javafx.scene.input.DataFormat in project RichTextFX by FXMisc.

the class ClipboardHelper method paste.

/**
 * Inserts the content from the clipboard into this text-editing area,
 * replacing the current selection. If there is no selection, the content
 * from the clipboard is inserted at the current caret position.
 */
default void paste() {
    Clipboard clipboard = Clipboard.getSystemClipboard();
    if (getStyleCodecs().isPresent()) {
        Tuple2<Codec<PS>, Codec<StyledSegment<SEG, S>>> codecs = getStyleCodecs().get();
        Codec<StyledDocument<PS, SEG, S>> codec = ReadOnlyStyledDocument.codec(codecs._1, codecs._2, getSegOps());
        DataFormat format = dataFormat(codec.getName());
        if (clipboard.hasContent(format)) {
            byte[] bytes = (byte[]) clipboard.getContent(format);
            ByteArrayInputStream is = new ByteArrayInputStream(bytes);
            DataInputStream dis = new DataInputStream(is);
            StyledDocument<PS, SEG, S> doc = null;
            try {
                doc = codec.decode(dis);
            } catch (IOException e) {
                System.err.println("Codec error: Failed to decode '" + codec.getName() + "':");
                e.printStackTrace();
            }
            if (doc != null) {
                replaceSelection(doc);
                return;
            }
        }
    }
    if (clipboard.hasString()) {
        String text = clipboard.getString();
        if (text != null) {
            replaceSelection(text);
        }
    }
}
Also used : ReadOnlyStyledDocument(org.fxmisc.richtext.model.ReadOnlyStyledDocument) StyledDocument(org.fxmisc.richtext.model.StyledDocument) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Codec(org.fxmisc.richtext.model.Codec) ByteArrayInputStream(java.io.ByteArrayInputStream) DataFormat(javafx.scene.input.DataFormat) Clipboard(javafx.scene.input.Clipboard)

Aggregations

DataFormat (javafx.scene.input.DataFormat)4 IOException (java.io.IOException)2 Clipboard (javafx.scene.input.Clipboard)2 ClipboardContent (javafx.scene.input.ClipboardContent)2 ReadOnlyStyledDocument (org.fxmisc.richtext.model.ReadOnlyStyledDocument)2 StyledDocument (org.fxmisc.richtext.model.StyledDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 HashMap (java.util.HashMap)1 FXML (javafx.fxml.FXML)1 Group (javafx.scene.Group)1 Scene (javafx.scene.Scene)1 IndexRange (javafx.scene.control.IndexRange)1 Dragboard (javafx.scene.input.Dragboard)1 Text (javafx.scene.text.Text)1 MaterialDesignLabel (jgnash.uifx.resource.font.MaterialDesignLabel)1 InjectFXML (jgnash.uifx.util.InjectFXML)1 Codec (org.fxmisc.richtext.model.Codec)1