Skip to content

Commit

Permalink
Refactored for more clean-ups and naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Aug 23, 2020
1 parent e5ea359 commit fac9f99
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 39 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,11 @@ private void automaticallySetFileLinks(List<ParserResult> loaded) {
for (ParserResult parserResult : loaded) {
BibDatabase database = parserResult.getDatabase();
LOGGER.info(Localization.lang("Automatically setting file links"));
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(parserResult.getDatabaseContext(), Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
parserResult.getDatabaseContext(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
util.linkAssociatedFiles(database.getEntries(), new NamedCompound(""));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public void execute() {
List<BibEntry> entries = stateManager.getSelectedEntries();

final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(database, preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
Task<List<BibEntry>> linkFilesTask = new Task<List<BibEntry>>() {
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
database,
preferences.getFilePreferences(),
preferences.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
Task<List<BibEntry>> linkFilesTask = new Task<>() {

@Override
protected List<BibEntry> call() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ public LinkedFileViewModel fromFile(Path file) {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences());

LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes);
return new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes);
return new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes);
}

public boolean isFulltextLookupInProgress() {
Expand All @@ -114,7 +122,15 @@ public BooleanProperty fulltextLookupInProgressProperty() {

private List<LinkedFileViewModel> parseToFileViewModel(String stringValue) {
return FileFieldParser.parse(stringValue).stream()
.map(linkedFile -> new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes))
.map(linkedFile -> new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes))
.collect(Collectors.toList());
}

Expand All @@ -137,7 +153,15 @@ public void addNewFile() {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences());
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> {
LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes);
files.add(new LinkedFileViewModel(newLinkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes));
files.add(new LinkedFileViewModel(
newLinkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes));
});
}

Expand All @@ -159,11 +183,23 @@ public void bindToEntry(BibEntry entry) {
private List<LinkedFileViewModel> findAssociatedNotLinkedFiles(BibEntry entry) {
List<LinkedFileViewModel> result = new ArrayList<>();

AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance());
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
databaseContext,
preferences.getFilePreferences(),
preferences.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
try {
List<LinkedFile> linkedFiles = util.findAssociatedNotLinkedFiles(entry);
for (LinkedFile linkedFile : linkedFiles) {
LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes);
LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes);
newLinkedFile.markAsAutomaticallyFound();
result.add(newLinkedFile);
}
Expand Down Expand Up @@ -206,7 +242,15 @@ public void addFromURL() {
}

private void addFromURL(URL url) {
LinkedFileViewModel onlineFile = new LinkedFileViewModel(new LinkedFile(url, ""), entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes);
LinkedFileViewModel onlineFile = new LinkedFileViewModel(
new LinkedFile(url, ""),
entry,
databaseContext,
taskExecutor,
dialogService,
preferences.getXMPPreferences(),
preferences.getFilePreferences(),
externalFileTypes);
files.add(onlineFile);
onlineFile.download();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void setValues() {

@Override
public void storeSettings() {

// NO TITLE
preferences.putBoolean(JabRefPreferences.OPEN_LAST_EDITED, openLastStartupProperty.getValue());
if (!noWrapFilesProperty.getValue().trim().equals(preferences.get(JabRefPreferences.NON_WRAPPABLE_FIELDS))) {
preferences.put(JabRefPreferences.NON_WRAPPABLE_FIELDS, noWrapFilesProperty.getValue());
Expand All @@ -112,16 +114,18 @@ public void storeSettings() {
if (autolinkUseRegexProperty.getValue()) {
preferences.put(JabRefPreferences.AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY, autolinkRegexKeyProperty.getValue());
}
preferences.setNewLineSeparator(selectedNewLineSeparatorProperty.getValue());
preferences.storeNewLineSeparator(selectedNewLineSeparatorProperty.getValue());
preferences.putBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT, alwaysReformatBibProperty.getValue());

// EXTERNAL FILE LINKS
preferences.put(JabRefPreferences.MAIN_FILE_DIRECTORY, mainFileDirProperty.getValue());
preferences.putBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR, useBibLocationAsPrimaryProperty.getValue());
preferences.putBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY, autolinkUseRegexProperty.getValue());
preferences.putBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY, autolinkFileExactBibtexProperty.getValue());

preferences.putBoolean(JabRefPreferences.RUN_AUTOMATIC_FILE_SEARCH, searchFilesOnOpenProperty.getValue());
preferences.putBoolean(JabRefPreferences.ALLOW_FILE_AUTO_OPEN_BROWSE, openBrowseOnCreateProperty.getValue());

// Autosave
preferences.putBoolean(JabRefPreferences.LOCAL_AUTO_SAVE, autosaveLocalLibraries.getValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void importPreferences() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.XML)
.withDefaultExtension(StandardFileType.XML)
.withInitialDirectory(preferences.setLastPreferencesExportPath()).build();
.withInitialDirectory(preferences.getLastPreferencesExportPath()).build();

dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> {
try {
Expand All @@ -89,15 +89,15 @@ public void exportPreferences() {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(StandardFileType.XML)
.withDefaultExtension(StandardFileType.XML)
.withInitialDirectory(preferences.setLastPreferencesExportPath())
.withInitialDirectory(preferences.getLastPreferencesExportPath())
.build();

dialogService.showFileSaveDialog(fileDialogConfiguration)
.ifPresent(exportFile -> {
try {
storeAllSettings();
preferences.exportPreferences(exportFile);
preferences.setLastPreferencesExportPath(exportFile);
preferences.storeLastPreferencesExportPath(exportFile);
} catch (JabRefException ex) {
LOGGER.warn(ex.getMessage(), ex);
dialogService.showErrorDialogAndWait(Localization.lang("Export preferences"), ex);
Expand Down
57 changes: 32 additions & 25 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1458,14 +1458,6 @@ public Optional<Integer> getFontSize() {
}
}

public String setLastPreferencesExportPath() {
return get(PREFS_EXPORT_PATH);
}

public void setLastPreferencesExportPath(Path exportFile) {
put(PREFS_EXPORT_PATH, exportFile.toString());
}

public void setIdBasedFetcherForEntryGenerator(String fetcherName) {
put(ID_ENTRY_GENERATOR, fetcherName);
}
Expand Down Expand Up @@ -1554,18 +1546,6 @@ private void saveCustomEntryTypes(BibDatabaseMode bibDatabaseMode, BibEntryTypes
storeBibEntryTypes(customBiblatexBibTexTypes, bibDatabaseMode);
}

public NewLineSeparator getNewLineSeparator() {
return NewLineSeparator.parse(get(JabRefPreferences.NEWLINE));
}

public void setNewLineSeparator(NewLineSeparator newLineSeparator) {
String escapeChars = newLineSeparator.toString();
put(JabRefPreferences.NEWLINE, escapeChars);

// we also have to change Globals variable as globals is not a getter, but a constant
OS.NEWLINE = escapeChars;
}

//*************************************************************************************************************
// GeneralPreferences
//*************************************************************************************************************
Expand Down Expand Up @@ -2209,6 +2189,17 @@ public void storeMainTableNameFormatPreferences(MainTableNameFormatPreferences p
// File preferences
//*************************************************************************************************************

@Override
public String getLastPreferencesExportPath() {
return get(PREFS_EXPORT_PATH);
}

@Override
public void storeLastPreferencesExportPath(Path exportFile) {
put(PREFS_EXPORT_PATH, exportFile.toString());
}

// ToDo: Can this be disbanded?
@Override
public ImportFormatPreferences getImportFormatPreferences() {
return new ImportFormatPreferences(
Expand Down Expand Up @@ -2252,12 +2243,20 @@ public SavePreferences getSavePreferences() {
getCitationKeyPatternPreferences());
}


@Override
public FieldWriterPreferences getFieldWriterPreferences() {
return new FieldWriterPreferences(
getBoolean(RESOLVE_STRINGS_ALL_FIELDS),
getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()),
getFieldContentParserPreferences());
public NewLineSeparator getNewLineSeparator() {
return NewLineSeparator.parse(get(JabRefPreferences.NEWLINE));
}

// ToDo: Can this be disbanded?
@Override
public void storeNewLineSeparator(NewLineSeparator newLineSeparator) {
String escapeChars = newLineSeparator.toString();
put(JabRefPreferences.NEWLINE, escapeChars);

// we also have to change Globals variable as globals is not a getter, but a constant
OS.NEWLINE = escapeChars;
}

@Override
Expand All @@ -2266,6 +2265,14 @@ public FieldContentFormatterPreferences getFieldContentParserPreferences() {
getStringList(NON_WRAPPABLE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toList()));
}

@Override
public FieldWriterPreferences getFieldWriterPreferences() {
return new FieldWriterPreferences(
getBoolean(RESOLVE_STRINGS_ALL_FIELDS),
getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()),
getFieldContentParserPreferences());
}

@Override
public FileHistory getFileHistory() {
return new FileHistory(getStringList(RECENT_DATABASES).stream().map(Path::of).collect(Collectors.toList()));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ public interface PreferencesService {

void storeMainTableNameFormatPreferences(MainTableNameFormatPreferences preferences);

//*************************************************************************************************************
// File preferences
//*************************************************************************************************************

NewLineSeparator getNewLineSeparator();

void storeNewLineSeparator(NewLineSeparator newLineSeparator);

void storeLastPreferencesExportPath(Path exportFile);

String getLastPreferencesExportPath();

//*************************************************************************************************************
// ToDo: Misc preferences
//*************************************************************************************************************
Expand Down

0 comments on commit fac9f99

Please sign in to comment.