| 34 | 34 | import com.scrivenvar.editors.VariableNameInjector; |
| 35 | 35 | import com.scrivenvar.editors.markdown.MarkdownEditorPane; |
| 36 | | import com.scrivenvar.preview.HTMLPreviewPane; |
| 37 | | import com.scrivenvar.processors.Processor; |
| 38 | | import com.scrivenvar.processors.ProcessorFactory; |
| 39 | | import com.scrivenvar.service.Options; |
| 40 | | import com.scrivenvar.service.Snitch; |
| 41 | | import com.scrivenvar.service.events.Notifier; |
| 42 | | import com.scrivenvar.util.Action; |
| 43 | | import com.scrivenvar.util.ActionUtils; |
| 44 | | import static com.scrivenvar.util.StageState.*; |
| 45 | | import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| 46 | | import java.nio.file.Path; |
| 47 | | import java.util.HashMap; |
| 48 | | import java.util.Map; |
| 49 | | import java.util.Observable; |
| 50 | | import java.util.Observer; |
| 51 | | import java.util.function.Function; |
| 52 | | import java.util.prefs.Preferences; |
| 53 | | import javafx.application.Platform; |
| 54 | | import javafx.beans.binding.Bindings; |
| 55 | | import javafx.beans.binding.BooleanBinding; |
| 56 | | import javafx.beans.property.BooleanProperty; |
| 57 | | import javafx.beans.property.SimpleBooleanProperty; |
| 58 | | import javafx.beans.value.ObservableBooleanValue; |
| 59 | | import javafx.beans.value.ObservableValue; |
| 60 | | import javafx.collections.ListChangeListener.Change; |
| 61 | | import javafx.collections.ObservableList; |
| 62 | | import static javafx.event.Event.fireEvent; |
| 63 | | import javafx.geometry.Pos; |
| 64 | | import javafx.scene.Node; |
| 65 | | import javafx.scene.Scene; |
| 66 | | import javafx.scene.control.Alert; |
| 67 | | import javafx.scene.control.Alert.AlertType; |
| 68 | | import javafx.scene.control.Menu; |
| 69 | | import javafx.scene.control.MenuBar; |
| 70 | | import javafx.scene.control.SplitPane; |
| 71 | | import javafx.scene.control.Tab; |
| 72 | | import javafx.scene.control.ToolBar; |
| 73 | | import javafx.scene.control.TreeView; |
| 74 | | import javafx.scene.image.Image; |
| 75 | | import javafx.scene.image.ImageView; |
| 76 | | import static javafx.scene.input.KeyCode.ESCAPE; |
| 77 | | import javafx.scene.input.KeyEvent; |
| 78 | | import static javafx.scene.input.KeyEvent.CHAR_UNDEFINED; |
| 79 | | import static javafx.scene.input.KeyEvent.KEY_PRESSED; |
| 80 | | import javafx.scene.layout.BorderPane; |
| 81 | | import javafx.scene.layout.VBox; |
| 82 | | import javafx.scene.text.Text; |
| 83 | | import javafx.stage.Window; |
| 84 | | import javafx.stage.WindowEvent; |
| 85 | | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 86 | | import org.controlsfx.control.StatusBar; |
| 87 | | import org.fxmisc.richtext.model.TwoDimensional.Position; |
| 88 | | |
| 89 | | /** |
| 90 | | * Main window containing a tab pane in the center for file editors. |
| 91 | | * |
| 92 | | * @author Karl Tauber and White Magic Software, Ltd. |
| 93 | | */ |
| 94 | | public class MainWindow implements Observer { |
| 95 | | |
| 96 | | private final Options options = Services.load( Options.class ); |
| 97 | | private final Snitch snitch = Services.load( Snitch.class ); |
| 98 | | private final Notifier notifier = Services.load( Notifier.class ); |
| 99 | | |
| 100 | | private Scene scene; |
| 101 | | private MenuBar menuBar; |
| 102 | | private StatusBar statusBar; |
| 103 | | private Text lineNumberText; |
| 104 | | |
| 105 | | private DefinitionSource definitionSource; |
| 106 | | private DefinitionPane definitionPane; |
| 107 | | private FileEditorTabPane fileEditorPane; |
| 108 | | private HTMLPreviewPane previewPane; |
| 109 | | |
| 110 | | /** |
| 111 | | * Prevent re-instantiation processing classes. |
| 112 | | */ |
| 113 | | private Map<FileEditorTab, Processor<String>> processors; |
| 114 | | |
| 115 | | public MainWindow() { |
| 116 | | initLayout(); |
| 117 | | initDefinitionListener(); |
| 118 | | initTabAddedListener(); |
| 119 | | initTabChangedListener(); |
| 120 | | initPreferences(); |
| 121 | | initSnitch(); |
| 122 | | } |
| 123 | | |
| 124 | | /** |
| 125 | | * Listen for file editor tab pane to receive an open definition source event. |
| 126 | | */ |
| 127 | | private void initDefinitionListener() { |
| 128 | | getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| 129 | | (ObservableValue<? extends Path> definitionFile, |
| 130 | | final Path oldPath, final Path newPath) -> { |
| 131 | | openDefinition( newPath ); |
| 132 | | |
| 133 | | // Indirectly refresh the resolved map. |
| 134 | | setProcessors( null ); |
| 135 | | |
| 136 | | updateDefinitionPane(); |
| 137 | | |
| 138 | | // Will create new processors and therefore a new resolved map. |
| 139 | | refreshSelectedTab( getActiveFileEditor() ); |
| 140 | | } |
| 141 | | ); |
| 142 | | } |
| 143 | | |
| 144 | | /** |
| 145 | | * When tabs are added, hook the various change listeners onto the new tab so |
| 146 | | * that the preview pane refreshes as necessary. |
| 147 | | */ |
| 148 | | private void initTabAddedListener() { |
| 149 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 150 | | |
| 151 | | // Make sure the text processor kicks off when new files are opened. |
| 152 | | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 153 | | |
| 154 | | // Update the preview pane on tab changes. |
| 155 | | tabs.addListener( |
| 156 | | (final Change<? extends Tab> change) -> { |
| 157 | | while( change.next() ) { |
| 158 | | if( change.wasAdded() ) { |
| 159 | | // Multiple tabs can be added simultaneously. |
| 160 | | for( final Tab newTab : change.getAddedSubList() ) { |
| 161 | | final FileEditorTab tab = (FileEditorTab)newTab; |
| 162 | | |
| 163 | | initTextChangeListener( tab ); |
| 164 | | initCaretParagraphListener( tab ); |
| 165 | | initVariableNameInjector( tab ); |
| 166 | | // initSyntaxListener( tab ); |
| 167 | | } |
| 168 | | } |
| 169 | | } |
| 170 | | } |
| 171 | | ); |
| 172 | | } |
| 173 | | |
| 174 | | /** |
| 175 | | * Reloads the preferences from the previous load. |
| 176 | | */ |
| 177 | | private void initPreferences() { |
| 178 | | restoreDefinitionSource(); |
| 179 | | getFileEditorPane().restorePreferences(); |
| 180 | | updateDefinitionPane(); |
| 181 | | } |
| 182 | | |
| 183 | | /** |
| 184 | | * Listen for new tab selection events. |
| 185 | | */ |
| 186 | | private void initTabChangedListener() { |
| 187 | | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 188 | | |
| 189 | | // Update the preview pane changing tabs. |
| 190 | | editorPane.addTabSelectionListener( |
| 191 | | (ObservableValue<? extends Tab> tabPane, |
| 192 | | final Tab oldTab, final Tab newTab) -> { |
| 193 | | |
| 194 | | // If there was no old tab, then this is a first time load, which |
| 195 | | // can be ignored. |
| 196 | | if( oldTab != null ) { |
| 197 | | if( newTab == null ) { |
| 198 | | closeRemainingTab(); |
| 199 | | } |
| 200 | | else { |
| 201 | | // Update the preview with the edited text. |
| 202 | | refreshSelectedTab( (FileEditorTab)newTab ); |
| 203 | | } |
| 204 | | } |
| 205 | | } |
| 206 | | ); |
| 207 | | } |
| 208 | | |
| 209 | | private void initTextChangeListener( final FileEditorTab tab ) { |
| 210 | | tab.addTextChangeListener( |
| 211 | | (ObservableValue<? extends String> editor, |
| 212 | | final String oldValue, final String newValue) -> { |
| 213 | | refreshSelectedTab( tab ); |
| 214 | | } |
| 215 | | ); |
| 216 | | } |
| 217 | | |
| 218 | | private void initCaretParagraphListener( final FileEditorTab tab ) { |
| 219 | | tab.addCaretParagraphListener( |
| 220 | | (ObservableValue<? extends Integer> editor, |
| 221 | | final Integer oldValue, final Integer newValue) -> { |
| 222 | | refreshSelectedTab( tab ); |
| 223 | | } |
| 224 | | ); |
| 225 | | } |
| 226 | | |
| 227 | | private void initVariableNameInjector( final FileEditorTab tab ) { |
| 228 | | VariableNameInjector.listen( tab, getDefinitionPane() ); |
| 229 | | } |
| 230 | | |
| 231 | | /** |
| 232 | | * Watch for changes to external files. In particular, this awaits |
| 233 | | * modifications to any XSL files associated with XML files being edited. When |
| 234 | | * an XSL file is modified (external to the application), the snitch's ears |
| 235 | | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 236 | | * date with what's on the file system. |
| 237 | | */ |
| 238 | | private void initSnitch() { |
| 239 | | getSnitch().addObserver( this ); |
| 240 | | } |
| 241 | | |
| 242 | | /** |
| 243 | | * Called whenever the preview pane becomes out of sync with the file editor |
| 244 | | * tab. This can be called when the text changes, the caret paragraph changes, |
| 245 | | * or the file tab changes. |
| 246 | | * |
| 247 | | * @param tab The file editor tab that has been changed in some fashion. |
| 248 | | */ |
| 249 | | private void refreshSelectedTab( final FileEditorTab tab ) { |
| 250 | | if( tab.isFileOpen() ) { |
| 251 | | getPreviewPane().setPath( tab.getPath() ); |
| 252 | | |
| 253 | | // TODO: https://github.com/DaveJarvis/scrivenvar/issues/29 |
| 254 | | final Position p = tab.getCaretOffset(); |
| 255 | | getLineNumberText().setText( |
| 256 | | get( STATUS_BAR_LINE, |
| 257 | | p.getMajor() + 1, |
| 258 | | p.getMinor() + 1, |
| 259 | | tab.getCaretPosition() + 1 |
| 260 | | ) |
| 261 | | ); |
| 262 | | |
| 263 | | Processor<String> processor = getProcessors().get( tab ); |
| 264 | | |
| 265 | | if( processor == null ) { |
| 266 | | processor = createProcessor( tab ); |
| 267 | | getProcessors().put( tab, processor ); |
| 268 | | } |
| 269 | | |
| 270 | | try { |
| 271 | | getNotifier().clear(); |
| 272 | | processor.processChain( tab.getEditorText() ); |
| 273 | | } catch( final Exception ex ) { |
| 274 | | error( ex ); |
| 275 | | } |
| 276 | | } |
| 277 | | } |
| 278 | | |
| 279 | | /** |
| 280 | | * Returns the variable map of interpolated definitions. |
| 281 | | * |
| 282 | | * @return A map to help dereference variables. |
| 283 | | */ |
| 284 | | private Map<String, String> getResolvedMap() { |
| 285 | | return getDefinitionSource().getResolvedMap(); |
| 286 | | } |
| 287 | | |
| 288 | | /** |
| 289 | | * Returns the root node for the hierarchical definition source. |
| 290 | | * |
| 291 | | * @return Data to display in the definition pane. |
| 292 | | */ |
| 293 | | private TreeView<String> getTreeView() { |
| 294 | | try { |
| 295 | | return getDefinitionSource().asTreeView(); |
| 296 | | } catch( Exception e ) { |
| 297 | | error( e ); |
| 298 | | } |
| 299 | | |
| 300 | | // Slightly redundant as getDefinitionSource() might have returned an |
| 301 | | // empty definition source. |
| 302 | | return (new EmptyDefinitionSource()).asTreeView(); |
| 303 | | } |
| 304 | | |
| 305 | | /** |
| 306 | | * Called when a definition source is opened. |
| 307 | | * |
| 308 | | * @param path Path to the definition source that was opened. |
| 309 | | */ |
| 310 | | private void openDefinition( final Path path ) { |
| 311 | | try { |
| 312 | | final DefinitionSource ds = createDefinitionSource( path.toString() ); |
| 313 | | setDefinitionSource( ds ); |
| 314 | | storeDefinitionSource(); |
| 315 | | updateDefinitionPane(); |
| 316 | | } catch( final Exception e ) { |
| 317 | | error( e ); |
| 318 | | } |
| 319 | | } |
| 320 | | |
| 321 | | private void updateDefinitionPane() { |
| 322 | | getDefinitionPane().setRoot( getDefinitionSource().asTreeView() ); |
| 323 | | } |
| 324 | | |
| 325 | | private void restoreDefinitionSource() { |
| 326 | | final Preferences preferences = getPreferences(); |
| 327 | | final String source = preferences.get( PREFS_DEFINITION_SOURCE, null ); |
| 328 | | |
| 329 | | // If there's no definition source set, don't try to load it. |
| 330 | | if( source != null ) { |
| 331 | | setDefinitionSource( createDefinitionSource( source ) ); |
| 332 | | } |
| 333 | | } |
| 334 | | |
| 335 | | private void storeDefinitionSource() { |
| 336 | | final Preferences preferences = getPreferences(); |
| 337 | | final DefinitionSource ds = getDefinitionSource(); |
| 338 | | |
| 339 | | preferences.put( PREFS_DEFINITION_SOURCE, ds.toString() ); |
| 340 | | } |
| 341 | | |
| 342 | | /** |
| 343 | | * Called when the last open tab is closed to clear the preview pane. |
| 344 | | */ |
| 345 | | private void closeRemainingTab() { |
| 346 | | getPreviewPane().clear(); |
| 347 | | } |
| 348 | | |
| 349 | | /** |
| 350 | | * Called when an exception occurs that warrants the user's attention. |
| 351 | | * |
| 352 | | * @param e The exception with a message that the user should know about. |
| 353 | | */ |
| 354 | | private void error( final Exception e ) { |
| 355 | | getNotifier().notify( e ); |
| 356 | | } |
| 357 | | |
| 358 | | //---- File actions ------------------------------------------------------- |
| 359 | | /** |
| 360 | | * Called when an observable instance has changed. This includes the snitch |
| 361 | | * service and the notify service. |
| 362 | | * |
| 363 | | * @param observable The observed instance. |
| 364 | | * @param value The noteworthy item. |
| 365 | | */ |
| 366 | | @Override |
| 367 | | public void update( final Observable observable, final Object value ) { |
| 368 | | if( value != null ) { |
| 369 | | if( observable instanceof Snitch && value instanceof Path ) { |
| 370 | | update( (Path)value ); |
| 371 | | } |
| 372 | | else if( observable instanceof Notifier && value instanceof String ) { |
| 373 | | final String s = (String)value; |
| 374 | | final int index = s.indexOf( '\n' ); |
| 375 | | final String message = s.substring( 0, index > 0 ? index : s.length() ); |
| 376 | | |
| 377 | | getStatusBar().setText( message ); |
| 378 | | } |
| 379 | | } |
| 380 | | } |
| 381 | | |
| 382 | | /** |
| 383 | | * Called when a file has been modified. |
| 384 | | * |
| 385 | | * @param file Path to the modified file. |
| 386 | | */ |
| 387 | | private void update( final Path file ) { |
| 388 | | // Avoid throwing IllegalStateException by running from a non-JavaFX thread. |
| 389 | | Platform.runLater( |
| 390 | | () -> { |
| 391 | | // Brute-force XSLT file reload by re-instantiating all processors. |
| 392 | | resetProcessors(); |
| 393 | | refreshSelectedTab( getActiveFileEditor() ); |
| 394 | | } |
| 395 | | ); |
| 396 | | } |
| 397 | | |
| 398 | | /** |
| 399 | | * After resetting the processors, they will refresh anew to be up-to-date |
| 400 | | * with the files (text and definition) currently loaded into the editor. |
| 401 | | */ |
| 402 | | private void resetProcessors() { |
| 403 | | getProcessors().clear(); |
| 404 | | } |
| 405 | | |
| 406 | | //---- File actions ------------------------------------------------------- |
| 407 | | private void fileNew() { |
| 408 | | getFileEditorPane().newEditor(); |
| 409 | | } |
| 410 | | |
| 411 | | private void fileOpen() { |
| 412 | | getFileEditorPane().openFileDialog(); |
| 413 | | } |
| 414 | | |
| 415 | | private void fileClose() { |
| 416 | | getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| 417 | | } |
| 418 | | |
| 419 | | private void fileCloseAll() { |
| 420 | | getFileEditorPane().closeAllEditors(); |
| 421 | | } |
| 422 | | |
| 423 | | private void fileSave() { |
| 424 | | getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| 425 | | } |
| 426 | | |
| 427 | | private void fileSaveAll() { |
| 428 | | getFileEditorPane().saveAllEditors(); |
| 429 | | } |
| 430 | | |
| 431 | | private void fileExit() { |
| 432 | | final Window window = getWindow(); |
| 433 | | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 434 | | } |
| 435 | | |
| 436 | | //---- Help actions ------------------------------------------------------- |
| 437 | | private void helpAbout() { |
| 438 | | Alert alert = new Alert( AlertType.INFORMATION ); |
| 439 | | alert.setTitle( get( "Dialog.about.title" ) ); |
| 440 | | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 441 | | alert.setContentText( get( "Dialog.about.content" ) ); |
| 442 | | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 443 | | alert.initOwner( getWindow() ); |
| 444 | | |
| 445 | | alert.showAndWait(); |
| 446 | | } |
| 447 | | |
| 448 | | //---- Convenience accessors ---------------------------------------------- |
| 449 | | private float getFloat( final String key, final float defaultValue ) { |
| 450 | | return getPreferences().getFloat( key, defaultValue ); |
| 451 | | } |
| 452 | | |
| 453 | | private Preferences getPreferences() { |
| 454 | | return getOptions().getState(); |
| 455 | | } |
| 456 | | |
| 457 | | protected Scene getScene() { |
| 458 | | if( this.scene == null ) { |
| 459 | | this.scene = createScene(); |
| 460 | | } |
| 461 | | |
| 462 | | return this.scene; |
| 463 | | } |
| 464 | | |
| 465 | | public Window getWindow() { |
| 466 | | return getScene().getWindow(); |
| 467 | | } |
| 468 | | |
| 469 | | private MarkdownEditorPane getActiveEditor() { |
| 470 | | final EditorPane pane = getActiveFileEditor().getEditorPane(); |
| 471 | | |
| 472 | | return pane instanceof MarkdownEditorPane ? (MarkdownEditorPane)pane : null; |
| 473 | | } |
| 474 | | |
| 475 | | private FileEditorTab getActiveFileEditor() { |
| 476 | | return getFileEditorPane().getActiveFileEditor(); |
| 477 | | } |
| 478 | | |
| 479 | | //---- Member accessors --------------------------------------------------- |
| 480 | | private void setScene( Scene scene ) { |
| 481 | | this.scene = scene; |
| 482 | | } |
| 483 | | |
| 484 | | private void setProcessors( final Map<FileEditorTab, Processor<String>> map ) { |
| 485 | | this.processors = map; |
| 486 | | } |
| 487 | | |
| 488 | | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 489 | | if( this.processors == null ) { |
| 490 | | setProcessors( new HashMap<>() ); |
| 491 | | } |
| 492 | | |
| 493 | | return this.processors; |
| 494 | | } |
| 495 | | |
| 496 | | private FileEditorTabPane getFileEditorPane() { |
| 497 | | if( this.fileEditorPane == null ) { |
| 498 | | this.fileEditorPane = createFileEditorPane(); |
| 499 | | } |
| 500 | | |
| 501 | | return this.fileEditorPane; |
| 502 | | } |
| 503 | | |
| 504 | | private HTMLPreviewPane getPreviewPane() { |
| 505 | | if( this.previewPane == null ) { |
| 506 | | this.previewPane = createPreviewPane(); |
| 507 | | } |
| 508 | | |
| 509 | | return this.previewPane; |
| 510 | | } |
| 511 | | |
| 512 | | private void setDefinitionSource( final DefinitionSource definitionSource ) { |
| 513 | | this.definitionSource = definitionSource; |
| 514 | | } |
| 515 | | |
| 516 | | private DefinitionSource getDefinitionSource() { |
| 517 | | if( this.definitionSource == null ) { |
| 518 | | this.definitionSource = new EmptyDefinitionSource(); |
| 519 | | } |
| 520 | | |
| 521 | | return this.definitionSource; |
| 522 | | } |
| 523 | | |
| 524 | | private DefinitionPane getDefinitionPane() { |
| 525 | | if( this.definitionPane == null ) { |
| 526 | | this.definitionPane = createDefinitionPane(); |
| 527 | | } |
| 528 | | |
| 529 | | return this.definitionPane; |
| 530 | | } |
| 531 | | |
| 532 | | private Options getOptions() { |
| 533 | | return this.options; |
| 534 | | } |
| 535 | | |
| 536 | | private Snitch getSnitch() { |
| 537 | | return this.snitch; |
| 538 | | } |
| 539 | | |
| 540 | | private Notifier getNotifier() { |
| 541 | | return this.notifier; |
| 542 | | } |
| 543 | | |
| 544 | | public void setMenuBar( final MenuBar menuBar ) { |
| 545 | | this.menuBar = menuBar; |
| 546 | | } |
| 547 | | |
| 548 | | public MenuBar getMenuBar() { |
| 549 | | return this.menuBar; |
| 550 | | } |
| 551 | | |
| 552 | | private Text getLineNumberText() { |
| 553 | | if( this.lineNumberText == null ) { |
| 554 | | this.lineNumberText = createLineNumberText(); |
| 555 | | } |
| 556 | | |
| 557 | | return this.lineNumberText; |
| 558 | | } |
| 559 | | |
| 560 | | private synchronized StatusBar getStatusBar() { |
| 561 | | if( this.statusBar == null ) { |
| 562 | | this.statusBar = createStatusBar(); |
| 563 | | } |
| 564 | | |
| 565 | | return this.statusBar; |
| 566 | | } |
| 567 | | |
| 568 | | //---- Member creators ---------------------------------------------------- |
| 569 | | /** |
| 570 | | * Factory to create processors that are suited to different file types. |
| 571 | | * |
| 572 | | * @param tab The tab that is subjected to processing. |
| 573 | | * |
| 574 | | * @return A processor suited to the file type specified by the tab's path. |
| 575 | | */ |
| 576 | | private Processor<String> createProcessor( final FileEditorTab tab ) { |
| 577 | | return createProcessorFactory().createProcessor( tab ); |
| 578 | | } |
| 579 | | |
| 580 | | private ProcessorFactory createProcessorFactory() { |
| 581 | | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 582 | | } |
| 583 | | |
| 584 | | private DefinitionSource createDefinitionSource( final String path ) { |
| 585 | | return createDefinitionFactory().createDefinitionSource( path ); |
| 586 | | } |
| 587 | | |
| 588 | | /** |
| 589 | | * Create an editor pane to hold file editor tabs. |
| 590 | | * |
| 591 | | * @return A new instance, never null. |
| 592 | | */ |
| 593 | | private FileEditorTabPane createFileEditorPane() { |
| 594 | | return new FileEditorTabPane(); |
| 595 | | } |
| 596 | | |
| 597 | | private HTMLPreviewPane createPreviewPane() { |
| 598 | | return new HTMLPreviewPane(); |
| 599 | | } |
| 600 | | |
| 601 | | private DefinitionPane createDefinitionPane() { |
| 602 | | return new DefinitionPane( getTreeView() ); |
| 603 | | } |
| 604 | | |
| 605 | | private DefinitionFactory createDefinitionFactory() { |
| 606 | | return new DefinitionFactory(); |
| 607 | | } |
| 608 | | |
| 609 | | private StatusBar createStatusBar() { |
| 610 | | return new StatusBar(); |
| 611 | | } |
| 612 | | |
| 613 | | private Scene createScene() { |
| 614 | | final SplitPane splitPane = new SplitPane( |
| 615 | | getDefinitionPane().getNode(), |
| 616 | | getFileEditorPane().getNode(), |
| 617 | | getPreviewPane().getNode() ); |
| 618 | | |
| 619 | | splitPane.setDividerPositions( |
| 620 | | getFloat( K_PANE_SPLIT_DEFINITION, .10f ), |
| 621 | | getFloat( K_PANE_SPLIT_EDITOR, .45f ), |
| 622 | | getFloat( K_PANE_SPLIT_PREVIEW, .45f ) ); |
| 623 | | |
| 624 | | // See: http://broadlyapplicable.blogspot.ca/2015/03/javafx-capture-restorePreferences-splitpane.html |
| 625 | | final BorderPane borderPane = new BorderPane(); |
| 626 | | borderPane.setPrefSize( 1024, 800 ); |
| 627 | | borderPane.setTop( createMenuBar() ); |
| 628 | | borderPane.setBottom( getStatusBar() ); |
| 629 | | borderPane.setCenter( splitPane ); |
| 630 | | |
| 631 | | final VBox box = new VBox(); |
| 632 | | box.setAlignment( Pos.BASELINE_CENTER ); |
| 633 | | box.getChildren().add( getLineNumberText() ); |
| 634 | | getStatusBar().getRightItems().add( box ); |
| 635 | | |
| 636 | | return new Scene( borderPane ); |
| 637 | | } |
| 638 | | |
| 639 | | private Text createLineNumberText() { |
| 640 | | return new Text( get( STATUS_BAR_LINE, 1, 1, 1 ) ); |
| 641 | | } |
| 642 | | |
| 643 | | private Node createMenuBar() { |
| 644 | | final BooleanBinding activeFileEditorIsNull = getFileEditorPane().activeFileEditorProperty().isNull(); |
| 645 | | |
| 646 | | // File actions |
| 647 | | Action fileNewAction = new Action( get( "Main.menu.file.new" ), "Shortcut+N", FILE_ALT, e -> fileNew() ); |
| 648 | | Action fileOpenAction = new Action( get( "Main.menu.file.open" ), "Shortcut+O", FOLDER_OPEN_ALT, e -> fileOpen() ); |
| 649 | | Action fileCloseAction = new Action( get( "Main.menu.file.close" ), "Shortcut+W", null, e -> fileClose(), activeFileEditorIsNull ); |
| 650 | | Action fileCloseAllAction = new Action( get( "Main.menu.file.close_all" ), null, null, e -> fileCloseAll(), activeFileEditorIsNull ); |
| 651 | | Action fileSaveAction = new Action( get( "Main.menu.file.save" ), "Shortcut+S", FLOPPY_ALT, e -> fileSave(), |
| 652 | | createActiveBooleanProperty( FileEditorTab::modifiedProperty ).not() ); |
| 653 | | Action fileSaveAllAction = new Action( get( "Main.menu.file.save_all" ), "Shortcut+Shift+S", null, e -> fileSaveAll(), |
| 654 | | Bindings.not( getFileEditorPane().anyFileEditorModifiedProperty() ) ); |
| 655 | | Action fileExitAction = new Action( get( "Main.menu.file.exit" ), null, null, e -> fileExit() ); |
| 656 | | |
| 657 | | // Edit actions |
| 658 | | Action editUndoAction = new Action( get( "Main.menu.edit.undo" ), "Shortcut+Z", UNDO, |
| 659 | | e -> getActiveEditor().undo(), |
| 660 | | createActiveBooleanProperty( FileEditorTab::canUndoProperty ).not() ); |
| 661 | | Action editRedoAction = new Action( get( "Main.menu.edit.redo" ), "Shortcut+Y", REPEAT, |
| 662 | | e -> getActiveEditor().redo(), |
| 663 | | createActiveBooleanProperty( FileEditorTab::canRedoProperty ).not() ); |
| 664 | | Action editFindAction = new Action( Messages.get( "Main.menu.edit.find" ), "Shortcut+F", SEARCH, |
| 665 | | e -> getActiveEditor().find(), |
| 666 | | activeFileEditorIsNull ); |
| 667 | | Action editReplaceAction = new Action( Messages.get( "Main.menu.edit.find.replace" ), "Shortcut+H", RETWEET, |
| 668 | | e -> getActiveEditor().replace(), |
| 669 | | activeFileEditorIsNull ); |
| 670 | | Action editFindNextAction = new Action( Messages.get( "Main.menu.edit.find.next" ), "F3", null, |
| 671 | | e -> getActiveEditor().findNext(), |
| 36 | import com.scrivenvar.predicates.files.FileTypePredicate; |
| 37 | import com.scrivenvar.preview.HTMLPreviewPane; |
| 38 | import com.scrivenvar.processors.Processor; |
| 39 | import com.scrivenvar.processors.ProcessorFactory; |
| 40 | import com.scrivenvar.service.Options; |
| 41 | import com.scrivenvar.service.Snitch; |
| 42 | import com.scrivenvar.service.events.Notifier; |
| 43 | import com.scrivenvar.util.Action; |
| 44 | import com.scrivenvar.util.ActionUtils; |
| 45 | import static com.scrivenvar.util.StageState.*; |
| 46 | import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| 47 | import java.io.IOException; |
| 48 | import java.nio.file.Path; |
| 49 | import java.util.HashMap; |
| 50 | import java.util.Map; |
| 51 | import java.util.Observable; |
| 52 | import java.util.Observer; |
| 53 | import java.util.function.Function; |
| 54 | import java.util.prefs.Preferences; |
| 55 | import javafx.application.Platform; |
| 56 | import javafx.beans.binding.Bindings; |
| 57 | import javafx.beans.binding.BooleanBinding; |
| 58 | import javafx.beans.property.BooleanProperty; |
| 59 | import javafx.beans.property.SimpleBooleanProperty; |
| 60 | import javafx.beans.value.ObservableBooleanValue; |
| 61 | import javafx.beans.value.ObservableValue; |
| 62 | import javafx.collections.ListChangeListener.Change; |
| 63 | import javafx.collections.ObservableList; |
| 64 | import static javafx.event.Event.fireEvent; |
| 65 | import javafx.geometry.Pos; |
| 66 | import javafx.scene.Node; |
| 67 | import javafx.scene.Scene; |
| 68 | import javafx.scene.control.Alert; |
| 69 | import javafx.scene.control.Alert.AlertType; |
| 70 | import javafx.scene.control.Menu; |
| 71 | import javafx.scene.control.MenuBar; |
| 72 | import javafx.scene.control.SplitPane; |
| 73 | import javafx.scene.control.Tab; |
| 74 | import javafx.scene.control.TextField; |
| 75 | import javafx.scene.control.ToolBar; |
| 76 | import javafx.scene.control.TreeView; |
| 77 | import javafx.scene.image.Image; |
| 78 | import javafx.scene.image.ImageView; |
| 79 | import static javafx.scene.input.KeyCode.ESCAPE; |
| 80 | import javafx.scene.input.KeyEvent; |
| 81 | import static javafx.scene.input.KeyEvent.CHAR_UNDEFINED; |
| 82 | import static javafx.scene.input.KeyEvent.KEY_PRESSED; |
| 83 | import javafx.scene.layout.BorderPane; |
| 84 | import javafx.scene.layout.VBox; |
| 85 | import javafx.scene.text.Text; |
| 86 | import javafx.stage.Window; |
| 87 | import javafx.stage.WindowEvent; |
| 88 | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 89 | import org.controlsfx.control.StatusBar; |
| 90 | import org.fxmisc.richtext.model.TwoDimensional.Position; |
| 91 | |
| 92 | /** |
| 93 | * Main window containing a tab pane in the center for file editors. |
| 94 | * |
| 95 | * @author Karl Tauber and White Magic Software, Ltd. |
| 96 | */ |
| 97 | public class MainWindow implements Observer { |
| 98 | |
| 99 | private final Options options = Services.load( Options.class ); |
| 100 | private final Snitch snitch = Services.load( Snitch.class ); |
| 101 | private final Notifier notifier = Services.load( Notifier.class ); |
| 102 | |
| 103 | private Scene scene; |
| 104 | private MenuBar menuBar; |
| 105 | private StatusBar statusBar; |
| 106 | private Text lineNumberText; |
| 107 | |
| 108 | private DefinitionSource definitionSource; |
| 109 | private DefinitionPane definitionPane; |
| 110 | private FileEditorTabPane fileEditorPane; |
| 111 | private HTMLPreviewPane previewPane; |
| 112 | |
| 113 | /** |
| 114 | * Prevent re-instantiation processing classes. |
| 115 | */ |
| 116 | private Map<FileEditorTab, Processor<String>> processors; |
| 117 | |
| 118 | public MainWindow() { |
| 119 | initLayout(); |
| 120 | initSnitch(); |
| 121 | initDefinitionListener(); |
| 122 | initTabAddedListener(); |
| 123 | initTabChangedListener(); |
| 124 | initPreferences(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Listen for file editor tab pane to receive an open definition source event. |
| 129 | */ |
| 130 | private void initDefinitionListener() { |
| 131 | getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| 132 | (ObservableValue<? extends Path> definitionFile, |
| 133 | final Path oldPath, final Path newPath) -> { |
| 134 | openDefinition( newPath ); |
| 135 | |
| 136 | // Indirectly refresh the resolved map. |
| 137 | setProcessors( null ); |
| 138 | |
| 139 | updateDefinitionPane(); |
| 140 | |
| 141 | try { |
| 142 | getSnitch().ignore( oldPath ); |
| 143 | getSnitch().listen( newPath ); |
| 144 | } catch( final IOException ex ) { |
| 145 | error( ex ); |
| 146 | } |
| 147 | |
| 148 | // Will create new processors and therefore a new resolved map. |
| 149 | refreshSelectedTab( getActiveFileEditor() ); |
| 150 | } |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * When tabs are added, hook the various change listeners onto the new tab so |
| 156 | * that the preview pane refreshes as necessary. |
| 157 | */ |
| 158 | private void initTabAddedListener() { |
| 159 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 160 | |
| 161 | // Make sure the text processor kicks off when new files are opened. |
| 162 | final ObservableList<Tab> tabs = editorPane.getTabs(); |
| 163 | |
| 164 | // Update the preview pane on tab changes. |
| 165 | tabs.addListener( |
| 166 | (final Change<? extends Tab> change) -> { |
| 167 | while( change.next() ) { |
| 168 | if( change.wasAdded() ) { |
| 169 | // Multiple tabs can be added simultaneously. |
| 170 | for( final Tab newTab : change.getAddedSubList() ) { |
| 171 | final FileEditorTab tab = (FileEditorTab)newTab; |
| 172 | |
| 173 | initTextChangeListener( tab ); |
| 174 | initCaretParagraphListener( tab ); |
| 175 | initVariableNameInjector( tab ); |
| 176 | // initSyntaxListener( tab ); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Reloads the preferences from the previous load. |
| 186 | */ |
| 187 | private void initPreferences() { |
| 188 | restoreDefinitionSource(); |
| 189 | getFileEditorPane().restorePreferences(); |
| 190 | updateDefinitionPane(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Listen for new tab selection events. |
| 195 | */ |
| 196 | private void initTabChangedListener() { |
| 197 | final FileEditorTabPane editorPane = getFileEditorPane(); |
| 198 | |
| 199 | // Update the preview pane changing tabs. |
| 200 | editorPane.addTabSelectionListener( |
| 201 | (ObservableValue<? extends Tab> tabPane, |
| 202 | final Tab oldTab, final Tab newTab) -> { |
| 203 | |
| 204 | // If there was no old tab, then this is a first time load, which |
| 205 | // can be ignored. |
| 206 | if( oldTab != null ) { |
| 207 | if( newTab == null ) { |
| 208 | closeRemainingTab(); |
| 209 | } |
| 210 | else { |
| 211 | // Update the preview with the edited text. |
| 212 | refreshSelectedTab( (FileEditorTab)newTab ); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | private void initTextChangeListener( final FileEditorTab tab ) { |
| 220 | tab.addTextChangeListener( |
| 221 | (ObservableValue<? extends String> editor, |
| 222 | final String oldValue, final String newValue) -> { |
| 223 | refreshSelectedTab( tab ); |
| 224 | } |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | private void initCaretParagraphListener( final FileEditorTab tab ) { |
| 229 | tab.addCaretParagraphListener( |
| 230 | (ObservableValue<? extends Integer> editor, |
| 231 | final Integer oldValue, final Integer newValue) -> { |
| 232 | refreshSelectedTab( tab ); |
| 233 | } |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | private void initVariableNameInjector( final FileEditorTab tab ) { |
| 238 | VariableNameInjector.listen( tab, getDefinitionPane() ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Watch for changes to external files. In particular, this awaits |
| 243 | * modifications to any XSL files associated with XML files being edited. When |
| 244 | * an XSL file is modified (external to the application), the snitch's ears |
| 245 | * perk up and the file is reloaded. This keeps the XSL transformation up to |
| 246 | * date with what's on the file system. |
| 247 | */ |
| 248 | private void initSnitch() { |
| 249 | getSnitch().addObserver( this ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Called whenever the preview pane becomes out of sync with the file editor |
| 254 | * tab. This can be called when the text changes, the caret paragraph changes, |
| 255 | * or the file tab changes. |
| 256 | * |
| 257 | * @param tab The file editor tab that has been changed in some fashion. |
| 258 | */ |
| 259 | private void refreshSelectedTab( final FileEditorTab tab ) { |
| 260 | if( tab.isFileOpen() ) { |
| 261 | getPreviewPane().setPath( tab.getPath() ); |
| 262 | |
| 263 | // TODO: https://github.com/DaveJarvis/scrivenvar/issues/29 |
| 264 | final Position p = tab.getCaretOffset(); |
| 265 | getLineNumberText().setText( |
| 266 | get( STATUS_BAR_LINE, |
| 267 | p.getMajor() + 1, |
| 268 | p.getMinor() + 1, |
| 269 | tab.getCaretPosition() + 1 |
| 270 | ) |
| 271 | ); |
| 272 | |
| 273 | Processor<String> processor = getProcessors().get( tab ); |
| 274 | |
| 275 | if( processor == null ) { |
| 276 | processor = createProcessor( tab ); |
| 277 | getProcessors().put( tab, processor ); |
| 278 | } |
| 279 | |
| 280 | try { |
| 281 | getNotifier().clear(); |
| 282 | processor.processChain( tab.getEditorText() ); |
| 283 | } catch( final Exception ex ) { |
| 284 | error( ex ); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Used to find text in the active file editor window. |
| 291 | */ |
| 292 | private void find() { |
| 293 | final TextField input = new TextField(); |
| 294 | |
| 295 | input.setOnKeyPressed( (KeyEvent event) -> { |
| 296 | switch( event.getCode() ) { |
| 297 | case F3: |
| 298 | case ENTER: |
| 299 | getActiveFileEditor().searchNext( input.getText() ); |
| 300 | break; |
| 301 | case F: |
| 302 | if( !event.isControlDown() ) { |
| 303 | break; |
| 304 | } |
| 305 | case ESCAPE: |
| 306 | getStatusBar().setGraphic( null ); |
| 307 | getActiveFileEditor().getEditorPane().requestFocus(); |
| 308 | break; |
| 309 | } |
| 310 | } ); |
| 311 | |
| 312 | // Remove when the input field loses focus. |
| 313 | input.focusedProperty().addListener( |
| 314 | ( |
| 315 | final ObservableValue<? extends Boolean> focused, |
| 316 | final Boolean oFocus, |
| 317 | final Boolean nFocus) -> { |
| 318 | if( !nFocus ) { |
| 319 | getStatusBar().setGraphic( null ); |
| 320 | } |
| 321 | } |
| 322 | ); |
| 323 | |
| 324 | getStatusBar().setGraphic( input ); |
| 325 | |
| 326 | input.requestFocus(); |
| 327 | } |
| 328 | |
| 329 | public void findNext() { |
| 330 | System.out.println( "find next" ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns the variable map of interpolated definitions. |
| 335 | * |
| 336 | * @return A map to help dereference variables. |
| 337 | */ |
| 338 | private Map<String, String> getResolvedMap() { |
| 339 | return getDefinitionSource().getResolvedMap(); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns the root node for the hierarchical definition source. |
| 344 | * |
| 345 | * @return Data to display in the definition pane. |
| 346 | */ |
| 347 | private TreeView<String> getTreeView() { |
| 348 | try { |
| 349 | return getDefinitionSource().asTreeView(); |
| 350 | } catch( Exception e ) { |
| 351 | error( e ); |
| 352 | } |
| 353 | |
| 354 | // Slightly redundant as getDefinitionSource() might have returned an |
| 355 | // empty definition source. |
| 356 | return (new EmptyDefinitionSource()).asTreeView(); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Called when a definition source is opened. |
| 361 | * |
| 362 | * @param path Path to the definition source that was opened. |
| 363 | */ |
| 364 | private void openDefinition( final Path path ) { |
| 365 | try { |
| 366 | final DefinitionSource ds = createDefinitionSource( path.toString() ); |
| 367 | setDefinitionSource( ds ); |
| 368 | storeDefinitionSource(); |
| 369 | updateDefinitionPane(); |
| 370 | } catch( final Exception e ) { |
| 371 | error( e ); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | private void updateDefinitionPane() { |
| 376 | getDefinitionPane().setRoot( getDefinitionSource().asTreeView() ); |
| 377 | } |
| 378 | |
| 379 | private void restoreDefinitionSource() { |
| 380 | final Preferences preferences = getPreferences(); |
| 381 | final String source = preferences.get( PREFS_DEFINITION_SOURCE, null ); |
| 382 | |
| 383 | // If there's no definition source set, don't try to load it. |
| 384 | if( source != null ) { |
| 385 | setDefinitionSource( createDefinitionSource( source ) ); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | private void storeDefinitionSource() { |
| 390 | final Preferences preferences = getPreferences(); |
| 391 | final DefinitionSource ds = getDefinitionSource(); |
| 392 | |
| 393 | preferences.put( PREFS_DEFINITION_SOURCE, ds.toString() ); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Called when the last open tab is closed to clear the preview pane. |
| 398 | */ |
| 399 | private void closeRemainingTab() { |
| 400 | getPreviewPane().clear(); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Called when an exception occurs that warrants the user's attention. |
| 405 | * |
| 406 | * @param e The exception with a message that the user should know about. |
| 407 | */ |
| 408 | private void error( final Exception e ) { |
| 409 | getNotifier().notify( e ); |
| 410 | } |
| 411 | |
| 412 | //---- File actions ------------------------------------------------------- |
| 413 | /** |
| 414 | * Called when an observable instance has changed. This is called by both the |
| 415 | * snitch service and the notify service. The snitch service can be called for |
| 416 | * different file types, including definition sources. |
| 417 | * |
| 418 | * @param observable The observed instance. |
| 419 | * @param value The noteworthy item. |
| 420 | */ |
| 421 | @Override |
| 422 | public void update( final Observable observable, final Object value ) { |
| 423 | if( value != null ) { |
| 424 | if( observable instanceof Snitch && value instanceof Path ) { |
| 425 | final Path path = (Path)value; |
| 426 | final FileTypePredicate predicate |
| 427 | = new FileTypePredicate( GLOB_DEFINITION_EXTENSIONS ); |
| 428 | |
| 429 | // Reload definitions. |
| 430 | if( predicate.test( path.toFile() ) ) { |
| 431 | updateDefinitionSource( path ); |
| 432 | } |
| 433 | |
| 434 | updateSelectedTab(); |
| 435 | } |
| 436 | else if( observable instanceof Notifier && value instanceof String ) { |
| 437 | updateStatusBar( (String)value ); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Updates the status bar to show the given message. |
| 444 | * |
| 445 | * @param s The message to show in the status bar. |
| 446 | */ |
| 447 | private void updateStatusBar( final String s ) { |
| 448 | Platform.runLater( |
| 449 | () -> { |
| 450 | final int index = s.indexOf( '\n' ); |
| 451 | final String message = s.substring( 0, index > 0 ? index : s.length() ); |
| 452 | |
| 453 | getStatusBar().setText( message ); |
| 454 | } |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Called when a file has been modified. |
| 460 | * |
| 461 | * @param file Path to the modified file. |
| 462 | */ |
| 463 | private void updateSelectedTab() { |
| 464 | Platform.runLater( |
| 465 | () -> { |
| 466 | // Brute-force XSLT file reload by re-instantiating all processors. |
| 467 | resetProcessors(); |
| 468 | refreshSelectedTab( getActiveFileEditor() ); |
| 469 | } |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Reloads the definition source from the given path. |
| 475 | * |
| 476 | * @param path The path containing new definition information. |
| 477 | */ |
| 478 | private void updateDefinitionSource( final Path path ) { |
| 479 | Platform.runLater( |
| 480 | () -> { |
| 481 | openDefinition( path ); |
| 482 | } |
| 483 | ); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * After resetting the processors, they will refresh anew to be up-to-date |
| 488 | * with the files (text and definition) currently loaded into the editor. |
| 489 | */ |
| 490 | private void resetProcessors() { |
| 491 | getProcessors().clear(); |
| 492 | } |
| 493 | |
| 494 | //---- File actions ------------------------------------------------------- |
| 495 | private void fileNew() { |
| 496 | getFileEditorPane().newEditor(); |
| 497 | } |
| 498 | |
| 499 | private void fileOpen() { |
| 500 | getFileEditorPane().openFileDialog(); |
| 501 | } |
| 502 | |
| 503 | private void fileClose() { |
| 504 | getFileEditorPane().closeEditor( getActiveFileEditor(), true ); |
| 505 | } |
| 506 | |
| 507 | private void fileCloseAll() { |
| 508 | getFileEditorPane().closeAllEditors(); |
| 509 | } |
| 510 | |
| 511 | private void fileSave() { |
| 512 | getFileEditorPane().saveEditor( getActiveFileEditor() ); |
| 513 | } |
| 514 | |
| 515 | private void fileSaveAll() { |
| 516 | getFileEditorPane().saveAllEditors(); |
| 517 | } |
| 518 | |
| 519 | private void fileExit() { |
| 520 | final Window window = getWindow(); |
| 521 | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 522 | } |
| 523 | |
| 524 | //---- Help actions ------------------------------------------------------- |
| 525 | private void helpAbout() { |
| 526 | Alert alert = new Alert( AlertType.INFORMATION ); |
| 527 | alert.setTitle( get( "Dialog.about.title" ) ); |
| 528 | alert.setHeaderText( get( "Dialog.about.header" ) ); |
| 529 | alert.setContentText( get( "Dialog.about.content" ) ); |
| 530 | alert.setGraphic( new ImageView( new Image( FILE_LOGO_32 ) ) ); |
| 531 | alert.initOwner( getWindow() ); |
| 532 | |
| 533 | alert.showAndWait(); |
| 534 | } |
| 535 | |
| 536 | //---- Convenience accessors ---------------------------------------------- |
| 537 | private float getFloat( final String key, final float defaultValue ) { |
| 538 | return getPreferences().getFloat( key, defaultValue ); |
| 539 | } |
| 540 | |
| 541 | private Preferences getPreferences() { |
| 542 | return getOptions().getState(); |
| 543 | } |
| 544 | |
| 545 | protected Scene getScene() { |
| 546 | if( this.scene == null ) { |
| 547 | this.scene = createScene(); |
| 548 | } |
| 549 | |
| 550 | return this.scene; |
| 551 | } |
| 552 | |
| 553 | public Window getWindow() { |
| 554 | return getScene().getWindow(); |
| 555 | } |
| 556 | |
| 557 | private MarkdownEditorPane getActiveEditor() { |
| 558 | final EditorPane pane = getActiveFileEditor().getEditorPane(); |
| 559 | |
| 560 | return pane instanceof MarkdownEditorPane ? (MarkdownEditorPane)pane : null; |
| 561 | } |
| 562 | |
| 563 | private FileEditorTab getActiveFileEditor() { |
| 564 | return getFileEditorPane().getActiveFileEditor(); |
| 565 | } |
| 566 | |
| 567 | //---- Member accessors --------------------------------------------------- |
| 568 | private void setProcessors( final Map<FileEditorTab, Processor<String>> map ) { |
| 569 | this.processors = map; |
| 570 | } |
| 571 | |
| 572 | private Map<FileEditorTab, Processor<String>> getProcessors() { |
| 573 | if( this.processors == null ) { |
| 574 | setProcessors( new HashMap<>() ); |
| 575 | } |
| 576 | |
| 577 | return this.processors; |
| 578 | } |
| 579 | |
| 580 | private FileEditorTabPane getFileEditorPane() { |
| 581 | if( this.fileEditorPane == null ) { |
| 582 | this.fileEditorPane = createFileEditorPane(); |
| 583 | } |
| 584 | |
| 585 | return this.fileEditorPane; |
| 586 | } |
| 587 | |
| 588 | private HTMLPreviewPane getPreviewPane() { |
| 589 | if( this.previewPane == null ) { |
| 590 | this.previewPane = createPreviewPane(); |
| 591 | } |
| 592 | |
| 593 | return this.previewPane; |
| 594 | } |
| 595 | |
| 596 | private void setDefinitionSource( final DefinitionSource definitionSource ) { |
| 597 | this.definitionSource = definitionSource; |
| 598 | } |
| 599 | |
| 600 | private DefinitionSource getDefinitionSource() { |
| 601 | if( this.definitionSource == null ) { |
| 602 | this.definitionSource = new EmptyDefinitionSource(); |
| 603 | } |
| 604 | |
| 605 | return this.definitionSource; |
| 606 | } |
| 607 | |
| 608 | private DefinitionPane getDefinitionPane() { |
| 609 | if( this.definitionPane == null ) { |
| 610 | this.definitionPane = createDefinitionPane(); |
| 611 | } |
| 612 | |
| 613 | return this.definitionPane; |
| 614 | } |
| 615 | |
| 616 | private Options getOptions() { |
| 617 | return this.options; |
| 618 | } |
| 619 | |
| 620 | private Snitch getSnitch() { |
| 621 | return this.snitch; |
| 622 | } |
| 623 | |
| 624 | private Notifier getNotifier() { |
| 625 | return this.notifier; |
| 626 | } |
| 627 | |
| 628 | public void setMenuBar( final MenuBar menuBar ) { |
| 629 | this.menuBar = menuBar; |
| 630 | } |
| 631 | |
| 632 | public MenuBar getMenuBar() { |
| 633 | return this.menuBar; |
| 634 | } |
| 635 | |
| 636 | private Text getLineNumberText() { |
| 637 | if( this.lineNumberText == null ) { |
| 638 | this.lineNumberText = createLineNumberText(); |
| 639 | } |
| 640 | |
| 641 | return this.lineNumberText; |
| 642 | } |
| 643 | |
| 644 | private synchronized StatusBar getStatusBar() { |
| 645 | if( this.statusBar == null ) { |
| 646 | this.statusBar = createStatusBar(); |
| 647 | } |
| 648 | |
| 649 | return this.statusBar; |
| 650 | } |
| 651 | |
| 652 | //---- Member creators ---------------------------------------------------- |
| 653 | /** |
| 654 | * Factory to create processors that are suited to different file types. |
| 655 | * |
| 656 | * @param tab The tab that is subjected to processing. |
| 657 | * |
| 658 | * @return A processor suited to the file type specified by the tab's path. |
| 659 | */ |
| 660 | private Processor<String> createProcessor( final FileEditorTab tab ) { |
| 661 | return createProcessorFactory().createProcessor( tab ); |
| 662 | } |
| 663 | |
| 664 | private ProcessorFactory createProcessorFactory() { |
| 665 | return new ProcessorFactory( getPreviewPane(), getResolvedMap() ); |
| 666 | } |
| 667 | |
| 668 | private DefinitionSource createDefinitionSource( final String path ) { |
| 669 | final DefinitionSource ds |
| 670 | = createDefinitionFactory().createDefinitionSource( path ); |
| 671 | |
| 672 | if( ds instanceof FileDefinitionSource ) { |
| 673 | try { |
| 674 | getSnitch().listen( ((FileDefinitionSource)ds).getPath() ); |
| 675 | } catch( final IOException ex ) { |
| 676 | error( ex ); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return ds; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Create an editor pane to hold file editor tabs. |
| 685 | * |
| 686 | * @return A new instance, never null. |
| 687 | */ |
| 688 | private FileEditorTabPane createFileEditorPane() { |
| 689 | return new FileEditorTabPane(); |
| 690 | } |
| 691 | |
| 692 | private HTMLPreviewPane createPreviewPane() { |
| 693 | return new HTMLPreviewPane(); |
| 694 | } |
| 695 | |
| 696 | private DefinitionPane createDefinitionPane() { |
| 697 | return new DefinitionPane( getTreeView() ); |
| 698 | } |
| 699 | |
| 700 | private DefinitionFactory createDefinitionFactory() { |
| 701 | return new DefinitionFactory(); |
| 702 | } |
| 703 | |
| 704 | private StatusBar createStatusBar() { |
| 705 | return new StatusBar(); |
| 706 | } |
| 707 | |
| 708 | private Scene createScene() { |
| 709 | final SplitPane splitPane = new SplitPane( |
| 710 | getDefinitionPane().getNode(), |
| 711 | getFileEditorPane().getNode(), |
| 712 | getPreviewPane().getNode() ); |
| 713 | |
| 714 | splitPane.setDividerPositions( |
| 715 | getFloat( K_PANE_SPLIT_DEFINITION, .10f ), |
| 716 | getFloat( K_PANE_SPLIT_EDITOR, .45f ), |
| 717 | getFloat( K_PANE_SPLIT_PREVIEW, .45f ) ); |
| 718 | |
| 719 | // See: http://broadlyapplicable.blogspot.ca/2015/03/javafx-capture-restorePreferences-splitpane.html |
| 720 | final BorderPane borderPane = new BorderPane(); |
| 721 | borderPane.setPrefSize( 1024, 800 ); |
| 722 | borderPane.setTop( createMenuBar() ); |
| 723 | borderPane.setBottom( getStatusBar() ); |
| 724 | borderPane.setCenter( splitPane ); |
| 725 | |
| 726 | final VBox box = new VBox(); |
| 727 | box.setAlignment( Pos.BASELINE_CENTER ); |
| 728 | box.getChildren().add( getLineNumberText() ); |
| 729 | getStatusBar().getRightItems().add( box ); |
| 730 | |
| 731 | return new Scene( borderPane ); |
| 732 | } |
| 733 | |
| 734 | private Text createLineNumberText() { |
| 735 | return new Text( get( STATUS_BAR_LINE, 1, 1, 1 ) ); |
| 736 | } |
| 737 | |
| 738 | private Node createMenuBar() { |
| 739 | final BooleanBinding activeFileEditorIsNull = getFileEditorPane().activeFileEditorProperty().isNull(); |
| 740 | |
| 741 | // File actions |
| 742 | Action fileNewAction = new Action( get( "Main.menu.file.new" ), "Shortcut+N", FILE_ALT, e -> fileNew() ); |
| 743 | Action fileOpenAction = new Action( get( "Main.menu.file.open" ), "Shortcut+O", FOLDER_OPEN_ALT, e -> fileOpen() ); |
| 744 | Action fileCloseAction = new Action( get( "Main.menu.file.close" ), "Shortcut+W", null, e -> fileClose(), activeFileEditorIsNull ); |
| 745 | Action fileCloseAllAction = new Action( get( "Main.menu.file.close_all" ), null, null, e -> fileCloseAll(), activeFileEditorIsNull ); |
| 746 | Action fileSaveAction = new Action( get( "Main.menu.file.save" ), "Shortcut+S", FLOPPY_ALT, e -> fileSave(), |
| 747 | createActiveBooleanProperty( FileEditorTab::modifiedProperty ).not() ); |
| 748 | Action fileSaveAllAction = new Action( get( "Main.menu.file.save_all" ), "Shortcut+Shift+S", null, e -> fileSaveAll(), |
| 749 | Bindings.not( getFileEditorPane().anyFileEditorModifiedProperty() ) ); |
| 750 | Action fileExitAction = new Action( get( "Main.menu.file.exit" ), null, null, e -> fileExit() ); |
| 751 | |
| 752 | // Edit actions |
| 753 | Action editUndoAction = new Action( get( "Main.menu.edit.undo" ), "Shortcut+Z", UNDO, |
| 754 | e -> getActiveEditor().undo(), |
| 755 | createActiveBooleanProperty( FileEditorTab::canUndoProperty ).not() ); |
| 756 | Action editRedoAction = new Action( get( "Main.menu.edit.redo" ), "Shortcut+Y", REPEAT, |
| 757 | e -> getActiveEditor().redo(), |
| 758 | createActiveBooleanProperty( FileEditorTab::canRedoProperty ).not() ); |
| 759 | Action editFindAction = new Action( Messages.get( "Main.menu.edit.find" ), "Ctrl+F", SEARCH, |
| 760 | e -> find(), |
| 761 | activeFileEditorIsNull ); |
| 762 | Action editReplaceAction = new Action( Messages.get( "Main.menu.edit.find.replace" ), "Shortcut+H", RETWEET, |
| 763 | e -> getActiveEditor().replace(), |
| 764 | activeFileEditorIsNull ); |
| 765 | Action editFindNextAction = new Action( Messages.get( "Main.menu.edit.find.next" ), "F3", null, |
| 766 | e -> findNext(), |
| 672 | 767 | activeFileEditorIsNull ); |
| 673 | 768 | Action editFindPreviousAction = new Action( Messages.get( "Main.menu.edit.find.previous" ), "Shift+F3", null, |