Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ static class CitationKeyPatternSuggestionTextField extends TextField {
// Maximum number of entries that can be displayed in the popup menu.
private static final int MAX_ENTRIES = 7;

// Maximum number of entries that can be displayed in the popup menu.
private static final int MAX_COMPOUND_PATTERNS = 10;

private final List<String> citationKeyPatterns;
private final ContextMenu suggestionsList;
private String enteredText;

public CitationKeyPatternSuggestionTextField(List<String> citationKeyPatterns) {
this.citationKeyPatterns = new ArrayList<>(citationKeyPatterns);
Expand All @@ -72,7 +76,7 @@ public CitationKeyPatternSuggestionTextField(List<String> citationKeyPatterns) {

private void setListener() {
textProperty().addListener((observable, oldValue, newValue) -> {
String enteredText = getText();
this.enteredText = getText();
if (enteredText == null || enteredText.isEmpty()) {
suggestionsList.hide();
} else {
Expand All @@ -90,6 +94,7 @@ private void setListener() {
} else {
suggestionsList.hide();
}
showCompoundPatternsMenu(enteredText);
}
});

Expand Down Expand Up @@ -121,13 +126,59 @@ private void populatePopup(List<String> searchResult) {

suggestionsList.getItems().clear();
suggestionsList.getItems().add(createPatternsSubMenu());
suggestionsList.getItems().add(showCompoundPatternsMenu(enteredText));
suggestionsList.getItems().addAll(menuItems);

if (!menuItems.isEmpty()) {
menuItems.getFirst().getContent().requestFocus();
}
}

public Menu showCompoundPatternsMenu(String enteredText) {
return createCompoundPatternsSubMenu(generateCompoundPatterns(enteredText));
}

public List<String> generateCompoundPatterns(String enteredText) {
List<String> patterns = List.of("auth", "edtr", "year", "title", "date");
Copy link
Contributor Author

@priyanshu16095 priyanshu16095 Feb 27, 2025

Choose a reason for hiding this comment

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

@subhramit I used this list as I don’t have much knowledge about the fields and can you explain more about compound patterns?

Copy link
Member

Choose a reason for hiding this comment

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

Please use the constants of org.jabref.model.entry.field.StandardField here.

The fields can be read about at BibLaTeX documentation at https://ctan.org/pkg/biblatex.

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 list should be manually crafted instead of just combinding all fields

The description at #12502 (comment) talks about auto completion

That would be nice.

If this is not possible, some pre-selected combinations should be doable.

I think, it needs to be a separate pop up then - even if multiple modals are not a good style ^^

Grammar: [{pattern}](_[{pattern}])*

  1. A user can select a pattern.
  2. Then, the user can select "+"
  3. Then, JabRef adds "_" and offers another dropdown to select (the one selected before is NOT offered)

Reason: A user wants to use AuthEtAl together with year typically.

@priyanshu16095 Think of this as autogenerated (!) human-readable-primary-key in databases. MAybe this helps. -- Another thing: Either data of a BibEntry can be used directly or some processing of JabRef can be used. The former is ensured by CAPITAL LETTERS (first paragraph at https://docs.jabref.org/setup/citationkeypatterns#citation-key-patterns). The latter is described in the other text.

List<String> filteredPatterns = new ArrayList<>();

for (String pattern : patterns) {
if (pattern.contains(enteredText)) {
filteredPatterns.add(pattern);
}
}

List<String> compoundPatterns = new ArrayList<>();

for (String pattern : filteredPatterns) {
for (String otherPattern : patterns) {
if (!pattern.equals(otherPattern)) {
compoundPatterns.add("[" + pattern + "]_" + "[" + otherPattern + "]");
}
}
}

return compoundPatterns;
}

private Menu createCompoundPatternsSubMenu(List<String> compoundPatterns) {
Menu compoundPatternsSubMenu = new Menu(Localization.lang("Compound patterns"));

int count = Math.min(compoundPatterns.size(), MAX_COMPOUND_PATTERNS);
for (int i = 0; i < count; i++) {
final String compoundPattern = compoundPatterns.get(i);
MenuItem menuItem = new MenuItem(compoundPattern.replace("_", "__"));
menuItem.setOnAction(event -> {
setText(compoundPattern);
positionCaret(compoundPattern.length());
suggestionsList.hide();
});
compoundPatternsSubMenu.getItems().add(menuItem);
}

return compoundPatternsSubMenu;
}

private Menu createPatternsSubMenu() {
Menu patternsSubMenu = new Menu(Localization.lang("All patterns"));

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2858,6 +2858,7 @@ Entries\ copied\ successfully,\ including\ cross-references.=Entries copied succ
Entries\ copied\ successfully,\ without\ cross-references.=Entries copied successfully, without cross-references.

All\ patterns=All patterns
Compound\ patterns=Compound patterns
Author\ related=Author related
Editor\ related=Editor related
Title\ related=Title related
Expand Down