1. Java FX RichTextView API
package com.home.fx; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; import javafx.scene.control.SeparatorMenuItem; import javafx.scene.input.Clipboard; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.LineNumberFactory; public class FxRichText extends CodeArea { //VirtualizedScrollPane scroll; CodeArea text; int fontSize = 14; public FxRichText(Scene scene) { text = this; //new CodeArea(); text.setStyle("-fx-font: " + fontSize + " arial;"); text.setParagraphGraphicFactory(LineNumberFactory.get(text)); ContextMenu menu = new ContextMenu(); addMenuItem(menu, "Clear", () -> text.clear() ); addMenuItem(menu, "Copy", () -> text.copy() ); addMenuItem(menu, "Paste", () -> text.paste()); addMenuItem(menu, "Copy All", () -> { text.selectAll(); text.copy(); text.deselect(); }); addMenuItem(menu, "Paste All", () -> { text.clear(); text.paste(); } ); addMenuItem(menu, "Paste Html", this::pasteHtml); addSeparator(menu); addMenuItem(menu, "Wrap", this::wrapToggle); addMenuItem(menu, "Font(-)", this::fontDown); addMenuItem(menu, "Font(+)", this::fontUp); text.setContextMenu(menu); setSyntaxHighlightPUml(scene); /* scroll = new VirtualizedScrollPane(text); scroll.setVbarPolicy(ScrollBarPolicy.ALWAYS); //)ALWAYS; scroll.setHbarPolicy(ScrollBarPolicy.AS_NEEDED); //scroll.setPannable(true); //scroll.setFitToWidth(true); //scroll.setFitToHeight(true); */ } public Node getLayout() { return text; } public void setSyntaxHighlightPUml(Scene scene) { /* scene.getStylesheets().add(FxRichText.class.getResource("puml-keywords.css").toExternalForm()); text.multiPlainChanges() .successionEnds(Duration.ofMillis(500)) .subscribe(ignore -> text.setStyleSpans(0, FxRichTextPUml.computeHighlighting(text.getText()))); */ } public void setText(String data) { text.clear(); text.appendText(data); } public void append(String data) { text.appendText(data); } public void insert(int index, String data) { text.insertText(index, data); } public void wrapToggle() { text.setWrapText( ! text.isWrapText() ); } public void showLineNumber() { text.setParagraphGraphicFactory(LineNumberFactory.get(text)); } public void pasteHtml() { if( Clipboard.getSystemClipboard().hasHtml() ) { text.clear(); text.appendText( Clipboard.getSystemClipboard().getHtml() ); } } public void fontDown() { if( fontSize > 1 ) { fontSize--; //text.setStyle("-fx-font: " + fontSize + " arial;"); text.setStyle("-fx-font-size: " + fontSize + ";"); } } public void fontUp() { if( fontSize < 127 ) { fontSize++; text.setStyle("-fx-font-size: " + fontSize + ";"); } } public void addMenuItem(ContextMenu menu, String title, Runnable runnable) { MenuItem item = new MenuItem(); item.setText(title); item.setOnAction(e -> runnable.run()); menu.getItems().add(item); } public void addSeparator(ContextMenu menu) { menu.getItems().add(new SeparatorMenuItem()); } }
댓글 없음:
댓글 쓰기