Skip to content

Commit

Permalink
Refactor EntryEvents - removal part (#5410)
Browse files Browse the repository at this point in the history
* Initial changes to remove entry functionality

* Fix logic error in KeyChangeListener

* Fix logic error in BasePanel

* Changes to tests

* Fix typo

* Move remove entry calls to batch version

* Un-propagate for loop in KeyChangeListener

* Finalize changes to SharedEntriesNotPresentEvent

* Fix bug

* Fix other compile errors

* Fix bug and update tests

* Fix tests

* Fix test omission

* Update l10n

* For loop to citationStyle

* Add comment for method not working

* Clarify var name

* Allow single entry for undo

* Replace compound edit undo with normal undo in BasePanel

* Typo

* Simplify loop in DBMSSynchronizer

* Use if instead of stream

* Pluralize Javadoc

* EntryEvent -> EntriesEvent in Javadoc, comments, and var names

* Make imports explicit in BasePanel

* Final EntriesRemovedEvent fixes

* Fix checkStyleMain

* More checkStyle fixes

* More fixes

* Move List coercion into DuplicateSearchResult method

* Fix typo

* Fix pesky BibDatabaseTest error with setStrings

* Fixed BibDatabase Javadoc
  • Loading branch information
abepolk authored and tobiasdiez committed Nov 11, 2019
1 parent 204d922 commit 3d8a09d
Show file tree
Hide file tree
Showing 36 changed files with 428 additions and 330 deletions.
39 changes: 16 additions & 23 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.citationstyle.CitationStyleCache;
Expand All @@ -80,15 +80,15 @@
import org.jabref.model.database.KeyCollisionException;
import org.jabref.model.database.event.BibDatabaseContextChangedEvent;
import org.jabref.model.database.event.CoarseChangeFilter;
import org.jabref.model.database.event.EntriesRemovedEvent;
import org.jabref.model.database.event.EntryAddedEvent;
import org.jabref.model.database.event.EntryRemovedEvent;
import org.jabref.model.database.shared.DatabaseLocation;
import org.jabref.model.database.shared.DatabaseSynchronizer;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FileFieldParser;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.event.EntriesEventSource;
import org.jabref.model.entry.event.EntryChangedEvent;
import org.jabref.model.entry.event.EntryEventSource;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.SpecialField;
Expand Down Expand Up @@ -408,19 +408,9 @@ private void delete(boolean cut, List<BibEntry> entries) {
return;
}

NamedCompound compound;
if (cut) {
compound = new NamedCompound((entries.size() > 1 ? Localization.lang("cut entries") : Localization.lang("cut entry")));
} else {
compound = new NamedCompound((entries.size() > 1 ? Localization.lang("delete entries") : Localization.lang("delete entry")));
}
for (BibEntry entry : entries) {
compound.addEdit(new UndoableRemoveEntry(bibDatabaseContext.getDatabase(), entry));
bibDatabaseContext.getDatabase().removeEntry(entry);
ensureNotShowingBottomPanel(entry);
}
compound.end();
getUndoManager().addEdit(compound);
getUndoManager().addEdit(new UndoableRemoveEntries(bibDatabaseContext.getDatabase(), entries, cut));
bibDatabaseContext.getDatabase().removeEntries(entries);
ensureNotShowingBottomPanel(entries);

markBaseChanged();
this.output(formatOutputMessage(cut ? Localization.lang("Cut") : Localization.lang("Deleted"), entries.size()));
Expand Down Expand Up @@ -892,10 +882,13 @@ public void entryEditorClosing() {
}

/**
* Closes the entry editor if it is showing the given entry.
* Closes the entry editor if it is showing any of the given entries.
*/
private void ensureNotShowingBottomPanel(BibEntry entry) {
if (((mode == BasePanelMode.SHOWING_EDITOR) && (entryEditor.getEntry() == entry))) {
private void ensureNotShowingBottomPanel(List<BibEntry> entriesToCheck) {

// This method is not able to close the bottom pane currently

if ((mode == BasePanelMode.SHOWING_EDITOR) && (entriesToCheck.contains(entryEditor.getEntry()))) {
closeBottomPane();
}
}
Expand Down Expand Up @@ -1128,7 +1121,7 @@ private class GroupTreeListener {
@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
// if the added entry is an undo don't add it to the current group
if (addedEntryEvent.getEntryEventSource() == EntryEventSource.UNDO) {
if (addedEntryEvent.getEntriesEventSource() == EntriesEventSource.UNDO) {
return;
}

Expand All @@ -1144,8 +1137,8 @@ public void listen(EntryAddedEvent addedEntryEvent) {
private class EntryRemovedListener {

@Subscribe
public void listen(EntryRemovedEvent entryRemovedEvent) {
ensureNotShowingBottomPanel(entryRemovedEvent.getBibEntry());
public void listen(EntriesRemovedEvent entriesRemovedEvent) {
ensureNotShowingBottomPanel(entriesRemovedEvent.getBibEntries());
}
}

Expand Down Expand Up @@ -1183,7 +1176,7 @@ public void listen(EntryChangedEvent entryChangedEvent) {
}

@Subscribe
public void listen(EntryRemovedEvent removedEntryEvent) {
public void listen(EntriesRemovedEvent removedEntriesEvent) {
// IMO only used to update the status (found X entries)
DefaultTaskExecutor.runInJavaFXThread(() -> frame.getGlobalSearchBar().performSearch());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jabref.JabRefGUI;
import org.jabref.gui.preview.PreviewViewer;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.logic.bibtex.DuplicateCheck;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -40,7 +40,7 @@ public EntryDeleteChangeViewModel(BibEntry memEntry, BibEntry tmpEntry) {
@Override
public void makeChange(BibDatabaseContext database, NamedCompound undoEdit) {
database.getDatabase().removeEntry(memEntry);
undoEdit.addEdit(new UndoableRemoveEntry(database.getDatabase(), memEntry));
undoEdit.addEdit(new UndoableRemoveEntries(database.getDatabase(), memEntry));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -23,7 +22,7 @@
import org.jabref.gui.duplicationFinder.DuplicateResolverDialog.DuplicateResolverType;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.bibtex.DuplicateCheck;
Expand Down Expand Up @@ -162,10 +161,8 @@ private void handleDuplicates(DuplicateSearchResult result) {
final NamedCompound compoundEdit = new NamedCompound(Localization.lang("duplicate removal"));
// Now, do the actual removal:
if (!result.getToRemove().isEmpty()) {
for (BibEntry entry : result.getToRemove()) {
panel.getDatabase().removeEntry(entry);
compoundEdit.addEdit(new UndoableRemoveEntry(panel.getDatabase(), entry));
}
compoundEdit.addEdit(new UndoableRemoveEntries(panel.getDatabase(), result.getToRemove()));
panel.getDatabase().removeEntries(result.getToRemove());
panel.markBaseChanged();
}
// and adding merged entries:
Expand Down Expand Up @@ -196,8 +193,8 @@ class DuplicateSearchResult {

private int duplicates = 0;

public synchronized Collection<BibEntry> getToRemove() {
return toRemove.values();
public synchronized List<BibEntry> getToRemove() {
return new ArrayList<>(toRemove.values());
}

public synchronized List<BibEntry> getToAdd() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.mergeentries;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

Expand All @@ -10,7 +11,7 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;

Expand Down Expand Up @@ -57,10 +58,9 @@ public void execute() {
// Remove the other two entries and add them to the undo stack (which is not working...)
NamedCompound ce = new NamedCompound(Localization.lang("Merge entries"));
ce.addEdit(new UndoableInsertEntry(basePanel.getDatabase(), mergedEntry.get()));
ce.addEdit(new UndoableRemoveEntry(basePanel.getDatabase(), one));
basePanel.getDatabase().removeEntry(one);
ce.addEdit(new UndoableRemoveEntry(basePanel.getDatabase(), two));
basePanel.getDatabase().removeEntry(two);
List<BibEntry> entriesToRemove = Arrays.asList(one, two);
ce.addEdit(new UndoableRemoveEntries(basePanel.getDatabase(), entriesToRemove));
basePanel.getDatabase().removeEntries(entriesToRemove);
ce.end();
basePanel.getUndoManager().addEdit(ce);

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/gui/shared/SharedDatabaseUIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import org.jabref.gui.entryeditor.EntryEditor;
import org.jabref.gui.exporter.SaveDatabaseAction;
import org.jabref.gui.mergeentries.MergeEntriesDialog;
import org.jabref.gui.undo.UndoableRemoveEntry;
import org.jabref.gui.undo.UndoableRemoveEntries;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.shared.DBMSConnection;
import org.jabref.logic.shared.DBMSConnectionProperties;
import org.jabref.logic.shared.DBMSSynchronizer;
import org.jabref.logic.shared.event.ConnectionLostEvent;
import org.jabref.logic.shared.event.SharedEntryNotPresentEvent;
import org.jabref.logic.shared.event.SharedEntriesNotPresentEvent;
import org.jabref.logic.shared.event.UpdateRefusedEvent;
import org.jabref.logic.shared.exception.InvalidDBMSConnectionPropertiesException;
import org.jabref.logic.shared.exception.NotASharedDatabaseException;
Expand Down Expand Up @@ -119,13 +119,13 @@ public void listen(UpdateRefusedEvent updateRefusedEvent) {
}

@Subscribe
public void listen(SharedEntryNotPresentEvent event) {
public void listen(SharedEntriesNotPresentEvent event) {
BasePanel panel = jabRefFrame.getCurrentBasePanel();
EntryEditor entryEditor = panel.getEntryEditor();

panel.getUndoManager().addEdit(new UndoableRemoveEntry(panel.getDatabase(), event.getBibEntry()));
panel.getUndoManager().addEdit(new UndoableRemoveEntries(panel.getDatabase(), event.getBibEntries()));

if (Objects.nonNull(entryEditor) && (entryEditor.getEntry() == event.getBibEntry())) {
if (Objects.nonNull(entryEditor) && (event.getBibEntries().contains(entryEditor.getEntry()))) {

dialogService.showInformationDialogAndWait(Localization.lang("Shared entry is no longer present"),
Localization.lang("The entry you currently work on has been deleted on the shared side.")
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/org/jabref/gui/undo/UndoableRemoveEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.jabref.gui.undo;

import java.util.Collections;
import java.util.List;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.EntriesEventSource;
import org.jabref.model.strings.StringUtil;

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

/**
* This class represents the removal of entries. The constructor needs
* references to the database, the entries, and the map of open entry editors.
* TODO is this map still being used?
* The latter to be able to close the entry's editor if it is opened after
* an undo, and the removal is then undone.
*/
public class UndoableRemoveEntries extends AbstractUndoableJabRefEdit {

private static final Logger LOGGER = LoggerFactory.getLogger(UndoableRemoveEntries.class);
private final BibDatabase base;
private final List<BibEntry> entries;
private final boolean cut;

public UndoableRemoveEntries(BibDatabase base, BibEntry entry) {
this(base, Collections.singletonList(entry));
}

public UndoableRemoveEntries(BibDatabase base, List<BibEntry> entries) {
this(base, entries, false);
}

public UndoableRemoveEntries(BibDatabase base, List<BibEntry> entries, boolean cut) {
this.base = base;
this.entries = entries;
this.cut = cut;
}

@Override
public String getPresentationName() {
if (cut) {
if (entries.size() > 1) {
return Localization.lang("cut entries");
} else if (entries.size() == 1) {
return Localization.lang("cut entry %0",
StringUtil.boldHTML(entries.get(0).getCiteKeyOptional().orElse(Localization.lang("undefined"))));
} else {
return null;
}
} else {
if (entries.size() > 1) {
return Localization.lang("remove entries");
} else if (entries.size() == 1) {
return Localization.lang("remove entry %0",
StringUtil.boldHTML(entries.get(0).getCiteKeyOptional().orElse(Localization.lang("undefined"))));
} else {
return null;
}
}
}

@Override
public void undo() {
super.undo();
base.insertEntries(entries, EntriesEventSource.UNDO);
}

@Override
public void redo() {
super.redo();

// Redo the change.
try {
base.removeEntries(entries);
} catch (Throwable ex) {
LOGGER.warn("Problem to redo `remove entries`", ex);
}
}

}
53 changes: 0 additions & 53 deletions src/main/java/org/jabref/gui/undo/UndoableRemoveEntry.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Objects;

import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.EntryRemovedEvent;
import org.jabref.model.database.event.EntriesRemovedEvent;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.EntryChangedEvent;

Expand Down Expand Up @@ -62,11 +62,13 @@ public void listen(EntryChangedEvent entryChangedEvent) {
}

/**
* removes the citation of the removed entry as it's not needed anymore
* removes the citation of the removed entries as they are not needed anymore
*/
@Subscribe
public void listen(EntryRemovedEvent entryRemovedEvent) {
citationStyleCache.invalidate(entryRemovedEvent.getBibEntry());
public void listen(EntriesRemovedEvent entriesRemovedEvent) {
for (BibEntry entry : entriesRemovedEvent.getBibEntries()) {
citationStyleCache.invalidate(entry);
}
}
}
}
Loading

0 comments on commit 3d8a09d

Please sign in to comment.