Search in sources :

Example 1 with Codec

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

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

DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 Codec (org.fxmisc.richtext.model.Codec)2 ReadOnlyStyledDocument (org.fxmisc.richtext.model.ReadOnlyStyledDocument)2 StyledDocument (org.fxmisc.richtext.model.StyledDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 Clipboard (javafx.scene.input.Clipboard)1 DataFormat (javafx.scene.input.DataFormat)1 Either (org.reactfx.util.Either)1