Search in sources :

Example 1 with StyledDocument

use of org.fxmisc.richtext.model.StyledDocument in project RichTextFX by FXMisc.

the class RichText method save.

private void save(File file) {
    StyledDocument<ParStyle, Either<String, LinkedImage>, TextStyle> doc = area.getDocument();
    // Use the Codec to save the document in a binary format
    area.getStyleCodecs().ifPresent(codecs -> {
        Codec<StyledDocument<ParStyle, Either<String, LinkedImage>, TextStyle>> codec = ReadOnlyStyledDocument.codec(codecs._1, codecs._2, area.getSegOps());
        try {
            FileOutputStream fos = new FileOutputStream(file);
            DataOutputStream dos = new DataOutputStream(fos);
            codec.encode(dos, doc);
            fos.close();
        } catch (IOException fnfe) {
            fnfe.printStackTrace();
        }
    });
}
Also used : DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) ReadOnlyStyledDocument(org.fxmisc.richtext.model.ReadOnlyStyledDocument) StyledDocument(org.fxmisc.richtext.model.StyledDocument) Either(org.reactfx.util.Either) IOException(java.io.IOException)

Example 2 with StyledDocument

use of org.fxmisc.richtext.model.StyledDocument 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 3 with StyledDocument

use of org.fxmisc.richtext.model.StyledDocument in project RichTextFX by FXMisc.

the class RichText method load.

private void load(File file) {
    if (area.getStyleCodecs().isPresent()) {
        Tuple2<Codec<ParStyle>, Codec<StyledSegment<Either<String, LinkedImage>, TextStyle>>> codecs = area.getStyleCodecs().get();
        Codec<StyledDocument<ParStyle, Either<String, LinkedImage>, TextStyle>> codec = ReadOnlyStyledDocument.codec(codecs._1, codecs._2, area.getSegOps());
        try {
            FileInputStream fis = new FileInputStream(file);
            DataInputStream dis = new DataInputStream(fis);
            StyledDocument<ParStyle, Either<String, LinkedImage>, TextStyle> doc = codec.decode(dis);
            fis.close();
            if (doc != null) {
                area.replaceSelection(doc);
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : ReadOnlyStyledDocument(org.fxmisc.richtext.model.ReadOnlyStyledDocument) StyledDocument(org.fxmisc.richtext.model.StyledDocument) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) Codec(org.fxmisc.richtext.model.Codec) Either(org.reactfx.util.Either)

Example 4 with StyledDocument

use of org.fxmisc.richtext.model.StyledDocument 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

IOException (java.io.IOException)4 ReadOnlyStyledDocument (org.fxmisc.richtext.model.ReadOnlyStyledDocument)4 StyledDocument (org.fxmisc.richtext.model.StyledDocument)4 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 DataFormat (javafx.scene.input.DataFormat)2 Codec (org.fxmisc.richtext.model.Codec)2 Either (org.reactfx.util.Either)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IndexRange (javafx.scene.control.IndexRange)1 Clipboard (javafx.scene.input.Clipboard)1 ClipboardContent (javafx.scene.input.ClipboardContent)1