Skip to content

Commit

Permalink
Refactored and extended AutoLinkPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Aug 23, 2020
1 parent fac9f99 commit 1fb6e7a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 48 deletions.
48 changes: 33 additions & 15 deletions src/main/java/org/jabref/gui/preferences/FileTabViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.AutoLinkPreferences;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.NewLineSeparator;

Expand Down Expand Up @@ -52,10 +53,12 @@ public class FileTabViewModel implements PreferenceTabViewModel {

private final DialogService dialogService;
private final JabRefPreferences preferences;
private final AutoLinkPreferences initialAutoLinkPreferences;

public FileTabViewModel(DialogService dialogService, JabRefPreferences preferences) {
this.dialogService = dialogService;
this.preferences = preferences;
this.initialAutoLinkPreferences = preferences.getAutoLinkPreferences();

mainFileDirValidator = new FunctionBasedValidator<>(
mainFileDirProperty,
Expand Down Expand Up @@ -86,16 +89,23 @@ public void setValues() {

mainFileDirProperty.setValue(preferences.getAsOptional(JabRefPreferences.MAIN_FILE_DIRECTORY).orElse(""));
useBibLocationAsPrimaryProperty.setValue(preferences.getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR));
if (preferences.getBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY)) { // Flipped around
autolinkUseRegexProperty.setValue(true);
} else if (preferences.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY)) {
autolinkFileExactBibtexProperty.setValue(true);
} else {
autolinkFileStartsBibtexProperty.setValue(true);

// Autolink preferences
switch (initialAutoLinkPreferences.getCitationKeyDependency()) {
default:
case START:
autolinkFileStartsBibtexProperty.setValue(true);
break;
case EXACT:
autolinkFileExactBibtexProperty.setValue(true);
break;
case REGEX:
autolinkUseRegexProperty.setValue(true);
break;
}
autolinkRegexKeyProperty.setValue(preferences.get(JabRefPreferences.AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY));
searchFilesOnOpenProperty.setValue(preferences.getBoolean(JabRefPreferences.RUN_AUTOMATIC_FILE_SEARCH));
openBrowseOnCreateProperty.setValue(preferences.getBoolean(JabRefPreferences.ALLOW_FILE_AUTO_OPEN_BROWSE));
autolinkRegexKeyProperty.setValue(initialAutoLinkPreferences.getRegularExpression());
searchFilesOnOpenProperty.setValue(initialAutoLinkPreferences.shouldSearchFilesOnOpen());
openBrowseOnCreateProperty.setValue(initialAutoLinkPreferences.shouldOpenBrowseOnCreate());

autosaveLocalLibraries.setValue(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE));
}
Expand All @@ -111,19 +121,27 @@ public void storeSettings() {
preferences.putBoolean(JabRefPreferences.RESOLVE_STRINGS_ALL_FIELDS, resolveStringsAllProperty.getValue());
preferences.put(JabRefPreferences.DO_NOT_RESOLVE_STRINGS_FOR, resolveStringsExceptProperty.getValue().trim());
resolveStringsExceptProperty.setValue(preferences.get(JabRefPreferences.DO_NOT_RESOLVE_STRINGS_FOR));
if (autolinkUseRegexProperty.getValue()) {
preferences.put(JabRefPreferences.AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY, autolinkRegexKeyProperty.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.RUN_AUTOMATIC_FILE_SEARCH, searchFilesOnOpenProperty.getValue());
preferences.putBoolean(JabRefPreferences.ALLOW_FILE_AUTO_OPEN_BROWSE, openBrowseOnCreateProperty.getValue());
// Autolink preferences
AutoLinkPreferences.CitationKeyDependency citationKeyDependency = AutoLinkPreferences.CitationKeyDependency.START;
if (autolinkFileExactBibtexProperty.getValue()) {
citationKeyDependency = AutoLinkPreferences.CitationKeyDependency.EXACT;
} else if (autolinkUseRegexProperty.getValue()) {
citationKeyDependency = AutoLinkPreferences.CitationKeyDependency.REGEX;
}

preferences.storeAutoLinkPreferences(new AutoLinkPreferences(
citationKeyDependency,
autolinkRegexKeyProperty.getValue(),
searchFilesOnOpenProperty.getValue(),
openBrowseOnCreateProperty.getValue(),
preferences.getKeywordDelimiter()));

// Autosave
preferences.putBoolean(JabRefPreferences.LOCAL_AUTO_SAVE, autosaveLocalLibraries.getValue());
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/org/jabref/logic/util/io/AutoLinkPreferences.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
package org.jabref.logic.util.io;

public class AutoLinkPreferences {
boolean useRegularExpression;
String regularExpression;
Character keywordDelimiter;
boolean onlyFindByExactCiteKey;

public AutoLinkPreferences(boolean useRegularExpression, String regularExpression, boolean onlyFindByExactCiteKey, Character keywordDelimiter) {
this.useRegularExpression = useRegularExpression;
public enum CitationKeyDependency {
START, // Filenames starting with the citation key
EXACT, // Filenames exactly matching the citation key
REGEX // Filenames matching a regular expression pattern
}

private final CitationKeyDependency citationKeyDependency;
private final String regularExpression;
private final boolean shouldSearchFilesOnOpen;
private final boolean shouldOpenBrowseOnCreate;
private final Character keywordDelimiter;

public AutoLinkPreferences(CitationKeyDependency citationKeyDependency,
String regularExpression,
boolean shouldSearchFilesOnOpen,
boolean shouldOpenBrowseOnCreate,
Character keywordDelimiter) {
this.citationKeyDependency = citationKeyDependency;
this.regularExpression = regularExpression;
this.onlyFindByExactCiteKey = onlyFindByExactCiteKey;
this.shouldSearchFilesOnOpen = shouldSearchFilesOnOpen;
this.shouldOpenBrowseOnCreate = shouldOpenBrowseOnCreate;
this.keywordDelimiter = keywordDelimiter;
}

public boolean isUseRegularExpression() {
return useRegularExpression;
public CitationKeyDependency getCitationKeyDependency() {
return citationKeyDependency;
}

public String getRegularExpression() {
return regularExpression;
}

public boolean isOnlyFindByExactCiteKey() {
return onlyFindByExactCiteKey;
public boolean shouldSearchFilesOnOpen() {
return shouldSearchFilesOnOpen;
}

public boolean shouldOpenBrowseOnCreate() {
return shouldOpenBrowseOnCreate;
}

public Character getKeywordDelimiter() {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/jabref/logic/util/io/FileFinders.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ public class FileFinders {
* Creates a preconfigurated file finder based on the given AutoLink preferences.
*/
public static FileFinder constructFromConfiguration(AutoLinkPreferences autoLinkPreferences) {
if (autoLinkPreferences.isUseRegularExpression()) {
return new RegExpBasedFileFinder(autoLinkPreferences.getRegularExpression(), autoLinkPreferences.getKeywordDelimiter());
} else {
return new CitationKeyBasedFileFinder(autoLinkPreferences.isOnlyFindByExactCiteKey());
switch (autoLinkPreferences.getCitationKeyDependency()) {
default:
case START:
return new CitationKeyBasedFileFinder(false);
case EXACT:
return new CitationKeyBasedFileFinder(true);
case REGEX:
return new RegExpBasedFileFinder(autoLinkPreferences.getRegularExpression(), autoLinkPreferences.getKeywordDelimiter());
}
}
}
64 changes: 49 additions & 15 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,12 @@ public boolean isKeywordSyncEnabled() {
&& getBoolean(JabRefPreferences.AUTOSYNCSPECIALFIELDSTOKEYWORDS);
}

private FileLinkPreferences getFileLinkPreferences() {
return new FileLinkPreferences(
get(MAIN_FILE_DIRECTORY), // REALLY HERE?
fileDirForDatabase);
}

@Override
public LayoutFormatterPreferences getLayoutFormatterPreferences(JournalAbbreviationRepository repository) {
return new LayoutFormatterPreferences(
Expand Down Expand Up @@ -1419,15 +1425,6 @@ public void storeJournalAbbreviationPreferences(JournalAbbreviationPreferences a
putStringList(JabRefPreferences.EXTERNAL_JOURNAL_LISTS, abbreviationsPreferences.getExternalJournalLists());
}

@Override
public AutoLinkPreferences getAutoLinkPreferences() {
return new AutoLinkPreferences(
getBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY),
get(JabRefPreferences.AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY),
getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY),
getKeywordDelimiter());
}

public void storeSidePanePreferredPositions(Map<SidePaneType, Integer> preferredPositions) {
// Split the map into a pair of parallel String lists suitable for storage
List<String> names = preferredPositions.keySet().stream()
Expand Down Expand Up @@ -2285,12 +2282,6 @@ public void storeFileHistory(FileHistory history) {
}
}

private FileLinkPreferences getFileLinkPreferences() {
return new FileLinkPreferences(
get(MAIN_FILE_DIRECTORY),
fileDirForDatabase);
}

@Override
public Path getWorkingDir() {
return Path.of(get(WORKING_DIRECTORY));
Expand Down Expand Up @@ -2321,6 +2312,49 @@ public void storeFilePreferences(FilePreferences filePreferences) {
putBoolean(JabRefPreferences.DOWNLOAD_LINKED_FILES, filePreferences.shouldDownloadLinkedFiles());
}

@Override
public AutoLinkPreferences getAutoLinkPreferences() {

AutoLinkPreferences.CitationKeyDependency citationKeyDependency =
AutoLinkPreferences.CitationKeyDependency.START; // default
if (getBoolean(AUTOLINK_EXACT_KEY_ONLY)) {
citationKeyDependency = AutoLinkPreferences.CitationKeyDependency.EXACT;
} else if (getBoolean(AUTOLINK_USE_REG_EXP_SEARCH_KEY)) {
citationKeyDependency = AutoLinkPreferences.CitationKeyDependency.REGEX;
}

return new AutoLinkPreferences(
citationKeyDependency,
get(AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY),
getBoolean(RUN_AUTOMATIC_FILE_SEARCH),
getBoolean(ALLOW_FILE_AUTO_OPEN_BROWSE),
getKeywordDelimiter());
}

@Override
public void storeAutoLinkPreferences(AutoLinkPreferences autoLinkPreferences) {
// Should be an enum
// Starts bibtex only omitted, as it is not being saved
switch (autoLinkPreferences.getCitationKeyDependency()) {
default:
case START:
putBoolean(AUTOLINK_EXACT_KEY_ONLY, false);
putBoolean(AUTOLINK_USE_REG_EXP_SEARCH_KEY, false);
break;
case EXACT:
putBoolean(AUTOLINK_EXACT_KEY_ONLY, true);
putBoolean(AUTOLINK_USE_REG_EXP_SEARCH_KEY, false);
break;
case REGEX:
putBoolean(AUTOLINK_EXACT_KEY_ONLY, false);
putBoolean(AUTOLINK_USE_REG_EXP_SEARCH_KEY, true);
break;
}
put(JabRefPreferences.AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY, autoLinkPreferences.getRegularExpression());
putBoolean(RUN_AUTOMATIC_FILE_SEARCH, autoLinkPreferences.shouldSearchFilesOnOpen());
putBoolean(ALLOW_FILE_AUTO_OPEN_BROWSE, autoLinkPreferences.shouldOpenBrowseOnCreate());
}

//*************************************************************************************************************
// ToDo: Misc preferences
//*************************************************************************************************************
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public interface PreferencesService {

XmpPreferences getXMPPreferences();

AutoLinkPreferences getAutoLinkPreferences();

Path getWorkingDir();

void setWorkingDir(Path dir);
Expand Down Expand Up @@ -249,6 +247,10 @@ public interface PreferencesService {

String getLastPreferencesExportPath();

AutoLinkPreferences getAutoLinkPreferences();

void storeAutoLinkPreferences(AutoLinkPreferences autoLinkPreferences);

//*************************************************************************************************************
// ToDo: Misc preferences
//*************************************************************************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
public class AutoSetFileLinksUtilTest {

private final FilePreferences fileDirPrefs = mock(FilePreferences.class);
private final AutoLinkPreferences autoLinkPrefs = new AutoLinkPreferences(false, "", true, ';');
private final AutoLinkPreferences autoLinkPrefs = new AutoLinkPreferences(
AutoLinkPreferences.CitationKeyDependency.REGEX,
"",
false,
false,
';');
private final BibDatabaseContext databaseContext = mock(BibDatabaseContext.class);
private final ExternalFileTypes externalFileTypes = mock(ExternalFileTypes.class);
private final BibEntry entry = new BibEntry(StandardEntryType.Article);
Expand Down

0 comments on commit 1fb6e7a

Please sign in to comment.