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