| Author | Karl Tauber <email> |
|---|---|
| Date | 2015-07-22 15:33:17 GMT+0200 |
| Commit | bd6a6708c82a49606e0b8abad3cb7753bf84b068 |
| Parent | c848136 |
| import javafx.application.Application; | ||
| import javafx.scene.Scene; | ||
| -import javafx.scene.layout.BorderPane; | ||
| +import javafx.scene.control.SplitPane; | ||
| import javafx.stage.Stage; | ||
| import org.markdownwriterfx.editor.MarkdownEditorPane; | ||
| +import org.markdownwriterfx.preview.MarkdownPreviewPane; | ||
| /** | ||
| * Markdown Writer FX application. | ||
| * | ||
| * @author Karl Tauber | ||
| */ | ||
| public class MarkdownWriterFXApp | ||
| extends Application | ||
| { | ||
| + private MarkdownEditorPane markdownEditorPane; | ||
| + private MarkdownPreviewPane markdownPreviewPane; | ||
| + | ||
| public static void main(String[] args) { | ||
| launch(args); | ||
| } | ||
| @Override | ||
| public void start(Stage primaryStage) throws Exception { | ||
| - MarkdownEditorPane markdownEditorPane = new MarkdownEditorPane(); | ||
| + markdownEditorPane = new MarkdownEditorPane(); | ||
| markdownEditorPane.setMarkdown("# h1\n\n## h2\n\nsome **bold** text\n\n* ul 1\n* ul 2\n* ul 3"); | ||
| - BorderPane content = new BorderPane(markdownEditorPane); | ||
| - content.setPrefSize(600, 800); | ||
| + markdownPreviewPane = new MarkdownPreviewPane(); | ||
| + markdownPreviewPane.markdownASTProperty().bind(markdownEditorPane.markdownASTProperty()); | ||
| + | ||
| + SplitPane content = new SplitPane(markdownEditorPane.getNode(), markdownPreviewPane.getNode()); | ||
| + content.setPrefSize(800, 800); | ||
| primaryStage.setTitle("Markdown Writer FX"); |
| package org.markdownwriterfx.editor; | ||
| -import javafx.scene.layout.BorderPane; | ||
| +import javafx.beans.property.ReadOnlyObjectProperty; | ||
| +import javafx.beans.property.ReadOnlyObjectWrapper; | ||
| +import javafx.beans.value.ObservableValue; | ||
| +import javafx.scene.Node; | ||
| import org.fxmisc.richtext.StyleClassedTextArea; | ||
| import org.pegdown.Extensions; | ||
| */ | ||
| public class MarkdownEditorPane | ||
| - extends BorderPane | ||
| { | ||
| private final StyleClassedTextArea textArea; | ||
| + private final ReadOnlyObjectWrapper<RootNode> markdownAST = new ReadOnlyObjectWrapper<>(); | ||
| private PegDownProcessor pegDownProcessor; | ||
| public MarkdownEditorPane() { | ||
| textArea = new StyleClassedTextArea(); | ||
| textArea.setWrapText(true); | ||
| textArea.getStyleClass().add("markdown-editor"); | ||
| textArea.getStylesheets().add("org/markdownwriterfx/editor/MarkdownEditor.css"); | ||
| textArea.textProperty().addListener((observable, oldText, newText) -> { | ||
| - applyHighlighting(parseMarkdown(newText)); | ||
| + RootNode astRoot = parseMarkdown(newText); | ||
| + applyHighlighting(astRoot); | ||
| + markdownAST.set(astRoot); | ||
| }); | ||
| - | ||
| - setCenter(textArea); | ||
| } | ||
| - public String getMarkdown() { | ||
| - return textArea.getText(); | ||
| + public Node getNode() { | ||
| + return textArea; | ||
| } | ||
| - public void setMarkdown(String markdown) { | ||
| - textArea.replaceText(markdown); | ||
| - } | ||
| + // markdown property | ||
| + public String getMarkdown() { return textArea.getText(); } | ||
| + public void setMarkdown(String markdown) { textArea.replaceText(markdown); } | ||
| + public ObservableValue<String> markdownProperty() { return textArea.textProperty(); } | ||
| + | ||
| + // markdownAST property | ||
| + public RootNode getMarkdownAST() { return markdownAST.get(); } | ||
| + public ReadOnlyObjectProperty<RootNode> markdownASTProperty() { return markdownAST.getReadOnlyProperty(); } | ||
| private RootNode parseMarkdown(String text) { | ||
| +/* | ||
| + * Copyright (c) 2015 Karl Tauber <karl at jformdesigner dot com> | ||
| + * All rights reserved. | ||
| + * | ||
| + * Redistribution and use in source and binary forms, with or without | ||
| + * modification, are permitted provided that the following conditions are met: | ||
| + * | ||
| + * o Redistributions of source code must retain the above copyright | ||
| + * notice, this list of conditions and the following disclaimer. | ||
| + * | ||
| + * o Redistributions in binary form must reproduce the above copyright | ||
| + * notice, this list of conditions and the following disclaimer in the | ||
| + * documentation and/or other materials provided with the distribution. | ||
| + * | ||
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| + */ | ||
| + | ||
| +package org.markdownwriterfx.preview; | ||
| + | ||
| +import javafx.scene.Node; | ||
| +import javafx.scene.control.TextArea; | ||
| +import org.parboiled.support.ToStringFormatter; | ||
| +import org.parboiled.trees.GraphUtils; | ||
| +import org.pegdown.ast.RootNode; | ||
| + | ||
| +/** | ||
| + * Pegdown AST preview. | ||
| + * Prints the AST tree to a text area. | ||
| + * | ||
| + * @author Karl Tauber | ||
| + */ | ||
| +class ASTPreview | ||
| +{ | ||
| + private final TextArea textArea = new TextArea(); | ||
| + | ||
| + ASTPreview() { | ||
| + textArea.setEditable(false); | ||
| + } | ||
| + | ||
| + Node getNode() { | ||
| + return textArea; | ||
| + } | ||
| + | ||
| + void update(RootNode astRoot) { | ||
| + textArea.setText(GraphUtils.printTree(astRoot, new ToStringFormatter<>())); | ||
| + } | ||
| +} | ||
| +/* | ||
| + * Copyright (c) 2015 Karl Tauber <karl at jformdesigner dot com> | ||
| + * All rights reserved. | ||
| + * | ||
| + * Redistribution and use in source and binary forms, with or without | ||
| + * modification, are permitted provided that the following conditions are met: | ||
| + * | ||
| + * o Redistributions of source code must retain the above copyright | ||
| + * notice, this list of conditions and the following disclaimer. | ||
| + * | ||
| + * o Redistributions in binary form must reproduce the above copyright | ||
| + * notice, this list of conditions and the following disclaimer in the | ||
| + * documentation and/or other materials provided with the distribution. | ||
| + * | ||
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| + */ | ||
| + | ||
| +package org.markdownwriterfx.preview; | ||
| + | ||
| +import javafx.beans.property.ObjectProperty; | ||
| +import javafx.beans.property.SimpleObjectProperty; | ||
| +import javafx.scene.Node; | ||
| +import org.pegdown.ast.RootNode; | ||
| + | ||
| +/** | ||
| + * Markdown preview pane. | ||
| + * | ||
| + * Uses pegdown AST. | ||
| + * | ||
| + * @author Karl Tauber | ||
| + */ | ||
| +public class MarkdownPreviewPane | ||
| +{ | ||
| + private final ASTPreview astPreview = new ASTPreview(); | ||
| + | ||
| + public MarkdownPreviewPane() { | ||
| + markdownAST.addListener((observable, oldValue, newValue) -> { | ||
| + astPreview.update(newValue); | ||
| + }); | ||
| + } | ||
| + | ||
| + public Node getNode() { | ||
| + return astPreview.getNode(); | ||
| + } | ||
| + | ||
| + // markdownAST property | ||
| + private final ObjectProperty<RootNode> markdownAST = new SimpleObjectProperty<RootNode>(); | ||
| + public RootNode getMarkdownAST() { return markdownAST.get(); } | ||
| + public void setMarkdownAST(RootNode astRoot) { markdownAST.set(astRoot); } | ||
| + public ObjectProperty<RootNode> markdownASTProperty() { return markdownAST; } | ||
| +} | ||
| Delta | 147 lines added, 14 lines removed, 133-line increase |
|---|