Skip to content

Commit

Permalink
Fix #1122: Group view is immediately updated after adding an entry to…
Browse files Browse the repository at this point in the history
… a group

Using the new and shiny event system
  • Loading branch information
tobiasdiez committed May 19, 2016
1 parent 6d93ac0 commit 2f12768
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1364](https://github.com/JabRef/jabref/issues/1364): Windows: install to LOCALAPPDATA directory for non-admin users
- Fixed [#1365](https://github.com/JabRef/jabref/issues/1365): Default label pattern back to `[auth][year]`
- Fixed [#796](https://github.com/JabRef/jabref/issues/796): Undoing more than one entry at the same time is now working
- Fixed [#1122](https://github.com/JabRef/jabref/issues/1122): Group view is immediately updated after adding an entry to a group
- Fixed [#1353](https://github.com/JabRef/jabref/issues/1353): Fetch-Preview did not display updated BibTeX-Key after clicking on `Generate Now`
- Fixed [#1381](https://github.com/JabRef/jabref/issues/1381): File links containing blanks are broken if non-default viewer is set
- Fixed sourceforge bug 1000: shorttitleINI can generate the initials of the shorttitle
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1252,14 +1252,11 @@ public SearchBar getSearchBar() {
}


/**
* This listener is used to add a new entry to a group (or a set of groups) in case the Group View is selected and
* one or more groups are marked
*/
private class GroupTreeListener {

@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
// Automatically add new entry to the selected group (or set of groups)
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_ASSIGN_GROUP) && frame.groupToggle.isSelected()) {
final List<BibEntry> entries = Collections.singletonList(addedEntryEvent.getBibEntry());
final TreePath[] selection = frame.getGroupSelector().getGroupsTree().getSelectionPaths();
Expand All @@ -1271,6 +1268,15 @@ public void listen(EntryAddedEvent addedEntryEvent) {
}
SwingUtilities.invokeLater(() -> BasePanel.this.getGroupSelector().valueChanged(null));
}

// Update group display (for example to reflect that the number of contained entries has changed)
frame.getGroupSelector().revalidateGroups();
}

@Subscribe
public void listen(EntryChangedEvent entryChangedEvent) {
// Update group display in order to take changes into account
frame.getGroupSelector().revalidateGroups();
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/net/sf/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@
import java.util.regex.Pattern;

import net.sf.jabref.event.EntryAddedEvent;
import net.sf.jabref.event.EntryChangedEvent;
import net.sf.jabref.event.EntryRemovedEvent;
import net.sf.jabref.event.FieldChangedEvent;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexString;
import net.sf.jabref.model.entry.EntryUtil;
import net.sf.jabref.model.entry.MonthUtil;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand Down Expand Up @@ -184,6 +187,8 @@ public synchronized boolean insertEntry(BibEntry entry) throws KeyCollisionExcep

internalIDs.add(id);
entries.add(entry);
entry.registerListener(this);

eventBus.post(new EntryAddedEvent(entry));
return duplicationChecker.checkForDuplicateKeyAndAdd(null, entry.getCiteKey());
}
Expand Down Expand Up @@ -549,18 +554,20 @@ public String getEpilog() {

/**
* Registers an listener object (subscriber) to the internal event bus.
* All subscribers should contain at least one <code>@Subscribe</code> annotated
* method accepting one of the following event types:
* The following events are posted:
*
* - {@link EntryAddedEvent}
* - {@link EntryChangedEvent}
* - {@link EntryRemovedEvent}
*
* or another {@link EntryEvent} extending type.
*
* @param object Listener (subscriber)
* @param listener listener (subscriber) to add
*/
public void registerListener(Object object) {
this.eventBus.register(object);
public void registerListener(Object listener) {
this.eventBus.register(listener);
}

@Subscribe
private void relayEntryChangeEvent(FieldChangedEvent event) {
eventBus.post(event);
}
}
60 changes: 23 additions & 37 deletions src/test/java/net/sf/jabref/model/database/BibDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.sf.jabref.model.entry.BibtexString;
import net.sf.jabref.model.entry.IdGenerator;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -33,22 +32,15 @@ public class BibDatabaseTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

private BibDatabase database;

@Before
public void setUp() {
Globals.prefs = JabRefPreferences.getInstance(); // set preferences for this test
}

@After
public void tearDown() {
Globals.prefs = null;
database = new BibDatabase();
}

/**
* Some basic test cases for resolving strings.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void resolveStrings() throws IOException {
try (FileInputStream stream = new FileInputStream("src/test/resources/net/sf/jabref/util/twente.bib");
Expand All @@ -67,10 +59,7 @@ public void resolveStrings() throws IOException {
}

@Test
public void insertEntry() {
BibDatabase database = new BibDatabase();
assertEquals(Collections.emptyList(), database.getEntries());

public void insertEntryAddsEntryToEntriesList() {
BibEntry entry = new BibEntry();
database.insertEntry(entry);
assertEquals(database.getEntries().size(), 1);
Expand All @@ -79,8 +68,7 @@ public void insertEntry() {
}

@Test
public void containsEntryId() {
BibDatabase database = new BibDatabase();
public void containsEntryIdFindsEntry() {
BibEntry entry = new BibEntry();
assertFalse(database.containsEntryWithId(entry.getId()));
database.insertEntry(entry);
Expand All @@ -89,8 +77,6 @@ public void containsEntryId() {

@Test(expected = KeyCollisionException.class)
public void insertEntryWithSameIdThrowsException() {
BibDatabase database = new BibDatabase();

BibEntry entry0 = new BibEntry();
database.insertEntry(entry0);

Expand All @@ -100,9 +86,7 @@ public void insertEntryWithSameIdThrowsException() {
}

@Test
public void removeEntry() {
BibDatabase database = new BibDatabase();

public void removeEntryRemovesEntryFromEntriesList() {
BibEntry entry = new BibEntry();
database.insertEntry(entry);

Expand All @@ -113,28 +97,24 @@ public void removeEntry() {

@Test(expected = NullPointerException.class)
public void insertNullEntryThrowsException() {
BibDatabase database = new BibDatabase();
database.insertEntry(null);
fail();
}

@Test(expected = NullPointerException.class)
public void removeNullEntryThrowsException() {
BibDatabase database = new BibDatabase();
database.removeEntry(null);
fail();
}

@Test
public void emptyDatabaseHasNoStrings() {
BibDatabase database = new BibDatabase();
assertEquals(Collections.emptySet(), database.getStringKeySet());
assertTrue(database.hasNoStrings());
}

@Test
public void insertString() {
BibDatabase database = new BibDatabase();
public void insertStringUpdatesStringList() {
BibtexString string = new BibtexString(IdGenerator.next(), "DSP", "Digital Signal Processing");
database.addString(string);
assertFalse(database.hasNoStrings());
Expand All @@ -146,8 +126,7 @@ public void insertString() {
}

@Test
public void insertAndRemoveString() {
BibDatabase database = new BibDatabase();
public void removeStringUpdatesStringList() {
BibtexString string = new BibtexString(IdGenerator.next(), "DSP", "Digital Signal Processing");
database.addString(string);
database.removeString(string.getId());
Expand All @@ -160,8 +139,7 @@ public void insertAndRemoveString() {
}

@Test
public void hasStringLabel() {
BibDatabase database = new BibDatabase();
public void hasStringLabelFindsString() {
BibtexString string = new BibtexString(IdGenerator.next(), "DSP", "Digital Signal Processing");
database.addString(string);
assertTrue(database.hasStringLabel("DSP"));
Expand All @@ -170,7 +148,6 @@ public void hasStringLabel() {

@Test(expected = KeyCollisionException.class)
public void addSameStringLabelTwiceThrowsKeyCollisionException() {
BibDatabase database = new BibDatabase();
BibtexString string = new BibtexString(IdGenerator.next(), "DSP", "Digital Signal Processing");
database.addString(string);
string = new BibtexString(IdGenerator.next(), "DSP", "Digital Signal Processor");
Expand All @@ -180,7 +157,6 @@ public void addSameStringLabelTwiceThrowsKeyCollisionException() {

@Test(expected = KeyCollisionException.class)
public void addSameStringIdTwiceThrowsKeyCollisionException() {
BibDatabase database = new BibDatabase();
String id = IdGenerator.next();
BibtexString string = new BibtexString(id, "DSP", "Digital Signal Processing");
database.addString(string);
Expand All @@ -190,8 +166,7 @@ public void addSameStringIdTwiceThrowsKeyCollisionException() {
}

@Test
public void testAddedEntryEventReceivement() {
BibDatabase database = new BibDatabase();
public void insertEntryPostsAddedEntryEvent() {
BibEntry expectedEntry = new BibEntry();
TestEventListener tel = new TestEventListener();
database.registerListener(tel);
Expand All @@ -201,8 +176,7 @@ public void testAddedEntryEventReceivement() {
}

@Test
public void testRemovedEntryEventReceivement() {
BibDatabase database = new BibDatabase();
public void removeEntryPostsRemovedEntryEvent() {
BibEntry expectedEntry = new BibEntry();
TestEventListener tel = new TestEventListener();
database.insertEntry(expectedEntry);
Expand All @@ -211,4 +185,16 @@ public void testRemovedEntryEventReceivement() {
BibEntry actualEntry = tel.getBibEntry();
assertEquals(expectedEntry, actualEntry);
}

@Test
public void changingEntryPostsChangeEntryEvent() {
BibEntry entry = new BibEntry();
TestEventListener tel = new TestEventListener();
database.insertEntry(entry);
database.registerListener(tel);

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

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

0 comments on commit 2f12768

Please sign in to comment.