Search in sources :

Example 1 with CustomEntryType

use of org.jabref.model.entry.CustomEntryType in project jabref by JabRef.

the class EntryCustomizationDialog method applyChanges.

private void applyChanges() {
    valueChanged(new ListSelectionEvent(new JList<>(), 0, 0, false));
    List<String> actuallyChangedTypes = new ArrayList<>();
    // Iterate over our map of required fields, and list those types if necessary:
    List<String> types = typeComp.getFields();
    for (Map.Entry<String, List<String>> stringListEntry : reqLists.entrySet()) {
        if (!types.contains(stringListEntry.getKey())) {
            continue;
        }
        List<String> requiredFieldsList = stringListEntry.getValue();
        List<String> optionalFieldsList = optLists.get(stringListEntry.getKey());
        List<String> secondaryOptionalFieldsLists = opt2Lists.get(stringListEntry.getKey());
        if (secondaryOptionalFieldsLists == null) {
            secondaryOptionalFieldsLists = new ArrayList<>(0);
        }
        // If this type is already existing, check if any changes have
        // been made
        boolean changesMade = true;
        if (defaulted.contains(stringListEntry.getKey())) {
            // This type should be reverted to its default setup.
            EntryTypes.removeType(stringListEntry.getKey(), bibDatabaseMode);
            actuallyChangedTypes.add(stringListEntry.getKey().toLowerCase(Locale.ENGLISH));
            defaulted.remove(stringListEntry.getKey());
            continue;
        }
        Optional<EntryType> oldType = EntryTypes.getType(stringListEntry.getKey(), bibDatabaseMode);
        if (oldType.isPresent()) {
            List<String> oldRequiredFieldsList = oldType.get().getRequiredFieldsFlat();
            List<String> oldOptionalFieldsList = oldType.get().getOptionalFields();
            if (biblatexMode) {
                List<String> oldPrimaryOptionalFieldsLists = oldType.get().getPrimaryOptionalFields();
                List<String> oldSecondaryOptionalFieldsList = oldType.get().getSecondaryOptionalFields();
                if (equalLists(oldRequiredFieldsList, requiredFieldsList) && equalLists(oldPrimaryOptionalFieldsLists, optionalFieldsList) && equalLists(oldSecondaryOptionalFieldsList, secondaryOptionalFieldsLists)) {
                    changesMade = false;
                }
            } else if (equalLists(oldRequiredFieldsList, requiredFieldsList) && equalLists(oldOptionalFieldsList, optionalFieldsList)) {
                changesMade = false;
            }
        }
        if (changesMade) {
            CustomEntryType customType = biblatexMode ? new CustomEntryType(StringUtil.capitalizeFirst(stringListEntry.getKey()), requiredFieldsList, optionalFieldsList, secondaryOptionalFieldsLists) : new CustomEntryType(StringUtil.capitalizeFirst(stringListEntry.getKey()), requiredFieldsList, optionalFieldsList);
            EntryTypes.addOrModifyCustomEntryType(customType, bibDatabaseMode);
            actuallyChangedTypes.add(customType.getName().toLowerCase(Locale.ENGLISH));
        }
    }
    // update all affected entries if something has been changed
    if (!actuallyChangedTypes.isEmpty()) {
        updateEntriesForChangedTypes(actuallyChangedTypes);
    }
    Set<String> typesToRemove = new HashSet<>();
    for (String existingType : EntryTypes.getAllTypes(bibDatabaseMode)) {
        if (!types.contains(existingType)) {
            typesToRemove.add(existingType);
        }
    }
    // Remove those that should be removed:
    if (!typesToRemove.isEmpty()) {
        for (String typeToRemove : typesToRemove) {
            deleteType(typeToRemove);
        }
    }
    updateTables();
    CustomEntryTypesManager.saveCustomEntryTypes(Globals.prefs);
}
Also used : ListSelectionEvent(javax.swing.event.ListSelectionEvent) ArrayList(java.util.ArrayList) CustomEntryType(org.jabref.model.entry.CustomEntryType) CustomEntryType(org.jabref.model.entry.CustomEntryType) EntryType(org.jabref.model.entry.EntryType) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ActionMap(javax.swing.ActionMap) InputMap(javax.swing.InputMap) JList(javax.swing.JList) HashSet(java.util.HashSet)

Example 2 with CustomEntryType

use of org.jabref.model.entry.CustomEntryType in project jabref by JabRef.

the class CustomEntryTypePreferenceMigration method getCustomEntryType.

/**
     * Retrieves all deprecated information about the entry type in preferences, with the tag given by number.
     *
     * (old implementation which has been copied)
     */
private static Optional<CustomEntryType> getCustomEntryType(int number) {
    String nr = String.valueOf(number);
    String name = prefs.get(CUSTOM_TYPE_NAME + nr);
    if (name == null) {
        return Optional.empty();
    }
    List<String> req = prefs.getStringList(CUSTOM_TYPE_REQ + nr);
    List<String> opt = prefs.getStringList(CUSTOM_TYPE_OPT + nr);
    List<String> priOpt = prefs.getStringList(CUSTOM_TYPE_PRIOPT + nr);
    if (priOpt.isEmpty()) {
        return Optional.of(new CustomEntryType(StringUtil.capitalizeFirst(name), req, opt));
    }
    List<String> secondary = new ArrayList<>(opt);
    secondary.removeAll(priOpt);
    return Optional.of(new CustomEntryType(StringUtil.capitalizeFirst(name), req, priOpt, secondary));
}
Also used : CustomEntryType(org.jabref.model.entry.CustomEntryType) ArrayList(java.util.ArrayList)

Example 3 with CustomEntryType

use of org.jabref.model.entry.CustomEntryType in project jabref by JabRef.

the class EntryTypesTest method setUp.

@Before
public void setUp() {
    newCustomType = new CustomEntryType("customType", "required", "optional");
    List<String> newRequiredFields = new ArrayList<>(BibtexEntryTypes.ARTICLE.getRequiredFields());
    newRequiredFields.add("additional");
    overwrittenStandardType = new CustomEntryType(BibtexEntryTypes.ARTICLE.getName(), newRequiredFields, Collections.singletonList("optional"));
}
Also used : CustomEntryType(org.jabref.model.entry.CustomEntryType) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Example 4 with CustomEntryType

use of org.jabref.model.entry.CustomEntryType in project jabref by JabRef.

the class BibDatabaseModeDetectionTest method ignoreUnknownTypesForBiblatexDecision.

@Test
public void ignoreUnknownTypesForBiblatexDecision() {
    BibEntry custom = new BibEntry(new CustomEntryType("unknowntype", new ArrayList<>(0), new ArrayList<>(0)).getName());
    BibEntry bibtex = new BibEntry(BibtexEntryTypes.ARTICLE.getName());
    BibEntry biblatex = new BibEntry(BiblatexEntryTypes.MVBOOK.getName());
    Collection<BibEntry> entries = Arrays.asList(custom, bibtex, biblatex);
    assertEquals(BibDatabaseMode.BIBLATEX, BibDatabaseModeDetection.inferMode(BibDatabases.createDatabase(entries)));
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) CustomEntryType(org.jabref.model.entry.CustomEntryType) Test(org.junit.Test)

Example 5 with CustomEntryType

use of org.jabref.model.entry.CustomEntryType in project jabref by JabRef.

the class BibDatabaseModeDetectionTest method detectUnknownTypeAsBibtex.

@Test
public void detectUnknownTypeAsBibtex() {
    BibEntry entry = new BibEntry(new CustomEntryType("unknowntype", new ArrayList<>(0), new ArrayList<>(0)).getName());
    Collection<BibEntry> entries = Arrays.asList(entry);
    assertEquals(BibDatabaseMode.BIBTEX, BibDatabaseModeDetection.inferMode(BibDatabases.createDatabase(entries)));
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) CustomEntryType(org.jabref.model.entry.CustomEntryType) Test(org.junit.Test)

Aggregations

CustomEntryType (org.jabref.model.entry.CustomEntryType)10 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 BibEntry (org.jabref.model.entry.BibEntry)4 EntryType (org.jabref.model.entry.EntryType)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 BackingStoreException (java.util.prefs.BackingStoreException)1 Preferences (java.util.prefs.Preferences)1 ActionMap (javax.swing.ActionMap)1 InputMap (javax.swing.InputMap)1 JList (javax.swing.JList)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 AutoCompletePreferences (org.jabref.logic.autocompleter.AutoCompletePreferences)1 FieldContentParserPreferences (org.jabref.logic.bibtex.FieldContentParserPreferences)1 LatexFieldFormatterPreferences (org.jabref.logic.bibtex.LatexFieldFormatterPreferences)1 BibtexKeyPatternPreferences (org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences)1 CleanupPreferences (org.jabref.logic.cleanup.CleanupPreferences)1