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

Enable/Disable the undo and redo toolbar/menu buttons correctly #11758

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- The browse button for a Custom theme now opens in the directory of the current used CSS file. [#11597](https://github.com/JabRef/jabref/pull/11597)
- The browse button for a Custom exporter now opens in the directory of the current used exporter file. [#11717](https://github.com/JabRef/jabref/pull/11717)
- We improved the display of long messages in the integrity check dialog. [#11619](https://github.com/JabRef/jabref/pull/11619)
- We improved the undo/redo buttons in the main toolbar and main menu to be disabled when there is nothing to undo/redo. [#8807](https://github.com/JabRef/jabref/issues/8807)

### Fixed

Expand Down
62 changes: 54 additions & 8 deletions src/main/java/org/jabref/gui/undo/CountingUndoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,81 @@
import javax.swing.undo.UndoManager;
import javax.swing.undo.UndoableEdit;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;

public class CountingUndoManager extends UndoManager {

private int unchangedPoint;
private int current;

/**
* Indicates the number of edits aka balance of edits on the stack +1 when an edit is added/redone and -1 when an edit is undoed.
* */
private final IntegerProperty balanceProperty = new SimpleIntegerProperty(0);
private final BooleanProperty undoableProperty = new SimpleBooleanProperty(false);
private final BooleanProperty redoableProperty = new SimpleBooleanProperty(false);

@Override
public synchronized boolean addEdit(UndoableEdit edit) {
current++;
boolean result = super.addEdit(edit);
return result;
boolean editAdded = super.addEdit(edit);
if (editAdded) {
incrementBalance();
updateUndoableStatus();
updateRedoableStatus();
return true;
} else {
return false;
}
}

@Override
public synchronized void undo() throws CannotUndoException {
super.undo();
current--;
decrementBalance();
updateUndoableStatus();
updateRedoableStatus();
}

@Override
public synchronized void redo() throws CannotUndoException {
super.redo();
current++;
incrementBalance();
updateUndoableStatus();
updateRedoableStatus();
}

public synchronized void markUnchanged() {
unchangedPoint = current;
unchangedPoint = balanceProperty.get();
}

public synchronized boolean hasChanged() {
return current != unchangedPoint;
return balanceProperty.get() != unchangedPoint;
}

private void incrementBalance() {
balanceProperty.setValue(balanceProperty.getValue() + 1);
}

private void decrementBalance() {
balanceProperty.setValue(balanceProperty.getValue() - 1);
}

private void updateUndoableStatus() {
undoableProperty.setValue(canUndo());
}

private void updateRedoableStatus() {
redoableProperty.setValue(canRedo());
}

public ReadOnlyBooleanProperty getUndoableProperty() {
return undoableProperty;
}

public ReadOnlyBooleanProperty getRedoableProperty() {
return redoableProperty;
}
}
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/gui/undo/RedoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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.logic.l10n.Localization;

Expand All @@ -22,9 +21,10 @@ public RedoAction(Supplier<LibraryTab> tabSupplier, DialogService dialogService,
this.tabSupplier = tabSupplier;
this.dialogService = dialogService;

// 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));
stateManager.activeTabProperty().addListener((observable, oldValue, activeLibraryTab) -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will add a listener after each tab change. The old listener should be removed. Otherwise, memory consumption will increase IMHO.

activeLibraryTab.ifPresent(libraryTab ->
this.executable.bind(libraryTab.getUndoManager().getRedoableProperty()));
});
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/gui/undo/UndoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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.logic.l10n.Localization;

Expand All @@ -22,9 +21,10 @@ public UndoAction(Supplier<LibraryTab> tabSupplier, DialogService dialogService,
this.tabSupplier = tabSupplier;
this.dialogService = dialogService;

// 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));
stateManager.activeTabProperty().addListener((observable, oldValue, activeLibraryTab) -> {
activeLibraryTab.ifPresent(libraryTab ->
this.executable.bind(libraryTab.getUndoManager().getUndoableProperty()));
});
}

@Override
Expand Down
Loading