From 4c440876337631555f03bf2cd99c33580647f74a Mon Sep 17 00:00:00 2001 From: Naitoreivun Date: Mon, 18 Jun 2018 03:02:47 +0200 Subject: [PATCH 1/5] force author, title and year text areas to be single line text field using text formatter --- .../gui/fieldeditors/EditorTextArea.java | 22 +++++++++++++-- .../jabref/gui/fieldeditors/FieldEditors.java | 28 +++++++++++++++---- .../gui/fieldeditors/PersonsEditor.java | 8 ++++-- .../jabref/gui/fieldeditors/SimpleEditor.java | 16 +++++++++-- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java b/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java index 6f783e05d06..42cf9808c9e 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java +++ b/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java @@ -8,6 +8,7 @@ import javafx.fxml.Initializable; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; +import javafx.scene.control.TextFormatter; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; @@ -16,10 +17,14 @@ public class EditorTextArea extends javafx.scene.control.TextArea implements Initializable { public EditorTextArea() { - this(""); + this("", false); } - public EditorTextArea(String text) { + public EditorTextArea(final boolean hasSingleLine) { + this("", hasSingleLine); + } + + public EditorTextArea(final String text, final boolean hasSingleLine) { super(text); setMinHeight(1); @@ -47,6 +52,19 @@ public EditorTextArea(String text) { event.consume(); } }); + + if (hasSingleLine) { + setTextFormatter(createSingleLineTextFormatter()); + } + } + + private static TextFormatter createSingleLineTextFormatter() { + return new TextFormatter<>(change -> { + if (change.isAdded()) { + change.setText(change.getText().replaceAll("\\s+", " ")); + } + return change; + }); } /** diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index cc2b1fd32ac..0db60b39b7f 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -22,6 +22,7 @@ import org.jabref.model.metadata.MetaData; import org.jabref.preferences.JabRefPreferences; +import com.google.common.collect.ImmutableSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,12 +30,29 @@ public class FieldEditors { private static final Logger LOGGER = LoggerFactory.getLogger(FieldEditors.class); - public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecutor, DialogService dialogService, JournalAbbreviationLoader journalAbbreviationLoader, JournalAbbreviationPreferences journalAbbreviationPreferences, JabRefPreferences preferences, BibDatabaseContext databaseContext, String entryType, SuggestionProviders suggestionProviders, UndoManager undoManager) { + private static final ImmutableSet SINGLE_LINE_FIELDS = ImmutableSet.of("title", "author", "year"); + + public static FieldEditorFX getForField(final String fieldName, + final TaskExecutor taskExecutor, + final DialogService dialogService, + final JournalAbbreviationLoader journalAbbreviationLoader, + final JournalAbbreviationPreferences journalAbbreviationPreferences, + final JabRefPreferences preferences, + final BibDatabaseContext databaseContext, + final String entryType, + final SuggestionProviders suggestionProviders, + final UndoManager undoManager) { final Set fieldExtras = InternalBibtexFields.getFieldProperties(fieldName); - AutoCompleteSuggestionProvider suggestionProvider = getSuggestionProvider(fieldName, suggestionProviders, databaseContext.getMetaData()); + final AutoCompleteSuggestionProvider suggestionProvider = getSuggestionProvider(fieldName, suggestionProviders, databaseContext.getMetaData()); + + final FieldCheckers fieldCheckers = new FieldCheckers( + databaseContext, + preferences.getFileDirectoryPreferences(), + journalAbbreviationLoader.getRepository(journalAbbreviationPreferences), + preferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY)); - FieldCheckers fieldCheckers = new FieldCheckers(databaseContext, preferences.getFileDirectoryPreferences(), journalAbbreviationLoader.getRepository(journalAbbreviationPreferences), preferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY)); + final boolean hasSingleLine = SINGLE_LINE_FIELDS.contains(fieldName.toLowerCase()); if (preferences.getTimestampPreferences().getTimestampField().equals(fieldName) || fieldExtras.contains(FieldProperty.DATE)) { if (fieldExtras.contains(FieldProperty.ISO_DATE)) { @@ -71,7 +89,7 @@ public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecu } else if (fieldExtras.contains(FieldProperty.SINGLE_ENTRY_LINK) || fieldExtras.contains(FieldProperty.MULTIPLE_ENTRY_LINK)) { return new LinkedEntriesEditor(fieldName, databaseContext, suggestionProvider, fieldCheckers); } else if (fieldExtras.contains(FieldProperty.PERSON_NAMES)) { - return new PersonsEditor(fieldName, suggestionProvider, preferences, fieldCheckers); + return new PersonsEditor(fieldName, suggestionProvider, preferences, fieldCheckers, hasSingleLine); } else if (FieldName.KEYWORDS.equals(fieldName)) { return new KeywordsEditor(fieldName, suggestionProvider, fieldCheckers, preferences); } else if (fieldExtras.contains(FieldProperty.MULTILINE_TEXT)) { @@ -81,7 +99,7 @@ public static FieldEditorFX getForField(String fieldName, TaskExecutor taskExecu } // default - return new SimpleEditor(fieldName, suggestionProvider, fieldCheckers, preferences); + return new SimpleEditor(fieldName, suggestionProvider, fieldCheckers, preferences, hasSingleLine); } @SuppressWarnings("unchecked") diff --git a/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java b/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java index 01f5dccc8b9..a8506779f85 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java @@ -18,10 +18,14 @@ public class PersonsEditor extends HBox implements FieldEditorFX { private EditorTextArea textArea; - public PersonsEditor(String fieldName, AutoCompleteSuggestionProvider suggestionProvider, JabRefPreferences preferences, FieldCheckers fieldCheckers) { + public PersonsEditor(final String fieldName, + final AutoCompleteSuggestionProvider suggestionProvider, + final JabRefPreferences preferences, + final FieldCheckers fieldCheckers, + final boolean hasSingleLine) { this.viewModel = new PersonsEditorViewModel(fieldName, suggestionProvider, preferences.getAutoCompletePreferences(), fieldCheckers); - textArea = new EditorTextArea(); + textArea = new EditorTextArea(hasSingleLine); HBox.setHgrow(textArea, Priority.ALWAYS); textArea.textProperty().bindBidirectional(viewModel.textProperty()); textArea.addToContextMenu(EditorMenus.getNameMenu(textArea)); diff --git a/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java b/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java index 00e0e991183..0c588a61b5f 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java @@ -17,10 +17,14 @@ public class SimpleEditor extends HBox implements FieldEditorFX { @FXML private final SimpleEditorViewModel viewModel; - public SimpleEditor(String fieldName, AutoCompleteSuggestionProvider suggestionProvider, FieldCheckers fieldCheckers, JabRefPreferences preferences) { + public SimpleEditor(final String fieldName, + final AutoCompleteSuggestionProvider suggestionProvider, + final FieldCheckers fieldCheckers, + final JabRefPreferences preferences, + final boolean hasSingleLine) { this.viewModel = new SimpleEditorViewModel(fieldName, suggestionProvider, fieldCheckers); - EditorTextArea textArea = new EditorTextArea(); + EditorTextArea textArea = new EditorTextArea(hasSingleLine); HBox.setHgrow(textArea, Priority.ALWAYS); textArea.textProperty().bindBidirectional(viewModel.textProperty()); textArea.addToContextMenu(EditorMenus.getDefaultMenu(textArea)); @@ -35,6 +39,14 @@ public SimpleEditor(String fieldName, AutoCompleteSuggestionProvider suggesti new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea); } + + public SimpleEditor(final String fieldName, + final AutoCompleteSuggestionProvider suggestionProvider, + final FieldCheckers fieldCheckers, + final JabRefPreferences preferences) { + this(fieldName, suggestionProvider, fieldCheckers, preferences, false); + } + @Override public void bindToEntry(BibEntry entry) { viewModel.bindToEntry(entry); From 24e2183ee85fb3f996f32842ca801f6ce630fe7f Mon Sep 17 00:00:00 2001 From: Naitoreivun Date: Mon, 18 Jun 2018 03:34:43 +0200 Subject: [PATCH 2/5] consider 'institution' as a field, that should contain only one line --- src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index 0db60b39b7f..3ec78546ed6 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -30,7 +30,8 @@ public class FieldEditors { private static final Logger LOGGER = LoggerFactory.getLogger(FieldEditors.class); - private static final ImmutableSet SINGLE_LINE_FIELDS = ImmutableSet.of("title", "author", "year"); + private static final ImmutableSet SINGLE_LINE_FIELDS = ImmutableSet.of( + "title", "author", "year", "institution"); public static FieldEditorFX getForField(final String fieldName, final TaskExecutor taskExecutor, From d4262913aa90fae2c07ec6ab6f733733a519b140 Mon Sep 17 00:00:00 2001 From: Naitoreivun Date: Thu, 21 Jun 2018 22:22:31 +0200 Subject: [PATCH 3/5] create EditorTextField and use that control for author, institution, year and title --- .../gui/fieldeditors/ContextMenuAddable.java | 15 +++++ .../gui/fieldeditors/EditorTextArea.java | 32 ++------- .../gui/fieldeditors/EditorTextField.java | 65 +++++++++++++++++++ .../gui/fieldeditors/EditorValidator.java | 6 +- .../jabref/gui/fieldeditors/FieldEditors.java | 14 +++- .../gui/fieldeditors/PersonsEditor.java | 21 +++--- .../jabref/gui/fieldeditors/SimpleEditor.java | 17 +++-- .../fieldeditors/contextmenu/ClearField.java | 4 +- .../fieldeditors/contextmenu/EditorMenus.java | 21 +++--- .../contextmenu/ProtectedTermsMenu.java | 6 +- 10 files changed, 138 insertions(+), 63 deletions(-) create mode 100644 src/main/java/org/jabref/gui/fieldeditors/ContextMenuAddable.java create mode 100644 src/main/java/org/jabref/gui/fieldeditors/EditorTextField.java diff --git a/src/main/java/org/jabref/gui/fieldeditors/ContextMenuAddable.java b/src/main/java/org/jabref/gui/fieldeditors/ContextMenuAddable.java new file mode 100644 index 00000000000..9f9bff69e41 --- /dev/null +++ b/src/main/java/org/jabref/gui/fieldeditors/ContextMenuAddable.java @@ -0,0 +1,15 @@ +package org.jabref.gui.fieldeditors; + +import java.util.List; +import java.util.function.Supplier; + +import javafx.scene.control.MenuItem; + +public interface ContextMenuAddable { + /** + * Adds the given list of menu items to the context menu. The usage of {@link Supplier} prevents that the menus need + * to be instantiated at this point. They are populated when the user needs them which prevents many unnecessary + * allocations when the main table is just scrolled with the entry editor open. + */ + void addToContextMenu(final Supplier> items); +} diff --git a/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java b/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java index 42cf9808c9e..1783e9f1396 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java +++ b/src/main/java/org/jabref/gui/fieldeditors/EditorTextArea.java @@ -8,23 +8,18 @@ import javafx.fxml.Initializable; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; -import javafx.scene.control.TextFormatter; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import com.sun.javafx.scene.control.skin.TextAreaSkin; -public class EditorTextArea extends javafx.scene.control.TextArea implements Initializable { +public class EditorTextArea extends javafx.scene.control.TextArea implements Initializable, ContextMenuAddable { public EditorTextArea() { - this("", false); + this(""); } - public EditorTextArea(final boolean hasSingleLine) { - this("", hasSingleLine); - } - - public EditorTextArea(final String text, final boolean hasSingleLine) { + public EditorTextArea(final String text) { super(text); setMinHeight(1); @@ -52,27 +47,10 @@ public EditorTextArea(final String text, final boolean hasSingleLine) { event.consume(); } }); - - if (hasSingleLine) { - setTextFormatter(createSingleLineTextFormatter()); - } - } - - private static TextFormatter createSingleLineTextFormatter() { - return new TextFormatter<>(change -> { - if (change.isAdded()) { - change.setText(change.getText().replaceAll("\\s+", " ")); - } - return change; - }); } - /** - * Adds the given list of menu items to the context menu. The usage of {@link Supplier} prevents that the menus need - * to be instantiated at this point. They are populated when the user needs them which prevents many unnecessary - * allocations when the main table is just scrolled with the entry editor open. - */ - public void addToContextMenu(Supplier> items) { + @Override + public void addToContextMenu(final Supplier> items) { TextAreaSkin customContextSkin = new TextAreaSkin(this) { @Override public void populateContextMenu(ContextMenu contextMenu) { diff --git a/src/main/java/org/jabref/gui/fieldeditors/EditorTextField.java b/src/main/java/org/jabref/gui/fieldeditors/EditorTextField.java new file mode 100644 index 00000000000..26c23006a88 --- /dev/null +++ b/src/main/java/org/jabref/gui/fieldeditors/EditorTextField.java @@ -0,0 +1,65 @@ +package org.jabref.gui.fieldeditors; + +import java.net.URL; +import java.util.List; +import java.util.ResourceBundle; +import java.util.function.Supplier; + +import javafx.fxml.Initializable; +import javafx.scene.control.ContextMenu; +import javafx.scene.control.MenuItem; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; + +import com.sun.javafx.scene.control.skin.TextFieldSkin; + +public class EditorTextField extends javafx.scene.control.TextField implements Initializable, ContextMenuAddable { + + public EditorTextField() { + this(""); + } + + public EditorTextField(final String text) { + super(text); + + setMinHeight(1); + setMinWidth(200); + + // Should behave as a normal text field with respect to TAB behaviour + addEventFilter(KeyEvent.KEY_PRESSED, event -> { + if (event.getCode() == KeyCode.TAB) { + TextFieldSkin skin = (TextFieldSkin) getSkin(); + if (event.isShiftDown()) { + // Shift + Tab > previous text area + skin.getBehavior().traversePrevious(); + } else { + if (event.isControlDown()) { + // Ctrl + Tab > insert tab + skin.getBehavior().callAction("InsertTab"); + } else { + // Tab > next text area + skin.getBehavior().traverseNext(); + } + } + event.consume(); + } + }); + } + + @Override + public void addToContextMenu(final Supplier> items) { + TextFieldSkin customContextSkin = new TextFieldSkin(this) { + @Override + public void populateContextMenu(ContextMenu contextMenu) { + super.populateContextMenu(contextMenu); + contextMenu.getItems().addAll(0, items.get()); + } + }; + setSkin(customContextSkin); + } + + @Override + public void initialize(URL location, ResourceBundle resources) { + // not needed + } +} diff --git a/src/main/java/org/jabref/gui/fieldeditors/EditorValidator.java b/src/main/java/org/jabref/gui/fieldeditors/EditorValidator.java index af3c8791a0d..626626ad468 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/EditorValidator.java +++ b/src/main/java/org/jabref/gui/fieldeditors/EditorValidator.java @@ -1,5 +1,7 @@ package org.jabref.gui.fieldeditors; +import javafx.scene.control.TextInputControl; + import org.jabref.gui.util.IconValidationDecorator; import org.jabref.preferences.JabRefPreferences; @@ -14,11 +16,11 @@ public EditorValidator(JabRefPreferences preferences) { this.preferences = preferences; } - public void configureValidation(ValidationStatus status, EditorTextArea area) { + public void configureValidation(final ValidationStatus status, final TextInputControl textInput) { if (preferences.getBoolean(JabRefPreferences.VALIDATE_IN_ENTRY_EDITOR)) { ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer(); validationVisualizer.setDecoration(new IconValidationDecorator()); - validationVisualizer.initVisualization(status, area); + validationVisualizer.initVisualization(status, textInput); } } } diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index 3ec78546ed6..b7fbee0ddd6 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -1,6 +1,9 @@ package org.jabref.gui.fieldeditors; import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -22,16 +25,21 @@ import org.jabref.model.metadata.MetaData; import org.jabref.preferences.JabRefPreferences; -import com.google.common.collect.ImmutableSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.jabref.model.entry.FieldName.AUTHOR; +import static org.jabref.model.entry.FieldName.INSTITUTION; +import static org.jabref.model.entry.FieldName.TITLE; +import static org.jabref.model.entry.FieldName.YEAR; + public class FieldEditors { private static final Logger LOGGER = LoggerFactory.getLogger(FieldEditors.class); - private static final ImmutableSet SINGLE_LINE_FIELDS = ImmutableSet.of( - "title", "author", "year", "institution"); + private static final Set SINGLE_LINE_FIELDS = Collections.unmodifiableSet(new HashSet<>( + Arrays.asList(TITLE, AUTHOR, YEAR, INSTITUTION) + )); public static FieldEditorFX getForField(final String fieldName, final TaskExecutor taskExecutor, diff --git a/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java b/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java index a8506779f85..65f23937361 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java @@ -2,6 +2,7 @@ import javafx.fxml.FXML; import javafx.scene.Parent; +import javafx.scene.control.TextInputControl; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; @@ -16,7 +17,7 @@ public class PersonsEditor extends HBox implements FieldEditorFX { @FXML private final PersonsEditorViewModel viewModel; - private EditorTextArea textArea; + private TextInputControl textInput; public PersonsEditor(final String fieldName, final AutoCompleteSuggestionProvider suggestionProvider, @@ -25,15 +26,17 @@ public PersonsEditor(final String fieldName, final boolean hasSingleLine) { this.viewModel = new PersonsEditorViewModel(fieldName, suggestionProvider, preferences.getAutoCompletePreferences(), fieldCheckers); - textArea = new EditorTextArea(hasSingleLine); - HBox.setHgrow(textArea, Priority.ALWAYS); - textArea.textProperty().bindBidirectional(viewModel.textProperty()); - textArea.addToContextMenu(EditorMenus.getNameMenu(textArea)); - this.getChildren().add(textArea); + textInput = hasSingleLine + ? new EditorTextField() + : new EditorTextArea(); + HBox.setHgrow(textInput, Priority.ALWAYS); + textInput.textProperty().bindBidirectional(viewModel.textProperty()); + ((ContextMenuAddable) textInput).addToContextMenu(EditorMenus.getNameMenu(textInput)); + this.getChildren().add(textInput); - AutoCompletionTextInputBinding.autoComplete(textArea, viewModel::complete, viewModel.getAutoCompletionConverter(), viewModel.getAutoCompletionStrategy()); + AutoCompletionTextInputBinding.autoComplete(textInput, viewModel::complete, viewModel.getAutoCompletionConverter(), viewModel.getAutoCompletionStrategy()); - new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea); + new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textInput); } @Override @@ -48,7 +51,7 @@ public Parent getNode() { @Override public void requestFocus() { - textArea.requestFocus(); + textInput.requestFocus(); } } diff --git a/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java b/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java index 0c588a61b5f..6709c4c4d2d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java @@ -2,6 +2,7 @@ import javafx.fxml.FXML; import javafx.scene.Parent; +import javafx.scene.control.TextInputControl; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; @@ -24,19 +25,21 @@ public SimpleEditor(final String fieldName, final boolean hasSingleLine) { this.viewModel = new SimpleEditorViewModel(fieldName, suggestionProvider, fieldCheckers); - EditorTextArea textArea = new EditorTextArea(hasSingleLine); - HBox.setHgrow(textArea, Priority.ALWAYS); - textArea.textProperty().bindBidirectional(viewModel.textProperty()); - textArea.addToContextMenu(EditorMenus.getDefaultMenu(textArea)); - this.getChildren().add(textArea); + TextInputControl textInput = hasSingleLine + ? new EditorTextField() + : new EditorTextArea(); + HBox.setHgrow(textInput, Priority.ALWAYS); + textInput.textProperty().bindBidirectional(viewModel.textProperty()); + ((ContextMenuAddable) textInput).addToContextMenu(EditorMenus.getDefaultMenu(textInput)); + this.getChildren().add(textInput); - AutoCompletionTextInputBinding autoCompleter = AutoCompletionTextInputBinding.autoComplete(textArea, viewModel::complete, viewModel.getAutoCompletionStrategy()); + AutoCompletionTextInputBinding autoCompleter = AutoCompletionTextInputBinding.autoComplete(textInput, viewModel::complete, viewModel.getAutoCompletionStrategy()); if (suggestionProvider instanceof ContentSelectorSuggestionProvider) { // If content selector values are present, then we want to show the auto complete suggestions immediately on focus autoCompleter.setShowOnFocus(true); } - new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea); + new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textInput); } diff --git a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ClearField.java b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ClearField.java index 52ef77f1a8c..22b846f44e2 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ClearField.java +++ b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ClearField.java @@ -1,13 +1,13 @@ package org.jabref.gui.fieldeditors.contextmenu; import javafx.scene.control.MenuItem; -import javafx.scene.control.TextArea; +import javafx.scene.control.TextInputControl; import org.jabref.logic.l10n.Localization; class ClearField extends MenuItem { - public ClearField(TextArea opener) { + public ClearField(TextInputControl opener) { super(Localization.lang("Clear")); setOnAction(event -> opener.setText("")); } diff --git a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java index 12e6382a3b1..179a1cfb9b7 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java +++ b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/EditorMenus.java @@ -12,6 +12,7 @@ import javafx.scene.control.MenuItem; import javafx.scene.control.SeparatorMenuItem; import javafx.scene.control.TextArea; +import javafx.scene.control.TextInputControl; import javafx.scene.control.Tooltip; import org.jabref.gui.actions.CopyDoiUrlAction; @@ -29,18 +30,18 @@ public class EditorMenus { /** * The default menu that contains functions for changing the case of text and doing several conversions. * - * @param textArea text-area that this menu will be connected to + * @param textInput text-input-control that this menu will be connected to * @return default context menu available for most text fields */ - public static Supplier> getDefaultMenu(TextArea textArea) { + public static Supplier> getDefaultMenu(final TextInputControl textInput) { return () -> { List menuItems = new ArrayList<>(6); - menuItems.add(new CaseChangeMenu(textArea.textProperty())); - menuItems.add(new ConversionMenu(textArea.textProperty())); + menuItems.add(new CaseChangeMenu(textInput.textProperty())); + menuItems.add(new ConversionMenu(textInput.textProperty())); menuItems.add(new SeparatorMenuItem()); - menuItems.add(new ProtectedTermsMenu(textArea)); + menuItems.add(new ProtectedTermsMenu(textInput)); menuItems.add(new SeparatorMenuItem()); - menuItems.add(new ClearField(textArea)); + menuItems.add(new ClearField(textInput)); return menuItems; }; } @@ -48,18 +49,18 @@ public static Supplier> getDefaultMenu(TextArea textArea) { /** * The default context menu with a specific menu for normalizing person names regarding to BibTex rules. * - * @param textArea text-area that this menu will be connected to + * @param textInput text-input-control that this menu will be connected to * @return menu containing items of the default menu and an item for normalizing person names */ - public static Supplier> getNameMenu(TextArea textArea) { + public static Supplier> getNameMenu(final TextInputControl textInput) { return () -> { CustomMenuItem normalizeNames = new CustomMenuItem(new Label(Localization.lang("Normalize to BibTeX name format"))); - normalizeNames.setOnAction(event -> textArea.setText(new NormalizeNamesFormatter().format(textArea.getText()))); + normalizeNames.setOnAction(event -> textInput.setText(new NormalizeNamesFormatter().format(textInput.getText()))); Tooltip toolTip = new Tooltip(Localization.lang("If possible, normalize this list of names to conform to standard BibTeX name formatting")); Tooltip.install(normalizeNames.getContent(), toolTip); List menuItems = new ArrayList<>(6); menuItems.add(normalizeNames); - menuItems.addAll(getDefaultMenu(textArea).get()); + menuItems.addAll(getDefaultMenu(textInput).get()); return menuItems; }; } diff --git a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java index 3ede64f1fea..4af7fec8023 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java +++ b/src/main/java/org/jabref/gui/fieldeditors/contextmenu/ProtectedTermsMenu.java @@ -8,7 +8,7 @@ import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import javafx.scene.control.SeparatorMenuItem; -import javafx.scene.control.TextArea; +import javafx.scene.control.TextInputControl; import org.jabref.Globals; import org.jabref.JabRefGUI; @@ -23,9 +23,9 @@ class ProtectedTermsMenu extends Menu { private static final Formatter FORMATTER = new ProtectTermsFormatter(Globals.protectedTermsLoader); private final Menu externalFiles; - private final TextArea opener; + private final TextInputControl opener; - public ProtectedTermsMenu(TextArea opener) { + public ProtectedTermsMenu(final TextInputControl opener) { super(Localization.lang("Protect terms")); this.opener = opener; MenuItem protectItem = new MenuItem(Localization.lang("Add {} around selected text")); From 26475f674a763704c37984186e776d6b5607e72d Mon Sep 17 00:00:00 2001 From: Naitoreivun Date: Sat, 23 Jun 2018 14:23:13 +0200 Subject: [PATCH 4/5] pass JournalAbbrevRepo instead of Loader and Prefs --- .../jabref/gui/entryeditor/FieldsEditorTab.java | 5 ++--- .../org/jabref/gui/fieldeditors/FieldEditors.java | 10 ++++------ .../jabref/gui/fieldeditors/JournalEditor.java | 6 +++--- .../gui/fieldeditors/JournalEditorViewModel.java | 15 +++++---------- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java b/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java index a757a7ee4c7..c653ed1b23f 100644 --- a/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java @@ -85,9 +85,8 @@ private Region setupPanel(BibEntry entry, boolean compressed, SuggestionProvider boolean isFirstField = true; for (String fieldName : fields) { FieldEditorFX fieldEditor = FieldEditors.getForField(fieldName, Globals.TASK_EXECUTOR, dialogService, - Globals.journalAbbreviationLoader, Globals.prefs.getJournalAbbreviationPreferences(), Globals.prefs, - databaseContext, entry.getType(), - suggestionProviders, undoManager); + Globals.journalAbbreviationLoader.getRepository(Globals.prefs.getJournalAbbreviationPreferences()), + Globals.prefs, databaseContext, entry.getType(), suggestionProviders, undoManager); fieldEditor.bindToEntry(entry); editors.put(fieldName, fieldEditor); diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index b7fbee0ddd6..939695e5e0f 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -16,8 +16,7 @@ import org.jabref.gui.autocompleter.SuggestionProviders; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.integrity.FieldCheckers; -import org.jabref.logic.journals.JournalAbbreviationLoader; -import org.jabref.logic.journals.JournalAbbreviationPreferences; +import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.FieldName; import org.jabref.model.entry.FieldProperty; @@ -44,8 +43,7 @@ public class FieldEditors { public static FieldEditorFX getForField(final String fieldName, final TaskExecutor taskExecutor, final DialogService dialogService, - final JournalAbbreviationLoader journalAbbreviationLoader, - final JournalAbbreviationPreferences journalAbbreviationPreferences, + final JournalAbbreviationRepository journalAbbreviationRepository, final JabRefPreferences preferences, final BibDatabaseContext databaseContext, final String entryType, @@ -58,7 +56,7 @@ public static FieldEditorFX getForField(final String fieldName, final FieldCheckers fieldCheckers = new FieldCheckers( databaseContext, preferences.getFileDirectoryPreferences(), - journalAbbreviationLoader.getRepository(journalAbbreviationPreferences), + journalAbbreviationRepository, preferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY)); final boolean hasSingleLine = SINGLE_LINE_FIELDS.contains(fieldName.toLowerCase()); @@ -72,7 +70,7 @@ public static FieldEditorFX getForField(final String fieldName, } else if (fieldExtras.contains(FieldProperty.EXTERNAL)) { return new UrlEditor(fieldName, dialogService, suggestionProvider, fieldCheckers, preferences); } else if (fieldExtras.contains(FieldProperty.JOURNAL_NAME)) { - return new JournalEditor(fieldName, journalAbbreviationLoader, preferences, suggestionProvider, fieldCheckers); + return new JournalEditor(fieldName, journalAbbreviationRepository, preferences, suggestionProvider, fieldCheckers); } else if (fieldExtras.contains(FieldProperty.DOI) || fieldExtras.contains(FieldProperty.EPRINT) || fieldExtras.contains(FieldProperty.ISBN)) { return new IdentifierEditor(fieldName, taskExecutor, dialogService, suggestionProvider, fieldCheckers, preferences); } else if (fieldExtras.contains(FieldProperty.OWNER)) { diff --git a/src/main/java/org/jabref/gui/fieldeditors/JournalEditor.java b/src/main/java/org/jabref/gui/fieldeditors/JournalEditor.java index af338ef26fa..ad00729bab3 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/JournalEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/JournalEditor.java @@ -8,7 +8,7 @@ import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding; import org.jabref.gui.fieldeditors.contextmenu.EditorMenus; import org.jabref.logic.integrity.FieldCheckers; -import org.jabref.logic.journals.JournalAbbreviationLoader; +import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.model.entry.BibEntry; import org.jabref.preferences.JabRefPreferences; @@ -19,8 +19,8 @@ public class JournalEditor extends HBox implements FieldEditorFX { @FXML private JournalEditorViewModel viewModel; @FXML private EditorTextArea textArea; - public JournalEditor(String fieldName, JournalAbbreviationLoader journalAbbreviationLoader, JabRefPreferences preferences, AutoCompleteSuggestionProvider suggestionProvider, FieldCheckers fieldCheckers) { - this.viewModel = new JournalEditorViewModel(fieldName, suggestionProvider, journalAbbreviationLoader, preferences.getJournalAbbreviationPreferences(), fieldCheckers); + public JournalEditor(String fieldName, JournalAbbreviationRepository journalAbbreviationRepository, JabRefPreferences preferences, AutoCompleteSuggestionProvider suggestionProvider, FieldCheckers fieldCheckers) { + this.viewModel = new JournalEditorViewModel(fieldName, suggestionProvider, journalAbbreviationRepository, fieldCheckers); ViewLoader.view(this) .root(this) diff --git a/src/main/java/org/jabref/gui/fieldeditors/JournalEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/JournalEditorViewModel.java index e59d1305efc..2cf93f8bbec 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/JournalEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/JournalEditorViewModel.java @@ -4,20 +4,16 @@ import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider; import org.jabref.logic.integrity.FieldCheckers; -import org.jabref.logic.journals.JournalAbbreviationLoader; -import org.jabref.logic.journals.JournalAbbreviationPreferences; import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.model.strings.StringUtil; public class JournalEditorViewModel extends AbstractEditorViewModel { - private final JournalAbbreviationLoader journalAbbreviationLoader; - private final JournalAbbreviationPreferences journalAbbreviationPreferences; + private final JournalAbbreviationRepository journalAbbreviationRepository; - public JournalEditorViewModel(String fieldName, AutoCompleteSuggestionProvider suggestionProvider, JournalAbbreviationLoader journalAbbreviationLoader, JournalAbbreviationPreferences journalAbbreviationPreferences, FieldCheckers fieldCheckers) { + public JournalEditorViewModel(String fieldName, AutoCompleteSuggestionProvider suggestionProvider, JournalAbbreviationRepository journalAbbreviationRepository, FieldCheckers fieldCheckers) { super(fieldName, suggestionProvider, fieldCheckers); - this.journalAbbreviationLoader = journalAbbreviationLoader; - this.journalAbbreviationPreferences = journalAbbreviationPreferences; + this.journalAbbreviationRepository = journalAbbreviationRepository; } public void toggleAbbreviation() { @@ -25,9 +21,8 @@ public void toggleAbbreviation() { return; } - JournalAbbreviationRepository abbreviationRepository = journalAbbreviationLoader.getRepository(journalAbbreviationPreferences); - if (abbreviationRepository.isKnownName(text.get())) { - Optional nextAbbreviation = abbreviationRepository.getNextAbbreviation(text.get()); + if (journalAbbreviationRepository.isKnownName(text.get())) { + Optional nextAbbreviation = journalAbbreviationRepository.getNextAbbreviation(text.get()); if (nextAbbreviation.isPresent()) { text.set(nextAbbreviation.get()); From ac105932c0d949872d81dfc7a3cd2bb0b14518a2 Mon Sep 17 00:00:00 2001 From: Naitoreivun Date: Sun, 22 Jul 2018 20:54:34 +0200 Subject: [PATCH 5/5] code review fixes - move SINGLE_LINE_FIELDS list to InternalBibtexFields class - rename hasSingleLine var to isSingleLine --- .../jabref/gui/fieldeditors/FieldEditors.java | 18 +++--------------- .../jabref/gui/fieldeditors/PersonsEditor.java | 4 ++-- .../jabref/gui/fieldeditors/SimpleEditor.java | 4 ++-- .../model/entry/InternalBibtexFields.java | 8 ++++++++ 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java index 939695e5e0f..b94a480b081 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java +++ b/src/main/java/org/jabref/gui/fieldeditors/FieldEditors.java @@ -1,9 +1,6 @@ package org.jabref.gui.fieldeditors; import java.time.format.DateTimeFormatter; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Set; @@ -27,19 +24,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.jabref.model.entry.FieldName.AUTHOR; -import static org.jabref.model.entry.FieldName.INSTITUTION; -import static org.jabref.model.entry.FieldName.TITLE; -import static org.jabref.model.entry.FieldName.YEAR; - public class FieldEditors { private static final Logger LOGGER = LoggerFactory.getLogger(FieldEditors.class); - private static final Set SINGLE_LINE_FIELDS = Collections.unmodifiableSet(new HashSet<>( - Arrays.asList(TITLE, AUTHOR, YEAR, INSTITUTION) - )); - public static FieldEditorFX getForField(final String fieldName, final TaskExecutor taskExecutor, final DialogService dialogService, @@ -59,7 +47,7 @@ public static FieldEditorFX getForField(final String fieldName, journalAbbreviationRepository, preferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY)); - final boolean hasSingleLine = SINGLE_LINE_FIELDS.contains(fieldName.toLowerCase()); + final boolean isSingleLine = InternalBibtexFields.isSingleLineField(fieldName); if (preferences.getTimestampPreferences().getTimestampField().equals(fieldName) || fieldExtras.contains(FieldProperty.DATE)) { if (fieldExtras.contains(FieldProperty.ISO_DATE)) { @@ -96,7 +84,7 @@ public static FieldEditorFX getForField(final String fieldName, } else if (fieldExtras.contains(FieldProperty.SINGLE_ENTRY_LINK) || fieldExtras.contains(FieldProperty.MULTIPLE_ENTRY_LINK)) { return new LinkedEntriesEditor(fieldName, databaseContext, suggestionProvider, fieldCheckers); } else if (fieldExtras.contains(FieldProperty.PERSON_NAMES)) { - return new PersonsEditor(fieldName, suggestionProvider, preferences, fieldCheckers, hasSingleLine); + return new PersonsEditor(fieldName, suggestionProvider, preferences, fieldCheckers, isSingleLine); } else if (FieldName.KEYWORDS.equals(fieldName)) { return new KeywordsEditor(fieldName, suggestionProvider, fieldCheckers, preferences); } else if (fieldExtras.contains(FieldProperty.MULTILINE_TEXT)) { @@ -106,7 +94,7 @@ public static FieldEditorFX getForField(final String fieldName, } // default - return new SimpleEditor(fieldName, suggestionProvider, fieldCheckers, preferences, hasSingleLine); + return new SimpleEditor(fieldName, suggestionProvider, fieldCheckers, preferences, isSingleLine); } @SuppressWarnings("unchecked") diff --git a/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java b/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java index 65f23937361..f72a5cf23fe 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/PersonsEditor.java @@ -23,10 +23,10 @@ public PersonsEditor(final String fieldName, final AutoCompleteSuggestionProvider suggestionProvider, final JabRefPreferences preferences, final FieldCheckers fieldCheckers, - final boolean hasSingleLine) { + final boolean isSingleLine) { this.viewModel = new PersonsEditorViewModel(fieldName, suggestionProvider, preferences.getAutoCompletePreferences(), fieldCheckers); - textInput = hasSingleLine + textInput = isSingleLine ? new EditorTextField() : new EditorTextArea(); HBox.setHgrow(textInput, Priority.ALWAYS); diff --git a/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java b/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java index 6709c4c4d2d..de42b9e0d73 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/SimpleEditor.java @@ -22,10 +22,10 @@ public SimpleEditor(final String fieldName, final AutoCompleteSuggestionProvider suggestionProvider, final FieldCheckers fieldCheckers, final JabRefPreferences preferences, - final boolean hasSingleLine) { + final boolean isSingleLine) { this.viewModel = new SimpleEditorViewModel(fieldName, suggestionProvider, fieldCheckers); - TextInputControl textInput = hasSingleLine + TextInputControl textInput = isSingleLine ? new EditorTextField() : new EditorTextArea(); HBox.setHgrow(textInput, Priority.ALWAYS); diff --git a/src/main/java/org/jabref/model/entry/InternalBibtexFields.java b/src/main/java/org/jabref/model/entry/InternalBibtexFields.java index dfc7e097dd7..d33e73abdf8 100644 --- a/src/main/java/org/jabref/model/entry/InternalBibtexFields.java +++ b/src/main/java/org/jabref/model/entry/InternalBibtexFields.java @@ -93,6 +93,10 @@ public class InternalBibtexFields { SpecialField.RELEVANCE.getFieldName() ); + private static final Set SINGLE_LINE_FIELDS = Collections.unmodifiableSet(new HashSet<>( + Arrays.asList(FieldName.TITLE, FieldName.AUTHOR, FieldName.YEAR, FieldName.INSTITUTION) + )); + // singleton instance private static InternalBibtexFields RUNTIME = new InternalBibtexFields(); @@ -475,4 +479,8 @@ public static List getIEEETranBSTctlYesNoFields() { private void add(BibtexSingleField field) { fieldSet.put(field.getName(), field); } + + public static boolean isSingleLineField(final String fieldName) { + return SINGLE_LINE_FIELDS.contains(fieldName.toLowerCase()); + } }