Dave Jarvis' Repositories

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

Undo and Redo added to menu and toolbar

AuthorKarl Tauber <email>
Date2015-07-28 15:14:00 GMT+0200
Commit8c7e3f9c0fca72c35e467ac88234e81c235b16b6
Parentf546794
Delta82 lines added, 4 lines removed, 78-line increase
src/main/java/org/markdownwriterfx/FileEditor.java
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
+import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyBooleanWrapper;
+import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.Tooltip;
import javafx.scene.text.Text;
+import org.fxmisc.undo.UndoManager;
import org.markdownwriterfx.editor.MarkdownEditorPane;
import org.markdownwriterfx.preview.MarkdownPreviewPane;
boolean isModified() { return modified.get(); }
ReadOnlyBooleanProperty modifiedProperty() { return modified; }
+
+ // 'canUndo' property
+ private final BooleanProperty canUndo = new SimpleBooleanProperty();
+ BooleanProperty canUndoProperty() { return canUndo; }
+
+ // 'canRedo' property
+ private final BooleanProperty canRedo = new SimpleBooleanProperty();
+ BooleanProperty canRedoProperty() { return canRedo; }
private void updateTab() {
markdownPreviewPane.scrollYProperty().bind(markdownEditorPane.scrollYProperty());
- // bind the editor undo manager to the 'modified' property
- modified.bind(Bindings.not(markdownEditorPane.getUndoManager().atMarkedPositionProperty()));
+ // bind the editor undo manager to the properties
+ UndoManager undoManager = markdownEditorPane.getUndoManager();
+ modified.bind(Bindings.not(undoManager.atMarkedPositionProperty()));
+ canUndo.bind(undoManager.undoAvailableProperty());
+ canRedo.bind(undoManager.redoAvailableProperty());
SplitPane splitPane = new SplitPane(markdownEditorPane.getNode(), markdownPreviewPane.getNode());
return false;
}
+ }
+
+ void undo() {
+ if (markdownEditorPane != null)
+ markdownEditorPane.getUndoManager().undo();
+ }
+
+ void redo() {
+ if (markdownEditorPane != null)
+ markdownEditorPane.getUndoManager().redo();
}
}
src/main/java/org/markdownwriterfx/MainWindow.java
import javafx.beans.binding.Bindings;
+import javafx.beans.property.BooleanProperty;
+import javafx.beans.property.SimpleBooleanProperty;
+import javafx.beans.value.ObservableBooleanValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
+import javafx.scene.control.Separator;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToolBar;
import de.jensd.fx.glyphs.GlyphsDude;
import static de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon.*;
+import java.util.function.Function;
/**
new SeparatorMenuItem(),
fileExitMenuItem);
+
+ // Edit menu
+ MenuItem editUndoMenuItem = createMenuItem("Undo", "Shortcut+Z", UNDO, e -> editUndo());
+ MenuItem editRedoMenuItem = createMenuItem("Redo", "Shortcut+Y", REPEAT, e -> editRedo());
+
+ editUndoMenuItem.disableProperty().bind(createActiveBooleanProperty(FileEditor::canUndoProperty).not());
+ editRedoMenuItem.disableProperty().bind(createActiveBooleanProperty(FileEditor::canRedoProperty).not());
+
+ Menu editMenu = new Menu("Edit", null,
+ editUndoMenuItem,
+ editRedoMenuItem);
// Help menu
MenuItem helpAboutMenuItem = createMenuItem("About Markdown Writer FX", null, null, e -> helpAbout());
Menu helpMenu = new Menu("Help", null,
helpAboutMenuItem);
- return new MenuBar(fileMenu, helpMenu);
+ return new MenuBar(fileMenu, editMenu, 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());
fileSaveButton.disableProperty().bind(Bindings.not(fileEditorTabPane.activeFileEditorModifiedProperty()));
+
+ editUndoButton.disableProperty().bind(createActiveBooleanProperty(FileEditor::canUndoProperty).not());
+ editRedoButton.disableProperty().bind(createActiveBooleanProperty(FileEditor::canRedoProperty).not());
return new ToolBar(
fileNewButton,
fileOpenButton,
- fileSaveButton);
+ fileSaveButton,
+ new Separator(),
+ editUndoButton,
+ editRedoButton);
}
button.setOnAction(action);
return button;
+ }
+
+ /**
+ * Creates a boolean property that is bound to another boolean value
+ * of the active editor.
+ */
+ private BooleanProperty createActiveBooleanProperty(Function<FileEditor, ObservableBooleanValue> func) {
+ BooleanProperty b = new SimpleBooleanProperty();
+ FileEditor fileEditor = fileEditorTabPane.activeFileEditorProperty().get();
+ if (fileEditor != null)
+ b.bind(func.apply(fileEditor));
+ fileEditorTabPane.activeFileEditorProperty().addListener((observable, oldFileEditor, newFileEditor) -> {
+ b.unbind();
+ if (newFileEditor != null)
+ b.bind(func.apply(newFileEditor));
+ else
+ b.set(false);
+ });
+ return b;
}
Window window = scene.getWindow();
Event.fireEvent(window, new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
+ }
+
+ //---- Edit menu ----------------------------------------------------------
+
+ private void editUndo() {
+ fileEditorTabPane.activeFileEditorProperty().get().undo();
+ }
+
+ private void editRedo() {
+ fileEditorTabPane.activeFileEditorProperty().get().redo();
}