| 88 | 88 | |
| 89 | 89 | import java.io.BufferedReader; |
| 90 | | import java.io.InputStreamReader; |
| 91 | | import java.nio.file.Path; |
| 92 | | import java.nio.file.Paths; |
| 93 | | import java.util.*; |
| 94 | | import java.util.concurrent.atomic.AtomicInteger; |
| 95 | | import java.util.function.Consumer; |
| 96 | | import java.util.function.Function; |
| 97 | | import java.util.prefs.Preferences; |
| 98 | | import java.util.stream.Collectors; |
| 99 | | |
| 100 | | import static com.scrivenvar.Constants.*; |
| 101 | | import static com.scrivenvar.Messages.get; |
| 102 | | import static com.scrivenvar.StatusBarNotifier.alert; |
| 103 | | import static com.scrivenvar.util.StageState.*; |
| 104 | | import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| 105 | | import static java.nio.charset.StandardCharsets.UTF_8; |
| 106 | | import static java.util.Collections.emptyList; |
| 107 | | import static java.util.Collections.singleton; |
| 108 | | import static javafx.application.Platform.runLater; |
| 109 | | import static javafx.event.Event.fireEvent; |
| 110 | | import static javafx.scene.input.KeyCode.ENTER; |
| 111 | | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 112 | | import static org.fxmisc.richtext.model.TwoDimensional.Bias.Forward; |
| 113 | | |
| 114 | | /** |
| 115 | | * Main window containing a tab pane in the center for file editors. |
| 116 | | */ |
| 117 | | public class MainWindow implements Observer { |
| 118 | | /** |
| 119 | | * The {@code OPTIONS} variable must be declared before all other variables |
| 120 | | * to prevent subsequent initializations from failing due to missing user |
| 121 | | * preferences. |
| 122 | | */ |
| 123 | | private static final Options sOptions = Services.load( Options.class ); |
| 124 | | private static final Snitch SNITCH = Services.load( Snitch.class ); |
| 125 | | |
| 126 | | private final Scene mScene; |
| 127 | | private final StatusBar mStatusBar; |
| 128 | | private final Text mLineNumberText; |
| 129 | | private final TextField mFindTextField; |
| 130 | | private final SpellChecker mSpellChecker; |
| 131 | | |
| 132 | | private final Object mMutex = new Object(); |
| 133 | | |
| 134 | | /** |
| 135 | | * Prevents re-instantiation of processing classes. |
| 136 | | */ |
| 137 | | private final Map<FileEditorTab, Processor<String>> mProcessors = |
| 138 | | new HashMap<>(); |
| 139 | | |
| 140 | | private final Map<String, String> mResolvedMap = |
| 141 | | new HashMap<>( DEFAULT_MAP_SIZE ); |
| 142 | | |
| 143 | | private final EventHandler<PreferencesFxEvent> mRPreferencesListener = |
| 144 | | event -> rerender(); |
| 145 | | |
| 146 | | /** |
| 147 | | * Called when the definition data is changed. |
| 148 | | */ |
| 149 | | private final EventHandler<TreeItem.TreeModificationEvent<Event>> |
| 150 | | mTreeHandler = event -> { |
| 151 | | exportDefinitions( getDefinitionPath() ); |
| 152 | | interpolateResolvedMap(); |
| 153 | | rerender(); |
| 154 | | }; |
| 155 | | |
| 156 | | /** |
| 157 | | * Called to inject the selected item when the user presses ENTER in the |
| 158 | | * definition pane. |
| 159 | | */ |
| 160 | | private final EventHandler<? super KeyEvent> mDefinitionKeyHandler = |
| 161 | | event -> { |
| 162 | | if( event.getCode() == ENTER ) { |
| 163 | | getVariableNameInjector().injectSelectedItem(); |
| 164 | | } |
| 165 | | }; |
| 166 | | |
| 167 | | private final ChangeListener<Integer> mCaretPositionListener = |
| 168 | | ( observable, oldPosition, newPosition ) -> { |
| 169 | | final FileEditorTab tab = getActiveFileEditorTab(); |
| 170 | | final EditorPane pane = tab.getEditorPane(); |
| 171 | | final StyleClassedTextArea editor = pane.getEditor(); |
| 172 | | |
| 173 | | getLineNumberText().setText( |
| 174 | | get( STATUS_BAR_LINE, |
| 175 | | editor.getCurrentParagraph() + 1, |
| 176 | | editor.getParagraphs().size(), |
| 177 | | editor.getCaretPosition() |
| 178 | | ) |
| 179 | | ); |
| 180 | | }; |
| 181 | | |
| 182 | | private final ChangeListener<Integer> mCaretParagraphListener = |
| 183 | | ( observable, oldIndex, newIndex ) -> |
| 184 | | scrollToParagraph( newIndex, true ); |
| 185 | | |
| 186 | | private DefinitionSource mDefinitionSource = createDefaultDefinitionSource(); |
| 187 | | private final DefinitionPane mDefinitionPane = new DefinitionPane(); |
| 188 | | private final HTMLPreviewPane mPreviewPane = createHTMLPreviewPane(); |
| 189 | | private final FileEditorTabPane mFileEditorPane = new FileEditorTabPane( |
| 190 | | mCaretPositionListener, |
| 191 | | mCaretParagraphListener ); |
| 192 | | |
| 193 | | /** |
| 194 | | * Listens on the definition pane for double-click events. |
| 195 | | */ |
| 196 | | private final DefinitionNameInjector mVariableNameInjector |
| 197 | | = new DefinitionNameInjector( mDefinitionPane ); |
| 198 | | |
| 199 | | public MainWindow() { |
| 200 | | mStatusBar = createStatusBar(); |
| 201 | | mLineNumberText = createLineNumberText(); |
| 202 | | mFindTextField = createFindTextField(); |
| 203 | | mScene = createScene(); |
| 204 | | mSpellChecker = createSpellChecker(); |
| 205 | | |
| 206 | | // Add the close request listener before the window is shown. |
| 207 | | initLayout(); |
| 208 | | StatusBarNotifier.setStatusBar( mStatusBar ); |
| 209 | | } |
| 210 | | |
| 211 | | /** |
| 212 | | * Called after the stage is shown. |
| 213 | | */ |
| 214 | | public void init() { |
| 215 | | initFindInput(); |
| 216 | | initSnitch(); |
| 217 | | initDefinitionListener(); |
| 218 | | initTabAddedListener(); |
| 219 | | initTabChangedListener(); |
| 220 | | initPreferences(); |
| 221 | | initVariableNameInjector(); |
| 222 | | } |
| 223 | | |
| 224 | | private void initLayout() { |
| 225 | | final var scene = getScene(); |
| 226 | | |
| 227 | | scene.getStylesheets().add( STYLESHEET_SCENE ); |
| 228 | | scene.windowProperty().addListener( |
| 229 | | ( unused, oldWindow, newWindow ) -> |
| 230 | | newWindow.setOnCloseRequest( |
| 231 | | e -> { |
| 232 | | if( !getFileEditorPane().closeAllEditors() ) { |
| 233 | | e.consume(); |
| 234 | | } |
| 235 | | } |
| 236 | | ) |
| 237 | | ); |
| 238 | | } |
| 239 | | |
| 240 | | /** |
| 241 | | * Initialize the find input text field to listen on F3, ENTER, and |
| 242 | | * ESCAPE key presses. |
| 243 | | */ |
| 244 | | private void initFindInput() { |
| 245 | | final TextField input = getFindTextField(); |
| 246 | | |
| 247 | | input.setOnKeyPressed( ( KeyEvent event ) -> { |
| 248 | | switch( event.getCode() ) { |
| 249 | | case F3: |
| 250 | | case ENTER: |
| 251 | | editFindNext(); |
| 252 | | break; |
| 253 | | case F: |
| 254 | | if( !event.isControlDown() ) { |
| 255 | | break; |
| 256 | | } |
| 257 | | case ESCAPE: |
| 258 | | getStatusBar().setGraphic( null ); |
| 259 | | getActiveFileEditorTab().getEditorPane().requestFocus(); |
| 260 | | break; |
| 261 | | } |
| 262 | | } ); |
| 263 | | |
| 264 | | // Remove when the input field loses focus. |
| 265 | | input.focusedProperty().addListener( |
| 266 | | ( focused, oldFocus, newFocus ) -> { |
| 267 | | if( !newFocus ) { |
| 268 | | getStatusBar().setGraphic( null ); |
| 269 | | } |
| 270 | | } |
| 271 | | ); |
| 272 | | } |
| 273 | | |
| 274 | | /** |
| 275 | | * Watch for changes to external files. In particular, this awaits |
| 276 | | * modifications to any XSL files associated with XML files being edited. |
| 277 | | * When |
| 278 | | * an XSL file is modified (external to the application), the snitch's ears |
| 279 | | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 280 | | * date with what's on the file system. |
| 281 | | */ |
| 282 | | private void initSnitch() { |
| 283 | | SNITCH.addObserver( this ); |
| 284 | | } |
| 285 | | |
| 286 | | /** |
| 287 | | * Listen for {@link FileEditorTabPane} to receive open definition file |
| 288 | | * event. |
| 289 | | */ |
| 290 | | private void initDefinitionListener() { |
| 291 | | getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| 292 | | ( final ObservableValue<? extends Path> file, |
| 293 | | final Path oldPath, final Path newPath ) -> { |
| 294 | | openDefinitions( newPath ); |
| 295 | | rerender(); |
| 296 | | } |
| 297 | | ); |
| 298 | | } |
| 299 | | |
| 300 | | /** |
| 301 | | * Re-instantiates all processors then re-renders the active tab. This |
| 302 | | * will refresh the resolved map, force R to re-initialize, and brute-force |
| 303 | | * XSLT file reloads. |
| 304 | | */ |
| 305 | | private void rerender() { |
| 306 | | runLater( |
| 307 | | () -> { |
| 308 | | resetProcessors(); |
| 309 | | renderActiveTab(); |
| 310 | | } |
| 311 | | ); |
| 312 | | } |
| 313 | | |
| 314 | | /** |
| 315 | | * When tabs are added, hook the various change listeners onto the new |
| 316 | | * tab sothat the preview pane refreshes as necessary. |
| 317 | | */ |
| 318 | | private void initTabAddedListener() { |
| 319 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 320 | | |
| 321 | | // Make sure the text processor kicks off when new files are opened. |
| 322 | | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 323 | | |
| 324 | | // Update the preview pane on tab changes. |
| 325 | | tabs.addListener( |
| 326 | | ( final Change<? extends Tab> change ) -> { |
| 327 | | while( change.next() ) { |
| 328 | | if( change.wasAdded() ) { |
| 329 | | // Multiple tabs can be added simultaneously. |
| 330 | | for( final Tab newTab : change.getAddedSubList() ) { |
| 331 | | final FileEditorTab tab = (FileEditorTab) newTab; |
| 332 | | |
| 333 | | initTextChangeListener( tab ); |
| 334 | | initScrollEventListener( tab ); |
| 335 | | initSpellCheckListener( tab ); |
| 336 | | // initSyntaxListener( tab ); |
| 337 | | } |
| 338 | | } |
| 339 | | } |
| 340 | | } |
| 341 | | ); |
| 342 | | } |
| 343 | | |
| 344 | | private void initTextChangeListener( final FileEditorTab tab ) { |
| 345 | | tab.addTextChangeListener( |
| 346 | | ( __, ov, nv ) -> { |
| 347 | | process( tab ); |
| 348 | | scrollToParagraph( getCurrentParagraphIndex() ); |
| 349 | | } |
| 350 | | ); |
| 351 | | } |
| 352 | | |
| 353 | | private void initScrollEventListener( final FileEditorTab tab ) { |
| 354 | | final var scrollPane = tab.getScrollPane(); |
| 355 | | final var scrollBar = getPreviewPane().getVerticalScrollBar(); |
| 356 | | |
| 357 | | addShowListener( scrollPane, ( __ ) -> { |
| 358 | | final var handler = new ScrollEventHandler( scrollPane, scrollBar ); |
| 359 | | handler.enabledProperty().bind( tab.selectedProperty() ); |
| 360 | | } ); |
| 361 | | } |
| 362 | | |
| 363 | | /** |
| 364 | | * Listen for changes to the any particular paragraph and perform a quick |
| 365 | | * spell check upon it. The style classes in the editor will be changed to |
| 366 | | * mark any spelling mistakes in the paragraph. The user may then interact |
| 367 | | * with any misspelled word (i.e., any piece of text that is marked) to |
| 368 | | * revise the spelling. |
| 369 | | * |
| 370 | | * @param tab The tab to spellcheck. |
| 371 | | */ |
| 372 | | private void initSpellCheckListener( final FileEditorTab tab ) { |
| 373 | | final var editor = tab.getEditorPane().getEditor(); |
| 374 | | |
| 375 | | // When the editor first appears, run a full spell check. This allows |
| 376 | | // spell checking while typing to be restricted to the active paragraph, |
| 377 | | // which is usually substantially smaller than the whole document. |
| 378 | | addShowListener( |
| 379 | | editor, ( __ ) -> spellcheck( editor, editor.getText() ) |
| 380 | | ); |
| 381 | | |
| 382 | | // Use the plain text changes so that notifications of style changes |
| 383 | | // are suppressed. Checking against the identity ensures that only |
| 384 | | // new text additions or deletions trigger proofreading. |
| 385 | | editor.plainTextChanges() |
| 386 | | .filter( p -> !p.isIdentity() ).subscribe( change -> { |
| 387 | | |
| 388 | | // Only perform a spell check on the current paragraph. The |
| 389 | | // entire document is processed once, when opened. |
| 390 | | final var offset = change.getPosition(); |
| 391 | | final var position = editor.offsetToPosition( offset, Forward ); |
| 392 | | final var paraId = position.getMajor(); |
| 393 | | final var paragraph = editor.getParagraph( paraId ); |
| 394 | | final var text = paragraph.getText(); |
| 395 | | |
| 396 | | // Ensure that styles aren't doubled-up. |
| 397 | | editor.clearStyle( paraId ); |
| 398 | | |
| 399 | | spellcheck( editor, text, paraId ); |
| 400 | | } ); |
| 401 | | } |
| 402 | | |
| 403 | | /** |
| 404 | | * Listen for new tab selection events. |
| 405 | | */ |
| 406 | | private void initTabChangedListener() { |
| 407 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 408 | | |
| 409 | | // Update the preview pane changing tabs. |
| 410 | | editorPane.addTabSelectionListener( |
| 411 | | ( tabPane, oldTab, newTab ) -> { |
| 412 | | if( newTab == null ) { |
| 413 | | // Clear the preview pane when closing an editor. When the last |
| 414 | | // tab is closed, this ensures that the preview pane is empty. |
| 415 | | getPreviewPane().clear(); |
| 416 | | } |
| 417 | | else { |
| 418 | | final var tab = (FileEditorTab) newTab; |
| 419 | | updateVariableNameInjector( tab ); |
| 420 | | process( tab ); |
| 421 | | } |
| 422 | | } |
| 423 | | ); |
| 424 | | } |
| 425 | | |
| 426 | | /** |
| 427 | | * Reloads the preferences from the previous session. |
| 428 | | */ |
| 429 | | private void initPreferences() { |
| 430 | | initDefinitionPane(); |
| 431 | | getFileEditorPane().initPreferences(); |
| 432 | | getUserPreferences().addSaveEventHandler( mRPreferencesListener ); |
| 433 | | } |
| 434 | | |
| 435 | | private void initVariableNameInjector() { |
| 436 | | updateVariableNameInjector( getActiveFileEditorTab() ); |
| 437 | | } |
| 438 | | |
| 439 | | /** |
| 440 | | * Calls the listener when the given node is shown for the first time. The |
| 441 | | * visible property is not the same as the initial showing event; visibility |
| 442 | | * can be triggered numerous times (such as going off screen). |
| 443 | | * <p> |
| 444 | | * This is called, for example, before the drag handler can be attached, |
| 445 | | * because the scrollbar for the text editor pane must be visible. |
| 446 | | * </p> |
| 447 | | * |
| 448 | | * @param node The node to watch for showing. |
| 449 | | * @param consumer The consumer to invoke when the event fires. |
| 450 | | */ |
| 451 | | private void addShowListener( |
| 452 | | final Node node, final Consumer<Void> consumer ) { |
| 453 | | final ChangeListener<? super Boolean> listener = ( o, oldShow, newShow ) -> |
| 454 | | runLater( () -> { |
| 455 | | if( newShow ) { |
| 456 | | try { |
| 457 | | consumer.accept( null ); |
| 458 | | } catch( final Exception ex ) { |
| 459 | | alert( ex ); |
| 460 | | } |
| 461 | | } |
| 462 | | } ); |
| 463 | | |
| 464 | | Val.flatMap( node.sceneProperty(), Scene::windowProperty ) |
| 465 | | .flatMap( Window::showingProperty ) |
| 466 | | .addListener( listener ); |
| 467 | | } |
| 468 | | |
| 469 | | private void scrollToParagraph( final int id ) { |
| 470 | | scrollToParagraph( id, false ); |
| 471 | | } |
| 472 | | |
| 473 | | /** |
| 474 | | * @param id The paragraph to scroll to, will be approximated if it doesn't |
| 475 | | * exist. |
| 476 | | * @param force {@code true} means to force scrolling immediately, which |
| 477 | | * should only be attempted when it is known that the document |
| 478 | | * has been fully rendered. Otherwise the internal map of ID |
| 479 | | * attributes will be incomplete and scrolling will flounder. |
| 480 | | */ |
| 481 | | private void scrollToParagraph( final int id, final boolean force ) { |
| 482 | | synchronized( mMutex ) { |
| 483 | | final var previewPane = getPreviewPane(); |
| 484 | | final var scrollPane = previewPane.getScrollPane(); |
| 485 | | final int approxId = getActiveEditorPane().approximateParagraphId( id ); |
| 486 | | |
| 487 | | if( force ) { |
| 488 | | previewPane.scrollTo( approxId ); |
| 489 | | } |
| 490 | | else { |
| 491 | | previewPane.tryScrollTo( approxId ); |
| 492 | | } |
| 493 | | |
| 494 | | scrollPane.repaint(); |
| 495 | | } |
| 496 | | } |
| 497 | | |
| 498 | | private void updateVariableNameInjector( final FileEditorTab tab ) { |
| 499 | | getVariableNameInjector().addListener( tab ); |
| 500 | | } |
| 501 | | |
| 502 | | /** |
| 503 | | * Called whenever the preview pane becomes out of sync with the file editor |
| 504 | | * tab. This can be called when the text changes, the caret paragraph |
| 505 | | * changes, or the file tab changes. |
| 506 | | * |
| 507 | | * @param tab The file editor tab that has been changed in some fashion. |
| 508 | | */ |
| 509 | | private void process( final FileEditorTab tab ) { |
| 510 | | if( tab != null ) { |
| 511 | | getPreviewPane().setPath( tab.getPath() ); |
| 512 | | |
| 513 | | final Processor<String> processor = getProcessors().computeIfAbsent( |
| 514 | | tab, p -> createProcessors( tab ) |
| 515 | | ); |
| 516 | | |
| 517 | | try { |
| 518 | | processChain( processor, tab.getEditorText() ); |
| 519 | | } catch( final Exception ex ) { |
| 520 | | alert( ex ); |
| 521 | | } |
| 522 | | } |
| 523 | | } |
| 524 | | |
| 525 | | /** |
| 526 | | * Executes the processing chain, operating on the given string. |
| 527 | | * |
| 528 | | * @param handler The first processor in the chain to call. |
| 529 | | * @param text The initial value of the text to process. |
| 530 | | * @return The final value of the text that was processed by the chain. |
| 531 | | */ |
| 532 | | private String processChain( Processor<String> handler, String text ) { |
| 533 | | while( handler != null && text != null ) { |
| 534 | | text = handler.apply( text ); |
| 535 | | handler = handler.next(); |
| 536 | | } |
| 537 | | |
| 538 | | return text; |
| 539 | | } |
| 540 | | |
| 541 | | private void renderActiveTab() { |
| 542 | | process( getActiveFileEditorTab() ); |
| 543 | | } |
| 544 | | |
| 545 | | /** |
| 546 | | * Called when a definition source is opened. |
| 547 | | * |
| 548 | | * @param path Path to the definition source that was opened. |
| 549 | | */ |
| 550 | | private void openDefinitions( final Path path ) { |
| 551 | | try { |
| 552 | | final var ds = createDefinitionSource( path ); |
| 553 | | setDefinitionSource( ds ); |
| 554 | | |
| 555 | | final var prefs = getUserPreferences(); |
| 556 | | prefs.definitionPathProperty().setValue( path.toFile() ); |
| 557 | | prefs.save(); |
| 558 | | |
| 559 | | final var tooltipPath = new Tooltip( path.toString() ); |
| 560 | | tooltipPath.setShowDelay( Duration.millis( 200 ) ); |
| 561 | | |
| 562 | | final var pane = getDefinitionPane(); |
| 563 | | pane.update( ds ); |
| 564 | | pane.addTreeChangeHandler( mTreeHandler ); |
| 565 | | pane.addKeyEventHandler( mDefinitionKeyHandler ); |
| 566 | | pane.filenameProperty().setValue( path.getFileName().toString() ); |
| 567 | | pane.setTooltip( tooltipPath ); |
| 568 | | |
| 569 | | interpolateResolvedMap(); |
| 570 | | } catch( final Exception ex ) { |
| 571 | | alert( ex ); |
| 572 | | } |
| 573 | | } |
| 574 | | |
| 575 | | private void exportDefinitions( final Path path ) { |
| 576 | | try { |
| 577 | | final var pane = getDefinitionPane(); |
| 578 | | final var root = pane.getTreeView().getRoot(); |
| 579 | | final var problemChild = pane.isTreeWellFormed(); |
| 580 | | |
| 581 | | if( problemChild == null ) { |
| 582 | | getDefinitionSource().getTreeAdapter().export( root, path ); |
| 583 | | } |
| 584 | | else { |
| 585 | | alert( "yaml.error.tree.form", problemChild.getValue() ); |
| 586 | | } |
| 587 | | } catch( final Exception ex ) { |
| 588 | | alert( ex ); |
| 589 | | } |
| 590 | | } |
| 591 | | |
| 592 | | private void interpolateResolvedMap() { |
| 593 | | final var treeMap = getDefinitionPane().toMap(); |
| 594 | | final var map = new HashMap<>( treeMap ); |
| 595 | | MapInterpolator.interpolate( map ); |
| 596 | | |
| 597 | | getResolvedMap().clear(); |
| 598 | | getResolvedMap().putAll( map ); |
| 599 | | } |
| 600 | | |
| 601 | | private void initDefinitionPane() { |
| 602 | | openDefinitions( getDefinitionPath() ); |
| 603 | | } |
| 604 | | |
| 605 | | //---- File actions ------------------------------------------------------- |
| 606 | | |
| 607 | | /** |
| 608 | | * Called when an {@link Observable} instance has changed. This is called |
| 609 | | * by both the {@link Snitch} service and the notify service. The @link |
| 610 | | * Snitch} service can be called for different file types, including |
| 611 | | * {@link DefinitionSource} instances. |
| 612 | | * |
| 613 | | * @param observable The observed instance. |
| 614 | | * @param value The noteworthy item. |
| 615 | | */ |
| 616 | | @Override |
| 617 | | public void update( final Observable observable, final Object value ) { |
| 618 | | if( value instanceof Path && observable instanceof Snitch ) { |
| 619 | | updateSelectedTab(); |
| 620 | | } |
| 621 | | } |
| 622 | | |
| 623 | | /** |
| 624 | | * Called when a file has been modified. |
| 625 | | */ |
| 626 | | private void updateSelectedTab() { |
| 627 | | rerender(); |
| 628 | | } |
| 629 | | |
| 630 | | /** |
| 631 | | * After resetting the processors, they will refresh anew to be up-to-date |
| 632 | | * with the files (text and definition) currently loaded into the editor. |
| 633 | | */ |
| 634 | | private void resetProcessors() { |
| 635 | | getProcessors().clear(); |
| 636 | | } |
| 637 | | |
| 638 | | //---- File actions ------------------------------------------------------- |
| 639 | | |
| 640 | | private void fileNew() { |
| 641 | | getFileEditorPane().newEditor(); |
| 642 | | } |
| 643 | | |
| 644 | | private void fileOpen() { |
| 645 | | getFileEditorPane().openFileDialog(); |
| 646 | | } |
| 647 | | |
| 648 | | private void fileClose() { |
| 649 | | getFileEditorPane().closeEditor( getActiveFileEditorTab(), true ); |
| 650 | | } |
| 651 | | |
| 652 | | /** |
| 653 | | * TODO: Upon closing, first remove the tab change listeners. (There's no |
| 654 | | * need to re-render each tab when all are being closed.) |
| 655 | | */ |
| 656 | | private void fileCloseAll() { |
| 657 | | getFileEditorPane().closeAllEditors(); |
| 658 | | } |
| 659 | | |
| 660 | | private void fileSave() { |
| 661 | | getFileEditorPane().saveEditor( getActiveFileEditorTab() ); |
| 662 | | } |
| 663 | | |
| 664 | | private void fileSaveAs() { |
| 665 | | final FileEditorTab editor = getActiveFileEditorTab(); |
| 666 | | getFileEditorPane().saveEditorAs( editor ); |
| 667 | | getProcessors().remove( editor ); |
| 668 | | |
| 669 | | try { |
| 670 | | process( editor ); |
| 671 | | } catch( final Exception ex ) { |
| 672 | | alert( ex ); |
| 673 | | } |
| 674 | | } |
| 675 | | |
| 676 | | private void fileSaveAll() { |
| 677 | | getFileEditorPane().saveAllEditors(); |
| 678 | | } |
| 679 | | |
| 680 | | private void fileExit() { |
| 681 | | final Window window = getWindow(); |
| 682 | | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 683 | | } |
| 684 | | |
| 685 | | //---- Edit actions ------------------------------------------------------- |
| 686 | | |
| 687 | | /** |
| 688 | | * Transform the Markdown into HTML then copy that HTML into the copy |
| 689 | | * buffer. |
| 690 | | */ |
| 691 | | private void copyHtml() { |
| 692 | | final var markdown = getActiveEditorPane().getText(); |
| 693 | | final var processors = createProcessorFactory().createProcessors( |
| 694 | | getActiveFileEditorTab() |
| 695 | | ); |
| 696 | | |
| 697 | | final var chain = processors.remove( HtmlPreviewProcessor.class ); |
| 698 | | |
| 699 | | final String html = processChain( chain, markdown ); |
| 700 | | |
| 701 | | final Clipboard clipboard = Clipboard.getSystemClipboard(); |
| 702 | | final ClipboardContent content = new ClipboardContent(); |
| 703 | | content.putString( html ); |
| 704 | | clipboard.setContent( content ); |
| 705 | | } |
| 706 | | |
| 707 | | /** |
| 708 | | * Used to find text in the active file editor window. |
| 709 | | */ |
| 710 | | private void editFind() { |
| 711 | | final TextField input = getFindTextField(); |
| 712 | | getStatusBar().setGraphic( input ); |
| 713 | | input.requestFocus(); |
| 714 | | } |
| 715 | | |
| 716 | | public void editFindNext() { |
| 717 | | getActiveFileEditorTab().searchNext( getFindTextField().getText() ); |
| 718 | | } |
| 719 | | |
| 720 | | public void editPreferences() { |
| 721 | | getUserPreferences().show(); |
| 722 | | } |
| 723 | | |
| 724 | | //---- Insert actions ----------------------------------------------------- |
| 725 | | |
| 726 | | /** |
| 727 | | * Delegates to the active editor to handle wrapping the current text |
| 728 | | * selection with leading and trailing strings. |
| 729 | | * |
| 730 | | * @param leading The string to put before the selection. |
| 731 | | * @param trailing The string to put after the selection. |
| 732 | | */ |
| 733 | | private void insertMarkdown( |
| 734 | | final String leading, final String trailing ) { |
| 735 | | getActiveEditorPane().surroundSelection( leading, trailing ); |
| 736 | | } |
| 737 | | |
| 738 | | private void insertMarkdown( |
| 739 | | final String leading, final String trailing, final String hint ) { |
| 740 | | getActiveEditorPane().surroundSelection( leading, trailing, hint ); |
| 741 | | } |
| 742 | | |
| 743 | | //---- View actions ------------------------------------------------------- |
| 744 | | |
| 745 | | private void viewRefresh() { |
| 746 | | rerender(); |
| 747 | | } |
| 748 | | |
| 749 | | //---- Help actions ------------------------------------------------------- |
| 750 | | |
| 751 | | private void helpAbout() { |
| 752 | | final Alert alert = new Alert( AlertType.INFORMATION ); |
| 753 | | alert.setTitle( get( "Dialog.about.title" ) ); |
| 754 | | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 755 | | alert.setContentText( get( "Dialog.about.content" ) ); |
| 756 | | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 757 | | alert.initOwner( getWindow() ); |
| 758 | | |
| 759 | | alert.showAndWait(); |
| 760 | | } |
| 761 | | |
| 762 | | //---- Member creators ---------------------------------------------------- |
| 763 | | |
| 764 | | private SpellChecker createSpellChecker() { |
| 765 | | try { |
| 766 | | final Collection<String> lexicon = readLexicon( "en.txt" ); |
| 767 | | return SymSpellSpeller.forLexicon( lexicon ); |
| 768 | | } catch( final Exception ex ) { |
| 769 | | alert( ex ); |
| 770 | | return new PermissiveSpeller(); |
| 771 | | } |
| 772 | | } |
| 773 | | |
| 774 | | /** |
| 775 | | * Factory to create processors that are suited to different file types. |
| 776 | | * |
| 777 | | * @param tab The tab that is subjected to processing. |
| 778 | | * @return A processor suited to the file type specified by the tab's path. |
| 779 | | */ |
| 780 | | private Processor<String> createProcessors( final FileEditorTab tab ) { |
| 781 | | return createProcessorFactory().createProcessors( tab ); |
| 782 | | } |
| 783 | | |
| 784 | | private ProcessorFactory createProcessorFactory() { |
| 785 | | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 786 | | } |
| 787 | | |
| 788 | | private HTMLPreviewPane createHTMLPreviewPane() { |
| 789 | | return new HTMLPreviewPane(); |
| 790 | | } |
| 791 | | |
| 792 | | private DefinitionSource createDefaultDefinitionSource() { |
| 793 | | return new YamlDefinitionSource( getDefinitionPath() ); |
| 794 | | } |
| 795 | | |
| 796 | | private DefinitionSource createDefinitionSource( final Path path ) { |
| 797 | | try { |
| 798 | | return createDefinitionFactory().createDefinitionSource( path ); |
| 799 | | } catch( final Exception ex ) { |
| 800 | | alert( ex ); |
| 801 | | return createDefaultDefinitionSource(); |
| 802 | | } |
| 803 | | } |
| 804 | | |
| 805 | | private TextField createFindTextField() { |
| 806 | | return new TextField(); |
| 807 | | } |
| 808 | | |
| 809 | | private DefinitionFactory createDefinitionFactory() { |
| 810 | | return new DefinitionFactory(); |
| 811 | | } |
| 812 | | |
| 813 | | private StatusBar createStatusBar() { |
| 814 | | return new StatusBar(); |
| 815 | | } |
| 816 | | |
| 817 | | private Scene createScene() { |
| 818 | | final SplitPane splitPane = new SplitPane( |
| 819 | | getDefinitionPane(), |
| 820 | | getFileEditorPane(), |
| 821 | | getPreviewPane() ); |
| 822 | | |
| 823 | | splitPane.setDividerPositions( |
| 824 | | getFloat( K_PANE_SPLIT_DEFINITION, .22f ), |
| 825 | | getFloat( K_PANE_SPLIT_EDITOR, .60f ), |
| 826 | | getFloat( K_PANE_SPLIT_PREVIEW, .18f ) ); |
| 827 | | |
| 828 | | getDefinitionPane().prefHeightProperty() |
| 829 | | .bind( splitPane.heightProperty() ); |
| 830 | | |
| 831 | | final BorderPane borderPane = new BorderPane(); |
| 832 | | borderPane.setPrefSize( 1280, 800 ); |
| 833 | | borderPane.setTop( createMenuBar() ); |
| 834 | | borderPane.setBottom( getStatusBar() ); |
| 835 | | borderPane.setCenter( splitPane ); |
| 836 | | |
| 837 | | final VBox statusBar = new VBox(); |
| 838 | | statusBar.setAlignment( Pos.BASELINE_CENTER ); |
| 839 | | statusBar.getChildren().add( getLineNumberText() ); |
| 840 | | getStatusBar().getRightItems().add( statusBar ); |
| 841 | | |
| 842 | | // Force preview pane refresh on Windows. |
| 843 | | if( SystemUtils.IS_OS_WINDOWS ) { |
| 844 | | splitPane.getDividers().get( 1 ).positionProperty().addListener( |
| 845 | | ( l, oValue, nValue ) -> runLater( |
| 846 | | () -> getPreviewPane().getScrollPane().repaint() |
| 847 | | ) |
| 848 | | ); |
| 849 | | } |
| 850 | | |
| 851 | | return new Scene( borderPane ); |
| 852 | | } |
| 853 | | |
| 854 | | private Text createLineNumberText() { |
| 855 | | return new Text( get( STATUS_BAR_LINE, 1, 1, 1 ) ); |
| 856 | | } |
| 857 | | |
| 858 | | private Node createMenuBar() { |
| 859 | | final BooleanBinding activeFileEditorIsNull = |
| 860 | | getFileEditorPane().activeFileEditorProperty().isNull(); |
| 861 | | |
| 862 | | // File actions |
| 863 | | final Action fileNewAction = new ActionBuilder() |
| 864 | | .setText( "Main.menu.file.new" ) |
| 865 | | .setAccelerator( "Shortcut+N" ) |
| 866 | | .setIcon( FILE_ALT ) |
| 867 | | .setAction( e -> fileNew() ) |
| 868 | | .build(); |
| 869 | | final Action fileOpenAction = new ActionBuilder() |
| 870 | | .setText( "Main.menu.file.open" ) |
| 871 | | .setAccelerator( "Shortcut+O" ) |
| 872 | | .setIcon( FOLDER_OPEN_ALT ) |
| 873 | | .setAction( e -> fileOpen() ) |
| 874 | | .build(); |
| 875 | | final Action fileCloseAction = new ActionBuilder() |
| 876 | | .setText( "Main.menu.file.close" ) |
| 877 | | .setAccelerator( "Shortcut+W" ) |
| 878 | | .setAction( e -> fileClose() ) |
| 879 | | .setDisable( activeFileEditorIsNull ) |
| 880 | | .build(); |
| 881 | | final Action fileCloseAllAction = new ActionBuilder() |
| 882 | | .setText( "Main.menu.file.close_all" ) |
| 883 | | .setAction( e -> fileCloseAll() ) |
| 884 | | .setDisable( activeFileEditorIsNull ) |
| 885 | | .build(); |
| 886 | | final Action fileSaveAction = new ActionBuilder() |
| 887 | | .setText( "Main.menu.file.save" ) |
| 888 | | .setAccelerator( "Shortcut+S" ) |
| 889 | | .setIcon( FLOPPY_ALT ) |
| 890 | | .setAction( e -> fileSave() ) |
| 891 | | .setDisable( createActiveBooleanProperty( |
| 892 | | FileEditorTab::modifiedProperty ).not() ) |
| 893 | | .build(); |
| 894 | | final Action fileSaveAsAction = new ActionBuilder() |
| 895 | | .setText( "Main.menu.file.save_as" ) |
| 896 | | .setAction( e -> fileSaveAs() ) |
| 897 | | .setDisable( activeFileEditorIsNull ) |
| 898 | | .build(); |
| 899 | | final Action fileSaveAllAction = new ActionBuilder() |
| 900 | | .setText( "Main.menu.file.save_all" ) |
| 901 | | .setAccelerator( "Shortcut+Shift+S" ) |
| 902 | | .setAction( e -> fileSaveAll() ) |
| 903 | | .setDisable( Bindings.not( |
| 904 | | getFileEditorPane().anyFileEditorModifiedProperty() ) ) |
| 905 | | .build(); |
| 906 | | final Action fileExitAction = new ActionBuilder() |
| 907 | | .setText( "Main.menu.file.exit" ) |
| 908 | | .setAction( e -> fileExit() ) |
| 909 | | .build(); |
| 910 | | |
| 911 | | // Edit actions |
| 912 | | final Action editCopyHtmlAction = new ActionBuilder() |
| 913 | | .setText( "Main.menu.edit.copy.html" ) |
| 914 | | .setIcon( HTML5 ) |
| 915 | | .setAction( e -> copyHtml() ) |
| 916 | | .setDisable( activeFileEditorIsNull ) |
| 917 | | .build(); |
| 918 | | |
| 919 | | final Action editUndoAction = new ActionBuilder() |
| 920 | | .setText( "Main.menu.edit.undo" ) |
| 921 | | .setAccelerator( "Shortcut+Z" ) |
| 922 | | .setIcon( UNDO ) |
| 923 | | .setAction( e -> getActiveEditorPane().undo() ) |
| 924 | | .setDisable( createActiveBooleanProperty( |
| 925 | | FileEditorTab::canUndoProperty ).not() ) |
| 926 | | .build(); |
| 927 | | final Action editRedoAction = new ActionBuilder() |
| 928 | | .setText( "Main.menu.edit.redo" ) |
| 929 | | .setAccelerator( "Shortcut+Y" ) |
| 930 | | .setIcon( REPEAT ) |
| 931 | | .setAction( e -> getActiveEditorPane().redo() ) |
| 932 | | .setDisable( createActiveBooleanProperty( |
| 933 | | FileEditorTab::canRedoProperty ).not() ) |
| 934 | | .build(); |
| 935 | | |
| 936 | | final Action editCutAction = new ActionBuilder() |
| 937 | | .setText( "Main.menu.edit.cut" ) |
| 938 | | .setAccelerator( "Shortcut+X" ) |
| 939 | | .setIcon( CUT ) |
| 940 | | .setAction( e -> getActiveEditorPane().cut() ) |
| 941 | | .setDisable( activeFileEditorIsNull ) |
| 942 | | .build(); |
| 943 | | final Action editCopyAction = new ActionBuilder() |
| 944 | | .setText( "Main.menu.edit.copy" ) |
| 945 | | .setAccelerator( "Shortcut+C" ) |
| 946 | | .setIcon( COPY ) |
| 947 | | .setAction( e -> getActiveEditorPane().copy() ) |
| 948 | | .setDisable( activeFileEditorIsNull ) |
| 949 | | .build(); |
| 950 | | final Action editPasteAction = new ActionBuilder() |
| 951 | | .setText( "Main.menu.edit.paste" ) |
| 952 | | .setAccelerator( "Shortcut+V" ) |
| 953 | | .setIcon( PASTE ) |
| 954 | | .setAction( e -> getActiveEditorPane().paste() ) |
| 955 | | .setDisable( activeFileEditorIsNull ) |
| 956 | | .build(); |
| 957 | | final Action editSelectAllAction = new ActionBuilder() |
| 958 | | .setText( "Main.menu.edit.selectAll" ) |
| 959 | | .setAccelerator( "Shortcut+A" ) |
| 960 | | .setAction( e -> getActiveEditorPane().selectAll() ) |
| 961 | | .setDisable( activeFileEditorIsNull ) |
| 962 | | .build(); |
| 963 | | |
| 964 | | final Action editFindAction = new ActionBuilder() |
| 965 | | .setText( "Main.menu.edit.find" ) |
| 966 | | .setAccelerator( "Ctrl+F" ) |
| 967 | | .setIcon( SEARCH ) |
| 968 | | .setAction( e -> editFind() ) |
| 969 | | .setDisable( activeFileEditorIsNull ) |
| 970 | | .build(); |
| 971 | | final Action editFindNextAction = new ActionBuilder() |
| 972 | | .setText( "Main.menu.edit.find.next" ) |
| 973 | | .setAccelerator( "F3" ) |
| 974 | | .setIcon( null ) |
| 975 | | .setAction( e -> editFindNext() ) |
| 976 | | .setDisable( activeFileEditorIsNull ) |
| 977 | | .build(); |
| 978 | | final Action editPreferencesAction = new ActionBuilder() |
| 979 | | .setText( "Main.menu.edit.preferences" ) |
| 980 | | .setAccelerator( "Ctrl+Alt+S" ) |
| 981 | | .setAction( e -> editPreferences() ) |
| 982 | | .build(); |
| 983 | | |
| 984 | | // Format actions |
| 985 | | final Action formatBoldAction = new ActionBuilder() |
| 986 | | .setText( "Main.menu.format.bold" ) |
| 987 | | .setAccelerator( "Shortcut+B" ) |
| 988 | | .setIcon( BOLD ) |
| 989 | | .setAction( e -> insertMarkdown( "**", "**" ) ) |
| 990 | | .setDisable( activeFileEditorIsNull ) |
| 991 | | .build(); |
| 992 | | final Action formatItalicAction = new ActionBuilder() |
| 993 | | .setText( "Main.menu.format.italic" ) |
| 994 | | .setAccelerator( "Shortcut+I" ) |
| 995 | | .setIcon( ITALIC ) |
| 996 | | .setAction( e -> insertMarkdown( "*", "*" ) ) |
| 997 | | .setDisable( activeFileEditorIsNull ) |
| 998 | | .build(); |
| 999 | | final Action formatSuperscriptAction = new ActionBuilder() |
| 1000 | | .setText( "Main.menu.format.superscript" ) |
| 1001 | | .setAccelerator( "Shortcut+[" ) |
| 1002 | | .setIcon( SUPERSCRIPT ) |
| 1003 | | .setAction( e -> insertMarkdown( "^", "^" ) ) |
| 1004 | | .setDisable( activeFileEditorIsNull ) |
| 1005 | | .build(); |
| 1006 | | final Action formatSubscriptAction = new ActionBuilder() |
| 1007 | | .setText( "Main.menu.format.subscript" ) |
| 1008 | | .setAccelerator( "Shortcut+]" ) |
| 1009 | | .setIcon( SUBSCRIPT ) |
| 1010 | | .setAction( e -> insertMarkdown( "~", "~" ) ) |
| 1011 | | .setDisable( activeFileEditorIsNull ) |
| 1012 | | .build(); |
| 1013 | | final Action formatStrikethroughAction = new ActionBuilder() |
| 1014 | | .setText( "Main.menu.format.strikethrough" ) |
| 1015 | | .setAccelerator( "Shortcut+T" ) |
| 1016 | | .setIcon( STRIKETHROUGH ) |
| 1017 | | .setAction( e -> insertMarkdown( "~~", "~~" ) ) |
| 1018 | | .setDisable( activeFileEditorIsNull ) |
| 1019 | | .build(); |
| 1020 | | |
| 1021 | | // Insert actions |
| 1022 | | final Action insertBlockquoteAction = new ActionBuilder() |
| 1023 | | .setText( "Main.menu.insert.blockquote" ) |
| 1024 | | .setAccelerator( "Ctrl+Q" ) |
| 1025 | | .setIcon( QUOTE_LEFT ) |
| 1026 | | .setAction( e -> insertMarkdown( "\n\n> ", "" ) ) |
| 1027 | | .setDisable( activeFileEditorIsNull ) |
| 1028 | | .build(); |
| 1029 | | final Action insertCodeAction = new ActionBuilder() |
| 1030 | | .setText( "Main.menu.insert.code" ) |
| 1031 | | .setAccelerator( "Shortcut+K" ) |
| 1032 | | .setIcon( CODE ) |
| 1033 | | .setAction( e -> insertMarkdown( "`", "`" ) ) |
| 1034 | | .setDisable( activeFileEditorIsNull ) |
| 1035 | | .build(); |
| 1036 | | final Action insertFencedCodeBlockAction = new ActionBuilder() |
| 1037 | | .setText( "Main.menu.insert.fenced_code_block" ) |
| 1038 | | .setAccelerator( "Shortcut+Shift+K" ) |
| 1039 | | .setIcon( FILE_CODE_ALT ) |
| 1040 | | .setAction( e -> insertMarkdown( |
| 1041 | | "\n\n```\n", |
| 1042 | | "\n```\n\n", |
| 1043 | | get( "Main.menu.insert.fenced_code_block.prompt" ) ) ) |
| 1044 | | .setDisable( activeFileEditorIsNull ) |
| 1045 | | .build(); |
| 1046 | | final Action insertLinkAction = new ActionBuilder() |
| 1047 | | .setText( "Main.menu.insert.link" ) |
| 1048 | | .setAccelerator( "Shortcut+L" ) |
| 1049 | | .setIcon( LINK ) |
| 1050 | | .setAction( e -> getActiveEditorPane().insertLink() ) |
| 1051 | | .setDisable( activeFileEditorIsNull ) |
| 1052 | | .build(); |
| 1053 | | final Action insertImageAction = new ActionBuilder() |
| 1054 | | .setText( "Main.menu.insert.image" ) |
| 1055 | | .setAccelerator( "Shortcut+G" ) |
| 1056 | | .setIcon( PICTURE_ALT ) |
| 1057 | | .setAction( e -> getActiveEditorPane().insertImage() ) |
| 1058 | | .setDisable( activeFileEditorIsNull ) |
| 1059 | | .build(); |
| 1060 | | |
| 1061 | | // Number of heading actions (H1 ... H3) |
| 1062 | | final int HEADINGS = 3; |
| 1063 | | final Action[] headings = new Action[ HEADINGS ]; |
| 1064 | | |
| 1065 | | for( int i = 1; i <= HEADINGS; i++ ) { |
| 1066 | | final String hashes = new String( new char[ i ] ).replace( "\0", "#" ); |
| 1067 | | final String markup = String.format( "%n%n%s ", hashes ); |
| 1068 | | final String text = "Main.menu.insert.heading." + i; |
| 1069 | | final String accelerator = "Shortcut+" + i; |
| 1070 | | final String prompt = text + ".prompt"; |
| 1071 | | |
| 1072 | | headings[ i - 1 ] = new ActionBuilder() |
| 1073 | | .setText( text ) |
| 1074 | | .setAccelerator( accelerator ) |
| 1075 | | .setIcon( HEADER ) |
| 1076 | | .setAction( e -> insertMarkdown( markup, "", get( prompt ) ) ) |
| 1077 | | .setDisable( activeFileEditorIsNull ) |
| 1078 | | .build(); |
| 1079 | | } |
| 1080 | | |
| 1081 | | final Action insertUnorderedListAction = new ActionBuilder() |
| 1082 | | .setText( "Main.menu.insert.unordered_list" ) |
| 1083 | | .setAccelerator( "Shortcut+U" ) |
| 1084 | | .setIcon( LIST_UL ) |
| 1085 | | .setAction( e -> insertMarkdown( "\n\n* ", "" ) ) |
| 1086 | | .setDisable( activeFileEditorIsNull ) |
| 1087 | | .build(); |
| 1088 | | final Action insertOrderedListAction = new ActionBuilder() |
| 1089 | | .setText( "Main.menu.insert.ordered_list" ) |
| 1090 | | .setAccelerator( "Shortcut+Shift+O" ) |
| 1091 | | .setIcon( LIST_OL ) |
| 1092 | | .setAction( e -> insertMarkdown( |
| 1093 | | "\n\n1. ", "" ) ) |
| 1094 | | .setDisable( activeFileEditorIsNull ) |
| 1095 | | .build(); |
| 1096 | | final Action insertHorizontalRuleAction = new ActionBuilder() |
| 1097 | | .setText( "Main.menu.insert.horizontal_rule" ) |
| 1098 | | .setAccelerator( "Shortcut+H" ) |
| 1099 | | .setAction( e -> insertMarkdown( |
| 1100 | | "\n\n---\n\n", "" ) ) |
| 1101 | | .setDisable( activeFileEditorIsNull ) |
| 1102 | | .build(); |
| 1103 | | |
| 1104 | | // Definition actions |
| 1105 | | final Action definitionCreateAction = new ActionBuilder() |
| 1106 | | .setText( "Main.menu.definition.create" ) |
| 1107 | | .setIcon( TREE ) |
| 1108 | | .setAction( e -> getDefinitionPane().addItem() ) |
| 1109 | | .build(); |
| 1110 | | final Action definitionInsertAction = new ActionBuilder() |
| 1111 | | .setText( "Main.menu.definition.insert" ) |
| 1112 | | .setAccelerator( "Ctrl+Space" ) |
| 1113 | | .setIcon( STAR ) |
| 1114 | | .setAction( e -> definitionInsert() ) |
| 1115 | | .build(); |
| 1116 | | |
| 1117 | | // Help actions |
| 1118 | | final Action helpAboutAction = new ActionBuilder() |
| 1119 | | .setText( "Main.menu.help.about" ) |
| 1120 | | .setAction( e -> helpAbout() ) |
| 1121 | | .build(); |
| 1122 | | |
| 1123 | | //---- MenuBar ---- |
| 1124 | | |
| 1125 | | // File Menu |
| 1126 | | final var fileMenu = ActionUtils.createMenu( |
| 1127 | | get( "Main.menu.file" ), |
| 1128 | | fileNewAction, |
| 1129 | | fileOpenAction, |
| 1130 | | null, |
| 1131 | | fileCloseAction, |
| 1132 | | fileCloseAllAction, |
| 1133 | | null, |
| 1134 | | fileSaveAction, |
| 1135 | | fileSaveAsAction, |
| 1136 | | fileSaveAllAction, |
| 1137 | | null, |
| 1138 | | fileExitAction ); |
| 1139 | | |
| 1140 | | // Edit Menu |
| 1141 | | final var editMenu = ActionUtils.createMenu( |
| 1142 | | get( "Main.menu.edit" ), |
| 1143 | | editCopyHtmlAction, |
| 1144 | | null, |
| 1145 | | editUndoAction, |
| 1146 | | editRedoAction, |
| 1147 | | null, |
| 1148 | | editCutAction, |
| 1149 | | editCopyAction, |
| 1150 | | editPasteAction, |
| 1151 | | editSelectAllAction, |
| 1152 | | null, |
| 1153 | | editFindAction, |
| 1154 | | editFindNextAction, |
| 1155 | | null, |
| 1156 | | editPreferencesAction ); |
| 1157 | | |
| 1158 | | // Format Menu |
| 1159 | | final var formatMenu = ActionUtils.createMenu( |
| 1160 | | get( "Main.menu.format" ), |
| 1161 | | formatBoldAction, |
| 1162 | | formatItalicAction, |
| 1163 | | formatSuperscriptAction, |
| 1164 | | formatSubscriptAction, |
| 1165 | | formatStrikethroughAction |
| 1166 | | ); |
| 1167 | | |
| 1168 | | // Insert Menu |
| 1169 | | final var insertMenu = ActionUtils.createMenu( |
| 1170 | | get( "Main.menu.insert" ), |
| 1171 | | insertBlockquoteAction, |
| 1172 | | insertCodeAction, |
| 1173 | | insertFencedCodeBlockAction, |
| 1174 | | null, |
| 1175 | | insertLinkAction, |
| 1176 | | insertImageAction, |
| 1177 | | null, |
| 1178 | | headings[ 0 ], |
| 1179 | | headings[ 1 ], |
| 1180 | | headings[ 2 ], |
| 1181 | | null, |
| 1182 | | insertUnorderedListAction, |
| 1183 | | insertOrderedListAction, |
| 1184 | | insertHorizontalRuleAction |
| 1185 | | ); |
| 1186 | | |
| 1187 | | // Definition Menu |
| 1188 | | final var definitionMenu = ActionUtils.createMenu( |
| 1189 | | get( "Main.menu.definition" ), |
| 1190 | | definitionCreateAction, |
| 1191 | | definitionInsertAction ); |
| 1192 | | |
| 1193 | | // Help Menu |
| 1194 | | final var helpMenu = ActionUtils.createMenu( |
| 1195 | | get( "Main.menu.help" ), |
| 1196 | | helpAboutAction ); |
| 1197 | | |
| 1198 | | //---- MenuBar ---- |
| 1199 | | final var menuBar = new MenuBar( |
| 1200 | | fileMenu, |
| 1201 | | editMenu, |
| 1202 | | formatMenu, |
| 1203 | | insertMenu, |
| 1204 | | definitionMenu, |
| 1205 | | helpMenu ); |
| 1206 | | |
| 1207 | | //---- ToolBar ---- |
| 1208 | | final var toolBar = ActionUtils.createToolBar( |
| 1209 | | fileNewAction, |
| 1210 | | fileOpenAction, |
| 1211 | | fileSaveAction, |
| 1212 | | null, |
| 1213 | | editUndoAction, |
| 1214 | | editRedoAction, |
| 1215 | | editCutAction, |
| 1216 | | editCopyAction, |
| 1217 | | editPasteAction, |
| 1218 | | null, |
| 1219 | | formatBoldAction, |
| 1220 | | formatItalicAction, |
| 1221 | | formatSuperscriptAction, |
| 1222 | | formatSubscriptAction, |
| 1223 | | insertBlockquoteAction, |
| 1224 | | insertCodeAction, |
| 1225 | | insertFencedCodeBlockAction, |
| 1226 | | null, |
| 1227 | | insertLinkAction, |
| 1228 | | insertImageAction, |
| 1229 | | null, |
| 1230 | | headings[ 0 ], |
| 1231 | | null, |
| 1232 | | insertUnorderedListAction, |
| 1233 | | insertOrderedListAction ); |
| 1234 | | |
| 1235 | | return new VBox( menuBar, toolBar ); |
| 1236 | | } |
| 1237 | | |
| 1238 | | /** |
| 1239 | | * Performs the autoinsert function on the active file editor. |
| 1240 | | */ |
| 1241 | | private void definitionInsert() { |
| 1242 | | } |
| 1243 | | |
| 1244 | | /** |
| 1245 | | * Creates a boolean property that is bound to another boolean value of the |
| 1246 | | * active editor. |
| 1247 | | */ |
| 1248 | | private BooleanProperty createActiveBooleanProperty( |
| 1249 | | final Function<FileEditorTab, ObservableBooleanValue> func ) { |
| 1250 | | |
| 1251 | | final BooleanProperty b = new SimpleBooleanProperty(); |
| 1252 | | final FileEditorTab tab = getActiveFileEditorTab(); |
| 1253 | | |
| 1254 | | if( tab != null ) { |
| 1255 | | b.bind( func.apply( tab ) ); |
| 1256 | | } |
| 1257 | | |
| 1258 | | getFileEditorPane().activeFileEditorProperty().addListener( |
| 1259 | | ( observable, oldFileEditor, newFileEditor ) -> { |
| 1260 | | b.unbind(); |
| 1261 | | |
| 1262 | | if( newFileEditor == null ) { |
| 1263 | | b.set( false ); |
| 1264 | | } |
| 1265 | | else { |
| 1266 | | b.bind( func.apply( newFileEditor ) ); |
| 1267 | | } |
| 1268 | | } |
| 1269 | | ); |
| 1270 | | |
| 1271 | | return b; |
| 1272 | | } |
| 1273 | | |
| 1274 | | //---- Convenience accessors ---------------------------------------------- |
| 1275 | | |
| 1276 | | private Preferences getPreferences() { |
| 1277 | | return sOptions.getState(); |
| 1278 | | } |
| 1279 | | |
| 1280 | | private int getCurrentParagraphIndex() { |
| 1281 | | return getActiveEditorPane().getCurrentParagraphIndex(); |
| 1282 | | } |
| 1283 | | |
| 1284 | | private float getFloat( final String key, final float defaultValue ) { |
| 1285 | | return getPreferences().getFloat( key, defaultValue ); |
| 1286 | | } |
| 1287 | | |
| 1288 | | public Window getWindow() { |
| 1289 | | return getScene().getWindow(); |
| 1290 | | } |
| 1291 | | |
| 1292 | | private MarkdownEditorPane getActiveEditorPane() { |
| 1293 | | return getActiveFileEditorTab().getEditorPane(); |
| 1294 | | } |
| 1295 | | |
| 1296 | | private FileEditorTab getActiveFileEditorTab() { |
| 1297 | | return getFileEditorPane().getActiveFileEditor(); |
| 1298 | | } |
| 1299 | | |
| 1300 | | //---- Member accessors --------------------------------------------------- |
| 1301 | | |
| 1302 | | protected Scene getScene() { |
| 1303 | | return mScene; |
| 1304 | | } |
| 1305 | | |
| 1306 | | private SpellChecker getSpellChecker() { |
| 1307 | | return mSpellChecker; |
| 1308 | | } |
| 1309 | | |
| 1310 | | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 1311 | | return mProcessors; |
| 1312 | | } |
| 1313 | | |
| 1314 | | private FileEditorTabPane getFileEditorPane() { |
| 1315 | | return mFileEditorPane; |
| 1316 | | } |
| 1317 | | |
| 1318 | | private HTMLPreviewPane getPreviewPane() { |
| 1319 | | return mPreviewPane; |
| 1320 | | } |
| 1321 | | |
| 1322 | | private void setDefinitionSource( |
| 1323 | | final DefinitionSource definitionSource ) { |
| 1324 | | assert definitionSource != null; |
| 1325 | | mDefinitionSource = definitionSource; |
| 1326 | | } |
| 1327 | | |
| 1328 | | private DefinitionSource getDefinitionSource() { |
| 1329 | | return mDefinitionSource; |
| 1330 | | } |
| 1331 | | |
| 1332 | | private DefinitionPane getDefinitionPane() { |
| 1333 | | return mDefinitionPane; |
| 1334 | | } |
| 1335 | | |
| 1336 | | private Text getLineNumberText() { |
| 1337 | | return mLineNumberText; |
| 1338 | | } |
| 1339 | | |
| 1340 | | private StatusBar getStatusBar() { |
| 1341 | | return mStatusBar; |
| 1342 | | } |
| 1343 | | |
| 1344 | | private TextField getFindTextField() { |
| 1345 | | return mFindTextField; |
| 1346 | | } |
| 1347 | | |
| 1348 | | private DefinitionNameInjector getVariableNameInjector() { |
| 1349 | | return mVariableNameInjector; |
| 1350 | | } |
| 1351 | | |
| 1352 | | /** |
| 1353 | | * Returns the variable map of interpolated definitions. |
| 1354 | | * |
| 1355 | | * @return A map to help dereference variables. |
| 1356 | | */ |
| 1357 | | private Map<String, String> getResolvedMap() { |
| 1358 | | return mResolvedMap; |
| 1359 | | } |
| 1360 | | |
| 1361 | | //---- Persistence accessors ---------------------------------------------- |
| 1362 | | |
| 1363 | | private UserPreferences getUserPreferences() { |
| 1364 | | return sOptions.getUserPreferences(); |
| 1365 | | } |
| 1366 | | |
| 1367 | | private Path getDefinitionPath() { |
| 1368 | | return getUserPreferences().getDefinitionPath(); |
| 1369 | | } |
| 1370 | | |
| 1371 | | //---- Spelling ----------------------------------------------------------- |
| 1372 | | |
| 1373 | | /** |
| 1374 | | * Delegates to {@link #spellcheck(StyleClassedTextArea, String, int)}. |
| 1375 | | * This is called to spell check the document, rather than a single paragraph. |
| 1376 | | * |
| 1377 | | * @param text The full document text. |
| 1378 | | */ |
| 1379 | | private void spellcheck( |
| 1380 | | final StyleClassedTextArea editor, final String text ) { |
| 1381 | | spellcheck( editor, text, -1 ); |
| 1382 | | } |
| 1383 | | |
| 1384 | | /** |
| 1385 | | * Spellchecks a subset of the entire document. |
| 1386 | | * |
| 1387 | | * @param text Look up words for this text in the lexicon. |
| 1388 | | * @param paraId Set to -1 to apply resulting style spans to the entire |
| 1389 | | * text. |
| 1390 | | */ |
| 1391 | | private void spellcheck( |
| 1392 | | final StyleClassedTextArea editor, final String text, final int paraId ) { |
| 1393 | | final var builder = new StyleSpansBuilder<Collection<String>>(); |
| 1394 | | final var runningIndex = new AtomicInteger( 0 ); |
| 1395 | | final var checker = getSpellChecker(); |
| 1396 | | |
| 1397 | | // The text nodes must be relayed through a contextual "visitor" that |
| 1398 | | // can return text in chunks with correlative offsets into the string. |
| 1399 | | // This allows Markdown, R Markdown, XML, and R XML documents to return |
| 1400 | | // sets of words to check. |
| 1401 | | |
| 1402 | | final var node = mParser.parse( text ); |
| 1403 | | final var visitor = new TextVisitor( ( visited, bIndex, eIndex ) -> { |
| 1404 | | // Treat hyphenated compound words as individual words. |
| 1405 | | final var check = visited.replace( '-', ' ' ); |
| 1406 | | |
| 1407 | | checker.proofread( check, ( misspelled, prevIndex, currIndex ) -> { |
| 1408 | | prevIndex += bIndex; |
| 1409 | | currIndex += bIndex; |
| 1410 | | |
| 1411 | | // Clear styling between lexiconically absent words. |
| 1412 | | builder.add( emptyList(), prevIndex - runningIndex.get() ); |
| 1413 | | builder.add( singleton( "spelling" ), currIndex - prevIndex ); |
| 1414 | | runningIndex.set( currIndex ); |
| 1415 | | } ); |
| 1416 | | } ); |
| 1417 | | |
| 1418 | | visitor.visit( node ); |
| 1419 | | |
| 1420 | | // If the running index was set, at least one word triggered the listener. |
| 1421 | | if( runningIndex.get() > 0 ) { |
| 1422 | | // Clear styling after the last lexiconically absent word. |
| 1423 | | builder.add( emptyList(), text.length() - runningIndex.get() ); |
| 1424 | | |
| 1425 | | final var spans = builder.create(); |
| 1426 | | |
| 1427 | | if( paraId >= 0 ) { |
| 1428 | | editor.setStyleSpans( paraId, 0, spans ); |
| 1429 | | } |
| 1430 | | else { |
| 1431 | | editor.setStyleSpans( 0, spans ); |
| 1432 | | } |
| 1433 | | } |
| 1434 | | } |
| 1435 | | |
| 1436 | | @SuppressWarnings("SameParameterValue") |
| 1437 | | private Collection<String> readLexicon( final String filename ) |
| 1438 | | throws Exception { |
| 1439 | | final var path = Paths.get( LEXICONS_DIRECTORY, filename ).toString(); |
| 1440 | | final var classLoader = MainWindow.class.getClassLoader(); |
| 1441 | | |
| 1442 | | try( final var resource = classLoader.getResourceAsStream( path ) ) { |
| 1443 | | assert resource != null; |
| 1444 | | |
| 1445 | | return new BufferedReader( new InputStreamReader( resource, UTF_8 ) ) |
| 1446 | | .lines() |
| 1447 | | .collect( Collectors.toList() ); |
| 90 | import java.io.FileNotFoundException; |
| 91 | import java.io.InputStreamReader; |
| 92 | import java.nio.file.Path; |
| 93 | import java.util.*; |
| 94 | import java.util.concurrent.atomic.AtomicInteger; |
| 95 | import java.util.function.Consumer; |
| 96 | import java.util.function.Function; |
| 97 | import java.util.prefs.Preferences; |
| 98 | import java.util.stream.Collectors; |
| 99 | |
| 100 | import static com.scrivenvar.Constants.*; |
| 101 | import static com.scrivenvar.Messages.get; |
| 102 | import static com.scrivenvar.StatusBarNotifier.alert; |
| 103 | import static com.scrivenvar.util.StageState.*; |
| 104 | import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| 105 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 106 | import static java.util.Collections.emptyList; |
| 107 | import static java.util.Collections.singleton; |
| 108 | import static javafx.application.Platform.runLater; |
| 109 | import static javafx.event.Event.fireEvent; |
| 110 | import static javafx.scene.input.KeyCode.ENTER; |
| 111 | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 112 | import static org.fxmisc.richtext.model.TwoDimensional.Bias.Forward; |
| 113 | |
| 114 | /** |
| 115 | * Main window containing a tab pane in the center for file editors. |
| 116 | */ |
| 117 | public class MainWindow implements Observer { |
| 118 | /** |
| 119 | * The {@code OPTIONS} variable must be declared before all other variables |
| 120 | * to prevent subsequent initializations from failing due to missing user |
| 121 | * preferences. |
| 122 | */ |
| 123 | private static final Options sOptions = Services.load( Options.class ); |
| 124 | private static final Snitch SNITCH = Services.load( Snitch.class ); |
| 125 | |
| 126 | private final Scene mScene; |
| 127 | private final StatusBar mStatusBar; |
| 128 | private final Text mLineNumberText; |
| 129 | private final TextField mFindTextField; |
| 130 | private final SpellChecker mSpellChecker; |
| 131 | |
| 132 | private final Object mMutex = new Object(); |
| 133 | |
| 134 | /** |
| 135 | * Prevents re-instantiation of processing classes. |
| 136 | */ |
| 137 | private final Map<FileEditorTab, Processor<String>> mProcessors = |
| 138 | new HashMap<>(); |
| 139 | |
| 140 | private final Map<String, String> mResolvedMap = |
| 141 | new HashMap<>( DEFAULT_MAP_SIZE ); |
| 142 | |
| 143 | private final EventHandler<PreferencesFxEvent> mRPreferencesListener = |
| 144 | event -> rerender(); |
| 145 | |
| 146 | /** |
| 147 | * Called when the definition data is changed. |
| 148 | */ |
| 149 | private final EventHandler<TreeItem.TreeModificationEvent<Event>> |
| 150 | mTreeHandler = event -> { |
| 151 | exportDefinitions( getDefinitionPath() ); |
| 152 | interpolateResolvedMap(); |
| 153 | rerender(); |
| 154 | }; |
| 155 | |
| 156 | /** |
| 157 | * Called to inject the selected item when the user presses ENTER in the |
| 158 | * definition pane. |
| 159 | */ |
| 160 | private final EventHandler<? super KeyEvent> mDefinitionKeyHandler = |
| 161 | event -> { |
| 162 | if( event.getCode() == ENTER ) { |
| 163 | getDefinitionNameInjector().injectSelectedItem(); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | private final ChangeListener<Integer> mCaretPositionListener = |
| 168 | ( observable, oldPosition, newPosition ) -> { |
| 169 | final FileEditorTab tab = getActiveFileEditorTab(); |
| 170 | final EditorPane pane = tab.getEditorPane(); |
| 171 | final StyleClassedTextArea editor = pane.getEditor(); |
| 172 | |
| 173 | getLineNumberText().setText( |
| 174 | get( STATUS_BAR_LINE, |
| 175 | editor.getCurrentParagraph() + 1, |
| 176 | editor.getParagraphs().size(), |
| 177 | editor.getCaretPosition() |
| 178 | ) |
| 179 | ); |
| 180 | }; |
| 181 | |
| 182 | private final ChangeListener<Integer> mCaretParagraphListener = |
| 183 | ( observable, oldIndex, newIndex ) -> |
| 184 | scrollToParagraph( newIndex, true ); |
| 185 | |
| 186 | private DefinitionSource mDefinitionSource = createDefaultDefinitionSource(); |
| 187 | private final DefinitionPane mDefinitionPane = createDefinitionPane(); |
| 188 | private final HTMLPreviewPane mPreviewPane = createHTMLPreviewPane(); |
| 189 | private final FileEditorTabPane mFileEditorPane = new FileEditorTabPane( |
| 190 | mCaretPositionListener, |
| 191 | mCaretParagraphListener ); |
| 192 | |
| 193 | /** |
| 194 | * Listens on the definition pane for double-click events. |
| 195 | */ |
| 196 | private final DefinitionNameInjector mDefinitionNameInjector |
| 197 | = new DefinitionNameInjector( mDefinitionPane ); |
| 198 | |
| 199 | public MainWindow() { |
| 200 | mStatusBar = createStatusBar(); |
| 201 | mLineNumberText = createLineNumberText(); |
| 202 | mFindTextField = createFindTextField(); |
| 203 | mScene = createScene(); |
| 204 | mSpellChecker = createSpellChecker(); |
| 205 | |
| 206 | // Add the close request listener before the window is shown. |
| 207 | initLayout(); |
| 208 | StatusBarNotifier.setStatusBar( mStatusBar ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Called after the stage is shown. |
| 213 | */ |
| 214 | public void init() { |
| 215 | initFindInput(); |
| 216 | initSnitch(); |
| 217 | initDefinitionListener(); |
| 218 | initTabAddedListener(); |
| 219 | initTabChangedListener(); |
| 220 | initPreferences(); |
| 221 | initVariableNameInjector(); |
| 222 | } |
| 223 | |
| 224 | private void initLayout() { |
| 225 | final var scene = getScene(); |
| 226 | |
| 227 | scene.getStylesheets().add( STYLESHEET_SCENE ); |
| 228 | scene.windowProperty().addListener( |
| 229 | ( unused, oldWindow, newWindow ) -> |
| 230 | newWindow.setOnCloseRequest( |
| 231 | e -> { |
| 232 | if( !getFileEditorPane().closeAllEditors() ) { |
| 233 | e.consume(); |
| 234 | } |
| 235 | } |
| 236 | ) |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Initialize the find input text field to listen on F3, ENTER, and |
| 242 | * ESCAPE key presses. |
| 243 | */ |
| 244 | private void initFindInput() { |
| 245 | final TextField input = getFindTextField(); |
| 246 | |
| 247 | input.setOnKeyPressed( ( KeyEvent event ) -> { |
| 248 | switch( event.getCode() ) { |
| 249 | case F3: |
| 250 | case ENTER: |
| 251 | editFindNext(); |
| 252 | break; |
| 253 | case F: |
| 254 | if( !event.isControlDown() ) { |
| 255 | break; |
| 256 | } |
| 257 | case ESCAPE: |
| 258 | getStatusBar().setGraphic( null ); |
| 259 | getActiveFileEditorTab().getEditorPane().requestFocus(); |
| 260 | break; |
| 261 | } |
| 262 | } ); |
| 263 | |
| 264 | // Remove when the input field loses focus. |
| 265 | input.focusedProperty().addListener( |
| 266 | ( focused, oldFocus, newFocus ) -> { |
| 267 | if( !newFocus ) { |
| 268 | getStatusBar().setGraphic( null ); |
| 269 | } |
| 270 | } |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Watch for changes to external files. In particular, this awaits |
| 276 | * modifications to any XSL files associated with XML files being edited. |
| 277 | * When |
| 278 | * an XSL file is modified (external to the application), the snitch's ears |
| 279 | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 280 | * date with what's on the file system. |
| 281 | */ |
| 282 | private void initSnitch() { |
| 283 | SNITCH.addObserver( this ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Listen for {@link FileEditorTabPane} to receive open definition file |
| 288 | * event. |
| 289 | */ |
| 290 | private void initDefinitionListener() { |
| 291 | getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| 292 | ( final ObservableValue<? extends Path> file, |
| 293 | final Path oldPath, final Path newPath ) -> { |
| 294 | openDefinitions( newPath ); |
| 295 | rerender(); |
| 296 | } |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Re-instantiates all processors then re-renders the active tab. This |
| 302 | * will refresh the resolved map, force R to re-initialize, and brute-force |
| 303 | * XSLT file reloads. |
| 304 | */ |
| 305 | private void rerender() { |
| 306 | runLater( |
| 307 | () -> { |
| 308 | resetProcessors(); |
| 309 | renderActiveTab(); |
| 310 | } |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * When tabs are added, hook the various change listeners onto the new |
| 316 | * tab sothat the preview pane refreshes as necessary. |
| 317 | */ |
| 318 | private void initTabAddedListener() { |
| 319 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 320 | |
| 321 | // Make sure the text processor kicks off when new files are opened. |
| 322 | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 323 | |
| 324 | // Update the preview pane on tab changes. |
| 325 | tabs.addListener( |
| 326 | ( final Change<? extends Tab> change ) -> { |
| 327 | while( change.next() ) { |
| 328 | if( change.wasAdded() ) { |
| 329 | // Multiple tabs can be added simultaneously. |
| 330 | for( final Tab newTab : change.getAddedSubList() ) { |
| 331 | final FileEditorTab tab = (FileEditorTab) newTab; |
| 332 | |
| 333 | initTextChangeListener( tab ); |
| 334 | initScrollEventListener( tab ); |
| 335 | initSpellCheckListener( tab ); |
| 336 | // initSyntaxListener( tab ); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | private void initTextChangeListener( final FileEditorTab tab ) { |
| 345 | tab.addTextChangeListener( |
| 346 | ( __, ov, nv ) -> { |
| 347 | process( tab ); |
| 348 | scrollToParagraph( getCurrentParagraphIndex() ); |
| 349 | } |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | private void initScrollEventListener( final FileEditorTab tab ) { |
| 354 | final var scrollPane = tab.getScrollPane(); |
| 355 | final var scrollBar = getPreviewPane().getVerticalScrollBar(); |
| 356 | |
| 357 | addShowListener( scrollPane, ( __ ) -> { |
| 358 | final var handler = new ScrollEventHandler( scrollPane, scrollBar ); |
| 359 | handler.enabledProperty().bind( tab.selectedProperty() ); |
| 360 | } ); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Listen for changes to the any particular paragraph and perform a quick |
| 365 | * spell check upon it. The style classes in the editor will be changed to |
| 366 | * mark any spelling mistakes in the paragraph. The user may then interact |
| 367 | * with any misspelled word (i.e., any piece of text that is marked) to |
| 368 | * revise the spelling. |
| 369 | * |
| 370 | * @param tab The tab to spellcheck. |
| 371 | */ |
| 372 | private void initSpellCheckListener( final FileEditorTab tab ) { |
| 373 | final var editor = tab.getEditorPane().getEditor(); |
| 374 | |
| 375 | // When the editor first appears, run a full spell check. This allows |
| 376 | // spell checking while typing to be restricted to the active paragraph, |
| 377 | // which is usually substantially smaller than the whole document. |
| 378 | addShowListener( |
| 379 | editor, ( __ ) -> spellcheck( editor, editor.getText() ) |
| 380 | ); |
| 381 | |
| 382 | // Use the plain text changes so that notifications of style changes |
| 383 | // are suppressed. Checking against the identity ensures that only |
| 384 | // new text additions or deletions trigger proofreading. |
| 385 | editor.plainTextChanges() |
| 386 | .filter( p -> !p.isIdentity() ).subscribe( change -> { |
| 387 | |
| 388 | // Only perform a spell check on the current paragraph. The |
| 389 | // entire document is processed once, when opened. |
| 390 | final var offset = change.getPosition(); |
| 391 | final var position = editor.offsetToPosition( offset, Forward ); |
| 392 | final var paraId = position.getMajor(); |
| 393 | final var paragraph = editor.getParagraph( paraId ); |
| 394 | final var text = paragraph.getText(); |
| 395 | |
| 396 | // Ensure that styles aren't doubled-up. |
| 397 | editor.clearStyle( paraId ); |
| 398 | |
| 399 | spellcheck( editor, text, paraId ); |
| 400 | } ); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Listen for new tab selection events. |
| 405 | */ |
| 406 | private void initTabChangedListener() { |
| 407 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 408 | |
| 409 | // Update the preview pane changing tabs. |
| 410 | editorPane.addTabSelectionListener( |
| 411 | ( tabPane, oldTab, newTab ) -> { |
| 412 | if( newTab == null ) { |
| 413 | // Clear the preview pane when closing an editor. When the last |
| 414 | // tab is closed, this ensures that the preview pane is empty. |
| 415 | getPreviewPane().clear(); |
| 416 | } |
| 417 | else { |
| 418 | final var tab = (FileEditorTab) newTab; |
| 419 | updateVariableNameInjector( tab ); |
| 420 | process( tab ); |
| 421 | } |
| 422 | } |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Reloads the preferences from the previous session. |
| 428 | */ |
| 429 | private void initPreferences() { |
| 430 | initDefinitionPane(); |
| 431 | getFileEditorPane().initPreferences(); |
| 432 | getUserPreferences().addSaveEventHandler( mRPreferencesListener ); |
| 433 | } |
| 434 | |
| 435 | private void initVariableNameInjector() { |
| 436 | updateVariableNameInjector( getActiveFileEditorTab() ); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Calls the listener when the given node is shown for the first time. The |
| 441 | * visible property is not the same as the initial showing event; visibility |
| 442 | * can be triggered numerous times (such as going off screen). |
| 443 | * <p> |
| 444 | * This is called, for example, before the drag handler can be attached, |
| 445 | * because the scrollbar for the text editor pane must be visible. |
| 446 | * </p> |
| 447 | * |
| 448 | * @param node The node to watch for showing. |
| 449 | * @param consumer The consumer to invoke when the event fires. |
| 450 | */ |
| 451 | private void addShowListener( |
| 452 | final Node node, final Consumer<Void> consumer ) { |
| 453 | final ChangeListener<? super Boolean> listener = ( o, oldShow, newShow ) -> |
| 454 | runLater( () -> { |
| 455 | if( newShow != null && newShow ) { |
| 456 | try { |
| 457 | consumer.accept( null ); |
| 458 | } catch( final Exception ex ) { |
| 459 | alert( ex ); |
| 460 | } |
| 461 | } |
| 462 | } ); |
| 463 | |
| 464 | Val.flatMap( node.sceneProperty(), Scene::windowProperty ) |
| 465 | .flatMap( Window::showingProperty ) |
| 466 | .addListener( listener ); |
| 467 | } |
| 468 | |
| 469 | private void scrollToParagraph( final int id ) { |
| 470 | scrollToParagraph( id, false ); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * @param id The paragraph to scroll to, will be approximated if it doesn't |
| 475 | * exist. |
| 476 | * @param force {@code true} means to force scrolling immediately, which |
| 477 | * should only be attempted when it is known that the document |
| 478 | * has been fully rendered. Otherwise the internal map of ID |
| 479 | * attributes will be incomplete and scrolling will flounder. |
| 480 | */ |
| 481 | private void scrollToParagraph( final int id, final boolean force ) { |
| 482 | synchronized( mMutex ) { |
| 483 | final var previewPane = getPreviewPane(); |
| 484 | final var scrollPane = previewPane.getScrollPane(); |
| 485 | final int approxId = getActiveEditorPane().approximateParagraphId( id ); |
| 486 | |
| 487 | if( force ) { |
| 488 | previewPane.scrollTo( approxId ); |
| 489 | } |
| 490 | else { |
| 491 | previewPane.tryScrollTo( approxId ); |
| 492 | } |
| 493 | |
| 494 | scrollPane.repaint(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | private void updateVariableNameInjector( final FileEditorTab tab ) { |
| 499 | getDefinitionNameInjector().addListener( tab ); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Called whenever the preview pane becomes out of sync with the file editor |
| 504 | * tab. This can be called when the text changes, the caret paragraph |
| 505 | * changes, or the file tab changes. |
| 506 | * |
| 507 | * @param tab The file editor tab that has been changed in some fashion. |
| 508 | */ |
| 509 | private void process( final FileEditorTab tab ) { |
| 510 | if( tab != null ) { |
| 511 | getPreviewPane().setPath( tab.getPath() ); |
| 512 | |
| 513 | final Processor<String> processor = getProcessors().computeIfAbsent( |
| 514 | tab, p -> createProcessors( tab ) |
| 515 | ); |
| 516 | |
| 517 | try { |
| 518 | processChain( processor, tab.getEditorText() ); |
| 519 | } catch( final Exception ex ) { |
| 520 | alert( ex ); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Executes the processing chain, operating on the given string. |
| 527 | * |
| 528 | * @param handler The first processor in the chain to call. |
| 529 | * @param text The initial value of the text to process. |
| 530 | * @return The final value of the text that was processed by the chain. |
| 531 | */ |
| 532 | private String processChain( Processor<String> handler, String text ) { |
| 533 | while( handler != null && text != null ) { |
| 534 | text = handler.apply( text ); |
| 535 | handler = handler.next(); |
| 536 | } |
| 537 | |
| 538 | return text; |
| 539 | } |
| 540 | |
| 541 | private void renderActiveTab() { |
| 542 | process( getActiveFileEditorTab() ); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Called when a definition source is opened. |
| 547 | * |
| 548 | * @param path Path to the definition source that was opened. |
| 549 | */ |
| 550 | private void openDefinitions( final Path path ) { |
| 551 | try { |
| 552 | final var ds = createDefinitionSource( path ); |
| 553 | setDefinitionSource( ds ); |
| 554 | |
| 555 | final var prefs = getUserPreferences(); |
| 556 | prefs.definitionPathProperty().setValue( path.toFile() ); |
| 557 | prefs.save(); |
| 558 | |
| 559 | final var tooltipPath = new Tooltip( path.toString() ); |
| 560 | tooltipPath.setShowDelay( Duration.millis( 200 ) ); |
| 561 | |
| 562 | final var pane = getDefinitionPane(); |
| 563 | pane.update( ds ); |
| 564 | pane.addTreeChangeHandler( mTreeHandler ); |
| 565 | pane.addKeyEventHandler( mDefinitionKeyHandler ); |
| 566 | pane.filenameProperty().setValue( path.getFileName().toString() ); |
| 567 | pane.setTooltip( tooltipPath ); |
| 568 | |
| 569 | interpolateResolvedMap(); |
| 570 | } catch( final Exception ex ) { |
| 571 | alert( ex ); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | private void exportDefinitions( final Path path ) { |
| 576 | try { |
| 577 | final var pane = getDefinitionPane(); |
| 578 | final var root = pane.getTreeView().getRoot(); |
| 579 | final var problemChild = pane.isTreeWellFormed(); |
| 580 | |
| 581 | if( problemChild == null ) { |
| 582 | getDefinitionSource().getTreeAdapter().export( root, path ); |
| 583 | } |
| 584 | else { |
| 585 | alert( "yaml.error.tree.form", problemChild.getValue() ); |
| 586 | } |
| 587 | } catch( final Exception ex ) { |
| 588 | alert( ex ); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | private void interpolateResolvedMap() { |
| 593 | final var treeMap = getDefinitionPane().toMap(); |
| 594 | final var map = new HashMap<>( treeMap ); |
| 595 | MapInterpolator.interpolate( map ); |
| 596 | |
| 597 | getResolvedMap().clear(); |
| 598 | getResolvedMap().putAll( map ); |
| 599 | } |
| 600 | |
| 601 | private void initDefinitionPane() { |
| 602 | openDefinitions( getDefinitionPath() ); |
| 603 | } |
| 604 | |
| 605 | //---- File actions ------------------------------------------------------- |
| 606 | |
| 607 | /** |
| 608 | * Called when an {@link Observable} instance has changed. This is called |
| 609 | * by both the {@link Snitch} service and the notify service. The @link |
| 610 | * Snitch} service can be called for different file types, including |
| 611 | * {@link DefinitionSource} instances. |
| 612 | * |
| 613 | * @param observable The observed instance. |
| 614 | * @param value The noteworthy item. |
| 615 | */ |
| 616 | @Override |
| 617 | public void update( final Observable observable, final Object value ) { |
| 618 | if( value instanceof Path && observable instanceof Snitch ) { |
| 619 | updateSelectedTab(); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Called when a file has been modified. |
| 625 | */ |
| 626 | private void updateSelectedTab() { |
| 627 | rerender(); |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * After resetting the processors, they will refresh anew to be up-to-date |
| 632 | * with the files (text and definition) currently loaded into the editor. |
| 633 | */ |
| 634 | private void resetProcessors() { |
| 635 | getProcessors().clear(); |
| 636 | } |
| 637 | |
| 638 | //---- File actions ------------------------------------------------------- |
| 639 | |
| 640 | private void fileNew() { |
| 641 | getFileEditorPane().newEditor(); |
| 642 | } |
| 643 | |
| 644 | private void fileOpen() { |
| 645 | getFileEditorPane().openFileDialog(); |
| 646 | } |
| 647 | |
| 648 | private void fileClose() { |
| 649 | getFileEditorPane().closeEditor( getActiveFileEditorTab(), true ); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * TODO: Upon closing, first remove the tab change listeners. (There's no |
| 654 | * need to re-render each tab when all are being closed.) |
| 655 | */ |
| 656 | private void fileCloseAll() { |
| 657 | getFileEditorPane().closeAllEditors(); |
| 658 | } |
| 659 | |
| 660 | private void fileSave() { |
| 661 | getFileEditorPane().saveEditor( getActiveFileEditorTab() ); |
| 662 | } |
| 663 | |
| 664 | private void fileSaveAs() { |
| 665 | final FileEditorTab editor = getActiveFileEditorTab(); |
| 666 | getFileEditorPane().saveEditorAs( editor ); |
| 667 | getProcessors().remove( editor ); |
| 668 | |
| 669 | try { |
| 670 | process( editor ); |
| 671 | } catch( final Exception ex ) { |
| 672 | alert( ex ); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | private void fileSaveAll() { |
| 677 | getFileEditorPane().saveAllEditors(); |
| 678 | } |
| 679 | |
| 680 | private void fileExit() { |
| 681 | final Window window = getWindow(); |
| 682 | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 683 | } |
| 684 | |
| 685 | //---- Edit actions ------------------------------------------------------- |
| 686 | |
| 687 | /** |
| 688 | * Transform the Markdown into HTML then copy that HTML into the copy |
| 689 | * buffer. |
| 690 | */ |
| 691 | private void copyHtml() { |
| 692 | final var markdown = getActiveEditorPane().getText(); |
| 693 | final var processors = createProcessorFactory().createProcessors( |
| 694 | getActiveFileEditorTab() |
| 695 | ); |
| 696 | |
| 697 | final var chain = processors.remove( HtmlPreviewProcessor.class ); |
| 698 | |
| 699 | final String html = processChain( chain, markdown ); |
| 700 | |
| 701 | final Clipboard clipboard = Clipboard.getSystemClipboard(); |
| 702 | final ClipboardContent content = new ClipboardContent(); |
| 703 | content.putString( html ); |
| 704 | clipboard.setContent( content ); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Used to find text in the active file editor window. |
| 709 | */ |
| 710 | private void editFind() { |
| 711 | final TextField input = getFindTextField(); |
| 712 | getStatusBar().setGraphic( input ); |
| 713 | input.requestFocus(); |
| 714 | } |
| 715 | |
| 716 | public void editFindNext() { |
| 717 | getActiveFileEditorTab().searchNext( getFindTextField().getText() ); |
| 718 | } |
| 719 | |
| 720 | public void editPreferences() { |
| 721 | getUserPreferences().show(); |
| 722 | } |
| 723 | |
| 724 | //---- Insert actions ----------------------------------------------------- |
| 725 | |
| 726 | /** |
| 727 | * Delegates to the active editor to handle wrapping the current text |
| 728 | * selection with leading and trailing strings. |
| 729 | * |
| 730 | * @param leading The string to put before the selection. |
| 731 | * @param trailing The string to put after the selection. |
| 732 | */ |
| 733 | private void insertMarkdown( |
| 734 | final String leading, final String trailing ) { |
| 735 | getActiveEditorPane().surroundSelection( leading, trailing ); |
| 736 | } |
| 737 | |
| 738 | private void insertMarkdown( |
| 739 | final String leading, final String trailing, final String hint ) { |
| 740 | getActiveEditorPane().surroundSelection( leading, trailing, hint ); |
| 741 | } |
| 742 | |
| 743 | //---- View actions ------------------------------------------------------- |
| 744 | |
| 745 | private void viewRefresh() { |
| 746 | rerender(); |
| 747 | } |
| 748 | |
| 749 | //---- Help actions ------------------------------------------------------- |
| 750 | |
| 751 | private void helpAbout() { |
| 752 | final Alert alert = new Alert( AlertType.INFORMATION ); |
| 753 | alert.setTitle( get( "Dialog.about.title" ) ); |
| 754 | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 755 | alert.setContentText( get( "Dialog.about.content" ) ); |
| 756 | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 757 | alert.initOwner( getWindow() ); |
| 758 | |
| 759 | alert.showAndWait(); |
| 760 | } |
| 761 | |
| 762 | //---- Member creators ---------------------------------------------------- |
| 763 | |
| 764 | private SpellChecker createSpellChecker() { |
| 765 | try { |
| 766 | final Collection<String> lexicon = readLexicon( "en.txt" ); |
| 767 | return SymSpellSpeller.forLexicon( lexicon ); |
| 768 | } catch( final Exception ex ) { |
| 769 | alert( ex ); |
| 770 | return new PermissiveSpeller(); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Factory to create processors that are suited to different file types. |
| 776 | * |
| 777 | * @param tab The tab that is subjected to processing. |
| 778 | * @return A processor suited to the file type specified by the tab's path. |
| 779 | */ |
| 780 | private Processor<String> createProcessors( final FileEditorTab tab ) { |
| 781 | return createProcessorFactory().createProcessors( tab ); |
| 782 | } |
| 783 | |
| 784 | private ProcessorFactory createProcessorFactory() { |
| 785 | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 786 | } |
| 787 | |
| 788 | private DefinitionPane createDefinitionPane() { |
| 789 | return new DefinitionPane(); |
| 790 | } |
| 791 | |
| 792 | private HTMLPreviewPane createHTMLPreviewPane() { |
| 793 | return new HTMLPreviewPane(); |
| 794 | } |
| 795 | |
| 796 | private DefinitionSource createDefaultDefinitionSource() { |
| 797 | return new YamlDefinitionSource( getDefinitionPath() ); |
| 798 | } |
| 799 | |
| 800 | private DefinitionSource createDefinitionSource( final Path path ) { |
| 801 | try { |
| 802 | return createDefinitionFactory().createDefinitionSource( path ); |
| 803 | } catch( final Exception ex ) { |
| 804 | alert( ex ); |
| 805 | return createDefaultDefinitionSource(); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | private TextField createFindTextField() { |
| 810 | return new TextField(); |
| 811 | } |
| 812 | |
| 813 | private DefinitionFactory createDefinitionFactory() { |
| 814 | return new DefinitionFactory(); |
| 815 | } |
| 816 | |
| 817 | private StatusBar createStatusBar() { |
| 818 | return new StatusBar(); |
| 819 | } |
| 820 | |
| 821 | private Scene createScene() { |
| 822 | final SplitPane splitPane = new SplitPane( |
| 823 | getDefinitionPane(), |
| 824 | getFileEditorPane(), |
| 825 | getPreviewPane() ); |
| 826 | |
| 827 | splitPane.setDividerPositions( |
| 828 | getFloat( K_PANE_SPLIT_DEFINITION, .22f ), |
| 829 | getFloat( K_PANE_SPLIT_EDITOR, .60f ), |
| 830 | getFloat( K_PANE_SPLIT_PREVIEW, .18f ) ); |
| 831 | |
| 832 | getDefinitionPane().prefHeightProperty() |
| 833 | .bind( splitPane.heightProperty() ); |
| 834 | |
| 835 | final BorderPane borderPane = new BorderPane(); |
| 836 | borderPane.setPrefSize( 1280, 800 ); |
| 837 | borderPane.setTop( createMenuBar() ); |
| 838 | borderPane.setBottom( getStatusBar() ); |
| 839 | borderPane.setCenter( splitPane ); |
| 840 | |
| 841 | final VBox statusBar = new VBox(); |
| 842 | statusBar.setAlignment( Pos.BASELINE_CENTER ); |
| 843 | statusBar.getChildren().add( getLineNumberText() ); |
| 844 | getStatusBar().getRightItems().add( statusBar ); |
| 845 | |
| 846 | // Force preview pane refresh on Windows. |
| 847 | if( SystemUtils.IS_OS_WINDOWS ) { |
| 848 | splitPane.getDividers().get( 1 ).positionProperty().addListener( |
| 849 | ( l, oValue, nValue ) -> runLater( |
| 850 | () -> getPreviewPane().getScrollPane().repaint() |
| 851 | ) |
| 852 | ); |
| 853 | } |
| 854 | |
| 855 | return new Scene( borderPane ); |
| 856 | } |
| 857 | |
| 858 | private Text createLineNumberText() { |
| 859 | return new Text( get( STATUS_BAR_LINE, 1, 1, 1 ) ); |
| 860 | } |
| 861 | |
| 862 | private Node createMenuBar() { |
| 863 | final BooleanBinding activeFileEditorIsNull = |
| 864 | getFileEditorPane().activeFileEditorProperty().isNull(); |
| 865 | |
| 866 | // File actions |
| 867 | final Action fileNewAction = new ActionBuilder() |
| 868 | .setText( "Main.menu.file.new" ) |
| 869 | .setAccelerator( "Shortcut+N" ) |
| 870 | .setIcon( FILE_ALT ) |
| 871 | .setAction( e -> fileNew() ) |
| 872 | .build(); |
| 873 | final Action fileOpenAction = new ActionBuilder() |
| 874 | .setText( "Main.menu.file.open" ) |
| 875 | .setAccelerator( "Shortcut+O" ) |
| 876 | .setIcon( FOLDER_OPEN_ALT ) |
| 877 | .setAction( e -> fileOpen() ) |
| 878 | .build(); |
| 879 | final Action fileCloseAction = new ActionBuilder() |
| 880 | .setText( "Main.menu.file.close" ) |
| 881 | .setAccelerator( "Shortcut+W" ) |
| 882 | .setAction( e -> fileClose() ) |
| 883 | .setDisable( activeFileEditorIsNull ) |
| 884 | .build(); |
| 885 | final Action fileCloseAllAction = new ActionBuilder() |
| 886 | .setText( "Main.menu.file.close_all" ) |
| 887 | .setAction( e -> fileCloseAll() ) |
| 888 | .setDisable( activeFileEditorIsNull ) |
| 889 | .build(); |
| 890 | final Action fileSaveAction = new ActionBuilder() |
| 891 | .setText( "Main.menu.file.save" ) |
| 892 | .setAccelerator( "Shortcut+S" ) |
| 893 | .setIcon( FLOPPY_ALT ) |
| 894 | .setAction( e -> fileSave() ) |
| 895 | .setDisable( createActiveBooleanProperty( |
| 896 | FileEditorTab::modifiedProperty ).not() ) |
| 897 | .build(); |
| 898 | final Action fileSaveAsAction = new ActionBuilder() |
| 899 | .setText( "Main.menu.file.save_as" ) |
| 900 | .setAction( e -> fileSaveAs() ) |
| 901 | .setDisable( activeFileEditorIsNull ) |
| 902 | .build(); |
| 903 | final Action fileSaveAllAction = new ActionBuilder() |
| 904 | .setText( "Main.menu.file.save_all" ) |
| 905 | .setAccelerator( "Shortcut+Shift+S" ) |
| 906 | .setAction( e -> fileSaveAll() ) |
| 907 | .setDisable( Bindings.not( |
| 908 | getFileEditorPane().anyFileEditorModifiedProperty() ) ) |
| 909 | .build(); |
| 910 | final Action fileExitAction = new ActionBuilder() |
| 911 | .setText( "Main.menu.file.exit" ) |
| 912 | .setAction( e -> fileExit() ) |
| 913 | .build(); |
| 914 | |
| 915 | // Edit actions |
| 916 | final Action editCopyHtmlAction = new ActionBuilder() |
| 917 | .setText( "Main.menu.edit.copy.html" ) |
| 918 | .setIcon( HTML5 ) |
| 919 | .setAction( e -> copyHtml() ) |
| 920 | .setDisable( activeFileEditorIsNull ) |
| 921 | .build(); |
| 922 | |
| 923 | final Action editUndoAction = new ActionBuilder() |
| 924 | .setText( "Main.menu.edit.undo" ) |
| 925 | .setAccelerator( "Shortcut+Z" ) |
| 926 | .setIcon( UNDO ) |
| 927 | .setAction( e -> getActiveEditorPane().undo() ) |
| 928 | .setDisable( createActiveBooleanProperty( |
| 929 | FileEditorTab::canUndoProperty ).not() ) |
| 930 | .build(); |
| 931 | final Action editRedoAction = new ActionBuilder() |
| 932 | .setText( "Main.menu.edit.redo" ) |
| 933 | .setAccelerator( "Shortcut+Y" ) |
| 934 | .setIcon( REPEAT ) |
| 935 | .setAction( e -> getActiveEditorPane().redo() ) |
| 936 | .setDisable( createActiveBooleanProperty( |
| 937 | FileEditorTab::canRedoProperty ).not() ) |
| 938 | .build(); |
| 939 | |
| 940 | final Action editCutAction = new ActionBuilder() |
| 941 | .setText( "Main.menu.edit.cut" ) |
| 942 | .setAccelerator( "Shortcut+X" ) |
| 943 | .setIcon( CUT ) |
| 944 | .setAction( e -> getActiveEditorPane().cut() ) |
| 945 | .setDisable( activeFileEditorIsNull ) |
| 946 | .build(); |
| 947 | final Action editCopyAction = new ActionBuilder() |
| 948 | .setText( "Main.menu.edit.copy" ) |
| 949 | .setAccelerator( "Shortcut+C" ) |
| 950 | .setIcon( COPY ) |
| 951 | .setAction( e -> getActiveEditorPane().copy() ) |
| 952 | .setDisable( activeFileEditorIsNull ) |
| 953 | .build(); |
| 954 | final Action editPasteAction = new ActionBuilder() |
| 955 | .setText( "Main.menu.edit.paste" ) |
| 956 | .setAccelerator( "Shortcut+V" ) |
| 957 | .setIcon( PASTE ) |
| 958 | .setAction( e -> getActiveEditorPane().paste() ) |
| 959 | .setDisable( activeFileEditorIsNull ) |
| 960 | .build(); |
| 961 | final Action editSelectAllAction = new ActionBuilder() |
| 962 | .setText( "Main.menu.edit.selectAll" ) |
| 963 | .setAccelerator( "Shortcut+A" ) |
| 964 | .setAction( e -> getActiveEditorPane().selectAll() ) |
| 965 | .setDisable( activeFileEditorIsNull ) |
| 966 | .build(); |
| 967 | |
| 968 | final Action editFindAction = new ActionBuilder() |
| 969 | .setText( "Main.menu.edit.find" ) |
| 970 | .setAccelerator( "Ctrl+F" ) |
| 971 | .setIcon( SEARCH ) |
| 972 | .setAction( e -> editFind() ) |
| 973 | .setDisable( activeFileEditorIsNull ) |
| 974 | .build(); |
| 975 | final Action editFindNextAction = new ActionBuilder() |
| 976 | .setText( "Main.menu.edit.find.next" ) |
| 977 | .setAccelerator( "F3" ) |
| 978 | .setIcon( null ) |
| 979 | .setAction( e -> editFindNext() ) |
| 980 | .setDisable( activeFileEditorIsNull ) |
| 981 | .build(); |
| 982 | final Action editPreferencesAction = new ActionBuilder() |
| 983 | .setText( "Main.menu.edit.preferences" ) |
| 984 | .setAccelerator( "Ctrl+Alt+S" ) |
| 985 | .setAction( e -> editPreferences() ) |
| 986 | .build(); |
| 987 | |
| 988 | // Format actions |
| 989 | final Action formatBoldAction = new ActionBuilder() |
| 990 | .setText( "Main.menu.format.bold" ) |
| 991 | .setAccelerator( "Shortcut+B" ) |
| 992 | .setIcon( BOLD ) |
| 993 | .setAction( e -> insertMarkdown( "**", "**" ) ) |
| 994 | .setDisable( activeFileEditorIsNull ) |
| 995 | .build(); |
| 996 | final Action formatItalicAction = new ActionBuilder() |
| 997 | .setText( "Main.menu.format.italic" ) |
| 998 | .setAccelerator( "Shortcut+I" ) |
| 999 | .setIcon( ITALIC ) |
| 1000 | .setAction( e -> insertMarkdown( "*", "*" ) ) |
| 1001 | .setDisable( activeFileEditorIsNull ) |
| 1002 | .build(); |
| 1003 | final Action formatSuperscriptAction = new ActionBuilder() |
| 1004 | .setText( "Main.menu.format.superscript" ) |
| 1005 | .setAccelerator( "Shortcut+[" ) |
| 1006 | .setIcon( SUPERSCRIPT ) |
| 1007 | .setAction( e -> insertMarkdown( "^", "^" ) ) |
| 1008 | .setDisable( activeFileEditorIsNull ) |
| 1009 | .build(); |
| 1010 | final Action formatSubscriptAction = new ActionBuilder() |
| 1011 | .setText( "Main.menu.format.subscript" ) |
| 1012 | .setAccelerator( "Shortcut+]" ) |
| 1013 | .setIcon( SUBSCRIPT ) |
| 1014 | .setAction( e -> insertMarkdown( "~", "~" ) ) |
| 1015 | .setDisable( activeFileEditorIsNull ) |
| 1016 | .build(); |
| 1017 | final Action formatStrikethroughAction = new ActionBuilder() |
| 1018 | .setText( "Main.menu.format.strikethrough" ) |
| 1019 | .setAccelerator( "Shortcut+T" ) |
| 1020 | .setIcon( STRIKETHROUGH ) |
| 1021 | .setAction( e -> insertMarkdown( "~~", "~~" ) ) |
| 1022 | .setDisable( activeFileEditorIsNull ) |
| 1023 | .build(); |
| 1024 | |
| 1025 | // Insert actions |
| 1026 | final Action insertBlockquoteAction = new ActionBuilder() |
| 1027 | .setText( "Main.menu.insert.blockquote" ) |
| 1028 | .setAccelerator( "Ctrl+Q" ) |
| 1029 | .setIcon( QUOTE_LEFT ) |
| 1030 | .setAction( e -> insertMarkdown( "\n\n> ", "" ) ) |
| 1031 | .setDisable( activeFileEditorIsNull ) |
| 1032 | .build(); |
| 1033 | final Action insertCodeAction = new ActionBuilder() |
| 1034 | .setText( "Main.menu.insert.code" ) |
| 1035 | .setAccelerator( "Shortcut+K" ) |
| 1036 | .setIcon( CODE ) |
| 1037 | .setAction( e -> insertMarkdown( "`", "`" ) ) |
| 1038 | .setDisable( activeFileEditorIsNull ) |
| 1039 | .build(); |
| 1040 | final Action insertFencedCodeBlockAction = new ActionBuilder() |
| 1041 | .setText( "Main.menu.insert.fenced_code_block" ) |
| 1042 | .setAccelerator( "Shortcut+Shift+K" ) |
| 1043 | .setIcon( FILE_CODE_ALT ) |
| 1044 | .setAction( e -> insertMarkdown( |
| 1045 | "\n\n```\n", |
| 1046 | "\n```\n\n", |
| 1047 | get( "Main.menu.insert.fenced_code_block.prompt" ) ) ) |
| 1048 | .setDisable( activeFileEditorIsNull ) |
| 1049 | .build(); |
| 1050 | final Action insertLinkAction = new ActionBuilder() |
| 1051 | .setText( "Main.menu.insert.link" ) |
| 1052 | .setAccelerator( "Shortcut+L" ) |
| 1053 | .setIcon( LINK ) |
| 1054 | .setAction( e -> getActiveEditorPane().insertLink() ) |
| 1055 | .setDisable( activeFileEditorIsNull ) |
| 1056 | .build(); |
| 1057 | final Action insertImageAction = new ActionBuilder() |
| 1058 | .setText( "Main.menu.insert.image" ) |
| 1059 | .setAccelerator( "Shortcut+G" ) |
| 1060 | .setIcon( PICTURE_ALT ) |
| 1061 | .setAction( e -> getActiveEditorPane().insertImage() ) |
| 1062 | .setDisable( activeFileEditorIsNull ) |
| 1063 | .build(); |
| 1064 | |
| 1065 | // Number of heading actions (H1 ... H3) |
| 1066 | final int HEADINGS = 3; |
| 1067 | final Action[] headings = new Action[ HEADINGS ]; |
| 1068 | |
| 1069 | for( int i = 1; i <= HEADINGS; i++ ) { |
| 1070 | final String hashes = new String( new char[ i ] ).replace( "\0", "#" ); |
| 1071 | final String markup = String.format( "%n%n%s ", hashes ); |
| 1072 | final String text = "Main.menu.insert.heading." + i; |
| 1073 | final String accelerator = "Shortcut+" + i; |
| 1074 | final String prompt = text + ".prompt"; |
| 1075 | |
| 1076 | headings[ i - 1 ] = new ActionBuilder() |
| 1077 | .setText( text ) |
| 1078 | .setAccelerator( accelerator ) |
| 1079 | .setIcon( HEADER ) |
| 1080 | .setAction( e -> insertMarkdown( markup, "", get( prompt ) ) ) |
| 1081 | .setDisable( activeFileEditorIsNull ) |
| 1082 | .build(); |
| 1083 | } |
| 1084 | |
| 1085 | final Action insertUnorderedListAction = new ActionBuilder() |
| 1086 | .setText( "Main.menu.insert.unordered_list" ) |
| 1087 | .setAccelerator( "Shortcut+U" ) |
| 1088 | .setIcon( LIST_UL ) |
| 1089 | .setAction( e -> insertMarkdown( "\n\n* ", "" ) ) |
| 1090 | .setDisable( activeFileEditorIsNull ) |
| 1091 | .build(); |
| 1092 | final Action insertOrderedListAction = new ActionBuilder() |
| 1093 | .setText( "Main.menu.insert.ordered_list" ) |
| 1094 | .setAccelerator( "Shortcut+Shift+O" ) |
| 1095 | .setIcon( LIST_OL ) |
| 1096 | .setAction( e -> insertMarkdown( |
| 1097 | "\n\n1. ", "" ) ) |
| 1098 | .setDisable( activeFileEditorIsNull ) |
| 1099 | .build(); |
| 1100 | final Action insertHorizontalRuleAction = new ActionBuilder() |
| 1101 | .setText( "Main.menu.insert.horizontal_rule" ) |
| 1102 | .setAccelerator( "Shortcut+H" ) |
| 1103 | .setAction( e -> insertMarkdown( |
| 1104 | "\n\n---\n\n", "" ) ) |
| 1105 | .setDisable( activeFileEditorIsNull ) |
| 1106 | .build(); |
| 1107 | |
| 1108 | // Definition actions |
| 1109 | final Action definitionCreateAction = new ActionBuilder() |
| 1110 | .setText( "Main.menu.definition.create" ) |
| 1111 | .setIcon( TREE ) |
| 1112 | .setAction( e -> getDefinitionPane().addItem() ) |
| 1113 | .build(); |
| 1114 | final Action definitionInsertAction = new ActionBuilder() |
| 1115 | .setText( "Main.menu.definition.insert" ) |
| 1116 | .setAccelerator( "Ctrl+Space" ) |
| 1117 | .setIcon( STAR ) |
| 1118 | .setAction( e -> definitionInsert() ) |
| 1119 | .build(); |
| 1120 | |
| 1121 | // Help actions |
| 1122 | final Action helpAboutAction = new ActionBuilder() |
| 1123 | .setText( "Main.menu.help.about" ) |
| 1124 | .setAction( e -> helpAbout() ) |
| 1125 | .build(); |
| 1126 | |
| 1127 | //---- MenuBar ---- |
| 1128 | |
| 1129 | // File Menu |
| 1130 | final var fileMenu = ActionUtils.createMenu( |
| 1131 | get( "Main.menu.file" ), |
| 1132 | fileNewAction, |
| 1133 | fileOpenAction, |
| 1134 | null, |
| 1135 | fileCloseAction, |
| 1136 | fileCloseAllAction, |
| 1137 | null, |
| 1138 | fileSaveAction, |
| 1139 | fileSaveAsAction, |
| 1140 | fileSaveAllAction, |
| 1141 | null, |
| 1142 | fileExitAction ); |
| 1143 | |
| 1144 | // Edit Menu |
| 1145 | final var editMenu = ActionUtils.createMenu( |
| 1146 | get( "Main.menu.edit" ), |
| 1147 | editCopyHtmlAction, |
| 1148 | null, |
| 1149 | editUndoAction, |
| 1150 | editRedoAction, |
| 1151 | null, |
| 1152 | editCutAction, |
| 1153 | editCopyAction, |
| 1154 | editPasteAction, |
| 1155 | editSelectAllAction, |
| 1156 | null, |
| 1157 | editFindAction, |
| 1158 | editFindNextAction, |
| 1159 | null, |
| 1160 | editPreferencesAction ); |
| 1161 | |
| 1162 | // Format Menu |
| 1163 | final var formatMenu = ActionUtils.createMenu( |
| 1164 | get( "Main.menu.format" ), |
| 1165 | formatBoldAction, |
| 1166 | formatItalicAction, |
| 1167 | formatSuperscriptAction, |
| 1168 | formatSubscriptAction, |
| 1169 | formatStrikethroughAction |
| 1170 | ); |
| 1171 | |
| 1172 | // Insert Menu |
| 1173 | final var insertMenu = ActionUtils.createMenu( |
| 1174 | get( "Main.menu.insert" ), |
| 1175 | insertBlockquoteAction, |
| 1176 | insertCodeAction, |
| 1177 | insertFencedCodeBlockAction, |
| 1178 | null, |
| 1179 | insertLinkAction, |
| 1180 | insertImageAction, |
| 1181 | null, |
| 1182 | headings[ 0 ], |
| 1183 | headings[ 1 ], |
| 1184 | headings[ 2 ], |
| 1185 | null, |
| 1186 | insertUnorderedListAction, |
| 1187 | insertOrderedListAction, |
| 1188 | insertHorizontalRuleAction |
| 1189 | ); |
| 1190 | |
| 1191 | // Definition Menu |
| 1192 | final var definitionMenu = ActionUtils.createMenu( |
| 1193 | get( "Main.menu.definition" ), |
| 1194 | definitionCreateAction, |
| 1195 | definitionInsertAction ); |
| 1196 | |
| 1197 | // Help Menu |
| 1198 | final var helpMenu = ActionUtils.createMenu( |
| 1199 | get( "Main.menu.help" ), |
| 1200 | helpAboutAction ); |
| 1201 | |
| 1202 | //---- MenuBar ---- |
| 1203 | final var menuBar = new MenuBar( |
| 1204 | fileMenu, |
| 1205 | editMenu, |
| 1206 | formatMenu, |
| 1207 | insertMenu, |
| 1208 | definitionMenu, |
| 1209 | helpMenu ); |
| 1210 | |
| 1211 | //---- ToolBar ---- |
| 1212 | final var toolBar = ActionUtils.createToolBar( |
| 1213 | fileNewAction, |
| 1214 | fileOpenAction, |
| 1215 | fileSaveAction, |
| 1216 | null, |
| 1217 | editUndoAction, |
| 1218 | editRedoAction, |
| 1219 | editCutAction, |
| 1220 | editCopyAction, |
| 1221 | editPasteAction, |
| 1222 | null, |
| 1223 | formatBoldAction, |
| 1224 | formatItalicAction, |
| 1225 | formatSuperscriptAction, |
| 1226 | formatSubscriptAction, |
| 1227 | insertBlockquoteAction, |
| 1228 | insertCodeAction, |
| 1229 | insertFencedCodeBlockAction, |
| 1230 | null, |
| 1231 | insertLinkAction, |
| 1232 | insertImageAction, |
| 1233 | null, |
| 1234 | headings[ 0 ], |
| 1235 | null, |
| 1236 | insertUnorderedListAction, |
| 1237 | insertOrderedListAction ); |
| 1238 | |
| 1239 | return new VBox( menuBar, toolBar ); |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Performs the autoinsert function on the active file editor. |
| 1244 | */ |
| 1245 | private void definitionInsert() { |
| 1246 | getDefinitionNameInjector().autoinsert(); |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * Creates a boolean property that is bound to another boolean value of the |
| 1251 | * active editor. |
| 1252 | */ |
| 1253 | private BooleanProperty createActiveBooleanProperty( |
| 1254 | final Function<FileEditorTab, ObservableBooleanValue> func ) { |
| 1255 | |
| 1256 | final BooleanProperty b = new SimpleBooleanProperty(); |
| 1257 | final FileEditorTab tab = getActiveFileEditorTab(); |
| 1258 | |
| 1259 | if( tab != null ) { |
| 1260 | b.bind( func.apply( tab ) ); |
| 1261 | } |
| 1262 | |
| 1263 | getFileEditorPane().activeFileEditorProperty().addListener( |
| 1264 | ( observable, oldFileEditor, newFileEditor ) -> { |
| 1265 | b.unbind(); |
| 1266 | |
| 1267 | if( newFileEditor == null ) { |
| 1268 | b.set( false ); |
| 1269 | } |
| 1270 | else { |
| 1271 | b.bind( func.apply( newFileEditor ) ); |
| 1272 | } |
| 1273 | } |
| 1274 | ); |
| 1275 | |
| 1276 | return b; |
| 1277 | } |
| 1278 | |
| 1279 | //---- Convenience accessors ---------------------------------------------- |
| 1280 | |
| 1281 | private Preferences getPreferences() { |
| 1282 | return sOptions.getState(); |
| 1283 | } |
| 1284 | |
| 1285 | private int getCurrentParagraphIndex() { |
| 1286 | return getActiveEditorPane().getCurrentParagraphIndex(); |
| 1287 | } |
| 1288 | |
| 1289 | private float getFloat( final String key, final float defaultValue ) { |
| 1290 | return getPreferences().getFloat( key, defaultValue ); |
| 1291 | } |
| 1292 | |
| 1293 | public Window getWindow() { |
| 1294 | return getScene().getWindow(); |
| 1295 | } |
| 1296 | |
| 1297 | private MarkdownEditorPane getActiveEditorPane() { |
| 1298 | return getActiveFileEditorTab().getEditorPane(); |
| 1299 | } |
| 1300 | |
| 1301 | private FileEditorTab getActiveFileEditorTab() { |
| 1302 | return getFileEditorPane().getActiveFileEditor(); |
| 1303 | } |
| 1304 | |
| 1305 | //---- Member accessors --------------------------------------------------- |
| 1306 | |
| 1307 | protected Scene getScene() { |
| 1308 | return mScene; |
| 1309 | } |
| 1310 | |
| 1311 | private SpellChecker getSpellChecker() { |
| 1312 | return mSpellChecker; |
| 1313 | } |
| 1314 | |
| 1315 | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 1316 | return mProcessors; |
| 1317 | } |
| 1318 | |
| 1319 | private FileEditorTabPane getFileEditorPane() { |
| 1320 | return mFileEditorPane; |
| 1321 | } |
| 1322 | |
| 1323 | private HTMLPreviewPane getPreviewPane() { |
| 1324 | return mPreviewPane; |
| 1325 | } |
| 1326 | |
| 1327 | private void setDefinitionSource( |
| 1328 | final DefinitionSource definitionSource ) { |
| 1329 | assert definitionSource != null; |
| 1330 | mDefinitionSource = definitionSource; |
| 1331 | } |
| 1332 | |
| 1333 | private DefinitionSource getDefinitionSource() { |
| 1334 | return mDefinitionSource; |
| 1335 | } |
| 1336 | |
| 1337 | private DefinitionPane getDefinitionPane() { |
| 1338 | return mDefinitionPane; |
| 1339 | } |
| 1340 | |
| 1341 | private Text getLineNumberText() { |
| 1342 | return mLineNumberText; |
| 1343 | } |
| 1344 | |
| 1345 | private StatusBar getStatusBar() { |
| 1346 | return mStatusBar; |
| 1347 | } |
| 1348 | |
| 1349 | private TextField getFindTextField() { |
| 1350 | return mFindTextField; |
| 1351 | } |
| 1352 | |
| 1353 | private DefinitionNameInjector getDefinitionNameInjector() { |
| 1354 | return mDefinitionNameInjector; |
| 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * Returns the variable map of interpolated definitions. |
| 1359 | * |
| 1360 | * @return A map to help dereference variables. |
| 1361 | */ |
| 1362 | private Map<String, String> getResolvedMap() { |
| 1363 | return mResolvedMap; |
| 1364 | } |
| 1365 | |
| 1366 | //---- Persistence accessors ---------------------------------------------- |
| 1367 | |
| 1368 | private UserPreferences getUserPreferences() { |
| 1369 | return UserPreferences.getInstance(); |
| 1370 | } |
| 1371 | |
| 1372 | private Path getDefinitionPath() { |
| 1373 | return getUserPreferences().getDefinitionPath(); |
| 1374 | } |
| 1375 | |
| 1376 | //---- Spelling ----------------------------------------------------------- |
| 1377 | |
| 1378 | /** |
| 1379 | * Delegates to {@link #spellcheck(StyleClassedTextArea, String, int)}. |
| 1380 | * This is called to spell check the document, rather than a single paragraph. |
| 1381 | * |
| 1382 | * @param text The full document text. |
| 1383 | */ |
| 1384 | private void spellcheck( |
| 1385 | final StyleClassedTextArea editor, final String text ) { |
| 1386 | spellcheck( editor, text, -1 ); |
| 1387 | } |
| 1388 | |
| 1389 | /** |
| 1390 | * Spellchecks a subset of the entire document. |
| 1391 | * |
| 1392 | * @param text Look up words for this text in the lexicon. |
| 1393 | * @param paraId Set to -1 to apply resulting style spans to the entire |
| 1394 | * text. |
| 1395 | */ |
| 1396 | private void spellcheck( |
| 1397 | final StyleClassedTextArea editor, final String text, final int paraId ) { |
| 1398 | final var builder = new StyleSpansBuilder<Collection<String>>(); |
| 1399 | final var runningIndex = new AtomicInteger( 0 ); |
| 1400 | final var checker = getSpellChecker(); |
| 1401 | |
| 1402 | // The text nodes must be relayed through a contextual "visitor" that |
| 1403 | // can return text in chunks with correlative offsets into the string. |
| 1404 | // This allows Markdown, R Markdown, XML, and R XML documents to return |
| 1405 | // sets of words to check. |
| 1406 | |
| 1407 | final var node = mParser.parse( text ); |
| 1408 | final var visitor = new TextVisitor( ( visited, bIndex, eIndex ) -> { |
| 1409 | // Treat hyphenated compound words as individual words. |
| 1410 | final var check = visited.replace( '-', ' ' ); |
| 1411 | |
| 1412 | checker.proofread( check, ( misspelled, prevIndex, currIndex ) -> { |
| 1413 | prevIndex += bIndex; |
| 1414 | currIndex += bIndex; |
| 1415 | |
| 1416 | // Clear styling between lexiconically absent words. |
| 1417 | builder.add( emptyList(), prevIndex - runningIndex.get() ); |
| 1418 | builder.add( singleton( "spelling" ), currIndex - prevIndex ); |
| 1419 | runningIndex.set( currIndex ); |
| 1420 | } ); |
| 1421 | } ); |
| 1422 | |
| 1423 | visitor.visit( node ); |
| 1424 | |
| 1425 | // If the running index was set, at least one word triggered the listener. |
| 1426 | if( runningIndex.get() > 0 ) { |
| 1427 | // Clear styling after the last lexiconically absent word. |
| 1428 | builder.add( emptyList(), text.length() - runningIndex.get() ); |
| 1429 | |
| 1430 | final var spans = builder.create(); |
| 1431 | |
| 1432 | if( paraId >= 0 ) { |
| 1433 | editor.setStyleSpans( paraId, 0, spans ); |
| 1434 | } |
| 1435 | else { |
| 1436 | editor.setStyleSpans( 0, spans ); |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | @SuppressWarnings("SameParameterValue") |
| 1442 | private Collection<String> readLexicon( final String filename ) |
| 1443 | throws Exception { |
| 1444 | final var path = "/" + LEXICONS_DIRECTORY + "/" + filename; |
| 1445 | |
| 1446 | try( final var resource = getClass().getResourceAsStream( path ) ) { |
| 1447 | if( resource == null ) { |
| 1448 | throw new FileNotFoundException( path ); |
| 1449 | } |
| 1450 | |
| 1451 | try( final var isr = new InputStreamReader( resource, UTF_8 ); |
| 1452 | final var reader = new BufferedReader( isr ) ) { |
| 1453 | return reader.lines().collect( Collectors.toList() ); |
| 1454 | } |
| 1448 | 1455 | } |
| 1449 | 1456 | } |