From 69ca54f522804f6118a9b11b1ef56f86318dc32a Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sun, 5 Apr 2020 18:21:28 +0200 Subject: [PATCH 1/6] Refactored JabRefPreferences, concentrated EntryEditorPreferences and extracted store logic out of dialog CustomizeGeneralFieldsDialogViewModel. --- ...CustomizeGeneralFieldsDialogViewModel.java | 55 ++-- .../jabref/gui/entryeditor/EntryEditor.java | 2 +- .../entryeditor/EntryEditorPreferences.java | 12 +- .../gui/entryeditor/EntryEditorTabList.java | 43 ---- .../gui/entryeditor/OtherFieldsTab.java | 23 +- .../jabref/preferences/JabRefPreferences.java | 236 +++++++++++------- .../preferences/PreferencesService.java | 25 +- src/main/resources/l10n/JabRef_en.properties | 6 +- 8 files changed, 219 insertions(+), 183 deletions(-) delete mode 100644 src/main/java/org/jabref/gui/entryeditor/EntryEditorTabList.java diff --git a/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java b/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java index 47229fa6248..f1dbcd558bd 100644 --- a/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java +++ b/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java @@ -1,5 +1,6 @@ package org.jabref.gui.customizefields; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; @@ -11,7 +12,6 @@ import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.field.Field; import org.jabref.model.entry.field.FieldFactory; -import org.jabref.preferences.JabRefPreferences; import org.jabref.preferences.PreferencesService; public class CustomizeGeneralFieldsDialogViewModel { @@ -23,13 +23,16 @@ public class CustomizeGeneralFieldsDialogViewModel { public CustomizeGeneralFieldsDialogViewModel(DialogService dialogService, PreferencesService preferences) { this.dialogService = dialogService; this.preferences = preferences; - setInitialFieldsText(); + + // Using stored custom values or, if they do not exist, default values + setFieldsText(preferences.getEntryEditorTabList()); } - private void setInitialFieldsText() { + private void setFieldsText(Map> tabNamesAndFields) { StringBuilder sb = new StringBuilder(); - for (Map.Entry> tab : preferences.getEntryEditorTabList().entrySet()) { + // Fill with customized vars + for (Map.Entry> tab : tabNamesAndFields.entrySet()) { sb.append(tab.getKey()); sb.append(':'); sb.append(FieldFactory.serializeFieldsList(tab.getValue())); @@ -43,48 +46,36 @@ public StringProperty fieldsTextProperty() { } public void saveFields() { + Map> customTabsMap = new LinkedHashMap<>(); String[] lines = fieldsText.get().split("\n"); - int i = 0; - for (; i < lines.length; i++) { - String[] parts = lines[i].split(":"); + + for (String line : lines) { + String[] parts = line.split(":"); if (parts.length != 2) { - // Report error and exit. - String field = Localization.lang("field"); - String title = Localization.lang("Error"); - String content = Localization.lang("Each line must be of the following form") + " '" + - Localization.lang("Tabname") + ':' + field + "1;" + field + "2;...;" + field + "N'"; - dialogService.showInformationDialogAndWait(title, content); + dialogService.showInformationDialogAndWait( + Localization.lang("Error"), + Localization.lang("Each line must be of the following form: 'tab:field1;field2;...;fieldN'.")); return; } String testString = BibtexKeyGenerator.cleanKey(parts[1], preferences.getEnforceLegalKeys()); if (!testString.equals(parts[1]) || (parts[1].indexOf('&') >= 0)) { - String title = Localization.lang("Error"); - String content = Localization.lang("Field names are not allowed to contain white space or the following " - + "characters") - + ": # { } ( ) ~ , ^ & - \" ' ` ʹ \\"; - dialogService.showInformationDialogAndWait(title, content); + dialogService.showInformationDialogAndWait( + Localization.lang("Error"), + Localization.lang("Field names are not allowed to contain white spaces or certain characters (%0).", + "# { } ( ) ~ , ^ & - \" ' ` ʹ \\")); return; } - preferences.setCustomTabsNameAndFields(parts[0], parts[1], i); + customTabsMap.put(parts[0], FieldFactory.parseFieldList(parts[1])); } - preferences.purgeSeries(JabRefPreferences.CUSTOM_TAB_NAME, i); - preferences.purgeSeries(JabRefPreferences.CUSTOM_TAB_FIELDS, i); + + preferences.storeEntryEditorTabList(customTabsMap); preferences.updateEntryEditorTabList(); } public void resetFields() { - - StringBuilder sb = new StringBuilder(); - Map customTabNamesFields = preferences.getCustomTabsNamesAndFields(); - for (Map.Entryentry : customTabNamesFields.entrySet()) { - sb.append(entry.getKey()); - sb.append(':'); - sb.append(entry.getValue()); - sb.append('\n'); - } - fieldsText.set(sb.toString()); - + // Using default values + setFieldsText(preferences.getDefaultTabNamesAndFields()); } } diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index f6cbfb7b996..b6821df38d0 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -229,7 +229,7 @@ private List createTabs() { entryEditorTabs.add(new DeprecatedFieldsTab(databaseContext, panel.getSuggestionProviders(), undoManager, dialogService, Globals.prefs, Globals.entryTypesManager, ExternalFileTypes.getInstance(), Globals.TASK_EXECUTOR, Globals.journalAbbreviationLoader)); // Other fields - entryEditorTabs.add(new OtherFieldsTab(databaseContext, panel.getSuggestionProviders(), undoManager, entryEditorPreferences.getCustomTabFieldNames(), dialogService, Globals.prefs, Globals.entryTypesManager, ExternalFileTypes.getInstance(), Globals.TASK_EXECUTOR, Globals.journalAbbreviationLoader)); + entryEditorTabs.add(new OtherFieldsTab(databaseContext, panel.getSuggestionProviders(), undoManager, dialogService, Globals.prefs, Globals.entryTypesManager, ExternalFileTypes.getInstance(), Globals.TASK_EXECUTOR, Globals.journalAbbreviationLoader)); // General fields from preferences for (Map.Entry> tab : entryEditorPreferences.getEntryEditorTabList().entrySet()) { diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java index 26c4b48ef2a..3caba7af25d 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java @@ -24,7 +24,17 @@ public class EntryEditorPreferences { private boolean avoidOverwritingCiteKey; private final boolean shouldShowLatexCitationsTab; - public EntryEditorPreferences(Map> entryEditorTabList, FieldWriterPreferences fieldWriterPreferences, ImportFormatPreferences importFormatPreferences, List customTabFieldNames, boolean shouldShowRecommendationsTab, boolean isMrdlibAccepted, boolean shouldShowLatexCitationsTab, boolean showSourceTabByDefault, BibtexKeyPatternPreferences bibtexKeyPatternPreferences, KeyBindingRepository keyBindings, boolean avoidOverwritingCiteKey) { + public EntryEditorPreferences(Map> entryEditorTabList, + FieldWriterPreferences fieldWriterPreferences, + ImportFormatPreferences importFormatPreferences, + List customTabFieldNames, + boolean shouldShowRecommendationsTab, + boolean isMrdlibAccepted, + boolean shouldShowLatexCitationsTab, + boolean showSourceTabByDefault, + BibtexKeyPatternPreferences bibtexKeyPatternPreferences, + KeyBindingRepository keyBindings, + boolean avoidOverwritingCiteKey) { this.entryEditorTabList = entryEditorTabList; this.fieldWriterPreferences = fieldWriterPreferences; this.importFormatPreferences = importFormatPreferences; diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditorTabList.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditorTabList.java deleted file mode 100644 index d9caa0d9c28..00000000000 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditorTabList.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.jabref.gui.entryeditor; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; - -import org.jabref.model.entry.field.Field; -import org.jabref.model.entry.field.FieldFactory; -import org.jabref.preferences.JabRefPreferences; - -/** - * Class for creating the information about customizable entry editor tabs. - */ -public final class EntryEditorTabList { - - private EntryEditorTabList() { - - } - - public static Map> create(JabRefPreferences preferences) { - Map> tabs = new LinkedHashMap<>(); - int i = 0; - String name; - if (preferences.hasKey(JabRefPreferences.CUSTOM_TAB_NAME + 0)) { - // The user has modified from the default values: - while (preferences.hasKey(JabRefPreferences.CUSTOM_TAB_NAME + i)) { - name = preferences.get(JabRefPreferences.CUSTOM_TAB_NAME + i); - Set entry = FieldFactory.parseFieldList(preferences.get(JabRefPreferences.CUSTOM_TAB_FIELDS + i)); - tabs.put(name, entry); - i++; - } - } else { - // Nothing set, so we use the default values: - while (preferences.get(JabRefPreferences.CUSTOM_TAB_NAME + "_def" + i) != null) { - name = preferences.get(JabRefPreferences.CUSTOM_TAB_NAME + "_def" + i); - Set entry = FieldFactory.parseFieldList(preferences.get(JabRefPreferences.CUSTOM_TAB_FIELDS + "_def" + i)); - tabs.put(name, entry); - i++; - } - } - return tabs; - } -} diff --git a/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java index cf4a9b8323d..165347ff370 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java @@ -34,10 +34,27 @@ public class OtherFieldsTab extends FieldsEditorTab { private final List customTabFieldNames; private final BibEntryTypesManager entryTypesManager; - public OtherFieldsTab(BibDatabaseContext databaseContext, SuggestionProviders suggestionProviders, UndoManager undoManager, List customTabFieldNames, DialogService dialogService, JabRefPreferences preferences, BibEntryTypesManager entryTypesManager, ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationLoader journalAbbreviationLoader) { - super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, externalFileTypes, taskExecutor, journalAbbreviationLoader); + public OtherFieldsTab(BibDatabaseContext databaseContext, + SuggestionProviders suggestionProviders, + UndoManager undoManager, + DialogService dialogService, + JabRefPreferences preferences, + BibEntryTypesManager entryTypesManager, + ExternalFileTypes externalFileTypes, + TaskExecutor taskExecutor, + JournalAbbreviationLoader journalAbbreviationLoader) { + super(false, + databaseContext, + suggestionProviders, + undoManager, + dialogService, + preferences, // ToDo: Still uses JabRefPreferences instead of PreferencesService + externalFileTypes, + taskExecutor, + journalAbbreviationLoader); + this.entryTypesManager = entryTypesManager; - this.customTabFieldNames = customTabFieldNames; + this.customTabFieldNames = preferences.getAllDefaultTabFieldNames(); setText(Localization.lang("Other fields")); setTooltip(new Tooltip(Localization.lang("Show remaining fields"))); diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 096396915f3..348edd4862f 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -42,7 +43,6 @@ import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.gui.entryeditor.EntryEditorPreferences; -import org.jabref.gui.entryeditor.EntryEditorTabList; import org.jabref.gui.groups.GroupViewMode; import org.jabref.gui.keyboard.KeyBindingRepository; import org.jabref.gui.maintable.ColumnPreferences; @@ -763,21 +763,6 @@ private static void insertDefaultCleanupPreset(Map storage) { storage.put(CLEANUP_FORMATTERS, convertListToString(Cleanups.DEFAULT_SAVE_ACTIONS.getAsStringList(OS.NEWLINE))); } - @Override - public EntryEditorPreferences getEntryEditorPreferences() { - return new EntryEditorPreferences(getEntryEditorTabList(), - getFieldWriterPreferences(), - getImportFormatPreferences(), - getCustomTabFieldNames(), - getBoolean(SHOW_RECOMMENDATIONS), - getBoolean(ACCEPT_RECOMMENDATIONS), - getBoolean(SHOW_LATEX_CITATIONS), - getBoolean(DEFAULT_SHOW_SOURCE), - getBibtexKeyPatternPreferences(), - Globals.getKeyPrefs(), - getBoolean(AVOID_OVERWRITING_KEY)); - } - public Map getSidePanePreferredPositions() { Map preferredPositions = new HashMap<>(); @@ -814,58 +799,6 @@ public String getTheme() { return get(FX_THEME); } - /** - * Get a Map of default tab names to deafult tab fields. - * The fields are returned as a String with fields separated by ; - * They are combined into one string in order to feed into CustomizeGeneralFieldsDialogViewModel.resetFields() - * so that they do not have to be parsed in order to fit there - * - * @return A map of keys with tab names and values as a string containing - * fields for the given name separated by ; - */ - @Override - public Map getCustomTabsNamesAndFields() { - Map customTabsMap = new HashMap<>(); - - int defNumber = 0; - while (true) { - //Saved as CUSTOMTABNAME_def{number} and ; separated - String name = (String) defaults.get(CUSTOM_TAB_NAME + "_def" + defNumber); - String fields = (String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber); - - if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(fields)) { - break; - } - customTabsMap.put(name, fields); - defNumber++; - } - return customTabsMap; - } - - @Override - public void setCustomTabsNameAndFields(String name, String fields, int defNumber) { - prefs.put(CUSTOM_TAB_NAME + defNumber, name); - prefs.put(CUSTOM_TAB_FIELDS + defNumber, fields); - } - - private List getCustomTabFieldNames() { - List customFields = new ArrayList<>(); - - int defNumber = 0; - while (true) { - // saved as CUSTOMTABNAME_def{number} and ; separated - String fields = (String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber); - - if (StringUtil.isNullOrEmpty(fields)) { - break; - } - - customFields.addAll(Arrays.stream(fields.split(STRINGLIST_DELIMITER.toString())).map(FieldFactory::parseField).collect(Collectors.toList())); - defNumber++; - } - return customFields; - } - public void setLanguageDependentDefaultValues() { // Entry editor tab 0: defaults.put(CUSTOM_TAB_NAME + "_def0", Localization.lang("General")); @@ -1199,19 +1132,6 @@ public void purgeSeries(String prefix, int number) { } } - @Override - public Map> getEntryEditorTabList() { - if (tabList == null) { - updateEntryEditorTabList(); - } - return tabList; - } - - @Override - public void updateEntryEditorTabList() { - tabList = EntryEditorTabList.create(this); - } - /** * Exports Preferences to an XML file. * @@ -1287,13 +1207,6 @@ public FilePreferences getFilePreferences() { get(IMPORT_FILEDIRPATTERN)); } - public FieldWriterPreferences getFieldWriterPreferences() { - return new FieldWriterPreferences( - getBoolean(RESOLVE_STRINGS_ALL_FIELDS), - getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), - getFieldContentParserPreferences()); - } - public FieldContentFormatterPreferences getFieldContentParserPreferences() { return new FieldContentFormatterPreferences(getStringList(NON_WRAPPABLE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toList())); } @@ -1909,8 +1822,7 @@ public void saveCustomEntryTypes() { } private void saveCustomEntryTypes(BibDatabaseMode bibDatabaseMode) { - List customBiblatexBibTexTypes = Globals.entryTypesManager.getAllTypes(bibDatabaseMode).stream() - .map(entryType -> entryType).collect(Collectors.toList()); + List customBiblatexBibTexTypes = new ArrayList<>(Globals.entryTypesManager.getAllTypes(bibDatabaseMode)); storeBibEntryTypes(customBiblatexBibTexTypes, bibDatabaseMode); } @@ -2056,4 +1968,148 @@ public void storeTimestampPreferences(TimestampPreferences preferences) { put(TIME_STAMP_FORMAT, preferences.getTimestampFormat()); putBoolean(OVERWRITE_TIME_STAMP, preferences.isOverwriteTimestamp()); } + + //************************************************************************************************************* + // EntryEditorPreferences + //************************************************************************************************************* + + /** + * Cache a list of defined tabs in the entry editor + * + * @return a list of defined tabs + */ + @Override + public Map> getEntryEditorTabList() { + if (tabList == null) { + updateEntryEditorTabList(); + } + return tabList; + } + + /** + * Create a list of currently defined tabs in the entry editor + */ + @Override + public void updateEntryEditorTabList() { + Map> tabs = new LinkedHashMap<>(); + int i = 0; + String name; + if (hasKey(CUSTOM_TAB_NAME + 0)) { + // The user has modified from the default values: + while (hasKey(CUSTOM_TAB_NAME + i)) { + name = get(CUSTOM_TAB_NAME + i); + Set entry = FieldFactory.parseFieldList(get(CUSTOM_TAB_FIELDS + i)); + tabs.put(name, entry); + i++; + } + } else { + // Nothing set, so we use the default values: + while (get(JabRefPreferences.CUSTOM_TAB_NAME + "_def" + i) != null) { + name = get(JabRefPreferences.CUSTOM_TAB_NAME + "_def" + i); + Set entry = FieldFactory.parseFieldList(get(CUSTOM_TAB_FIELDS + "_def" + i)); + tabs.put(name, entry); + i++; + } + } + tabList = tabs; + } + + /** + * Stores the defined tabs and corresponding fields in the preferences. + * + * @param customTabs a map of tab names and the corresponding set of fields to be displayed in + */ + @Override + public void storeEntryEditorTabList(Map> customTabs) { + String[] names = customTabs.keySet().toArray(String[]::new); + String[] fields = customTabs.values().stream() + .map(set -> set.stream() + .map(Field::getName) + .collect(Collectors.joining(STRINGLIST_DELIMITER.toString()))) + .toArray(String[]::new); + + for (int i = 0; i < customTabs.size(); i++) { + put(CUSTOM_TAB_NAME + i, names[i]); + put(CUSTOM_TAB_FIELDS + i, fields[i]); + } + + purgeSeries(CUSTOM_TAB_NAME, customTabs.size()); + purgeSeries(CUSTOM_TAB_FIELDS, customTabs.size()); + } + + /** + * Get a Map of default tab names to default tab fields. + * + * @return A map of keys with tab names and a set of corresponding fields + */ + @Override + public Map> getDefaultTabNamesAndFields() { + Map> customTabsMap = new LinkedHashMap<>(); + + int defNumber = 0; + while (true) { + // Saved as 'CUSTOMTABNAME_def{number}' and seperated by ';' + String name = (String) defaults.get(CUSTOM_TAB_NAME + "_def" + defNumber); + String fields = (String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber); + + if (StringUtil.isNullOrEmpty(name) || StringUtil.isNullOrEmpty(fields)) { + break; + } + + customTabsMap.put(name, FieldFactory.parseFieldList((String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber))); + defNumber++; + } + return customTabsMap; + } + + /** + * Get a Map of default tab names to default tab fields. + * + * @return A map of keys with tab names and a set of corresponding fields + */ + @Override + public List getAllDefaultTabFieldNames() { + List customFields = new ArrayList<>(); + + int defNumber = 0; + while (true) { + // saved as CUSTOMTABNAME_def{number} and ; separated + String fields = (String) defaults.get(CUSTOM_TAB_FIELDS + "_def" + defNumber); + + if (StringUtil.isNullOrEmpty(fields)) { + break; + } + + customFields.addAll(Arrays.stream(fields.split(STRINGLIST_DELIMITER.toString())).map(FieldFactory::parseField).collect(Collectors.toList())); + defNumber++; + } + return customFields; + } + + @Override + public EntryEditorPreferences getEntryEditorPreferences() { + return new EntryEditorPreferences(getEntryEditorTabList(), + getFieldWriterPreferences(), + getImportFormatPreferences(), + getAllDefaultTabFieldNames(), + getBoolean(SHOW_RECOMMENDATIONS), + getBoolean(ACCEPT_RECOMMENDATIONS), + getBoolean(SHOW_LATEX_CITATIONS), + getBoolean(DEFAULT_SHOW_SOURCE), + getBibtexKeyPatternPreferences(), + Globals.getKeyPrefs(), + getBoolean(AVOID_OVERWRITING_KEY)); + } + + @Override + public void storeEntryEditorPreferences(EntryEditorPreferences preferences) { + // ToDo + } + + public FieldWriterPreferences getFieldWriterPreferences() { + return new FieldWriterPreferences( + getBoolean(RESOLVE_STRINGS_ALL_FIELDS), + getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), + getFieldContentParserPreferences()); + } } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 0bfba40b231..bf8abd03a9f 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -59,16 +59,8 @@ public interface PreferencesService { PreviewPreferences getPreviewPreferences(); - Map> getEntryEditorTabList(); - - Map getCustomTabsNamesAndFields(); - - void setCustomTabsNameAndFields(String name, String fields, int defNumber); - void purgeSeries(String prefix, int number); - void updateEntryEditorTabList(); - List getCustomExportFormats(JournalAbbreviationLoader loader); void storeCustomExportFormats(List exporters); @@ -101,8 +93,6 @@ public interface PreferencesService { void saveCustomEntryTypes(); - EntryEditorPreferences getEntryEditorPreferences(); - List loadBibEntryTypes(BibDatabaseMode mode); CleanupPreferences getCleanupPreferences(JournalAbbreviationLoader journalAbbreviationLoader); @@ -141,4 +131,19 @@ public interface PreferencesService { void storeTimestampPreferences(TimestampPreferences preferences); + // Entry Editor + + Map> getEntryEditorTabList(); + + void updateEntryEditorTabList(); + + Map> getDefaultTabNamesAndFields(); + + List getAllDefaultTabFieldNames(); + + void storeEntryEditorTabList(Map> customTabsMap); + + EntryEditorPreferences getEntryEditorPreferences(); + + void storeEntryEditorPreferences(EntryEditorPreferences preferences); } diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index 125b07a0806..0f85e9e69f9 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -264,7 +264,7 @@ Dynamically\ group\ entries\ by\ a\ free-form\ search\ expression=Dynamically gr Dynamically\ group\ entries\ by\ searching\ a\ field\ for\ a\ keyword=Dynamically group entries by searching a field for a keyword -Each\ line\ must\ be\ of\ the\ following\ form=Each line must be of the following form +Each\ line\ must\ be\ of\ the\ following\ form\:\ \'tab\:field1;field2;...;fieldN\'.=Each line must be of the following form\: 'tab\:field1;field2;...;fieldN'. Edit=Edit @@ -335,7 +335,8 @@ Field=Field field=field Field\ name=Field name -Field\ names\ are\ not\ allowed\ to\ contain\ white\ space\ or\ the\ following\ characters=Field names are not allowed to contain white space or the following characters + +Field\ names\ are\ not\ allowed\ to\ contain\ white\ spaces\ or\ certain\ characters\ (%0).=Field names are not allowed to contain white spaces or certain characters (%0). Field\ to\ group\ by=Field to group by @@ -839,7 +840,6 @@ Sublibrary\ from\ AUX\ to\ BibTeX=Sublibrary from AUX to BibTeX Switches\ between\ full\ and\ abbreviated\ journal\ name\ if\ the\ journal\ name\ is\ known.=Switches between full and abbreviated journal name if the journal name is known. -Tabname=Tabname Tertiary\ sort\ criterion=Tertiary sort criterion The\ chosen\ encoding\ '%0'\ could\ not\ encode\ the\ following\ characters\:=The chosen encoding '%0' could not encode the following characters: From d74b272660933fa1d7f67e15501047137f859ae9 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Wed, 8 Apr 2020 23:55:55 +0200 Subject: [PATCH 2/6] Extracted calls to JabRefPreferences::get and put out of EntryEditorTabViewModel --- .../GenerateBibtexKeySingleAction.java | 16 +-- .../jabref/gui/entryeditor/EntryEditor.java | 18 ++- .../entryeditor/EntryEditorPreferences.java | 66 +++-------- .../gui/fieldeditors/BibtexKeyEditor.java | 9 +- .../BibtexKeyEditorViewModel.java | 10 +- .../preferences/EntryEditorTabViewModel.java | 84 +++++++------- .../bibtexkeypattern/BibtexKeyGenerator.java | 2 +- .../BibtexKeyPatternPreferences.java | 18 ++- .../jabref/preferences/JabRefPreferences.java | 103 +++++++++++------- .../preferences/PreferencesService.java | 32 +++++- 10 files changed, 198 insertions(+), 160 deletions(-) diff --git a/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java b/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java index 678256e4cee..ac2cedd3d6c 100644 --- a/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java +++ b/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java @@ -4,29 +4,31 @@ import org.jabref.gui.DialogService; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.entryeditor.EntryEditorPreferences; import org.jabref.gui.undo.UndoableKeyChange; import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; +import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; +import org.jabref.preferences.PreferencesService; public class GenerateBibtexKeySingleAction extends SimpleCommand { private DialogService dialogService; private BibDatabaseContext databaseContext; - private EntryEditorPreferences preferences; + private PreferencesService preferencesService; + private BibtexKeyPatternPreferences bibtexKeyPatternPreferences; private BibEntry entry; private UndoManager undoManager; - public GenerateBibtexKeySingleAction(BibEntry entry, BibDatabaseContext databaseContext, DialogService dialogService, EntryEditorPreferences preferences, UndoManager undoManager) { + public GenerateBibtexKeySingleAction(BibEntry entry, BibDatabaseContext databaseContext, DialogService dialogService, PreferencesService preferencesService, UndoManager undoManager) { this.entry = entry; this.databaseContext = databaseContext; this.dialogService = dialogService; - this.preferences = preferences; + this.preferencesService = preferencesService; + this.bibtexKeyPatternPreferences = preferencesService.getBibtexKeyPatternPreferences(); this.undoManager = undoManager; - if (preferences.avoidOverwritingCiteKey()) { - // Only make command executable if cite key is empty + if (bibtexKeyPatternPreferences.avoidOverwritingCiteKey()) { this.executable.bind(entry.getCiteKeyBinding().isNull()); } } @@ -34,7 +36,7 @@ public GenerateBibtexKeySingleAction(BibEntry entry, BibDatabaseContext database @Override public void execute() { if (!entry.hasCiteKey() || GenerateBibtexKeyAction.confirmOverwriteKeys(dialogService)) { - new BibtexKeyGenerator(databaseContext, preferences.getBibtexKeyPatternPreferences()) + new BibtexKeyGenerator(databaseContext, preferencesService.getBibtexKeyPatternPreferences()) .generateAndSetKey(entry) .ifPresent(change -> undoManager.addEdit(new UndoableKeyChange(change))); } diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index b6821df38d0..052d5061691 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -152,7 +152,7 @@ public EntryEditor(BasePanel panel, ExternalFileTypes externalFileTypes) { */ private void setupKeyBindings() { this.addEventHandler(KeyEvent.KEY_PRESSED, event -> { - Optional keyBinding = entryEditorPreferences.getKeyBindings().mapToKeyBinding(event); + Optional keyBinding = Globals.getKeyPrefs().mapToKeyBinding(event); if (keyBinding.isPresent()) { switch (keyBinding.get()) { case ENTRY_EDITOR_NEXT_PANEL: @@ -202,7 +202,7 @@ private void deleteEntry() { @FXML void generateCiteKeyButton() { GenerateBibtexKeySingleAction action = new GenerateBibtexKeySingleAction(getEntry(), databaseContext, - dialogService, entryEditorPreferences, undoManager); + dialogService, preferencesService, undoManager); action.execute(); } @@ -242,9 +242,15 @@ private List createTabs() { entryEditorTabs.add(new RelatedArticlesTab(this, entryEditorPreferences, dialogService)); // Source tab - sourceTab = new SourceTab(databaseContext, undoManager, - entryEditorPreferences.getFieldWriterPreferences(), - entryEditorPreferences.getImportFormatPreferences(), fileMonitor, dialogService, stateManager, Globals.getKeyPrefs()); + sourceTab = new SourceTab( + databaseContext, + undoManager, + preferencesService.getFieldWriterPreferences(), + preferencesService.getImportFormatPreferences(), + fileMonitor, + dialogService, + stateManager, + Globals.getKeyPrefs()); entryEditorTabs.add(sourceTab); // LaTeX citations tab @@ -335,7 +341,7 @@ private void setupToolBar() { // Add menu for fetching bibliographic information ContextMenu fetcherMenu = new ContextMenu(); - for (EntryBasedFetcher fetcher : WebFetchers.getEntryBasedFetchers(entryEditorPreferences.getImportFormatPreferences())) { + for (EntryBasedFetcher fetcher : WebFetchers.getEntryBasedFetchers(preferencesService.getImportFormatPreferences())) { MenuItem fetcherMenuItem = new MenuItem(fetcher.getName()); fetcherMenuItem.setOnAction(event -> fetchAndMerge(fetcher)); fetcherMenu.getItems().add(fetcherMenuItem); diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java index 3caba7af25d..82d2fea3b06 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditorPreferences.java @@ -1,67 +1,43 @@ package org.jabref.gui.entryeditor; -import java.util.List; import java.util.Map; import java.util.Set; -import org.jabref.gui.keyboard.KeyBindingRepository; -import org.jabref.logic.bibtex.FieldWriterPreferences; -import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; -import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.model.entry.field.Field; public class EntryEditorPreferences { private final Map> entryEditorTabList; - private final FieldWriterPreferences fieldWriterPreferences; - private final ImportFormatPreferences importFormatPreferences; - private final BibtexKeyPatternPreferences bibtexKeyPatternPreferences; - private final List customTabFieldNames; + private boolean shouldOpenOnNewEntry; private final boolean shouldShowRecommendationsTab; private final boolean isMrdlibAccepted; - private boolean showSourceTabByDefault; - private final KeyBindingRepository keyBindings; - private boolean avoidOverwritingCiteKey; private final boolean shouldShowLatexCitationsTab; + private boolean showSourceTabByDefault; + private boolean enableValidation; public EntryEditorPreferences(Map> entryEditorTabList, - FieldWriterPreferences fieldWriterPreferences, - ImportFormatPreferences importFormatPreferences, - List customTabFieldNames, + boolean shouldOpenOnNewEntry, boolean shouldShowRecommendationsTab, boolean isMrdlibAccepted, boolean shouldShowLatexCitationsTab, boolean showSourceTabByDefault, - BibtexKeyPatternPreferences bibtexKeyPatternPreferences, - KeyBindingRepository keyBindings, - boolean avoidOverwritingCiteKey) { + boolean enableValidation) { + this.entryEditorTabList = entryEditorTabList; - this.fieldWriterPreferences = fieldWriterPreferences; - this.importFormatPreferences = importFormatPreferences; - this.customTabFieldNames = customTabFieldNames; + this.shouldOpenOnNewEntry = shouldOpenOnNewEntry; this.shouldShowRecommendationsTab = shouldShowRecommendationsTab; this.isMrdlibAccepted = isMrdlibAccepted; - this.showSourceTabByDefault = showSourceTabByDefault; - this.bibtexKeyPatternPreferences = bibtexKeyPatternPreferences; - this.keyBindings = keyBindings; - this.avoidOverwritingCiteKey = avoidOverwritingCiteKey; this.shouldShowLatexCitationsTab = shouldShowLatexCitationsTab; + this.showSourceTabByDefault = showSourceTabByDefault; + this.enableValidation = enableValidation; } public Map> getEntryEditorTabList() { return entryEditorTabList; } - public FieldWriterPreferences getFieldWriterPreferences() { - return fieldWriterPreferences; - } - - public ImportFormatPreferences getImportFormatPreferences() { - return importFormatPreferences; - } - - public List getCustomTabFieldNames() { - return customTabFieldNames; + public boolean shouldOpenOnNewEntry() { + return shouldOpenOnNewEntry; } public boolean shouldShowRecommendationsTab() { @@ -76,27 +52,11 @@ public boolean showSourceTabByDefault() { return showSourceTabByDefault; } - public KeyBindingRepository getKeyBindings() { - return keyBindings; - } - - public BibtexKeyPatternPreferences getBibtexKeyPatternPreferences() { - return bibtexKeyPatternPreferences; - } - - public boolean isShowSourceTabByDefault() { - return showSourceTabByDefault; - } - - public void setShowSourceTabByDefault(boolean showSourceTabByDefault) { - this.showSourceTabByDefault = showSourceTabByDefault; - } - public boolean shouldShowLatexCitationsTab() { return shouldShowLatexCitationsTab; } - public boolean avoidOverwritingCiteKey() { - return avoidOverwritingCiteKey; + public boolean isEnableValidation() { + return enableValidation; } } diff --git a/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditor.java b/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditor.java index 89737ceaa0f..5fea9df913d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditor.java @@ -28,7 +28,14 @@ public class BibtexKeyEditor extends HBox implements FieldEditorFX { public BibtexKeyEditor(Field field, JabRefPreferences preferences, AutoCompleteSuggestionProvider suggestionProvider, FieldCheckers fieldCheckers, BibDatabaseContext databaseContext, UndoManager undoManager, DialogService dialogService) { this.preferences = preferences; - this.viewModel = new BibtexKeyEditorViewModel(field, suggestionProvider, fieldCheckers, preferences.getEntryEditorPreferences(), databaseContext, undoManager, dialogService); + this.viewModel = new BibtexKeyEditorViewModel( + field, + suggestionProvider, + fieldCheckers, + preferences, + databaseContext, + undoManager, + dialogService); ViewLoader.view(this) .root(this) diff --git a/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditorViewModel.java index 4da93f60b87..98f6a784613 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/BibtexKeyEditorViewModel.java @@ -5,28 +5,28 @@ import org.jabref.gui.DialogService; import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider; import org.jabref.gui.bibtexkeypattern.GenerateBibtexKeySingleAction; -import org.jabref.gui.entryeditor.EntryEditorPreferences; import org.jabref.logic.integrity.FieldCheckers; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.field.Field; +import org.jabref.preferences.PreferencesService; import de.saxsys.mvvmfx.utils.commands.Command; public class BibtexKeyEditorViewModel extends AbstractEditorViewModel { - private final EntryEditorPreferences preferences; + private final PreferencesService preferencesService; private final BibDatabaseContext databaseContext; private final UndoManager undoManager; private final DialogService dialogService; - public BibtexKeyEditorViewModel(Field field, AutoCompleteSuggestionProvider suggestionProvider, FieldCheckers fieldCheckers, EntryEditorPreferences preferences, BibDatabaseContext databaseContext, UndoManager undoManager, DialogService dialogService) { + public BibtexKeyEditorViewModel(Field field, AutoCompleteSuggestionProvider suggestionProvider, FieldCheckers fieldCheckers, PreferencesService preferencesService, BibDatabaseContext databaseContext, UndoManager undoManager, DialogService dialogService) { super(field, suggestionProvider, fieldCheckers); - this.preferences = preferences; + this.preferencesService = preferencesService; this.databaseContext = databaseContext; this.undoManager = undoManager; this.dialogService = dialogService; } public Command getGenerateCiteKeyCommand() { - return new GenerateBibtexKeySingleAction(entry, databaseContext, dialogService, preferences, undoManager); + return new GenerateBibtexKeySingleAction(entry, databaseContext, dialogService, preferencesService, undoManager); } } diff --git a/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java b/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java index 6eaa08a10b2..fac807f8902 100644 --- a/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java @@ -11,7 +11,8 @@ import org.jabref.gui.DialogService; import org.jabref.gui.autocompleter.AutoCompleteFirstNameMode; import org.jabref.gui.autocompleter.AutoCompletePreferences; -import org.jabref.preferences.JabRefPreferences; +import org.jabref.gui.entryeditor.EntryEditorPreferences; +import org.jabref.preferences.PreferencesService; import static org.jabref.gui.autocompleter.AutoCompleteFirstNameMode.ONLY_ABBREVIATED; import static org.jabref.gui.autocompleter.AutoCompleteFirstNameMode.ONLY_FULL; @@ -33,37 +34,42 @@ public class EntryEditorTabViewModel implements PreferenceTabViewModel { private final BooleanProperty firstNameModeFullProperty = new SimpleBooleanProperty(); private final BooleanProperty firstNameModeBothProperty = new SimpleBooleanProperty(); - private AutoCompletePreferences autoCompletePreferences; - private final DialogService dialogService; - private final JabRefPreferences preferences; + private final PreferencesService preferencesService; + private final EntryEditorPreferences initialEntryEditorPreferences; + private final AutoCompletePreferences initialAutoCompletePreferences; - public EntryEditorTabViewModel(DialogService dialogService, JabRefPreferences preferences) { + public EntryEditorTabViewModel(DialogService dialogService, PreferencesService preferencesService) { this.dialogService = dialogService; - this.preferences = preferences; - this.autoCompletePreferences = preferences.getAutoCompletePreferences(); + this.preferencesService = preferencesService; + this.initialEntryEditorPreferences = preferencesService.getEntryEditorPreferences(); + this.initialAutoCompletePreferences = preferencesService.getAutoCompletePreferences(); } @Override public void setValues() { - openOnNewEntryProperty.setValue(preferences.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)); - defaultSourceProperty.setValue(preferences.getBoolean(JabRefPreferences.DEFAULT_SHOW_SOURCE)); - enableRelatedArticlesTabProperty.setValue(preferences.getBoolean(JabRefPreferences.SHOW_RECOMMENDATIONS)); - acceptRecommendationsProperty.setValue(preferences.getBoolean(JabRefPreferences.ACCEPT_RECOMMENDATIONS)); - enableLatexCitationsTabProperty.setValue(preferences.getBoolean(JabRefPreferences.SHOW_LATEX_CITATIONS)); - enableValidationProperty.setValue(preferences.getBoolean(JabRefPreferences.VALIDATE_IN_ENTRY_EDITOR)); - enableAutoCompleteProperty.setValue(autoCompletePreferences.shouldAutoComplete()); - autoCompleteFieldsProperty.setValue(autoCompletePreferences.getCompleteNamesAsString()); - - if (autoCompletePreferences.getOnlyCompleteFirstLast()) { + // ToDo: Include EntryEditorFieldsEditor in PreferencesDialog + // unused: initialEntryEditorPreferences.getEntryEditorTabList(); + + openOnNewEntryProperty.setValue(initialEntryEditorPreferences.shouldOpenOnNewEntry()); + defaultSourceProperty.setValue(initialEntryEditorPreferences.showSourceTabByDefault()); + enableRelatedArticlesTabProperty.setValue(initialEntryEditorPreferences.shouldShowRecommendationsTab()); + acceptRecommendationsProperty.setValue(initialEntryEditorPreferences.isMrdlibAccepted()); + enableLatexCitationsTabProperty.setValue(initialEntryEditorPreferences.shouldShowLatexCitationsTab()); + enableValidationProperty.setValue(initialEntryEditorPreferences.isEnableValidation()); + + enableAutoCompleteProperty.setValue(initialAutoCompletePreferences.shouldAutoComplete()); + autoCompleteFieldsProperty.setValue(initialAutoCompletePreferences.getCompleteNamesAsString()); + + if (initialAutoCompletePreferences.getOnlyCompleteFirstLast()) { autoCompleteFirstLastProperty.setValue(true); - } else if (autoCompletePreferences.getOnlyCompleteLastFirst()) { + } else if (initialAutoCompletePreferences.getOnlyCompleteLastFirst()) { autoCompleteLastFirstProperty.setValue(true); } else { autoCompleteBothProperty.setValue(true); } - switch (autoCompletePreferences.getFirstNameMode()) { + switch (initialAutoCompletePreferences.getFirstNameMode()) { case ONLY_ABBREVIATED: firstNameModeAbbreviatedProperty.setValue(true); break; @@ -78,37 +84,39 @@ public void setValues() { @Override public void storeSettings() { - preferences.putBoolean(JabRefPreferences.AUTO_OPEN_FORM, openOnNewEntryProperty.getValue()); - preferences.putBoolean(JabRefPreferences.DEFAULT_SHOW_SOURCE, defaultSourceProperty.getValue()); - preferences.putBoolean(JabRefPreferences.SHOW_RECOMMENDATIONS, enableRelatedArticlesTabProperty.getValue()); - preferences.putBoolean(JabRefPreferences.ACCEPT_RECOMMENDATIONS, acceptRecommendationsProperty.getValue()); - preferences.putBoolean(JabRefPreferences.SHOW_LATEX_CITATIONS, enableLatexCitationsTabProperty.getValue()); - preferences.putBoolean(JabRefPreferences.VALIDATE_IN_ENTRY_EDITOR, enableValidationProperty.getValue()); - - autoCompletePreferences.setShouldAutoComplete(enableAutoCompleteProperty.getValue()); - autoCompletePreferences.setCompleteNames(autoCompleteFieldsProperty.getValue()); + preferencesService.storeEntryEditorPreferences(new EntryEditorPreferences( + initialEntryEditorPreferences.getEntryEditorTabList(), + openOnNewEntryProperty.getValue(), + enableRelatedArticlesTabProperty.getValue(), + acceptRecommendationsProperty.getValue(), + enableLatexCitationsTabProperty.getValue(), + defaultSourceProperty.getValue(), + enableValidationProperty.getValue())); + + initialAutoCompletePreferences.setShouldAutoComplete(enableAutoCompleteProperty.getValue()); + initialAutoCompletePreferences.setCompleteNames(autoCompleteFieldsProperty.getValue()); if (autoCompleteBothProperty.getValue()) { - autoCompletePreferences.setOnlyCompleteFirstLast(false); - autoCompletePreferences.setOnlyCompleteLastFirst(false); + initialAutoCompletePreferences.setOnlyCompleteFirstLast(false); + initialAutoCompletePreferences.setOnlyCompleteLastFirst(false); } else if (autoCompleteFirstLastProperty.getValue()) { - autoCompletePreferences.setOnlyCompleteFirstLast(true); - autoCompletePreferences.setOnlyCompleteLastFirst(false); + initialAutoCompletePreferences.setOnlyCompleteFirstLast(true); + initialAutoCompletePreferences.setOnlyCompleteLastFirst(false); } else { - autoCompletePreferences.setOnlyCompleteFirstLast(false); - autoCompletePreferences.setOnlyCompleteLastFirst(true); + initialAutoCompletePreferences.setOnlyCompleteFirstLast(false); + initialAutoCompletePreferences.setOnlyCompleteLastFirst(true); } if (firstNameModeAbbreviatedProperty.getValue()) { - autoCompletePreferences.setFirstNameMode(ONLY_ABBREVIATED); + initialAutoCompletePreferences.setFirstNameMode(ONLY_ABBREVIATED); } else if (firstNameModeFullProperty.getValue()) { - autoCompletePreferences.setFirstNameMode(ONLY_FULL); + initialAutoCompletePreferences.setFirstNameMode(ONLY_FULL); } else { - autoCompletePreferences.setFirstNameMode(AutoCompleteFirstNameMode.BOTH); + initialAutoCompletePreferences.setFirstNameMode(AutoCompleteFirstNameMode.BOTH); } - preferences.storeAutoCompletePreferences(autoCompletePreferences); + preferencesService.storeAutoCompletePreferences(initialAutoCompletePreferences); } @Override diff --git a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java index 8a03e9d0966..9ef88afc4c9 100644 --- a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java @@ -52,7 +52,7 @@ static String generateKey(BibEntry entry, String pattern) { static String generateKey(BibEntry entry, String pattern, BibDatabase database) { GlobalBibtexKeyPattern keyPattern = new GlobalBibtexKeyPattern(Collections.emptyList()); keyPattern.setDefaultValue("[" + pattern + "]"); - return new BibtexKeyGenerator(keyPattern, database, new BibtexKeyPatternPreferences("", "", false, true, true, keyPattern, ',')) + return new BibtexKeyGenerator(keyPattern, database, new BibtexKeyPatternPreferences("", "", false, true, true, keyPattern, ',', false)) .generateKey(entry); } diff --git a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternPreferences.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternPreferences.java index 64d0137a74e..e67e234e931 100644 --- a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternPreferences.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternPreferences.java @@ -11,10 +11,17 @@ public class BibtexKeyPatternPreferences { private final boolean enforceLegalKey; private final GlobalBibtexKeyPattern keyPattern; private Character keywordDelimiter; + private boolean avoidOverwritingCiteKey; + + public BibtexKeyPatternPreferences(String keyPatternRegex, + String keyPatternReplacement, + boolean alwaysAddLetter, + boolean firstLetterA, + boolean enforceLegalKey, + GlobalBibtexKeyPattern keyPattern, + Character keywordDelimiter, + boolean avoidOverwritingCiteKey) { - public BibtexKeyPatternPreferences(String keyPatternRegex, String keyPatternReplacement, boolean alwaysAddLetter, - boolean firstLetterA, boolean enforceLegalKey, GlobalBibtexKeyPattern keyPattern, - Character keywordDelimiter) { this.keyPatternRegex = keyPatternRegex; this.keyPatternReplacement = keyPatternReplacement; this.alwaysAddLetter = alwaysAddLetter; @@ -22,6 +29,7 @@ public BibtexKeyPatternPreferences(String keyPatternRegex, String keyPatternRepl this.enforceLegalKey = enforceLegalKey; this.keyPattern = keyPattern; this.keywordDelimiter = keywordDelimiter; + this.avoidOverwritingCiteKey = avoidOverwritingCiteKey; } public String getKeyPatternRegex() { @@ -51,4 +59,8 @@ public GlobalBibtexKeyPattern getKeyPattern() { public Character getKeywordDelimiter() { return keywordDelimiter; } + + public boolean avoidOverwritingCiteKey() { + return avoidOverwritingCiteKey; + } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index e25ce5132ec..35d6a4baa1d 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1123,8 +1123,7 @@ private Object getObject(String key) { * * @param number or higher. */ - @Override - public void purgeSeries(String prefix, int number) { + private void purgeSeries(String prefix, int number) { int n = number; while (get(prefix + n) != null) { remove(prefix + n); @@ -1207,6 +1206,14 @@ public FilePreferences getFilePreferences() { get(IMPORT_FILEDIRPATTERN)); } + @Override + public FieldWriterPreferences getFieldWriterPreferences() { + return new FieldWriterPreferences( + getBoolean(RESOLVE_STRINGS_ALL_FIELDS), + getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), + getFieldContentParserPreferences()); + } + public FieldContentFormatterPreferences getFieldContentParserPreferences() { return new FieldContentFormatterPreferences(getStringList(NON_WRAPPABLE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toList())); } @@ -1219,9 +1226,14 @@ public boolean isKeywordSyncEnabled() { @Override public ImportFormatPreferences getImportFormatPreferences() { - return new ImportFormatPreferences(customImports, getDefaultEncoding(), getKeywordDelimiter(), - getBibtexKeyPatternPreferences(), getFieldContentParserPreferences(), getXMPPreferences(), - isKeywordSyncEnabled()); + return new ImportFormatPreferences( + customImports, + getDefaultEncoding(), + getKeywordDelimiter(), + getBibtexKeyPatternPreferences(), + getFieldContentParserPreferences(), + getXMPPreferences(), + isKeywordSyncEnabled()); } @Override @@ -1270,15 +1282,17 @@ public ExporterFactory getExporterFactory(JournalAbbreviationLoader abbreviation return ExporterFactory.create(customFormats, layoutPreferences, savePreferences, xmpPreferences); } + @Override public BibtexKeyPatternPreferences getBibtexKeyPatternPreferences() { return new BibtexKeyPatternPreferences( - get(KEY_PATTERN_REGEX), - get(KEY_PATTERN_REPLACEMENT), - getBoolean(KEY_GEN_ALWAYS_ADD_LETTER), - getBoolean(KEY_GEN_FIRST_LETTER_A), - getBoolean(ENFORCE_LEGAL_BIBTEX_KEY), - getKeyPattern(), - getKeywordDelimiter()); + get(KEY_PATTERN_REGEX), + get(KEY_PATTERN_REPLACEMENT), + getBoolean(KEY_GEN_ALWAYS_ADD_LETTER), + getBoolean(KEY_GEN_FIRST_LETTER_A), + getBoolean(ENFORCE_LEGAL_BIBTEX_KEY), + getKeyPattern(), + getKeywordDelimiter(), + getBoolean(AVOID_OVERWRITING_KEY)); } @Override @@ -1562,24 +1576,6 @@ public AutoLinkPreferences getAutoLinkPreferences() { getKeywordDelimiter()); } - public AutoCompletePreferences getAutoCompletePreferences() { - return new AutoCompletePreferences( - getBoolean(AUTO_COMPLETE), - AutoCompleteFirstNameMode.parse(get(AUTOCOMPLETER_FIRSTNAME_MODE)), - getBoolean(AUTOCOMPLETER_LAST_FIRST), - getBoolean(AUTOCOMPLETER_FIRST_LAST), - getStringList(AUTOCOMPLETER_COMPLETE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toSet()), - getJournalAbbreviationPreferences()); - } - - public void storeAutoCompletePreferences(AutoCompletePreferences autoCompletePreferences) { - putBoolean(AUTO_COMPLETE, autoCompletePreferences.shouldAutoComplete()); - put(AUTOCOMPLETER_FIRSTNAME_MODE, autoCompletePreferences.getFirstNameMode().name()); - putBoolean(AUTOCOMPLETER_LAST_FIRST, autoCompletePreferences.getOnlyCompleteLastFirst()); - putBoolean(AUTOCOMPLETER_FIRST_LAST, autoCompletePreferences.getOnlyCompleteFirstLast()); - putStringList(AUTOCOMPLETER_COMPLETE_FIELDS, autoCompletePreferences.getCompleteFields().stream().map(Field::getName).collect(Collectors.toList())); - } - public void storeSidePanePreferredPositions(Map preferredPositions) { // Split the map into a pair of parallel String lists suitable for storage List names = preferredPositions.keySet().stream() @@ -1967,6 +1963,10 @@ public void storeTimestampPreferences(TimestampPreferences preferences) { putBoolean(OVERWRITE_TIME_STAMP, preferences.isOverwriteTimestamp()); } + //************************************************************************************************************* + // ToDo: GroupPreferences + //************************************************************************************************************* + @Override public boolean getDisplayGroupCount() { return getBoolean(JabRefPreferences.DISPLAY_GROUP_COUNT); @@ -2092,27 +2092,46 @@ public List getAllDefaultTabFieldNames() { @Override public EntryEditorPreferences getEntryEditorPreferences() { return new EntryEditorPreferences(getEntryEditorTabList(), - getFieldWriterPreferences(), - getImportFormatPreferences(), - getAllDefaultTabFieldNames(), + getBoolean(AUTO_OPEN_FORM), getBoolean(SHOW_RECOMMENDATIONS), getBoolean(ACCEPT_RECOMMENDATIONS), getBoolean(SHOW_LATEX_CITATIONS), getBoolean(DEFAULT_SHOW_SOURCE), - getBibtexKeyPatternPreferences(), - Globals.getKeyPrefs(), - getBoolean(AVOID_OVERWRITING_KEY)); + getBoolean(VALIDATE_IN_ENTRY_EDITOR)); } @Override public void storeEntryEditorPreferences(EntryEditorPreferences preferences) { - // ToDo + storeEntryEditorTabList(preferences.getEntryEditorTabList()); + putBoolean(AUTO_OPEN_FORM, preferences.shouldOpenOnNewEntry()); + putBoolean(SHOW_RECOMMENDATIONS, preferences.shouldShowRecommendationsTab()); + putBoolean(ACCEPT_RECOMMENDATIONS, preferences.isMrdlibAccepted()); + putBoolean(SHOW_LATEX_CITATIONS, preferences.shouldShowLatexCitationsTab()); + putBoolean(DEFAULT_SHOW_SOURCE, preferences.showSourceTabByDefault()); + putBoolean(VALIDATE_IN_ENTRY_EDITOR, preferences.isEnableValidation()); } - public FieldWriterPreferences getFieldWriterPreferences() { - return new FieldWriterPreferences( - getBoolean(RESOLVE_STRINGS_ALL_FIELDS), - getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), - getFieldContentParserPreferences()); + //************************************************************************************************************* + // ToDo: Misc preferences + //************************************************************************************************************* + + @Override + public AutoCompletePreferences getAutoCompletePreferences() { + return new AutoCompletePreferences( + getBoolean(AUTO_COMPLETE), + AutoCompleteFirstNameMode.parse(get(AUTOCOMPLETER_FIRSTNAME_MODE)), + getBoolean(AUTOCOMPLETER_LAST_FIRST), + getBoolean(AUTOCOMPLETER_FIRST_LAST), + getStringList(AUTOCOMPLETER_COMPLETE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toSet()), + getJournalAbbreviationPreferences()); + } + + @Override + public void storeAutoCompletePreferences(AutoCompletePreferences autoCompletePreferences) { + putBoolean(AUTO_COMPLETE, autoCompletePreferences.shouldAutoComplete()); + put(AUTOCOMPLETER_FIRSTNAME_MODE, autoCompletePreferences.getFirstNameMode().name()); + putBoolean(AUTOCOMPLETER_LAST_FIRST, autoCompletePreferences.getOnlyCompleteLastFirst()); + putBoolean(AUTOCOMPLETER_FIRST_LAST, autoCompletePreferences.getOnlyCompleteFirstLast()); + putStringList(AUTOCOMPLETER_COMPLETE_FIELDS, autoCompletePreferences.getCompleteFields().stream().map(Field::getName).collect(Collectors.toList())); } } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 464ce598b9d..f8b272dd6b3 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -6,8 +6,12 @@ import java.util.Map; import java.util.Set; +import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.entryeditor.EntryEditorPreferences; import org.jabref.gui.keyboard.KeyBindingRepository; +import org.jabref.logic.bibtex.FieldContentFormatterPreferences; +import org.jabref.logic.bibtex.FieldWriterPreferences; +import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.logic.cleanup.CleanupPreferences; import org.jabref.logic.cleanup.CleanupPreset; import org.jabref.logic.exporter.SavePreferences; @@ -45,6 +49,10 @@ public interface PreferencesService { FilePreferences getFilePreferences(); + FieldWriterPreferences getFieldWriterPreferences(); + + FieldContentFormatterPreferences getFieldContentParserPreferences(); + XmpPreferences getXMPPreferences(); AutoLinkPreferences getAutoLinkPreferences(); @@ -59,12 +67,12 @@ public interface PreferencesService { PreviewPreferences getPreviewPreferences(); - void purgeSeries(String prefix, int number); - List getCustomExportFormats(JournalAbbreviationLoader loader); void storeCustomExportFormats(List exporters); + BibtexKeyPatternPreferences getBibtexKeyPatternPreferences(); + LayoutFormatterPreferences getLayoutFormatterPreferences(JournalAbbreviationLoader loader); ImportFormatPreferences getImportFormatPreferences(); @@ -101,7 +109,9 @@ public interface PreferencesService { void setCleanupPreset(CleanupPreset cleanupPreset); - // General + //************************************************************************************************************* + // GeneralPreferences + //************************************************************************************************************* Language getLanguage(); @@ -131,9 +141,15 @@ public interface PreferencesService { void storeTimestampPreferences(TimestampPreferences preferences); + //************************************************************************************************************* + // ToDo: GroupPreferences + //************************************************************************************************************* + boolean getDisplayGroupCount(); - // Entry Editor + //************************************************************************************************************* + // EntryEditorPreferences + //************************************************************************************************************* Map> getEntryEditorTabList(); @@ -148,4 +164,12 @@ public interface PreferencesService { EntryEditorPreferences getEntryEditorPreferences(); void storeEntryEditorPreferences(EntryEditorPreferences preferences); + + //************************************************************************************************************* + // ToDo: Misc preferences + //************************************************************************************************************* + + AutoCompletePreferences getAutoCompletePreferences(); + + void storeAutoCompletePreferences(AutoCompletePreferences autoCompletePreferences); } From 6af3ddd12ae0b66b121473ae47ededfd4c3921ca Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Thu, 9 Apr 2020 00:07:24 +0200 Subject: [PATCH 3/6] Fixed tests --- .../bibtexkeypattern/MakeLabelWithDatabaseTest.java | 12 ++++++------ .../jabref/logic/integrity/IntegrityCheckTest.java | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java b/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java index 584f14d05f1..78eab56427f 100644 --- a/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java +++ b/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithDatabaseTest.java @@ -32,7 +32,7 @@ void setUp() { database.insertEntry(entry); pattern = GlobalBibtexKeyPattern.fromPattern("[auth][year]"); bibtexKeyPattern = new DatabaseBibtexKeyPattern(pattern); - preferences = new BibtexKeyPatternPreferences("", "", false, true, true, pattern, ','); + preferences = new BibtexKeyPatternPreferences("", "", false, true, true, pattern, ',', false); } @Test @@ -54,14 +54,14 @@ void generateDefaultKeyAlreadyExistsDuplicatesStartAtA() { @Test void generateDefaultKeyAlwaysLetter() { - preferences = new BibtexKeyPatternPreferences("", "", true, true, true, pattern, ','); + preferences = new BibtexKeyPatternPreferences("", "", true, true, true, pattern, ',', false); new BibtexKeyGenerator(bibtexKeyPattern, database, preferences).generateAndSetKey(entry); assertEquals(Optional.of("Doe2016a"), entry.getCiteKeyOptional()); } @Test void generateDefaultKeyAlwaysLetterAlreadyExistsDuplicatesStartAtB() { - preferences = new BibtexKeyPatternPreferences("", "", true, true, true, pattern, ','); + preferences = new BibtexKeyPatternPreferences("", "", true, true, true, pattern, ',', false); BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibtexKeyPattern, database, preferences); keyGenerator.generateAndSetKey(entry); @@ -74,14 +74,14 @@ void generateDefaultKeyAlwaysLetterAlreadyExistsDuplicatesStartAtB() { @Test void generateDefaultKeyStartDuplicatesAtB() { - preferences = new BibtexKeyPatternPreferences("", "", false, false, true, pattern, ','); + preferences = new BibtexKeyPatternPreferences("", "", false, false, true, pattern, ',', false); new BibtexKeyGenerator(bibtexKeyPattern, database, preferences).generateAndSetKey(entry); assertEquals(Optional.of("Doe2016"), entry.getCiteKeyOptional()); } @Test void generateDefaultKeyAlreadyExistsDuplicatesStartAtB() { - preferences = new BibtexKeyPatternPreferences("", "", false, false, true, pattern, ','); + preferences = new BibtexKeyPatternPreferences("", "", false, false, true, pattern, ',', false); BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibtexKeyPattern, database, preferences); keyGenerator.generateAndSetKey(entry); @@ -370,7 +370,7 @@ void generateKeyAuthNMLargeMReallyReturnsEmptyString() { @Test void generateKeyRegExReplace() { - preferences = new BibtexKeyPatternPreferences("2", "3", false, true, true, pattern, ','); + preferences = new BibtexKeyPatternPreferences("2", "3", false, true, true, pattern, ',', false); bibtexKeyPattern.setDefaultValue("[auth][year]"); entry.setField(StandardField.AUTHOR, "John Doe and Donald Smith and Will Wonder"); new BibtexKeyGenerator(bibtexKeyPattern, database, preferences).generateAndSetKey(entry); diff --git a/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java b/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java index 6f2a4a62009..7b0c88c41d9 100644 --- a/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java +++ b/src/test/java/org/jabref/logic/integrity/IntegrityCheckTest.java @@ -441,7 +441,8 @@ private BibtexKeyPatternPreferences createBibtexKeyPatternPreferences() { false, false, keyPattern, - ','); + ',', + false); } private BibDatabaseContext withMode(BibDatabaseContext context, BibDatabaseMode mode) { From 6d0a90af6e623bd02f0ffee5306a79e90838aebf Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Fri, 10 Apr 2020 21:16:58 +0200 Subject: [PATCH 4/6] Refactored AutoCompletePreferences --- .../AutoCompletePreferences.java | 44 ++++--------------- .../PersonNameStringConverter.java | 23 ++++++---- .../preferences/EntryEditorTabViewModel.java | 42 ++++++++---------- .../jabref/preferences/JabRefPreferences.java | 31 +++++++++---- 4 files changed, 64 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java b/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java index bd2088e6a3c..a54fb475b82 100644 --- a/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java +++ b/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java @@ -8,26 +8,24 @@ public class AutoCompletePreferences { + public enum onlyCompleteNameFormat { + LAST_FIRST, FIRST_LAST, BOTH + } + private boolean shouldAutoComplete; private AutoCompleteFirstNameMode firstNameMode; - private boolean onlyCompleteLastFirst; - private boolean onlyCompleteFirstLast; + private AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat; private Set completeFields; private final JournalAbbreviationPreferences journalAbbreviationPreferences; - public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, boolean onlyCompleteLastFirst, boolean onlyCompleteFirstLast, Set completeFields, JournalAbbreviationPreferences journalAbbreviationPreferences) { + public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat, Set completeFields, JournalAbbreviationPreferences journalAbbreviationPreferences) { this.shouldAutoComplete = shouldAutoComplete; this.firstNameMode = firstNameMode; - this.onlyCompleteLastFirst = onlyCompleteLastFirst; - this.onlyCompleteFirstLast = onlyCompleteFirstLast; + this.onlyCompleteNameFormat = onlyCompleteNameFormat; this.completeFields = completeFields; this.journalAbbreviationPreferences = journalAbbreviationPreferences; } - public void setShouldAutoComplete(boolean shouldAutoComplete) { - this.shouldAutoComplete = shouldAutoComplete; - } - public boolean shouldAutoComplete() { return shouldAutoComplete; } @@ -39,25 +37,7 @@ public AutoCompleteFirstNameMode getFirstNameMode() { return firstNameMode; } - public void setFirstNameMode(AutoCompleteFirstNameMode firstNameMode) { - this.firstNameMode = firstNameMode; - } - - public boolean getOnlyCompleteLastFirst() { - return onlyCompleteLastFirst; - } - - public void setOnlyCompleteLastFirst(boolean onlyCompleteLastFirst) { - this.onlyCompleteLastFirst = onlyCompleteLastFirst; - } - - public boolean getOnlyCompleteFirstLast() { - return onlyCompleteFirstLast; - } - - public void setOnlyCompleteFirstLast(boolean onlyCompleteFirstLast) { - this.onlyCompleteFirstLast = onlyCompleteFirstLast; - } + public AutoCompletePreferences.onlyCompleteNameFormat getOnlyCompleteNameFormat() { return onlyCompleteNameFormat; } /** * Returns the list of fields for which autocomplete is enabled @@ -67,14 +47,6 @@ public Set getCompleteFields() { return completeFields; } - public void setCompleteFields(Set completeFields) { - this.completeFields = completeFields; - } - - public void setCompleteNames(String input) { - setCompleteFields(FieldFactory.parseFieldList(input)); - } - public String getCompleteNamesAsString() { return FieldFactory.serializeFieldsList(completeFields); } diff --git a/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java b/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java index a37ed5c9b87..65f5ecb2a2d 100644 --- a/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java +++ b/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java @@ -18,15 +18,20 @@ public PersonNameStringConverter(boolean autoCompFF, boolean autoCompLF, AutoCom } public PersonNameStringConverter(AutoCompletePreferences preferences) { - if (preferences.getOnlyCompleteFirstLast()) { - autoCompFF = true; - autoCompLF = false; - } else if (preferences.getOnlyCompleteLastFirst()) { - autoCompFF = false; - autoCompLF = true; - } else { - autoCompFF = true; - autoCompLF = true; + switch (preferences.getOnlyCompleteNameFormat()) { + case FIRST_LAST: + autoCompFF = true; + autoCompLF = false; + break; + case LAST_FIRST: + autoCompFF = false; + autoCompLF = true; + break; + default: + case BOTH: + autoCompFF = true; + autoCompLF = true; + break; } autoCompleteFirstNameMode = preferences.getFirstNameMode(); diff --git a/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java b/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java index fac807f8902..88b3652a4a2 100644 --- a/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java @@ -12,11 +12,9 @@ import org.jabref.gui.autocompleter.AutoCompleteFirstNameMode; import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.entryeditor.EntryEditorPreferences; +import org.jabref.model.entry.field.FieldFactory; import org.jabref.preferences.PreferencesService; -import static org.jabref.gui.autocompleter.AutoCompleteFirstNameMode.ONLY_ABBREVIATED; -import static org.jabref.gui.autocompleter.AutoCompleteFirstNameMode.ONLY_FULL; - public class EntryEditorTabViewModel implements PreferenceTabViewModel { private final BooleanProperty openOnNewEntryProperty = new SimpleBooleanProperty(); @@ -61,9 +59,9 @@ public void setValues() { enableAutoCompleteProperty.setValue(initialAutoCompletePreferences.shouldAutoComplete()); autoCompleteFieldsProperty.setValue(initialAutoCompletePreferences.getCompleteNamesAsString()); - if (initialAutoCompletePreferences.getOnlyCompleteFirstLast()) { + if (initialAutoCompletePreferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.FIRST_LAST) { autoCompleteFirstLastProperty.setValue(true); - } else if (initialAutoCompletePreferences.getOnlyCompleteLastFirst()) { + } else if (initialAutoCompletePreferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST) { autoCompleteLastFirstProperty.setValue(true); } else { autoCompleteBothProperty.setValue(true); @@ -93,30 +91,28 @@ public void storeSettings() { defaultSourceProperty.getValue(), enableValidationProperty.getValue())); - initialAutoCompletePreferences.setShouldAutoComplete(enableAutoCompleteProperty.getValue()); - initialAutoCompletePreferences.setCompleteNames(autoCompleteFieldsProperty.getValue()); - if (autoCompleteBothProperty.getValue()) { - initialAutoCompletePreferences.setOnlyCompleteFirstLast(false); - initialAutoCompletePreferences.setOnlyCompleteLastFirst(false); - } - else if (autoCompleteFirstLastProperty.getValue()) { - initialAutoCompletePreferences.setOnlyCompleteFirstLast(true); - initialAutoCompletePreferences.setOnlyCompleteLastFirst(false); - } - else { - initialAutoCompletePreferences.setOnlyCompleteFirstLast(false); - initialAutoCompletePreferences.setOnlyCompleteLastFirst(true); + // default + AutoCompletePreferences.onlyCompleteNameFormat nameFormat = AutoCompletePreferences.onlyCompleteNameFormat.BOTH; + if (autoCompleteFirstLastProperty.getValue()) { + nameFormat = AutoCompletePreferences.onlyCompleteNameFormat.FIRST_LAST; + } else if (autoCompleteLastFirstProperty.getValue()) { + nameFormat = AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST; } + // default: AutoCompleteFirstNameMode.BOTH + AutoCompleteFirstNameMode firstNameMode = AutoCompleteFirstNameMode.BOTH; if (firstNameModeAbbreviatedProperty.getValue()) { - initialAutoCompletePreferences.setFirstNameMode(ONLY_ABBREVIATED); + firstNameMode = AutoCompleteFirstNameMode.ONLY_ABBREVIATED; } else if (firstNameModeFullProperty.getValue()) { - initialAutoCompletePreferences.setFirstNameMode(ONLY_FULL); - } else { - initialAutoCompletePreferences.setFirstNameMode(AutoCompleteFirstNameMode.BOTH); + firstNameMode = AutoCompleteFirstNameMode.ONLY_FULL; } - preferencesService.storeAutoCompletePreferences(initialAutoCompletePreferences); + preferencesService.storeAutoCompletePreferences(new AutoCompletePreferences( + enableAutoCompleteProperty.getValue(), + firstNameMode, + nameFormat, + FieldFactory.parseFieldList(autoCompleteFieldsProperty.getValue()), + preferencesService.getJournalAbbreviationPreferences())); } @Override diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 35d6a4baa1d..02719701136 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2117,21 +2117,36 @@ public void storeEntryEditorPreferences(EntryEditorPreferences preferences) { @Override public AutoCompletePreferences getAutoCompletePreferences() { + AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat = AutoCompletePreferences.onlyCompleteNameFormat.BOTH; + if (getBoolean(AUTOCOMPLETER_LAST_FIRST)) { + onlyCompleteNameFormat = AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST; + } else if (getBoolean(AUTOCOMPLETER_FIRST_LAST)) { + onlyCompleteNameFormat = AutoCompletePreferences.onlyCompleteNameFormat.FIRST_LAST; + } + return new AutoCompletePreferences( getBoolean(AUTO_COMPLETE), AutoCompleteFirstNameMode.parse(get(AUTOCOMPLETER_FIRSTNAME_MODE)), - getBoolean(AUTOCOMPLETER_LAST_FIRST), - getBoolean(AUTOCOMPLETER_FIRST_LAST), + onlyCompleteNameFormat, getStringList(AUTOCOMPLETER_COMPLETE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toSet()), getJournalAbbreviationPreferences()); } @Override - public void storeAutoCompletePreferences(AutoCompletePreferences autoCompletePreferences) { - putBoolean(AUTO_COMPLETE, autoCompletePreferences.shouldAutoComplete()); - put(AUTOCOMPLETER_FIRSTNAME_MODE, autoCompletePreferences.getFirstNameMode().name()); - putBoolean(AUTOCOMPLETER_LAST_FIRST, autoCompletePreferences.getOnlyCompleteLastFirst()); - putBoolean(AUTOCOMPLETER_FIRST_LAST, autoCompletePreferences.getOnlyCompleteFirstLast()); - putStringList(AUTOCOMPLETER_COMPLETE_FIELDS, autoCompletePreferences.getCompleteFields().stream().map(Field::getName).collect(Collectors.toList())); + public void storeAutoCompletePreferences(AutoCompletePreferences preferences) { + putBoolean(AUTO_COMPLETE, preferences.shouldAutoComplete()); + put(AUTOCOMPLETER_FIRSTNAME_MODE, preferences.getFirstNameMode().name()); + putStringList(AUTOCOMPLETER_COMPLETE_FIELDS, preferences.getCompleteFields().stream().map(Field::getName).collect(Collectors.toList())); + + if (preferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.BOTH) { + putBoolean(AUTOCOMPLETER_LAST_FIRST, false); + putBoolean(AUTOCOMPLETER_FIRST_LAST, false); + } else if (preferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST) { + putBoolean(AUTOCOMPLETER_LAST_FIRST, true); + putBoolean(AUTOCOMPLETER_FIRST_LAST, false); + } else { + putBoolean(AUTOCOMPLETER_LAST_FIRST, false); + putBoolean(AUTOCOMPLETER_FIRST_LAST, true); + } } } From 647a781d25048dbc26308c075cb04c20f58f02c7 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Fri, 17 Apr 2020 18:19:14 +0200 Subject: [PATCH 5/6] Checkstyle and missing comment --- src/main/java/org/jabref/preferences/JabRefPreferences.java | 2 +- src/main/java/org/jabref/preferences/PreferencesService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index e89a5400e66..60069a765e6 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1174,7 +1174,7 @@ public void importPreferences(Path file) throws JabRefException { * * Do not use in production code. Otherwise the singleton pattern is broken and preferences might get lost. * - * @param owPrefs + * @param owPrefs The custom preferences to overwrite the currently present */ public void overwritePreferences(JabRefPreferences owPrefs) { singleton = owPrefs; diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index f8b272dd6b3..93ec72ac594 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -159,7 +159,7 @@ public interface PreferencesService { List getAllDefaultTabFieldNames(); - void storeEntryEditorTabList(Map> customTabsMap); + void storeEntryEditorTabList(Map> customTabsMap); EntryEditorPreferences getEntryEditorPreferences(); From 36d675cba95c692a35508636ecc491cbedc1437a Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Fri, 17 Apr 2020 19:24:11 +0200 Subject: [PATCH 6/6] Rewording and small refactorings regarding discussion --- .../autocompleter/AutoCompletePreferences.java | 16 ++++++++-------- .../autocompleter/PersonNameStringConverter.java | 2 +- .../GenerateBibtexKeySingleAction.java | 15 ++++++--------- .../CustomizeGeneralFieldsDialogViewModel.java | 1 - .../gui/preferences/EntryEditorTabViewModel.java | 14 +++++++------- .../jabref/preferences/JabRefPreferences.java | 14 ++++++++------ 6 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java b/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java index a54fb475b82..99ba956eb46 100644 --- a/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java +++ b/src/main/java/org/jabref/gui/autocompleter/AutoCompletePreferences.java @@ -8,20 +8,20 @@ public class AutoCompletePreferences { - public enum onlyCompleteNameFormat { + public enum NameFormat { LAST_FIRST, FIRST_LAST, BOTH } - private boolean shouldAutoComplete; - private AutoCompleteFirstNameMode firstNameMode; - private AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat; - private Set completeFields; + private final boolean shouldAutoComplete; + private final AutoCompleteFirstNameMode firstNameMode; + private final NameFormat nameFormat; + private final Set completeFields; private final JournalAbbreviationPreferences journalAbbreviationPreferences; - public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat, Set completeFields, JournalAbbreviationPreferences journalAbbreviationPreferences) { + public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, NameFormat nameFormat, Set completeFields, JournalAbbreviationPreferences journalAbbreviationPreferences) { this.shouldAutoComplete = shouldAutoComplete; this.firstNameMode = firstNameMode; - this.onlyCompleteNameFormat = onlyCompleteNameFormat; + this.nameFormat = nameFormat; this.completeFields = completeFields; this.journalAbbreviationPreferences = journalAbbreviationPreferences; } @@ -37,7 +37,7 @@ public AutoCompleteFirstNameMode getFirstNameMode() { return firstNameMode; } - public AutoCompletePreferences.onlyCompleteNameFormat getOnlyCompleteNameFormat() { return onlyCompleteNameFormat; } + public NameFormat getNameFormat() { return nameFormat; } /** * Returns the list of fields for which autocomplete is enabled diff --git a/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java b/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java index 65f5ecb2a2d..04d085fcb1b 100644 --- a/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java +++ b/src/main/java/org/jabref/gui/autocompleter/PersonNameStringConverter.java @@ -18,7 +18,7 @@ public PersonNameStringConverter(boolean autoCompFF, boolean autoCompLF, AutoCom } public PersonNameStringConverter(AutoCompletePreferences preferences) { - switch (preferences.getOnlyCompleteNameFormat()) { + switch (preferences.getNameFormat()) { case FIRST_LAST: autoCompFF = true; autoCompLF = false; diff --git a/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java b/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java index ac2cedd3d6c..90789e32990 100644 --- a/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java +++ b/src/main/java/org/jabref/gui/bibtexkeypattern/GenerateBibtexKeySingleAction.java @@ -6,29 +6,26 @@ import org.jabref.gui.actions.SimpleCommand; import org.jabref.gui.undo.UndoableKeyChange; import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; -import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.PreferencesService; public class GenerateBibtexKeySingleAction extends SimpleCommand { - private DialogService dialogService; - private BibDatabaseContext databaseContext; - private PreferencesService preferencesService; - private BibtexKeyPatternPreferences bibtexKeyPatternPreferences; - private BibEntry entry; - private UndoManager undoManager; + private final DialogService dialogService; + private final BibDatabaseContext databaseContext; + private final PreferencesService preferencesService; + private final BibEntry entry; + private final UndoManager undoManager; public GenerateBibtexKeySingleAction(BibEntry entry, BibDatabaseContext databaseContext, DialogService dialogService, PreferencesService preferencesService, UndoManager undoManager) { this.entry = entry; this.databaseContext = databaseContext; this.dialogService = dialogService; this.preferencesService = preferencesService; - this.bibtexKeyPatternPreferences = preferencesService.getBibtexKeyPatternPreferences(); this.undoManager = undoManager; - if (bibtexKeyPatternPreferences.avoidOverwritingCiteKey()) { + if (preferencesService.getBibtexKeyPatternPreferences().avoidOverwritingCiteKey()) { this.executable.bind(entry.getCiteKeyBinding().isNull()); } } diff --git a/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java b/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java index f1dbcd558bd..f09fd144411 100644 --- a/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java +++ b/src/main/java/org/jabref/gui/customizefields/CustomizeGeneralFieldsDialogViewModel.java @@ -71,7 +71,6 @@ public void saveFields() { } preferences.storeEntryEditorTabList(customTabsMap); - preferences.updateEntryEditorTabList(); } public void resetFields() { diff --git a/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java b/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java index 88b3652a4a2..52adaabd610 100644 --- a/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/EntryEditorTabViewModel.java @@ -46,8 +46,8 @@ public EntryEditorTabViewModel(DialogService dialogService, PreferencesService p @Override public void setValues() { - // ToDo: Include EntryEditorFieldsEditor in PreferencesDialog - // unused: initialEntryEditorPreferences.getEntryEditorTabList(); + // ToDo: Include CustomizeGeneralFieldsDialog in PreferencesDialog + // therefore yet unused: initialEntryEditorPreferences.getEntryEditorTabList(); openOnNewEntryProperty.setValue(initialEntryEditorPreferences.shouldOpenOnNewEntry()); defaultSourceProperty.setValue(initialEntryEditorPreferences.showSourceTabByDefault()); @@ -59,9 +59,9 @@ public void setValues() { enableAutoCompleteProperty.setValue(initialAutoCompletePreferences.shouldAutoComplete()); autoCompleteFieldsProperty.setValue(initialAutoCompletePreferences.getCompleteNamesAsString()); - if (initialAutoCompletePreferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.FIRST_LAST) { + if (initialAutoCompletePreferences.getNameFormat() == AutoCompletePreferences.NameFormat.FIRST_LAST) { autoCompleteFirstLastProperty.setValue(true); - } else if (initialAutoCompletePreferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST) { + } else if (initialAutoCompletePreferences.getNameFormat() == AutoCompletePreferences.NameFormat.LAST_FIRST) { autoCompleteLastFirstProperty.setValue(true); } else { autoCompleteBothProperty.setValue(true); @@ -92,11 +92,11 @@ public void storeSettings() { enableValidationProperty.getValue())); // default - AutoCompletePreferences.onlyCompleteNameFormat nameFormat = AutoCompletePreferences.onlyCompleteNameFormat.BOTH; + AutoCompletePreferences.NameFormat nameFormat = AutoCompletePreferences.NameFormat.BOTH; if (autoCompleteFirstLastProperty.getValue()) { - nameFormat = AutoCompletePreferences.onlyCompleteNameFormat.FIRST_LAST; + nameFormat = AutoCompletePreferences.NameFormat.FIRST_LAST; } else if (autoCompleteLastFirstProperty.getValue()) { - nameFormat = AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST; + nameFormat = AutoCompletePreferences.NameFormat.LAST_FIRST; } // default: AutoCompleteFirstNameMode.BOTH diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 60069a765e6..13e6edac036 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2038,6 +2038,8 @@ public void storeEntryEditorTabList(Map> customTabs) { purgeSeries(CUSTOM_TAB_NAME, customTabs.size()); purgeSeries(CUSTOM_TAB_FIELDS, customTabs.size()); + + updateEntryEditorTabList(); } /** @@ -2117,17 +2119,17 @@ public void storeEntryEditorPreferences(EntryEditorPreferences preferences) { @Override public AutoCompletePreferences getAutoCompletePreferences() { - AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat = AutoCompletePreferences.onlyCompleteNameFormat.BOTH; + AutoCompletePreferences.NameFormat nameFormat = AutoCompletePreferences.NameFormat.BOTH; if (getBoolean(AUTOCOMPLETER_LAST_FIRST)) { - onlyCompleteNameFormat = AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST; + nameFormat = AutoCompletePreferences.NameFormat.LAST_FIRST; } else if (getBoolean(AUTOCOMPLETER_FIRST_LAST)) { - onlyCompleteNameFormat = AutoCompletePreferences.onlyCompleteNameFormat.FIRST_LAST; + nameFormat = AutoCompletePreferences.NameFormat.FIRST_LAST; } return new AutoCompletePreferences( getBoolean(AUTO_COMPLETE), AutoCompleteFirstNameMode.parse(get(AUTOCOMPLETER_FIRSTNAME_MODE)), - onlyCompleteNameFormat, + nameFormat, getStringList(AUTOCOMPLETER_COMPLETE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toSet()), getJournalAbbreviationPreferences()); } @@ -2138,10 +2140,10 @@ public void storeAutoCompletePreferences(AutoCompletePreferences preferences) { put(AUTOCOMPLETER_FIRSTNAME_MODE, preferences.getFirstNameMode().name()); putStringList(AUTOCOMPLETER_COMPLETE_FIELDS, preferences.getCompleteFields().stream().map(Field::getName).collect(Collectors.toList())); - if (preferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.BOTH) { + if (preferences.getNameFormat() == AutoCompletePreferences.NameFormat.BOTH) { putBoolean(AUTOCOMPLETER_LAST_FIRST, false); putBoolean(AUTOCOMPLETER_FIRST_LAST, false); - } else if (preferences.getOnlyCompleteNameFormat() == AutoCompletePreferences.onlyCompleteNameFormat.LAST_FIRST) { + } else if (preferences.getNameFormat() == AutoCompletePreferences.NameFormat.LAST_FIRST) { putBoolean(AUTOCOMPLETER_LAST_FIRST, true); putBoolean(AUTOCOMPLETER_FIRST_LAST, false); } else {