| 30 | 30 | import com.scrivenvar.service.events.Notification; |
| 31 | 31 | import com.scrivenvar.service.events.Notifier; |
| 32 | | import javafx.application.Platform; |
| 33 | 32 | import javafx.beans.binding.Bindings; |
| 34 | 33 | import javafx.beans.property.BooleanProperty; |
| ... |
| 59 | 58 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 60 | 59 | import static java.util.Locale.ENGLISH; |
| 60 | import static javafx.application.Platform.runLater; |
| 61 | 61 | |
| 62 | 62 | /** |
| ... |
| 89 | 89 | setOnSelectionChanged( e -> { |
| 90 | 90 | if( isSelected() ) { |
| 91 | | Platform.runLater( this::activated ); |
| 91 | runLater( this::activated ); |
| 92 | requestFocus(); |
| 92 | 93 | } |
| 93 | 94 | } ); |
| ... |
| 137 | 138 | } |
| 138 | 139 | |
| 139 | | // Switch to the tab without loading if the contents are already in memory. |
| 140 | | if( getContent() != null ) { |
| 141 | | getEditorPane().requestFocus(); |
| 142 | | return; |
| 140 | // If the tab is devoid of content, load it. |
| 141 | if( getContent() == null ) { |
| 142 | readFile(); |
| 143 | initLayout(); |
| 144 | initUndoManager(); |
| 143 | 145 | } |
| 144 | | |
| 145 | | // Load the text and update the preview before the undo manager. |
| 146 | | load(); |
| 147 | | |
| 148 | | // Track undo requests -- can only be called *after* load. |
| 149 | | initUndoManager(); |
| 150 | | initLayout(); |
| 151 | | initFocus(); |
| 152 | 146 | } |
| 153 | 147 | |
| 154 | 148 | private void initLayout() { |
| 155 | 149 | setContent( getScrollPane() ); |
| 156 | | } |
| 157 | | |
| 158 | | private void initFocus() { |
| 159 | | getEditorPane().requestFocus(); |
| 160 | 150 | } |
| 161 | 151 | |
| 152 | /** |
| 153 | * Tracks undo requests, but can only be called <em>after</em> load. |
| 154 | */ |
| 162 | 155 | private void initUndoManager() { |
| 163 | 156 | final UndoManager<?> undoManager = getUndoManager(); |
| 164 | 157 | undoManager.forgetHistory(); |
| 165 | 158 | |
| 166 | 159 | // Bind the editor undo manager to the properties. |
| 167 | 160 | mModified.bind( Bindings.not( undoManager.atMarkedPositionProperty() ) ); |
| 168 | 161 | canUndo.bind( undoManager.undoAvailableProperty() ); |
| 169 | 162 | canRedo.bind( undoManager.redoAvailableProperty() ); |
| 163 | } |
| 164 | |
| 165 | private void requestFocus() { |
| 166 | getEditorPane().requestFocus(); |
| 170 | 167 | } |
| 171 | 168 | |
| ... |
| 242 | 239 | * Reads the entire file contents from the path associated with this tab. |
| 243 | 240 | */ |
| 244 | | private void load() { |
| 241 | private void readFile() { |
| 245 | 242 | final Path path = getPath(); |
| 246 | 243 | final File file = path.toFile(); |