| 12 | 12 | import com.keenwrite.events.CaretMovedEvent; |
| 13 | 13 | import com.keenwrite.events.ExportFailedEvent; |
| 14 | | import com.keenwrite.preferences.Key; |
| 15 | | import com.keenwrite.preferences.PreferencesController; |
| 16 | | import com.keenwrite.preferences.Workspace; |
| 17 | | import com.keenwrite.processors.markdown.MarkdownProcessor; |
| 18 | | import com.keenwrite.search.SearchModel; |
| 19 | | import com.keenwrite.typesetting.Typesetter; |
| 20 | | import com.keenwrite.ui.controls.SearchBar; |
| 21 | | import com.keenwrite.ui.dialogs.ExportDialog; |
| 22 | | import com.keenwrite.ui.dialogs.ExportSettings; |
| 23 | | import com.keenwrite.ui.dialogs.ImageDialog; |
| 24 | | import com.keenwrite.ui.dialogs.LinkDialog; |
| 25 | | import com.keenwrite.ui.explorer.FilePicker; |
| 26 | | import com.keenwrite.ui.explorer.FilePickerFactory; |
| 27 | | import com.keenwrite.ui.logging.LogView; |
| 28 | | import com.vladsch.flexmark.ast.Link; |
| 29 | | import javafx.concurrent.Service; |
| 30 | | import javafx.concurrent.Task; |
| 31 | | import javafx.scene.control.Alert; |
| 32 | | import javafx.scene.control.Dialog; |
| 33 | | import javafx.stage.Window; |
| 34 | | import javafx.stage.WindowEvent; |
| 35 | | |
| 36 | | import java.io.File; |
| 37 | | import java.nio.file.Path; |
| 38 | | import java.util.List; |
| 39 | | import java.util.Optional; |
| 40 | | |
| 41 | | import static com.keenwrite.Bootstrap.*; |
| 42 | | import static com.keenwrite.ExportFormat.*; |
| 43 | | import static com.keenwrite.Messages.get; |
| 44 | | import static com.keenwrite.constants.Constants.PDF_DEFAULT; |
| 45 | | import static com.keenwrite.constants.Constants.USER_DIRECTORY; |
| 46 | | import static com.keenwrite.constants.GraphicsConstants.ICON_DIALOG_NODE; |
| 47 | | import static com.keenwrite.events.StatusEvent.clue; |
| 48 | | import static com.keenwrite.preferences.AppKeys.*; |
| 49 | | import static com.keenwrite.processors.ProcessorFactory.createProcessors; |
| 50 | | import static com.keenwrite.ui.explorer.FilePickerFactory.SelectionType; |
| 51 | | import static com.keenwrite.ui.explorer.FilePickerFactory.SelectionType.*; |
| 52 | | import static java.nio.file.Files.writeString; |
| 53 | | import static javafx.application.Platform.runLater; |
| 54 | | import static javafx.event.Event.fireEvent; |
| 55 | | import static javafx.scene.control.Alert.AlertType.INFORMATION; |
| 56 | | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 57 | | import static org.apache.commons.io.FilenameUtils.getExtension; |
| 58 | | |
| 59 | | /** |
| 60 | | * Responsible for abstracting how functionality is mapped to the application. |
| 61 | | * This allows users to customize accelerator keys and will provide pluggable |
| 62 | | * functionality so that different text markup languages can change documents |
| 63 | | * using their respective syntax. |
| 64 | | */ |
| 65 | | public final class GuiCommands { |
| 66 | | private static final String STYLE_SEARCH = "search"; |
| 67 | | |
| 68 | | /** |
| 69 | | * When an action is executed, this is one of the recipients. |
| 70 | | */ |
| 71 | | private final MainPane mMainPane; |
| 72 | | |
| 73 | | private final MainScene mMainScene; |
| 74 | | |
| 75 | | private final LogView mLogView; |
| 76 | | |
| 77 | | /** |
| 78 | | * Tracks finding text in the active document. |
| 79 | | */ |
| 80 | | private final SearchModel mSearchModel; |
| 81 | | |
| 82 | | private boolean mCanTypeset; |
| 83 | | |
| 84 | | /** |
| 85 | | * A {@link Task} can only be run once, so wrap it in a {@link Service} to |
| 86 | | * allow re-launching the typesetting task repeatedly. |
| 87 | | */ |
| 88 | | private Service<Path> mTypesetService; |
| 89 | | |
| 90 | | /** |
| 91 | | * Prevent a race-condition between checking to see if the typesetting task |
| 92 | | * is running and restarting the task itself. |
| 93 | | */ |
| 94 | | private final Object mMutex = new Object(); |
| 95 | | |
| 96 | | public GuiCommands( final MainScene scene, final MainPane pane ) { |
| 97 | | mMainScene = scene; |
| 98 | | mMainPane = pane; |
| 99 | | mLogView = new LogView(); |
| 100 | | mSearchModel = new SearchModel(); |
| 101 | | mSearchModel.matchOffsetProperty().addListener( ( c, o, n ) -> { |
| 102 | | final var editor = getActiveTextEditor(); |
| 103 | | |
| 104 | | // Clear highlighted areas before highlighting a new region. |
| 105 | | if( o != null ) { |
| 106 | | editor.unstylize( STYLE_SEARCH ); |
| 107 | | } |
| 108 | | |
| 109 | | if( n != null ) { |
| 110 | | editor.moveTo( n.getStart() ); |
| 111 | | editor.stylize( n, STYLE_SEARCH ); |
| 112 | | } |
| 113 | | } ); |
| 114 | | |
| 115 | | // When the active text editor changes ... |
| 116 | | mMainPane.textEditorProperty().addListener( |
| 117 | | ( c, o, n ) -> { |
| 118 | | // ... update the haystack. |
| 119 | | mSearchModel.search( getActiveTextEditor().getText() ); |
| 120 | | |
| 121 | | // ... update the status bar with the current caret position. |
| 122 | | if( n != null ) { |
| 123 | | final var w = getWorkspace(); |
| 124 | | final var recentDoc = w.fileProperty( KEY_UI_RECENT_DOCUMENT ); |
| 125 | | |
| 126 | | // ... preserve the most recent document. |
| 127 | | recentDoc.setValue( n.getFile() ); |
| 128 | | CaretMovedEvent.fire( n.getCaret() ); |
| 129 | | } |
| 130 | | } |
| 131 | | ); |
| 132 | | } |
| 133 | | |
| 134 | | public void file_new() { |
| 135 | | getMainPane().newTextEditor(); |
| 136 | | } |
| 137 | | |
| 138 | | public void file_open() { |
| 139 | | pickFiles( FILE_OPEN_MULTIPLE ).ifPresent( l -> getMainPane().open( l ) ); |
| 140 | | } |
| 141 | | |
| 142 | | public void file_close() { |
| 143 | | getMainPane().close(); |
| 144 | | } |
| 145 | | |
| 146 | | public void file_close_all() { |
| 147 | | getMainPane().closeAll(); |
| 148 | | } |
| 149 | | |
| 150 | | public void file_save() { |
| 151 | | getMainPane().save(); |
| 152 | | } |
| 153 | | |
| 154 | | public void file_save_as() { |
| 155 | | pickFiles( FILE_SAVE_AS ).ifPresent( l -> getMainPane().saveAs( l ) ); |
| 156 | | } |
| 157 | | |
| 158 | | public void file_save_all() { |
| 159 | | getMainPane().saveAll(); |
| 160 | | } |
| 161 | | |
| 162 | | /** |
| 163 | | * Converts the actively edited file in the given file format. |
| 164 | | * |
| 165 | | * @param format The destination file format. |
| 166 | | */ |
| 167 | | private void file_export( final ExportFormat format ) { |
| 168 | | file_export( format, false ); |
| 169 | | } |
| 170 | | |
| 171 | | /** |
| 172 | | * Converts one or more files into the given file format. If {@code dir} |
| 173 | | * is set to true, this will first append all files in the same directory |
| 174 | | * as the actively edited file. |
| 175 | | * |
| 176 | | * @param format The destination file format. |
| 177 | | * @param dir Export all files in the actively edited file's directory. |
| 178 | | */ |
| 179 | | private void file_export( final ExportFormat format, final boolean dir ) { |
| 180 | | final var editor = getMainPane().getTextEditor(); |
| 181 | | final var exported = getWorkspace().fileProperty( KEY_UI_RECENT_EXPORT ); |
| 182 | | final var exportParent = exported.get().toPath().getParent(); |
| 183 | | final var editorParent = editor.getPath().getParent(); |
| 184 | | final var userHomeParent = USER_DIRECTORY.toPath(); |
| 185 | | final var exportPath = exportParent != null |
| 186 | | ? exportParent |
| 187 | | : editorParent != null |
| 188 | | ? editorParent |
| 189 | | : userHomeParent; |
| 190 | | |
| 191 | | final var filename = format.toExportFilename( editor.getPath() ); |
| 192 | | final var selected = PDF_DEFAULT |
| 193 | | .getName() |
| 194 | | .equals( exported.get().getName() ); |
| 195 | | final var selection = pickFile( |
| 196 | | selected |
| 197 | | ? filename |
| 198 | | : exported.get(), |
| 199 | | exportPath, |
| 200 | | FILE_EXPORT |
| 201 | | ); |
| 202 | | |
| 203 | | selection.ifPresent( files -> file_export( editor, format, files, dir ) ); |
| 204 | | } |
| 205 | | |
| 206 | | private void file_export( |
| 207 | | final TextEditor editor, |
| 208 | | final ExportFormat format, |
| 209 | | final List<File> files, |
| 210 | | final boolean dir ) { |
| 211 | | editor.save(); |
| 212 | | final var main = getMainPane(); |
| 213 | | final var exported = getWorkspace().fileProperty( KEY_UI_RECENT_EXPORT ); |
| 214 | | |
| 215 | | final var sourceFile = files.get( 0 ); |
| 216 | | final var sourcePath = sourceFile.toPath(); |
| 217 | | final var document = dir ? append( editor ) : editor.getText(); |
| 218 | | final var context = main.createProcessorContext( sourcePath, format ); |
| 219 | | |
| 220 | | final var service = new Service<Path>() { |
| 221 | | @Override |
| 222 | | protected Task<Path> createTask() { |
| 223 | | final var task = new Task<Path>() { |
| 224 | | @Override |
| 225 | | protected Path call() throws Exception { |
| 226 | | final var chain = createProcessors( context ); |
| 227 | | final var export = chain.apply( document ); |
| 228 | | |
| 229 | | // Processors can export binary files. In such cases, processors |
| 230 | | // return null to prevent further processing. |
| 231 | | return export == null ? null : writeString( sourcePath, export ); |
| 232 | | } |
| 233 | | }; |
| 234 | | |
| 235 | | task.setOnSucceeded( |
| 236 | | e -> { |
| 237 | | // Remember the exported file name for next time. |
| 238 | | exported.setValue( sourceFile ); |
| 239 | | |
| 240 | | final var result = task.getValue(); |
| 241 | | |
| 242 | | // Binary formats must notify users of success independently. |
| 243 | | if( result != null ) { |
| 244 | | clue( "Main.status.export.success", result ); |
| 245 | | } |
| 246 | | } |
| 247 | | ); |
| 248 | | |
| 249 | | task.setOnFailed( e -> { |
| 250 | | final var ex = task.getException(); |
| 251 | | clue( ex ); |
| 252 | | |
| 253 | | if( ex instanceof TypeNotPresentException ) { |
| 254 | | fireExportFailedEvent(); |
| 255 | | } |
| 256 | | } ); |
| 257 | | |
| 258 | | return task; |
| 259 | | } |
| 260 | | }; |
| 261 | | |
| 262 | | mTypesetService = service; |
| 263 | | typeset( service ); |
| 264 | | } |
| 265 | | |
| 266 | | /** |
| 267 | | * @param dir {@code true} means to export all files in the active file |
| 268 | | * editor's directory; {@code false} means to export only the |
| 269 | | * actively edited file. |
| 270 | | */ |
| 271 | | private void file_export_pdf( final boolean dir ) { |
| 272 | | final var workspace = getWorkspace(); |
| 273 | | final var themes = workspace.getFile( |
| 274 | | KEY_TYPESET_CONTEXT_THEMES_PATH |
| 275 | | ); |
| 276 | | final var theme = workspace.stringProperty( |
| 277 | | KEY_TYPESET_CONTEXT_THEME_SELECTION |
| 278 | | ); |
| 279 | | final var chapters = workspace.stringProperty( |
| 280 | | KEY_TYPESET_CONTEXT_CHAPTERS |
| 281 | | ); |
| 282 | | final var settings = ExportSettings |
| 283 | | .builder() |
| 284 | | .with( ExportSettings.Mutator::setTheme, theme ) |
| 285 | | .with( ExportSettings.Mutator::setChapters, chapters ) |
| 286 | | .build(); |
| 287 | | |
| 288 | | // Don't re-validate the typesetter installation each time. If the |
| 289 | | // user mucks up the typesetter installation, it'll get caught the |
| 290 | | // next time the application is started. Don't use |= because it |
| 291 | | // won't short-circuit. |
| 292 | | mCanTypeset = mCanTypeset || Typesetter.canRun(); |
| 293 | | |
| 294 | | if( mCanTypeset ) { |
| 295 | | // If the typesetter is installed, allow the user to select a theme. If |
| 296 | | // the themes aren't installed, a status message will appear. |
| 297 | | if( ExportDialog.choose( getWindow(), themes, settings, dir ) ) { |
| 298 | | file_export( APPLICATION_PDF, dir ); |
| 299 | | } |
| 300 | | } |
| 301 | | else { |
| 302 | | fireExportFailedEvent(); |
| 303 | | } |
| 304 | | } |
| 305 | | |
| 306 | | public void file_export_pdf() { |
| 307 | | file_export_pdf( false ); |
| 308 | | } |
| 309 | | |
| 310 | | public void file_export_pdf_dir() { |
| 311 | | file_export_pdf( true ); |
| 312 | | } |
| 313 | | |
| 314 | | public void file_export_html_dir() { |
| 315 | | file_export( XHTML_TEX, true ); |
| 316 | | } |
| 317 | | |
| 318 | | public void file_export_repeat() { |
| 319 | | typeset( mTypesetService ); |
| 320 | | } |
| 321 | | |
| 322 | | public void file_export_html_svg() { |
| 323 | | file_export( HTML_TEX_SVG ); |
| 324 | | } |
| 325 | | |
| 326 | | public void file_export_html_tex() { |
| 327 | | file_export( HTML_TEX_DELIMITED ); |
| 328 | | } |
| 329 | | |
| 330 | | public void file_export_xhtml_tex() { |
| 331 | | file_export( XHTML_TEX ); |
| 332 | | } |
| 333 | | |
| 334 | | private void fireExportFailedEvent() { |
| 335 | | runLater( ExportFailedEvent::fire ); |
| 336 | | } |
| 337 | | |
| 338 | | public void file_exit() { |
| 339 | | final var window = getWindow(); |
| 340 | | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 341 | | } |
| 342 | | |
| 343 | | public void edit_undo() { |
| 344 | | getActiveTextEditor().undo(); |
| 345 | | } |
| 346 | | |
| 347 | | public void edit_redo() { |
| 348 | | getActiveTextEditor().redo(); |
| 349 | | } |
| 350 | | |
| 351 | | public void edit_cut() { |
| 352 | | getActiveTextEditor().cut(); |
| 353 | | } |
| 354 | | |
| 355 | | public void edit_copy() { |
| 356 | | getActiveTextEditor().copy(); |
| 357 | | } |
| 358 | | |
| 359 | | public void edit_paste() { |
| 360 | | getActiveTextEditor().paste(); |
| 361 | | } |
| 362 | | |
| 363 | | public void edit_select_all() { |
| 364 | | getActiveTextEditor().selectAll(); |
| 365 | | } |
| 366 | | |
| 367 | | public void edit_find() { |
| 368 | | final var nodes = getMainScene().getStatusBar().getLeftItems(); |
| 369 | | |
| 370 | | if( nodes.isEmpty() ) { |
| 371 | | final var searchBar = new SearchBar(); |
| 372 | | |
| 373 | | searchBar.matchIndexProperty().bind( mSearchModel.matchIndexProperty() ); |
| 374 | | searchBar.matchCountProperty().bind( mSearchModel.matchCountProperty() ); |
| 375 | | |
| 376 | | searchBar.setOnCancelAction( event -> { |
| 377 | | final var editor = getActiveTextEditor(); |
| 378 | | nodes.remove( searchBar ); |
| 379 | | editor.unstylize( STYLE_SEARCH ); |
| 380 | | editor.getNode().requestFocus(); |
| 381 | | } ); |
| 382 | | |
| 383 | | searchBar.addInputListener( ( c, o, n ) -> { |
| 384 | | if( n != null && !n.isEmpty() ) { |
| 385 | | mSearchModel.search( n, getActiveTextEditor().getText() ); |
| 386 | | } |
| 387 | | } ); |
| 388 | | |
| 389 | | searchBar.setOnNextAction( event -> edit_find_next() ); |
| 390 | | searchBar.setOnPrevAction( event -> edit_find_prev() ); |
| 391 | | |
| 392 | | nodes.add( searchBar ); |
| 393 | | searchBar.requestFocus(); |
| 394 | | } |
| 395 | | } |
| 396 | | |
| 397 | | public void edit_find_next() { |
| 398 | | mSearchModel.advance(); |
| 399 | | } |
| 400 | | |
| 401 | | public void edit_find_prev() { |
| 402 | | mSearchModel.retreat(); |
| 403 | | } |
| 404 | | |
| 405 | | public void edit_preferences() { |
| 406 | | try { |
| 407 | | new PreferencesController( getWorkspace() ).show(); |
| 408 | | } catch( final Exception ex ) { |
| 409 | | clue( ex ); |
| 410 | | } |
| 411 | | } |
| 412 | | |
| 413 | | public void format_bold() { |
| 414 | | getActiveTextEditor().bold(); |
| 415 | | } |
| 416 | | |
| 417 | | public void format_italic() { |
| 418 | | getActiveTextEditor().italic(); |
| 419 | | } |
| 420 | | |
| 421 | | public void format_monospace() { |
| 422 | | getActiveTextEditor().monospace(); |
| 423 | | } |
| 424 | | |
| 425 | | public void format_superscript() { |
| 426 | | getActiveTextEditor().superscript(); |
| 427 | | } |
| 428 | | |
| 429 | | public void format_subscript() { |
| 430 | | getActiveTextEditor().subscript(); |
| 431 | | } |
| 432 | | |
| 433 | | public void format_strikethrough() { |
| 434 | | getActiveTextEditor().strikethrough(); |
| 435 | | } |
| 436 | | |
| 437 | | public void insert_blockquote() { |
| 438 | | getActiveTextEditor().blockquote(); |
| 439 | | } |
| 440 | | |
| 441 | | public void insert_code() { |
| 442 | | getActiveTextEditor().code(); |
| 443 | | } |
| 444 | | |
| 445 | | public void insert_fenced_code_block() { |
| 446 | | getActiveTextEditor().fencedCodeBlock(); |
| 447 | | } |
| 448 | | |
| 449 | | public void insert_link() { |
| 450 | | insertObject( createLinkDialog() ); |
| 451 | | } |
| 452 | | |
| 453 | | public void insert_image() { |
| 454 | | insertObject( createImageDialog() ); |
| 455 | | } |
| 456 | | |
| 457 | | private void insertObject( final Dialog<String> dialog ) { |
| 458 | | final var textArea = getActiveTextEditor().getTextArea(); |
| 459 | | dialog.showAndWait().ifPresent( textArea::replaceSelection ); |
| 460 | | } |
| 461 | | |
| 462 | | private Dialog<String> createLinkDialog() { |
| 463 | | return new LinkDialog( getWindow(), createHyperlinkModel() ); |
| 464 | | } |
| 465 | | |
| 466 | | private Dialog<String> createImageDialog() { |
| 467 | | final var path = getActiveTextEditor().getPath(); |
| 468 | | final var parentDir = path.getParent(); |
| 469 | | return new ImageDialog( getWindow(), parentDir ); |
| 470 | | } |
| 471 | | |
| 472 | | /** |
| 473 | | * Returns one of: selected text, word under cursor, or parsed hyperlink from |
| 474 | | * the Markdown AST. |
| 475 | | * |
| 476 | | * @return An instance containing the link URL and display text. |
| 477 | | */ |
| 478 | | private HyperlinkModel createHyperlinkModel() { |
| 479 | | final var context = getMainPane().createProcessorContext(); |
| 480 | | final var editor = getActiveTextEditor(); |
| 481 | | final var textArea = editor.getTextArea(); |
| 482 | | final var selectedText = textArea.getSelectedText(); |
| 483 | | |
| 484 | | // Convert current paragraph to Markdown nodes. |
| 485 | | final var mp = MarkdownProcessor.create( context ); |
| 486 | | final var p = textArea.getCurrentParagraph(); |
| 487 | | final var paragraph = textArea.getText( p ); |
| 488 | | final var node = mp.toNode( paragraph ); |
| 489 | | final var visitor = new LinkVisitor( textArea.getCaretColumn() ); |
| 490 | | final var link = visitor.process( node ); |
| 491 | | |
| 492 | | if( link != null ) { |
| 493 | | textArea.selectRange( p, link.getStartOffset(), p, link.getEndOffset() ); |
| 494 | | } |
| 495 | | |
| 496 | | return createHyperlinkModel( link, selectedText ); |
| 497 | | } |
| 498 | | |
| 499 | | private HyperlinkModel createHyperlinkModel( |
| 500 | | final Link link, final String selection ) { |
| 501 | | |
| 502 | | return link == null |
| 503 | | ? new HyperlinkModel( selection, "https://localhost" ) |
| 504 | | : new HyperlinkModel( link ); |
| 505 | | } |
| 506 | | |
| 507 | | public void insert_heading_1() { |
| 508 | | insert_heading( 1 ); |
| 509 | | } |
| 510 | | |
| 511 | | public void insert_heading_2() { |
| 512 | | insert_heading( 2 ); |
| 513 | | } |
| 514 | | |
| 515 | | public void insert_heading_3() { |
| 516 | | insert_heading( 3 ); |
| 517 | | } |
| 518 | | |
| 519 | | private void insert_heading( final int level ) { |
| 520 | | getActiveTextEditor().heading( level ); |
| 521 | | } |
| 522 | | |
| 523 | | public void insert_unordered_list() { |
| 524 | | getActiveTextEditor().unorderedList(); |
| 525 | | } |
| 526 | | |
| 527 | | public void insert_ordered_list() { |
| 528 | | getActiveTextEditor().orderedList(); |
| 529 | | } |
| 530 | | |
| 531 | | public void insert_horizontal_rule() { |
| 532 | | getActiveTextEditor().horizontalRule(); |
| 533 | | } |
| 534 | | |
| 535 | | public void definition_create() { |
| 536 | | getActiveTextDefinition().createDefinition(); |
| 537 | | } |
| 538 | | |
| 539 | | public void definition_rename() { |
| 540 | | getActiveTextDefinition().renameDefinition(); |
| 541 | | } |
| 542 | | |
| 543 | | public void definition_delete() { |
| 544 | | getActiveTextDefinition().deleteDefinitions(); |
| 545 | | } |
| 546 | | |
| 547 | | public void definition_autoinsert() { |
| 548 | | getMainPane().autoinsert(); |
| 549 | | } |
| 550 | | |
| 551 | | public void view_refresh() { |
| 552 | | getMainPane().viewRefresh(); |
| 553 | | } |
| 554 | | |
| 555 | | public void view_preview() { |
| 556 | | getMainPane().viewPreview(); |
| 557 | | } |
| 558 | | |
| 559 | | public void view_outline() { |
| 560 | | getMainPane().viewOutline(); |
| 561 | | } |
| 562 | | |
| 563 | | public void view_files() { getMainPane().viewFiles(); } |
| 564 | | |
| 565 | | public void view_statistics() { |
| 566 | | getMainPane().viewStatistics(); |
| 567 | | } |
| 568 | | |
| 569 | | public void view_menubar() { |
| 570 | | getMainScene().toggleMenuBar(); |
| 571 | | } |
| 572 | | |
| 573 | | public void view_toolbar() { |
| 574 | | getMainScene().toggleToolBar(); |
| 575 | | } |
| 576 | | |
| 577 | | public void view_statusbar() { |
| 578 | | getMainScene().toggleStatusBar(); |
| 579 | | } |
| 580 | | |
| 581 | | public void view_log() { |
| 582 | | mLogView.view(); |
| 583 | | } |
| 584 | | |
| 585 | | public void help_about() { |
| 586 | | final var alert = new Alert( INFORMATION ); |
| 587 | | final var prefix = "Dialog.about."; |
| 588 | | alert.setTitle( get( prefix + "title", APP_TITLE ) ); |
| 589 | | alert.setHeaderText( get( prefix + "header", APP_TITLE ) ); |
| 590 | | alert.setContentText( get( prefix + "content", APP_YEAR, APP_VERSION ) ); |
| 591 | | alert.setGraphic( ICON_DIALOG_NODE ); |
| 592 | | alert.initOwner( getWindow() ); |
| 593 | | alert.showAndWait(); |
| 594 | | } |
| 595 | | |
| 596 | | private <T> void typeset( final Service<T> service ) { |
| 597 | | synchronized( mMutex ) { |
| 598 | | if( service != null && !service.isRunning() ) { |
| 599 | | service.reset(); |
| 600 | | service.start(); |
| 601 | | } |
| 602 | | } |
| 603 | | } |
| 604 | | |
| 605 | | /** |
| 606 | | * Concatenates all the files in the same directory as the given file into |
| 607 | | * a string. The extension is determined by the given file name pattern; the |
| 608 | | * order files are concatenated is based on their numeric sort order (this |
| 609 | | * avoids lexicographic sorting). |
| 610 | | * <p> |
| 611 | | * If the parent path to the file being edited in the text editor cannot |
| 612 | | * be found then this will return the editor's text, without iterating through |
| 613 | | * the parent directory. (Should never happen, but who knows?) |
| 614 | | * </p> |
| 615 | | * <p> |
| 616 | | * New lines are automatically appended to separate each file. |
| 617 | | * </p> |
| 618 | | * |
| 619 | | * @param editor The text editor containing |
| 620 | | * @return All files in the same directory as the file being edited |
| 621 | | * concatenated into a single string. |
| 622 | | */ |
| 623 | | private String append( final TextEditor editor ) { |
| 624 | | final var pattern = editor.getPath(); |
| 625 | | final var parent = pattern.getParent(); |
| 626 | | |
| 627 | | // Short-circuit because nothing else can be done. |
| 628 | | if( parent == null ) { |
| 629 | | clue( "Main.status.export.concat.parent", pattern ); |
| 630 | | return editor.getText(); |
| 631 | | } |
| 632 | | |
| 633 | | final var filename = pattern.getFileName().toString(); |
| 14 | import com.keenwrite.io.SysFile; |
| 15 | import com.keenwrite.preferences.Key; |
| 16 | import com.keenwrite.preferences.PreferencesController; |
| 17 | import com.keenwrite.preferences.Workspace; |
| 18 | import com.keenwrite.processors.markdown.MarkdownProcessor; |
| 19 | import com.keenwrite.search.SearchModel; |
| 20 | import com.keenwrite.typesetting.Typesetter; |
| 21 | import com.keenwrite.ui.controls.SearchBar; |
| 22 | import com.keenwrite.ui.dialogs.ExportDialog; |
| 23 | import com.keenwrite.ui.dialogs.ExportSettings; |
| 24 | import com.keenwrite.ui.dialogs.ImageDialog; |
| 25 | import com.keenwrite.ui.dialogs.LinkDialog; |
| 26 | import com.keenwrite.ui.explorer.FilePicker; |
| 27 | import com.keenwrite.ui.explorer.FilePickerFactory; |
| 28 | import com.keenwrite.ui.logging.LogView; |
| 29 | import com.vladsch.flexmark.ast.Link; |
| 30 | import javafx.concurrent.Service; |
| 31 | import javafx.concurrent.Task; |
| 32 | import javafx.scene.control.Alert; |
| 33 | import javafx.scene.control.Dialog; |
| 34 | import javafx.stage.Window; |
| 35 | import javafx.stage.WindowEvent; |
| 36 | |
| 37 | import java.io.File; |
| 38 | import java.nio.file.Path; |
| 39 | import java.util.List; |
| 40 | import java.util.Optional; |
| 41 | |
| 42 | import static com.keenwrite.Bootstrap.*; |
| 43 | import static com.keenwrite.ExportFormat.*; |
| 44 | import static com.keenwrite.Messages.get; |
| 45 | import static com.keenwrite.constants.Constants.PDF_DEFAULT; |
| 46 | import static com.keenwrite.constants.Constants.USER_DIRECTORY; |
| 47 | import static com.keenwrite.constants.GraphicsConstants.ICON_DIALOG_NODE; |
| 48 | import static com.keenwrite.events.StatusEvent.clue; |
| 49 | import static com.keenwrite.preferences.AppKeys.*; |
| 50 | import static com.keenwrite.processors.ProcessorFactory.createProcessors; |
| 51 | import static com.keenwrite.ui.explorer.FilePickerFactory.SelectionType; |
| 52 | import static com.keenwrite.ui.explorer.FilePickerFactory.SelectionType.*; |
| 53 | import static java.nio.file.Files.writeString; |
| 54 | import static javafx.application.Platform.runLater; |
| 55 | import static javafx.event.Event.fireEvent; |
| 56 | import static javafx.scene.control.Alert.AlertType.INFORMATION; |
| 57 | import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| 58 | import static org.apache.commons.io.FilenameUtils.getExtension; |
| 59 | |
| 60 | /** |
| 61 | * Responsible for abstracting how functionality is mapped to the application. |
| 62 | * This allows users to customize accelerator keys and will provide pluggable |
| 63 | * functionality so that different text markup languages can change documents |
| 64 | * using their respective syntax. |
| 65 | */ |
| 66 | public final class GuiCommands { |
| 67 | private static final String STYLE_SEARCH = "search"; |
| 68 | |
| 69 | /** |
| 70 | * When an action is executed, this is one of the recipients. |
| 71 | */ |
| 72 | private final MainPane mMainPane; |
| 73 | |
| 74 | private final MainScene mMainScene; |
| 75 | |
| 76 | private final LogView mLogView; |
| 77 | |
| 78 | /** |
| 79 | * Tracks finding text in the active document. |
| 80 | */ |
| 81 | private final SearchModel mSearchModel; |
| 82 | |
| 83 | private boolean mCanTypeset; |
| 84 | |
| 85 | /** |
| 86 | * A {@link Task} can only be run once, so wrap it in a {@link Service} to |
| 87 | * allow re-launching the typesetting task repeatedly. |
| 88 | */ |
| 89 | private Service<Path> mTypesetService; |
| 90 | |
| 91 | /** |
| 92 | * Prevent a race-condition between checking to see if the typesetting task |
| 93 | * is running and restarting the task itself. |
| 94 | */ |
| 95 | private final Object mMutex = new Object(); |
| 96 | |
| 97 | public GuiCommands( final MainScene scene, final MainPane pane ) { |
| 98 | mMainScene = scene; |
| 99 | mMainPane = pane; |
| 100 | mLogView = new LogView(); |
| 101 | mSearchModel = new SearchModel(); |
| 102 | mSearchModel.matchOffsetProperty().addListener( ( c, o, n ) -> { |
| 103 | final var editor = getActiveTextEditor(); |
| 104 | |
| 105 | // Clear highlighted areas before highlighting a new region. |
| 106 | if( o != null ) { |
| 107 | editor.unstylize( STYLE_SEARCH ); |
| 108 | } |
| 109 | |
| 110 | if( n != null ) { |
| 111 | editor.moveTo( n.getStart() ); |
| 112 | editor.stylize( n, STYLE_SEARCH ); |
| 113 | } |
| 114 | } ); |
| 115 | |
| 116 | // When the active text editor changes ... |
| 117 | mMainPane.textEditorProperty().addListener( |
| 118 | ( c, o, n ) -> { |
| 119 | // ... update the haystack. |
| 120 | mSearchModel.search( getActiveTextEditor().getText() ); |
| 121 | |
| 122 | // ... update the status bar with the current caret position. |
| 123 | if( n != null ) { |
| 124 | final var w = getWorkspace(); |
| 125 | final var recentDoc = w.fileProperty( KEY_UI_RECENT_DOCUMENT ); |
| 126 | |
| 127 | // ... preserve the most recent document. |
| 128 | recentDoc.setValue( n.getFile() ); |
| 129 | CaretMovedEvent.fire( n.getCaret() ); |
| 130 | } |
| 131 | } |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | public void file_new() { |
| 136 | getMainPane().newTextEditor(); |
| 137 | } |
| 138 | |
| 139 | public void file_open() { |
| 140 | pickFiles( FILE_OPEN_MULTIPLE ).ifPresent( l -> getMainPane().open( l ) ); |
| 141 | } |
| 142 | |
| 143 | public void file_close() { |
| 144 | getMainPane().close(); |
| 145 | } |
| 146 | |
| 147 | public void file_close_all() { |
| 148 | getMainPane().closeAll(); |
| 149 | } |
| 150 | |
| 151 | public void file_save() { |
| 152 | getMainPane().save(); |
| 153 | } |
| 154 | |
| 155 | public void file_save_as() { |
| 156 | pickFiles( FILE_SAVE_AS ).ifPresent( l -> getMainPane().saveAs( l ) ); |
| 157 | } |
| 158 | |
| 159 | public void file_save_all() { |
| 160 | getMainPane().saveAll(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Converts the actively edited file in the given file format. |
| 165 | * |
| 166 | * @param format The destination file format. |
| 167 | */ |
| 168 | private void file_export( final ExportFormat format ) { |
| 169 | file_export( format, false ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Converts one or more files into the given file format. If {@code dir} |
| 174 | * is set to true, this will first append all files in the same directory |
| 175 | * as the actively edited file. |
| 176 | * |
| 177 | * @param format The destination file format. |
| 178 | * @param dir Export all files in the actively edited file's directory. |
| 179 | */ |
| 180 | private void file_export( final ExportFormat format, final boolean dir ) { |
| 181 | final var editor = getMainPane().getTextEditor(); |
| 182 | final var exported = getWorkspace().fileProperty( KEY_UI_RECENT_EXPORT ); |
| 183 | final var exportParent = exported.get().toPath().getParent(); |
| 184 | final var editorParent = editor.getPath().getParent(); |
| 185 | final var userHomeParent = USER_DIRECTORY.toPath(); |
| 186 | final var exportPath = exportParent != null |
| 187 | ? exportParent |
| 188 | : editorParent != null |
| 189 | ? editorParent |
| 190 | : userHomeParent; |
| 191 | |
| 192 | final var filename = format.toExportFilename( editor.getPath() ); |
| 193 | final var selected = PDF_DEFAULT |
| 194 | .getName() |
| 195 | .equals( exported.get().getName() ); |
| 196 | final var selection = pickFile( |
| 197 | selected |
| 198 | ? filename |
| 199 | : exported.get(), |
| 200 | exportPath, |
| 201 | FILE_EXPORT |
| 202 | ); |
| 203 | |
| 204 | selection.ifPresent( files -> file_export( editor, format, files, dir ) ); |
| 205 | } |
| 206 | |
| 207 | private void file_export( |
| 208 | final TextEditor editor, |
| 209 | final ExportFormat format, |
| 210 | final List<File> files, |
| 211 | final boolean dir ) { |
| 212 | editor.save(); |
| 213 | final var main = getMainPane(); |
| 214 | final var exported = getWorkspace().fileProperty( KEY_UI_RECENT_EXPORT ); |
| 215 | |
| 216 | final var sourceFile = files.get( 0 ); |
| 217 | final var sourcePath = sourceFile.toPath(); |
| 218 | final var document = dir ? append( editor ) : editor.getText(); |
| 219 | final var context = main.createProcessorContext( sourcePath, format ); |
| 220 | |
| 221 | final var service = new Service<Path>() { |
| 222 | @Override |
| 223 | protected Task<Path> createTask() { |
| 224 | final var task = new Task<Path>() { |
| 225 | @Override |
| 226 | protected Path call() throws Exception { |
| 227 | final var chain = createProcessors( context ); |
| 228 | final var export = chain.apply( document ); |
| 229 | |
| 230 | // Processors can export binary files. In such cases, processors |
| 231 | // return null to prevent further processing. |
| 232 | return export == null ? null : writeString( sourcePath, export ); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | task.setOnSucceeded( |
| 237 | e -> { |
| 238 | // Remember the exported file name for next time. |
| 239 | exported.setValue( sourceFile ); |
| 240 | |
| 241 | final var result = task.getValue(); |
| 242 | |
| 243 | // Binary formats must notify users of success independently. |
| 244 | if( result != null ) { |
| 245 | clue( "Main.status.export.success", result ); |
| 246 | } |
| 247 | } |
| 248 | ); |
| 249 | |
| 250 | task.setOnFailed( e -> { |
| 251 | final var ex = task.getException(); |
| 252 | clue( ex ); |
| 253 | |
| 254 | if( ex instanceof TypeNotPresentException ) { |
| 255 | fireExportFailedEvent(); |
| 256 | } |
| 257 | } ); |
| 258 | |
| 259 | return task; |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | mTypesetService = service; |
| 264 | typeset( service ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @param dir {@code true} means to export all files in the active file |
| 269 | * editor's directory; {@code false} means to export only the |
| 270 | * actively edited file. |
| 271 | */ |
| 272 | private void file_export_pdf( final boolean dir ) { |
| 273 | final var workspace = getWorkspace(); |
| 274 | final var themes = workspace.getFile( |
| 275 | KEY_TYPESET_CONTEXT_THEMES_PATH |
| 276 | ); |
| 277 | final var theme = workspace.stringProperty( |
| 278 | KEY_TYPESET_CONTEXT_THEME_SELECTION |
| 279 | ); |
| 280 | final var chapters = workspace.stringProperty( |
| 281 | KEY_TYPESET_CONTEXT_CHAPTERS |
| 282 | ); |
| 283 | final var settings = ExportSettings |
| 284 | .builder() |
| 285 | .with( ExportSettings.Mutator::setTheme, theme ) |
| 286 | .with( ExportSettings.Mutator::setChapters, chapters ) |
| 287 | .build(); |
| 288 | |
| 289 | // Don't re-validate the typesetter installation each time. If the |
| 290 | // user mucks up the typesetter installation, it'll get caught the |
| 291 | // next time the application is started. Don't use |= because it |
| 292 | // won't short-circuit. |
| 293 | mCanTypeset = mCanTypeset || Typesetter.canRun(); |
| 294 | |
| 295 | if( mCanTypeset ) { |
| 296 | // If the typesetter is installed, allow the user to select a theme. If |
| 297 | // the themes aren't installed, a status message will appear. |
| 298 | if( ExportDialog.choose( getWindow(), themes, settings, dir ) ) { |
| 299 | file_export( APPLICATION_PDF, dir ); |
| 300 | } |
| 301 | } |
| 302 | else { |
| 303 | fireExportFailedEvent(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | public void file_export_pdf() { |
| 308 | file_export_pdf( false ); |
| 309 | } |
| 310 | |
| 311 | public void file_export_pdf_dir() { |
| 312 | file_export_pdf( true ); |
| 313 | } |
| 314 | |
| 315 | public void file_export_html_dir() { |
| 316 | file_export( XHTML_TEX, true ); |
| 317 | } |
| 318 | |
| 319 | public void file_export_repeat() { |
| 320 | typeset( mTypesetService ); |
| 321 | } |
| 322 | |
| 323 | public void file_export_html_svg() { |
| 324 | file_export( HTML_TEX_SVG ); |
| 325 | } |
| 326 | |
| 327 | public void file_export_html_tex() { |
| 328 | file_export( HTML_TEX_DELIMITED ); |
| 329 | } |
| 330 | |
| 331 | public void file_export_xhtml_tex() { |
| 332 | file_export( XHTML_TEX ); |
| 333 | } |
| 334 | |
| 335 | private void fireExportFailedEvent() { |
| 336 | runLater( ExportFailedEvent::fire ); |
| 337 | } |
| 338 | |
| 339 | public void file_exit() { |
| 340 | final var window = getWindow(); |
| 341 | fireEvent( window, new WindowEvent( window, WINDOW_CLOSE_REQUEST ) ); |
| 342 | } |
| 343 | |
| 344 | public void edit_undo() { |
| 345 | getActiveTextEditor().undo(); |
| 346 | } |
| 347 | |
| 348 | public void edit_redo() { |
| 349 | getActiveTextEditor().redo(); |
| 350 | } |
| 351 | |
| 352 | public void edit_cut() { |
| 353 | getActiveTextEditor().cut(); |
| 354 | } |
| 355 | |
| 356 | public void edit_copy() { |
| 357 | getActiveTextEditor().copy(); |
| 358 | } |
| 359 | |
| 360 | public void edit_paste() { |
| 361 | getActiveTextEditor().paste(); |
| 362 | } |
| 363 | |
| 364 | public void edit_select_all() { |
| 365 | getActiveTextEditor().selectAll(); |
| 366 | } |
| 367 | |
| 368 | public void edit_find() { |
| 369 | final var nodes = getMainScene().getStatusBar().getLeftItems(); |
| 370 | |
| 371 | if( nodes.isEmpty() ) { |
| 372 | final var searchBar = new SearchBar(); |
| 373 | |
| 374 | searchBar.matchIndexProperty().bind( mSearchModel.matchIndexProperty() ); |
| 375 | searchBar.matchCountProperty().bind( mSearchModel.matchCountProperty() ); |
| 376 | |
| 377 | searchBar.setOnCancelAction( event -> { |
| 378 | final var editor = getActiveTextEditor(); |
| 379 | nodes.remove( searchBar ); |
| 380 | editor.unstylize( STYLE_SEARCH ); |
| 381 | editor.getNode().requestFocus(); |
| 382 | } ); |
| 383 | |
| 384 | searchBar.addInputListener( ( c, o, n ) -> { |
| 385 | if( n != null && !n.isEmpty() ) { |
| 386 | mSearchModel.search( n, getActiveTextEditor().getText() ); |
| 387 | } |
| 388 | } ); |
| 389 | |
| 390 | searchBar.setOnNextAction( event -> edit_find_next() ); |
| 391 | searchBar.setOnPrevAction( event -> edit_find_prev() ); |
| 392 | |
| 393 | nodes.add( searchBar ); |
| 394 | searchBar.requestFocus(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | public void edit_find_next() { |
| 399 | mSearchModel.advance(); |
| 400 | } |
| 401 | |
| 402 | public void edit_find_prev() { |
| 403 | mSearchModel.retreat(); |
| 404 | } |
| 405 | |
| 406 | public void edit_preferences() { |
| 407 | try { |
| 408 | new PreferencesController( getWorkspace() ).show(); |
| 409 | } catch( final Exception ex ) { |
| 410 | clue( ex ); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | public void format_bold() { |
| 415 | getActiveTextEditor().bold(); |
| 416 | } |
| 417 | |
| 418 | public void format_italic() { |
| 419 | getActiveTextEditor().italic(); |
| 420 | } |
| 421 | |
| 422 | public void format_monospace() { |
| 423 | getActiveTextEditor().monospace(); |
| 424 | } |
| 425 | |
| 426 | public void format_superscript() { |
| 427 | getActiveTextEditor().superscript(); |
| 428 | } |
| 429 | |
| 430 | public void format_subscript() { |
| 431 | getActiveTextEditor().subscript(); |
| 432 | } |
| 433 | |
| 434 | public void format_strikethrough() { |
| 435 | getActiveTextEditor().strikethrough(); |
| 436 | } |
| 437 | |
| 438 | public void insert_blockquote() { |
| 439 | getActiveTextEditor().blockquote(); |
| 440 | } |
| 441 | |
| 442 | public void insert_code() { |
| 443 | getActiveTextEditor().code(); |
| 444 | } |
| 445 | |
| 446 | public void insert_fenced_code_block() { |
| 447 | getActiveTextEditor().fencedCodeBlock(); |
| 448 | } |
| 449 | |
| 450 | public void insert_link() { |
| 451 | insertObject( createLinkDialog() ); |
| 452 | } |
| 453 | |
| 454 | public void insert_image() { |
| 455 | insertObject( createImageDialog() ); |
| 456 | } |
| 457 | |
| 458 | private void insertObject( final Dialog<String> dialog ) { |
| 459 | final var textArea = getActiveTextEditor().getTextArea(); |
| 460 | dialog.showAndWait().ifPresent( textArea::replaceSelection ); |
| 461 | } |
| 462 | |
| 463 | private Dialog<String> createLinkDialog() { |
| 464 | return new LinkDialog( getWindow(), createHyperlinkModel() ); |
| 465 | } |
| 466 | |
| 467 | private Dialog<String> createImageDialog() { |
| 468 | final var path = getActiveTextEditor().getPath(); |
| 469 | final var parentDir = path.getParent(); |
| 470 | return new ImageDialog( getWindow(), parentDir ); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Returns one of: selected text, word under cursor, or parsed hyperlink from |
| 475 | * the Markdown AST. |
| 476 | * |
| 477 | * @return An instance containing the link URL and display text. |
| 478 | */ |
| 479 | private HyperlinkModel createHyperlinkModel() { |
| 480 | final var context = getMainPane().createProcessorContext(); |
| 481 | final var editor = getActiveTextEditor(); |
| 482 | final var textArea = editor.getTextArea(); |
| 483 | final var selectedText = textArea.getSelectedText(); |
| 484 | |
| 485 | // Convert current paragraph to Markdown nodes. |
| 486 | final var mp = MarkdownProcessor.create( context ); |
| 487 | final var p = textArea.getCurrentParagraph(); |
| 488 | final var paragraph = textArea.getText( p ); |
| 489 | final var node = mp.toNode( paragraph ); |
| 490 | final var visitor = new LinkVisitor( textArea.getCaretColumn() ); |
| 491 | final var link = visitor.process( node ); |
| 492 | |
| 493 | if( link != null ) { |
| 494 | textArea.selectRange( p, link.getStartOffset(), p, link.getEndOffset() ); |
| 495 | } |
| 496 | |
| 497 | return createHyperlinkModel( link, selectedText ); |
| 498 | } |
| 499 | |
| 500 | private HyperlinkModel createHyperlinkModel( |
| 501 | final Link link, final String selection ) { |
| 502 | |
| 503 | return link == null |
| 504 | ? new HyperlinkModel( selection, "https://localhost" ) |
| 505 | : new HyperlinkModel( link ); |
| 506 | } |
| 507 | |
| 508 | public void insert_heading_1() { |
| 509 | insert_heading( 1 ); |
| 510 | } |
| 511 | |
| 512 | public void insert_heading_2() { |
| 513 | insert_heading( 2 ); |
| 514 | } |
| 515 | |
| 516 | public void insert_heading_3() { |
| 517 | insert_heading( 3 ); |
| 518 | } |
| 519 | |
| 520 | private void insert_heading( final int level ) { |
| 521 | getActiveTextEditor().heading( level ); |
| 522 | } |
| 523 | |
| 524 | public void insert_unordered_list() { |
| 525 | getActiveTextEditor().unorderedList(); |
| 526 | } |
| 527 | |
| 528 | public void insert_ordered_list() { |
| 529 | getActiveTextEditor().orderedList(); |
| 530 | } |
| 531 | |
| 532 | public void insert_horizontal_rule() { |
| 533 | getActiveTextEditor().horizontalRule(); |
| 534 | } |
| 535 | |
| 536 | public void definition_create() { |
| 537 | getActiveTextDefinition().createDefinition(); |
| 538 | } |
| 539 | |
| 540 | public void definition_rename() { |
| 541 | getActiveTextDefinition().renameDefinition(); |
| 542 | } |
| 543 | |
| 544 | public void definition_delete() { |
| 545 | getActiveTextDefinition().deleteDefinitions(); |
| 546 | } |
| 547 | |
| 548 | public void definition_autoinsert() { |
| 549 | getMainPane().autoinsert(); |
| 550 | } |
| 551 | |
| 552 | public void view_refresh() { |
| 553 | getMainPane().viewRefresh(); |
| 554 | } |
| 555 | |
| 556 | public void view_preview() { |
| 557 | getMainPane().viewPreview(); |
| 558 | } |
| 559 | |
| 560 | public void view_outline() { |
| 561 | getMainPane().viewOutline(); |
| 562 | } |
| 563 | |
| 564 | public void view_files() { getMainPane().viewFiles(); } |
| 565 | |
| 566 | public void view_statistics() { |
| 567 | getMainPane().viewStatistics(); |
| 568 | } |
| 569 | |
| 570 | public void view_menubar() { |
| 571 | getMainScene().toggleMenuBar(); |
| 572 | } |
| 573 | |
| 574 | public void view_toolbar() { |
| 575 | getMainScene().toggleToolBar(); |
| 576 | } |
| 577 | |
| 578 | public void view_statusbar() { |
| 579 | getMainScene().toggleStatusBar(); |
| 580 | } |
| 581 | |
| 582 | public void view_log() { |
| 583 | mLogView.view(); |
| 584 | } |
| 585 | |
| 586 | public void help_about() { |
| 587 | final var alert = new Alert( INFORMATION ); |
| 588 | final var prefix = "Dialog.about."; |
| 589 | alert.setTitle( get( prefix + "title", APP_TITLE ) ); |
| 590 | alert.setHeaderText( get( prefix + "header", APP_TITLE ) ); |
| 591 | alert.setContentText( get( prefix + "content", APP_YEAR, APP_VERSION ) ); |
| 592 | alert.setGraphic( ICON_DIALOG_NODE ); |
| 593 | alert.initOwner( getWindow() ); |
| 594 | alert.showAndWait(); |
| 595 | } |
| 596 | |
| 597 | private <T> void typeset( final Service<T> service ) { |
| 598 | synchronized( mMutex ) { |
| 599 | if( service != null && !service.isRunning() ) { |
| 600 | service.reset(); |
| 601 | service.start(); |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Concatenates all the files in the same directory as the given file into |
| 608 | * a string. The extension is determined by the given file name pattern; the |
| 609 | * order files are concatenated is based on their numeric sort order (this |
| 610 | * avoids lexicographic sorting). |
| 611 | * <p> |
| 612 | * If the parent path to the file being edited in the text editor cannot |
| 613 | * be found then this will return the editor's text, without iterating through |
| 614 | * the parent directory. (Should never happen, but who knows?) |
| 615 | * </p> |
| 616 | * <p> |
| 617 | * New lines are automatically appended to separate each file. |
| 618 | * </p> |
| 619 | * |
| 620 | * @param editor The text editor containing |
| 621 | * @return All files in the same directory as the file being edited |
| 622 | * concatenated into a single string. |
| 623 | */ |
| 624 | private String append( final TextEditor editor ) { |
| 625 | final var pattern = editor.getPath(); |
| 626 | final var parent = pattern.getParent(); |
| 627 | |
| 628 | // Short-circuit because nothing else can be done. |
| 629 | if( parent == null ) { |
| 630 | clue( "Main.status.export.concat.parent", pattern ); |
| 631 | return editor.getText(); |
| 632 | } |
| 633 | |
| 634 | final var filename = SysFile.getFileName( pattern ); |
| 634 | 635 | final var extension = getExtension( filename ); |
| 635 | 636 | |