Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

'Bold' and 'Insert' actions added to menu/toolbar

AuthorKarl Tauber <email>
Date2015-07-30 17:17:03 GMT+0200
Commitcd8027253a764ef4ed60e16a05d7417d9df63603
Parentaa786ce
Delta71 lines added, 9 lines removed, 62-line increase
src/main/java/org/markdownwriterfx/editor/MarkdownEditorPane.java
import javafx.event.EventHandler;
import javafx.scene.Node;
+import javafx.scene.control.IndexRange;
import javafx.scene.control.ScrollBar;
import javafx.scene.input.KeyEvent;
private void applyHighlighting(RootNode astRoot) {
MarkdownSyntaxHighlighter.highlight(textArea, astRoot);
+ }
+
+ public void surroundSelection(String leading, String trailing) {
+ // Note: not using textArea.insertText() to insert leading and trailing
+ // because this would add two changes to undo history
+
+ IndexRange selection = textArea.getSelection();
+ int start = selection.getStart();
+ int end = selection.getEnd();
+
+ String selectedText = textArea.getSelectedText();
+
+ // remove leading and trailing whitespaces from selected text
+ String trimmedSelectedText = selectedText.trim();
+ if (trimmedSelectedText.length() < selectedText.length()) {
+ start += selectedText.indexOf(trimmedSelectedText);
+ end = start + trimmedSelectedText.length();
+ }
+
+ textArea.replaceText(start, end, leading + trimmedSelectedText + trailing);
+ textArea.selectRange(start + leading.length(), end + leading.length());
}
}
src/main/java/org/markdownwriterfx/FileEditorTabPane.java
}
+ // 'activeFileEditor' property
+ FileEditor getActiveFileEditor() { return activeFileEditor.get(); }
ReadOnlyObjectProperty<FileEditor> activeFileEditorProperty() {
return activeFileEditor.getReadOnlyProperty();
}
+ // 'anyFileEditorModified' property
ReadOnlyBooleanProperty anyFileEditorModifiedProperty() {
return anyFileEditorModified.getReadOnlyProperty();
src/main/java/org/markdownwriterfx/MainWindow.java
import javafx.beans.binding.Bindings;
+import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
MenuItem fileExitMenuItem = createMenuItem("Exit", null, null, e -> fileExit());
- fileCloseMenuItem.disableProperty().bind(fileEditorTabPane.activeFileEditorProperty().isNull());
- fileCloseAllMenuItem.disableProperty().bind(fileEditorTabPane.activeFileEditorProperty().isNull());
+ BooleanBinding activeFileEditorIsNull = fileEditorTabPane.activeFileEditorProperty().isNull();
+ fileCloseMenuItem.disableProperty().bind(activeFileEditorIsNull);
+ fileCloseAllMenuItem.disableProperty().bind(activeFileEditorIsNull);
fileSaveMenuItem.disableProperty().bind(createActiveBooleanProperty(FileEditor::modifiedProperty).not());
fileSaveAllMenuItem.disableProperty().bind(Bindings.not(fileEditorTabPane.anyFileEditorModifiedProperty()));
editUndoMenuItem,
editRedoMenuItem);
+
+ // Insert menu
+ MenuItem insertBoldMenuItem = createMenuItem("Bold", "Shortcut+B", BOLD, e -> insertBold());
+ MenuItem insertItalicMenuItem = createMenuItem("Italic", "Shortcut+I", ITALIC, e -> insertItalic());
+
+ insertBoldMenuItem.disableProperty().bind(activeFileEditorIsNull);
+ insertItalicMenuItem.disableProperty().bind(activeFileEditorIsNull);
+
+ Menu insertMenu = new Menu("Insert", null,
+ insertBoldMenuItem,
+ insertItalicMenuItem);
// Help menu
MenuItem helpAboutMenuItem = createMenuItem("About Markdown Writer FX", null, null, e -> helpAbout());
Menu helpMenu = new Menu("Help", null,
helpAboutMenuItem);
- return new MenuBar(fileMenu, editMenu, helpMenu);
+ return new MenuBar(fileMenu, editMenu, insertMenu, helpMenu);
}
private ToolBar createToolBar() {
Button fileNewButton = createToolBarButton(FILE_ALT, "New", "Shortcut+N", e -> fileNew());
Button fileOpenButton = createToolBarButton(FOLDER_OPEN_ALT, "Open", "Shortcut+O", e -> fileOpen());
Button fileSaveButton = createToolBarButton(FLOPPY_ALT, "Save", "Shortcut+S", e -> fileSave());
Button editUndoButton = createToolBarButton(UNDO, "Undo", "Shortcut+Z", e -> editUndo());
Button editRedoButton = createToolBarButton(REPEAT, "Redo", "Shortcut+Y", e -> editRedo());
+
+ Button insertBoldButton = createToolBarButton(BOLD, "Bold", "Shortcut+B", e -> insertBold());
+ Button insertItalicButton = createToolBarButton(ITALIC, "Italic", "Shortcut+I", e -> insertItalic());
fileSaveButton.disableProperty().bind(createActiveBooleanProperty(FileEditor::modifiedProperty).not());
editUndoButton.disableProperty().bind(createActiveBooleanProperty(FileEditor::canUndoProperty).not());
editRedoButton.disableProperty().bind(createActiveBooleanProperty(FileEditor::canRedoProperty).not());
+
+ BooleanBinding activeFileEditorIsNull = fileEditorTabPane.activeFileEditorProperty().isNull();
+ insertBoldButton.disableProperty().bind(activeFileEditorIsNull);
+ insertItalicButton.disableProperty().bind(activeFileEditorIsNull);
return new ToolBar(
fileNewButton,
fileOpenButton,
fileSaveButton,
new Separator(),
editUndoButton,
- editRedoButton);
+ editRedoButton,
+ new Separator(),
+ insertBoldButton,
+ insertItalicButton);
}
private BooleanProperty createActiveBooleanProperty(Function<FileEditor, ObservableBooleanValue> func) {
BooleanProperty b = new SimpleBooleanProperty();
- FileEditor fileEditor = fileEditorTabPane.activeFileEditorProperty().get();
+ FileEditor fileEditor = fileEditorTabPane.getActiveFileEditor();
if (fileEditor != null)
b.bind(func.apply(fileEditor));
editorShortcuts = EventHandlerHelper
+ // File
.on(EventPattern.keyPressed(KeyCode.N, KeyCombination.SHORTCUT_DOWN)).act(e -> fileNew())
.on(EventPattern.keyPressed(KeyCode.O, KeyCombination.SHORTCUT_DOWN)).act(e -> fileOpen())
.on(EventPattern.keyPressed(KeyCode.W, KeyCombination.SHORTCUT_DOWN)).act(e -> fileClose())
.on(EventPattern.keyPressed(KeyCode.S, KeyCombination.SHORTCUT_DOWN)).act(e -> fileSave())
.on(EventPattern.keyPressed(KeyCode.S, KeyCombination.SHORTCUT_DOWN, KeyCombination.SHIFT_DOWN)).act(e -> fileSaveAll())
+ // Insert
+ .on(EventPattern.keyPressed(KeyCode.B, KeyCombination.SHORTCUT_DOWN)).act(e -> insertBold())
+ .on(EventPattern.keyPressed(KeyCode.I, KeyCombination.SHORTCUT_DOWN)).act(e -> insertItalic())
.create();
return editorShortcuts;
private void fileClose() {
- fileEditorTabPane.closeEditor(fileEditorTabPane.activeFileEditorProperty().get());
+ fileEditorTabPane.closeEditor(fileEditorTabPane.getActiveFileEditor());
}
private void fileCloseAll() {
fileEditorTabPane.closeAllEditors();
}
private void fileSave() {
- fileEditorTabPane.saveEditor(fileEditorTabPane.activeFileEditorProperty().get());
+ fileEditorTabPane.saveEditor(fileEditorTabPane.getActiveFileEditor());
}
private void editUndo() {
- fileEditorTabPane.activeFileEditorProperty().get().undo();
+ fileEditorTabPane.getActiveFileEditor().undo();
}
private void editRedo() {
- fileEditorTabPane.activeFileEditorProperty().get().redo();
+ fileEditorTabPane.getActiveFileEditor().redo();
+ }
+
+ //---- Insert menu --------------------------------------------------------
+
+ private void insertBold() {
+ fileEditorTabPane.getActiveFileEditor().getEditor().surroundSelection("**", "**");
+ }
+
+ private void insertItalic() {
+ fileEditorTabPane.getActiveFileEditor().getEditor().surroundSelection("*", "*");
}