| 134 | 134 | setProcessors( null ); |
| 135 | 135 | |
| 136 | | // Will create new processors and therefore a new resolved map. |
| 137 | | refreshSelectedTab( getActiveFileEditor() ); |
| 138 | | updateDefinitionPane(); |
| 139 | | } |
| 140 | | ); |
| 141 | | } |
| 142 | | |
| 143 | | /** |
| 144 | | * When tabs are added, hook the various change listeners onto the new tab so |
| 145 | | * that the preview pane refreshes as necessary. |
| 146 | | */ |
| 147 | | private void initTabAddedListener() { |
| 148 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 149 | | |
| 150 | | // Make sure the text processor kicks off when new files are opened. |
| 151 | | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 152 | | |
| 153 | | // Update the preview pane on tab changes. |
| 154 | | tabs.addListener( |
| 155 | | (final Change<? extends Tab> change) -> { |
| 156 | | while( change.next() ) { |
| 157 | | if( change.wasAdded() ) { |
| 158 | | // Multiple tabs can be added simultaneously. |
| 159 | | for( final Tab newTab : change.getAddedSubList() ) { |
| 160 | | final FileEditorTab tab = (FileEditorTab)newTab; |
| 161 | | |
| 162 | | initTextChangeListener( tab ); |
| 163 | | initCaretParagraphListener( tab ); |
| 164 | | initVariableNameInjector( tab ); |
| 165 | | // initSyntaxListener( tab ); |
| 166 | | } |
| 167 | | } |
| 168 | | } |
| 169 | | } |
| 170 | | ); |
| 171 | | } |
| 172 | | |
| 173 | | /** |
| 174 | | * Reloads the preferences from the previous load. |
| 175 | | */ |
| 176 | | private void initPreferences() { |
| 177 | | restoreDefinitionSource(); |
| 178 | | getFileEditorPane().restorePreferences(); |
| 179 | | updateDefinitionPane(); |
| 180 | | } |
| 181 | | |
| 182 | | /** |
| 183 | | * Listen for new tab selection events. |
| 184 | | */ |
| 185 | | private void initTabChangedListener() { |
| 186 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 187 | | |
| 188 | | // Update the preview pane changing tabs. |
| 189 | | editorPane.addTabSelectionListener( |
| 190 | | (ObservableValue<? extends Tab> tabPane, |
| 191 | | final Tab oldTab, final Tab newTab) -> { |
| 192 | | |
| 193 | | // If there was no old tab, then this is a first time load, which |
| 194 | | // can be ignored. |
| 195 | | if( oldTab != null ) { |
| 196 | | if( newTab == null ) { |
| 197 | | closeRemainingTab(); |
| 198 | | } |
| 199 | | else { |
| 200 | | // Update the preview with the edited text. |
| 201 | | refreshSelectedTab( (FileEditorTab)newTab ); |
| 202 | | } |
| 203 | | } |
| 204 | | } |
| 205 | | ); |
| 206 | | } |
| 207 | | |
| 208 | | private void initTextChangeListener( final FileEditorTab tab ) { |
| 209 | | tab.addTextChangeListener( |
| 210 | | (ObservableValue<? extends String> editor, |
| 211 | | final String oldValue, final String newValue) -> { |
| 212 | | refreshSelectedTab( tab ); |
| 213 | | } |
| 214 | | ); |
| 215 | | } |
| 216 | | |
| 217 | | private void initCaretParagraphListener( final FileEditorTab tab ) { |
| 218 | | tab.addCaretParagraphListener( |
| 219 | | (ObservableValue<? extends Integer> editor, |
| 220 | | final Integer oldValue, final Integer newValue) -> { |
| 221 | | refreshSelectedTab( tab ); |
| 222 | | } |
| 223 | | ); |
| 224 | | } |
| 225 | | |
| 226 | | private void initVariableNameInjector( final FileEditorTab tab ) { |
| 227 | | VariableNameInjector.listen( tab, getDefinitionPane() ); |
| 228 | | } |
| 229 | | |
| 230 | | /** |
| 231 | | * Watch for changes to external files. In particular, this awaits |
| 232 | | * modifications to any XSL files associated with XML files being edited. When |
| 233 | | * an XSL file is modified (external to the application), the snitch's ears |
| 234 | | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 235 | | * date with what's on the file system. |
| 236 | | */ |
| 237 | | private void initSnitch() { |
| 238 | | getSnitch().addObserver( this ); |
| 239 | | } |
| 240 | | |
| 241 | | /** |
| 242 | | * Called whenever the preview pane becomes out of sync with the file editor |
| 243 | | * tab. This can be called when the text changes, the caret paragraph changes, |
| 244 | | * or the file tab changes. |
| 245 | | * |
| 246 | | * @param tab The file editor tab that has been changed in some fashion. |
| 247 | | */ |
| 248 | | private void refreshSelectedTab( final FileEditorTab tab ) { |
| 249 | | if( tab.isFileOpen() ) { |
| 250 | | getPreviewPane().setPath( tab.getPath() ); |
| 251 | | |
| 252 | | // TODO: https://github.com/DaveJarvis/scrivenvar/issues/29 |
| 253 | | final Position p = tab.getCaretOffset(); |
| 254 | | getLineNumberText().setText( |
| 255 | | get( STATUS_BAR_LINE, p.getMajor() + 1, p.getMinor() + 1 ) |
| 256 | | ); |
| 257 | | |
| 258 | | Processor<String> processor = getProcessors().get( tab ); |
| 259 | | |
| 260 | | if( processor == null ) { |
| 261 | | processor = createProcessor( tab ); |
| 262 | | getProcessors().put( tab, processor ); |
| 263 | | } |
| 264 | | |
| 265 | | try { |
| 266 | | processor.processChain( tab.getEditorText() ); |
| 267 | | getNotifier().clear(); |
| 268 | | } catch( final Exception ex ) { |
| 269 | | error( ex ); |
| 270 | | } |
| 271 | | } |
| 272 | | } |
| 273 | | |
| 274 | | /** |
| 275 | | * Returns the variable map of interpolated definitions. |
| 276 | | * |
| 277 | | * @return A map to help dereference variables. |
| 278 | | */ |
| 279 | | private Map<String, String> getResolvedMap() { |
| 280 | | return getDefinitionSource().getResolvedMap(); |
| 281 | | } |
| 282 | | |
| 283 | | /** |
| 284 | | * Returns the root node for the hierarchical definition source. |
| 285 | | * |
| 286 | | * @return Data to display in the definition pane. |
| 287 | | */ |
| 288 | | private TreeView<String> getTreeView() { |
| 289 | | try { |
| 290 | | return getDefinitionSource().asTreeView(); |
| 291 | | } catch( Exception e ) { |
| 292 | | error( e ); |
| 293 | | } |
| 294 | | |
| 295 | | return new TreeView<>(); |
| 296 | | } |
| 297 | | |
| 298 | | /** |
| 299 | | * Called when a definition source is opened. |
| 300 | | * |
| 301 | | * @param path Path to the definition source that was opened. |
| 302 | | */ |
| 303 | | private void openDefinition( final Path path ) { |
| 304 | | try { |
| 305 | | final DefinitionSource ds = createDefinitionSource( path.toString() ); |
| 306 | | setDefinitionSource( ds ); |
| 307 | | storeDefinitionSource(); |
| 308 | | updateDefinitionPane(); |
| 309 | | } catch( final Exception e ) { |
| 310 | | error( e ); |
| 311 | | } |
| 312 | | } |
| 313 | | |
| 314 | | private void updateDefinitionPane() { |
| 315 | | getDefinitionPane().setRoot( getDefinitionSource().asTreeView() ); |
| 316 | | } |
| 317 | | |
| 318 | | private void restoreDefinitionSource() { |
| 319 | | final Preferences preferences = getPreferences(); |
| 320 | | final String source = preferences.get( PREFS_DEFINITION_SOURCE, null ); |
| 321 | | |
| 322 | | // If there's no definition source set, don't try to load it. |
| 323 | | if( source != null ) { |
| 324 | | setDefinitionSource( createDefinitionSource( source ) ); |
| 325 | | } |
| 326 | | } |
| 327 | | |
| 328 | | private void storeDefinitionSource() { |
| 329 | | final Preferences preferences = getPreferences(); |
| 330 | | final DefinitionSource ds = getDefinitionSource(); |
| 331 | | |
| 332 | | preferences.put( PREFS_DEFINITION_SOURCE, ds.toString() ); |
| 333 | | } |
| 334 | | |
| 335 | | /** |
| 336 | | * Called when the last open tab is closed to clear the preview pane. |
| 337 | | */ |
| 338 | | private void closeRemainingTab() { |
| 339 | | getPreviewPane().clear(); |
| 340 | | } |
| 341 | | |
| 342 | | /** |
| 343 | | * Called when an exception occurs that warrants the user's attention. |
| 344 | | * |
| 345 | | * @param e The exception with a message that the user should know about. |
| 346 | | */ |
| 347 | | private void error( final Exception e ) { |
| 348 | | getNotifier().notify( e ); |
| 349 | | } |
| 350 | | |
| 351 | | //---- File actions ------------------------------------------------------- |
| 352 | | /** |
| 353 | | * Called when an observable instance has changed. This includes the snitch |
| 354 | | * service and the notify service. |
| 355 | | * |
| 356 | | * @param observable The observed instance. |
| 357 | | * @param value The noteworthy item. |
| 358 | | */ |
| 359 | | @Override |
| 360 | | public void update( final Observable observable, final Object value ) { |
| 361 | | if( value != null ) { |
| 362 | | if( observable instanceof Snitch && value instanceof Path ) { |
| 363 | | update( (Path)value ); |
| 364 | | } |
| 365 | | else if( observable instanceof Notifier && value instanceof String ) { |
| 366 | | final String s = (String)value; |
| 367 | | final int index = s.indexOf( '\n' ); |
| 368 | | final String message = s.substring( 0, index > 0 ? index : s.length() ); |
| 369 | | |
| 370 | | getStatusBar().setText( message ); |
| 371 | | } |
| 372 | | } |
| 373 | | } |
| 374 | | |
| 375 | | /** |
| 376 | | * Called when a file has been modified. |
| 377 | | * |
| 378 | | * @param file Path to the modified file. |
| 379 | | */ |
| 380 | | private void update( final Path file ) { |
| 381 | | // Avoid throwing IllegalStateException by running from a non-JavaFX thread. |
| 382 | | Platform.runLater( |
| 383 | | () -> { |
| 384 | | // Brute-force XSLT file reload by re-instantiating all processors. |
| 385 | | resetProcessors(); |
| 386 | | refreshSelectedTab( getActiveFileEditor() ); |
| 387 | | } |
| 388 | | ); |
| 389 | | } |
| 390 | | |
| 391 | | /** |
| 392 | | * After resetting the processors, they will refresh anew to be up-to-date |
| 393 | | * with the files (text and definition) currently loaded into the editor. |
| 394 | | */ |
| 395 | | private void resetProcessors() { |
| 396 | | getProcessors().clear(); |
| 397 | | } |
| 398 | | |
| 399 | | //---- File actions ------------------------------------------------------- |
| 400 | | private void fileNew() { |
| 401 | | getFileEditorPane().newEditor(); |
| 402 | | } |
| 403 | | |
| 404 | | private void fileOpen() { |
| 405 | | getFileEditorPane().openFileDialog(); |
| 406 | | } |
| 407 | | |
| 408 | | private void fileClose() { |
| 409 | | getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| 410 | | } |
| 411 | | |
| 412 | | private void fileCloseAll() { |
| 413 | | getFileEditorPane().closeAllEditors(); |
| 414 | | } |
| 415 | | |
| 416 | | private void fileSave() { |
| 417 | | getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| 418 | | } |
| 419 | | |
| 420 | | private void fileSaveAll() { |
| 421 | | getFileEditorPane().saveAllEditors(); |
| 422 | | } |
| 423 | | |
| 424 | | private void fileExit() { |
| 425 | | final Window window = getWindow(); |
| 426 | | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 427 | | } |
| 428 | | |
| 429 | | //---- Help actions ------------------------------------------------------- |
| 430 | | private void helpAbout() { |
| 431 | | Alert alert = new Alert( AlertType.INFORMATION ); |
| 432 | | alert.setTitle( get( "Dialog.about.title" ) ); |
| 433 | | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 434 | | alert.setContentText( get( "Dialog.about.content" ) ); |
| 435 | | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 436 | | alert.initOwner( getWindow() ); |
| 437 | | |
| 438 | | alert.showAndWait(); |
| 439 | | } |
| 440 | | |
| 441 | | //---- Convenience accessors ---------------------------------------------- |
| 442 | | private float getFloat( final String key, final float defaultValue ) { |
| 443 | | return getPreferences().getFloat( key, defaultValue ); |
| 444 | | } |
| 445 | | |
| 446 | | private Preferences getPreferences() { |
| 447 | | return getOptions().getState(); |
| 448 | | } |
| 449 | | |
| 450 | | protected Scene getScene() { |
| 451 | | if( this.scene == null ) { |
| 452 | | this.scene = createScene(); |
| 453 | | } |
| 454 | | |
| 455 | | return this.scene; |
| 456 | | } |
| 457 | | |
| 458 | | public Window getWindow() { |
| 459 | | return getScene().getWindow(); |
| 460 | | } |
| 461 | | |
| 462 | | private MarkdownEditorPane getActiveEditor() { |
| 463 | | final EditorPane pane = getActiveFileEditor().getEditorPane(); |
| 464 | | |
| 465 | | return pane instanceof MarkdownEditorPane ? (MarkdownEditorPane)pane : null; |
| 466 | | } |
| 467 | | |
| 468 | | private FileEditorTab getActiveFileEditor() { |
| 469 | | return getFileEditorPane().getActiveFileEditor(); |
| 470 | | } |
| 471 | | |
| 472 | | //---- Member accessors --------------------------------------------------- |
| 473 | | private void setScene( Scene scene ) { |
| 474 | | this.scene = scene; |
| 475 | | } |
| 476 | | |
| 477 | | private void setProcessors( final Map<FileEditorTab, Processor<String>> map ) { |
| 478 | | this.processors = map; |
| 479 | | } |
| 480 | | |
| 481 | | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 482 | | if( this.processors == null ) { |
| 483 | | setProcessors( new HashMap<>() ); |
| 484 | | } |
| 485 | | |
| 486 | | return this.processors; |
| 487 | | } |
| 488 | | |
| 489 | | private FileEditorTabPane getFileEditorPane() { |
| 490 | | if( this.fileEditorPane == null ) { |
| 491 | | this.fileEditorPane = createFileEditorPane(); |
| 492 | | } |
| 493 | | |
| 494 | | return this.fileEditorPane; |
| 495 | | } |
| 496 | | |
| 497 | | private HTMLPreviewPane getPreviewPane() { |
| 498 | | if( this.previewPane == null ) { |
| 499 | | this.previewPane = createPreviewPane(); |
| 500 | | } |
| 501 | | |
| 502 | | return this.previewPane; |
| 503 | | } |
| 504 | | |
| 505 | | private void setDefinitionSource( final DefinitionSource definitionSource ) { |
| 506 | | this.definitionSource = definitionSource; |
| 507 | | } |
| 508 | | |
| 509 | | private DefinitionSource getDefinitionSource() { |
| 510 | | if( this.definitionSource == null ) { |
| 511 | | this.definitionSource = new EmptyDefinitionSource(); |
| 512 | | } |
| 513 | | |
| 514 | | return this.definitionSource; |
| 515 | | } |
| 516 | | |
| 517 | | private DefinitionPane getDefinitionPane() { |
| 518 | | if( this.definitionPane == null ) { |
| 519 | | this.definitionPane = createDefinitionPane(); |
| 520 | | } |
| 521 | | |
| 522 | | return this.definitionPane; |
| 523 | | } |
| 524 | | |
| 525 | | private Options getOptions() { |
| 526 | | return this.options; |
| 527 | | } |
| 528 | | |
| 529 | | private Snitch getSnitch() { |
| 530 | | return this.snitch; |
| 531 | | } |
| 532 | | |
| 533 | | private Notifier getNotifier() { |
| 534 | | return this.notifier; |
| 535 | | } |
| 536 | | |
| 537 | | public void setMenuBar( final MenuBar menuBar ) { |
| 538 | | this.menuBar = menuBar; |
| 539 | | } |
| 540 | | |
| 541 | | public MenuBar getMenuBar() { |
| 542 | | return this.menuBar; |
| 543 | | } |
| 544 | | |
| 545 | | private Text getLineNumberText() { |
| 546 | | if( this.lineNumberText == null ) { |
| 547 | | this.lineNumberText = createLineNumberText(); |
| 548 | | } |
| 549 | | |
| 550 | | return this.lineNumberText; |
| 551 | | } |
| 552 | | |
| 553 | | private synchronized StatusBar getStatusBar() { |
| 554 | | if( this.statusBar == null ) { |
| 555 | | this.statusBar = createStatusBar(); |
| 556 | | } |
| 557 | | |
| 558 | | return this.statusBar; |
| 559 | | } |
| 560 | | |
| 561 | | //---- Member creators ---------------------------------------------------- |
| 562 | | /** |
| 563 | | * Factory to create processors that are suited to different file types. |
| 564 | | * |
| 565 | | * @param tab The tab that is subjected to processing. |
| 566 | | * |
| 567 | | * @return A processor suited to the file type specified by the tab's path. |
| 568 | | */ |
| 569 | | private Processor<String> createProcessor( final FileEditorTab tab ) { |
| 570 | | return createProcessorFactory().createProcessor( tab ); |
| 571 | | } |
| 572 | | |
| 573 | | private ProcessorFactory createProcessorFactory() { |
| 574 | | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 575 | | } |
| 576 | | |
| 577 | | private DefinitionSource createDefinitionSource( final String path ) { |
| 578 | | return createDefinitionFactory().createDefinitionSource( path ); |
| 579 | | } |
| 580 | | |
| 581 | | /** |
| 582 | | * Create an editor pane to hold file editor tabs. |
| 583 | | * |
| 584 | | * @return A new instance, never null. |
| 585 | | */ |
| 586 | | private FileEditorTabPane createFileEditorPane() { |
| 587 | | return new FileEditorTabPane(); |
| 588 | | } |
| 589 | | |
| 590 | | private HTMLPreviewPane createPreviewPane() { |
| 591 | | return new HTMLPreviewPane(); |
| 592 | | } |
| 593 | | |
| 594 | | private DefinitionPane createDefinitionPane() { |
| 595 | | return new DefinitionPane( getTreeView() ); |
| 596 | | } |
| 597 | | |
| 598 | | private DefinitionFactory createDefinitionFactory() { |
| 599 | | return new DefinitionFactory(); |
| 600 | | } |
| 601 | | |
| 602 | | private StatusBar createStatusBar() { |
| 603 | | return new StatusBar(); |
| 604 | | } |
| 605 | | |
| 606 | | private Scene createScene() { |
| 607 | | final SplitPane splitPane = new SplitPane( |
| 608 | | getDefinitionPane().getNode(), |
| 609 | | getFileEditorPane().getNode(), |
| 610 | | getPreviewPane().getNode() ); |
| 611 | | |
| 612 | | splitPane.setDividerPositions( |
| 613 | | getFloat( K_PANE_SPLIT_DEFINITION, .10f ), |
| 614 | | getFloat( K_PANE_SPLIT_EDITOR, .45f ), |
| 615 | | getFloat( K_PANE_SPLIT_PREVIEW, .45f ) ); |
| 616 | | |
| 617 | | // See: http://broadlyapplicable.blogspot.ca/2015/03/javafx-capture-restorePreferences-splitpane.html |
| 618 | | final BorderPane borderPane = new BorderPane(); |
| 619 | | borderPane.setPrefSize( 1024, 800 ); |
| 620 | | borderPane.setTop( createMenuBar() ); |
| 621 | | borderPane.setBottom( getStatusBar() ); |
| 622 | | borderPane.setCenter( splitPane ); |
| 623 | | |
| 624 | | final VBox box = new VBox(); |
| 625 | | box.setAlignment( Pos.BASELINE_CENTER ); |
| 626 | | box.getChildren().add( getLineNumberText() ); |
| 627 | | getStatusBar().getRightItems().add( box ); |
| 628 | | |
| 629 | | return new Scene( borderPane ); |
| 630 | | } |
| 631 | | |
| 632 | | private Text createLineNumberText() { |
| 633 | | return new Text( get( STATUS_BAR_LINE, 1, 1 ) ); |
| 136 | updateDefinitionPane(); |
| 137 | |
| 138 | // Will create new processors and therefore a new resolved map. |
| 139 | refreshSelectedTab( getActiveFileEditor() ); |
| 140 | } |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * When tabs are added, hook the various change listeners onto the new tab so |
| 146 | * that the preview pane refreshes as necessary. |
| 147 | */ |
| 148 | private void initTabAddedListener() { |
| 149 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 150 | |
| 151 | // Make sure the text processor kicks off when new files are opened. |
| 152 | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 153 | |
| 154 | // Update the preview pane on tab changes. |
| 155 | tabs.addListener( |
| 156 | (final Change<? extends Tab> change) -> { |
| 157 | while( change.next() ) { |
| 158 | if( change.wasAdded() ) { |
| 159 | // Multiple tabs can be added simultaneously. |
| 160 | for( final Tab newTab : change.getAddedSubList() ) { |
| 161 | final FileEditorTab tab = (FileEditorTab)newTab; |
| 162 | |
| 163 | initTextChangeListener( tab ); |
| 164 | initCaretParagraphListener( tab ); |
| 165 | initVariableNameInjector( tab ); |
| 166 | // initSyntaxListener( tab ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Reloads the preferences from the previous load. |
| 176 | */ |
| 177 | private void initPreferences() { |
| 178 | restoreDefinitionSource(); |
| 179 | getFileEditorPane().restorePreferences(); |
| 180 | updateDefinitionPane(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Listen for new tab selection events. |
| 185 | */ |
| 186 | private void initTabChangedListener() { |
| 187 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 188 | |
| 189 | // Update the preview pane changing tabs. |
| 190 | editorPane.addTabSelectionListener( |
| 191 | (ObservableValue<? extends Tab> tabPane, |
| 192 | final Tab oldTab, final Tab newTab) -> { |
| 193 | |
| 194 | // If there was no old tab, then this is a first time load, which |
| 195 | // can be ignored. |
| 196 | if( oldTab != null ) { |
| 197 | if( newTab == null ) { |
| 198 | closeRemainingTab(); |
| 199 | } |
| 200 | else { |
| 201 | // Update the preview with the edited text. |
| 202 | refreshSelectedTab( (FileEditorTab)newTab ); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | private void initTextChangeListener( final FileEditorTab tab ) { |
| 210 | tab.addTextChangeListener( |
| 211 | (ObservableValue<? extends String> editor, |
| 212 | final String oldValue, final String newValue) -> { |
| 213 | refreshSelectedTab( tab ); |
| 214 | } |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | private void initCaretParagraphListener( final FileEditorTab tab ) { |
| 219 | tab.addCaretParagraphListener( |
| 220 | (ObservableValue<? extends Integer> editor, |
| 221 | final Integer oldValue, final Integer newValue) -> { |
| 222 | refreshSelectedTab( tab ); |
| 223 | } |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | private void initVariableNameInjector( final FileEditorTab tab ) { |
| 228 | VariableNameInjector.listen( tab, getDefinitionPane() ); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Watch for changes to external files. In particular, this awaits |
| 233 | * modifications to any XSL files associated with XML files being edited. When |
| 234 | * an XSL file is modified (external to the application), the snitch's ears |
| 235 | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 236 | * date with what's on the file system. |
| 237 | */ |
| 238 | private void initSnitch() { |
| 239 | getSnitch().addObserver( this ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Called whenever the preview pane becomes out of sync with the file editor |
| 244 | * tab. This can be called when the text changes, the caret paragraph changes, |
| 245 | * or the file tab changes. |
| 246 | * |
| 247 | * @param tab The file editor tab that has been changed in some fashion. |
| 248 | */ |
| 249 | private void refreshSelectedTab( final FileEditorTab tab ) { |
| 250 | if( tab.isFileOpen() ) { |
| 251 | getPreviewPane().setPath( tab.getPath() ); |
| 252 | |
| 253 | // TODO: https://github.com/DaveJarvis/scrivenvar/issues/29 |
| 254 | final Position p = tab.getCaretOffset(); |
| 255 | getLineNumberText().setText( |
| 256 | get( STATUS_BAR_LINE, |
| 257 | p.getMajor() + 1, |
| 258 | p.getMinor() + 1, |
| 259 | tab.getCaretPosition() + 1 |
| 260 | ) |
| 261 | ); |
| 262 | |
| 263 | Processor<String> processor = getProcessors().get( tab ); |
| 264 | |
| 265 | if( processor == null ) { |
| 266 | processor = createProcessor( tab ); |
| 267 | getProcessors().put( tab, processor ); |
| 268 | } |
| 269 | |
| 270 | try { |
| 271 | getNotifier().clear(); |
| 272 | processor.processChain( tab.getEditorText() ); |
| 273 | } catch( final Exception ex ) { |
| 274 | error( ex ); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Returns the variable map of interpolated definitions. |
| 281 | * |
| 282 | * @return A map to help dereference variables. |
| 283 | */ |
| 284 | private Map<String, String> getResolvedMap() { |
| 285 | return getDefinitionSource().getResolvedMap(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Returns the root node for the hierarchical definition source. |
| 290 | * |
| 291 | * @return Data to display in the definition pane. |
| 292 | */ |
| 293 | private TreeView<String> getTreeView() { |
| 294 | try { |
| 295 | return getDefinitionSource().asTreeView(); |
| 296 | } catch( Exception e ) { |
| 297 | error( e ); |
| 298 | } |
| 299 | |
| 300 | // Slightly redundant as getDefinitionSource() might have returned an |
| 301 | // empty definition source. |
| 302 | return (new EmptyDefinitionSource()).asTreeView(); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Called when a definition source is opened. |
| 307 | * |
| 308 | * @param path Path to the definition source that was opened. |
| 309 | */ |
| 310 | private void openDefinition( final Path path ) { |
| 311 | try { |
| 312 | final DefinitionSource ds = createDefinitionSource( path.toString() ); |
| 313 | setDefinitionSource( ds ); |
| 314 | storeDefinitionSource(); |
| 315 | updateDefinitionPane(); |
| 316 | } catch( final Exception e ) { |
| 317 | error( e ); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | private void updateDefinitionPane() { |
| 322 | getDefinitionPane().setRoot( getDefinitionSource().asTreeView() ); |
| 323 | } |
| 324 | |
| 325 | private void restoreDefinitionSource() { |
| 326 | final Preferences preferences = getPreferences(); |
| 327 | final String source = preferences.get( PREFS_DEFINITION_SOURCE, null ); |
| 328 | |
| 329 | // If there's no definition source set, don't try to load it. |
| 330 | if( source != null ) { |
| 331 | setDefinitionSource( createDefinitionSource( source ) ); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | private void storeDefinitionSource() { |
| 336 | final Preferences preferences = getPreferences(); |
| 337 | final DefinitionSource ds = getDefinitionSource(); |
| 338 | |
| 339 | preferences.put( PREFS_DEFINITION_SOURCE, ds.toString() ); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Called when the last open tab is closed to clear the preview pane. |
| 344 | */ |
| 345 | private void closeRemainingTab() { |
| 346 | getPreviewPane().clear(); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Called when an exception occurs that warrants the user's attention. |
| 351 | * |
| 352 | * @param e The exception with a message that the user should know about. |
| 353 | */ |
| 354 | private void error( final Exception e ) { |
| 355 | getNotifier().notify( e ); |
| 356 | } |
| 357 | |
| 358 | //---- File actions ------------------------------------------------------- |
| 359 | /** |
| 360 | * Called when an observable instance has changed. This includes the snitch |
| 361 | * service and the notify service. |
| 362 | * |
| 363 | * @param observable The observed instance. |
| 364 | * @param value The noteworthy item. |
| 365 | */ |
| 366 | @Override |
| 367 | public void update( final Observable observable, final Object value ) { |
| 368 | if( value != null ) { |
| 369 | if( observable instanceof Snitch && value instanceof Path ) { |
| 370 | update( (Path)value ); |
| 371 | } |
| 372 | else if( observable instanceof Notifier && value instanceof String ) { |
| 373 | final String s = (String)value; |
| 374 | final int index = s.indexOf( '\n' ); |
| 375 | final String message = s.substring( 0, index > 0 ? index : s.length() ); |
| 376 | |
| 377 | getStatusBar().setText( message ); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Called when a file has been modified. |
| 384 | * |
| 385 | * @param file Path to the modified file. |
| 386 | */ |
| 387 | private void update( final Path file ) { |
| 388 | // Avoid throwing IllegalStateException by running from a non-JavaFX thread. |
| 389 | Platform.runLater( |
| 390 | () -> { |
| 391 | // Brute-force XSLT file reload by re-instantiating all processors. |
| 392 | resetProcessors(); |
| 393 | refreshSelectedTab( getActiveFileEditor() ); |
| 394 | } |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * After resetting the processors, they will refresh anew to be up-to-date |
| 400 | * with the files (text and definition) currently loaded into the editor. |
| 401 | */ |
| 402 | private void resetProcessors() { |
| 403 | getProcessors().clear(); |
| 404 | } |
| 405 | |
| 406 | //---- File actions ------------------------------------------------------- |
| 407 | private void fileNew() { |
| 408 | getFileEditorPane().newEditor(); |
| 409 | } |
| 410 | |
| 411 | private void fileOpen() { |
| 412 | getFileEditorPane().openFileDialog(); |
| 413 | } |
| 414 | |
| 415 | private void fileClose() { |
| 416 | getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| 417 | } |
| 418 | |
| 419 | private void fileCloseAll() { |
| 420 | getFileEditorPane().closeAllEditors(); |
| 421 | } |
| 422 | |
| 423 | private void fileSave() { |
| 424 | getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| 425 | } |
| 426 | |
| 427 | private void fileSaveAll() { |
| 428 | getFileEditorPane().saveAllEditors(); |
| 429 | } |
| 430 | |
| 431 | private void fileExit() { |
| 432 | final Window window = getWindow(); |
| 433 | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 434 | } |
| 435 | |
| 436 | //---- Help actions ------------------------------------------------------- |
| 437 | private void helpAbout() { |
| 438 | Alert alert = new Alert( AlertType.INFORMATION ); |
| 439 | alert.setTitle( get( "Dialog.about.title" ) ); |
| 440 | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 441 | alert.setContentText( get( "Dialog.about.content" ) ); |
| 442 | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 443 | alert.initOwner( getWindow() ); |
| 444 | |
| 445 | alert.showAndWait(); |
| 446 | } |
| 447 | |
| 448 | //---- Convenience accessors ---------------------------------------------- |
| 449 | private float getFloat( final String key, final float defaultValue ) { |
| 450 | return getPreferences().getFloat( key, defaultValue ); |
| 451 | } |
| 452 | |
| 453 | private Preferences getPreferences() { |
| 454 | return getOptions().getState(); |
| 455 | } |
| 456 | |
| 457 | protected Scene getScene() { |
| 458 | if( this.scene == null ) { |
| 459 | this.scene = createScene(); |
| 460 | } |
| 461 | |
| 462 | return this.scene; |
| 463 | } |
| 464 | |
| 465 | public Window getWindow() { |
| 466 | return getScene().getWindow(); |
| 467 | } |
| 468 | |
| 469 | private MarkdownEditorPane getActiveEditor() { |
| 470 | final EditorPane pane = getActiveFileEditor().getEditorPane(); |
| 471 | |
| 472 | return pane instanceof MarkdownEditorPane ? (MarkdownEditorPane)pane : null; |
| 473 | } |
| 474 | |
| 475 | private FileEditorTab getActiveFileEditor() { |
| 476 | return getFileEditorPane().getActiveFileEditor(); |
| 477 | } |
| 478 | |
| 479 | //---- Member accessors --------------------------------------------------- |
| 480 | private void setScene( Scene scene ) { |
| 481 | this.scene = scene; |
| 482 | } |
| 483 | |
| 484 | private void setProcessors( final Map<FileEditorTab, Processor<String>> map ) { |
| 485 | this.processors = map; |
| 486 | } |
| 487 | |
| 488 | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 489 | if( this.processors == null ) { |
| 490 | setProcessors( new HashMap<>() ); |
| 491 | } |
| 492 | |
| 493 | return this.processors; |
| 494 | } |
| 495 | |
| 496 | private FileEditorTabPane getFileEditorPane() { |
| 497 | if( this.fileEditorPane == null ) { |
| 498 | this.fileEditorPane = createFileEditorPane(); |
| 499 | } |
| 500 | |
| 501 | return this.fileEditorPane; |
| 502 | } |
| 503 | |
| 504 | private HTMLPreviewPane getPreviewPane() { |
| 505 | if( this.previewPane == null ) { |
| 506 | this.previewPane = createPreviewPane(); |
| 507 | } |
| 508 | |
| 509 | return this.previewPane; |
| 510 | } |
| 511 | |
| 512 | private void setDefinitionSource( final DefinitionSource definitionSource ) { |
| 513 | this.definitionSource = definitionSource; |
| 514 | } |
| 515 | |
| 516 | private DefinitionSource getDefinitionSource() { |
| 517 | if( this.definitionSource == null ) { |
| 518 | this.definitionSource = new EmptyDefinitionSource(); |
| 519 | } |
| 520 | |
| 521 | return this.definitionSource; |
| 522 | } |
| 523 | |
| 524 | private DefinitionPane getDefinitionPane() { |
| 525 | if( this.definitionPane == null ) { |
| 526 | this.definitionPane = createDefinitionPane(); |
| 527 | } |
| 528 | |
| 529 | return this.definitionPane; |
| 530 | } |
| 531 | |
| 532 | private Options getOptions() { |
| 533 | return this.options; |
| 534 | } |
| 535 | |
| 536 | private Snitch getSnitch() { |
| 537 | return this.snitch; |
| 538 | } |
| 539 | |
| 540 | private Notifier getNotifier() { |
| 541 | return this.notifier; |
| 542 | } |
| 543 | |
| 544 | public void setMenuBar( final MenuBar menuBar ) { |
| 545 | this.menuBar = menuBar; |
| 546 | } |
| 547 | |
| 548 | public MenuBar getMenuBar() { |
| 549 | return this.menuBar; |
| 550 | } |
| 551 | |
| 552 | private Text getLineNumberText() { |
| 553 | if( this.lineNumberText == null ) { |
| 554 | this.lineNumberText = createLineNumberText(); |
| 555 | } |
| 556 | |
| 557 | return this.lineNumberText; |
| 558 | } |
| 559 | |
| 560 | private synchronized StatusBar getStatusBar() { |
| 561 | if( this.statusBar == null ) { |
| 562 | this.statusBar = createStatusBar(); |
| 563 | } |
| 564 | |
| 565 | return this.statusBar; |
| 566 | } |
| 567 | |
| 568 | //---- Member creators ---------------------------------------------------- |
| 569 | /** |
| 570 | * Factory to create processors that are suited to different file types. |
| 571 | * |
| 572 | * @param tab The tab that is subjected to processing. |
| 573 | * |
| 574 | * @return A processor suited to the file type specified by the tab's path. |
| 575 | */ |
| 576 | private Processor<String> createProcessor( final FileEditorTab tab ) { |
| 577 | return createProcessorFactory().createProcessor( tab ); |
| 578 | } |
| 579 | |
| 580 | private ProcessorFactory createProcessorFactory() { |
| 581 | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 582 | } |
| 583 | |
| 584 | private DefinitionSource createDefinitionSource( final String path ) { |
| 585 | return createDefinitionFactory().createDefinitionSource( path ); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Create an editor pane to hold file editor tabs. |
| 590 | * |
| 591 | * @return A new instance, never null. |
| 592 | */ |
| 593 | private FileEditorTabPane createFileEditorPane() { |
| 594 | return new FileEditorTabPane(); |
| 595 | } |
| 596 | |
| 597 | private HTMLPreviewPane createPreviewPane() { |
| 598 | return new HTMLPreviewPane(); |
| 599 | } |
| 600 | |
| 601 | private DefinitionPane createDefinitionPane() { |
| 602 | return new DefinitionPane( getTreeView() ); |
| 603 | } |
| 604 | |
| 605 | private DefinitionFactory createDefinitionFactory() { |
| 606 | return new DefinitionFactory(); |
| 607 | } |
| 608 | |
| 609 | private StatusBar createStatusBar() { |
| 610 | return new StatusBar(); |
| 611 | } |
| 612 | |
| 613 | private Scene createScene() { |
| 614 | final SplitPane splitPane = new SplitPane( |
| 615 | getDefinitionPane().getNode(), |
| 616 | getFileEditorPane().getNode(), |
| 617 | getPreviewPane().getNode() ); |
| 618 | |
| 619 | splitPane.setDividerPositions( |
| 620 | getFloat( K_PANE_SPLIT_DEFINITION, .10f ), |
| 621 | getFloat( K_PANE_SPLIT_EDITOR, .45f ), |
| 622 | getFloat( K_PANE_SPLIT_PREVIEW, .45f ) ); |
| 623 | |
| 624 | // See: http://broadlyapplicable.blogspot.ca/2015/03/javafx-capture-restorePreferences-splitpane.html |
| 625 | final BorderPane borderPane = new BorderPane(); |
| 626 | borderPane.setPrefSize( 1024, 800 ); |
| 627 | borderPane.setTop( createMenuBar() ); |
| 628 | borderPane.setBottom( getStatusBar() ); |
| 629 | borderPane.setCenter( splitPane ); |
| 630 | |
| 631 | final VBox box = new VBox(); |
| 632 | box.setAlignment( Pos.BASELINE_CENTER ); |
| 633 | box.getChildren().add( getLineNumberText() ); |
| 634 | getStatusBar().getRightItems().add( box ); |
| 635 | |
| 636 | return new Scene( borderPane ); |
| 637 | } |
| 638 | |
| 639 | private Text createLineNumberText() { |
| 640 | return new Text( get( STATUS_BAR_LINE, 1, 1, 1 ) ); |
| 634 | 641 | } |
| 635 | 642 | |