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();
}
}
}
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);
}
}
}
Aggregations