Skip to content

Commit 8f513c1

Browse files
pluto-hanSiedlerchrcalixtussubhramit
authored
Fix: Unable to remove multiline property from field (#15242)
* previous work on toggling multiline/singleline * remove multiline property from field * update CHANGELOG.md * modify javadoc * move DEFAULT_MULTILINE_FIELDS to StandardField.java * refactor FieldPreferences * fix "reset preference" would not reset multiline property * remove improper codes * Add comments at construct method * Change variable name * Update jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> * Remove comments * Extract strings as constants * Resolve merge conflict --------- Co-authored-by: Christoph <siedlerkiller@gmail.com> Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Co-authored-by: Subhramit Basu <subhramit.bb@live.in>
1 parent 394ce8d commit 8f513c1

File tree

8 files changed

+106
-12
lines changed

8 files changed

+106
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
4343

4444
### Fixed
4545

46+
- We fixed an issue where multiline property of fields could not be removed properly. [#11897](https://github.com/JabRef/jabref/issues/11897)
4647
- We fixed an issue where entries were being deselected after entering a BibTeX source and clicking elsewhere. [#15412](https://github.com/JabRef/jabref/issues/15412)
4748
- We fixed pages checker to allow suffix letters in the page range like "436S-439S". [#13701](https://github.com/JabRef/jabref/issues/13701)
4849
- We fixed an issue where the Web search table had extra space, to improve the layout. [#14556](https://github.com/JabRef/jabref/issues/14556)

jabgui/src/main/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTab.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.stream.Collectors;
44

55
import javafx.application.Platform;
6+
import javafx.beans.property.BooleanProperty;
67
import javafx.beans.property.ReadOnlyStringWrapper;
78
import javafx.collections.ObservableList;
89
import javafx.fxml.FXML;
@@ -184,7 +185,7 @@ private void setupFieldsTable() {
184185
makeRotatedColumnHeader(fieldTypeColumn, Localization.lang("Required"));
185186

186187
fieldTypeMultilineColumn.setCellFactory(CheckBoxTableCell.forTableColumn(fieldTypeMultilineColumn));
187-
fieldTypeMultilineColumn.setCellValueFactory(item -> item.getValue().multilineProperty());
188+
fieldTypeMultilineColumn.setCellValueFactory(this::createMultilinePropertyListener);
188189
makeRotatedColumnHeader(fieldTypeMultilineColumn, Localization.lang("Multiline"));
189190

190191
fieldTypeActionColumn.setSortable(false);
@@ -304,10 +305,24 @@ void resetEntryTypes() {
304305
Localization.lang("Reset to default"));
305306
if (reset) {
306307
viewModel.resetAllCustomEntryTypes();
308+
viewModel.resetMultilineFieldsToDefault();
307309
fields.getSelectionModel().clearSelection();
308310
entryTypesTable.getSelectionModel().clearSelection();
309311
viewModel.setValues();
310312
entryTypesTable.refresh();
311313
}
312314
}
315+
316+
/// For multiline property, fields with the same name in each entry type will be updated as the standard fields are global.
317+
private BooleanProperty createMultilinePropertyListener(TableColumn.CellDataFeatures<FieldViewModel, Boolean> item) {
318+
BooleanProperty property = item.getValue().multilineProperty();
319+
property.addListener((obs, wasSelected, isSelected) -> {
320+
viewModel.entryTypes().forEach(typeViewModel -> {
321+
typeViewModel.fields().stream()
322+
.filter(field -> field.displayNameProperty().get().equals(item.getValue().displayNameProperty().get()))
323+
.forEach(field -> field.multilineProperty().set(isSelected));
324+
});
325+
});
326+
return property;
327+
}
313328
}

jabgui/src/main/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTabViewModel.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.jabref.gui.DialogService;
2121
import org.jabref.gui.preferences.PreferenceTabViewModel;
22+
import org.jabref.logic.bibtex.FieldPreferences;
2223
import org.jabref.logic.l10n.Localization;
2324
import org.jabref.logic.preferences.CliPreferences;
2425
import org.jabref.logic.util.strings.StringUtil;
@@ -32,6 +33,7 @@
3233
import org.jabref.model.entry.field.FieldProperty;
3334
import org.jabref.model.entry.field.FieldTextMapper;
3435
import org.jabref.model.entry.field.OrFields;
36+
import org.jabref.model.entry.field.StandardField;
3537
import org.jabref.model.entry.field.UnknownField;
3638
import org.jabref.model.entry.types.EntryType;
3739
import org.jabref.model.entry.types.UnknownEntryType;
@@ -87,6 +89,7 @@ public CustomEntryTypesTabViewModel(BibDatabaseMode mode,
8789
public void setValues() {
8890
if (!this.entryTypesWithFields.isEmpty()) {
8991
this.entryTypesWithFields.clear();
92+
this.resetMultilineFieldsToDefault();
9093
}
9194
Collection<BibEntryType> allTypes = entryTypesManager.getAllTypes(bibDatabaseMode);
9295

@@ -218,4 +221,22 @@ public ValidationStatus entryTypeValidationStatus() {
218221
public ValidationStatus fieldValidationStatus() {
219222
return fieldValidator.getValidationStatus();
220223
}
224+
225+
public void resetMultilineFieldsToDefault() {
226+
resetStandardFieldMultilineToDefaults();
227+
List<Field> defaultNonWrappableFields = FieldPreferences.getDefault().getNonWrappableFields();
228+
preferences.getFieldPreferences().setNonWrappableFields(defaultNonWrappableFields);
229+
multiLineFields.clear();
230+
multiLineFields.addAll(defaultNonWrappableFields);
231+
}
232+
233+
private void resetStandardFieldMultilineToDefaults() {
234+
for (StandardField field : StandardField.values()) {
235+
if (StandardField.BUILT_IN_MULTILINE_FIELDS.contains(field)) {
236+
field.getProperties().add(FieldProperty.MULTILINE_TEXT);
237+
} else {
238+
field.getProperties().remove(FieldProperty.MULTILINE_TEXT);
239+
}
240+
}
241+
}
221242
}

jabgui/src/main/java/org/jabref/gui/preferences/customentrytypes/FieldViewModel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public Field toField(EntryType type) {
6363
Field field = FieldFactory.parseField(type, displayName.getValue());
6464
if (multiline.getValue()) {
6565
field.getProperties().add(FieldProperty.MULTILINE_TEXT);
66+
} else {
67+
field.getProperties().remove(FieldProperty.MULTILINE_TEXT);
6668
}
6769
return field;
6870
}

jabgui/src/test/java/org/jabref/gui/preferences/customentrytypes/CustomEntryTypesTabViewModelTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.jabref.model.entry.BibEntryType;
1313
import org.jabref.model.entry.BibEntryTypeBuilder;
1414
import org.jabref.model.entry.BibEntryTypesManager;
15+
import org.jabref.model.entry.field.FieldProperty;
1516
import org.jabref.model.entry.field.StandardField;
1617
import org.jabref.model.entry.types.BiblatexEntryTypeDefinitions;
1718
import org.jabref.model.entry.types.StandardEntryType;
@@ -22,6 +23,8 @@
2223
import org.junit.jupiter.params.provider.EnumSource;
2324

2425
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
2528
import static org.mockito.Mockito.mock;
2629
import static org.mockito.Mockito.when;
2730

@@ -92,4 +95,24 @@ void storeSettingsUpdatesType() {
9295
TreeSet<BibEntryType> expected = new TreeSet<>(List.of(modified));
9396
assertEquals(expected, entryTypesManager.getAllCustomizedTypes(BibDatabaseMode.BIBLATEX));
9497
}
98+
99+
@Test
100+
void resetMultilinePropertyOfStandardFieldsToDefault() {
101+
CustomEntryTypesTabViewModel model = new CustomEntryTypesTabViewModel(BibDatabaseMode.BIBLATEX, entryTypesManager, mock(DialogService.class), preferences);
102+
model.resetMultilineFieldsToDefault();
103+
104+
StandardField fieldTitle = StandardField.TITLE;
105+
assertFalse(fieldTitle.getProperties().contains(FieldProperty.MULTILINE_TEXT));
106+
fieldTitle.getProperties().add(FieldProperty.MULTILINE_TEXT);
107+
assertTrue(fieldTitle.getProperties().contains(FieldProperty.MULTILINE_TEXT));
108+
model.resetMultilineFieldsToDefault();
109+
assertFalse(fieldTitle.getProperties().contains(FieldProperty.MULTILINE_TEXT));
110+
111+
StandardField fieldAbstract = StandardField.ABSTRACT;
112+
assertTrue(fieldAbstract.getProperties().contains(FieldProperty.MULTILINE_TEXT));
113+
fieldAbstract.getProperties().remove(FieldProperty.MULTILINE_TEXT);
114+
assertFalse(fieldAbstract.getProperties().contains(FieldProperty.MULTILINE_TEXT));
115+
model.resetMultilineFieldsToDefault();
116+
assertTrue(fieldAbstract.getProperties().contains(FieldProperty.MULTILINE_TEXT));
117+
}
95118
}

jablib/src/main/java/org/jabref/logic/bibtex/FieldPreferences.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import javafx.collections.ObservableList;
1010

1111
import org.jabref.model.entry.field.Field;
12+
import org.jabref.model.entry.field.FieldFactory;
1213

1314
public class FieldPreferences {
15+
private static final String DEFAULT_RESOLVABLE_FIELDS = "author;booktitle;editor;editora;editorb;editorc;institution;issuetitle;journal;journalsubtitle;journaltitle;mainsubtitle;month;monthfiled;publisher;shortauthor;shorteditor;subtitle;titleaddon";
16+
private static final String DEFAULT_NON_WRAPPABLE_FIELDS = "pdf;ps;url;doi;file;isbn;issn";
1417

1518
private final BooleanProperty resolveStrings = new SimpleBooleanProperty();
1619
private final ObservableList<Field> resolvableFields;
@@ -25,6 +28,27 @@ public FieldPreferences(boolean resolveStrings,
2528
this.nonWrappableFields = FXCollections.observableArrayList(nonWrappableFields);
2629
}
2730

31+
private FieldPreferences() {
32+
this(
33+
// Default resolve string setting
34+
true,
35+
// Default resolvable field
36+
List.copyOf(FieldFactory.parseFieldList(DEFAULT_RESOLVABLE_FIELDS)),
37+
// Default non wrappable field
38+
List.copyOf(FieldFactory.parseFieldList(DEFAULT_NON_WRAPPABLE_FIELDS))
39+
);
40+
}
41+
42+
public static FieldPreferences getDefault() {
43+
return new FieldPreferences();
44+
}
45+
46+
public void setAll(FieldPreferences preferences) {
47+
setResolveStrings(preferences.shouldResolveStrings());
48+
setResolvableFields(preferences.getResolvableFields());
49+
setNonWrappableFields(preferences.getNonWrappableFields());
50+
}
51+
2852
public boolean shouldResolveStrings() {
2953
return resolveStrings.get();
3054
}

jablib/src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,6 @@ public JabRefCliPreferences() {
628628
defaults.put(OPEN_FILE_EXPLORER_IN_LAST_USED_DIRECTORY, Boolean.FALSE);
629629
defaults.put(DEFAULT_CITATION_KEY_PATTERN, "[auth][year]");
630630
defaults.put(UNWANTED_CITATION_KEY_CHARACTERS, "-`ʹ:!;?^$");
631-
defaults.put(RESOLVE_STRINGS_FOR_FIELDS, "author;booktitle;editor;editora;editorb;editorc;institution;issuetitle;journal;journalsubtitle;journaltitle;mainsubtitle;month;monthfiled;publisher;shortauthor;shorteditor;subtitle;titleaddon");
632-
defaults.put(DO_NOT_RESOLVE_STRINGS, Boolean.FALSE);
633-
defaults.put(NON_WRAPPABLE_FIELDS, "pdf;ps;url;doi;file;isbn;issn");
634631
defaults.put(WARN_ABOUT_DUPLICATES_IN_INSPECTION, Boolean.TRUE);
635632
defaults.put(ADD_CREATION_DATE, Boolean.FALSE);
636633
defaults.put(ADD_MODIFICATION_DATE, Boolean.FALSE);
@@ -1032,6 +1029,7 @@ public void clear() throws BackingStoreException {
10321029

10331030
getProxyPreferences().setAll(ProxyPreferences.getDefault());
10341031
getPushToApplicationPreferences().setAll(PushToApplicationPreferences.getDefault());
1032+
getFieldPreferences().setAll(FieldPreferences.getDefault());
10351033
}
10361034

10371035
/// Imports Preferences from an XML file.
@@ -1046,6 +1044,8 @@ public void importPreferences(Path path) throws JabRefException {
10461044
// See org.jabref.gui.preferences.JabRefGuiPreferences.importPreferences for the GUI
10471045

10481046
// in case of incomplete or corrupt xml fall back to current preferences
1047+
getProxyPreferences().setAll(getProxyPreferencesFromBackingStore(ProxyPreferences.getDefault()));
1048+
getFieldPreferences().setAll(getFieldPreferencesFromBackingStore(FieldPreferences.getDefault()));
10491049
getProxyPreferences().setAll(getProxyPreferencesFromBackingStore(getProxyPreferences()));
10501050
getPushToApplicationPreferences().setAll(getPushToApplicationPreferencesFromBackingStore(getPushToApplicationPreferences()));
10511051
}
@@ -1590,14 +1590,7 @@ public FieldPreferences getFieldPreferences() {
15901590
return fieldPreferences;
15911591
}
15921592

1593-
fieldPreferences = new FieldPreferences(
1594-
!getBoolean(DO_NOT_RESOLVE_STRINGS), // mind the !
1595-
getStringList(RESOLVE_STRINGS_FOR_FIELDS).stream()
1596-
.map(FieldFactory::parseField)
1597-
.collect(Collectors.toList()),
1598-
getStringList(NON_WRAPPABLE_FIELDS).stream()
1599-
.map(FieldFactory::parseField)
1600-
.collect(Collectors.toList()));
1593+
fieldPreferences = getFieldPreferencesFromBackingStore(FieldPreferences.getDefault());
16011594

16021595
EasyBind.listen(fieldPreferences.resolveStringsProperty(), (_, _, newValue) -> putBoolean(DO_NOT_RESOLVE_STRINGS, !newValue));
16031596
fieldPreferences.getResolvableFields().addListener((InvalidationListener) _ ->
@@ -1608,6 +1601,16 @@ public FieldPreferences getFieldPreferences() {
16081601
return fieldPreferences;
16091602
}
16101603

1604+
private FieldPreferences getFieldPreferencesFromBackingStore(FieldPreferences defaults) {
1605+
return new FieldPreferences(
1606+
!getBoolean(DO_NOT_RESOLVE_STRINGS, !defaults.shouldResolveStrings()),
1607+
List.copyOf(FieldFactory.parseFieldList(get(RESOLVE_STRINGS_FOR_FIELDS,
1608+
FieldFactory.serializeFieldsList(defaults.getResolvableFields())))),
1609+
List.copyOf(FieldFactory.parseFieldList(get(NON_WRAPPABLE_FIELDS,
1610+
FieldFactory.serializeFieldsList(defaults.getNonWrappableFields()))))
1611+
);
1612+
}
1613+
16111614
// region Linked files preferences
16121615
protected boolean moveToTrashSupported() {
16131616
return false;

jablib/src/main/java/org/jabref/model/entry/field/StandardField.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public enum StandardField implements Field {
149149
// endregion
150150

151151
public static final Set<Field> AUTOMATIC_FIELDS = Set.of(OWNER, TIMESTAMP, CREATIONDATE, MODIFICATIONDATE);
152+
public static final Set<StandardField> BUILT_IN_MULTILINE_FIELDS = Set.of(
153+
ABSTRACT,
154+
COMMENT,
155+
REVIEW
156+
);
152157

153158
private static final Map<String, StandardField> NAME_TO_STANDARD_FIELD = new HashMap<>();
154159

0 commit comments

Comments
 (0)