-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 support for (biblatex) langid
to be an optional field in entry editor
#12071
base: main
Are you sure you want to change the base?
Changes from all commits
bf270cc
3d21f10
08eaec7
203ad3b
5aa170a
5611bf7
b459a05
2b0f8a5
7aa34d7
dcbf144
1e7a04e
ac09f53
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jabref.gui.fieldeditors.optioneditors; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import javax.swing.undo.UndoManager; | ||
|
||
import javafx.util.StringConverter; | ||
|
||
import org.jabref.gui.autocompleter.SuggestionProvider; | ||
import org.jabref.logic.integrity.FieldCheckers; | ||
import org.jabref.model.database.BibDatabaseMode; | ||
import org.jabref.model.entry.Langid; | ||
import org.jabref.model.entry.field.Field; | ||
import org.jabref.model.strings.StringUtil; | ||
|
||
public class LanguageEditorViewModel extends OptionEditorViewModel<Langid> { | ||
private BibDatabaseMode databaseMode; | ||
|
||
public LanguageEditorViewModel(Field field, SuggestionProvider<?> suggestionProvider, BibDatabaseMode databaseMode, FieldCheckers fieldCheckers, UndoManager undoManager) { | ||
super(field, suggestionProvider, fieldCheckers, undoManager); | ||
this.databaseMode = databaseMode; | ||
} | ||
|
||
@Override | ||
public StringConverter<Langid> getStringConverter() { | ||
return new StringConverter<>() { | ||
@Override | ||
public String toString(Langid object) { | ||
if (object == null) { | ||
return null; | ||
} else { | ||
return object.getLangid(); // Langid used as both display and value | ||
} | ||
} | ||
|
||
@Override | ||
public Langid fromString(String string) { | ||
if (StringUtil.isNotBlank(string)) { | ||
return Langid.parse(string).orElse(null); | ||
} else { | ||
return null; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Collection<Langid> getItems() { | ||
return Arrays.asList(Langid.values()); | ||
} | ||
|
||
@Override | ||
public String convertToDisplayText(Langid object) { | ||
return object.getName(); // Langid and display text are the same | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.jabref.model.entry; | ||
|
||
import java.util.Optional; | ||
|
||
import org.jabref.logic.bibtex.FieldWriter; | ||
/** | ||
* Language identifiers based on BibLaTeX manual specifications. | ||
* See the BibLaTeX documentation for full details: | ||
* <a href="http://mirrors.ctan.org/macros/latex/contrib/biblatex/doc/biblatex.pdfhangelo">BibLaTeX manual</a> | ||
*/ | ||
|
||
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. JavaDoc needs to be attached to class --> remove empty line here (and add empty line above) |
||
public enum Langid { | ||
mag-sun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BASQUE("Basque", "basque"), | ||
BULGARIAN("Bulgarian", "bulgarian"), | ||
CATALAN("Catalan", "catalan"), | ||
CROATIAN("Croatian", "croatian"), | ||
CZECH("Czech", "czech"), | ||
DANISH("Danish", "danish"), | ||
AMERICAN("American", "american"), | ||
USENGLISH("US English", "USenglish"), | ||
ENGLISH("English", "english"), | ||
BRITISH("British", "british"), | ||
UKENGLISH("UK English", "UKenglish"), | ||
CANADIAN("Canadian", "canadian"), | ||
AUSTRALIAN("Australian", "australian"), | ||
NEWZEALAND("New Zealand", "newzealand"), | ||
ESTONIAN("Estonian", "estonian"), | ||
FINNISH("Finnish", "finnish"), | ||
FRENCH("French", "french"), | ||
GERMAN("German", "german"), | ||
AUSTRIAN("Austrian", "austrian"), | ||
SWISSGERMAN("Swiss German", "swissgerman"), | ||
NGERMAN("German (New)", "ngerman"), | ||
NAUSTRIAN("Austrian (New)", "naustrian"), | ||
NSWISSGERMAN("Swiss German (New)", "nswissgerman"), | ||
GREEK("Greek", "greek"), | ||
MAGYAR("Hungarian", "hungarian"), | ||
HUNGARIAN("Hungarian", "hungarian"), | ||
ICELANDIC("Icelandic", "icelandic"), | ||
ITALIAN("Italian", "italian"), | ||
LATVIAN("Latvian", "latvian"), | ||
LITHUANIAN("Lithuanian", "lithuanian"), | ||
MARATHI("Marathi", "marathi"), | ||
NORSK("Norwegian (Bokmål)", "norsk"), | ||
NYNORSK("Norwegian (Nynorsk)", "nynorsk"), | ||
POLISH("Polish", "polish"), | ||
BRAZIL("Portuguese (Brazilian)", "brazil"), | ||
PORTUGUESE("Portuguese", "portuguese"), | ||
PORTUGES("Portuguese (alt)", "portuges"), | ||
ROMANIAN("Romanian", "romanian"), | ||
RUSSIAN("Russian", "russian"), | ||
SERBIAN("Serbian (Latin)", "serbian"), | ||
SERBIANC("Serbian (Cyrillic)", "serbianc"), | ||
SLOVAK("Slovak", "slovak"), | ||
SLOVENE("Slovene", "slovene"), | ||
SLOVENIAN("Slovenian", "slovenian"), | ||
SPANISH("Spanish", "spanish"), | ||
SWEDISH("Swedish", "swedish"), | ||
TURKISH("Turkish", "turkish"), | ||
UKRAINIAN("Ukrainian", "ukrainian"); | ||
|
||
|
||
private final String name; | ||
private final String langid; | ||
|
||
Langid(String name, String langid) { | ||
this.name = name; | ||
this.langid = langid; | ||
} | ||
|
||
public String getLangid() { | ||
return langid; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public static Optional<Langid> getByLangid(String id) { | ||
for (Langid lang : Langid.values()) { | ||
if (lang.langid.equalsIgnoreCase(id)) { | ||
return Optional.of(lang); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static Optional<Langid> parse(String value) { | ||
return Langid.getByLangid(value.trim().toLowerCase()); | ||
} | ||
|
||
public String getJabRefFormat() { | ||
return (FieldWriter.BIBTEX_STRING_START_END_SYMBOL + "%s" + FieldWriter.BIBTEX_STRING_START_END_SYMBOL).formatted(langid); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.jabref.gui.fieldeditors.optioneditors; | ||
|
||
import java.util.Collection; | ||
|
||
import javax.swing.undo.UndoManager; | ||
|
||
import org.jabref.gui.autocompleter.SuggestionProvider; | ||
import org.jabref.logic.FilePreferences; | ||
import org.jabref.logic.integrity.FieldCheckers; | ||
import org.jabref.logic.journals.JournalAbbreviationRepository; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.database.BibDatabaseMode; | ||
import org.jabref.model.entry.Langid; | ||
import org.jabref.model.entry.field.StandardField; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class LanguageEditorViewModelTest { | ||
|
||
private LanguageEditorViewModel languageEditorViewModel; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// Mock dependencies | ||
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. Remove comment. The next lines state that by code. |
||
BibDatabaseContext databaseContext = Mockito.mock(BibDatabaseContext.class); | ||
FilePreferences filePreferences = Mockito.mock(FilePreferences.class); | ||
JournalAbbreviationRepository abbreviationRepository = Mockito.mock(JournalAbbreviationRepository.class); | ||
|
||
// Initialize FieldCheckers with mocked instances | ||
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. Remove comment. The next lines state that by code. |
||
FieldCheckers fieldCheckers = new FieldCheckers(databaseContext, filePreferences, abbreviationRepository, false); | ||
|
||
// Mock the SuggestionProvider | ||
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. Remove comment. The next lines state that by code. |
||
SuggestionProvider<?> suggestionProvider = Mockito.mock(SuggestionProvider.class); | ||
|
||
// Initialize the LangidEditorViewModel | ||
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. Remove comment. The next lines state that by code. Comments are useful if they add information. E.g. for the reasons of the code... - But not necessary here IMHO. |
||
languageEditorViewModel = new LanguageEditorViewModel( | ||
StandardField.LANGUAGEID, // Use the correct field | ||
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. Remove comment. |
||
suggestionProvider, // Mocked SuggestionProvider | ||
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. Remove comment. |
||
BibDatabaseMode.BIBLATEX, // Use the correct BibDatabaseMode | ||
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. Remove comment. |
||
fieldCheckers, // FieldCheckers instance | ||
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. Remove comment. |
||
new UndoManager() // UndoManager instance | ||
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. Remove comment. |
||
); | ||
} | ||
|
||
@Test | ||
void getItemsShouldReturnAllLangidValues() { | ||
Collection<Langid> items = languageEditorViewModel.getItems(); | ||
assertEquals(Langid.values().length, items.size()); | ||
assertTrue(items.contains(Langid.BASQUE)); // Check if it contains a specific Langid | ||
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. Remove comment. |
||
assertTrue(items.contains(Langid.AMERICAN)); // Additional check for another Langid | ||
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. Try to rewrite multiple assert statements to one assertEquals(List.of(...), items) |
||
} | ||
|
||
@Test | ||
void testStringConversion() { | ||
String langidString = "bulgarian"; | ||
Langid langid = languageEditorViewModel.getStringConverter().fromString(langidString); | ||
assertEquals(Langid.BULGARIAN, langid, "String should convert to the corresponding Langid"); | ||
|
||
String convertedString = languageEditorViewModel.getStringConverter().toString(Langid.BULGARIAN); | ||
assertEquals(langidString, convertedString, "Langid should convert back to its string representation"); | ||
} | ||
|
||
@Test | ||
void testStringConversionWithHumanReadableName() { | ||
// Test conversion from human-readable name to Langid | ||
String langidString = "Basque"; | ||
Langid langid = languageEditorViewModel.getStringConverter().fromString(langidString); | ||
assertEquals(Langid.BASQUE, langid, "Human-readable name should convert to the corresponding Langid"); | ||
|
||
// Test conversion from Langid to human-readable name | ||
String convertedString = languageEditorViewModel.getStringConverter().toString(Langid.BASQUE); | ||
assertEquals("basque", convertedString, "Langid should convert back to its lowercase string representation"); | ||
} | ||
|
||
@Test | ||
void testHandlingNullValue() { | ||
// Test the handling of a null value | ||
Langid result = languageEditorViewModel.getStringConverter().fromString(null); | ||
assertEquals(null, result, "Null input should return null Langid"); | ||
} | ||
|
||
@Test | ||
void testHandlingBlankValue() { | ||
// Test the handling of a blank string | ||
Langid result = languageEditorViewModel.getStringConverter().fromString(" "); | ||
assertEquals(null, result, "Blank input should return null Langid"); | ||
} | ||
} | ||
|
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.
Revert this change.