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

Added button to crossref entry editor field to select parent entry #1563

Merged
merged 4 commits into from
Jul 12, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -18,6 +18,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Updated German translation
- When resolving duplicate BibTeX-keys there is now an "Ignore" button. "Cancel" and close key now quits the resolving.
- The [online forum](http://discourse.jabref.org/) is now directly accessible via the "Help" menu
- Implemented [#1338](https://github.com/JabRef/jabref/issues/1338): clicking on a crossref in the main table selects the parent entry and added a button in the entry editor to select the parent entry.

### Fixed
- Fixed [#405](https://github.com/JabRef/jabref/issues/405): Added more {} around capital letters in Unicode/HTML to LaTeX conversion to preserve them
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {
return FieldExtraComponents.getPaginationExtraComponent(editor, this);
} else if (fieldExtras.contains(FieldProperties.TYPE)) {
return FieldExtraComponents.getTypeExtraComponent(editor, this, "patent".equalsIgnoreCase(entry.getType()));
} else if (fieldExtras.contains(FieldProperties.CROSSREF)) {
return FieldExtraComponents.getCrossrefExtraComponent(editor, frame.getCurrentBasePanel());
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,55 @@ public static Optional<JComponent> getGenderExtraComponent(FieldEditor fieldEdit
return Optional.of(gender);

}

/**
* Return a button which sets the owner if the field for fields with EXTRA_SET_OWNER
Copy link
Member

Choose a reason for hiding this comment

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

I think the comment is wrong here.

Copy link
Contributor Author

@oscargus oscargus Jul 9, 2016 via email

Choose a reason for hiding this comment

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

* @param fieldEditor
* @param storeFieldAction
* @return
*/

public static Optional<JComponent> getCrossrefExtraComponent(FieldEditor fieldEditor,
BasePanel panel) {
JButton button = new JButton(Localization.lang("Select"));
Copy link
Member

Choose a reason for hiding this comment

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

Rename to "Go to parent"?

JTextComponent crossref = (JTextComponent) fieldEditor;

button.addActionListener(actionEvent -> {
panel.highlightEntry(panel.getDatabase().getEntryByKey(crossref.getText()));
});

// enable/disable button

DocumentListener documentListener = new DocumentListener() {

@Override
public void changedUpdate(DocumentEvent documentEvent) {
checkUrl();
Copy link
Member

Choose a reason for hiding this comment

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

rename checkUrl

}

@Override
public void insertUpdate(DocumentEvent documentEvent) {
checkUrl();
}

@Override
public void removeUpdate(DocumentEvent documentEvent) {
checkUrl();
}

private void checkUrl() {
if (panel.getDatabase().containsEntryWithKey(crossref.getText())) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
};

crossref.getDocument().addDocumentListener(documentListener);
Copy link
Member

Choose a reason for hiding this comment

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

Inline documentListener?

Copy link
Contributor Author

@oscargus oscargus Jul 9, 2016 via email

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

In that step you could make a lamdba from it...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I understand how. As I recall, lambda are only usable if there is a single method that is called? (Which there is, but through three different overridden methods.)


return Optional.of(button);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ public void mouseClicked(MouseEvent e) {
}
}
});
} else if (modelColumn.getBibtexFields().contains("crossref")) { // Clicking on crossref column
tableRows.get(row).getFieldOptional("crossref")
.ifPresent(crossref -> panel.highlightEntry(panel.getDatabase().getEntryByKey(crossref)));
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/sf/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ public boolean containsEntryWithId(String id) {
return internalIDs.contains(id);
}

/**
* Returns whether an entry with the given bibtex key exists.
*/
public synchronized boolean containsEntryWithKey(String key) {
int keyHash = key.hashCode(); // key hash for better performance
Copy link
Member

Choose a reason for hiding this comment

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

just use getEntryByKey(key) != null.
By the way, I would welcome if you could change the return type of getEntryByKey to optional.

Copy link
Contributor Author

@oscargus oscargus Jul 9, 2016 via email

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Well, I think it is safe to change getEntryByKey to return the first entry with the key (instead of the last one). And if you at changing this method, then replace the comparison of hashcodes with real equals (like in getEntriesByKey)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MIssed this comment, but getEntryByKeyOptional was introduced in #1568. It is an intermediate solution before figuring out the final conversion problem.

Makes sense to return the first, so I'll implement that later on tonight.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two-stage solution. Removed containsEntryWithKey here and will do the other changes in #1568.


for (BibEntry entry : entries) {
if ((entry != null) && (entry.getCiteKey() != null) && (keyHash == entry.getCiteKey().hashCode())) {
return true;
}
}
return false;
}

public List<BibEntry> getEntries() {
return Collections.unmodifiableList(entries);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/model/entry/FieldProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public enum FieldProperties {
DOI,
EDITOR_TYPE,
PAGINATION,
TYPE;
TYPE,
CROSSREF;

public static final Set<FieldProperties> ALL_OPTS = EnumSet.allOf(FieldProperties.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ private InternalBibtexFields() {
add(new BibtexSingleField("author", true, BibtexSingleField.MEDIUM_W, 280));
add(new BibtexSingleField("booktitle", true, 175));
add(new BibtexSingleField("chapter", true, BibtexSingleField.SMALL_W));
add(new BibtexSingleField("crossref", true, BibtexSingleField.SMALL_W));
dummy = new BibtexSingleField("crossref", true, BibtexSingleField.SMALL_W);
dummy.setExtras(EnumSet.of(FieldProperties.CROSSREF));
add(dummy);
add(new BibtexSingleField("edition", true, BibtexSingleField.SMALL_W));
add(new BibtexSingleField("editor", true, BibtexSingleField.MEDIUM_W, 280));
add(new BibtexSingleField("howpublished", true, BibtexSingleField.MEDIUM_W));
Expand Down