Skip to content

Commit

Permalink
Resolves #2181 Focus entries by ID instead of bibtexkey
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Dec 8, 2016
1 parent 1371834 commit 70b31da
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 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
32 changes: 18 additions & 14 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 @@ -31,13 +32,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 @@ -56,26 +54,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 @@ -86,13 +88,12 @@ 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
}
Expand All @@ -101,8 +102,11 @@ public boolean include(Entry<?, ?> entry) {

GUIUtil.correctRowHeight(table);

// 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
16 changes: 15 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,21 @@ 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) {
for (BibEntry entry : entries) {
if (entry.getId().equals(id)) {
return Optional.of(entry);
}
}
return Optional.empty();
}

/**
* 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 70b31da

Please sign in to comment.