-
-
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
Add drop down for citation key patterns #12516
Merged
+286
−4
Merged
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1618eec
Add a suggestions text field
priyanshu16095 7597467
Improve text field and make use of CitationKeyPattern record
priyanshu16095 1b819f9
Fix the check
priyanshu16095 b8527aa
Change a comment
priyanshu16095 88965e9
Make the fields private
priyanshu16095 40ce160
Make remaining fields private
priyanshu16095 948a495
Fix the check
priyanshu16095 b874dfd
Make fields public again
priyanshu16095 fac8fbc
Fix the UI issue
priyanshu16095 7a61dad
Update CitationKeyPattern.java
priyanshu16095 ed42bcc
Define the MAX_ENTRIES variable
priyanshu16095 43292ba
Merge branch 'main' into addDropdown
subhramit 249229b
Add submenu and improve suggestions
priyanshu16095 4dff9f2
Add pattern in Java record
priyanshu16095 10522b4
Fix the check
priyanshu16095 58ef2f4
Add setOnAction on menu item
priyanshu16095 539c1fd
Add a CHANGELOG.md entry
priyanshu16095 6f235c9
Fix the issue
priyanshu16095 353f1bd
Add keys in JabRef_en.properties
priyanshu16095 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/main/java/org/jabref/gui/commonfxcontrols/CitationKeyPatternSuggestionCell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.jabref.gui.commonfxcontrols; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javafx.scene.control.ContextMenu; | ||
import javafx.scene.control.CustomMenuItem; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.control.cell.TextFieldTableCell; | ||
|
||
public class CitationKeyPatternSuggestionCell extends TextFieldTableCell<CitationKeyPatternsPanelItemModel, String> { | ||
private final CitationKeyPatternSuggestoinTextField searchField; | ||
|
||
public CitationKeyPatternSuggestionCell(List<String> citationKeyPatterns) { | ||
this.searchField = new CitationKeyPatternSuggestoinTextField(citationKeyPatterns); | ||
searchField.setOnAction(event -> commitEdit(searchField.getText())); | ||
} | ||
|
||
@Override | ||
public void startEdit() { | ||
super.startEdit(); | ||
searchField.setText(getItem()); | ||
setGraphic(searchField); | ||
searchField.requestFocus(); | ||
} | ||
|
||
@Override | ||
public void cancelEdit() { | ||
super.cancelEdit(); | ||
setGraphic(null); | ||
} | ||
|
||
@Override | ||
public void commitEdit(String newValue) { | ||
super.commitEdit(newValue); | ||
getTableView().getItems().get(getIndex()).setPattern(newValue); | ||
setGraphic(null); | ||
} | ||
|
||
@Override | ||
public void updateItem(String item, boolean empty) { | ||
super.updateItem(item, empty); | ||
if (empty || item == null) { | ||
setGraphic(null); | ||
setText(null); | ||
} else { | ||
setText(item); | ||
} | ||
} | ||
|
||
static class CitationKeyPatternSuggestoinTextField extends TextField { | ||
private final List<String> citationKeyPatterns; | ||
private final ContextMenu suggestionsList; | ||
|
||
public CitationKeyPatternSuggestoinTextField(List<String> citationKeyPatterns) { | ||
this.citationKeyPatterns = new ArrayList<>(citationKeyPatterns); | ||
this.suggestionsList = new ContextMenu(); | ||
|
||
setListener(); | ||
} | ||
|
||
private void setListener() { | ||
textProperty().addListener((observable, oldValue, newValue) -> { | ||
String enteredText = getText(); | ||
if (enteredText == null || enteredText.isEmpty()) { | ||
suggestionsList.hide(); | ||
} else { | ||
List<String> filteredEntries = citationKeyPatterns.stream() | ||
.filter(e -> e.toLowerCase().contains(enteredText.toLowerCase())) | ||
.collect(Collectors.toList()); | ||
if (!filteredEntries.isEmpty()) { | ||
populatePopup(filteredEntries); | ||
if (!suggestionsList.isShowing() && getScene() != null) { | ||
double screenX = localToScreen(0, 0).getX(); | ||
double screenY = localToScreen(0, 0).getY() + getHeight(); | ||
suggestionsList.show(this, screenX, screenY); | ||
} | ||
} else { | ||
suggestionsList.hide(); | ||
} | ||
} | ||
}); | ||
|
||
focusedProperty().addListener((observable, oldValue, newValue) -> { | ||
suggestionsList.hide(); | ||
}); | ||
} | ||
|
||
private void populatePopup(List<String> searchResult) { | ||
List<CustomMenuItem> menuItems = new ArrayList<>(); | ||
int maxEntries = 7; | ||
int count = Math.min(searchResult.size(), maxEntries); | ||
|
||
for (int i = 0; i < count; i++) { | ||
final String result = searchResult.get(i); | ||
Label entryLabel = new Label(result); | ||
entryLabel.setPrefWidth(getWidth()); | ||
|
||
CustomMenuItem item = new CustomMenuItem(entryLabel, true); | ||
item.setHideOnClick(false); | ||
|
||
menuItems.add(item); | ||
|
||
item.setOnAction(actionEvent -> { | ||
setText(result); | ||
positionCaret(result.length()); | ||
suggestionsList.hide(); | ||
}); | ||
} | ||
|
||
suggestionsList.getItems().clear(); | ||
suggestionsList.getItems().addAll(menuItems); | ||
|
||
if (!menuItems.isEmpty()) { | ||
menuItems.getFirst().getContent().requestFocus(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/org/jabref/logic/citationkeypattern/CitationKeyPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,93 @@ | ||
package org.jabref.logic.citationkeypattern; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public record CitationKeyPattern(String stringRepresentation) { | ||
public static final CitationKeyPattern NULL_CITATION_KEY_PATTERN = new CitationKeyPattern(""); | ||
|
||
// region - Author-related field markers | ||
public static final CitationKeyPattern AUTHOR_YEAR = new CitationKeyPattern("[auth]"); | ||
public static final CitationKeyPattern AUTHOR_FIRST_FULL = new CitationKeyPattern("[authFirstFull]"); | ||
public static final CitationKeyPattern AUTHOR_FORE_INI = new CitationKeyPattern("[authForeIni]"); | ||
public static final CitationKeyPattern AUTHOR_ETAL = new CitationKeyPattern("[auth.etal]"); | ||
public static final CitationKeyPattern AUTHOR_ET_AL = new CitationKeyPattern("[authEtAl]"); | ||
public static final CitationKeyPattern AUTHOR_AUTH_EA = new CitationKeyPattern("[auth.auth.ea]"); | ||
public static final CitationKeyPattern AUTHORS = new CitationKeyPattern("[authors]"); | ||
public static final CitationKeyPattern AUTHORS_N = new CitationKeyPattern("[authorsN]"); | ||
public static final CitationKeyPattern AUTH_INI_N = new CitationKeyPattern("[authIniN]"); | ||
public static final CitationKeyPattern AUTH_N = new CitationKeyPattern("[authN]"); | ||
public static final CitationKeyPattern AUTH_N_M = new CitationKeyPattern("[authN_M]"); | ||
public static final CitationKeyPattern AUTHOR_INI = new CitationKeyPattern("[authorIni]"); | ||
public static final CitationKeyPattern AUTH_SHORT = new CitationKeyPattern("[authshort]"); | ||
public static final CitationKeyPattern AUTHORS_ALPHA = new CitationKeyPattern("[authorsAlpha]"); | ||
public static final CitationKeyPattern AUTHORS_ALPHA_LNI = new CitationKeyPattern("[authorsAlphaLNI]"); | ||
public static final CitationKeyPattern AUTHORS_LAST = new CitationKeyPattern("[authorsLast]"); | ||
public static final CitationKeyPattern AUTHORS_LAST_FORE_INI = new CitationKeyPattern("[authorsLastForeIni]"); | ||
// endregion | ||
|
||
// region - Editor-related field markers | ||
public static final CitationKeyPattern EDTR = new CitationKeyPattern("[edtr]"); | ||
public static final CitationKeyPattern EDTR_INI_N = new CitationKeyPattern("[edtrIniN]"); | ||
public static final CitationKeyPattern EDITORS = new CitationKeyPattern("[editors]"); | ||
public static final CitationKeyPattern EDITOR_LAST = new CitationKeyPattern("[editorLast]"); | ||
public static final CitationKeyPattern EDITOR_INI = new CitationKeyPattern("[editorIni]"); | ||
public static final CitationKeyPattern EDTR_N = new CitationKeyPattern("[edtrN]"); | ||
public static final CitationKeyPattern EDTR_N_M = new CitationKeyPattern("[edtrN_M]"); | ||
public static final CitationKeyPattern EDTR_EDTR_EA = new CitationKeyPattern("[edtr.edtr.ea]"); | ||
public static final CitationKeyPattern EDTRSHORT = new CitationKeyPattern("[edtrshort]"); | ||
public static final CitationKeyPattern EDTR_FORE_INI = new CitationKeyPattern("[edtrForeIni]"); | ||
public static final CitationKeyPattern EDITOR_LAST_FORE_INI = new CitationKeyPattern("[editorLastForeIni]"); | ||
// endregion | ||
|
||
// region - Title-related field markers | ||
public static final CitationKeyPattern SHORTTITLE = new CitationKeyPattern("[shorttitle]"); | ||
public static final CitationKeyPattern SHORTTITLE_INI = new CitationKeyPattern("[shorttitleINI]"); | ||
public static final CitationKeyPattern VERYSHORTTITLE = new CitationKeyPattern("[veryshorttitle]"); | ||
public static final CitationKeyPattern CAMEL = new CitationKeyPattern("[camel]"); | ||
public static final CitationKeyPattern CAMEL_N = new CitationKeyPattern("[camelN]"); | ||
public static final CitationKeyPattern TITLE = new CitationKeyPattern("[title]"); | ||
public static final CitationKeyPattern FULLTITLE = new CitationKeyPattern("[fulltitle]"); | ||
// endregion | ||
|
||
// region - Other field markers | ||
public static final CitationKeyPattern ENTRYTYPE = new CitationKeyPattern("[entrytype]"); | ||
public static final CitationKeyPattern FIRSTPAGE = new CitationKeyPattern("[firstpage]"); | ||
public static final CitationKeyPattern PAGEPREFIX = new CitationKeyPattern("[pageprefix]"); | ||
public static final CitationKeyPattern KEYWORD_N = new CitationKeyPattern("[keywordN]"); | ||
public static final CitationKeyPattern KEYWORDS_N = new CitationKeyPattern("[keywordsN]"); | ||
public static final CitationKeyPattern LASTPAGE = new CitationKeyPattern("[lastpage]"); | ||
public static final CitationKeyPattern SHORTYEAR = new CitationKeyPattern("[shortyear]"); | ||
// endregion | ||
|
||
// region - Bibentry fields | ||
public static final CitationKeyPattern AUTHOR = new CitationKeyPattern("[AUTHOR]"); | ||
public static final CitationKeyPattern DATE = new CitationKeyPattern("[DATE]"); | ||
public static final CitationKeyPattern DAY = new CitationKeyPattern("[DAY]"); | ||
public static final CitationKeyPattern GROUPS = new CitationKeyPattern("[GROUPS]"); | ||
public static final CitationKeyPattern MONTH = new CitationKeyPattern("[MONTH]"); | ||
public static final CitationKeyPattern YEAR = new CitationKeyPattern("[YEAR]"); | ||
// endregion | ||
|
||
public static List<CitationKeyPattern> getAllPatterns() { | ||
return Arrays.stream(CitationKeyPattern.class.getDeclaredFields()) | ||
.filter(field -> { | ||
int modifiers = field.getModifiers(); | ||
return Modifier.isPublic(modifiers) && | ||
Modifier.isStatic(modifiers) && | ||
Modifier.isFinal(modifiers) && | ||
field.getType() == CitationKeyPattern.class && | ||
!field.equals(CitationKeyPattern.NULL_CITATION_KEY_PATTERN); | ||
}) | ||
.map(field -> { | ||
try { | ||
return (CitationKeyPattern) field.get(null); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException("Could not access field", e); | ||
} | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Move this to a class-level constant with a brief comment explaining its use and the value