| | import com.scrivenvar.service.Options; |
| | import com.scrivenvar.service.Snitch; |
| | -import com.scrivenvar.service.events.Notifier; |
| | -import com.scrivenvar.spelling.api.SpellCheckListener; |
| | -import com.scrivenvar.spelling.api.SpellChecker; |
| | -import com.scrivenvar.spelling.impl.PermissiveSpeller; |
| | -import com.scrivenvar.spelling.impl.SymSpellSpeller; |
| | -import com.scrivenvar.util.Action; |
| | -import com.scrivenvar.util.ActionBuilder; |
| | -import com.scrivenvar.util.ActionUtils; |
| | -import com.vladsch.flexmark.parser.Parser; |
| | -import com.vladsch.flexmark.util.ast.NodeVisitor; |
| | -import com.vladsch.flexmark.util.ast.VisitHandler; |
| | -import javafx.beans.binding.Bindings; |
| | -import javafx.beans.binding.BooleanBinding; |
| | -import javafx.beans.property.BooleanProperty; |
| | -import javafx.beans.property.SimpleBooleanProperty; |
| | -import javafx.beans.value.ChangeListener; |
| | -import javafx.beans.value.ObservableBooleanValue; |
| | -import javafx.beans.value.ObservableValue; |
| | -import javafx.collections.ListChangeListener.Change; |
| | -import javafx.collections.ObservableList; |
| | -import javafx.event.Event; |
| | -import javafx.event.EventHandler; |
| | -import javafx.geometry.Pos; |
| | -import javafx.scene.Node; |
| | -import javafx.scene.Scene; |
| | -import javafx.scene.control.*; |
| | -import javafx.scene.control.Alert.AlertType; |
| | -import javafx.scene.image.Image; |
| | -import javafx.scene.image.ImageView; |
| | -import javafx.scene.input.Clipboard; |
| | -import javafx.scene.input.ClipboardContent; |
| | -import javafx.scene.input.KeyEvent; |
| | -import javafx.scene.layout.BorderPane; |
| | -import javafx.scene.layout.VBox; |
| | -import javafx.scene.text.Text; |
| | -import javafx.stage.Window; |
| | -import javafx.stage.WindowEvent; |
| | -import javafx.util.Duration; |
| | -import org.apache.commons.lang3.SystemUtils; |
| | -import org.controlsfx.control.StatusBar; |
| | -import org.fxmisc.richtext.StyleClassedTextArea; |
| | -import org.fxmisc.richtext.model.StyleSpansBuilder; |
| | -import org.reactfx.value.Val; |
| | - |
| | -import java.io.BufferedReader; |
| | -import java.io.InputStreamReader; |
| | -import java.nio.file.Path; |
| | -import java.nio.file.Paths; |
| | -import java.util.*; |
| | -import java.util.concurrent.atomic.AtomicInteger; |
| | -import java.util.function.Consumer; |
| | -import java.util.function.Function; |
| | -import java.util.prefs.Preferences; |
| | -import java.util.stream.Collectors; |
| | - |
| | -import static com.scrivenvar.Constants.*; |
| | -import static com.scrivenvar.Messages.get; |
| | -import static com.scrivenvar.service.GlobalNotifier.*; |
| | -import static com.scrivenvar.util.StageState.*; |
| | -import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| | -import static java.nio.charset.StandardCharsets.UTF_8; |
| | -import static java.util.Collections.emptyList; |
| | -import static java.util.Collections.singleton; |
| | -import static javafx.application.Platform.runLater; |
| | -import static javafx.event.Event.fireEvent; |
| | -import static javafx.scene.input.KeyCode.ENTER; |
| | -import static javafx.scene.input.KeyCode.TAB; |
| | -import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| | -import static org.fxmisc.richtext.model.TwoDimensional.Bias.Forward; |
| | - |
| | -/** |
| | - * Main window containing a tab pane in the center for file editors. |
| | - */ |
| | -public class MainWindow implements Observer { |
| | - /** |
| | - * The {@code OPTIONS} variable must be declared before all other variables |
| | - * to prevent subsequent initializations from failing due to missing user |
| | - * preferences. |
| | - */ |
| | - private static final Options sOptions = Services.load( Options.class ); |
| | - private static final Snitch SNITCH = Services.load( Snitch.class ); |
| | - |
| | - private final Scene mScene; |
| | - private final StatusBar mStatusBar; |
| | - private final Text mLineNumberText; |
| | - private final TextField mFindTextField; |
| | - private final SpellChecker mSpellChecker; |
| | - |
| | - private final Object mMutex = new Object(); |
| | - |
| | - /** |
| | - * Prevents re-instantiation of processing classes. |
| | - */ |
| | - private final Map<FileEditorTab, Processor<String>> mProcessors = |
| | - new HashMap<>(); |
| | - |
| | - private final Map<String, String> mResolvedMap = |
| | - new HashMap<>( DEFAULT_MAP_SIZE ); |
| | - |
| | - private final EventHandler<PreferencesFxEvent> mRPreferencesListener = |
| | - event -> rerender(); |
| | - |
| | - /** |
| | - * Called when the definition data is changed. |
| | - */ |
| | - private final EventHandler<TreeItem.TreeModificationEvent<Event>> |
| | - mTreeHandler = event -> { |
| | - exportDefinitions( getDefinitionPath() ); |
| | - interpolateResolvedMap(); |
| | - rerender(); |
| | - }; |
| | - |
| | - /** |
| | - * Called to switch to the definition pane when the user presses the TAB key. |
| | - */ |
| | - private final EventHandler<? super KeyEvent> mTabKeyHandler = |
| | - (EventHandler<KeyEvent>) event -> { |
| | - if( event.getCode() == TAB ) { |
| | - getDefinitionPane().requestFocus(); |
| | - event.consume(); |
| | - } |
| | - }; |
| | - |
| | - /** |
| | - * Called to inject the selected item when the user presses ENTER in the |
| | - * definition pane. |
| | - */ |
| | - private final EventHandler<? super KeyEvent> mDefinitionKeyHandler = |
| | - event -> { |
| | - if( event.getCode() == ENTER ) { |
| | - getVariableNameInjector().injectSelectedItem(); |
| | - } |
| | - }; |
| | - |
| | - private final ChangeListener<Integer> mCaretPositionListener = |
| | - ( observable, oldPosition, newPosition ) -> { |
| | - final FileEditorTab tab = getActiveFileEditorTab(); |
| | - final EditorPane pane = tab.getEditorPane(); |
| | - final StyleClassedTextArea editor = pane.getEditor(); |
| | - |
| | - getLineNumberText().setText( |
| | - get( STATUS_BAR_LINE, |
| | - editor.getCurrentParagraph() + 1, |
| | - editor.getParagraphs().size(), |
| | - editor.getCaretPosition() |
| | - ) |
| | - ); |
| | - }; |
| | - |
| | - private final ChangeListener<Integer> mCaretParagraphListener = |
| | - ( observable, oldIndex, newIndex ) -> |
| | - scrollToParagraph( newIndex, true ); |
| | - |
| | - private DefinitionSource mDefinitionSource = createDefaultDefinitionSource(); |
| | - private final DefinitionPane mDefinitionPane = new DefinitionPane(); |
| | - private final HTMLPreviewPane mPreviewPane = createHTMLPreviewPane(); |
| | - private final FileEditorTabPane mFileEditorPane = new FileEditorTabPane( |
| | - mCaretPositionListener, |
| | - mCaretParagraphListener ); |
| | - |
| | - /** |
| | - * Listens on the definition pane for double-click events. |
| | - */ |
| | - private final DefinitionNameInjector mVariableNameInjector |
| | - = new DefinitionNameInjector( mDefinitionPane ); |
| | - |
| | - public MainWindow() { |
| | - getNotifier().addObserver( this ); |
| | - |
| | - mStatusBar = createStatusBar(); |
| | - mLineNumberText = createLineNumberText(); |
| | - mFindTextField = createFindTextField(); |
| | - mScene = createScene(); |
| | - mSpellChecker = createSpellChecker(); |
| | - |
| | - // Add the close request listener before the window is shown. |
| | - initLayout(); |
| | - } |
| | - |
| | - /** |
| | - * Called after the stage is shown. |
| | - */ |
| | - public void init() { |
| | - initFindInput(); |
| | - initSnitch(); |
| | - initDefinitionListener(); |
| | - initTabAddedListener(); |
| | - initTabChangedListener(); |
| | - initPreferences(); |
| | - initVariableNameInjector(); |
| | - } |
| | - |
| | - private void initLayout() { |
| | - final var scene = getScene(); |
| | - |
| | - scene.getStylesheets().add( STYLESHEET_SCENE ); |
| | - scene.windowProperty().addListener( |
| | - ( unused, oldWindow, newWindow ) -> |
| | - newWindow.setOnCloseRequest( |
| | - e -> { |
| | - if( !getFileEditorPane().closeAllEditors() ) { |
| | - e.consume(); |
| | - } |
| | - } |
| | - ) |
| | - ); |
| | - } |
| | - |
| | - /** |
| | - * Initialize the find input text field to listen on F3, ENTER, and |
| | - * ESCAPE key presses. |
| | - */ |
| | - private void initFindInput() { |
| | - final TextField input = getFindTextField(); |
| | - |
| | - input.setOnKeyPressed( ( KeyEvent event ) -> { |
| | - switch( event.getCode() ) { |
| | - case F3: |
| | - case ENTER: |
| | - editFindNext(); |
| | - break; |
| | - case F: |
| | - if( !event.isControlDown() ) { |
| | - break; |
| | - } |
| | - case ESCAPE: |
| | - getStatusBar().setGraphic( null ); |
| | - getActiveFileEditorTab().getEditorPane().requestFocus(); |
| | - break; |
| | - } |
| | - } ); |
| | - |
| | - // Remove when the input field loses focus. |
| | - input.focusedProperty().addListener( |
| | - ( focused, oldFocus, newFocus ) -> { |
| | - if( !newFocus ) { |
| | - getStatusBar().setGraphic( null ); |
| | - } |
| | - } |
| | - ); |
| | - } |
| | - |
| | - /** |
| | - * Watch for changes to external files. In particular, this awaits |
| | - * modifications to any XSL files associated with XML files being edited. |
| | - * When |
| | - * an XSL file is modified (external to the application), the snitch's ears |
| | - * perk up and the file is reloaded. This keeps the XSL transformation up to |
| | - * date with what's on the file system. |
| | - */ |
| | - private void initSnitch() { |
| | - SNITCH.addObserver( this ); |
| | - } |
| | - |
| | - /** |
| | - * Listen for {@link FileEditorTabPane} to receive open definition file |
| | - * event. |
| | - */ |
| | - private void initDefinitionListener() { |
| | - getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| | - ( final ObservableValue<? extends Path> file, |
| | - final Path oldPath, final Path newPath ) -> { |
| | - openDefinitions( newPath ); |
| | - rerender(); |
| | - } |
| | - ); |
| | - } |
| | - |
| | - /** |
| | - * Re-instantiates all processors then re-renders the active tab. This |
| | - * will refresh the resolved map, force R to re-initialize, and brute-force |
| | - * XSLT file reloads. |
| | - */ |
| | - private void rerender() { |
| | - runLater( |
| | - () -> { |
| | - resetProcessors(); |
| | - renderActiveTab(); |
| | - } |
| | - ); |
| | - } |
| | - |
| | - /** |
| | - * When tabs are added, hook the various change listeners onto the new |
| | - * tab sothat the preview pane refreshes as necessary. |
| | - */ |
| | - private void initTabAddedListener() { |
| | - final FileEditorTabPane editorPane = getFileEditorPane(); |
| | - |
| | - // Make sure the text processor kicks off when new files are opened. |
| | - final ObservableList<Tab> tabs = editorPane.getTabs(); |
| | - |
| | - // Update the preview pane on tab changes. |
| | - tabs.addListener( |
| | - ( final Change<? extends Tab> change ) -> { |
| | - while( change.next() ) { |
| | - if( change.wasAdded() ) { |
| | - // Multiple tabs can be added simultaneously. |
| | - for( final Tab newTab : change.getAddedSubList() ) { |
| | - final FileEditorTab tab = (FileEditorTab) newTab; |
| | - |
| | - initTextChangeListener( tab ); |
| | - initTabKeyEventListener( tab ); |
| | - initScrollEventListener( tab ); |
| | - initSpellCheckListener( tab ); |
| | -// initSyntaxListener( tab ); |
| | - } |
| | - } |
| | - } |
| | - } |
| | - ); |
| | - } |
| | - |
| | - private void initTextChangeListener( final FileEditorTab tab ) { |
| | - tab.addTextChangeListener( |
| | - ( editor, oldValue, newValue ) -> { |
| | - process( tab ); |
| | - scrollToParagraph( getCurrentParagraphIndex() ); |
| | - } |
| | - ); |
| | - } |
| | - |
| | - /** |
| | - * Ensure that the keyboard events are received when a new tab is added |
| | - * to the user interface. |
| | - * |
| | - * @param tab The tab editor that can trigger keyboard events. |
| | - */ |
| | - private void initTabKeyEventListener( final FileEditorTab tab ) { |
| | - tab.addEventFilter( KeyEvent.KEY_PRESSED, mTabKeyHandler ); |
| | - } |
| | - |
| | - private void initScrollEventListener( final FileEditorTab tab ) { |
| | - final var scrollPane = tab.getScrollPane(); |
| | - final var scrollBar = getPreviewPane().getVerticalScrollBar(); |
| | - |
| | - addShowListener( scrollPane, ( __ ) -> { |
| | - final var handler = new ScrollEventHandler( scrollPane, scrollBar ); |
| | - handler.enabledProperty().bind( tab.selectedProperty() ); |
| | - } ); |
| | - } |
| | - |
| | - /** |
| | - * Listen for changes to the any particular paragraph and perform a quick |
| | - * spell check upon it. The style classes in the editor will be changed to |
| | - * mark any spelling mistakes in the paragraph. The user may then interact |
| | - * with any misspelled word (i.e., any piece of text that is marked) to |
| | - * revise the spelling. |
| | - * |
| | - * @param tab The tab to spellcheck. |
| | - */ |
| | - private void initSpellCheckListener( final FileEditorTab tab ) { |
| | - final var editor = tab.getEditorPane().getEditor(); |
| | - |
| | - // When the editor first appears, run a full spell check. This allows |
| | - // spell checking while typing to be restricted to the active paragraph, |
| | - // which is usually substantially smaller than the whole document. |
| | - addShowListener( |
| | - editor, ( __ ) -> spellcheck( editor, editor.getText() ) |
| | - ); |
| | - |
| | - // Use the plain text changes so that notifications of style changes |
| | - // are suppressed. Checking against the identity ensures that only |
| | - // new text additions or deletions trigger proofreading. |
| | - editor.plainTextChanges() |
| | - .filter( p -> !p.isIdentity() ).subscribe( change -> { |
| | - |
| | - // Only perform a spell check on the current paragraph. The |
| | - // entire document is processed once, when opened. |
| | - final var offset = change.getPosition(); |
| | - final var position = editor.offsetToPosition( offset, Forward ); |
| | - final var paraId = position.getMajor(); |
| | - final var paragraph = editor.getParagraph( paraId ); |
| | - final var text = paragraph.getText(); |
| | - |
| | - // Ensure that styles aren't doubled-up. |
| | - editor.clearStyle( paraId ); |
| | - |
| | - spellcheck( editor, text, paraId ); |
| | - } ); |
| | - } |
| | - |
| | - /** |
| | - * Listen for new tab selection events. |
| | - */ |
| | - private void initTabChangedListener() { |
| | - final FileEditorTabPane editorPane = getFileEditorPane(); |
| | - |
| | - // Update the preview pane changing tabs. |
| | - editorPane.addTabSelectionListener( |
| | - ( tabPane, oldTab, newTab ) -> { |
| | - if( newTab == null ) { |
| | - // Clear the preview pane when closing an editor. When the last |
| | - // tab is closed, this ensures that the preview pane is empty. |
| | - getPreviewPane().clear(); |
| | - } |
| | - else { |
| | - final var tab = (FileEditorTab) newTab; |
| | - updateVariableNameInjector( tab ); |
| | - process( tab ); |
| | - } |
| | - } |
| | - ); |
| | - } |
| | - |
| | - /** |
| | - * Reloads the preferences from the previous session. |
| | - */ |
| | - private void initPreferences() { |
| | - initDefinitionPane(); |
| | - getFileEditorPane().initPreferences(); |
| | - getUserPreferences().addSaveEventHandler( mRPreferencesListener ); |
| | - } |
| | - |
| | - private void initVariableNameInjector() { |
| | - updateVariableNameInjector( getActiveFileEditorTab() ); |
| | - } |
| | - |
| | - /** |
| | - * Calls the listener when the given node is shown for the first time. The |
| | - * visible property is not the same as the initial showing event; visibility |
| | - * can be triggered numerous times (such as going off screen). |
| | - * <p> |
| | - * This is called, for example, before the drag handler can be attached, |
| | - * because the scrollbar for the text editor pane must be visible. |
| | - * </p> |
| | - * |
| | - * @param node The node to watch for showing. |
| | - * @param consumer The consumer to invoke when the event fires. |
| | - */ |
| | - private void addShowListener( |
| | - final Node node, final Consumer<Void> consumer ) { |
| | - final ChangeListener<? super Boolean> listener = ( o, oldShow, newShow ) -> |
| | - runLater( () -> { |
| | - if( newShow ) { |
| | - try { |
| | - consumer.accept( null ); |
| | - } catch( final Exception ex ) { |
| | - alert( ex ); |
| | - } |
| | - } |
| | - } ); |
| | - |
| | - Val.flatMap( node.sceneProperty(), Scene::windowProperty ) |
| | - .flatMap( Window::showingProperty ) |
| | - .addListener( listener ); |
| | - } |
| | - |
| | - private void scrollToParagraph( final int id ) { |
| | - scrollToParagraph( id, false ); |
| | - } |
| | - |
| | - /** |
| | - * @param id The paragraph to scroll to, will be approximated if it doesn't |
| | - * exist. |
| | - * @param force {@code true} means to force scrolling immediately, which |
| | - * should only be attempted when it is known that the document |
| | - * has been fully rendered. Otherwise the internal map of ID |
| | - * attributes will be incomplete and scrolling will flounder. |
| | - */ |
| | - private void scrollToParagraph( final int id, final boolean force ) { |
| | - synchronized( mMutex ) { |
| | - final var previewPane = getPreviewPane(); |
| | - final var scrollPane = previewPane.getScrollPane(); |
| | - final int approxId = getActiveEditorPane().approximateParagraphId( id ); |
| | - |
| | - if( force ) { |
| | - previewPane.scrollTo( approxId ); |
| | - } |
| | - else { |
| | - previewPane.tryScrollTo( approxId ); |
| | - } |
| | - |
| | - scrollPane.repaint(); |
| | - } |
| | - } |
| | - |
| | - private void updateVariableNameInjector( final FileEditorTab tab ) { |
| | - getVariableNameInjector().addListener( tab ); |
| | - } |
| | - |
| | - /** |
| | - * Called whenever the preview pane becomes out of sync with the file editor |
| | - * tab. This can be called when the text changes, the caret paragraph |
| | - * changes, or the file tab changes. |
| | - * |
| | - * @param tab The file editor tab that has been changed in some fashion. |
| | - */ |
| | - private void process( final FileEditorTab tab ) { |
| | - if( tab != null ) { |
| | - getPreviewPane().setPath( tab.getPath() ); |
| | - |
| | - final Processor<String> processor = getProcessors().computeIfAbsent( |
| | - tab, p -> createProcessors( tab ) |
| | - ); |
| | - |
| | - try { |
| | - processChain( processor, tab.getEditorText() ); |
| | - } catch( final Exception ex ) { |
| | - alert( ex ); |
| | - } |
| | - } |
| | - } |
| | - |
| | - /** |
| | - * Executes the processing chain, operating on the given string. |
| | - * |
| | - * @param handler The first processor in the chain to call. |
| | - * @param text The initial value of the text to process. |
| | - * @return The final value of the text that was processed by the chain. |
| | - */ |
| | - private String processChain( Processor<String> handler, String text ) { |
| | - while( handler != null && text != null ) { |
| | - text = handler.apply( text ); |
| | - handler = handler.next(); |
| | - } |
| | - |
| | - return text; |
| | - } |
| | - |
| | - private void renderActiveTab() { |
| | - process( getActiveFileEditorTab() ); |
| | - } |
| | - |
| | - /** |
| | - * Called when a definition source is opened. |
| | - * |
| | - * @param path Path to the definition source that was opened. |
| | - */ |
| | - private void openDefinitions( final Path path ) { |
| | - try { |
| | - final var ds = createDefinitionSource( path ); |
| | - setDefinitionSource( ds ); |
| | - |
| | - final var prefs = getUserPreferences(); |
| | - prefs.definitionPathProperty().setValue( path.toFile() ); |
| | - prefs.save(); |
| | - |
| | - final var tooltipPath = new Tooltip( path.toString() ); |
| | - tooltipPath.setShowDelay( Duration.millis( 200 ) ); |
| | - |
| | - final var pane = getDefinitionPane(); |
| | - pane.update( ds ); |
| | - pane.addTreeChangeHandler( mTreeHandler ); |
| | - pane.addKeyEventHandler( mDefinitionKeyHandler ); |
| | - pane.filenameProperty().setValue( path.getFileName().toString() ); |
| | - pane.setTooltip( tooltipPath ); |
| | - |
| | - interpolateResolvedMap(); |
| | - } catch( final Exception ex ) { |
| | - alert( ex ); |
| | - } |
| | - } |
| | - |
| | - private void exportDefinitions( final Path path ) { |
| | - try { |
| | - final var pane = getDefinitionPane(); |
| | - final var root = pane.getTreeView().getRoot(); |
| | - final var problemChild = pane.isTreeWellFormed(); |
| | - |
| | - if( problemChild == null ) { |
| | - getDefinitionSource().getTreeAdapter().export( root, path ); |
| | - clearAlert(); |
| | - } |
| | - else { |
| | - alert( get( "yaml.error.tree.form", problemChild.getValue() ) ); |
| | - } |
| | - } catch( final Exception ex ) { |
| | - alert( ex ); |
| | - } |
| | - } |
| | - |
| | - private void interpolateResolvedMap() { |
| | - final var treeMap = getDefinitionPane().toMap(); |
| | - final var map = new HashMap<>( treeMap ); |
| | - MapInterpolator.interpolate( map ); |
| | - |
| | - getResolvedMap().clear(); |
| | - getResolvedMap().putAll( map ); |
| | - } |
| | - |
| | - private void initDefinitionPane() { |
| | - openDefinitions( getDefinitionPath() ); |
| | - } |
| | - |
| | - //---- File actions ------------------------------------------------------- |
| | - |
| | - /** |
| | - * Called when an {@link Observable} instance has changed. This is called |
| | - * by both the {@link Snitch} service and the notify service. The @link |
| | - * Snitch} service can be called for different file types, including |
| | - * {@link DefinitionSource} instances. |
| | - * |
| | - * @param observable The observed instance. |
| | - * @param value The noteworthy item. |
| | - */ |
| | - @Override |
| | - public void update( final Observable observable, final Object value ) { |
| | - if( value != null ) { |
| | - if( observable instanceof Snitch && value instanceof Path ) { |
| | - updateSelectedTab(); |
| | - } |
| | - else if( observable instanceof Notifier && value instanceof String ) { |
| | - updateStatusBar( (String) value ); |
| | - } |
| | - } |
| | - } |
| | - |
| | - /** |
| | - * Updates the status bar to show the given message. |
| | - * |
| | - * @param s The message to show in the status bar. |
| | - */ |
| | - private void updateStatusBar( final String s ) { |
| | - runLater( |
| | - () -> { |
| | - final int index = s.indexOf( '\n' ); |
| | - final String message = s.substring( |
| | - 0, index > 0 ? index : s.length() ); |
| | - |
| | - getStatusBar().setText( message ); |
| | - } |
| | - ); |
| | +import com.scrivenvar.spelling.api.SpellCheckListener; |
| | +import com.scrivenvar.spelling.api.SpellChecker; |
| | +import com.scrivenvar.spelling.impl.PermissiveSpeller; |
| | +import com.scrivenvar.spelling.impl.SymSpellSpeller; |
| | +import com.scrivenvar.util.Action; |
| | +import com.scrivenvar.util.ActionBuilder; |
| | +import com.scrivenvar.util.ActionUtils; |
| | +import com.vladsch.flexmark.parser.Parser; |
| | +import com.vladsch.flexmark.util.ast.NodeVisitor; |
| | +import com.vladsch.flexmark.util.ast.VisitHandler; |
| | +import javafx.beans.binding.Bindings; |
| | +import javafx.beans.binding.BooleanBinding; |
| | +import javafx.beans.property.BooleanProperty; |
| | +import javafx.beans.property.SimpleBooleanProperty; |
| | +import javafx.beans.value.ChangeListener; |
| | +import javafx.beans.value.ObservableBooleanValue; |
| | +import javafx.beans.value.ObservableValue; |
| | +import javafx.collections.ListChangeListener.Change; |
| | +import javafx.collections.ObservableList; |
| | +import javafx.event.Event; |
| | +import javafx.event.EventHandler; |
| | +import javafx.geometry.Pos; |
| | +import javafx.scene.Node; |
| | +import javafx.scene.Scene; |
| | +import javafx.scene.control.*; |
| | +import javafx.scene.control.Alert.AlertType; |
| | +import javafx.scene.image.Image; |
| | +import javafx.scene.image.ImageView; |
| | +import javafx.scene.input.Clipboard; |
| | +import javafx.scene.input.ClipboardContent; |
| | +import javafx.scene.input.KeyEvent; |
| | +import javafx.scene.layout.BorderPane; |
| | +import javafx.scene.layout.VBox; |
| | +import javafx.scene.text.Text; |
| | +import javafx.stage.Window; |
| | +import javafx.stage.WindowEvent; |
| | +import javafx.util.Duration; |
| | +import org.apache.commons.lang3.SystemUtils; |
| | +import org.controlsfx.control.StatusBar; |
| | +import org.fxmisc.richtext.StyleClassedTextArea; |
| | +import org.fxmisc.richtext.model.StyleSpansBuilder; |
| | +import org.reactfx.value.Val; |
| | + |
| | +import java.io.BufferedReader; |
| | +import java.io.InputStreamReader; |
| | +import java.nio.file.Path; |
| | +import java.nio.file.Paths; |
| | +import java.util.*; |
| | +import java.util.concurrent.atomic.AtomicInteger; |
| | +import java.util.function.Consumer; |
| | +import java.util.function.Function; |
| | +import java.util.prefs.Preferences; |
| | +import java.util.stream.Collectors; |
| | + |
| | +import static com.scrivenvar.Constants.*; |
| | +import static com.scrivenvar.Messages.get; |
| | +import static com.scrivenvar.StatusBarNotifier.alert; |
| | +import static com.scrivenvar.StatusBarNotifier.clearAlert; |
| | +import static com.scrivenvar.util.StageState.*; |
| | +import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*; |
| | +import static java.nio.charset.StandardCharsets.UTF_8; |
| | +import static java.util.Collections.emptyList; |
| | +import static java.util.Collections.singleton; |
| | +import static javafx.application.Platform.runLater; |
| | +import static javafx.event.Event.fireEvent; |
| | +import static javafx.scene.input.KeyCode.ENTER; |
| | +import static javafx.scene.input.KeyCode.TAB; |
| | +import static javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST; |
| | +import static org.fxmisc.richtext.model.TwoDimensional.Bias.Forward; |
| | + |
| | +/** |
| | + * Main window containing a tab pane in the center for file editors. |
| | + */ |
| | +public class MainWindow implements Observer { |
| | + /** |
| | + * The {@code OPTIONS} variable must be declared before all other variables |
| | + * to prevent subsequent initializations from failing due to missing user |
| | + * preferences. |
| | + */ |
| | + private static final Options sOptions = Services.load( Options.class ); |
| | + private static final Snitch SNITCH = Services.load( Snitch.class ); |
| | + |
| | + private final Scene mScene; |
| | + private final StatusBar mStatusBar; |
| | + private final Text mLineNumberText; |
| | + private final TextField mFindTextField; |
| | + private final SpellChecker mSpellChecker; |
| | + |
| | + private final Object mMutex = new Object(); |
| | + |
| | + /** |
| | + * Prevents re-instantiation of processing classes. |
| | + */ |
| | + private final Map<FileEditorTab, Processor<String>> mProcessors = |
| | + new HashMap<>(); |
| | + |
| | + private final Map<String, String> mResolvedMap = |
| | + new HashMap<>( DEFAULT_MAP_SIZE ); |
| | + |
| | + private final EventHandler<PreferencesFxEvent> mRPreferencesListener = |
| | + event -> rerender(); |
| | + |
| | + /** |
| | + * Called when the definition data is changed. |
| | + */ |
| | + private final EventHandler<TreeItem.TreeModificationEvent<Event>> |
| | + mTreeHandler = event -> { |
| | + exportDefinitions( getDefinitionPath() ); |
| | + interpolateResolvedMap(); |
| | + rerender(); |
| | + }; |
| | + |
| | + /** |
| | + * Called to switch to the definition pane when the user presses the TAB key. |
| | + */ |
| | + private final EventHandler<? super KeyEvent> mTabKeyHandler = |
| | + (EventHandler<KeyEvent>) event -> { |
| | + if( event.getCode() == TAB ) { |
| | + getDefinitionPane().requestFocus(); |
| | + event.consume(); |
| | + } |
| | + }; |
| | + |
| | + /** |
| | + * Called to inject the selected item when the user presses ENTER in the |
| | + * definition pane. |
| | + */ |
| | + private final EventHandler<? super KeyEvent> mDefinitionKeyHandler = |
| | + event -> { |
| | + if( event.getCode() == ENTER ) { |
| | + getVariableNameInjector().injectSelectedItem(); |
| | + } |
| | + }; |
| | + |
| | + private final ChangeListener<Integer> mCaretPositionListener = |
| | + ( observable, oldPosition, newPosition ) -> { |
| | + final FileEditorTab tab = getActiveFileEditorTab(); |
| | + final EditorPane pane = tab.getEditorPane(); |
| | + final StyleClassedTextArea editor = pane.getEditor(); |
| | + |
| | + getLineNumberText().setText( |
| | + get( STATUS_BAR_LINE, |
| | + editor.getCurrentParagraph() + 1, |
| | + editor.getParagraphs().size(), |
| | + editor.getCaretPosition() |
| | + ) |
| | + ); |
| | + }; |
| | + |
| | + private final ChangeListener<Integer> mCaretParagraphListener = |
| | + ( observable, oldIndex, newIndex ) -> |
| | + scrollToParagraph( newIndex, true ); |
| | + |
| | + private DefinitionSource mDefinitionSource = createDefaultDefinitionSource(); |
| | + private final DefinitionPane mDefinitionPane = new DefinitionPane(); |
| | + private final HTMLPreviewPane mPreviewPane = createHTMLPreviewPane(); |
| | + private final FileEditorTabPane mFileEditorPane = new FileEditorTabPane( |
| | + mCaretPositionListener, |
| | + mCaretParagraphListener ); |
| | + |
| | + /** |
| | + * Listens on the definition pane for double-click events. |
| | + */ |
| | + private final DefinitionNameInjector mVariableNameInjector |
| | + = new DefinitionNameInjector( mDefinitionPane ); |
| | + |
| | + public MainWindow() { |
| | + mStatusBar = createStatusBar(); |
| | + mLineNumberText = createLineNumberText(); |
| | + mFindTextField = createFindTextField(); |
| | + mScene = createScene(); |
| | + mSpellChecker = createSpellChecker(); |
| | + |
| | + // Add the close request listener before the window is shown. |
| | + initLayout(); |
| | + StatusBarNotifier.setStatusBar( mStatusBar ); |
| | + } |
| | + |
| | + /** |
| | + * Called after the stage is shown. |
| | + */ |
| | + public void init() { |
| | + initFindInput(); |
| | + initSnitch(); |
| | + initDefinitionListener(); |
| | + initTabAddedListener(); |
| | + initTabChangedListener(); |
| | + initPreferences(); |
| | + initVariableNameInjector(); |
| | + } |
| | + |
| | + private void initLayout() { |
| | + final var scene = getScene(); |
| | + |
| | + scene.getStylesheets().add( STYLESHEET_SCENE ); |
| | + scene.windowProperty().addListener( |
| | + ( unused, oldWindow, newWindow ) -> |
| | + newWindow.setOnCloseRequest( |
| | + e -> { |
| | + if( !getFileEditorPane().closeAllEditors() ) { |
| | + e.consume(); |
| | + } |
| | + } |
| | + ) |
| | + ); |
| | + } |
| | + |
| | + /** |
| | + * Initialize the find input text field to listen on F3, ENTER, and |
| | + * ESCAPE key presses. |
| | + */ |
| | + private void initFindInput() { |
| | + final TextField input = getFindTextField(); |
| | + |
| | + input.setOnKeyPressed( ( KeyEvent event ) -> { |
| | + switch( event.getCode() ) { |
| | + case F3: |
| | + case ENTER: |
| | + editFindNext(); |
| | + break; |
| | + case F: |
| | + if( !event.isControlDown() ) { |
| | + break; |
| | + } |
| | + case ESCAPE: |
| | + getStatusBar().setGraphic( null ); |
| | + getActiveFileEditorTab().getEditorPane().requestFocus(); |
| | + break; |
| | + } |
| | + } ); |
| | + |
| | + // Remove when the input field loses focus. |
| | + input.focusedProperty().addListener( |
| | + ( focused, oldFocus, newFocus ) -> { |
| | + if( !newFocus ) { |
| | + getStatusBar().setGraphic( null ); |
| | + } |
| | + } |
| | + ); |
| | + } |
| | + |
| | + /** |
| | + * Watch for changes to external files. In particular, this awaits |
| | + * modifications to any XSL files associated with XML files being edited. |
| | + * When |
| | + * an XSL file is modified (external to the application), the snitch's ears |
| | + * perk up and the file is reloaded. This keeps the XSL transformation up to |
| | + * date with what's on the file system. |
| | + */ |
| | + private void initSnitch() { |
| | + SNITCH.addObserver( this ); |
| | + } |
| | + |
| | + /** |
| | + * Listen for {@link FileEditorTabPane} to receive open definition file |
| | + * event. |
| | + */ |
| | + private void initDefinitionListener() { |
| | + getFileEditorPane().onOpenDefinitionFileProperty().addListener( |
| | + ( final ObservableValue<? extends Path> file, |
| | + final Path oldPath, final Path newPath ) -> { |
| | + openDefinitions( newPath ); |
| | + rerender(); |
| | + } |
| | + ); |
| | + } |
| | + |
| | + /** |
| | + * Re-instantiates all processors then re-renders the active tab. This |
| | + * will refresh the resolved map, force R to re-initialize, and brute-force |
| | + * XSLT file reloads. |
| | + */ |
| | + private void rerender() { |
| | + runLater( |
| | + () -> { |
| | + resetProcessors(); |
| | + renderActiveTab(); |
| | + } |
| | + ); |
| | + } |
| | + |
| | + /** |
| | + * When tabs are added, hook the various change listeners onto the new |
| | + * tab sothat the preview pane refreshes as necessary. |
| | + */ |
| | + private void initTabAddedListener() { |
| | + final FileEditorTabPane editorPane = getFileEditorPane(); |
| | + |
| | + // Make sure the text processor kicks off when new files are opened. |
| | + final ObservableList<Tab> tabs = editorPane.getTabs(); |
| | + |
| | + // Update the preview pane on tab changes. |
| | + tabs.addListener( |
| | + ( final Change<? extends Tab> change ) -> { |
| | + while( change.next() ) { |
| | + if( change.wasAdded() ) { |
| | + // Multiple tabs can be added simultaneously. |
| | + for( final Tab newTab : change.getAddedSubList() ) { |
| | + final FileEditorTab tab = (FileEditorTab) newTab; |
| | + |
| | + initTextChangeListener( tab ); |
| | + initTabKeyEventListener( tab ); |
| | + initScrollEventListener( tab ); |
| | + initSpellCheckListener( tab ); |
| | +// initSyntaxListener( tab ); |
| | + } |
| | + } |
| | + } |
| | + } |
| | + ); |
| | + } |
| | + |
| | + private void initTextChangeListener( final FileEditorTab tab ) { |
| | + tab.addTextChangeListener( |
| | + ( editor, oldValue, newValue ) -> { |
| | + process( tab ); |
| | + scrollToParagraph( getCurrentParagraphIndex() ); |
| | + } |
| | + ); |
| | + } |
| | + |
| | + /** |
| | + * Ensure that the keyboard events are received when a new tab is added |
| | + * to the user interface. |
| | + * |
| | + * @param tab The tab editor that can trigger keyboard events. |
| | + */ |
| | + private void initTabKeyEventListener( final FileEditorTab tab ) { |
| | + tab.addEventFilter( KeyEvent.KEY_PRESSED, mTabKeyHandler ); |
| | + } |
| | + |
| | + private void initScrollEventListener( final FileEditorTab tab ) { |
| | + final var scrollPane = tab.getScrollPane(); |
| | + final var scrollBar = getPreviewPane().getVerticalScrollBar(); |
| | + |
| | + addShowListener( scrollPane, ( __ ) -> { |
| | + final var handler = new ScrollEventHandler( scrollPane, scrollBar ); |
| | + handler.enabledProperty().bind( tab.selectedProperty() ); |
| | + } ); |
| | + } |
| | + |
| | + /** |
| | + * Listen for changes to the any particular paragraph and perform a quick |
| | + * spell check upon it. The style classes in the editor will be changed to |
| | + * mark any spelling mistakes in the paragraph. The user may then interact |
| | + * with any misspelled word (i.e., any piece of text that is marked) to |
| | + * revise the spelling. |
| | + * |
| | + * @param tab The tab to spellcheck. |
| | + */ |
| | + private void initSpellCheckListener( final FileEditorTab tab ) { |
| | + final var editor = tab.getEditorPane().getEditor(); |
| | + |
| | + // When the editor first appears, run a full spell check. This allows |
| | + // spell checking while typing to be restricted to the active paragraph, |
| | + // which is usually substantially smaller than the whole document. |
| | + addShowListener( |
| | + editor, ( __ ) -> spellcheck( editor, editor.getText() ) |
| | + ); |
| | + |
| | + // Use the plain text changes so that notifications of style changes |
| | + // are suppressed. Checking against the identity ensures that only |
| | + // new text additions or deletions trigger proofreading. |
| | + editor.plainTextChanges() |
| | + .filter( p -> !p.isIdentity() ).subscribe( change -> { |
| | + |
| | + // Only perform a spell check on the current paragraph. The |
| | + // entire document is processed once, when opened. |
| | + final var offset = change.getPosition(); |
| | + final var position = editor.offsetToPosition( offset, Forward ); |
| | + final var paraId = position.getMajor(); |
| | + final var paragraph = editor.getParagraph( paraId ); |
| | + final var text = paragraph.getText(); |
| | + |
| | + // Ensure that styles aren't doubled-up. |
| | + editor.clearStyle( paraId ); |
| | + |
| | + spellcheck( editor, text, paraId ); |
| | + } ); |
| | + } |
| | + |
| | + /** |
| | + * Listen for new tab selection events. |
| | + */ |
| | + private void initTabChangedListener() { |
| | + final FileEditorTabPane editorPane = getFileEditorPane(); |
| | + |
| | + // Update the preview pane changing tabs. |
| | + editorPane.addTabSelectionListener( |
| | + ( tabPane, oldTab, newTab ) -> { |
| | + if( newTab == null ) { |
| | + // Clear the preview pane when closing an editor. When the last |
| | + // tab is closed, this ensures that the preview pane is empty. |
| | + getPreviewPane().clear(); |
| | + } |
| | + else { |
| | + final var tab = (FileEditorTab) newTab; |
| | + updateVariableNameInjector( tab ); |
| | + process( tab ); |
| | + } |
| | + } |
| | + ); |
| | + } |
| | + |
| | + /** |
| | + * Reloads the preferences from the previous session. |
| | + */ |
| | + private void initPreferences() { |
| | + initDefinitionPane(); |
| | + getFileEditorPane().initPreferences(); |
| | + getUserPreferences().addSaveEventHandler( mRPreferencesListener ); |
| | + } |
| | + |
| | + private void initVariableNameInjector() { |
| | + updateVariableNameInjector( getActiveFileEditorTab() ); |
| | + } |
| | + |
| | + /** |
| | + * Calls the listener when the given node is shown for the first time. The |
| | + * visible property is not the same as the initial showing event; visibility |
| | + * can be triggered numerous times (such as going off screen). |
| | + * <p> |
| | + * This is called, for example, before the drag handler can be attached, |
| | + * because the scrollbar for the text editor pane must be visible. |
| | + * </p> |
| | + * |
| | + * @param node The node to watch for showing. |
| | + * @param consumer The consumer to invoke when the event fires. |
| | + */ |
| | + private void addShowListener( |
| | + final Node node, final Consumer<Void> consumer ) { |
| | + final ChangeListener<? super Boolean> listener = ( o, oldShow, newShow ) -> |
| | + runLater( () -> { |
| | + if( newShow ) { |
| | + try { |
| | + consumer.accept( null ); |
| | + } catch( final Exception ex ) { |
| | + alert( ex ); |
| | + } |
| | + } |
| | + } ); |
| | + |
| | + Val.flatMap( node.sceneProperty(), Scene::windowProperty ) |
| | + .flatMap( Window::showingProperty ) |
| | + .addListener( listener ); |
| | + } |
| | + |
| | + private void scrollToParagraph( final int id ) { |
| | + scrollToParagraph( id, false ); |
| | + } |
| | + |
| | + /** |
| | + * @param id The paragraph to scroll to, will be approximated if it doesn't |
| | + * exist. |
| | + * @param force {@code true} means to force scrolling immediately, which |
| | + * should only be attempted when it is known that the document |
| | + * has been fully rendered. Otherwise the internal map of ID |
| | + * attributes will be incomplete and scrolling will flounder. |
| | + */ |
| | + private void scrollToParagraph( final int id, final boolean force ) { |
| | + synchronized( mMutex ) { |
| | + final var previewPane = getPreviewPane(); |
| | + final var scrollPane = previewPane.getScrollPane(); |
| | + final int approxId = getActiveEditorPane().approximateParagraphId( id ); |
| | + |
| | + if( force ) { |
| | + previewPane.scrollTo( approxId ); |
| | + } |
| | + else { |
| | + previewPane.tryScrollTo( approxId ); |
| | + } |
| | + |
| | + scrollPane.repaint(); |
| | + } |
| | + } |
| | + |
| | + private void updateVariableNameInjector( final FileEditorTab tab ) { |
| | + getVariableNameInjector().addListener( tab ); |
| | + } |
| | + |
| | + /** |
| | + * Called whenever the preview pane becomes out of sync with the file editor |
| | + * tab. This can be called when the text changes, the caret paragraph |
| | + * changes, or the file tab changes. |
| | + * |
| | + * @param tab The file editor tab that has been changed in some fashion. |
| | + */ |
| | + private void process( final FileEditorTab tab ) { |
| | + if( tab != null ) { |
| | + getPreviewPane().setPath( tab.getPath() ); |
| | + |
| | + final Processor<String> processor = getProcessors().computeIfAbsent( |
| | + tab, p -> createProcessors( tab ) |
| | + ); |
| | + |
| | + try { |
| | + processChain( processor, tab.getEditorText() ); |
| | + } catch( final Exception ex ) { |
| | + alert( ex ); |
| | + } |
| | + } |
| | + } |
| | + |
| | + /** |
| | + * Executes the processing chain, operating on the given string. |
| | + * |
| | + * @param handler The first processor in the chain to call. |
| | + * @param text The initial value of the text to process. |
| | + * @return The final value of the text that was processed by the chain. |
| | + */ |
| | + private String processChain( Processor<String> handler, String text ) { |
| | + while( handler != null && text != null ) { |
| | + text = handler.apply( text ); |
| | + handler = handler.next(); |
| | + } |
| | + |
| | + return text; |
| | + } |
| | + |
| | + private void renderActiveTab() { |
| | + process( getActiveFileEditorTab() ); |
| | + } |
| | + |
| | + /** |
| | + * Called when a definition source is opened. |
| | + * |
| | + * @param path Path to the definition source that was opened. |
| | + */ |
| | + private void openDefinitions( final Path path ) { |
| | + try { |
| | + final var ds = createDefinitionSource( path ); |
| | + setDefinitionSource( ds ); |
| | + |
| | + final var prefs = getUserPreferences(); |
| | + prefs.definitionPathProperty().setValue( path.toFile() ); |
| | + prefs.save(); |
| | + |
| | + final var tooltipPath = new Tooltip( path.toString() ); |
| | + tooltipPath.setShowDelay( Duration.millis( 200 ) ); |
| | + |
| | + final var pane = getDefinitionPane(); |
| | + pane.update( ds ); |
| | + pane.addTreeChangeHandler( mTreeHandler ); |
| | + pane.addKeyEventHandler( mDefinitionKeyHandler ); |
| | + pane.filenameProperty().setValue( path.getFileName().toString() ); |
| | + pane.setTooltip( tooltipPath ); |
| | + |
| | + interpolateResolvedMap(); |
| | + } catch( final Exception ex ) { |
| | + alert( ex ); |
| | + } |
| | + } |
| | + |
| | + private void exportDefinitions( final Path path ) { |
| | + try { |
| | + final var pane = getDefinitionPane(); |
| | + final var root = pane.getTreeView().getRoot(); |
| | + final var problemChild = pane.isTreeWellFormed(); |
| | + |
| | + if( problemChild == null ) { |
| | + getDefinitionSource().getTreeAdapter().export( root, path ); |
| | + clearAlert(); |
| | + } |
| | + else { |
| | + alert( get( "yaml.error.tree.form", problemChild.getValue() ) ); |
| | + } |
| | + } catch( final Exception ex ) { |
| | + alert( ex ); |
| | + } |
| | + } |
| | + |
| | + private void interpolateResolvedMap() { |
| | + final var treeMap = getDefinitionPane().toMap(); |
| | + final var map = new HashMap<>( treeMap ); |
| | + MapInterpolator.interpolate( map ); |
| | + |
| | + getResolvedMap().clear(); |
| | + getResolvedMap().putAll( map ); |
| | + } |
| | + |
| | + private void initDefinitionPane() { |
| | + openDefinitions( getDefinitionPath() ); |
| | + } |
| | + |
| | + //---- File actions ------------------------------------------------------- |
| | + |
| | + /** |
| | + * Called when an {@link Observable} instance has changed. This is called |
| | + * by both the {@link Snitch} service and the notify service. The @link |
| | + * Snitch} service can be called for different file types, including |
| | + * {@link DefinitionSource} instances. |
| | + * |
| | + * @param observable The observed instance. |
| | + * @param value The noteworthy item. |
| | + */ |
| | + @Override |
| | + public void update( final Observable observable, final Object value ) { |
| | + if( value != null ) { |
| | + if( observable instanceof Snitch && value instanceof Path ) { |
| | + updateSelectedTab(); |
| | + } |
| | + } |
| | } |
| | |