Skip to content

Commit

Permalink
Used EventBus for undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Jul 26, 2016
1 parent f69b7ea commit 138928e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 35 deletions.
74 changes: 63 additions & 11 deletions src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import net.sf.jabref.gui.menus.RightClickMenu;
import net.sf.jabref.gui.openoffice.OpenOfficePanel;
import net.sf.jabref.gui.preftabs.PreferencesDialog;
import net.sf.jabref.gui.undo.CountingUndoManager;
import net.sf.jabref.gui.util.FocusRequester;
import net.sf.jabref.gui.util.PositionWindow;
import net.sf.jabref.gui.worker.MarkEntriesAction;
Expand All @@ -122,6 +123,8 @@
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.logging.GuiAppender;
import net.sf.jabref.logic.preferences.LastFocusedTabPreferences;
import net.sf.jabref.logic.undo.AddUndoEvent;
import net.sf.jabref.logic.undo.UndoRedoEvent;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.database.BibDatabaseMode;
Expand All @@ -139,6 +142,7 @@
import net.sf.jabref.specialfields.SpecialFieldsUtils;
import net.sf.jabref.sql.importer.DbImportAction;

import com.google.common.eventbus.Subscribe;
import com.jgoodies.looks.HeaderStyle;
import com.jgoodies.looks.Options;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -640,6 +644,7 @@ public void windowClosing(WindowEvent e) {
bp.updateSearchManager();
// Set correct enabled state for Back and Forward actions:
bp.setBackAndForwardEnabledState();
bp.getUndoManager().triggerEvent();
new FocusRequester(bp.getMainTable());
});

Expand All @@ -653,6 +658,7 @@ public void windowClosing(WindowEvent e) {
LOGGER.fatal("Could not interface with Mac OS X methods.", e);
}
}

}

private void positionWindowOnScreen() {
Expand Down Expand Up @@ -1638,6 +1644,10 @@ public void addTab(BasePanel bp, boolean raisePanel) {
if (raisePanel) {
tabbedPane.setSelectedComponent(bp);
}

// Register undo/redo listener
bp.getUndoManager().registerListener(new UndoRedoEventManager(bp.getUndoManager()));
bp.getUndoManager().triggerEvent();
}

public BasePanel addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
Expand Down Expand Up @@ -2248,19 +2258,61 @@ public void setPreviewToggle(boolean enabled) {
previewToggle.setSelected(enabled);
}

public void setUndoText(String undoPresentationName) {
undo.putValue(Action.SHORT_DESCRIPTION, undoPresentationName);
}

public void setRedoText(String undoPresentationName) {
redo.putValue(Action.SHORT_DESCRIPTION, undoPresentationName);
}
private class UndoRedoEventManager {

public void enableUndo(boolean value) {
undo.setEnabled(value);
}
private final CountingUndoManager undoManager;


public UndoRedoEventManager(CountingUndoManager undoManager) {
this.undoManager = undoManager;
}

@Subscribe
public void listen(UndoRedoEvent event) {
updateTexts();
JabRefFrame.this.getCurrentBasePanel().updateEntryEditorIfShowing();
}

@Subscribe
public void listen(AddUndoEvent event) {
updateTexts();
}

private void updateTexts() {
if (JabRefFrame.this != null) {
if (undoManager.canUndo()) {
setUndoText(undoManager.getUndoPresentationName());
enableUndo(true);
} else {
setUndoText(Localization.lang("Undo"));
enableUndo(false);
}

if (undoManager.canRedo()) {
setRedoText(undoManager.getRedoPresentationName());
enableRedo(true);
} else {
setRedoText(Localization.lang("Redo"));
enableRedo(false);
}
}
}

public void enableRedo(boolean value) {
redo.setEnabled(value);
private void setUndoText(String undoPresentationName) {
undo.putValue(Action.SHORT_DESCRIPTION, undoPresentationName);
}

private void setRedoText(String undoPresentationName) {
redo.putValue(Action.SHORT_DESCRIPTION, undoPresentationName);
}

private void enableUndo(boolean value) {
undo.setEnabled(value);
}

private void enableRedo(boolean value) {
redo.setEnabled(value);
}
}
}
45 changes: 21 additions & 24 deletions src/main/java/net/sf/jabref/gui/undo/CountingUndoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,46 @@
import javax.swing.undo.UndoableEdit;

import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.undo.AddUndoEvent;
import net.sf.jabref.logic.undo.UndoRedoEvent;

import com.google.common.eventbus.EventBus;

public class CountingUndoManager extends UndoManager {

private int unchangedPoint;
private int current;
private final BasePanel panel;

private final EventBus eventBus = new EventBus();


public CountingUndoManager(BasePanel basePanel) {
super();
panel = Objects.requireNonNull(basePanel);
updateTexts();
eventBus.post(new UndoRedoEvent());
}

@Override
public synchronized boolean addEdit(UndoableEdit edit) {
current++;
boolean returnvalue = super.addEdit(edit);
updateTexts();
eventBus.post(new AddUndoEvent());
return returnvalue;
}

@Override
public synchronized void undo() throws CannotUndoException {
super.undo();
current--;
updateTexts();
panel.updateEntryEditorIfShowing();
eventBus.post(new UndoRedoEvent());
}

@Override
public synchronized void redo() throws CannotUndoException {
super.redo();
current++;
updateTexts();
eventBus.post(new UndoRedoEvent());
panel.updateEntryEditorIfShowing();
}

Expand All @@ -69,23 +73,16 @@ public synchronized boolean hasChanged() {
return (current != unchangedPoint);
}

private void updateTexts() {
if (panel.frame() != null) {
if (super.canUndo()) {
panel.frame().setUndoText(super.editToBeUndone().getUndoPresentationName());
panel.frame().enableUndo(true);
} else {
panel.frame().setUndoText(Localization.lang("Undo"));
panel.frame().enableUndo(false);
}

if (super.canRedo()) {
panel.frame().setRedoText(super.editToBeRedone().getRedoPresentationName());
panel.frame().enableRedo(true);
} else {
panel.frame().setRedoText(Localization.lang("Redo"));
panel.frame().enableRedo(false);
}
}

public void registerListener(Object object) {
this.eventBus.register(object);
}

public void unregisterListener(Object object) {
this.eventBus.unregister(object);
}

public void triggerEvent() {
eventBus.post(new UndoRedoEvent());
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/sf/jabref/logic/undo/AddUndoEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.sf.jabref.logic.undo;


public class AddUndoEvent {

public AddUndoEvent() {
// Empty
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/sf/jabref/logic/undo/UndoRedoEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.sf.jabref.logic.undo;


public class UndoRedoEvent {

public UndoRedoEvent() {
// Empty
}
}

0 comments on commit 138928e

Please sign in to comment.