| | import javafx.beans.property.SimpleBooleanProperty; |
| | import javafx.beans.value.ObservableBooleanValue; |
| | -import javafx.collections.ListChangeListener.Change; |
| | -import javafx.collections.ObservableList; |
| | -import javafx.event.Event; |
| | -import javafx.scene.Node; |
| | -import javafx.scene.Scene; |
| | -import javafx.scene.control.Alert; |
| | -import javafx.scene.control.Alert.AlertType; |
| | -import javafx.scene.control.Menu; |
| | -import javafx.scene.control.MenuBar; |
| | -import javafx.scene.control.SplitPane; |
| | -import javafx.scene.control.Tab; |
| | -import javafx.scene.control.ToolBar; |
| | -import javafx.scene.control.TreeView; |
| | -import javafx.scene.image.Image; |
| | -import javafx.scene.image.ImageView; |
| | -import static javafx.scene.input.KeyCode.ESCAPE; |
| | -import javafx.scene.input.KeyEvent; |
| | -import static javafx.scene.input.KeyEvent.CHAR_UNDEFINED; |
| | -import static javafx.scene.input.KeyEvent.KEY_PRESSED; |
| | -import javafx.scene.layout.BorderPane; |
| | -import javafx.scene.layout.VBox; |
| | -import javafx.stage.Window; |
| | -import javafx.stage.WindowEvent; |
| | - |
| | -/** |
| | - * Main window containing a tab pane in the center for file editors. |
| | - * |
| | - * @author Karl Tauber and White Magic Software, Ltd. |
| | - */ |
| | -public class MainWindow { |
| | - |
| | - private final Options options = Services.load( Options.class ); |
| | - |
| | - private Scene scene; |
| | - |
| | - private TreeView<String> treeView; |
| | - private FileEditorTabPane fileEditorPane; |
| | - private DefinitionPane definitionPane; |
| | - |
| | - private VariableNameInjector variableNameInjector; |
| | - |
| | - private YamlTreeAdapter yamlTreeAdapter; |
| | - private YamlParser yamlParser; |
| | - |
| | - private MenuBar menuBar; |
| | - |
| | - public MainWindow() { |
| | - initLayout(); |
| | - initVariableNameInjector(); |
| | - } |
| | - |
| | - private void initLayout() { |
| | - final SplitPane splitPane = new SplitPane( |
| | - getDefinitionPane().getNode(), |
| | - getFileEditorPane().getNode() ); |
| | - |
| | - splitPane.setDividerPositions( |
| | - getFloat( K_PANE_SPLIT_DEFINITION, .05f ), |
| | - getFloat( K_PANE_SPLIT_EDITOR, .95f ) ); |
| | - |
| | - // See: http://broadlyapplicable.blogspot.ca/2015/03/javafx-capture-restore-splitpane.html |
| | - final BorderPane borderPane = new BorderPane(); |
| | - borderPane.setPrefSize( 1024, 800 ); |
| | - borderPane.setTop( createMenuBar() ); |
| | - borderPane.setCenter( splitPane ); |
| | - |
| | - setScene( new Scene( borderPane ) ); |
| | - getScene().getStylesheets().add( Constants.STYLESHEET_PREVIEW ); |
| | - getScene().windowProperty().addListener( |
| | - (observable, oldWindow, newWindow) -> { |
| | - newWindow.setOnCloseRequest( e -> { |
| | - if( !getFileEditorPane().closeAllEditors() ) { |
| | - e.consume(); |
| | - } |
| | - } ); |
| | - |
| | - // Workaround JavaFX bug: deselect menubar if window loses focus. |
| | - newWindow.focusedProperty().addListener( |
| | - (obs, oldFocused, newFocused) -> { |
| | - if( !newFocused ) { |
| | - // Send an ESC key event to the menubar |
| | - this.menuBar.fireEvent( |
| | - new KeyEvent( |
| | - KEY_PRESSED, CHAR_UNDEFINED, "", ESCAPE, |
| | - false, false, false, false ) ); |
| | - } |
| | - } ); |
| | - } ); |
| | - } |
| | - |
| | - private void initVariableNameInjector() { |
| | - setVariableNameInjector( new VariableNameInjector( |
| | - getFileEditorPane(), |
| | - getDefinitionPane() ) |
| | - ); |
| | - } |
| | - |
| | - private Window getWindow() { |
| | - return getScene().getWindow(); |
| | - } |
| | - |
| | - public Scene getScene() { |
| | - return this.scene; |
| | - } |
| | - |
| | - private void setScene( Scene scene ) { |
| | - this.scene = scene; |
| | - } |
| | - |
| | - /** |
| | - * Creates a boolean property that is bound to another boolean value of the |
| | - * active editor. |
| | - */ |
| | - private BooleanProperty createActiveBooleanProperty( |
| | - final Function<FileEditorTab, ObservableBooleanValue> func ) { |
| | - |
| | - final BooleanProperty b = new SimpleBooleanProperty(); |
| | - final FileEditorTab fileEditor = getActiveFileEditor(); |
| | - |
| | - if( fileEditor != null ) { |
| | - b.bind( func.apply( fileEditor ) ); |
| | - } |
| | - |
| | - getFileEditorPane().activeFileEditorProperty().addListener( |
| | - (observable, oldFileEditor, newFileEditor) -> { |
| | - b.unbind(); |
| | - |
| | - if( newFileEditor != null ) { |
| | - b.bind( func.apply( newFileEditor ) ); |
| | - } else { |
| | - b.set( false ); |
| | - } |
| | - } ); |
| | - |
| | - return b; |
| | - } |
| | - |
| | - //---- File actions ------------------------------------------------------- |
| | - private void fileNew() { |
| | - getFileEditorPane().newEditor(); |
| | - } |
| | - |
| | - private void fileOpen() { |
| | - getFileEditorPane().openFileDialog(); |
| | - } |
| | - |
| | - private void fileClose() { |
| | - getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| | - } |
| | - |
| | - private void fileCloseAll() { |
| | - getFileEditorPane().closeAllEditors(); |
| | - } |
| | - |
| | - private void fileSave() { |
| | - getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| | - } |
| | - |
| | - private void fileSaveAll() { |
| | - getFileEditorPane().saveAllEditors(); |
| | - } |
| | - |
| | - private void fileExit() { |
| | - final Window window = getWindow(); |
| | - Event.fireEvent( window, |
| | - new WindowEvent( window, WindowEvent.WINDOW_CLOSE_REQUEST ) ); |
| | - } |
| | - |
| | - //---- Tools actions ------------------------------------------------------ |
| | - private void toolsOptions() { |
| | - new OptionsDialog( getWindow() ).showAndWait(); |
| | - } |
| | - |
| | - //---- Help actions ------------------------------------------------------- |
| | - private void helpAbout() { |
| | - Alert alert = new Alert( AlertType.INFORMATION ); |
| | - alert.setTitle( Messages.get( "Dialog.about.title" ) ); |
| | - alert.setHeaderText( Messages.get( "Dialog.about.header" ) ); |
| | - alert.setContentText( Messages.get( "Dialog.about.content" ) ); |
| | - alert.setGraphic( new ImageView( new Image( LOGO_32 ) ) ); |
| | - alert.initOwner( getWindow() ); |
| | - |
| | - alert.showAndWait(); |
| | - } |
| | - |
| | - private FileEditorTabPane getFileEditorPane() { |
| | - if( this.fileEditorPane == null ) { |
| | - this.fileEditorPane = createFileEditorPane(); |
| | - } |
| | - |
| | - return this.fileEditorPane; |
| | - } |
| | - |
| | - private FileEditorTabPane createFileEditorPane() { |
| | - // Create an editor pane to hold file editor tabs. |
| | - final FileEditorTabPane editorPane = new FileEditorTabPane(); |
| | - |
| | - // Make sure the text processor kicks off when new files are opened. |
| | - final ObservableList<Tab> tabs = editorPane.getTabs(); |
| | - |
| | - tabs.addListener((Change<? extends Tab> change) -> { |
| | - while( change.next() ) { |
| | - if( change.wasAdded() ) { |
| | - // Multiple tabs can be added simultaneously. |
| | - for( final Tab tab : change.getAddedSubList() ) { |
| | - final FileEditorTab feTab = (FileEditorTab)tab; |
| | - final HTMLPreviewPane previewPane = feTab.getPreviewPane(); |
| | - final EditorPane editorPane1 = feTab.getEditorPane(); |
| | - |
| | - // Load file and create UI when the tab becomes visible the first time. |
| | - // TODO: Change this to use a factory based on the filename extension. |
| | - // See: https://github.com/DaveJarvis/scrivenvar/issues/17 |
| | - // See: https://github.com/DaveJarvis/scrivenvar/issues/18 |
| | - final Processor<String> hpp = new HTMLPreviewProcessor( previewPane ); |
| | - final Processor<String> mcrp = new MarkdownCaretReplacementProcessor( hpp ); |
| | - final Processor<String> mp = new MarkdownProcessor( mcrp ); |
| | - final Processor<String> mcip = new MarkdownCaretInsertionProcessor( mp, feTab.getEditorPane().getEditor() ); |
| | - final Processor<String> vnp = new VariableProcessor( mcip, getResolvedMap() ); |
| | - final TextChangeProcessor tp = new TextChangeProcessor( vnp ); |
| | - |
| | - editorPane1.addChangeListener( tp ); |
| | - } |
| | - } |
| | - } |
| | - }); |
| | - |
| | - // After the processors are in place, restore the previously closed |
| | - // tabs. Adding them will trigger the change event, above. |
| | - editorPane.restoreState(); |
| | - |
| | - return editorPane; |
| | - } |
| | - |
| | - private MarkdownEditorPane getActiveEditor() { |
| | - return (MarkdownEditorPane)(getActiveFileEditor().getEditorPane()); |
| | - } |
| | - |
| | - private FileEditorTab getActiveFileEditor() { |
| | - return getFileEditorPane().getActiveFileEditor(); |
| | - } |
| | - |
| | - protected DefinitionPane createDefinitionPane() { |
| | - return new DefinitionPane( getTreeView() ); |
| | - } |
| | - |
| | - private DefinitionPane getDefinitionPane() { |
| | - if( this.definitionPane == null ) { |
| | - this.definitionPane = createDefinitionPane(); |
| | - } |
| | - |
| | - return this.definitionPane; |
| | - } |
| | - |
| | - public MenuBar getMenuBar() { |
| | - return menuBar; |
| | - } |
| | - |
| | - public void setMenuBar( MenuBar menuBar ) { |
| | - this.menuBar = menuBar; |
| | - } |
| | - |
| | - public VariableNameInjector getVariableNameInjector() { |
| | - return this.variableNameInjector; |
| | - } |
| | - |
| | - public void setVariableNameInjector( VariableNameInjector variableNameInjector ) { |
| | - this.variableNameInjector = variableNameInjector; |
| | - } |
| | - |
| | - private float getFloat( final String key, final float defaultValue ) { |
| | - return getPreferences().getFloat( key, defaultValue ); |
| | - } |
| | - |
| | - private Preferences getPreferences() { |
| | - return getOptions().getState(); |
| | - } |
| | - |
| | - private Options getOptions() { |
| | - return this.options; |
| | - } |
| | - |
| | - private synchronized TreeView<String> getTreeView() throws RuntimeException { |
| | - if( this.treeView == null ) { |
| | - try { |
| | - this.treeView = createTreeView(); |
| | - } catch( IOException ex ) { |
| | - |
| | - // TODO: Pop an error message. |
| | - throw new RuntimeException( ex ); |
| | - } |
| | - } |
| | - |
| | - return this.treeView; |
| | - } |
| | - |
| | - private InputStream asStream( final String resource ) { |
| | - return getClass().getResourceAsStream( resource ); |
| | - } |
| | - |
| | - private TreeView<String> createTreeView() throws IOException { |
| | - // TODO: Associate variable file with path to current file. |
| | - return getYamlTreeAdapter().adapt( |
| | - asStream( "/com/scrivenvar/variables.yaml" ), |
| | - get( "Pane.defintion.node.root.title" ) |
| | - ); |
| | - } |
| | - |
| | - private Map<String, String> getResolvedMap() { |
| | - return getYamlParser().createResolvedMap(); |
| | - } |
| | - |
| | - private YamlTreeAdapter getYamlTreeAdapter() { |
| | - if( this.yamlTreeAdapter == null ) { |
| | - setYamlTreeAdapter( new YamlTreeAdapter( getYamlParser() ) ); |
| | - } |
| | - |
| | - return this.yamlTreeAdapter; |
| | - } |
| | - |
| | - private void setYamlTreeAdapter( final YamlTreeAdapter yamlTreeAdapter ) { |
| | - this.yamlTreeAdapter = yamlTreeAdapter; |
| | - } |
| | - |
| | - private YamlParser getYamlParser() { |
| | - if( this.yamlParser == null ) { |
| | - setYamlParser( new YamlParser() ); |
| | - } |
| | - |
| | - return this.yamlParser; |
| | - } |
| | - |
| | - private void setYamlParser( final YamlParser yamlParser ) { |
| | - this.yamlParser = yamlParser; |
| | - } |
| | - |
| | - private Node createMenuBar() { |
| | - final BooleanBinding activeFileEditorIsNull = getFileEditorPane().activeFileEditorProperty().isNull(); |
| | - |
| | - // File actions |
| | - Action fileNewAction = new Action( Messages.get( "Main.menu.file.new" ), "Shortcut+N", FILE_ALT, e -> fileNew() ); |
| | - Action fileOpenAction = new Action( Messages.get( "Main.menu.file.open" ), "Shortcut+O", FOLDER_OPEN_ALT, e -> fileOpen() ); |
| | - Action fileCloseAction = new Action( Messages.get( "Main.menu.file.close" ), "Shortcut+W", null, e -> fileClose(), activeFileEditorIsNull ); |
| | - Action fileCloseAllAction = new Action( Messages.get( "Main.menu.file.close_all" ), null, null, e -> fileCloseAll(), activeFileEditorIsNull ); |
| | - Action fileSaveAction = new Action( Messages.get( "Main.menu.file.save" ), "Shortcut+S", FLOPPY_ALT, e -> fileSave(), |
| | - createActiveBooleanProperty( FileEditorTab::modifiedProperty ).not() ); |
| | - Action fileSaveAllAction = new Action( Messages.get( "Main.menu.file.save_all" ), "Shortcut+Shift+S", null, e -> fileSaveAll(), |
| | - Bindings.not( getFileEditorPane().anyFileEditorModifiedProperty() ) ); |
| | - Action fileExitAction = new Action( Messages.get( "Main.menu.file.exit" ), null, null, e -> fileExit() ); |
| | - |
| | - // Edit actions |
| | - Action editUndoAction = new Action( Messages.get( "Main.menu.edit.undo" ), "Shortcut+Z", UNDO, |
| | - e -> getActiveEditor().undo(), |
| | - createActiveBooleanProperty( FileEditorTab::canUndoProperty ).not() ); |
| | - Action editRedoAction = new Action( Messages.get( "Main.menu.edit.redo" ), "Shortcut+Y", REPEAT, |
| | - e -> getActiveEditor().redo(), |
| | - createActiveBooleanProperty( FileEditorTab::canRedoProperty ).not() ); |
| | - |
| | - // Insert actions |
| | - Action insertBoldAction = new Action( Messages.get( "Main.menu.insert.bold" ), "Shortcut+B", BOLD, |
| | - e -> getActiveEditor().surroundSelection( "**", "**" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertItalicAction = new Action( Messages.get( "Main.menu.insert.italic" ), "Shortcut+I", ITALIC, |
| | - e -> getActiveEditor().surroundSelection( "*", "*" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertStrikethroughAction = new Action( Messages.get( "Main.menu.insert.strikethrough" ), "Shortcut+T", STRIKETHROUGH, |
| | - e -> getActiveEditor().surroundSelection( "~~", "~~" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertBlockquoteAction = new Action( Messages.get( "Main.menu.insert.blockquote" ), "Ctrl+Q", QUOTE_LEFT, // not Shortcut+Q because of conflict on Mac |
| | - e -> getActiveEditor().surroundSelection( "\n\n> ", "" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertCodeAction = new Action( Messages.get( "Main.menu.insert.code" ), "Shortcut+K", CODE, |
| | - e -> getActiveEditor().surroundSelection( "`", "`" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertFencedCodeBlockAction = new Action( Messages.get( "Main.menu.insert.fenced_code_block" ), "Shortcut+Shift+K", FILE_CODE_ALT, |
| | - e -> getActiveEditor().surroundSelection( "\n\n```\n", "\n```\n\n", Messages.get( "Main.menu.insert.fenced_code_block.prompt" ) ), |
| | - activeFileEditorIsNull ); |
| | - |
| | - Action insertLinkAction = new Action( Messages.get( "Main.menu.insert.link" ), "Shortcut+L", LINK, |
| | - e -> getActiveEditor().insertLink(), |
| | - activeFileEditorIsNull ); |
| | - Action insertImageAction = new Action( Messages.get( "Main.menu.insert.image" ), "Shortcut+G", PICTURE_ALT, |
| | - e -> getActiveEditor().insertImage(), |
| | - activeFileEditorIsNull ); |
| | - |
| | - final Action[] headers = new Action[ 6 ]; |
| | - |
| | - // Insert header actions (H1 ... H6) |
| | - for( int i = 1; i <= 6; i++ ) { |
| | - final String hashes = new String( new char[ i ] ).replace( "\0", "#" ); |
| | - final String markup = String.format( "\n\n%s ", hashes ); |
| | - final String text = Messages.get( "Main.menu.insert.header_" + i ); |
| | - final String accelerator = "Shortcut+" + i; |
| | - final String prompt = Messages.get( "Main.menu.insert.header_" + i + ".prompt" ); |
| | - |
| | - headers[ i - 1 ] = new Action( text, accelerator, HEADER, |
| | - e -> getActiveEditor().surroundSelection( markup, "", prompt ), |
| | - activeFileEditorIsNull ); |
| | - } |
| | - |
| | - Action insertUnorderedListAction = new Action( Messages.get( "Main.menu.insert.unordered_list" ), "Shortcut+U", LIST_UL, |
| | - e -> getActiveEditor().surroundSelection( "\n\n* ", "" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertOrderedListAction = new Action( Messages.get( "Main.menu.insert.ordered_list" ), "Shortcut+Shift+O", LIST_OL, |
| | - e -> getActiveEditor().surroundSelection( "\n\n1. ", "" ), |
| | - activeFileEditorIsNull ); |
| | - Action insertHorizontalRuleAction = new Action( Messages.get( "Main.menu.insert.horizontal_rule" ), "Shortcut+H", null, |
| | - e -> getActiveEditor().surroundSelection( "\n\n---\n\n", "" ), |
| | - activeFileEditorIsNull ); |
| | - |
| | - // Tools actions |
| | - Action toolsOptionsAction = new Action( Messages.get( "Main.menu.tools.options" ), "Shortcut+,", null, e -> toolsOptions() ); |
| | - |
| | - // Help actions |
| | - Action helpAboutAction = new Action( Messages.get( "Main.menu.help.about" ), null, null, e -> helpAbout() ); |
| | - |
| | - //---- MenuBar ---- |
| | - Menu fileMenu = ActionUtils.createMenu( Messages.get( "Main.menu.file" ), |
| | - fileNewAction, |
| | - fileOpenAction, |
| | - null, |
| | - fileCloseAction, |
| | - fileCloseAllAction, |
| | - null, |
| | - fileSaveAction, |
| | - fileSaveAllAction, |
| | - null, |
| | - fileExitAction ); |
| | - |
| | - Menu editMenu = ActionUtils.createMenu( Messages.get( "Main.menu.edit" ), |
| | - editUndoAction, |
| | - editRedoAction ); |
| | - |
| | - Menu insertMenu = ActionUtils.createMenu( Messages.get( "Main.menu.insert" ), |
| | - insertBoldAction, |
| | - insertItalicAction, |
| | - insertStrikethroughAction, |
| | - insertBlockquoteAction, |
| | - insertCodeAction, |
| | - insertFencedCodeBlockAction, |
| | - null, |
| | - insertLinkAction, |
| | - insertImageAction, |
| | - null, |
| | - headers[ 0 ], |
| | - headers[ 1 ], |
| | - headers[ 2 ], |
| | - headers[ 3 ], |
| | - headers[ 4 ], |
| | - headers[ 5 ], |
| | - null, |
| | - insertUnorderedListAction, |
| | - insertOrderedListAction, |
| | - insertHorizontalRuleAction ); |
| | - |
| | - Menu toolsMenu = ActionUtils.createMenu( Messages.get( "Main.menu.tools" ), |
| | - toolsOptionsAction ); |
| | - |
| | - Menu helpMenu = ActionUtils.createMenu( Messages.get( "Main.menu.help" ), |
| | - helpAboutAction ); |
| | - |
| | - menuBar = new MenuBar( fileMenu, editMenu, insertMenu, toolsMenu, helpMenu ); |
| | - |
| | - //---- ToolBar ---- |
| | - ToolBar toolBar = ActionUtils.createToolBar( |
| | - fileNewAction, |
| | - fileOpenAction, |
| | - fileSaveAction, |
| | - null, |
| | - editUndoAction, |
| | - editRedoAction, |
| | - null, |
| | - insertBoldAction, |
| | - insertItalicAction, |
| | - insertBlockquoteAction, |
| | - insertCodeAction, |
| | - insertFencedCodeBlockAction, |
| | - null, |
| | - insertLinkAction, |
| | - insertImageAction, |
| | - null, |
| | - headers[ 0 ], |
| | - null, |
| | - insertUnorderedListAction, |
| | - insertOrderedListAction ); |
| | - |
| | - return new VBox( menuBar, toolBar ); |
| | - } |
| | - |
| | +import javafx.beans.value.ObservableValue; |
| | +import javafx.collections.ListChangeListener.Change; |
| | +import javafx.collections.ObservableList; |
| | +import javafx.event.Event; |
| | +import javafx.scene.Node; |
| | +import javafx.scene.Scene; |
| | +import javafx.scene.control.Alert; |
| | +import javafx.scene.control.Alert.AlertType; |
| | +import javafx.scene.control.Menu; |
| | +import javafx.scene.control.MenuBar; |
| | +import javafx.scene.control.SplitPane; |
| | +import javafx.scene.control.Tab; |
| | +import javafx.scene.control.ToolBar; |
| | +import javafx.scene.control.TreeView; |
| | +import javafx.scene.image.Image; |
| | +import javafx.scene.image.ImageView; |
| | +import static javafx.scene.input.KeyCode.ESCAPE; |
| | +import javafx.scene.input.KeyEvent; |
| | +import static javafx.scene.input.KeyEvent.CHAR_UNDEFINED; |
| | +import static javafx.scene.input.KeyEvent.KEY_PRESSED; |
| | +import javafx.scene.layout.BorderPane; |
| | +import javafx.scene.layout.VBox; |
| | +import javafx.stage.Window; |
| | +import javafx.stage.WindowEvent; |
| | +import org.fxmisc.richtext.StyleClassedTextArea; |
| | + |
| | +/** |
| | + * Main window containing a tab pane in the center for file editors. |
| | + * |
| | + * @author Karl Tauber and White Magic Software, Ltd. |
| | + */ |
| | +public class MainWindow { |
| | + |
| | + private final Options options = Services.load( Options.class ); |
| | + |
| | + private Scene scene; |
| | + |
| | + private TreeView<String> treeView; |
| | + private FileEditorTabPane fileEditorPane; |
| | + private DefinitionPane definitionPane; |
| | + |
| | + private VariableNameInjector variableNameInjector; |
| | + |
| | + private YamlTreeAdapter yamlTreeAdapter; |
| | + private YamlParser yamlParser; |
| | + |
| | + private MenuBar menuBar; |
| | + |
| | + public MainWindow() { |
| | + initLayout(); |
| | + initVariableNameInjector(); |
| | + } |
| | + |
| | + private void initLayout() { |
| | + final SplitPane splitPane = new SplitPane( |
| | + getDefinitionPane().getNode(), |
| | + getFileEditorPane().getNode() ); |
| | + |
| | + splitPane.setDividerPositions( |
| | + getFloat( K_PANE_SPLIT_DEFINITION, .05f ), |
| | + getFloat( K_PANE_SPLIT_EDITOR, .95f ) ); |
| | + |
| | + // See: http://broadlyapplicable.blogspot.ca/2015/03/javafx-capture-restore-splitpane.html |
| | + final BorderPane borderPane = new BorderPane(); |
| | + borderPane.setPrefSize( 1024, 800 ); |
| | + borderPane.setTop( createMenuBar() ); |
| | + borderPane.setCenter( splitPane ); |
| | + |
| | + setScene( new Scene( borderPane ) ); |
| | + getScene().getStylesheets().add( Constants.STYLESHEET_PREVIEW ); |
| | + getScene().windowProperty().addListener( |
| | + (observable, oldWindow, newWindow) -> { |
| | + newWindow.setOnCloseRequest( e -> { |
| | + if( !getFileEditorPane().closeAllEditors() ) { |
| | + e.consume(); |
| | + } |
| | + } ); |
| | + |
| | + // Workaround JavaFX bug: deselect menubar if window loses focus. |
| | + newWindow.focusedProperty().addListener( |
| | + (obs, oldFocused, newFocused) -> { |
| | + if( !newFocused ) { |
| | + // Send an ESC key event to the menubar |
| | + this.menuBar.fireEvent( |
| | + new KeyEvent( |
| | + KEY_PRESSED, CHAR_UNDEFINED, "", ESCAPE, |
| | + false, false, false, false ) ); |
| | + } |
| | + } ); |
| | + } ); |
| | + } |
| | + |
| | + private void initVariableNameInjector() { |
| | + setVariableNameInjector( new VariableNameInjector( |
| | + getFileEditorPane(), |
| | + getDefinitionPane() ) |
| | + ); |
| | + } |
| | + |
| | + private Window getWindow() { |
| | + return getScene().getWindow(); |
| | + } |
| | + |
| | + public Scene getScene() { |
| | + return this.scene; |
| | + } |
| | + |
| | + private void setScene( Scene scene ) { |
| | + this.scene = scene; |
| | + } |
| | + |
| | + /** |
| | + * Creates a boolean property that is bound to another boolean value of the |
| | + * active editor. |
| | + */ |
| | + private BooleanProperty createActiveBooleanProperty( |
| | + final Function<FileEditorTab, ObservableBooleanValue> func ) { |
| | + |
| | + final BooleanProperty b = new SimpleBooleanProperty(); |
| | + final FileEditorTab fileEditor = getActiveFileEditor(); |
| | + |
| | + if( fileEditor != null ) { |
| | + b.bind( func.apply( fileEditor ) ); |
| | + } |
| | + |
| | + getFileEditorPane().activeFileEditorProperty().addListener( |
| | + (observable, oldFileEditor, newFileEditor) -> { |
| | + b.unbind(); |
| | + |
| | + if( newFileEditor != null ) { |
| | + b.bind( func.apply( newFileEditor ) ); |
| | + } else { |
| | + b.set( false ); |
| | + } |
| | + } ); |
| | + |
| | + return b; |
| | + } |
| | + |
| | + //---- File actions ------------------------------------------------------- |
| | + private void fileNew() { |
| | + getFileEditorPane().newEditor(); |
| | + } |
| | + |
| | + private void fileOpen() { |
| | + getFileEditorPane().openFileDialog(); |
| | + } |
| | + |
| | + private void fileClose() { |
| | + getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| | + } |
| | + |
| | + private void fileCloseAll() { |
| | + getFileEditorPane().closeAllEditors(); |
| | + } |
| | + |
| | + private void fileSave() { |
| | + getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| | + } |
| | + |
| | + private void fileSaveAll() { |
| | + getFileEditorPane().saveAllEditors(); |
| | + } |
| | + |
| | + private void fileExit() { |
| | + final Window window = getWindow(); |
| | + Event.fireEvent( window, |
| | + new WindowEvent( window, WindowEvent.WINDOW_CLOSE_REQUEST ) ); |
| | + } |
| | + |
| | + //---- Tools actions ------------------------------------------------------ |
| | + private void toolsOptions() { |
| | + new OptionsDialog( getWindow() ).showAndWait(); |
| | + } |
| | + |
| | + //---- Help actions ------------------------------------------------------- |
| | + private void helpAbout() { |
| | + Alert alert = new Alert( AlertType.INFORMATION ); |
| | + alert.setTitle( Messages.get( "Dialog.about.title" ) ); |
| | + alert.setHeaderText( Messages.get( "Dialog.about.header" ) ); |
| | + alert.setContentText( Messages.get( "Dialog.about.content" ) ); |
| | + alert.setGraphic( new ImageView( new Image( LOGO_32 ) ) ); |
| | + alert.initOwner( getWindow() ); |
| | + |
| | + alert.showAndWait(); |
| | + } |
| | + |
| | + private FileEditorTabPane getFileEditorPane() { |
| | + if( this.fileEditorPane == null ) { |
| | + this.fileEditorPane = createFileEditorPane(); |
| | + } |
| | + |
| | + return this.fileEditorPane; |
| | + } |
| | + |
| | + private FileEditorTabPane createFileEditorPane() { |
| | + // Create an editor pane to hold file editor tabs. |
| | + final FileEditorTabPane editorPane = new FileEditorTabPane(); |
| | + |
| | + // Make sure the text processor kicks off when new files are opened. |
| | + final ObservableList<Tab> tabs = editorPane.getTabs(); |
| | + |
| | + tabs.addListener( (Change<? extends Tab> change) -> { |
| | + while( change.next() ) { |
| | + if( change.wasAdded() ) { |
| | + // Multiple tabs can be added simultaneously. |
| | + for( final Tab tab : change.getAddedSubList() ) { |
| | + addListener( (FileEditorTab)tab ); |
| | + } |
| | + } |
| | + } |
| | + } ); |
| | + |
| | + // After the processors are in place, restore the previously closed |
| | + // tabs. Adding them will trigger the change event, above. |
| | + editorPane.restoreState(); |
| | + |
| | + return editorPane; |
| | + } |
| | + |
| | + private MarkdownEditorPane getActiveEditor() { |
| | + return (MarkdownEditorPane)(getActiveFileEditor().getEditorPane()); |
| | + } |
| | + |
| | + private FileEditorTab getActiveFileEditor() { |
| | + return getFileEditorPane().getActiveFileEditor(); |
| | + } |
| | + |
| | + /** |
| | + * Monitors the tab (and its text editor) for changes. |
| | + * |
| | + * @see https://github.com/DaveJarvis/scrivenvar/issues/17 |
| | + * @see https://github.com/DaveJarvis/scrivenvar/issues/18 |
| | + * |
| | + * @param tab The file editor tab that contains a text editor. |
| | + */ |
| | + private void addListener( FileEditorTab tab ) { |
| | + final HTMLPreviewPane previewPane = tab.getPreviewPane(); |
| | + final EditorPane editorPanel = tab.getEditorPane(); |
| | + final StyleClassedTextArea editor = editorPanel.getEditor(); |
| | + |
| | + // TODO: Use a factory based on the filename extension. The default |
| | + // extension will be for a markdown file (e.g., on file new). |
| | + final Processor<String> hpp = new HTMLPreviewProcessor( previewPane ); |
| | + final Processor<String> mcrp = new MarkdownCaretReplacementProcessor( hpp ); |
| | + final Processor<String> mp = new MarkdownProcessor( mcrp ); |
| | + final Processor<String> mcip = new MarkdownCaretInsertionProcessor( mp, editor ); |
| | + final Processor<String> vnp = new VariableProcessor( mcip, getResolvedMap() ); |
| | + final TextChangeProcessor tp = new TextChangeProcessor( vnp ); |
| | + |
| | + editorPanel.addChangeListener( tp ); |
| | + editorPanel.getEditor().currentParagraphProperty().addListener( |
| | + (final ObservableValue<? extends Integer> observable, |
| | + final Integer oldValue, final Integer newValue) -> { |
| | + |
| | + // Kick off the processing chain at the variable processor when the |
| | + // cursor changes paragraphs. This might cause some slight duplication |
| | + // when the Enter key is pressed. |
| | + vnp.processChain( editor.getText() ); |
| | + } ); |
| | + } |
| | + |
| | + protected DefinitionPane createDefinitionPane() { |
| | + return new DefinitionPane( getTreeView() ); |
| | + } |
| | + |
| | + private DefinitionPane getDefinitionPane() { |
| | + if( this.definitionPane == null ) { |
| | + this.definitionPane = createDefinitionPane(); |
| | + } |
| | + |
| | + return this.definitionPane; |
| | + } |
| | + |
| | + public MenuBar getMenuBar() { |
| | + return menuBar; |
| | + } |
| | + |
| | + public void setMenuBar( MenuBar menuBar ) { |
| | + this.menuBar = menuBar; |
| | + } |
| | + |
| | + public VariableNameInjector getVariableNameInjector() { |
| | + return this.variableNameInjector; |
| | + } |
| | + |
| | + public void setVariableNameInjector( VariableNameInjector variableNameInjector ) { |
| | + this.variableNameInjector = variableNameInjector; |
| | + } |
| | + |
| | + private float getFloat( final String key, final float defaultValue ) { |
| | + return getPreferences().getFloat( key, defaultValue ); |
| | + } |
| | + |
| | + private Preferences getPreferences() { |
| | + return getOptions().getState(); |
| | + } |
| | + |
| | + private Options getOptions() { |
| | + return this.options; |
| | + } |
| | + |
| | + private synchronized TreeView<String> getTreeView() throws RuntimeException { |
| | + if( this.treeView == null ) { |
| | + try { |
| | + this.treeView = createTreeView(); |
| | + } catch( IOException ex ) { |
| | + |
| | + // TODO: Pop an error message. |
| | + throw new RuntimeException( ex ); |
| | + } |
| | + } |
| | + |
| | + return this.treeView; |
| | + } |
| | + |
| | + private InputStream asStream( final String resource ) { |
| | + return getClass().getResourceAsStream( resource ); |
| | + } |
| | + |
| | + private TreeView<String> createTreeView() throws IOException { |
| | + // TODO: Associate variable file with path to current file. |
| | + return getYamlTreeAdapter().adapt( |
| | + asStream( "/com/scrivenvar/variables.yaml" ), |
| | + get( "Pane.defintion.node.root.title" ) |
| | + ); |
| | + } |
| | + |
| | + private Map<String, String> getResolvedMap() { |
| | + return getYamlParser().createResolvedMap(); |
| | + } |
| | + |
| | + private YamlTreeAdapter getYamlTreeAdapter() { |
| | + if( this.yamlTreeAdapter == null ) { |
| | + setYamlTreeAdapter( new YamlTreeAdapter( getYamlParser() ) ); |
| | + } |
| | + |
| | + return this.yamlTreeAdapter; |
| | + } |
| | + |
| | + private void setYamlTreeAdapter( final YamlTreeAdapter yamlTreeAdapter ) { |
| | + this.yamlTreeAdapter = yamlTreeAdapter; |
| | + } |
| | + |
| | + private YamlParser getYamlParser() { |
| | + if( this.yamlParser == null ) { |
| | + setYamlParser( new YamlParser() ); |
| | + } |
| | + |
| | + return this.yamlParser; |
| | + } |
| | + |
| | + private void setYamlParser( final YamlParser yamlParser ) { |
| | + this.yamlParser = yamlParser; |
| | + } |
| | + |
| | + private Node createMenuBar() { |
| | + final BooleanBinding activeFileEditorIsNull = getFileEditorPane().activeFileEditorProperty().isNull(); |
| | + |
| | + // File actions |
| | + Action fileNewAction = new Action( Messages.get( "Main.menu.file.new" ), "Shortcut+N", FILE_ALT, e -> fileNew() ); |
| | + Action fileOpenAction = new Action( Messages.get( "Main.menu.file.open" ), "Shortcut+O", FOLDER_OPEN_ALT, e -> fileOpen() ); |
| | + Action fileCloseAction = new Action( Messages.get( "Main.menu.file.close" ), "Shortcut+W", null, e -> fileClose(), activeFileEditorIsNull ); |
| | + Action fileCloseAllAction = new Action( Messages.get( "Main.menu.file.close_all" ), null, null, e -> fileCloseAll(), activeFileEditorIsNull ); |
| | + Action fileSaveAction = new Action( Messages.get( "Main.menu.file.save" ), "Shortcut+S", FLOPPY_ALT, e -> fileSave(), |
| | + createActiveBooleanProperty( FileEditorTab::modifiedProperty ).not() ); |
| | + Action fileSaveAllAction = new Action( Messages.get( "Main.menu.file.save_all" ), "Shortcut+Shift+S", null, e -> fileSaveAll(), |
| | + Bindings.not( getFileEditorPane().anyFileEditorModifiedProperty() ) ); |
| | + Action fileExitAction = new Action( Messages.get( "Main.menu.file.exit" ), null, null, e -> fileExit() ); |
| | + |
| | + // Edit actions |
| | + Action editUndoAction = new Action( Messages.get( "Main.menu.edit.undo" ), "Shortcut+Z", UNDO, |
| | + e -> getActiveEditor().undo(), |
| | + createActiveBooleanProperty( FileEditorTab::canUndoProperty ).not() ); |
| | + Action editRedoAction = new Action( Messages.get( "Main.menu.edit.redo" ), "Shortcut+Y", REPEAT, |
| | + e -> getActiveEditor().redo(), |
| | + createActiveBooleanProperty( FileEditorTab::canRedoProperty ).not() ); |
| | + |
| | + // Insert actions |
| | + Action insertBoldAction = new Action( Messages.get( "Main.menu.insert.bold" ), "Shortcut+B", BOLD, |
| | + e -> getActiveEditor().surroundSelection( "**", "**" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertItalicAction = new Action( Messages.get( "Main.menu.insert.italic" ), "Shortcut+I", ITALIC, |
| | + e -> getActiveEditor().surroundSelection( "*", "*" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertStrikethroughAction = new Action( Messages.get( "Main.menu.insert.strikethrough" ), "Shortcut+T", STRIKETHROUGH, |
| | + e -> getActiveEditor().surroundSelection( "~~", "~~" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertBlockquoteAction = new Action( Messages.get( "Main.menu.insert.blockquote" ), "Ctrl+Q", QUOTE_LEFT, // not Shortcut+Q because of conflict on Mac |
| | + e -> getActiveEditor().surroundSelection( "\n\n> ", "" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertCodeAction = new Action( Messages.get( "Main.menu.insert.code" ), "Shortcut+K", CODE, |
| | + e -> getActiveEditor().surroundSelection( "`", "`" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertFencedCodeBlockAction = new Action( Messages.get( "Main.menu.insert.fenced_code_block" ), "Shortcut+Shift+K", FILE_CODE_ALT, |
| | + e -> getActiveEditor().surroundSelection( "\n\n```\n", "\n```\n\n", Messages.get( "Main.menu.insert.fenced_code_block.prompt" ) ), |
| | + activeFileEditorIsNull ); |
| | + |
| | + Action insertLinkAction = new Action( Messages.get( "Main.menu.insert.link" ), "Shortcut+L", LINK, |
| | + e -> getActiveEditor().insertLink(), |
| | + activeFileEditorIsNull ); |
| | + Action insertImageAction = new Action( Messages.get( "Main.menu.insert.image" ), "Shortcut+G", PICTURE_ALT, |
| | + e -> getActiveEditor().insertImage(), |
| | + activeFileEditorIsNull ); |
| | + |
| | + final Action[] headers = new Action[ 6 ]; |
| | + |
| | + // Insert header actions (H1 ... H6) |
| | + for( int i = 1; i <= 6; i++ ) { |
| | + final String hashes = new String( new char[ i ] ).replace( "\0", "#" ); |
| | + final String markup = String.format( "\n\n%s ", hashes ); |
| | + final String text = Messages.get( "Main.menu.insert.header_" + i ); |
| | + final String accelerator = "Shortcut+" + i; |
| | + final String prompt = Messages.get( "Main.menu.insert.header_" + i + ".prompt" ); |
| | + |
| | + headers[ i - 1 ] = new Action( text, accelerator, HEADER, |
| | + e -> getActiveEditor().surroundSelection( markup, "", prompt ), |
| | + activeFileEditorIsNull ); |
| | + } |
| | + |
| | + Action insertUnorderedListAction = new Action( Messages.get( "Main.menu.insert.unordered_list" ), "Shortcut+U", LIST_UL, |
| | + e -> getActiveEditor().surroundSelection( "\n\n* ", "" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertOrderedListAction = new Action( Messages.get( "Main.menu.insert.ordered_list" ), "Shortcut+Shift+O", LIST_OL, |
| | + e -> getActiveEditor().surroundSelection( "\n\n1. ", "" ), |
| | + activeFileEditorIsNull ); |
| | + Action insertHorizontalRuleAction = new Action( Messages.get( "Main.menu.insert.horizontal_rule" ), "Shortcut+H", null, |
| | + e -> getActiveEditor().surroundSelection( "\n\n---\n\n", "" ), |
| | + activeFileEditorIsNull ); |
| | + |
| | + // Tools actions |
| | + Action toolsOptionsAction = new Action( Messages.get( "Main.menu.tools.options" ), "Shortcut+,", null, e -> toolsOptions() ); |
| | + |
| | + // Help actions |
| | + Action helpAboutAction = new Action( Messages.get( "Main.menu.help.about" ), null, null, e -> helpAbout() ); |
| | + |
| | + //---- MenuBar ---- |
| | + Menu fileMenu = ActionUtils.createMenu( Messages.get( "Main.menu.file" ), |
| | + fileNewAction, |
| | + fileOpenAction, |
| | + null, |
| | + fileCloseAction, |
| | + fileCloseAllAction, |
| | + null, |
| | + fileSaveAction, |
| | + fileSaveAllAction, |
| | + null, |
| | + fileExitAction ); |
| | + |
| | + Menu editMenu = ActionUtils.createMenu( Messages.get( "Main.menu.edit" ), |
| | + editUndoAction, |
| | + editRedoAction ); |
| | + |
| | + Menu insertMenu = ActionUtils.createMenu( Messages.get( "Main.menu.insert" ), |
| | + insertBoldAction, |
| | + insertItalicAction, |
| | + insertStrikethroughAction, |
| | + insertBlockquoteAction, |
| | + insertCodeAction, |
| | + insertFencedCodeBlockAction, |
| | + null, |
| | + insertLinkAction, |
| | + insertImageAction, |
| | + null, |
| | + headers[ 0 ], |
| | + headers[ 1 ], |
| | + headers[ 2 ], |
| | + headers[ 3 ], |
| | + headers[ 4 ], |
| | + headers[ 5 ], |
| | + null, |
| | + insertUnorderedListAction, |
| | + insertOrderedListAction, |
| | + insertHorizontalRuleAction ); |
| | + |
| | + Menu toolsMenu = ActionUtils.createMenu( Messages.get( "Main.menu.tools" ), |
| | + toolsOptionsAction ); |
| | + |
| | + Menu helpMenu = ActionUtils.createMenu( Messages.get( "Main.menu.help" ), |
| | + helpAboutAction ); |
| | + |
| | + menuBar = new MenuBar( fileMenu, editMenu, insertMenu, toolsMenu, helpMenu ); |
| | + |
| | + //---- ToolBar ---- |
| | + ToolBar toolBar = ActionUtils.createToolBar( |
| | + fileNewAction, |
| | + fileOpenAction, |
| | + fileSaveAction, |
| | + null, |
| | + editUndoAction, |
| | + editRedoAction, |
| | + null, |
| | + insertBoldAction, |
| | + insertItalicAction, |
| | + insertBlockquoteAction, |
| | + insertCodeAction, |
| | + insertFencedCodeBlockAction, |
| | + null, |
| | + insertLinkAction, |
| | + insertImageAction, |
| | + null, |
| | + headers[ 0 ], |
| | + null, |
| | + insertUnorderedListAction, |
| | + insertOrderedListAction ); |
| | + |
| | + return new VBox( menuBar, toolBar ); |
| | + } |
| | } |
| | |