Skip to content

Commit

Permalink
Implementation of EntryEventlistener.
Browse files Browse the repository at this point in the history
Functional substitution for fireDatabaseChanged() in BibDatabase.java.
Extracting event code to new EntryEventListener.
Event passing for GroupTreeUpdater, GlazedEntrySorter, SearchAutoCompleterUpdater and AutoCompletersUpdater.
Handle nullpointer errors while testing.
  • Loading branch information
obraliar committed Apr 4, 2016
1 parent bb419c8 commit 2e8afac
Show file tree
Hide file tree
Showing 22 changed files with 393 additions and 45 deletions.
46 changes: 35 additions & 11 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
import ca.odell.glazedlists.matchers.Matcher;

import com.google.common.eventbus.EventBus;
import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import net.sf.jabref.*;
Expand All @@ -35,6 +37,7 @@
import net.sf.jabref.gui.actions.CleanupAction;
import net.sf.jabref.gui.desktop.JabRefDesktop;
import net.sf.jabref.gui.entryeditor.EntryEditor;
import net.sf.jabref.gui.event.listener.EntryEventListener;
import net.sf.jabref.gui.fieldeditors.FieldEditor;
import net.sf.jabref.gui.journals.AbbreviateAction;
import net.sf.jabref.gui.journals.UnabbreviateAction;
Expand Down Expand Up @@ -68,7 +71,6 @@
import net.sf.jabref.logic.util.io.FileBasedLock;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.database.*;
import net.sf.jabref.model.database.DatabaseChangeEvent.ChangeType;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.EntryType;
import net.sf.jabref.model.entry.IdGenerator;
Expand Down Expand Up @@ -104,6 +106,9 @@ public class BasePanel extends JPanel implements ClipboardOwner, FileUpdateListe
private final BibDatabase database;
private final BibDatabaseContext bibDatabaseContext;

private final EventBus eventBus;
private final EntryEventListener entryEventListener;

private BasePanelMode mode;
private EntryEditor currentEditor;
private PreviewPanel currentPreview;
Expand Down Expand Up @@ -179,20 +184,30 @@ public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext, Chars
this.encoding = encoding;
this.bibDatabaseContext = bibDatabaseContext;

this.eventBus = new EventBus();
this.sidePaneManager = frame.getSidePaneManager();
this.frame = frame;
this.database = bibDatabaseContext.getDatabase();
this.database.setEventBus(eventBus);

searchBar = new SearchBar(this);

//setting up entryEventListener
entryEventListener = new EntryEventListener();
entryEventListener.setGroupSelector(frame.getGroupSelector());
entryEventListener.setGroupToggleBtn(frame.groupToggle);

eventBus.register(entryEventListener);

setupMainPanel();

setupActions();

File file = bibDatabaseContext.getDatabaseFile();

// ensure that at each addition of a new entry, the entry is added to the groups interface
this.database.addDatabaseChangeListener(new GroupTreeUpdater());
//this.database.addDatabaseChangeListener(new GroupTreeUpdater());


if (file == null) {
if (!database.getEntries().isEmpty()) {
Expand All @@ -214,6 +229,9 @@ public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext, Chars
EverythingMatcher.INSTANCE);
filterGroupToggle = new StartStopListAction<>(groupFilterList, GroupMatcher.INSTANCE,
EverythingMatcher.INSTANCE);



}

// Returns a collection of AutoCompleters, which are populated from the current database
Expand Down Expand Up @@ -1252,7 +1270,7 @@ 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 GroupTreeUpdater implements DatabaseChangeListener {
/*private class GroupTreeUpdater implements DatabaseChangeListener {
@Override
public void databaseChanged(final DatabaseChangeEvent e) {
Expand All @@ -1270,35 +1288,35 @@ public void databaseChanged(final DatabaseChangeEvent e) {
SwingUtilities.invokeLater(() -> BasePanel.this.getGroupSelector().valueChanged(null));
}
}
}
}*/

/**
* Ensures that the search auto completer is up to date when entries are changed AKA Let the auto completer, if any,
* harvest words from the entry
*/
private class SearchAutoCompleterUpdater implements DatabaseChangeListener {
/*private class SearchAutoCompleterUpdater implements DatabaseChangeListener {
@Override
public void databaseChanged(DatabaseChangeEvent e) {
if ((e.getType() == ChangeType.CHANGED_ENTRY) || (e.getType() == ChangeType.ADDED_ENTRY)) {
searchAutoCompleter.addBibtexEntry(e.getEntry());
}
}
}
}*/

/**
* Ensures that auto completers are up to date when entries are changed AKA Let the auto completer, if any, harvest
* words from the entry
*/
private class AutoCompletersUpdater implements DatabaseChangeListener {
/*private class AutoCompletersUpdater implements DatabaseChangeListener {
@Override
public void databaseChanged(DatabaseChangeEvent e) {
if ((e.getType() == ChangeType.CHANGED_ENTRY) || (e.getType() == ChangeType.ADDED_ENTRY)) {
BasePanel.this.autoCompleters.addEntry(e.getEntry());
}
}
}
}*/


/**
Expand Down Expand Up @@ -1348,9 +1366,12 @@ private void createMainTable() {
//Comparator comp = new FieldComparator("author");

final GlazedEntrySorter eventList = new GlazedEntrySorter(database.getEntries());

entryEventListener.setGlazedEntrySorter(eventList);

// Must initialize sort columns somehow:

database.addDatabaseChangeListener(eventList);
//database.addDatabaseChangeListener(eventList);
database.addDatabaseChangeListener(SpecialFieldDatabaseChangeListener.getInstance());
groupFilterList = new FilterList<>(eventList.getTheList(), EverythingMatcher.INSTANCE);
if (filterGroupToggle != null) {
Expand Down Expand Up @@ -1540,20 +1561,22 @@ protected boolean accept(Component c) {
// Set up name autocompleter for search:
//if (!Globals.prefs.getBoolean("searchAutoComplete")) {
instantiateSearchAutoCompleter();
this.getDatabase().addDatabaseChangeListener(new SearchAutoCompleterUpdater());
//this.getDatabase().addDatabaseChangeListener(new SearchAutoCompleterUpdater());

AutoCompletePreferences autoCompletePreferences = new AutoCompletePreferences(Globals.prefs);
// Set up AutoCompleters for this panel:
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_COMPLETE)) {
autoCompleters = new ContentAutoCompleters(getDatabase(), bibDatabaseContext.getMetaData(),
autoCompletePreferences, Globals.journalAbbreviationLoader);
// ensure that the autocompleters are in sync with entries
this.getDatabase().addDatabaseChangeListener(new AutoCompletersUpdater());
//this.getDatabase().addDatabaseChangeListener(new AutoCompletersUpdater());
} else {
// create empty ContentAutoCompleters() if autoCompletion is deactivated
autoCompleters = new ContentAutoCompleters(Globals.journalAbbreviationLoader);
}

entryEventListener.setAutoCompleters(autoCompleters);

// restore floating search result
// (needed if preferences have been changed which causes a recreation of the main table)
if (floatSearchActive) {
Expand All @@ -1573,6 +1596,7 @@ private void instantiateSearchAutoCompleter() {
AutoCompletePreferences autoCompletePreferences = new AutoCompletePreferences(Globals.prefs);
AutoCompleterFactory autoCompleterFactory = new AutoCompleterFactory(autoCompletePreferences);
searchAutoCompleter = autoCompleterFactory.getPersonAutoCompleter();
entryEventListener.setSearchAutoCompleter(searchAutoCompleter);
for (BibEntry entry : database.getEntries()) {
searchAutoCompleter.addBibtexEntry(entry);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/gui/GlazedEntrySorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public EventList<BibEntry> getTheList() {

@Override
public void databaseChanged(DatabaseChangeEvent e) {
/*
list.getReadWriteLock().writeLock().lock();
if (e.getType() == DatabaseChangeEvent.ChangeType.ADDED_ENTRY) {
list.add(e.getEntry());
Expand All @@ -62,7 +63,7 @@ public void databaseChanged(DatabaseChangeEvent e) {
}
}
list.getReadWriteLock().writeLock().unlock();

*/
}

}
14 changes: 14 additions & 0 deletions src/main/java/net/sf/jabref/gui/event/EntryAddEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.sf.jabref.gui.event;

import net.sf.jabref.model.entry.BibEntry;

public class EntryAddEvent extends Event {

public EntryAddEvent(BibEntry entry) {
super(entry);
}

public BibEntry getEntry() {
return (BibEntry) this.getObject();
}
}
15 changes: 15 additions & 0 deletions src/main/java/net/sf/jabref/gui/event/EntryChangeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.sf.jabref.gui.event;

import net.sf.jabref.model.entry.BibEntry;

public class EntryChangeEvent extends Event {

public EntryChangeEvent(BibEntry entry) {
super(entry);
}

public BibEntry getEntry() {
return (BibEntry) this.getObject();
}

}
15 changes: 15 additions & 0 deletions src/main/java/net/sf/jabref/gui/event/EntryRemoveEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.sf.jabref.gui.event;

import net.sf.jabref.model.entry.BibEntry;

public class EntryRemoveEvent extends Event {

public EntryRemoveEvent(BibEntry entry) {
super(entry);
}

public BibEntry getEntry() {
return (BibEntry) this.getObject();
}

}
21 changes: 21 additions & 0 deletions src/main/java/net/sf/jabref/gui/event/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.sf.jabref.gui.event;

/**
* Represents a medium which can be used
* to pass objects on event occurrence.
*/

public class Event {

private final Object object;

public Event(Object object) {
this.object = object;
}

public Object getObject() {
return this.object;
}


}
14 changes: 14 additions & 0 deletions src/main/java/net/sf/jabref/gui/event/TestEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.sf.jabref.gui.event;


public class TestEvent extends Event {

public TestEvent(String eventMesage) {
super(eventMesage);
}

public String getMessage() {
return (String) this.getObject();
}

}
114 changes: 114 additions & 0 deletions src/main/java/net/sf/jabref/gui/event/listener/EntryEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package net.sf.jabref.gui.event.listener;

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

import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreePath;

import com.google.common.eventbus.Subscribe;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.groups.GroupSelector;
import net.sf.jabref.groups.GroupTreeNode;
import net.sf.jabref.gui.GlazedEntrySorter;
import net.sf.jabref.gui.event.*;
import net.sf.jabref.logic.autocompleter.AutoCompleter;
import net.sf.jabref.logic.autocompleter.ContentAutoCompleters;
import net.sf.jabref.model.entry.BibEntry;

public class EntryEventListener extends EventListener<Event> {

private GlazedEntrySorter glazedEntrySorter;
private GroupSelector groupSelector;
private JToggleButton groupToggleBtn;
private AutoCompleter<String> searchAutoCompleter;
private ContentAutoCompleters autoCompleters;

@Override
@Subscribe
public void listen(Event event) {
return;
}

@Subscribe
public void listen(EntryAddEvent eae) {
lock(true);
glazedEntrySorter.getTheList().add(eae.getEntry());
lock(false);

if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_ASSIGN_GROUP)
&& groupToggleBtn.isSelected()) {
final List<BibEntry> entries = Collections.singletonList(eae.getEntry());
final TreePath[] selection = groupSelector.getGroupsTree().getSelectionPaths();
if (selection != null) {
// it is possible that the user selected nothing. Therefore, checked for "!= null"
for (final TreePath tree : selection) {
((GroupTreeNode) tree.getLastPathComponent()).addToGroup(entries);
}
}
//BasePanel.this.updateEntryEditorIfShowing(); // doesn't seem to be necessary
SwingUtilities.invokeLater(() -> groupSelector.valueChanged(null));
}

searchAutoCompleter.addBibtexEntry(eae.getEntry());

autoCompleters.addEntry(eae.getEntry());

}


@Subscribe
public void listen(EntryChangeEvent ece) {
lock(true);
int index = glazedEntrySorter.getTheList().indexOf(ece.getEntry());
if (index != -1) {
// SpecialFieldUtils.syncSpecialFieldsFromKeywords update an entry during
// DatabaseChangeEvent.ADDED_ENTRY
// thus,
glazedEntrySorter.getTheList().set(index, ece.getEntry());
}
lock(false);

searchAutoCompleter.addBibtexEntry(ece.getEntry());

autoCompleters.addEntry(ece.getEntry());
}

@Subscribe
public void listen(EntryRemoveEvent ere) {
lock(true);
glazedEntrySorter.getTheList().remove(ere.getEntry());
lock(false);
}

public void setGlazedEntrySorter(GlazedEntrySorter glazedEntrySorter) {
this.glazedEntrySorter = glazedEntrySorter;
}

public void setGroupSelector(GroupSelector groupSelector) {
this.groupSelector = groupSelector;
}

public void setGroupToggleBtn(JToggleButton groupToggleBtn) {
this.groupToggleBtn = groupToggleBtn;
}

public void setSearchAutoCompleter(AutoCompleter<String> searchAutoCompleter) {
this.searchAutoCompleter = searchAutoCompleter;
}

public void setAutoCompleters(ContentAutoCompleters autoCompleters) {
this.autoCompleters = autoCompleters;
}

private void lock(boolean lock) {
if (lock) {
glazedEntrySorter.getTheList().getReadWriteLock().writeLock().lock();
} else {
glazedEntrySorter.getTheList().getReadWriteLock().writeLock().unlock();
}
}
}
Loading

0 comments on commit 2e8afac

Please sign in to comment.