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