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
Expand Up @@ -8,26 +8,24 @@

public class AutoCompletePreferences {

public enum onlyCompleteNameFormat {
calixtus marked this conversation as resolved.
Show resolved Hide resolved
LAST_FIRST, FIRST_LAST, BOTH
}

private boolean shouldAutoComplete;
private AutoCompleteFirstNameMode firstNameMode;
private boolean onlyCompleteLastFirst;
private boolean onlyCompleteFirstLast;
private AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat;
private Set<Field> completeFields;
private final JournalAbbreviationPreferences journalAbbreviationPreferences;

public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, boolean onlyCompleteLastFirst, boolean onlyCompleteFirstLast, Set<Field> completeFields, JournalAbbreviationPreferences journalAbbreviationPreferences) {
public AutoCompletePreferences(boolean shouldAutoComplete, AutoCompleteFirstNameMode firstNameMode, AutoCompletePreferences.onlyCompleteNameFormat onlyCompleteNameFormat, Set<Field> 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;
}
Expand All @@ -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
Expand All @@ -67,14 +47,6 @@ public Set<Field> getCompleteFields() {
return completeFields;
}

public void setCompleteFields(Set<Field> completeFields) {
this.completeFields = completeFields;
}

public void setCompleteNames(String input) {
setCompleteFields(FieldFactory.parseFieldList(input));
}

public String getCompleteNamesAsString() {
return FieldFactory.serializeFieldsList(completeFields);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@

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;
calixtus marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}

@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)));
}
Expand Down
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());
}
}
20 changes: 13 additions & 7 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public EntryEditor(BasePanel panel, ExternalFileTypes externalFileTypes) {
*/
private void setupKeyBindings() {
this.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
Optional<KeyBinding> keyBinding = entryEditorPreferences.getKeyBindings().mapToKeyBinding(event);
Optional<KeyBinding> keyBinding = Globals.getKeyPrefs().mapToKeyBinding(event);
if (keyBinding.isPresent()) {
switch (keyBinding.get()) {
case ENTRY_EDITOR_NEXT_PANEL:
Expand Down Expand Up @@ -202,7 +202,7 @@ private void deleteEntry() {
@FXML
void generateCiteKeyButton() {
GenerateBibtexKeySingleAction action = new GenerateBibtexKeySingleAction(getEntry(), databaseContext,
dialogService, entryEditorPreferences, undoManager);
dialogService, preferencesService, undoManager);
action.execute();
}

Expand All @@ -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 All @@ -242,9 +242,15 @@ private List<EntryEditorTab> 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
Expand Down Expand Up @@ -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);
Expand Down
Loading