Skip to content

Commit

Permalink
Enable/Disable the undo and redo toolbar/menu buttons correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
HoussemNasri committed Sep 13, 2024
1 parent 14ba56d commit 1ed366d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
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) -> {
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

0 comments on commit 1ed366d

Please sign in to comment.