Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor EntryEditorPreferences #6245

Merged
merged 10 commits into from
Apr 24, 2020
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.customizefields;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -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 {
Expand All @@ -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<String, Set<Field>> tabNamesAndFields) {
StringBuilder sb = new StringBuilder();

for (Map.Entry<String, Set<Field>> tab : preferences.getEntryEditorTabList().entrySet()) {
// Fill with customized vars
for (Map.Entry<String, Set<Field>> tab : tabNamesAndFields.entrySet()) {
sb.append(tab.getKey());
sb.append(':');
sb.append(FieldFactory.serializeFieldsList(tab.getValue()));
Expand All @@ -43,48 +46,36 @@ public StringProperty fieldsTextProperty() {
}

public void saveFields() {
Map<String, Set<Field>> 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);
calixtus marked this conversation as resolved.
Show resolved Hide resolved
preferences.updateEntryEditorTabList();
}

public void resetFields() {

StringBuilder sb = new StringBuilder();
Map<String,String> customTabNamesFields = preferences.getCustomTabsNamesAndFields();
for (Map.Entry<String,String>entry : 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());
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private List<EntryEditorTab> 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<String, Set<Field>> tab : entryEditorPreferences.getEntryEditorTabList().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ public class EntryEditorPreferences {
private boolean avoidOverwritingCiteKey;
private final boolean shouldShowLatexCitationsTab;

public EntryEditorPreferences(Map<String, Set<Field>> entryEditorTabList, FieldWriterPreferences fieldWriterPreferences, ImportFormatPreferences importFormatPreferences, List<Field> customTabFieldNames, boolean shouldShowRecommendationsTab, boolean isMrdlibAccepted, boolean shouldShowLatexCitationsTab, boolean showSourceTabByDefault, BibtexKeyPatternPreferences bibtexKeyPatternPreferences, KeyBindingRepository keyBindings, boolean avoidOverwritingCiteKey) {
public EntryEditorPreferences(Map<String, Set<Field>> entryEditorTabList,
FieldWriterPreferences fieldWriterPreferences,
ImportFormatPreferences importFormatPreferences,
List<Field> customTabFieldNames,
boolean shouldShowRecommendationsTab,
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
boolean isMrdlibAccepted,
boolean shouldShowLatexCitationsTab,
boolean showSourceTabByDefault,
BibtexKeyPatternPreferences bibtexKeyPatternPreferences,
KeyBindingRepository keyBindings,
boolean avoidOverwritingCiteKey) {
this.entryEditorTabList = entryEditorTabList;
this.fieldWriterPreferences = fieldWriterPreferences;
this.importFormatPreferences = importFormatPreferences;
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditorTabList.java

This file was deleted.

23 changes: 20 additions & 3 deletions src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,27 @@ public class OtherFieldsTab extends FieldsEditorTab {
private final List<Field> customTabFieldNames;
private final BibEntryTypesManager entryTypesManager;

public OtherFieldsTab(BibDatabaseContext databaseContext, SuggestionProviders suggestionProviders, UndoManager undoManager, List<Field> 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")));
Expand Down
Loading