| 70 | 70 | import javafx.stage.Window; |
| 71 | 71 | import javafx.stage.WindowEvent; |
| 72 | | import org.controlsfx.control.StatusBar; |
| 73 | | import org.fxmisc.richtext.model.TwoDimensional.Position; |
| 74 | | |
| 75 | | import java.io.File; |
| 76 | | import java.nio.file.Path; |
| 77 | | import java.util.*; |
| 78 | | import java.util.function.Function; |
| 79 | | import java.util.prefs.Preferences; |
| 80 | | |
| 81 | | import static com.scrivenvar.Constants.*; |
| 82 | | import static com.scrivenvar.Messages.get; |
| 83 | | import static com.scrivenvar.Messages.getLiteral; |
| 84 | | import static com.scrivenvar.util.StageState.*; |
| 85 | | import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| 86 | | import static javafx.event.Event.fireEvent; |
| 87 | | import static javafx.scene.input.KeyCode.ENTER; |
| 88 | | import static javafx.scene.input.KeyCode.TAB; |
| 89 | | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 90 | | |
| 91 | | /** |
| 92 | | * Main window containing a tab pane in the center for file editors. |
| 93 | | * |
| 94 | | * @author Karl Tauber and White Magic Software, Ltd. |
| 95 | | */ |
| 96 | | public class MainWindow implements Observer { |
| 97 | | |
| 98 | | private final Options mOptions = Services.load( Options.class ); |
| 99 | | private final Snitch mSnitch = Services.load( Snitch.class ); |
| 100 | | private final Settings mSettings = Services.load( Settings.class ); |
| 101 | | private final Notifier mNotifier = Services.load( Notifier.class ); |
| 102 | | |
| 103 | | private final Scene mScene; |
| 104 | | private final StatusBar mStatusBar; |
| 105 | | private final Text mLineNumberText; |
| 106 | | private final TextField mFindTextField; |
| 107 | | |
| 108 | | private DefinitionSource mDefinitionSource = createDefaultDefinitionSource(); |
| 109 | | private final DefinitionPane mDefinitionPane = new DefinitionPane(); |
| 110 | | private final HTMLPreviewPane mPreviewPane = new HTMLPreviewPane(); |
| 111 | | private FileEditorTabPane fileEditorPane; |
| 112 | | |
| 113 | | /** |
| 114 | | * Prevents re-instantiation of processing classes. |
| 115 | | */ |
| 116 | | private final Map<FileEditorTab, Processor<String>> mProcessors = |
| 117 | | new HashMap<>(); |
| 118 | | |
| 119 | | private final Map<String, String> mResolvedMap = |
| 120 | | new HashMap<>( DEFAULT_MAP_SIZE ); |
| 121 | | |
| 122 | | /** |
| 123 | | * Listens on the definition pane for double-click events. |
| 124 | | */ |
| 125 | | private VariableNameInjector variableNameInjector; |
| 126 | | |
| 127 | | /** |
| 128 | | * Called when the definition data is changed. |
| 129 | | */ |
| 130 | | final EventHandler<TreeItem.TreeModificationEvent<Event>> mTreeHandler = |
| 131 | | event -> { |
| 132 | | exportDefinitions( getDefinitionPath() ); |
| 133 | | interpolateResolvedMap(); |
| 134 | | refreshActiveTab(); |
| 135 | | }; |
| 136 | | |
| 137 | | final EventHandler<? super KeyEvent> mDefinitionKeyHandler = |
| 138 | | event -> { |
| 139 | | if( event.getCode() == ENTER ) { |
| 140 | | getVariableNameInjector().injectSelectedItem(); |
| 141 | | } |
| 142 | | }; |
| 143 | | |
| 144 | | final EventHandler<? super KeyEvent> mEditorKeyHandler = |
| 145 | | (EventHandler<KeyEvent>) event -> { |
| 146 | | if( event.getCode() == TAB ) { |
| 147 | | getDefinitionPane().requestFocus(); |
| 148 | | event.consume(); |
| 149 | | } |
| 150 | | }; |
| 151 | | |
| 152 | | public MainWindow() { |
| 153 | | mStatusBar = createStatusBar(); |
| 154 | | mLineNumberText = createLineNumberText(); |
| 155 | | mFindTextField = createFindTextField(); |
| 156 | | mScene = createScene(); |
| 157 | | |
| 158 | | initLayout(); |
| 159 | | initFindInput(); |
| 160 | | initSnitch(); |
| 161 | | initDefinitionListener(); |
| 162 | | initTabAddedListener(); |
| 163 | | initTabChangedListener(); |
| 164 | | initPreferences(); |
| 165 | | } |
| 166 | | |
| 167 | | /** |
| 168 | | * Watch for changes to external files. In particular, this awaits |
| 169 | | * modifications to any XSL files associated with XML files being edited. When |
| 170 | | * an XSL file is modified (external to the application), the snitch's ears |
| 171 | | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 172 | | * date with what's on the file system. |
| 173 | | */ |
| 174 | | private void initSnitch() { |
| 175 | | getSnitch().addObserver( this ); |
| 176 | | } |
| 177 | | |
| 178 | | /** |
| 179 | | * Initialize the find input text field to listen on F3, ENTER, and ESCAPE key |
| 180 | | * presses. |
| 181 | | */ |
| 182 | | private void initFindInput() { |
| 183 | | final TextField input = getFindTextField(); |
| 184 | | |
| 185 | | input.setOnKeyPressed( ( KeyEvent event ) -> { |
| 186 | | switch( event.getCode() ) { |
| 187 | | case F3: |
| 188 | | case ENTER: |
| 189 | | findNext(); |
| 190 | | break; |
| 191 | | case F: |
| 192 | | if( !event.isControlDown() ) { |
| 193 | | break; |
| 194 | | } |
| 195 | | case ESCAPE: |
| 196 | | getStatusBar().setGraphic( null ); |
| 197 | | getActiveFileEditor().getEditorPane().requestFocus(); |
| 198 | | break; |
| 199 | | } |
| 200 | | } ); |
| 201 | | |
| 202 | | // Remove when the input field loses focus. |
| 203 | | input.focusedProperty().addListener( |
| 204 | | ( |
| 205 | | final ObservableValue<? extends Boolean> focused, |
| 206 | | final Boolean oFocus, |
| 207 | | final Boolean nFocus ) -> { |
| 208 | | if( !nFocus ) { |
| 209 | | getStatusBar().setGraphic( null ); |
| 210 | | } |
| 211 | | } |
| 212 | | ); |
| 213 | | } |
| 214 | | |
| 215 | | /** |
| 216 | | * Listen for {@link FileEditorTabPane} to receive open definition file event. |
| 217 | | */ |
| 218 | | private void initDefinitionListener() { |
| 219 | | getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| 220 | | ( final ObservableValue<? extends Path> file, |
| 221 | | final Path oldPath, final Path newPath ) -> { |
| 222 | | // Indirectly refresh the resolved map. |
| 223 | | resetProcessors(); |
| 224 | | |
| 225 | | openDefinitions( newPath ); |
| 226 | | |
| 227 | | // Will create new processors and therefore a new resolved map. |
| 228 | | refreshActiveTab(); |
| 229 | | } |
| 230 | | ); |
| 231 | | } |
| 232 | | |
| 233 | | /** |
| 234 | | * When tabs are added, hook the various change listeners onto the new tab so |
| 235 | | * that the preview pane refreshes as necessary. |
| 236 | | */ |
| 237 | | private void initTabAddedListener() { |
| 238 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 239 | | |
| 240 | | // Make sure the text processor kicks off when new files are opened. |
| 241 | | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 242 | | |
| 243 | | // Update the preview pane on tab changes. |
| 244 | | tabs.addListener( |
| 245 | | ( final Change<? extends Tab> change ) -> { |
| 246 | | while( change.next() ) { |
| 247 | | if( change.wasAdded() ) { |
| 248 | | // Multiple tabs can be added simultaneously. |
| 249 | | for( final Tab newTab : change.getAddedSubList() ) { |
| 250 | | final FileEditorTab tab = (FileEditorTab) newTab; |
| 251 | | |
| 252 | | initTextChangeListener( tab ); |
| 253 | | initCaretParagraphListener( tab ); |
| 254 | | initKeyboardEventListeners( tab ); |
| 255 | | // initSyntaxListener( tab ); |
| 256 | | } |
| 257 | | } |
| 258 | | } |
| 259 | | } |
| 260 | | ); |
| 261 | | } |
| 262 | | |
| 263 | | /** |
| 264 | | * Reloads the preferences from the previous session. |
| 265 | | */ |
| 266 | | private void initPreferences() { |
| 267 | | restoreDefinitionPane(); |
| 268 | | getFileEditorPane().restorePreferences(); |
| 269 | | } |
| 270 | | |
| 271 | | /** |
| 272 | | * Listen for new tab selection events. |
| 273 | | */ |
| 274 | | private void initTabChangedListener() { |
| 275 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 276 | | |
| 277 | | // Update the preview pane changing tabs. |
| 278 | | editorPane.addTabSelectionListener( |
| 279 | | ( ObservableValue<? extends Tab> tabPane, |
| 280 | | final Tab oldTab, final Tab newTab ) -> { |
| 281 | | updateVariableNameInjector(); |
| 282 | | |
| 283 | | // If there was no old tab, then this is a first time load, which |
| 284 | | // can be ignored. |
| 285 | | if( oldTab != null ) { |
| 286 | | if( newTab == null ) { |
| 287 | | closeRemainingTab(); |
| 288 | | } |
| 289 | | else { |
| 290 | | // Update the preview with the edited text. |
| 291 | | refreshSelectedTab( (FileEditorTab) newTab ); |
| 292 | | } |
| 293 | | } |
| 294 | | } |
| 295 | | ); |
| 296 | | } |
| 297 | | |
| 298 | | /** |
| 299 | | * Ensure that the keyboard events are received when a new tab is added |
| 300 | | * to the user interface. |
| 301 | | * |
| 302 | | * @param tab The tab that can trigger keyboard events, such as control+space. |
| 303 | | */ |
| 304 | | private void initKeyboardEventListeners( final FileEditorTab tab ) { |
| 305 | | final VariableNameInjector vin = getVariableNameInjector(); |
| 306 | | vin.initKeyboardEventListeners( tab ); |
| 307 | | |
| 308 | | tab.addEventFilter( KeyEvent.KEY_PRESSED, mEditorKeyHandler ); |
| 309 | | } |
| 310 | | |
| 311 | | private void initTextChangeListener( final FileEditorTab tab ) { |
| 312 | | tab.addTextChangeListener( |
| 313 | | ( ObservableValue<? extends String> editor, |
| 314 | | final String oldValue, final String newValue ) -> |
| 315 | | refreshSelectedTab( tab ) |
| 316 | | ); |
| 317 | | } |
| 318 | | |
| 319 | | private void initCaretParagraphListener( final FileEditorTab tab ) { |
| 320 | | tab.addCaretParagraphListener( |
| 321 | | ( ObservableValue<? extends Integer> editor, |
| 322 | | final Integer oldValue, final Integer newValue ) -> |
| 323 | | refreshSelectedTab( tab ) |
| 324 | | ); |
| 325 | | } |
| 326 | | |
| 327 | | private void updateVariableNameInjector() { |
| 328 | | getVariableNameInjector().setFileEditorTab( getActiveFileEditor() ); |
| 329 | | } |
| 330 | | |
| 331 | | private void setVariableNameInjector( final VariableNameInjector injector ) { |
| 332 | | this.variableNameInjector = injector; |
| 333 | | } |
| 334 | | |
| 335 | | private synchronized VariableNameInjector getVariableNameInjector() { |
| 336 | | if( this.variableNameInjector == null ) { |
| 337 | | final VariableNameInjector vin = createVariableNameInjector(); |
| 338 | | setVariableNameInjector( vin ); |
| 339 | | } |
| 340 | | |
| 341 | | return this.variableNameInjector; |
| 342 | | } |
| 343 | | |
| 344 | | private VariableNameInjector createVariableNameInjector() { |
| 345 | | final FileEditorTab tab = getActiveFileEditor(); |
| 346 | | final DefinitionPane pane = getDefinitionPane(); |
| 347 | | |
| 348 | | return new VariableNameInjector( tab, pane ); |
| 349 | | } |
| 350 | | |
| 351 | | /** |
| 352 | | * Called whenever the preview pane becomes out of sync with the file editor |
| 353 | | * tab. This can be called when the text changes, the caret paragraph changes, |
| 354 | | * or the file tab changes. |
| 355 | | * |
| 356 | | * @param tab The file editor tab that has been changed in some fashion. |
| 357 | | */ |
| 358 | | private void refreshSelectedTab( final FileEditorTab tab ) { |
| 359 | | if( tab == null ) { |
| 360 | | return; |
| 361 | | } |
| 362 | | |
| 363 | | getPreviewPane().setPath( tab.getPath() ); |
| 364 | | |
| 365 | | // TODO: https://github.com/DaveJarvis/scrivenvar/issues/29 |
| 366 | | final Position p = tab.getCaretOffset(); |
| 367 | | getLineNumberText().setText( |
| 368 | | get( STATUS_BAR_LINE, |
| 369 | | p.getMajor() + 1, |
| 370 | | p.getMinor() + 1, |
| 371 | | tab.getCaretPosition() + 1 |
| 372 | | ) |
| 373 | | ); |
| 374 | | |
| 375 | | Processor<String> processor = getProcessors().get( tab ); |
| 376 | | |
| 377 | | if( processor == null ) { |
| 378 | | processor = createProcessor( tab ); |
| 379 | | getProcessors().put( tab, processor ); |
| 380 | | } |
| 381 | | |
| 382 | | try { |
| 383 | | processor.processChain( tab.getEditorText() ); |
| 384 | | } catch( final Exception ex ) { |
| 385 | | error( ex ); |
| 386 | | } |
| 387 | | } |
| 388 | | |
| 389 | | private void refreshActiveTab() { |
| 390 | | refreshSelectedTab( getActiveFileEditor() ); |
| 391 | | } |
| 392 | | |
| 393 | | /** |
| 394 | | * Used to find text in the active file editor window. |
| 395 | | */ |
| 396 | | private void find() { |
| 397 | | final TextField input = getFindTextField(); |
| 398 | | getStatusBar().setGraphic( input ); |
| 399 | | input.requestFocus(); |
| 400 | | } |
| 401 | | |
| 402 | | public void findNext() { |
| 403 | | getActiveFileEditor().searchNext( getFindTextField().getText() ); |
| 404 | | } |
| 405 | | |
| 406 | | /** |
| 407 | | * Returns the variable map of interpolated definitions. |
| 408 | | * |
| 409 | | * @return A map to help dereference variables. |
| 410 | | */ |
| 411 | | private Map<String, String> getResolvedMap() { |
| 412 | | return mResolvedMap; |
| 413 | | } |
| 414 | | |
| 415 | | private void interpolateResolvedMap() { |
| 416 | | final Map<String, String> treeMap = getDefinitionPane().toMap(); |
| 417 | | final Map<String, String> map = new HashMap<>( treeMap ); |
| 418 | | MapInterpolator.interpolate( map ); |
| 419 | | |
| 420 | | getResolvedMap().clear(); |
| 421 | | getResolvedMap().putAll( map ); |
| 422 | | } |
| 423 | | |
| 424 | | /** |
| 425 | | * Called when a definition source is opened. |
| 426 | | * |
| 427 | | * @param path Path to the definition source that was opened. |
| 428 | | */ |
| 429 | | private void openDefinitions( final Path path ) { |
| 430 | | try { |
| 431 | | final DefinitionSource ds = createDefinitionSource( path ); |
| 432 | | setDefinitionSource( ds ); |
| 433 | | storeDefinitionSourceFilename( path ); |
| 434 | | |
| 435 | | final DefinitionPane pane = getDefinitionPane(); |
| 436 | | pane.update( ds ); |
| 437 | | pane.addTreeChangeHandler( mTreeHandler ); |
| 438 | | pane.addKeyEventHandler( mDefinitionKeyHandler ); |
| 439 | | |
| 440 | | interpolateResolvedMap(); |
| 441 | | } catch( final Exception e ) { |
| 442 | | error( e ); |
| 443 | | } |
| 444 | | } |
| 445 | | |
| 446 | | private void exportDefinitions( final Path path ) { |
| 447 | | try { |
| 448 | | final DefinitionPane pane = getDefinitionPane(); |
| 449 | | final TreeItem<String> root = pane.getTreeView().getRoot(); |
| 450 | | final TreeItem<String> problemChild = pane.isTreeWellFormed(); |
| 451 | | |
| 452 | | if( problemChild == null ) { |
| 453 | | getDefinitionSource().getTreeAdapter().export( root, path ); |
| 454 | | getNotifier().clear(); |
| 455 | | } |
| 456 | | else { |
| 457 | | final String msg = get( "yaml.error.tree.form", |
| 458 | | problemChild.getValue() ); |
| 459 | | getNotifier().notify( msg ); |
| 460 | | } |
| 461 | | } catch( final Exception e ) { |
| 462 | | error( e ); |
| 463 | | } |
| 464 | | } |
| 465 | | |
| 466 | | private Path getDefinitionPath() { |
| 467 | | final String source = getPreferences().get( |
| 468 | | PERSIST_DEFINITION_SOURCE, "" ); |
| 469 | | |
| 470 | | return new File( |
| 471 | | source.isBlank() |
| 472 | | ? getSetting( "file.definition.default", "variables.yaml" ) |
| 473 | | : source |
| 474 | | ).toPath(); |
| 475 | | } |
| 476 | | |
| 477 | | private void restoreDefinitionPane() { |
| 478 | | openDefinitions( getDefinitionPath() ); |
| 479 | | } |
| 480 | | |
| 481 | | private void storeDefinitionSourceFilename( final Path path ) { |
| 482 | | getPreferences().put( PERSIST_DEFINITION_SOURCE, path.toString() ); |
| 483 | | } |
| 484 | | |
| 485 | | /** |
| 486 | | * Called when the last open tab is closed to clear the preview pane. |
| 487 | | */ |
| 488 | | private void closeRemainingTab() { |
| 489 | | getPreviewPane().clear(); |
| 490 | | } |
| 491 | | |
| 492 | | /** |
| 493 | | * Called when an exception occurs that warrants the user's attention. |
| 494 | | * |
| 495 | | * @param e The exception with a message that the user should know about. |
| 496 | | */ |
| 497 | | private void error( final Exception e ) { |
| 498 | | getNotifier().notify( e ); |
| 499 | | } |
| 500 | | |
| 501 | | //---- File actions ------------------------------------------------------- |
| 502 | | |
| 503 | | /** |
| 504 | | * Called when an observable instance has changed. This is called by both the |
| 505 | | * snitch service and the notify service. The snitch service can be called for |
| 506 | | * different file types, including definition sources. |
| 507 | | * |
| 508 | | * @param observable The observed instance. |
| 509 | | * @param value The noteworthy item. |
| 510 | | */ |
| 511 | | @Override |
| 512 | | public void update( final Observable observable, final Object value ) { |
| 513 | | if( value != null ) { |
| 514 | | if( observable instanceof Snitch && value instanceof Path ) { |
| 515 | | updateSelectedTab(); |
| 516 | | } |
| 517 | | else if( observable instanceof Notifier && value instanceof String ) { |
| 518 | | updateStatusBar( (String) value ); |
| 519 | | } |
| 520 | | } |
| 521 | | } |
| 522 | | |
| 523 | | /** |
| 524 | | * Updates the status bar to show the given message. |
| 525 | | * |
| 526 | | * @param s The message to show in the status bar. |
| 527 | | */ |
| 528 | | private void updateStatusBar( final String s ) { |
| 529 | | Platform.runLater( |
| 530 | | () -> { |
| 531 | | final int index = s.indexOf( '\n' ); |
| 532 | | final String message = s.substring( |
| 533 | | 0, index > 0 ? index : s.length() ); |
| 534 | | |
| 535 | | getStatusBar().setText( message ); |
| 536 | | } |
| 537 | | ); |
| 538 | | } |
| 539 | | |
| 540 | | /** |
| 541 | | * Called when a file has been modified. |
| 542 | | */ |
| 543 | | private void updateSelectedTab() { |
| 544 | | Platform.runLater( |
| 545 | | () -> { |
| 546 | | // Brute-force XSLT file reload by re-instantiating all processors. |
| 547 | | resetProcessors(); |
| 548 | | refreshActiveTab(); |
| 549 | | } |
| 550 | | ); |
| 551 | | } |
| 552 | | |
| 553 | | /** |
| 554 | | * After resetting the processors, they will refresh anew to be up-to-date |
| 555 | | * with the files (text and definition) currently loaded into the editor. |
| 556 | | */ |
| 557 | | private void resetProcessors() { |
| 558 | | getProcessors().clear(); |
| 559 | | } |
| 560 | | |
| 561 | | //---- File actions ------------------------------------------------------- |
| 562 | | private void fileNew() { |
| 563 | | getFileEditorPane().newEditor(); |
| 564 | | } |
| 565 | | |
| 566 | | private void fileOpen() { |
| 567 | | getFileEditorPane().openFileDialog(); |
| 568 | | } |
| 569 | | |
| 570 | | private void fileClose() { |
| 571 | | getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| 572 | | } |
| 573 | | |
| 574 | | private void fileCloseAll() { |
| 575 | | getFileEditorPane().closeAllEditors(); |
| 576 | | } |
| 577 | | |
| 578 | | private void fileSave() { |
| 579 | | getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| 580 | | } |
| 581 | | |
| 582 | | private void fileSaveAs() { |
| 583 | | final FileEditorTab editor = getActiveFileEditor(); |
| 584 | | getFileEditorPane().saveEditorAs( editor ); |
| 585 | | getProcessors().remove( editor ); |
| 586 | | |
| 587 | | try { |
| 588 | | refreshSelectedTab( editor ); |
| 589 | | } catch( final Exception ex ) { |
| 590 | | getNotifier().notify( ex ); |
| 591 | | } |
| 592 | | } |
| 593 | | |
| 594 | | private void fileSaveAll() { |
| 595 | | getFileEditorPane().saveAllEditors(); |
| 596 | | } |
| 597 | | |
| 598 | | private void fileExit() { |
| 599 | | final Window window = getWindow(); |
| 600 | | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 601 | | } |
| 602 | | |
| 603 | | //---- R menu actions |
| 604 | | private void rScript() { |
| 605 | | final String script = getPreferences().get( PERSIST_R_STARTUP, "" ); |
| 606 | | final RScriptDialog dialog = new RScriptDialog( |
| 607 | | getWindow(), "Dialog.r.script.title", script ); |
| 608 | | final Optional<String> result = dialog.showAndWait(); |
| 609 | | |
| 610 | | result.ifPresent( this::putStartupScript ); |
| 611 | | } |
| 612 | | |
| 613 | | private void rDirectory() { |
| 614 | | final TextInputDialog dialog = new TextInputDialog( |
| 615 | | getPreferences().get( PERSIST_R_DIRECTORY, USER_DIRECTORY ) |
| 616 | | ); |
| 617 | | |
| 618 | | dialog.setTitle( get( "Dialog.r.directory.title" ) ); |
| 619 | | dialog.setHeaderText( getLiteral( "Dialog.r.directory.header" ) ); |
| 620 | | dialog.setContentText( "Directory" ); |
| 621 | | |
| 622 | | final Optional<String> result = dialog.showAndWait(); |
| 623 | | |
| 624 | | result.ifPresent( this::putStartupDirectory ); |
| 625 | | } |
| 626 | | |
| 627 | | /** |
| 628 | | * Stores the R startup script into the user preferences. |
| 629 | | */ |
| 630 | | private void putStartupScript( final String script ) { |
| 631 | | putPreference( PERSIST_R_STARTUP, script ); |
| 632 | | } |
| 633 | | |
| 634 | | /** |
| 635 | | * Stores the R bootstrap script directory into the user preferences. |
| 636 | | */ |
| 637 | | private void putStartupDirectory( final String directory ) { |
| 638 | | putPreference( PERSIST_R_DIRECTORY, directory ); |
| 639 | | } |
| 640 | | |
| 641 | | //---- Help actions ------------------------------------------------------- |
| 642 | | private void helpAbout() { |
| 643 | | final Alert alert = new Alert( AlertType.INFORMATION ); |
| 644 | | alert.setTitle( get( "Dialog.about.title" ) ); |
| 645 | | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 646 | | alert.setContentText( get( "Dialog.about.content" ) ); |
| 647 | | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 648 | | alert.initOwner( getWindow() ); |
| 649 | | |
| 650 | | alert.showAndWait(); |
| 651 | | } |
| 652 | | |
| 653 | | //---- Convenience accessors ---------------------------------------------- |
| 654 | | private float getFloat( final String key, final float defaultValue ) { |
| 655 | | return getPreferences().getFloat( key, defaultValue ); |
| 656 | | } |
| 657 | | |
| 658 | | private Preferences getPreferences() { |
| 659 | | return getOptions().getState(); |
| 660 | | } |
| 661 | | |
| 662 | | protected Scene getScene() { |
| 663 | | return mScene; |
| 664 | | } |
| 665 | | |
| 666 | | public Window getWindow() { |
| 667 | | return getScene().getWindow(); |
| 668 | | } |
| 669 | | |
| 670 | | private MarkdownEditorPane getActiveEditor() { |
| 671 | | final EditorPane pane = getActiveFileEditor().getEditorPane(); |
| 672 | | |
| 673 | | return pane instanceof MarkdownEditorPane |
| 674 | | ? (MarkdownEditorPane) pane |
| 675 | | : null; |
| 676 | | } |
| 677 | | |
| 678 | | private FileEditorTab getActiveFileEditor() { |
| 679 | | return getFileEditorPane().getActiveFileEditor(); |
| 680 | | } |
| 681 | | |
| 682 | | //---- Member accessors --------------------------------------------------- |
| 683 | | |
| 684 | | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 685 | | return mProcessors; |
| 686 | | } |
| 687 | | |
| 688 | | private FileEditorTabPane getFileEditorPane() { |
| 689 | | if( this.fileEditorPane == null ) { |
| 690 | | this.fileEditorPane = createFileEditorPane(); |
| 691 | | } |
| 692 | | |
| 693 | | return this.fileEditorPane; |
| 694 | | } |
| 695 | | |
| 696 | | private HTMLPreviewPane getPreviewPane() { |
| 697 | | return mPreviewPane; |
| 698 | | } |
| 699 | | |
| 700 | | private void setDefinitionSource( final DefinitionSource definitionSource ) { |
| 701 | | assert definitionSource != null; |
| 702 | | mDefinitionSource = definitionSource; |
| 703 | | } |
| 704 | | |
| 705 | | private DefinitionSource getDefinitionSource() { |
| 706 | | return mDefinitionSource; |
| 707 | | } |
| 708 | | |
| 709 | | private DefinitionPane getDefinitionPane() { |
| 710 | | return mDefinitionPane; |
| 711 | | } |
| 712 | | |
| 713 | | private Options getOptions() { |
| 714 | | return mOptions; |
| 715 | | } |
| 716 | | |
| 717 | | private Snitch getSnitch() { |
| 718 | | return mSnitch; |
| 719 | | } |
| 720 | | |
| 721 | | private Notifier getNotifier() { |
| 722 | | return mNotifier; |
| 723 | | } |
| 724 | | |
| 725 | | private Text getLineNumberText() { |
| 726 | | return mLineNumberText; |
| 727 | | } |
| 728 | | |
| 729 | | private StatusBar getStatusBar() { |
| 730 | | return mStatusBar; |
| 731 | | } |
| 732 | | |
| 733 | | private TextField getFindTextField() { |
| 734 | | return mFindTextField; |
| 735 | | } |
| 736 | | |
| 737 | | //---- Member creators ---------------------------------------------------- |
| 738 | | |
| 739 | | /** |
| 740 | | * Factory to create processors that are suited to different file types. |
| 741 | | * |
| 742 | | * @param tab The tab that is subjected to processing. |
| 743 | | * @return A processor suited to the file type specified by the tab's path. |
| 744 | | */ |
| 745 | | private Processor<String> createProcessor( final FileEditorTab tab ) { |
| 746 | | return createProcessorFactory().createProcessor( tab ); |
| 747 | | } |
| 748 | | |
| 749 | | private ProcessorFactory createProcessorFactory() { |
| 750 | | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 751 | | } |
| 752 | | |
| 753 | | private DefinitionSource createDefaultDefinitionSource() { |
| 754 | | return new YamlDefinitionSource( getDefinitionPath() ); |
| 755 | | } |
| 756 | | |
| 757 | | private DefinitionSource createDefinitionSource( final Path path ) { |
| 758 | | try { |
| 759 | | return createDefinitionFactory().createDefinitionSource( path ); |
| 760 | | } catch( final Exception ex ) { |
| 761 | | error( ex ); |
| 762 | | return createDefaultDefinitionSource(); |
| 763 | | } |
| 764 | | } |
| 765 | | |
| 766 | | private TextField createFindTextField() { |
| 767 | | return new TextField(); |
| 768 | | } |
| 769 | | |
| 770 | | /** |
| 771 | | * Create an editor pane to hold file editor tabs. |
| 772 | | * |
| 773 | | * @return A new instance, never null. |
| 774 | | */ |
| 775 | | private FileEditorTabPane createFileEditorPane() { |
| 776 | | return new FileEditorTabPane(); |
| 777 | | } |
| 778 | | |
| 779 | | private DefinitionFactory createDefinitionFactory() { |
| 780 | | return new DefinitionFactory(); |
| 781 | | } |
| 782 | | |
| 783 | | private StatusBar createStatusBar() { |
| 784 | | return new StatusBar(); |
| 785 | | } |
| 786 | | |
| 787 | | private Scene createScene() { |
| 788 | | final SplitPane splitPane = new SplitPane( |
| 789 | | getDefinitionPane().getNode(), |
| 790 | | getFileEditorPane().getNode(), |
| 791 | | getPreviewPane().getNode() ); |
| 792 | | |
| 793 | | splitPane.setDividerPositions( |
| 794 | | getFloat( K_PANE_SPLIT_DEFINITION, .10f ), |
| 795 | | getFloat( K_PANE_SPLIT_EDITOR, .45f ), |
| 796 | | getFloat( K_PANE_SPLIT_PREVIEW, .45f ) ); |
| 797 | | |
| 798 | | // See: http://broadlyapplicable.blogspot |
| 799 | | // .ca/2015/03/javafx-capture-restorePreferences-splitpane.html |
| 800 | | final BorderPane borderPane = new BorderPane(); |
| 801 | | borderPane.setPrefSize( 1024, 800 ); |
| 802 | | borderPane.setTop( createMenuBar() ); |
| 803 | | borderPane.setBottom( getStatusBar() ); |
| 804 | | borderPane.setCenter( splitPane ); |
| 805 | | |
| 806 | | final VBox box = new VBox(); |
| 807 | | box.setAlignment( Pos.BASELINE_CENTER ); |
| 808 | | box.getChildren().add( getLineNumberText() ); |
| 809 | | getStatusBar().getRightItems().add( box ); |
| 72 | import javafx.util.Duration; |
| 73 | import org.controlsfx.control.StatusBar; |
| 74 | import org.fxmisc.richtext.model.TwoDimensional.Position; |
| 75 | |
| 76 | import java.io.File; |
| 77 | import java.nio.file.Path; |
| 78 | import java.util.*; |
| 79 | import java.util.function.Function; |
| 80 | import java.util.prefs.Preferences; |
| 81 | |
| 82 | import static com.scrivenvar.Constants.*; |
| 83 | import static com.scrivenvar.Messages.get; |
| 84 | import static com.scrivenvar.Messages.getLiteral; |
| 85 | import static com.scrivenvar.util.StageState.*; |
| 86 | import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| 87 | import static javafx.event.Event.fireEvent; |
| 88 | import static javafx.scene.input.KeyCode.ENTER; |
| 89 | import static javafx.scene.input.KeyCode.TAB; |
| 90 | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 91 | |
| 92 | /** |
| 93 | * Main window containing a tab pane in the center for file editors. |
| 94 | * |
| 95 | * @author Karl Tauber and White Magic Software, Ltd. |
| 96 | */ |
| 97 | public class MainWindow implements Observer { |
| 98 | |
| 99 | private final Options mOptions = Services.load( Options.class ); |
| 100 | private final Snitch mSnitch = Services.load( Snitch.class ); |
| 101 | private final Settings mSettings = Services.load( Settings.class ); |
| 102 | private final Notifier mNotifier = Services.load( Notifier.class ); |
| 103 | |
| 104 | private final Scene mScene; |
| 105 | private final StatusBar mStatusBar; |
| 106 | private final Text mLineNumberText; |
| 107 | private final TextField mFindTextField; |
| 108 | |
| 109 | private DefinitionSource mDefinitionSource = createDefaultDefinitionSource(); |
| 110 | private final DefinitionPane mDefinitionPane = new DefinitionPane(); |
| 111 | private final HTMLPreviewPane mPreviewPane = new HTMLPreviewPane(); |
| 112 | private FileEditorTabPane fileEditorPane; |
| 113 | |
| 114 | /** |
| 115 | * Prevents re-instantiation of processing classes. |
| 116 | */ |
| 117 | private final Map<FileEditorTab, Processor<String>> mProcessors = |
| 118 | new HashMap<>(); |
| 119 | |
| 120 | private final Map<String, String> mResolvedMap = |
| 121 | new HashMap<>( DEFAULT_MAP_SIZE ); |
| 122 | |
| 123 | /** |
| 124 | * Listens on the definition pane for double-click events. |
| 125 | */ |
| 126 | private VariableNameInjector variableNameInjector; |
| 127 | |
| 128 | /** |
| 129 | * Called when the definition data is changed. |
| 130 | */ |
| 131 | final EventHandler<TreeItem.TreeModificationEvent<Event>> mTreeHandler = |
| 132 | event -> { |
| 133 | exportDefinitions( getDefinitionPath() ); |
| 134 | interpolateResolvedMap(); |
| 135 | refreshActiveTab(); |
| 136 | }; |
| 137 | |
| 138 | final EventHandler<? super KeyEvent> mDefinitionKeyHandler = |
| 139 | event -> { |
| 140 | if( event.getCode() == ENTER ) { |
| 141 | getVariableNameInjector().injectSelectedItem(); |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | final EventHandler<? super KeyEvent> mEditorKeyHandler = |
| 146 | (EventHandler<KeyEvent>) event -> { |
| 147 | if( event.getCode() == TAB ) { |
| 148 | getDefinitionPane().requestFocus(); |
| 149 | event.consume(); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | public MainWindow() { |
| 154 | mStatusBar = createStatusBar(); |
| 155 | mLineNumberText = createLineNumberText(); |
| 156 | mFindTextField = createFindTextField(); |
| 157 | mScene = createScene(); |
| 158 | |
| 159 | initLayout(); |
| 160 | initFindInput(); |
| 161 | initSnitch(); |
| 162 | initDefinitionListener(); |
| 163 | initTabAddedListener(); |
| 164 | initTabChangedListener(); |
| 165 | initPreferences(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Watch for changes to external files. In particular, this awaits |
| 170 | * modifications to any XSL files associated with XML files being edited. When |
| 171 | * an XSL file is modified (external to the application), the snitch's ears |
| 172 | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 173 | * date with what's on the file system. |
| 174 | */ |
| 175 | private void initSnitch() { |
| 176 | getSnitch().addObserver( this ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Initialize the find input text field to listen on F3, ENTER, and ESCAPE key |
| 181 | * presses. |
| 182 | */ |
| 183 | private void initFindInput() { |
| 184 | final TextField input = getFindTextField(); |
| 185 | |
| 186 | input.setOnKeyPressed( ( KeyEvent event ) -> { |
| 187 | switch( event.getCode() ) { |
| 188 | case F3: |
| 189 | case ENTER: |
| 190 | findNext(); |
| 191 | break; |
| 192 | case F: |
| 193 | if( !event.isControlDown() ) { |
| 194 | break; |
| 195 | } |
| 196 | case ESCAPE: |
| 197 | getStatusBar().setGraphic( null ); |
| 198 | getActiveFileEditor().getEditorPane().requestFocus(); |
| 199 | break; |
| 200 | } |
| 201 | } ); |
| 202 | |
| 203 | // Remove when the input field loses focus. |
| 204 | input.focusedProperty().addListener( |
| 205 | ( |
| 206 | final ObservableValue<? extends Boolean> focused, |
| 207 | final Boolean oFocus, |
| 208 | final Boolean nFocus ) -> { |
| 209 | if( !nFocus ) { |
| 210 | getStatusBar().setGraphic( null ); |
| 211 | } |
| 212 | } |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Listen for {@link FileEditorTabPane} to receive open definition file event. |
| 218 | */ |
| 219 | private void initDefinitionListener() { |
| 220 | getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| 221 | ( final ObservableValue<? extends Path> file, |
| 222 | final Path oldPath, final Path newPath ) -> { |
| 223 | // Indirectly refresh the resolved map. |
| 224 | resetProcessors(); |
| 225 | |
| 226 | openDefinitions( newPath ); |
| 227 | |
| 228 | // Will create new processors and therefore a new resolved map. |
| 229 | refreshActiveTab(); |
| 230 | } |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * When tabs are added, hook the various change listeners onto the new tab so |
| 236 | * that the preview pane refreshes as necessary. |
| 237 | */ |
| 238 | private void initTabAddedListener() { |
| 239 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 240 | |
| 241 | // Make sure the text processor kicks off when new files are opened. |
| 242 | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 243 | |
| 244 | // Update the preview pane on tab changes. |
| 245 | tabs.addListener( |
| 246 | ( final Change<? extends Tab> change ) -> { |
| 247 | while( change.next() ) { |
| 248 | if( change.wasAdded() ) { |
| 249 | // Multiple tabs can be added simultaneously. |
| 250 | for( final Tab newTab : change.getAddedSubList() ) { |
| 251 | final FileEditorTab tab = (FileEditorTab) newTab; |
| 252 | |
| 253 | initTextChangeListener( tab ); |
| 254 | initCaretParagraphListener( tab ); |
| 255 | initKeyboardEventListeners( tab ); |
| 256 | // initSyntaxListener( tab ); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Reloads the preferences from the previous session. |
| 266 | */ |
| 267 | private void initPreferences() { |
| 268 | restoreDefinitionPane(); |
| 269 | getFileEditorPane().restorePreferences(); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Listen for new tab selection events. |
| 274 | */ |
| 275 | private void initTabChangedListener() { |
| 276 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 277 | |
| 278 | // Update the preview pane changing tabs. |
| 279 | editorPane.addTabSelectionListener( |
| 280 | ( ObservableValue<? extends Tab> tabPane, |
| 281 | final Tab oldTab, final Tab newTab ) -> { |
| 282 | updateVariableNameInjector(); |
| 283 | |
| 284 | // If there was no old tab, then this is a first time load, which |
| 285 | // can be ignored. |
| 286 | if( oldTab != null ) { |
| 287 | if( newTab == null ) { |
| 288 | closeRemainingTab(); |
| 289 | } |
| 290 | else { |
| 291 | // Update the preview with the edited text. |
| 292 | refreshSelectedTab( (FileEditorTab) newTab ); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Ensure that the keyboard events are received when a new tab is added |
| 301 | * to the user interface. |
| 302 | * |
| 303 | * @param tab The tab that can trigger keyboard events, such as control+space. |
| 304 | */ |
| 305 | private void initKeyboardEventListeners( final FileEditorTab tab ) { |
| 306 | final VariableNameInjector vin = getVariableNameInjector(); |
| 307 | vin.initKeyboardEventListeners( tab ); |
| 308 | |
| 309 | tab.addEventFilter( KeyEvent.KEY_PRESSED, mEditorKeyHandler ); |
| 310 | } |
| 311 | |
| 312 | private void initTextChangeListener( final FileEditorTab tab ) { |
| 313 | tab.addTextChangeListener( |
| 314 | ( ObservableValue<? extends String> editor, |
| 315 | final String oldValue, final String newValue ) -> |
| 316 | refreshSelectedTab( tab ) |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | private void initCaretParagraphListener( final FileEditorTab tab ) { |
| 321 | tab.addCaretParagraphListener( |
| 322 | ( ObservableValue<? extends Integer> editor, |
| 323 | final Integer oldValue, final Integer newValue ) -> |
| 324 | refreshSelectedTab( tab ) |
| 325 | ); |
| 326 | } |
| 327 | |
| 328 | private void updateVariableNameInjector() { |
| 329 | getVariableNameInjector().setFileEditorTab( getActiveFileEditor() ); |
| 330 | } |
| 331 | |
| 332 | private void setVariableNameInjector( final VariableNameInjector injector ) { |
| 333 | this.variableNameInjector = injector; |
| 334 | } |
| 335 | |
| 336 | private synchronized VariableNameInjector getVariableNameInjector() { |
| 337 | if( this.variableNameInjector == null ) { |
| 338 | final VariableNameInjector vin = createVariableNameInjector(); |
| 339 | setVariableNameInjector( vin ); |
| 340 | } |
| 341 | |
| 342 | return this.variableNameInjector; |
| 343 | } |
| 344 | |
| 345 | private VariableNameInjector createVariableNameInjector() { |
| 346 | final FileEditorTab tab = getActiveFileEditor(); |
| 347 | final DefinitionPane pane = getDefinitionPane(); |
| 348 | |
| 349 | return new VariableNameInjector( tab, pane ); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Called whenever the preview pane becomes out of sync with the file editor |
| 354 | * tab. This can be called when the text changes, the caret paragraph changes, |
| 355 | * or the file tab changes. |
| 356 | * |
| 357 | * @param tab The file editor tab that has been changed in some fashion. |
| 358 | */ |
| 359 | private void refreshSelectedTab( final FileEditorTab tab ) { |
| 360 | if( tab == null ) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | getPreviewPane().setPath( tab.getPath() ); |
| 365 | |
| 366 | // TODO: https://github.com/DaveJarvis/scrivenvar/issues/29 |
| 367 | final Position p = tab.getCaretOffset(); |
| 368 | getLineNumberText().setText( |
| 369 | get( STATUS_BAR_LINE, |
| 370 | p.getMajor() + 1, |
| 371 | p.getMinor() + 1, |
| 372 | tab.getCaretPosition() + 1 |
| 373 | ) |
| 374 | ); |
| 375 | |
| 376 | Processor<String> processor = getProcessors().get( tab ); |
| 377 | |
| 378 | if( processor == null ) { |
| 379 | processor = createProcessor( tab ); |
| 380 | getProcessors().put( tab, processor ); |
| 381 | } |
| 382 | |
| 383 | try { |
| 384 | processor.processChain( tab.getEditorText() ); |
| 385 | } catch( final Exception ex ) { |
| 386 | error( ex ); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private void refreshActiveTab() { |
| 391 | refreshSelectedTab( getActiveFileEditor() ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Used to find text in the active file editor window. |
| 396 | */ |
| 397 | private void find() { |
| 398 | final TextField input = getFindTextField(); |
| 399 | getStatusBar().setGraphic( input ); |
| 400 | input.requestFocus(); |
| 401 | } |
| 402 | |
| 403 | public void findNext() { |
| 404 | getActiveFileEditor().searchNext( getFindTextField().getText() ); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Returns the variable map of interpolated definitions. |
| 409 | * |
| 410 | * @return A map to help dereference variables. |
| 411 | */ |
| 412 | private Map<String, String> getResolvedMap() { |
| 413 | return mResolvedMap; |
| 414 | } |
| 415 | |
| 416 | private void interpolateResolvedMap() { |
| 417 | final Map<String, String> treeMap = getDefinitionPane().toMap(); |
| 418 | final Map<String, String> map = new HashMap<>( treeMap ); |
| 419 | MapInterpolator.interpolate( map ); |
| 420 | |
| 421 | getResolvedMap().clear(); |
| 422 | getResolvedMap().putAll( map ); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Called when a definition source is opened. |
| 427 | * |
| 428 | * @param path Path to the definition source that was opened. |
| 429 | */ |
| 430 | private void openDefinitions( final Path path ) { |
| 431 | try { |
| 432 | final DefinitionSource ds = createDefinitionSource( path ); |
| 433 | setDefinitionSource( ds ); |
| 434 | storeDefinitionSourceFilename( path ); |
| 435 | |
| 436 | final Tooltip tooltipPath = new Tooltip( path.toString() ); |
| 437 | tooltipPath.setShowDelay( Duration.millis( 200 ) ); |
| 438 | |
| 439 | final DefinitionPane pane = getDefinitionPane(); |
| 440 | pane.update( ds ); |
| 441 | pane.addTreeChangeHandler( mTreeHandler ); |
| 442 | pane.addKeyEventHandler( mDefinitionKeyHandler ); |
| 443 | pane.filenameProperty().setValue( path.getFileName().toString() ); |
| 444 | pane.setTooltip( tooltipPath ); |
| 445 | |
| 446 | interpolateResolvedMap(); |
| 447 | } catch( final Exception e ) { |
| 448 | error( e ); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | private void exportDefinitions( final Path path ) { |
| 453 | try { |
| 454 | final DefinitionPane pane = getDefinitionPane(); |
| 455 | final TreeItem<String> root = pane.getTreeView().getRoot(); |
| 456 | final TreeItem<String> problemChild = pane.isTreeWellFormed(); |
| 457 | |
| 458 | if( problemChild == null ) { |
| 459 | getDefinitionSource().getTreeAdapter().export( root, path ); |
| 460 | getNotifier().clear(); |
| 461 | } |
| 462 | else { |
| 463 | final String msg = get( "yaml.error.tree.form", |
| 464 | problemChild.getValue() ); |
| 465 | getNotifier().notify( msg ); |
| 466 | } |
| 467 | } catch( final Exception e ) { |
| 468 | error( e ); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | private Path getDefinitionPath() { |
| 473 | final String source = getPreferences().get( |
| 474 | PERSIST_DEFINITION_SOURCE, "" ); |
| 475 | |
| 476 | return new File( |
| 477 | source.isBlank() |
| 478 | ? getSetting( "file.definition.default", "variables.yaml" ) |
| 479 | : source |
| 480 | ).toPath(); |
| 481 | } |
| 482 | |
| 483 | private void restoreDefinitionPane() { |
| 484 | openDefinitions( getDefinitionPath() ); |
| 485 | } |
| 486 | |
| 487 | private void storeDefinitionSourceFilename( final Path path ) { |
| 488 | getPreferences().put( PERSIST_DEFINITION_SOURCE, path.toString() ); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Called when the last open tab is closed to clear the preview pane. |
| 493 | */ |
| 494 | private void closeRemainingTab() { |
| 495 | getPreviewPane().clear(); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Called when an exception occurs that warrants the user's attention. |
| 500 | * |
| 501 | * @param e The exception with a message that the user should know about. |
| 502 | */ |
| 503 | private void error( final Exception e ) { |
| 504 | getNotifier().notify( e ); |
| 505 | } |
| 506 | |
| 507 | //---- File actions ------------------------------------------------------- |
| 508 | |
| 509 | /** |
| 510 | * Called when an observable instance has changed. This is called by both the |
| 511 | * snitch service and the notify service. The snitch service can be called for |
| 512 | * different file types, including definition sources. |
| 513 | * |
| 514 | * @param observable The observed instance. |
| 515 | * @param value The noteworthy item. |
| 516 | */ |
| 517 | @Override |
| 518 | public void update( final Observable observable, final Object value ) { |
| 519 | if( value != null ) { |
| 520 | if( observable instanceof Snitch && value instanceof Path ) { |
| 521 | updateSelectedTab(); |
| 522 | } |
| 523 | else if( observable instanceof Notifier && value instanceof String ) { |
| 524 | updateStatusBar( (String) value ); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Updates the status bar to show the given message. |
| 531 | * |
| 532 | * @param s The message to show in the status bar. |
| 533 | */ |
| 534 | private void updateStatusBar( final String s ) { |
| 535 | Platform.runLater( |
| 536 | () -> { |
| 537 | final int index = s.indexOf( '\n' ); |
| 538 | final String message = s.substring( |
| 539 | 0, index > 0 ? index : s.length() ); |
| 540 | |
| 541 | getStatusBar().setText( message ); |
| 542 | } |
| 543 | ); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Called when a file has been modified. |
| 548 | */ |
| 549 | private void updateSelectedTab() { |
| 550 | Platform.runLater( |
| 551 | () -> { |
| 552 | // Brute-force XSLT file reload by re-instantiating all processors. |
| 553 | resetProcessors(); |
| 554 | refreshActiveTab(); |
| 555 | } |
| 556 | ); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * After resetting the processors, they will refresh anew to be up-to-date |
| 561 | * with the files (text and definition) currently loaded into the editor. |
| 562 | */ |
| 563 | private void resetProcessors() { |
| 564 | getProcessors().clear(); |
| 565 | } |
| 566 | |
| 567 | //---- File actions ------------------------------------------------------- |
| 568 | private void fileNew() { |
| 569 | getFileEditorPane().newEditor(); |
| 570 | } |
| 571 | |
| 572 | private void fileOpen() { |
| 573 | getFileEditorPane().openFileDialog(); |
| 574 | } |
| 575 | |
| 576 | private void fileClose() { |
| 577 | getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| 578 | } |
| 579 | |
| 580 | private void fileCloseAll() { |
| 581 | getFileEditorPane().closeAllEditors(); |
| 582 | } |
| 583 | |
| 584 | private void fileSave() { |
| 585 | getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| 586 | } |
| 587 | |
| 588 | private void fileSaveAs() { |
| 589 | final FileEditorTab editor = getActiveFileEditor(); |
| 590 | getFileEditorPane().saveEditorAs( editor ); |
| 591 | getProcessors().remove( editor ); |
| 592 | |
| 593 | try { |
| 594 | refreshSelectedTab( editor ); |
| 595 | } catch( final Exception ex ) { |
| 596 | getNotifier().notify( ex ); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | private void fileSaveAll() { |
| 601 | getFileEditorPane().saveAllEditors(); |
| 602 | } |
| 603 | |
| 604 | private void fileExit() { |
| 605 | final Window window = getWindow(); |
| 606 | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 607 | } |
| 608 | |
| 609 | //---- R menu actions |
| 610 | private void rScript() { |
| 611 | final String script = getPreferences().get( PERSIST_R_STARTUP, "" ); |
| 612 | final RScriptDialog dialog = new RScriptDialog( |
| 613 | getWindow(), "Dialog.r.script.title", script ); |
| 614 | final Optional<String> result = dialog.showAndWait(); |
| 615 | |
| 616 | result.ifPresent( this::putStartupScript ); |
| 617 | } |
| 618 | |
| 619 | private void rDirectory() { |
| 620 | final TextInputDialog dialog = new TextInputDialog( |
| 621 | getPreferences().get( PERSIST_R_DIRECTORY, USER_DIRECTORY ) |
| 622 | ); |
| 623 | |
| 624 | dialog.setTitle( get( "Dialog.r.directory.title" ) ); |
| 625 | dialog.setHeaderText( getLiteral( "Dialog.r.directory.header" ) ); |
| 626 | dialog.setContentText( "Directory" ); |
| 627 | |
| 628 | final Optional<String> result = dialog.showAndWait(); |
| 629 | |
| 630 | result.ifPresent( this::putStartupDirectory ); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Stores the R startup script into the user preferences. |
| 635 | */ |
| 636 | private void putStartupScript( final String script ) { |
| 637 | putPreference( PERSIST_R_STARTUP, script ); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Stores the R bootstrap script directory into the user preferences. |
| 642 | */ |
| 643 | private void putStartupDirectory( final String directory ) { |
| 644 | putPreference( PERSIST_R_DIRECTORY, directory ); |
| 645 | } |
| 646 | |
| 647 | //---- Help actions ------------------------------------------------------- |
| 648 | private void helpAbout() { |
| 649 | final Alert alert = new Alert( AlertType.INFORMATION ); |
| 650 | alert.setTitle( get( "Dialog.about.title" ) ); |
| 651 | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 652 | alert.setContentText( get( "Dialog.about.content" ) ); |
| 653 | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 654 | alert.initOwner( getWindow() ); |
| 655 | |
| 656 | alert.showAndWait(); |
| 657 | } |
| 658 | |
| 659 | //---- Convenience accessors ---------------------------------------------- |
| 660 | private float getFloat( final String key, final float defaultValue ) { |
| 661 | return getPreferences().getFloat( key, defaultValue ); |
| 662 | } |
| 663 | |
| 664 | private Preferences getPreferences() { |
| 665 | return getOptions().getState(); |
| 666 | } |
| 667 | |
| 668 | protected Scene getScene() { |
| 669 | return mScene; |
| 670 | } |
| 671 | |
| 672 | public Window getWindow() { |
| 673 | return getScene().getWindow(); |
| 674 | } |
| 675 | |
| 676 | private MarkdownEditorPane getActiveEditor() { |
| 677 | final EditorPane pane = getActiveFileEditor().getEditorPane(); |
| 678 | |
| 679 | return pane instanceof MarkdownEditorPane |
| 680 | ? (MarkdownEditorPane) pane |
| 681 | : null; |
| 682 | } |
| 683 | |
| 684 | private FileEditorTab getActiveFileEditor() { |
| 685 | return getFileEditorPane().getActiveFileEditor(); |
| 686 | } |
| 687 | |
| 688 | //---- Member accessors --------------------------------------------------- |
| 689 | |
| 690 | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 691 | return mProcessors; |
| 692 | } |
| 693 | |
| 694 | private FileEditorTabPane getFileEditorPane() { |
| 695 | if( this.fileEditorPane == null ) { |
| 696 | this.fileEditorPane = createFileEditorPane(); |
| 697 | } |
| 698 | |
| 699 | return this.fileEditorPane; |
| 700 | } |
| 701 | |
| 702 | private HTMLPreviewPane getPreviewPane() { |
| 703 | return mPreviewPane; |
| 704 | } |
| 705 | |
| 706 | private void setDefinitionSource( final DefinitionSource definitionSource ) { |
| 707 | assert definitionSource != null; |
| 708 | mDefinitionSource = definitionSource; |
| 709 | } |
| 710 | |
| 711 | private DefinitionSource getDefinitionSource() { |
| 712 | return mDefinitionSource; |
| 713 | } |
| 714 | |
| 715 | private DefinitionPane getDefinitionPane() { |
| 716 | return mDefinitionPane; |
| 717 | } |
| 718 | |
| 719 | private Options getOptions() { |
| 720 | return mOptions; |
| 721 | } |
| 722 | |
| 723 | private Snitch getSnitch() { |
| 724 | return mSnitch; |
| 725 | } |
| 726 | |
| 727 | private Notifier getNotifier() { |
| 728 | return mNotifier; |
| 729 | } |
| 730 | |
| 731 | private Text getLineNumberText() { |
| 732 | return mLineNumberText; |
| 733 | } |
| 734 | |
| 735 | private StatusBar getStatusBar() { |
| 736 | return mStatusBar; |
| 737 | } |
| 738 | |
| 739 | private TextField getFindTextField() { |
| 740 | return mFindTextField; |
| 741 | } |
| 742 | |
| 743 | //---- Member creators ---------------------------------------------------- |
| 744 | |
| 745 | /** |
| 746 | * Factory to create processors that are suited to different file types. |
| 747 | * |
| 748 | * @param tab The tab that is subjected to processing. |
| 749 | * @return A processor suited to the file type specified by the tab's path. |
| 750 | */ |
| 751 | private Processor<String> createProcessor( final FileEditorTab tab ) { |
| 752 | return createProcessorFactory().createProcessor( tab ); |
| 753 | } |
| 754 | |
| 755 | private ProcessorFactory createProcessorFactory() { |
| 756 | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 757 | } |
| 758 | |
| 759 | private DefinitionSource createDefaultDefinitionSource() { |
| 760 | return new YamlDefinitionSource( getDefinitionPath() ); |
| 761 | } |
| 762 | |
| 763 | private DefinitionSource createDefinitionSource( final Path path ) { |
| 764 | try { |
| 765 | return createDefinitionFactory().createDefinitionSource( path ); |
| 766 | } catch( final Exception ex ) { |
| 767 | error( ex ); |
| 768 | return createDefaultDefinitionSource(); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | private TextField createFindTextField() { |
| 773 | return new TextField(); |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Create an editor pane to hold file editor tabs. |
| 778 | * |
| 779 | * @return A new instance, never null. |
| 780 | */ |
| 781 | private FileEditorTabPane createFileEditorPane() { |
| 782 | return new FileEditorTabPane(); |
| 783 | } |
| 784 | |
| 785 | private DefinitionFactory createDefinitionFactory() { |
| 786 | return new DefinitionFactory(); |
| 787 | } |
| 788 | |
| 789 | private StatusBar createStatusBar() { |
| 790 | return new StatusBar(); |
| 791 | } |
| 792 | |
| 793 | private Scene createScene() { |
| 794 | final SplitPane splitPane = new SplitPane( |
| 795 | getDefinitionPane().getNode(), |
| 796 | getFileEditorPane().getNode(), |
| 797 | getPreviewPane().getNode() ); |
| 798 | |
| 799 | splitPane.setDividerPositions( |
| 800 | getFloat( K_PANE_SPLIT_DEFINITION, .10f ), |
| 801 | getFloat( K_PANE_SPLIT_EDITOR, .45f ), |
| 802 | getFloat( K_PANE_SPLIT_PREVIEW, .45f ) ); |
| 803 | |
| 804 | getDefinitionPane().prefHeightProperty().bind( splitPane.heightProperty() ); |
| 805 | |
| 806 | final BorderPane borderPane = new BorderPane(); |
| 807 | borderPane.setPrefSize( 1024, 800 ); |
| 808 | borderPane.setTop( createMenuBar() ); |
| 809 | borderPane.setBottom( getStatusBar() ); |
| 810 | borderPane.setCenter( splitPane ); |
| 811 | |
| 812 | final VBox statusBar = new VBox(); |
| 813 | statusBar.setAlignment( Pos.BASELINE_CENTER ); |
| 814 | statusBar.getChildren().add( getLineNumberText() ); |
| 815 | getStatusBar().getRightItems().add( statusBar ); |
| 810 | 816 | |
| 811 | 817 | return new Scene( borderPane ); |