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

Add change listener to main table to scroll to imported entry #5421

Merged
merged 10 commits into from
Oct 16, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed various issues (including [#5263](https://github.com/JabRef/jabref/issues/5263)) related to copying entries to the clipboard
- We fixed some display errors in the preferences dialog and replaced some of the controls [#5033](https://github.com/JabRef/jabref/pull/5033) [#5047](https://github.com/JabRef/jabref/pull/5047) [#5062](https://github.com/JabRef/jabref/pull/5062) [#5141](https://github.com/JabRef/jabref/pull/5141) [#5185](https://github.com/JabRef/jabref/pull/5185) [#5265](https://github.com/JabRef/jabref/pull/5265) [#5315](https://github.com/JabRef/jabref/pull/5315) [#5360](https://github.com/JabRef/jabref/pull/5360)
- We fixed an exception which occurred when trying to import entries without an open library. [#5447](https://github.com/JabRef/jabref/issues/5447)
- After successful import of one or multiple bib entries the main table scrolls to the first imported entry [#5383](https://github.com/JabRef/jabref/issues/5383)


### Removed
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.util.ControlHelper;
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.ViewModelTableRowFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.UpdateField;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.event.AllInsertsFinishedEvent;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.JabRefPreferences;

import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -68,6 +71,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,

this.model = model;
this.database = Objects.requireNonNull(database);

this.undoManager = panel.getUndoManager();

importHandler = new ImportHandler(
Expand Down Expand Up @@ -127,6 +131,13 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
//model.updateMarkingState(Globals.prefs.getBoolean(JabRefPreferences.FLOAT_MARKED_ENTRIES));

setupKeyBindings(keyBindingRepository);

database.getDatabase().registerListener(this);
}

@Subscribe
public void listen(AllInsertsFinishedEvent event) {
DefaultTaskExecutor.runInJavaFXThread(() -> clearAndSelect(event.getBibEntry()));
}

public void clearAndSelect(BibEntry bibEntry) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import org.jabref.model.database.event.AllInsertsFinishedEvent;
import org.jabref.model.database.event.EntryAddedEvent;
import org.jabref.model.database.event.EntryRemovedEvent;
import org.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -219,6 +220,7 @@ public synchronized void insertEntries(List<BibEntry> entries) throws KeyCollisi
private synchronized void insertEntries(List<BibEntry> newEntries, EntryEventSource eventSource) throws KeyCollisionException {
Objects.requireNonNull(newEntries);

BibEntry firstEntry = null;
for (BibEntry entry : newEntries) {
String id = entry.getId();
if (containsEntryWithId(id)) {
Expand All @@ -229,8 +231,15 @@ private synchronized void insertEntries(List<BibEntry> newEntries, EntryEventSou
entry.registerListener(this);

eventBus.post(new EntryAddedEvent(entry, eventSource));

if (firstEntry == null) {
firstEntry = entry;
}
}
entries.addAll(newEntries);
if (firstEntry != null) {
eventBus.post(new AllInsertsFinishedEvent(firstEntry, eventSource));
}
}

/**
Expand Down Expand Up @@ -563,6 +572,7 @@ public void setEpilog(String epilog) {
* - {@link EntryAddedEvent}
* - {@link EntryChangedEvent}
* - {@link EntryRemovedEvent}
* - {@link AllInsertsFinishedEvent}
*
* @param listener listener (subscriber) to add
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jabref.model.database.event;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.EntryEvent;
import org.jabref.model.entry.event.EntryEventSource;

/**
* {@link AllInsertsFinishedEvent} is fired when insertion of {@link BibEntry} to the {@link BibDatabase} was finished.
*/
public class AllInsertsFinishedEvent extends EntryEvent {

/**
* @param bibEntry the entry which has been added
*/
public AllInsertsFinishedEvent(BibEntry bibEntry) {
super(bibEntry);
}

/**
* @param bibEntry <code>BibEntry</code> object which has been added.
* @param location Location affected by this event
*/
public AllInsertsFinishedEvent(BibEntry bibEntry, EntryEventSource location) {
super(bibEntry, location);
}
}
19 changes: 15 additions & 4 deletions src/test/java/org/jabref/model/database/BibDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,19 @@ public void insertEntryPostsAddedEntryEvent() {
TestEventListener tel = new TestEventListener();
database.registerListener(tel);
database.insertEntry(expectedEntry);
BibEntry actualEntry = tel.getBibEntry();
assertEquals(expectedEntry, actualEntry);
assertEquals(expectedEntry, tel.getAddedEntry());
assertEquals(expectedEntry, tel.getFirstInsertedEntry());
}

@Test
public void insertMultipleEntriesPostsAddedEntryEvent() {
BibEntry firstEntry = new BibEntry();
BibEntry secondEntry = new BibEntry();
TestEventListener tel = new TestEventListener();
database.registerListener(tel);
database.insertEntries(firstEntry, secondEntry);
assertEquals(firstEntry, tel.getFirstInsertedEntry());
assertEquals(secondEntry, tel.getAddedEntry());
}

@Test
Expand All @@ -185,7 +196,7 @@ public void removeEntryPostsRemovedEntryEvent() {
database.insertEntry(expectedEntry);
database.registerListener(tel);
database.removeEntry(expectedEntry);
BibEntry actualEntry = tel.getBibEntry();
BibEntry actualEntry = tel.getRemovedEntry();
assertEquals(expectedEntry, actualEntry);
}

Expand All @@ -198,7 +209,7 @@ public void changingEntryPostsChangeEntryEvent() {

entry.setField(new UnknownField("test"), "some value");

assertEquals(entry, tel.getBibEntry());
assertEquals(entry, tel.getChangedEntry());
}

@Test
Expand Down
33 changes: 27 additions & 6 deletions src/test/java/org/jabref/model/event/TestEventListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.model.event;

import org.jabref.model.database.event.AllInsertsFinishedEvent;
import org.jabref.model.database.event.EntryAddedEvent;
import org.jabref.model.database.event.EntryRemovedEvent;
import org.jabref.model.entry.BibEntry;
Expand All @@ -9,24 +10,44 @@

public class TestEventListener {

private BibEntry bibEntry;
private BibEntry addedEntry;
private BibEntry firstInsertedEntry;
private BibEntry removedEntry;
private BibEntry changedEntry;

@Subscribe
public void listen(EntryAddedEvent event) {
this.bibEntry = event.getBibEntry();
this.addedEntry = event.getBibEntry();
}

@Subscribe
public void listen(AllInsertsFinishedEvent event) {
this.firstInsertedEntry = event.getBibEntry();
}

@Subscribe
public void listen(EntryRemovedEvent event) {
this.bibEntry = event.getBibEntry();
this.removedEntry = event.getBibEntry();
}

@Subscribe
public void listen(EntryChangedEvent event) {
this.bibEntry = event.getBibEntry();
this.changedEntry = event.getBibEntry();
}

public BibEntry getAddedEntry() {
return addedEntry;
}

public BibEntry getFirstInsertedEntry() {
return firstInsertedEntry;
}

public BibEntry getRemovedEntry() {
return removedEntry;
}

public BibEntry getBibEntry() {
return this.bibEntry;
public BibEntry getChangedEntry() {
return changedEntry;
}
}