Skip to content

Commit fe22fcb

Browse files
author
rahulkarru
committed
Refactor: Extract NewEntryPreferences from JabRefPreferences
1 parent 939b018 commit fe22fcb

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

jabgui/src/main/java/org/jabref/gui/newentry/NewEntryPreferences.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import javafx.beans.property.StringProperty;
99

1010
import org.jabref.model.entry.types.EntryType;
11+
/*these were added*/
12+
import org.jabref.model.entry.types.StandardEntryType;
13+
import org.jabref.logic.importer.fetcher.DoiFetcher;
14+
import org.jabref.logic.importer.plaincitation.PlainCitationParserChoice;
15+
/*-----*/
1116

1217
public class NewEntryPreferences {
1318
private final ObjectProperty<NewEntryDialogTab> latestApproach;
@@ -18,7 +23,25 @@ public class NewEntryPreferences {
1823
private final BooleanProperty idLookupGuessing;
1924
private final StringProperty latestIdFetcherName;
2025
private final StringProperty latestInterpretParserName;
26+
private NewEntryPreferences(){
27+
this(
28+
NewEntryDialogTab.CHOOSE_ENTRY_TYPE,
2129

30+
true,
31+
32+
false,
33+
34+
true,
35+
36+
StandardEntryType.Article,
37+
38+
true,
39+
40+
DoiFetcher.NAME,
41+
42+
PlainCitationParserChoice.RULE_BASED_GENERAL.getLocalizedName()
43+
);
44+
}
2245
public NewEntryPreferences(NewEntryDialogTab approach,
2346
boolean expandRecommended,
2447
boolean expandOther,
@@ -36,7 +59,19 @@ public NewEntryPreferences(NewEntryDialogTab approach,
3659
this.latestIdFetcherName = new SimpleStringProperty(idFetcherName);
3760
this.latestInterpretParserName = new SimpleStringProperty(interpretParserName);
3861
}
39-
62+
public static NewEntryPreferences getDefault(){
63+
return new NewEntryPreferences();
64+
}
65+
public void setAll(NewEntryPreferences other) {
66+
this.latestApproach.set(other.getLatestApproach());
67+
this.typesRecommendedExpanded.set(other.getTypesRecommendedExpanded());
68+
this.typesOtherExpanded.set(other.getTypesOtherExpanded());
69+
this.typesCustomExpanded.set(other.getTypesCustomExpanded());
70+
this.latestImmediateType.set(other.getLatestImmediateType());
71+
this.idLookupGuessing.set(other.getIdLookupGuessing());
72+
this.latestIdFetcherName.set(other.getLatestIdFetcher());
73+
this.latestInterpretParserName.set(other.getLatestInterpretParser());
74+
}
4075
public NewEntryDialogTab getLatestApproach() {
4176
return latestApproach.get();
4277
}
@@ -53,7 +88,7 @@ public boolean getTypesRecommendedExpanded() {
5388
return typesRecommendedExpanded.get();
5489
}
5590

56-
public void getTypesRecommendedExpanded(boolean expanded) {
91+
public void setTypesRecommendedExpanded(boolean expanded) {
5792
typesRecommendedExpanded.set(expanded);
5893
}
5994

jabgui/src/main/java/org/jabref/gui/preferences/JabRefGuiPreferences.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import org.jabref.model.groups.GroupHierarchyType;
7575
import org.jabref.model.metadata.SaveOrder;
7676
import org.jabref.model.metadata.SelfContainedSaveOrder;
77-
77+
import org.jabref.model.entry.types.EntryTypeFactory;
7878
import com.airhacks.afterburner.injection.Injector;
7979
import com.tobiasdiez.easybind.EasyBind;
8080
import org.slf4j.Logger;
@@ -368,16 +368,7 @@ private JabRefGuiPreferences() {
368368
defaults.put(DONATION_LAST_SHOWN_EPOCH_DAY, -1);
369369
// endregion
370370

371-
// region NewEntryUnifierPreferences
372-
defaults.put(CREATE_ENTRY_APPROACH, List.of(NewEntryDialogTab.values()).indexOf(NewEntryDialogTab.CHOOSE_ENTRY_TYPE));
373-
defaults.put(CREATE_ENTRY_EXPAND_RECOMMENDED, true);
374-
defaults.put(CREATE_ENTRY_EXPAND_OTHER, false);
375-
defaults.put(CREATE_ENTRY_EXPAND_CUSTOM, true);
376-
defaults.put(CREATE_ENTRY_IMMEDIATE_TYPE, StandardEntryType.Article.getDisplayName());
377-
defaults.put(CREATE_ENTRY_ID_LOOKUP_GUESSING, true);
378-
defaults.put(CREATE_ENTRY_ID_FETCHER_NAME, DoiFetcher.NAME);
379-
defaults.put(CREATE_ENTRY_INTERPRET_PARSER_NAME, PlainCitationParserChoice.RULE_BASED_GENERAL.getLocalizedName());
380-
// endregion
371+
381372
}
382373

383374
/**
@@ -414,6 +405,8 @@ public void clear() throws BackingStoreException {
414405

415406
getWorkspacePreferences().setAll(WorkspacePreferences.getDefault());
416407
getGuiPreferences().setAll(CoreGuiPreferences.getDefault());
408+
getNewEntryPreferences().setAll(NewEntryPreferences.getDefault());
409+
417410
}
418411

419412
@Override
@@ -423,6 +416,8 @@ public void importPreferences(Path file) throws JabRefException {
423416
// in case of incomplete or corrupt xml fall back to current preferences
424417
getWorkspacePreferences().setAll(getWorkspacePreferencesFromBackingStore(getWorkspacePreferences()));
425418
getGuiPreferences().setAll(getCoreGuiPreferencesFromBackingStore(getGuiPreferences()));
419+
getNewEntryPreferences().setAll(getNewEntryPreferencesFromLowLevelApi(getNewEntryPreferences()));
420+
426421
}
427422

428423
// region EntryEditorPreferences
@@ -686,6 +681,30 @@ private WorkspacePreferences getWorkspacePreferencesFromBackingStore(WorkspacePr
686681
getStringList(SELECTED_SLR_CATALOGS));
687682
}
688683

684+
private NewEntryPreferences getNewEntryPreferencesFromLowLevelApi(NewEntryPreferences defaults) {
685+
int approachIndex = getInt("createEntryApproach", defaults.getLatestApproach().ordinal());
686+
NewEntryDialogTab approach;
687+
if (approachIndex >= 0 && approachIndex < NewEntryDialogTab.values().length) {
688+
approach = NewEntryDialogTab.values()[approachIndex];
689+
} else {
690+
approach = defaults.getLatestApproach();
691+
}
692+
693+
String typeName = get("createEntryImmediateType", defaults.getLatestImmediateType().getDisplayName());
694+
EntryType immediateType = EntryTypeFactory.parse(typeName);
695+
696+
return new NewEntryPreferences(
697+
approach,
698+
getBoolean("createEntryExpandRecommended", defaults.getTypesRecommendedExpanded()),
699+
getBoolean("createEntryExpandOther", defaults.getTypesOtherExpanded()),
700+
getBoolean("createEntryExpandCustom", defaults.getTypesCustomExpanded()),
701+
immediateType,
702+
getBoolean("createEntryIdLookupGuessing", defaults.getIdLookupGuessing()),
703+
get("createEntryIdFetcherName", defaults.getLatestIdFetcher()),
704+
get("createEntryInterpretParserName", defaults.getLatestInterpretParser())
705+
);
706+
}
707+
689708
@Override
690709
public UnlinkedFilesDialogPreferences getUnlinkedFilesDialogPreferences() {
691710
if (unlinkedFilesDialogPreferences != null) {
@@ -1190,15 +1209,8 @@ public NewEntryPreferences getNewEntryPreferences() {
11901209
}
11911210
}
11921211

1193-
newEntryPreferences = new NewEntryPreferences(
1194-
approach,
1195-
getBoolean(CREATE_ENTRY_EXPAND_RECOMMENDED),
1196-
getBoolean(CREATE_ENTRY_EXPAND_OTHER),
1197-
getBoolean(CREATE_ENTRY_EXPAND_CUSTOM),
1198-
immediateType,
1199-
getBoolean(CREATE_ENTRY_ID_LOOKUP_GUESSING),
1200-
get(CREATE_ENTRY_ID_FETCHER_NAME),
1201-
get(CREATE_ENTRY_INTERPRET_PARSER_NAME));
1212+
newEntryPreferences = getNewEntryPreferencesFromLowLevelApi(NewEntryPreferences.getDefault());
1213+
12021214

12031215
EasyBind.listen(newEntryPreferences.latestApproachProperty(), (_, _, newValue) -> putInt(CREATE_ENTRY_APPROACH, List.of(NewEntryDialogTab.values()).indexOf(newValue)));
12041216
EasyBind.listen(newEntryPreferences.typesRecommendedExpandedProperty(), (_, _, newValue) -> putBoolean(CREATE_ENTRY_EXPAND_RECOMMENDED, newValue));

0 commit comments

Comments
 (0)