Skip to content

Commit

Permalink
Integrity checker improvements (#2349)
Browse files Browse the repository at this point in the history
* Resolves #2181 Focus entries by ID instead of bibtexkey

* Add bibtexkey field to field list in editortab to be able to focus it in integrity checks

* Use lambda
  • Loading branch information
stefan-kolb authored and Siedlerchr committed Dec 12, 2016
1 parent 4aa0da2 commit 59d9e35
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1299,16 +1299,16 @@ public void insertEntry(final BibEntry bibEntry) {
}
}

public void editEntryByKeyAndFocusField(final String bibtexKey, final String fieldName) {
final List<BibEntry> entries = bibDatabaseContext.getDatabase().getEntriesByKey(bibtexKey);
if (entries.size() == 1) {
mainTable.setSelected(mainTable.findEntry(entries.get(0)));
public void editEntryByIdAndFocusField(final String entryId, final String fieldName) {
final Optional<BibEntry> entry = bibDatabaseContext.getDatabase().getEntryById(entryId);
entry.ifPresent(e -> {
mainTable.setSelected(mainTable.findEntry(e));
selectionListener.editSignalled();
final EntryEditor editor = getEntryEditor(entries.get(0));
final EntryEditor editor = getEntryEditor(e);
editor.setFocusToField(fieldName);
this.showEntryEditor(editor);
editor.requestFocus();
}
});
}

public void updateTableFont() {
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/net/sf/jabref/gui/actions/IntegrityCheckAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

Expand All @@ -30,13 +31,10 @@
import com.jgoodies.forms.layout.FormLayout;

public class IntegrityCheckAction extends MnemonicAwareAction {

private static final String ELLIPSES = "...";


private final JabRefFrame frame;


public IntegrityCheckAction(JabRefFrame frame) {
this.frame = frame;
putValue(Action.NAME, Localization.menuTitle("Check integrity") + ELLIPSES);
Expand All @@ -55,26 +53,30 @@ public void actionPerformed(ActionEvent e) {
} else {
Map<String, Boolean> showMessage = new HashMap<>();
// prepare data model
Object[][] model = new Object[messages.size()][3];
Object[][] model = new Object[messages.size()][4];
int i = 0;
for (IntegrityMessage message : messages) {
model[i][0] = message.getEntry().getCiteKeyOptional().orElse("");
model[i][1] = message.getFieldName();
model[i][2] = message.getMessage();
model[i][0] = message.getEntry().getId();
model[i][1] = message.getEntry().getCiteKeyOptional().orElse("");
model[i][2] = message.getFieldName();
model[i][3] = message.getMessage();
showMessage.put(message.getMessage(), true);
i++;
}

// construct view
JTable table = new JTable(model,
new Object[] {Localization.lang("BibTeX key"), Localization.lang("Field"),
new Object[] {"ID", Localization.lang("BibTeX key"), Localization.lang("Field"),
Localization.lang("Message")});

RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
// hide IDs
TableColumnModel columnModel = table.getColumnModel();
columnModel.removeColumn(columnModel.getColumn(0));

RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {
@Override
public boolean include(Entry<?, ?> entry) {
return showMessage.get(entry.getStringValue(2));
return showMessage.get(entry.getStringValue(3));
}
};

Expand All @@ -85,21 +87,23 @@ public boolean include(Entry<?, ?> entry) {
table.setDefaultEditor(Object.class, null);
ListSelectionModel selectionModel = table.getSelectionModel();


selectionModel.addListSelectionListener(event -> {
if (!event.getValueIsAdjusting()) {
try {
String citeKey = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][0];
String fieldName = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][1];
frame.getCurrentBasePanel().editEntryByKeyAndFocusField(citeKey, fieldName);
String entryId = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][0];
String fieldName = (String) model[table.convertRowIndexToModel(table.getSelectedRow())][2];
frame.getCurrentBasePanel().editEntryByIdAndFocusField(entryId, fieldName);
} catch (ArrayIndexOutOfBoundsException exception) {
// Ignore -- most likely caused by filtering out the earlier selected row
}
}
});


// BibTeX key
table.getColumnModel().getColumn(0).setPreferredWidth(100);
// field name
table.getColumnModel().getColumn(1).setPreferredWidth(60);
// message
table.getColumnModel().getColumn(2).setPreferredWidth(400);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
JScrollPane scrollPane = new JScrollPane(table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -80,9 +81,9 @@ class EntryEditorTab {
public EntryEditorTab(JabRefFrame frame, BasePanel panel, List<String> fields, EntryEditor parent,
boolean addKeyField, boolean compressed, String tabTitle) {
if (fields == null) {
this.fields = Collections.emptyList();
this.fields = new ArrayList<>();
} else {
this.fields = fields;
this.fields = new ArrayList<>(fields);
}

this.parent = parent;
Expand Down Expand Up @@ -195,6 +196,7 @@ private void setupPanel(JabRefFrame frame, BasePanel bPanel, boolean addKeyField
setupJTextComponent(textField, null);

editors.put(BibEntry.KEY_FIELD, textField);
fields.add(BibEntry.KEY_FIELD);
/*
* If the key field is the only field, we should have only one
* editor, and this one should be set as active initially:
Expand Down Expand Up @@ -278,7 +280,7 @@ public FieldEditor getActive() {
}

public List<String> getFields() {
return fields;
return Collections.unmodifiableList(fields);
}

public void focus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.strings.StringUtil;

/**
* Currently only checks the key if there is an author, year, and title present.
*/
public class BibtexkeyChecker implements Checker {

@Override
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/net/sf/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* A bibliography database.
*/
public class BibDatabase {

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

/**
Expand Down Expand Up @@ -151,6 +150,16 @@ public synchronized List<BibEntry> getEntriesByKey(String key) {
return result;
}

/**
* Finds the entry with a specified ID.
*
* @param id
* @return The entry that has the given id
*/
public synchronized Optional<BibEntry> getEntryById(String id) {
return entries.stream().filter(entry -> entry.getId().equals(id)).findFirst();
}

/**
* Inserts the entry, given that its ID is not already in use.
* use Util.createId(...) to make up a unique ID for an entry.
Expand Down
1 change: 0 additions & 1 deletion src/main/java/net/sf/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.commons.logging.LogFactory;

public class BibEntry implements Cloneable {

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

public static final String TYPE_HEADER = "entrytype";
Expand Down

0 comments on commit 59d9e35

Please sign in to comment.