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

AddEventSystem #1028

Merged
merged 16 commits into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Implemented [#672](https://github.com/JabRef/jabref/issues/672): FileList now distributes its space dependent on the width of its columns
- Added missing German translations
- Swedish is added as a language option (still not a complete translation)
- [#969](https://github.com/JabRef/jabref/issues/969) Adding and replacing old event system mechanisms with Google Guava EventBus.

### Fixed
- Alleviate multiuser concurrency issue when near simultaneous saves occur to a shared database file
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/net/sf/jabref/event/EntryAddedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.sf.jabref.event;

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

/**
* <code>EntryAddedEvent</code> is fired when a new <code>BibEntry</code> was added
* to the database.
*/

public class EntryAddedEvent extends EntryEvent {

/**
* @param bibEntry <code>BibEntry</code> object which has been added.
*/
public EntryAddedEvent(BibEntry bibEntry) {
super(bibEntry);
}

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

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

/**
* <code>EntryChangedEvent</code> is fired when a <code>BibEntry</code> has been changed.
*/

public class EntryChangedEvent extends EntryEvent {

/**
* @param bibEntry <code>BibEntry</code> object the changes were applied on.
*/
public EntryChangedEvent(BibEntry bibEntry) {
super(bibEntry);
}

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

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

/**
* This abstract class pretends a minimal set of attributes and methods
* which an entry event should have.
*/
public abstract class EntryEvent {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc


private final BibEntry bibEntry;

/**
* @param bibEntry BibEntry object which is involved in this event
*/
public EntryEvent(BibEntry bibEntry) {
this.bibEntry = bibEntry;
}

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

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

/**
* <code>RemovedEntryEvent</code> is fired when a <code>BibEntry</code> was removed
* from the database.
*/

public class EntryRemovedEvent extends EntryEvent {

/**
* @param bibEntry <code>BibEntry</code> object which has been removed.
*/
public EntryRemovedEvent(BibEntry bibEntry) {
super(bibEntry);
}

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

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

/**
* <code>FieldChangedEvent</code> is fired when a field of <code>BibEntry</code> has been modified, removed or added.
*/
public class FieldChangedEvent extends EntryChangedEvent {

private final String fieldName;
private final String newValue;

/**
* @param bibEntry Affected BibEntry object
* @param fieldName Name of field which has been changed
* @param newValue new field value
*/
public FieldChangedEvent(BibEntry bibEntry, String fieldName, String newValue) {
super(bibEntry);
this.fieldName = fieldName;
this.newValue = newValue;
}

public String getFieldName() {
return fieldName;
}

public String getNewValue() {
return newValue;
}

}
57 changes: 31 additions & 26 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import net.sf.jabref.collab.ChangeScanner;
import net.sf.jabref.collab.FileUpdateListener;
import net.sf.jabref.collab.FileUpdatePanel;
import net.sf.jabref.event.EntryAddedEvent;
import net.sf.jabref.event.EntryChangedEvent;
import net.sf.jabref.exporter.BibDatabaseWriter;
import net.sf.jabref.exporter.ExportToClipboardAction;
import net.sf.jabref.exporter.SaveDatabaseAction;
Expand Down Expand Up @@ -123,9 +125,6 @@
import net.sf.jabref.logic.util.io.FileBasedLock;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.DatabaseChangeEvent;
import net.sf.jabref.model.database.DatabaseChangeEvent.ChangeType;
import net.sf.jabref.model.database.DatabaseChangeListener;
import net.sf.jabref.model.database.KeyCollisionException;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.EntryType;
Expand All @@ -147,6 +146,7 @@
import net.sf.jabref.sql.exporter.DatabaseExporter;

import ca.odell.glazedlists.event.ListEventListener;
import com.google.common.eventbus.Subscribe;
import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -241,7 +241,7 @@ public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext, Chars
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.registerListener(new GroupTreeListener());

if (file == null) {
if (database.hasEntries()) {
Expand Down Expand Up @@ -1256,13 +1256,12 @@ 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 GroupTreeListener {

@Override
public void databaseChanged(final DatabaseChangeEvent e) {
if ((e.getType() == ChangeType.ADDED_ENTRY) && Globals.prefs.getBoolean(JabRefPreferences.AUTO_ASSIGN_GROUP)
&& frame.groupToggle.isSelected()) {
final List<BibEntry> entries = Collections.singletonList(e.getEntry());
@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
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();
if (selection != null) {
// it is possible that the user selected nothing. Therefore, checked for "!= null"
Expand All @@ -1279,27 +1278,33 @@ public void databaseChanged(final DatabaseChangeEvent e) {
* 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 SearchAutoCompleteListener {

@Override
public void databaseChanged(DatabaseChangeEvent e) {
if ((e.getType() == ChangeType.CHANGED_ENTRY) || (e.getType() == ChangeType.ADDED_ENTRY)) {
searchAutoCompleter.addBibtexEntry(e.getEntry());
}
@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
searchAutoCompleter.addBibtexEntry(addedEntryEvent.getBibEntry());
}

@Subscribe
public void listen(EntryChangedEvent entryChangedEvent) {
searchAutoCompleter.addBibtexEntry(entryChangedEvent.getBibEntry());
}
}

/**
* 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 AutoCompleteListener {

@Override
public void databaseChanged(DatabaseChangeEvent e) {
if ((e.getType() == ChangeType.CHANGED_ENTRY) || (e.getType() == ChangeType.ADDED_ENTRY)) {
BasePanel.this.autoCompleters.addEntry(e.getEntry());
}
@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
BasePanel.this.autoCompleters.addEntry(addedEntryEvent.getBibEntry());
}

@Subscribe
public void listen(EntryChangedEvent entryChangedEvent) {
BasePanel.this.autoCompleters.addEntry(entryChangedEvent.getBibEntry());
}
}

Expand Down Expand Up @@ -1348,8 +1353,8 @@ public void updateTableFont() {
}

private void createMainTable() {
database.addDatabaseChangeListener(tableModel.getEventList());
database.addDatabaseChangeListener(SpecialFieldDatabaseChangeListener.getInstance());
database.registerListener(tableModel.getListSynchronizer());
database.registerListener(SpecialFieldDatabaseChangeListener.getInstance());

tableFormat = new MainTableFormat(database);
tableFormat.updateTableFormat();
Expand Down Expand Up @@ -1512,15 +1517,15 @@ public void setupMainPanel() {

// Set up name autocompleter for search:
instantiateSearchAutoCompleter();
this.getDatabase().addDatabaseChangeListener(new SearchAutoCompleterUpdater());
this.getDatabase().registerListener(new SearchAutoCompleteListener());

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().registerListener(new AutoCompleteListener());
} else {
// create empty ContentAutoCompleters() if autoCompletion is deactivated
autoCompleters = new ContentAutoCompleters(Globals.journalAbbreviationLoader);
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/net/sf/jabref/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.io.IOException;
import java.io.StringReader;
import java.util.Objects;
Expand Down Expand Up @@ -53,6 +50,7 @@
import net.sf.jabref.Globals;
import net.sf.jabref.JabRefExecutorService;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.event.FieldChangedEvent;
import net.sf.jabref.exporter.ExportFormats;
import net.sf.jabref.gui.desktop.JabRefDesktop;
import net.sf.jabref.gui.fieldeditors.PreviewPanelTransferHandler;
Expand All @@ -63,13 +61,15 @@
import net.sf.jabref.logic.search.SearchQueryHighlightListener;
import net.sf.jabref.model.entry.BibEntry;

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

/**
* Displays an BibEntry using the given layout format.
*/
public class PreviewPanel extends JPanel implements VetoableChangeListener, SearchQueryHighlightListener, EntryContainer {
public class PreviewPanel extends JPanel
implements SearchQueryHighlightListener, EntryContainer {

private static final Log LOGGER = LogFactory.getLog(PreviewPanel.class);

Expand Down Expand Up @@ -105,7 +105,6 @@ public class PreviewPanel extends JPanel implements VetoableChangeListener, Sear

private Optional<Pattern> highlightPattern = Optional.empty();


/**
* @param databaseContext
* (may be null) Optionally used to resolve strings and for resolving pdf directories for links.
Expand Down Expand Up @@ -275,16 +274,28 @@ public void setLayout(Layout layout) {
}

public void setEntry(BibEntry newEntry) {
if(entry.isPresent() && (entry.get() != newEntry)) {
entry.ifPresent(e -> e.removePropertyChangeListener(this));
newEntry.addPropertyChangeListener(this);

if (entry.isPresent() && (entry.get() != newEntry)) {
entry.ifPresent(e -> e.unregisterListener(this));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be written simpler: entry.filter(e -> e != newEntry).ifPresent(e -> e.unregisterListener(this));


entry.ifPresent(e -> e.registerListener(this));

entry = Optional.ofNullable(newEntry);

updateLayout();
update();
}


/**
* Listener for ChangedFieldEvent.
*/
@Subscribe
public void listen(FieldChangedEvent fieldChangedEvent) {
update();
}

@Override
public BibEntry getEntry() {
return this.entry.orElse(null);
Expand All @@ -311,19 +322,6 @@ private void scrollToTop() {
SwingUtilities.invokeLater(() -> scrollPane.getVerticalScrollBar().setValue(0));
}

/**
* The PreviewPanel has registered itself as an event listener with the
* currently displayed BibEntry. If the entry changes, an event is
* received here, and we can update the preview immediately.
*/
@Override
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
// TODO updating here is not really necessary isn't it?
// Only if we are visible.
update();
}

@Override
public void highlightPattern(Optional<Pattern> newPattern) {
this.highlightPattern = newPattern;
Expand Down
Loading