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