-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* @param fieldEditor | ||
* @param storeFieldAction | ||
* @return | ||
*/ | ||
|
||
public static Optional<JComponent> getCrossrefExtraComponent(FieldEditor fieldEditor, | ||
BasePanel panel) { | ||
JButton button = new JButton(Localization.lang("Select")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline documentListener? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My code checking tool already complains that the anonymous class is too
long. :-)
But, yes, could indeed be done.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that step you could make a lamdba from it... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just use getEntryByKey(key) != null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After checking that code I decided to write this method. I think it will be
noticeable on large databases. (getEntryByName always goes through the
whole database and returns the last entry with that key).
I was thinking about the optional thing though. :-)
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two-stage solution. Removed |
||
|
||
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); | ||
} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.