Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ctrl z textfield #11421

Merged
merged 17 commits into from
Jul 8, 2024
Merged
Prev Previous commit
Next Next commit
Refactor UndoRedo to more proper OO
  • Loading branch information
koppor committed Jul 7, 2024
commit 9f99c4a502a872a7d6d785ab5c409836f7b6e99c
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/JabRefDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ public <V> Optional<ButtonType> showBackgroundProgressDialogAndWait(String title

@Override
public void notify(String message) {
// TODO: Change to a notification overview instead of event log when that is available.
// The event log is not that user friendly (different purpose).
LOGGER.info(message);

UiTaskExecutor.runInJavaFXThread(() -> {
Expand All @@ -389,7 +391,6 @@ public void notify(String message) {
.threshold(5,
Notifications.create()
.title(Localization.lang("Last notification"))
// TODO: Change to a notification overview instead of event log when that is available. The event log is not that user friendly (different purpose).
.text(
"(" + Localization.lang("Check the event log to see all notifications") + ")"
+ "\n\n" + message)
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/gui/frame/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
import org.jabref.gui.slr.StartNewStudyAction;
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
import org.jabref.gui.texparser.ParseLatexAction;
import org.jabref.gui.undo.UndoRedoAction;
import org.jabref.gui.undo.RedoAction;
import org.jabref.gui.undo.UndoAction;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.help.HelpFile;
Expand Down Expand Up @@ -174,8 +175,8 @@ private void createMenu() {
);

edit.getItems().addAll(
factory.createMenuItem(StandardActions.UNDO, new UndoRedoAction(StandardActions.UNDO, frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createMenuItem(StandardActions.REDO, new UndoRedoAction(StandardActions.REDO, frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createMenuItem(StandardActions.UNDO, new UndoAction(frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createMenuItem(StandardActions.REDO, new RedoAction( frame::getCurrentLibraryTab, dialogService, stateManager)),
koppor marked this conversation as resolved.
Show resolved Hide resolved

new SeparatorMenuItem(),

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/gui/frame/MainToolBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.jabref.gui.push.PushToApplicationCommand;
import org.jabref.gui.search.GlobalSearchBar;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.UndoRedoAction;
import org.jabref.gui.undo.RedoAction;
import org.jabref.gui.undo.UndoAction;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -122,8 +123,8 @@ private void createToolBar() {
new Separator(Orientation.VERTICAL),

new HBox(
factory.createIconButton(StandardActions.UNDO, new UndoRedoAction(StandardActions.UNDO, frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createIconButton(StandardActions.REDO, new UndoRedoAction(StandardActions.REDO, frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createIconButton(StandardActions.UNDO, new UndoAction(frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createIconButton(StandardActions.REDO, new RedoAction(frame::getCurrentLibraryTab, dialogService, stateManager)),
factory.createIconButton(StandardActions.CUT, new EditAction(StandardActions.CUT, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createIconButton(StandardActions.COPY, new EditAction(StandardActions.COPY, frame::getCurrentLibraryTab, stateManager, undoManager)),
factory.createIconButton(StandardActions.PASTE, new EditAction(StandardActions.PASTE, frame::getCurrentLibraryTab, stateManager, undoManager))),
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/jabref/gui/undo/RedoAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jabref.gui.undo;

import java.util.function.Supplier;

import javax.swing.undo.CannotUndoException;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.logic.l10n.Localization;

public class RedoAction extends UndoRedoAction {
public RedoAction(Supplier<LibraryTab> tabSupplier, DialogService dialogService, StateManager stateManager) {
super(tabSupplier, dialogService, stateManager);
}

@Override
public void execute() {
LibraryTab libraryTab = this.tabSupplier.get();
try {
libraryTab.getUndoManager().redo();
libraryTab.markBaseChanged();
dialogService.notify(Localization.lang("Redo"));
} catch (CannotUndoException ex) {
dialogService.notify(Localization.lang("Nothing to redo") + '.');
}
libraryTab.markChangedOrUnChanged();
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/jabref/gui/undo/UndoAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jabref.gui.undo;

import java.util.function.Supplier;

import javax.swing.undo.CannotUndoException;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.logic.l10n.Localization;

public class UndoAction extends UndoRedoAction {
public UndoAction(Supplier<LibraryTab> tabSupplier, DialogService dialogService, StateManager stateManager) {
super(tabSupplier, dialogService, stateManager);
}

@Override
public void execute() {
LibraryTab libraryTab = this.tabSupplier.get();
try {
libraryTab.getUndoManager().undo();
libraryTab.markBaseChanged();
dialogService.notify(Localization.lang("Undo"));
} catch (CannotUndoException ex) {
dialogService.notify(Localization.lang("Nothing to undo") + '.');
}
libraryTab.markChangedOrUnChanged();
}
}
44 changes: 5 additions & 39 deletions src/main/java/org/jabref/gui/undo/UndoRedoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,28 @@

import java.util.function.Supplier;

import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.actions.StandardActions;
import org.jabref.logic.l10n.Localization;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UndoRedoAction extends SimpleCommand {
public abstract class UndoRedoAction extends SimpleCommand {

private static final Logger LOGGER = LoggerFactory.getLogger(UndoRedoAction.class);

private final StandardActions action;
private final Supplier<LibraryTab> tabSupplier;
private final DialogService dialogService;
protected final Supplier<LibraryTab> tabSupplier;
protected final DialogService dialogService;

public UndoRedoAction(StandardActions action, Supplier<LibraryTab> tabSupplier, DialogService dialogService, StateManager stateManager) {
this.action = action;
public UndoRedoAction(Supplier<LibraryTab> tabSupplier, DialogService dialogService, StateManager stateManager) {
this.tabSupplier = tabSupplier;
this.dialogService = dialogService;

// ToDo: Rework the UndoManager to something like the following, if it had a property.
// TODO: Rework the UndoManager to something like the following, if it had a property.
// this.executable.bind(frame.getCurrentBasePanel().getUndoManager().canUndo())
this.executable.bind(ActionHelper.needsDatabase(stateManager));
}

@Override
public void execute() {
LibraryTab libraryTab = this.tabSupplier.get();
if (action == StandardActions.UNDO) {
try {
libraryTab.getUndoManager().undo();
libraryTab.markBaseChanged();
dialogService.notify(Localization.lang("Undo"));
} catch (CannotUndoException ex) {
dialogService.notify(Localization.lang("Nothing to undo") + '.');
}
this.tabSupplier.get().markChangedOrUnChanged();
} else if (action == StandardActions.REDO) {
try {
libraryTab.getUndoManager().redo();
libraryTab.markBaseChanged();
dialogService.notify(Localization.lang("Redo"));
} catch (CannotRedoException ex) {
dialogService.notify(Localization.lang("Nothing to redo") + '.');
}

libraryTab.markChangedOrUnChanged();
} else {
LOGGER.debug("No undo/redo action: " + action.name());
}
}
}