From d9ce5ec4c61fb34c99610dd2110a28195e1a206c Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 6 Aug 2022 09:31:49 +0200 Subject: [PATCH 01/13] Simplify singleton --- .../gui/externalfiletype/ExternalFileTypes.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 89209f2cffa..551b9a70772 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -17,27 +17,24 @@ import org.jabref.model.util.FileHelper; // Do not make this class final, as it otherwise can't be mocked for tests -public class ExternalFileTypes { +public enum ExternalFileTypes { + INSTANCE; // This String is used in the encoded list in prefs of external file type // modifications, in order to indicate a removed default file type: private static final String FILE_TYPE_REMOVED_FLAG = "REMOVED"; - // The only instance of this class: - private static ExternalFileTypes singleton; + // Map containing all registered external file types: private final Set externalFileTypes = new TreeSet<>(Comparator.comparing(ExternalFileType::getName)); private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; - private ExternalFileTypes() { + ExternalFileTypes() { updateExternalFileTypes(); } public static ExternalFileTypes getInstance() { - if (ExternalFileTypes.singleton == null) { - ExternalFileTypes.singleton = new ExternalFileTypes(); - } - return ExternalFileTypes.singleton; + return INSTANCE; } public static List getDefaultExternalFileTypes() { From 1ff2d26becd18cd27ec2632a9412bbe7bfab33c8 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 6 Aug 2022 12:10:29 +0200 Subject: [PATCH 02/13] Fixed factory methods --- .../externalfiletype/ExternalFileType.java | 17 ++- .../externalfiletype/ExternalFileTypes.java | 113 ++++++++---------- .../ExternalFileTypesTab.java | 2 +- .../ExternalFileTypesTabViewModel.java | 6 +- .../jabref/preferences/JabRefPreferences.java | 4 +- .../preferences/PreferencesService.java | 3 +- 6 files changed, 75 insertions(+), 70 deletions(-) diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java index 15e4a869b51..0a5a44259cf 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java @@ -11,6 +11,10 @@ public interface ExternalFileType { String getMimeType(); + String getOpenWithApplication(); + + JabRefIcon getIcon(); + /** * Get the bibtex field name used for this file type. Currently we assume that field name equals filename extension. * @@ -20,7 +24,14 @@ default Field getField() { return FieldFactory.parseField(getExtension()); } - String getOpenWithApplication(); - - JabRefIcon getIcon(); + /** + * Return a String array representing this file type. This is used for storage into + * Preferences, and the same array can be used to construct the file type later, + * using the String[] constructor. + * + * @return A String[] containing all information about this file type. + */ + default String[] toStringArray() { + return new String[]{getName(), getExtension(), getMimeType(), getOpenWithApplication(), getIcon().name()}; + } } diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 551b9a70772..cd44fa0a1db 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -3,7 +3,9 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Comparator; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Optional; @@ -30,7 +32,7 @@ public enum ExternalFileTypes { private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; ExternalFileTypes() { - updateExternalFileTypes(); + externalFileTypes.addAll(fromString(Globals.prefs.getExternalFileTypes())); } public static ExternalFileTypes getInstance() { @@ -45,6 +47,11 @@ public Set getExternalFileTypeSelection() { return externalFileTypes; } + public void setExternalFileTypes(Collection types) { + externalFileTypes.clear(); + externalFileTypes.addAll(types); + } + /** * Look up the external file type registered with this name, if any. * @@ -124,21 +131,44 @@ public Optional getExternalFileTypeByMimeType(String mimeType) } } + public Optional getExternalFileTypeByFile(Path file) { + final String filePath = file.toString(); + final Optional extension = FileHelper.getFileExtension(filePath); + return extension.flatMap(this::getExternalFileTypeByExt); + } + + public Optional fromLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType) { + Optional type = getExternalFileTypeByName(linkedFile.getFileType()); + boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); + + if (isUnknownType && deduceUnknownType) { + // No file type was recognized. Try to find a usable file type based on mime type: + Optional mimeType = getExternalFileTypeByMimeType(linkedFile.getFileType()); + if (mimeType.isPresent()) { + return mimeType; + } + + // No type could be found from mime type. Try based on the extension: + return FileHelper.getFileExtension(linkedFile.getLink()) + .flatMap(this::getExternalFileTypeByExt); + } else { + return type; + } + } + /** - * Reset the List of external file types after user customization. - * - * @param types The new List of external file types. This is the complete list, not just new entries. + * @return A StringList of customized and removed file types compared to the default list of external file types for storing */ - public void setExternalFileTypes(List types) { + public String toString() { // First find a list of the default types: List defTypes = new ArrayList<>(getDefaultExternalFileTypes()); // Make a list of types that are unchanged: List unchanged = new ArrayList<>(); + // Create a result list + List results = new ArrayList<>(); - externalFileTypes.clear(); - for (ExternalFileType type : types) { - externalFileTypes.add(type); - + for (ExternalFileType type : externalFileTypes) { + results.add(type); // See if we can find a type with matching name in the default type list: ExternalFileType found = null; for (ExternalFileType defType : defTypes) { @@ -163,52 +193,38 @@ public void setExternalFileTypes(List types) { // and from the list of defaults, since we don't need to mention these in prefs: for (ExternalFileType type : unchanged) { defTypes.remove(type); - types.remove(type); + results.remove(type); } // Now set up the array to write to prefs, containing all new types, all modified // types, and a flag denoting each default type that has been removed: - String[][] array = new String[types.size() + defTypes.size()][]; + String[][] array = new String[results.size() + defTypes.size()][]; int i = 0; - for (ExternalFileType type : types) { - array[i] = getStringArrayRepresentation(type); + for (ExternalFileType type : results) { + array[i] = type.toStringArray(); i++; } for (ExternalFileType type : defTypes) { array[i] = new String[] {type.getName(), FILE_TYPE_REMOVED_FLAG}; i++; } - Globals.prefs.storeExternalFileTypes(FileFieldWriter.encodeStringArray(array)); + return FileFieldWriter.encodeStringArray(array); } /** - * Return a String array representing this file type. This is used for storage into - * Preferences, and the same array can be used to construct the file type later, - * using the String[] constructor. - * - * @return A String[] containing all information about this file type. - */ - private String[] getStringArrayRepresentation(ExternalFileType type) { - return new String[] {type.getName(), type.getExtension(), type.getMimeType(), type.getOpenWithApplication(), type.getIcon().name()}; - } - - /** - * Set up the list of external file types, either from default values, or from values recorded in Preferences. + * Set up the list of external file types, either from default values, or from values recorded in PreferencesService. */ - private void updateExternalFileTypes() { + public static Set fromString(String storedFileTypes) { // First get a list of the default file types as a starting point: - List types = new ArrayList<>(getDefaultExternalFileTypes()); + Set types = new HashSet<>(getDefaultExternalFileTypes()); + // If no changes have been stored, simply use the defaults: - Optional storedFileTypes = Globals.prefs.getExternalFileTypes(); - if (storedFileTypes.isEmpty()) { - externalFileTypes.clear(); - externalFileTypes.addAll(types); - return; + if (StringUtil.isBlank(storedFileTypes)) { + return types; } // Read the prefs information for file types: - String[][] vals = StringUtil - .decodeStringDoubleArray(storedFileTypes.orElse("")); + String[][] vals = StringUtil.decodeStringDoubleArray(storedFileTypes); for (String[] val : vals) { if ((val.length == 2) && val[1].equals(FILE_TYPE_REMOVED_FLAG)) { // This entry indicates that a default entry type should be removed: @@ -246,31 +262,6 @@ private void updateExternalFileTypes() { } // Finally, build the list of types based on the modified defaults list: - externalFileTypes.addAll(types); - } - - public Optional getExternalFileTypeByFile(Path file) { - final String filePath = file.toString(); - final Optional extension = FileHelper.getFileExtension(filePath); - return extension.flatMap(this::getExternalFileTypeByExt); - } - - public Optional fromLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType) { - Optional type = getExternalFileTypeByName(linkedFile.getFileType()); - boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); - - if (isUnknownType && deduceUnknownType) { - // No file type was recognized. Try to find a usable file type based on mime type: - Optional mimeType = getExternalFileTypeByMimeType(linkedFile.getFileType()); - if (mimeType.isPresent()) { - return mimeType; - } - - // No type could be found from mime type. Try based on the extension: - return FileHelper.getFileExtension(linkedFile.getLink()) - .flatMap(this::getExternalFileTypeByExt); - } else { - return type; - } + return types; } } diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java index 41998975660..a34be4f1368 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java @@ -43,7 +43,7 @@ public String getTabName() { @FXML public void initialize() { - viewModel = new ExternalFileTypesTabViewModel(ExternalFileTypes.getInstance()); + viewModel = new ExternalFileTypesTabViewModel(ExternalFileTypes.getInstance(), preferencesService); fileTypesTable.setItems(viewModel.getFileTypes()); diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java index ee669177d6d..4e2de674c4c 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java @@ -14,16 +14,19 @@ import org.jabref.gui.icon.IconTheme; import org.jabref.gui.preferences.PreferenceTabViewModel; import org.jabref.logic.l10n.Localization; +import org.jabref.preferences.PreferencesService; import com.airhacks.afterburner.injection.Injector; public class ExternalFileTypesTabViewModel implements PreferenceTabViewModel { private final ExternalFileTypes externalFileTypes; + private final PreferencesService preferencesService; private final ObservableList fileTypes = FXCollections.observableArrayList(); - public ExternalFileTypesTabViewModel(ExternalFileTypes externalFileTypes) { + public ExternalFileTypesTabViewModel(ExternalFileTypes externalFileTypes, PreferencesService preferencesService) { this.externalFileTypes = externalFileTypes; + this.preferencesService = preferencesService; } @Override @@ -34,6 +37,7 @@ public void setValues() { public void storeSettings() { externalFileTypes.setExternalFileTypes(fileTypes); + preferencesService.storeExternalFileTypes(externalFileTypes.toString()); } public void resetToDefaults() { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index d204f2f8900..48d3e55140d 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2760,8 +2760,8 @@ public void storeLastPreferencesExportPath(Path exportFile) { } @Override - public Optional getExternalFileTypes() { - return Optional.ofNullable(get(EXTERNAL_FILE_TYPES, null)); + public String getExternalFileTypes() { + return get(EXTERNAL_FILE_TYPES); } @Override diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 3e80aa2dde3..6ec3730efc9 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -3,7 +3,6 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.prefs.BackingStoreException; @@ -278,7 +277,7 @@ public interface PreferencesService { void storeLastPreferencesExportPath(Path exportFile); - Optional getExternalFileTypes(); + String getExternalFileTypes(); void storeExternalFileTypes(String externalFileTypes); From 73eda79ff770c4dfcd33a650df2ab4c447c906bb Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 6 Aug 2022 12:22:38 +0200 Subject: [PATCH 03/13] Reworded for consistency --- .../org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java | 2 +- .../org/jabref/gui/externalfiletype/ExternalFileTypes.java | 5 ++--- .../org/jabref/gui/fieldeditors/LinkedFileViewModel.java | 4 ++-- .../gui/linkedfile/LinkedFilesEditDialogViewModel.java | 4 ++-- src/main/java/org/jabref/gui/maintable/CellFactory.java | 2 +- .../java/org/jabref/gui/maintable/columns/FileColumn.java | 2 +- .../externalfiletypes/ExternalFileTypesTabViewModel.java | 2 +- .../org/jabref/gui/preferences/table/TableTabViewModel.java | 2 +- .../jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java | 2 +- .../org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java | 2 +- 10 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java index e783ee0aed3..976bf16d6ba 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java @@ -107,7 +107,7 @@ public LinkFilesResult linkAssociatedFiles(List entries, NamedCompound public List findAssociatedNotLinkedFiles(BibEntry entry) throws IOException { List linkedFiles = new ArrayList<>(); - List extensions = externalFileTypes.getExternalFileTypeSelection().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); + List extensions = externalFileTypes.getExternalFileTypes().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); // Run the search operation FileFinder fileFinder = FileFinders.constructFromConfiguration(autoLinkPreferences); diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index cd44fa0a1db..47cccc3b5f7 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -43,7 +43,7 @@ public static List getDefaultExternalFileTypes() { return Arrays.asList(StandardExternalFileType.values()); } - public Set getExternalFileTypeSelection() { + public Set getExternalFileTypes() { return externalFileTypes; } @@ -137,7 +137,7 @@ public Optional getExternalFileTypeByFile(Path file) { return extension.flatMap(this::getExternalFileTypeByExt); } - public Optional fromLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType) { + public Optional getExternalFileTypeByLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType) { Optional type = getExternalFileTypeByName(linkedFile.getFileType()); boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); @@ -261,7 +261,6 @@ public static Set fromString(String storedFileTypes) { } } - // Finally, build the list of types based on the modified defaults list: return types; } } diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index 89a943d9e1f..f45ce443904 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -176,7 +176,7 @@ public Optional findIn(List directories) { } public JabRefIcon getTypeIcon() { - return externalFileTypes.fromLinkedFile(linkedFile, false) + return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE); } @@ -199,7 +199,7 @@ public Observable[] getObservables() { public void open() { try { - Optional type = ExternalFileTypes.getInstance().fromLinkedFile(linkedFile, true); + Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByLinkedFile(linkedFile, true); boolean successful = JabRefDesktop.openExternalFileAnyFormat(databaseContext, preferences, linkedFile.getLink(), type); if (!successful) { dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink())); diff --git a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java index 12d3f3f45ce..67b28f12eba 100644 --- a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java +++ b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java @@ -48,7 +48,7 @@ public LinkedFilesEditDialogViewModel(LinkedFile linkedFile, BibDatabaseContext this.dialogService = dialogService; this.filePreferences = filePreferences; this.externalFileTypes = externalFileTypes; - allExternalFileTypes.set(FXCollections.observableArrayList(externalFileTypes.getExternalFileTypeSelection())); + allExternalFileTypes.set(FXCollections.observableArrayList(externalFileTypes.getExternalFileTypes())); monadicSelectedExternalFileType = EasyBind.wrapNullable(selectedExternalFileType); setValues(linkedFile); @@ -101,7 +101,7 @@ public void setValues(LinkedFile linkedFile) { selectedExternalFileType.setValue(null); // See what is a reasonable selection for the type combobox: - Optional fileType = externalFileTypes.fromLinkedFile(linkedFile, false); + Optional fileType = externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false); if (fileType.isPresent() && !(fileType.get() instanceof UnknownExternalFileType)) { selectedExternalFileType.setValue(fileType.get()); } else if ((linkedFile.getLink() != null) && (!linkedFile.getLink().isEmpty())) { diff --git a/src/main/java/org/jabref/gui/maintable/CellFactory.java b/src/main/java/org/jabref/gui/maintable/CellFactory.java index 9f0940831d4..a937acf9e7d 100644 --- a/src/main/java/org/jabref/gui/maintable/CellFactory.java +++ b/src/main/java/org/jabref/gui/maintable/CellFactory.java @@ -56,7 +56,7 @@ public CellFactory(ExternalFileTypes externalFileTypes, PreferencesService prefe // icon.setToolTipText(Localization.lang("Open file")); TABLE_ICONS.put(StandardField.FILE, icon); - for (ExternalFileType fileType : externalFileTypes.getExternalFileTypeSelection()) { + for (ExternalFileType fileType : externalFileTypes.getExternalFileTypes()) { icon = fileType.getIcon(); // icon.setToolTipText(Localization.lang("Open %0 file", fileType.getName())); TABLE_ICONS.put(fileType.getField(), icon); diff --git a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java index c325637af32..efbdfdd779a 100644 --- a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java +++ b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java @@ -147,7 +147,7 @@ private Node createFileIcon(List linkedFiles) { if (linkedFiles.size() > 1) { return IconTheme.JabRefIcons.FILE_MULTIPLE.getGraphicNode(); } else if (linkedFiles.size() == 1) { - return externalFileTypes.fromLinkedFile(linkedFiles.get(0), true) + return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFiles.get(0), true) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE) .getGraphicNode(); diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java index 4e2de674c4c..6f43868f147 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java @@ -31,7 +31,7 @@ public ExternalFileTypesTabViewModel(ExternalFileTypes externalFileTypes, Prefer @Override public void setValues() { - fileTypes.setAll(externalFileTypes.getExternalFileTypeSelection()); + fileTypes.setAll(externalFileTypes.getExternalFileTypes()); fileTypes.sort(Comparator.comparing(ExternalFileType::getName)); } diff --git a/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java b/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java index 2184469c933..81b66abe78e 100644 --- a/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java @@ -180,7 +180,7 @@ private void removeSpecialFieldColumns() { } private void insertExtraFileColumns() { - ExternalFileTypes.getInstance().getExternalFileTypeSelection().stream() + ExternalFileTypes.getInstance().getExternalFileTypes().stream() .map(ExternalFileType::getName) .map(name -> new MainTableColumnModel(MainTableColumnModel.Type.EXTRAFILE, name)) .forEach(item -> availableColumnsProperty.getValue().add(item)); diff --git a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java index 22c73109876..d7fc174a767 100644 --- a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java +++ b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java @@ -41,7 +41,7 @@ public void setUp(@TempDir Path folder) throws Exception { path = folder.resolve("CiteKey.pdf"); Files.createFile(path); entry.setCitationKey("CiteKey"); - when(externalFileTypes.getExternalFileTypeSelection()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); + when(externalFileTypes.getExternalFileTypes()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); } @Test diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index e01d9f46594..6cd38ef32d8 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -75,7 +75,7 @@ void setUp(@TempDir Path tempFolder) throws Exception { taskExecutor = mock(TaskExecutor.class); dialogService = mock(DialogService.class); - when(externalFileType.getExternalFileTypeSelection()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); + when(externalFileType.getExternalFileTypes()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); when(externalFileType.getExternalFileTypeByMimeType("application/pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF)); when(externalFileType.getExternalFileTypeByMimeType(contains("text/html"))).thenReturn(Optional.of(StandardExternalFileType.URL)); when(externalFileType.getExternalFileTypeByExt("pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF)); From be5143f538340135aab97a0f87693d348c66cf13 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 6 Aug 2022 18:48:44 +0200 Subject: [PATCH 04/13] Moved externalFileTypes to preferences --- .../externalfiletype/ExternalFileTypes.java | 30 +++++-------------- .../ExternalFileTypesTabViewModel.java | 3 +- .../jabref/preferences/JabRefPreferences.java | 23 +++++++++++--- .../preferences/PreferencesService.java | 6 ++-- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 47cccc3b5f7..0d56b0d13e7 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -4,13 +4,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Optional; import java.util.Set; -import java.util.TreeSet; import org.jabref.gui.Globals; import org.jabref.logic.bibtex.FileFieldWriter; @@ -26,15 +24,8 @@ public enum ExternalFileTypes { // modifications, in order to indicate a removed default file type: private static final String FILE_TYPE_REMOVED_FLAG = "REMOVED"; - // Map containing all registered external file types: - private final Set externalFileTypes = new TreeSet<>(Comparator.comparing(ExternalFileType::getName)); - private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; - ExternalFileTypes() { - externalFileTypes.addAll(fromString(Globals.prefs.getExternalFileTypes())); - } - public static ExternalFileTypes getInstance() { return INSTANCE; } @@ -44,12 +35,7 @@ public static List getDefaultExternalFileTypes() { } public Set getExternalFileTypes() { - return externalFileTypes; - } - - public void setExternalFileTypes(Collection types) { - externalFileTypes.clear(); - externalFileTypes.addAll(types); + return Globals.prefs.getExternalFileTypes(); } /** @@ -59,7 +45,7 @@ public void setExternalFileTypes(Collection types) { * @return The ExternalFileType registered, or null if none. */ public Optional getExternalFileTypeByName(String name) { - Optional externalFileType = externalFileTypes.stream().filter(type -> type.getName().equals(name)).findFirst(); + Optional externalFileType = getExternalFileTypes().stream().filter(type -> type.getName().equals(name)).findFirst(); if (externalFileType.isPresent()) { return externalFileType; } @@ -75,7 +61,7 @@ public Optional getExternalFileTypeByName(String name) { */ public Optional getExternalFileTypeByExt(String extension) { String extensionCleaned = extension.replace(".", "").replace("*", ""); - return externalFileTypes.stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); + return getExternalFileTypes().stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); } /** @@ -85,7 +71,7 @@ public Optional getExternalFileTypeByExt(String extension) { * @return true if an ExternalFileType with the extension exists, false otherwise */ public boolean isExternalFileTypeByExt(String extension) { - return externalFileTypes.stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); + return getExternalFileTypes().stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); } /** @@ -97,7 +83,7 @@ public boolean isExternalFileTypeByExt(String extension) { public Optional getExternalFileTypeForName(String filename) { int longestFound = -1; ExternalFileType foundType = null; - for (ExternalFileType type : externalFileTypes) { + for (ExternalFileType type : getExternalFileTypes()) { if (!type.getExtension().isEmpty() && filename.toLowerCase(Locale.ROOT).endsWith(type.getExtension().toLowerCase(Locale.ROOT)) && (type.getExtension().length() > longestFound)) { longestFound = type.getExtension().length(); @@ -119,7 +105,7 @@ public Optional getExternalFileTypeByMimeType(String mimeType) if (mimeType.indexOf(';') != -1) { mimeType = mimeType.substring(0, mimeType.indexOf(';')).trim(); } - for (ExternalFileType type : externalFileTypes) { + for (ExternalFileType type : getExternalFileTypes()) { if (type.getMimeType().equalsIgnoreCase(mimeType)) { return Optional.of(type); } @@ -159,7 +145,7 @@ public Optional getExternalFileTypeByLinkedFile(LinkedFile lin /** * @return A StringList of customized and removed file types compared to the default list of external file types for storing */ - public String toString() { + public static String toStringList(Collection fileTypes) { // First find a list of the default types: List defTypes = new ArrayList<>(getDefaultExternalFileTypes()); // Make a list of types that are unchanged: @@ -167,7 +153,7 @@ public String toString() { // Create a result list List results = new ArrayList<>(); - for (ExternalFileType type : externalFileTypes) { + for (ExternalFileType type : fileTypes) { results.add(type); // See if we can find a type with matching name in the default type list: ExternalFileType found = null; diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java index 6f43868f147..472a96a835e 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java @@ -36,8 +36,7 @@ public void setValues() { } public void storeSettings() { - externalFileTypes.setExternalFileTypes(fileTypes); - preferencesService.storeExternalFileTypes(externalFileTypes.toString()); + preferencesService.storeExternalFileTypes(fileTypes); } public void resetToDefaults() { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 48d3e55140d..5a3901d0e41 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -15,6 +15,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; @@ -42,6 +43,8 @@ import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.gui.entryeditor.EntryEditorPreferences; +import org.jabref.gui.externalfiletype.ExternalFileType; +import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.groups.GroupViewMode; import org.jabref.gui.groups.GroupsPreferences; import org.jabref.gui.keyboard.KeyBindingRepository; @@ -430,6 +433,8 @@ public class JabRefPreferences implements PreferencesService { private Set customImporters; private String userName; + private Set externalFileTypes; + private PreviewPreferences previewPreferences; private SidePanePreferences sidePanePreferences; private AppearancePreferences appearancePreferences; @@ -2760,13 +2765,23 @@ public void storeLastPreferencesExportPath(Path exportFile) { } @Override - public String getExternalFileTypes() { - return get(EXTERNAL_FILE_TYPES); + public Set getExternalFileTypes() { + if (externalFileTypes == null) { + externalFileTypes = new TreeSet<>(Comparator.comparing(ExternalFileType::getName)); + updateExternalFileTypes(); + } + return externalFileTypes; } @Override - public void storeExternalFileTypes(String externalFileTypes) { - put(EXTERNAL_FILE_TYPES, externalFileTypes); + public void storeExternalFileTypes(Collection externalFileTypes) { + put(EXTERNAL_FILE_TYPES, ExternalFileTypes.toStringList(externalFileTypes)); + updateExternalFileTypes(); + } + + private void updateExternalFileTypes() { + externalFileTypes.clear(); + externalFileTypes = ExternalFileTypes.fromString(get(EXTERNAL_FILE_TYPES)); } @Override diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 6ec3730efc9..db7cf81d5ea 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -1,6 +1,7 @@ package org.jabref.preferences; import java.nio.file.Path; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -8,6 +9,7 @@ import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.entryeditor.EntryEditorPreferences; +import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.groups.GroupsPreferences; import org.jabref.gui.keyboard.KeyBindingRepository; import org.jabref.gui.maintable.ColumnPreferences; @@ -277,9 +279,9 @@ public interface PreferencesService { void storeLastPreferencesExportPath(Path exportFile); - String getExternalFileTypes(); + Set getExternalFileTypes(); - void storeExternalFileTypes(String externalFileTypes); + void storeExternalFileTypes(Collection externalFileTypes); MrDlibPreferences getMrDlibPreferences(); From a6cf46d7baf8d4f3c3b578a77834f8e72b455b35 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 10 Aug 2022 22:48:24 +0200 Subject: [PATCH 05/13] Added deprecated notice --- .../jabref/gui/externalfiletype/ExternalFileTypes.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 0d56b0d13e7..0b14429c1f9 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -15,6 +15,7 @@ import org.jabref.model.entry.LinkedFile; import org.jabref.model.strings.StringUtil; import org.jabref.model.util.FileHelper; +import org.jabref.preferences.PreferencesService; // Do not make this class final, as it otherwise can't be mocked for tests public enum ExternalFileTypes { @@ -26,6 +27,10 @@ public enum ExternalFileTypes { private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; + /** + * @deprecated use {@link PreferencesService#getExternalFileTypes()} instead. + */ + @Deprecated public static ExternalFileTypes getInstance() { return INSTANCE; } @@ -34,6 +39,10 @@ public static List getDefaultExternalFileTypes() { return Arrays.asList(StandardExternalFileType.values()); } + /** + * @deprecated use {@link PreferencesService#getExternalFileTypes()} instead. + */ + @Deprecated public Set getExternalFileTypes() { return Globals.prefs.getExternalFileTypes(); } From 5df3bcd6bd353d1fc994fc65e6ea033c3f29480c Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 10 Aug 2022 23:34:50 +0200 Subject: [PATCH 06/13] Moved preferences to FilePreferences --- .../externalfiletype/ExternalFileTypes.java | 8 +++--- .../ExternalFileTypesTab.java | 3 +- .../ExternalFileTypesTabViewModel.java | 15 +++++----- .../jabref/preferences/FilePreferences.java | 15 +++++++++- .../jabref/preferences/JabRefPreferences.java | 28 +++---------------- .../preferences/PreferencesService.java | 6 ---- 6 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 0b14429c1f9..85154802cfe 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -15,7 +15,7 @@ import org.jabref.model.entry.LinkedFile; import org.jabref.model.strings.StringUtil; import org.jabref.model.util.FileHelper; -import org.jabref.preferences.PreferencesService; +import org.jabref.preferences.FilePreferences; // Do not make this class final, as it otherwise can't be mocked for tests public enum ExternalFileTypes { @@ -28,7 +28,7 @@ public enum ExternalFileTypes { private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; /** - * @deprecated use {@link PreferencesService#getExternalFileTypes()} instead. + * @deprecated use {@link FilePreferences#getExternalFileTypes()} instead. */ @Deprecated public static ExternalFileTypes getInstance() { @@ -40,11 +40,11 @@ public static List getDefaultExternalFileTypes() { } /** - * @deprecated use {@link PreferencesService#getExternalFileTypes()} instead. + * @deprecated use {@link FilePreferences#getExternalFileTypes()} instead. */ @Deprecated public Set getExternalFileTypes() { - return Globals.prefs.getExternalFileTypes(); + return Globals.prefs.getFilePreferences().getExternalFileTypes(); } /** diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java index a34be4f1368..0347047dee9 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java @@ -5,7 +5,6 @@ import javafx.scene.control.TableView; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.preferences.AbstractPreferenceTabView; @@ -43,7 +42,7 @@ public String getTabName() { @FXML public void initialize() { - viewModel = new ExternalFileTypesTabViewModel(ExternalFileTypes.getInstance(), preferencesService); + viewModel = new ExternalFileTypesTabViewModel(preferencesService.getFilePreferences()); fileTypesTable.setItems(viewModel.getFileTypes()); diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java index 472a96a835e..e1ff1973bcb 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java @@ -14,29 +14,28 @@ import org.jabref.gui.icon.IconTheme; import org.jabref.gui.preferences.PreferenceTabViewModel; import org.jabref.logic.l10n.Localization; -import org.jabref.preferences.PreferencesService; +import org.jabref.preferences.FilePreferences; import com.airhacks.afterburner.injection.Injector; public class ExternalFileTypesTabViewModel implements PreferenceTabViewModel { - private final ExternalFileTypes externalFileTypes; - private final PreferencesService preferencesService; + private final FilePreferences filePreferences; private final ObservableList fileTypes = FXCollections.observableArrayList(); - public ExternalFileTypesTabViewModel(ExternalFileTypes externalFileTypes, PreferencesService preferencesService) { - this.externalFileTypes = externalFileTypes; - this.preferencesService = preferencesService; + public ExternalFileTypesTabViewModel(FilePreferences filePreferences) { + this.filePreferences = filePreferences; } @Override public void setValues() { - fileTypes.setAll(externalFileTypes.getExternalFileTypes()); + fileTypes.setAll(filePreferences.getExternalFileTypes()); fileTypes.sort(Comparator.comparing(ExternalFileType::getName)); } public void storeSettings() { - preferencesService.storeExternalFileTypes(fileTypes); + filePreferences.getExternalFileTypes().clear(); + filePreferences.getExternalFileTypes().addAll(fileTypes); } public void resetToDefaults() { diff --git a/src/main/java/org/jabref/preferences/FilePreferences.java b/src/main/java/org/jabref/preferences/FilePreferences.java index a2f2c9e4a4b..037324fc1dd 100644 --- a/src/main/java/org/jabref/preferences/FilePreferences.java +++ b/src/main/java/org/jabref/preferences/FilePreferences.java @@ -1,7 +1,10 @@ package org.jabref.preferences; import java.nio.file.Path; +import java.util.Comparator; import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; @@ -9,7 +12,10 @@ import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableSet; +import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.model.strings.StringUtil; public class FilePreferences { @@ -24,6 +30,7 @@ public class FilePreferences { private final BooleanProperty downloadLinkedFiles = new SimpleBooleanProperty(); private final BooleanProperty fulltextIndexLinkedFiles = new SimpleBooleanProperty(); private final ObjectProperty workingDirectory = new SimpleObjectProperty<>(); + private final ObservableSet externalFileTypes = FXCollections.observableSet(new TreeSet<>(Comparator.comparing(ExternalFileType::getName))); public FilePreferences(String user, String mainFileDirectory, @@ -32,7 +39,8 @@ public FilePreferences(String user, String fileDirectoryPattern, boolean downloadLinkedFiles, boolean fulltextIndexLinkedFiles, - Path workingDirectory) { + Path workingDirectory, + Set externalFileTypes) { this.user.setValue(user); this.mainFileDirectory.setValue(mainFileDirectory); this.storeFilesRelativeToBibFile.setValue(storeFilesRelativeToBibFile); @@ -41,6 +49,7 @@ public FilePreferences(String user, this.downloadLinkedFiles.setValue(downloadLinkedFiles); this.fulltextIndexLinkedFiles.setValue(fulltextIndexLinkedFiles); this.workingDirectory.setValue(workingDirectory); + this.externalFileTypes.addAll(externalFileTypes); } public String getUser() { // Read only @@ -134,4 +143,8 @@ public ObjectProperty workingDirectoryProperty() { public void setWorkingDirectory(Path workingDirectory) { this.workingDirectory.set(workingDirectory); } + + public ObservableSet getExternalFileTypes() { + return this.externalFileTypes; + } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 5a3901d0e41..b954e883683 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -15,7 +15,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; @@ -433,8 +432,6 @@ public class JabRefPreferences implements PreferencesService { private Set customImporters; private String userName; - private Set externalFileTypes; - private PreviewPreferences previewPreferences; private SidePanePreferences sidePanePreferences; private AppearancePreferences appearancePreferences; @@ -2213,7 +2210,8 @@ public FilePreferences getFilePreferences() { get(IMPORT_FILEDIRPATTERN), getBoolean(DOWNLOAD_LINKED_FILES), getBoolean(FULLTEXT_INDEX_LINKED_FILES), - Path.of(get(WORKING_DIRECTORY)) + Path.of(get(WORKING_DIRECTORY)), + ExternalFileTypes.fromString(get(EXTERNAL_FILE_TYPES)) ); EasyBind.listen(filePreferences.mainFileDirectoryProperty(), (obs, oldValue, newValue) -> put(MAIN_FILE_DIRECTORY, newValue)); @@ -2223,6 +2221,8 @@ public FilePreferences getFilePreferences() { EasyBind.listen(filePreferences.downloadLinkedFilesProperty(), (obs, oldValue, newValue) -> putBoolean(DOWNLOAD_LINKED_FILES, newValue)); EasyBind.listen(filePreferences.fulltextIndexLinkedFilesProperty(), (obs, oldValue, newValue) -> putBoolean(FULLTEXT_INDEX_LINKED_FILES, newValue)); EasyBind.listen(filePreferences.workingDirectoryProperty(), (obs, oldValue, newValue) -> put(WORKING_DIRECTORY, newValue.toString())); + filePreferences.getExternalFileTypes().addListener((SetChangeListener) c -> + put(EXTERNAL_FILE_TYPES, ExternalFileTypes.toStringList(filePreferences.getExternalFileTypes()))); return filePreferences; } @@ -2764,26 +2764,6 @@ public void storeLastPreferencesExportPath(Path exportFile) { put(PREFS_EXPORT_PATH, exportFile.toString()); } - @Override - public Set getExternalFileTypes() { - if (externalFileTypes == null) { - externalFileTypes = new TreeSet<>(Comparator.comparing(ExternalFileType::getName)); - updateExternalFileTypes(); - } - return externalFileTypes; - } - - @Override - public void storeExternalFileTypes(Collection externalFileTypes) { - put(EXTERNAL_FILE_TYPES, ExternalFileTypes.toStringList(externalFileTypes)); - updateExternalFileTypes(); - } - - private void updateExternalFileTypes() { - externalFileTypes.clear(); - externalFileTypes = ExternalFileTypes.fromString(get(EXTERNAL_FILE_TYPES)); - } - @Override public MrDlibPreferences getMrDlibPreferences() { if (Objects.nonNull(mrDlibPreferences)) { diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index db7cf81d5ea..d3f7c692248 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -1,7 +1,6 @@ package org.jabref.preferences; import java.nio.file.Path; -import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; @@ -9,7 +8,6 @@ import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.entryeditor.EntryEditorPreferences; -import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.groups.GroupsPreferences; import org.jabref.gui.keyboard.KeyBindingRepository; import org.jabref.gui.maintable.ColumnPreferences; @@ -279,10 +277,6 @@ public interface PreferencesService { void storeLastPreferencesExportPath(Path exportFile); - Set getExternalFileTypes(); - - void storeExternalFileTypes(Collection externalFileTypes); - MrDlibPreferences getMrDlibPreferences(); ProtectedTermsPreferences getProtectedTermsPreferences(); From 6b1de0f2ac95d6e25fceddc90e8ffeeebb4b3bea Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 00:41:15 +0200 Subject: [PATCH 07/13] Extract static call in ExternalFileTypes methods --- .../org/jabref/gui/desktop/JabRefDesktop.java | 2 +- .../java/org/jabref/gui/desktop/os/Linux.java | 3 +- .../java/org/jabref/gui/desktop/os/OSX.java | 3 +- .../org/jabref/gui/desktop/os/Windows.java | 3 +- .../externalfiles/AutoSetFileLinksUtil.java | 8 +++-- .../ExternalFilesEntryLinker.java | 2 +- .../externalfiles/FileExtensionViewModel.java | 7 ++-- .../UnlinkedFilesDialogViewModel.java | 6 ++-- .../externalfiletype/ExternalFileTypes.java | 32 +++++++++---------- .../gui/fieldeditors/LinkedFileViewModel.java | 16 ++++++---- .../LinkedFilesEditorViewModel.java | 11 ++++--- .../org/jabref/gui/fieldeditors/URLUtil.java | 5 +-- .../gui/linkedfile/AttachFileAction.java | 6 ++-- .../LinkedFilesEditDialogViewModel.java | 6 ++-- .../gui/maintable/columns/FileColumn.java | 4 +-- .../StyleSelectDialogViewModel.java | 2 +- .../ProtectedTermsTabViewModel.java | 4 +-- .../fieldeditors/LinkedFileViewModelTest.java | 16 ++++++---- 18 files changed, 77 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index 96bc258e134..57731fafa6c 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -219,7 +219,7 @@ public static void openFolderAndSelectFile(Path fileLink, PreferencesService pre * @param url the URL to open */ public static void openBrowser(String url) throws IOException { - Optional fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt("html"); + Optional fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt("html", Globals.prefs.getFilePreferences()); openExternalFilePlatformIndependent(fileType, url); } diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index c522f172c55..b81bd105a60 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -12,6 +12,7 @@ import org.jabref.architecture.AllowedToUseAwt; import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; import org.jabref.gui.JabRefExecutorService; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; @@ -48,7 +49,7 @@ private void nativeOpenFile(String filePath) { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType); + Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); String viewer; if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { diff --git a/src/main/java/org/jabref/gui/desktop/os/OSX.java b/src/main/java/org/jabref/gui/desktop/os/OSX.java index 2bb731619fa..2465cded8c3 100644 --- a/src/main/java/org/jabref/gui/desktop/os/OSX.java +++ b/src/main/java/org/jabref/gui/desktop/os/OSX.java @@ -6,6 +6,7 @@ import org.jabref.architecture.AllowedToUseAwt; import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; @@ -14,7 +15,7 @@ public class OSX implements NativeDesktop { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType); + Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { openFileWithApplication(filePath, type.get().getOpenWithApplication()); } else { diff --git a/src/main/java/org/jabref/gui/desktop/os/Windows.java b/src/main/java/org/jabref/gui/desktop/os/Windows.java index a5a6acfe79a..ebc5946a3cb 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -7,6 +7,7 @@ import java.util.Optional; import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; @@ -15,7 +16,7 @@ public class Windows implements NativeDesktop { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType); + Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { openFileWithApplication(filePath, type.get().getOpenWithApplication()); diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java index 976bf16d6ba..821e1d10837 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java @@ -55,15 +55,17 @@ public List getFileExceptions() { private static final Logger LOGGER = LoggerFactory.getLogger(AutoSetFileLinksUtil.class); private final List directories; private final AutoLinkPreferences autoLinkPreferences; + private final FilePreferences filePreferences; private final ExternalFileTypes externalFileTypes; public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { - this(databaseContext.getFileDirectories(filePreferences), autoLinkPreferences, externalFileTypes); + this(databaseContext.getFileDirectories(filePreferences), filePreferences, autoLinkPreferences, externalFileTypes); } - private AutoSetFileLinksUtil(List directories, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { + private AutoSetFileLinksUtil(List directories, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { this.directories = directories; this.autoLinkPreferences = autoLinkPreferences; + this.filePreferences = filePreferences; this.externalFileTypes = externalFileTypes; } @@ -128,7 +130,7 @@ public List findAssociatedNotLinkedFiles(BibEntry entry) throws IOEx if (!fileAlreadyLinked) { Optional type = FileHelper.getFileExtension(foundFile) - .map(externalFileTypes::getExternalFileTypeByExt) + .map(extension -> externalFileTypes.getExternalFileTypeByExt(extension, filePreferences)) .orElse(Optional.of(new UnknownExternalFileType(""))); String strType = type.isPresent() ? type.get().getName() : ""; diff --git a/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java b/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java index b62748a4959..fbb28a36559 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java +++ b/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java @@ -62,7 +62,7 @@ public void moveLinkedFilesToFileDir(BibEntry entry) { public void addFilesToEntry(BibEntry entry, List files) { for (Path file : files) { FileUtil.getFileExtension(file).ifPresent(ext -> { - ExternalFileType type = externalFileTypes.getExternalFileTypeByExt(ext) + ExternalFileType type = externalFileTypes.getExternalFileTypeByExt(ext, filePreferences) .orElse(new UnknownExternalFileType(ext)); Path relativePath = FileUtil.relativize(file, bibDatabaseContext.getFileDirectories(filePreferences)); LinkedFile linkedfile = new LinkedFile("", relativePath, type.getName()); diff --git a/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java b/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java index 121517bf5bc..5ab38a18847 100644 --- a/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java @@ -11,17 +11,20 @@ import org.jabref.gui.util.FileFilterConverter; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.FileType; +import org.jabref.preferences.FilePreferences; public class FileExtensionViewModel { private final String description; private final List extensions; private final ExternalFileTypes externalFileTypes; + private final FilePreferences filePreferences; - FileExtensionViewModel(FileType fileType, ExternalFileTypes externalFileTypes) { + FileExtensionViewModel(FileType fileType, ExternalFileTypes externalFileTypes, FilePreferences filePreferences) { this.description = Localization.lang("%0 file", fileType.getName()); this.extensions = fileType.getExtensionsWithDot(); this.externalFileTypes = externalFileTypes; + this.filePreferences = filePreferences; } public String getDescription() { @@ -29,7 +32,7 @@ public String getDescription() { } public JabRefIcon getIcon() { - return externalFileTypes.getExternalFileTypeByExt(extensions.get(0)) + return externalFileTypes.getExternalFileTypeByExt(extensions.get(0), filePreferences) .map(ExternalFileType::getIcon) .orElse(null); } diff --git a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java index 7d19b2bdc98..92a9372c742 100644 --- a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java @@ -101,9 +101,9 @@ public UnlinkedFilesDialogViewModel(DialogService dialogService, dialogService); this.fileFilterList = FXCollections.observableArrayList( - new FileExtensionViewModel(StandardFileType.ANY_FILE, externalFileTypes), - new FileExtensionViewModel(StandardFileType.BIBTEX_DB, externalFileTypes), - new FileExtensionViewModel(StandardFileType.PDF, externalFileTypes)); + new FileExtensionViewModel(StandardFileType.ANY_FILE, externalFileTypes, preferences.getFilePreferences()), + new FileExtensionViewModel(StandardFileType.BIBTEX_DB, externalFileTypes, preferences.getFilePreferences()), + new FileExtensionViewModel(StandardFileType.PDF, externalFileTypes, preferences.getFilePreferences())); this.dateFilterList = FXCollections.observableArrayList(DateRange.values()); diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 85154802cfe..e04e21aa648 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -53,8 +53,8 @@ public Set getExternalFileTypes() { * @param name The file type name. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeByName(String name) { - Optional externalFileType = getExternalFileTypes().stream().filter(type -> type.getName().equals(name)).findFirst(); + public Optional getExternalFileTypeByName(String name, FilePreferences filePreferences) { + Optional externalFileType = filePreferences.getExternalFileTypes().stream().filter(type -> type.getName().equals(name)).findFirst(); if (externalFileType.isPresent()) { return externalFileType; } @@ -68,9 +68,9 @@ public Optional getExternalFileTypeByName(String name) { * @param extension The file extension. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeByExt(String extension) { + public Optional getExternalFileTypeByExt(String extension, FilePreferences filePreferences) { String extensionCleaned = extension.replace(".", "").replace("*", ""); - return getExternalFileTypes().stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); + return filePreferences.getExternalFileTypes().stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); } /** @@ -79,8 +79,8 @@ public Optional getExternalFileTypeByExt(String extension) { * @param extension The file extension. * @return true if an ExternalFileType with the extension exists, false otherwise */ - public boolean isExternalFileTypeByExt(String extension) { - return getExternalFileTypes().stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); + public boolean isExternalFileTypeByExt(String extension, FilePreferences filePreferences) { + return filePreferences.getExternalFileTypes().stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); } /** @@ -89,10 +89,10 @@ public boolean isExternalFileTypeByExt(String extension) { * @param filename The name of the file whose type to look up. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeForName(String filename) { + public Optional getExternalFileTypeForName(String filename, FilePreferences filePreferences) { int longestFound = -1; ExternalFileType foundType = null; - for (ExternalFileType type : getExternalFileTypes()) { + for (ExternalFileType type : filePreferences.getExternalFileTypes()) { if (!type.getExtension().isEmpty() && filename.toLowerCase(Locale.ROOT).endsWith(type.getExtension().toLowerCase(Locale.ROOT)) && (type.getExtension().length() > longestFound)) { longestFound = type.getExtension().length(); @@ -109,12 +109,12 @@ public Optional getExternalFileTypeForName(String filename) { * @return The ExternalFileType registered, or null if none. For the mime type "text/html", a valid file type is * guaranteed to be returned. */ - public Optional getExternalFileTypeByMimeType(String mimeType) { + public Optional getExternalFileTypeByMimeType(String mimeType, FilePreferences filePreferences) { // Ignores parameters according to link: (https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) if (mimeType.indexOf(';') != -1) { mimeType = mimeType.substring(0, mimeType.indexOf(';')).trim(); } - for (ExternalFileType type : getExternalFileTypes()) { + for (ExternalFileType type : filePreferences.getExternalFileTypes()) { if (type.getMimeType().equalsIgnoreCase(mimeType)) { return Optional.of(type); } @@ -126,26 +126,26 @@ public Optional getExternalFileTypeByMimeType(String mimeType) } } - public Optional getExternalFileTypeByFile(Path file) { + public Optional getExternalFileTypeByFile(Path file, FilePreferences filePreferences) { final String filePath = file.toString(); final Optional extension = FileHelper.getFileExtension(filePath); - return extension.flatMap(this::getExternalFileTypeByExt); + return extension.flatMap(ext -> this.getExternalFileTypeByExt(ext, filePreferences)); } - public Optional getExternalFileTypeByLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType) { - Optional type = getExternalFileTypeByName(linkedFile.getFileType()); + public Optional getExternalFileTypeByLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType, FilePreferences filePreferences) { + Optional type = getExternalFileTypeByName(linkedFile.getFileType(), filePreferences); boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); if (isUnknownType && deduceUnknownType) { // No file type was recognized. Try to find a usable file type based on mime type: - Optional mimeType = getExternalFileTypeByMimeType(linkedFile.getFileType()); + Optional mimeType = getExternalFileTypeByMimeType(linkedFile.getFileType(), filePreferences); if (mimeType.isPresent()) { return mimeType; } // No type could be found from mime type. Try based on the extension: return FileHelper.getFileExtension(linkedFile.getLink()) - .flatMap(this::getExternalFileTypeByExt); + .flatMap(extension -> this.getExternalFileTypeByExt(extension, filePreferences)); } else { return type; } diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index f45ce443904..aa85437be0d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -176,7 +176,7 @@ public Optional findIn(List directories) { } public JabRefIcon getTypeIcon() { - return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false) + return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, preferences.getFilePreferences()) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE); } @@ -199,7 +199,7 @@ public Observable[] getObservables() { public void open() { try { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByLinkedFile(linkedFile, true); + Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByLinkedFile(linkedFile, true, preferences.getFilePreferences()); boolean successful = JabRefDesktop.openExternalFileAnyFormat(databaseContext, preferences, linkedFile.getLink(), type); if (!successful) { dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink())); @@ -444,7 +444,11 @@ public void download() { } if (!isDuplicate) { - LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectories(preferences.getFilePreferences()), externalFileTypes); + LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile( + destination, + databaseContext.getFileDirectories(preferences.getFilePreferences()), + externalFileTypes, + preferences.getFilePreferences()); List linkedFiles = entry.getFiles(); entry.addLinkedFile(entry, linkedFile, newLinkedFile, linkedFiles); @@ -523,15 +527,15 @@ private Optional inferFileTypeFromMimeType(URLDownload urlDown if (mimeType != null) { LOGGER.debug("MIME Type suggested: " + mimeType); - return externalFileTypes.getExternalFileTypeByMimeType(mimeType); + return externalFileTypes.getExternalFileTypeByMimeType(mimeType, preferences.getFilePreferences()); } else { return Optional.empty(); } } private Optional inferFileTypeFromURL(String url) { - return URLUtil.getSuffix(url) - .flatMap(externalFileTypes::getExternalFileTypeByExt); + return URLUtil.getSuffix(url, preferences.getFilePreferences()) + .flatMap(extension -> externalFileTypes.getExternalFileTypeByExt(extension, preferences.getFilePreferences())); } public LinkedFile getFile() { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java index 4525c7f050c..0cccc94186d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java @@ -38,6 +38,7 @@ import org.jabref.model.entry.LinkedFile; import org.jabref.model.entry.field.Field; import org.jabref.model.util.FileHelper; +import org.jabref.preferences.FilePreferences; import org.jabref.preferences.PreferencesService; public class LinkedFilesEditorViewModel extends AbstractEditorViewModel { @@ -87,19 +88,19 @@ private static String getStringRepresentation(List files) { * * TODO: Move this method to {@link LinkedFile} as soon as {@link CustomExternalFileType} lives in model. */ - public static LinkedFile fromFile(Path file, List fileDirectories, ExternalFileTypes externalFileTypesFile) { + public static LinkedFile fromFile(Path file, List fileDirectories, ExternalFileTypes externalFileTypesFile, FilePreferences filePreferences) { String fileExtension = FileHelper.getFileExtension(file).orElse(""); ExternalFileType suggestedFileType = externalFileTypesFile - .getExternalFileTypeByExt(fileExtension) + .getExternalFileTypeByExt(fileExtension, filePreferences) .orElse(new UnknownExternalFileType(fileExtension)); Path relativePath = FileUtil.relativize(file, fileDirectories); return new LinkedFile("", relativePath, suggestedFileType.getName()); } - public LinkedFileViewModel fromFile(Path file) { + public LinkedFileViewModel fromFile(Path file, FilePreferences filePreferences) { List fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences()); - LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes); + LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes, filePreferences); return new LinkedFileViewModel( linkedFile, entry, @@ -145,7 +146,7 @@ public void addNewFile() { List fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences()); dialogService.showFileOpenDialogAndGetMultipleFiles(fileDialogConfiguration).forEach(newFile -> { - LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes); + LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes, preferences.getFilePreferences()); files.add(new LinkedFileViewModel( newLinkedFile, entry, diff --git a/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java b/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java index bb73b95a6d9..010bf682115 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java +++ b/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java @@ -8,6 +8,7 @@ import java.util.Optional; import org.jabref.gui.externalfiletype.ExternalFileTypes; +import org.jabref.preferences.FilePreferences; public class URLUtil { private static final String URL_EXP = "^(https?|ftp)://.+"; @@ -85,7 +86,7 @@ public static boolean isURL(String url) { * @param link The link * @return The suffix, excluding the dot (e.g. "pdf") */ - public static Optional getSuffix(final String link) { + public static Optional getSuffix(final String link, FilePreferences filePreferences) { String strippedLink = link; try { // Try to strip the query string, if any, to get the correct suffix: @@ -105,7 +106,7 @@ public static Optional getSuffix(final String link) { } else { suffix = strippedLink.substring(strippedLinkIndex + 1); } - if (!ExternalFileTypes.getInstance().isExternalFileTypeByExt(suffix)) { + if (!ExternalFileTypes.getInstance().isExternalFileTypeByExt(suffix, filePreferences)) { // If the suffix doesn't seem to give any reasonable file type, try // with the non-stripped link: int index = link.lastIndexOf('.'); diff --git a/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java b/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java index 00e0c1f3b4b..d7eae9b4729 100644 --- a/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java +++ b/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java @@ -62,9 +62,11 @@ public void execute() { .build(); dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> { - LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile(newFile, + LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile( + newFile, databaseContext.getFileDirectories(filePreferences), - ExternalFileTypes.getInstance()); + ExternalFileTypes.getInstance(), + filePreferences); LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(linkedFile); diff --git a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java index 67b28f12eba..3dafe95d25d 100644 --- a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java +++ b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java @@ -58,12 +58,12 @@ private void setExternalFileTypeByExtension(String link) { if (!link.isEmpty()) { // Check if this looks like a remote link: if (REMOTE_LINK_PATTERN.matcher(link).matches()) { - externalFileTypes.getExternalFileTypeByExt("html").ifPresent(selectedExternalFileType::setValue); + externalFileTypes.getExternalFileTypeByExt("html", filePreferences).ifPresent(selectedExternalFileType::setValue); } // Try to guess the file type: String theLink = link.trim(); - externalFileTypes.getExternalFileTypeForName(theLink).ifPresent(selectedExternalFileType::setValue); + externalFileTypes.getExternalFileTypeForName(theLink, filePreferences).ifPresent(selectedExternalFileType::setValue); } } @@ -101,7 +101,7 @@ public void setValues(LinkedFile linkedFile) { selectedExternalFileType.setValue(null); // See what is a reasonable selection for the type combobox: - Optional fileType = externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false); + Optional fileType = externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, filePreferences); if (fileType.isPresent() && !(fileType.get() instanceof UnknownExternalFileType)) { selectedExternalFileType.setValue(fileType.get()); } else if ((linkedFile.getLink() != null) && (!linkedFile.getLink().isEmpty())) { diff --git a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java index efbdfdd779a..9ab7a17318e 100644 --- a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java +++ b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java @@ -94,7 +94,7 @@ public FileColumn(MainTableColumnModel model, setCommonSettings(); this.setGraphic(externalFileTypes - .getExternalFileTypeByName(fileType) + .getExternalFileTypeByName(fileType, preferencesService.getFilePreferences()) .map(ExternalFileType::getIcon).orElse(IconTheme.JabRefIcons.FILE) .getGraphicNode()); @@ -147,7 +147,7 @@ private Node createFileIcon(List linkedFiles) { if (linkedFiles.size() > 1) { return IconTheme.JabRefIcons.FILE_MULTIPLE.getGraphicNode(); } else if (linkedFiles.size() == 1) { - return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFiles.get(0), true) + return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFiles.get(0), true, preferencesService.getFilePreferences()) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE) .getGraphicNode(); diff --git a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java index 92bd6dd607f..9b44d980c9d 100644 --- a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java +++ b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java @@ -93,7 +93,7 @@ public void deleteStyle() { public void editStyle() { OOBibStyle style = selectedItem.getValue().getStyle(); - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt("jstyle"); + Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt("jstyle", preferencesService.getFilePreferences()); try { JabRefDesktop.openExternalFileAnyFormat(new BibDatabaseContext(), preferencesService, style.getPath(), type); } catch (IOException e) { diff --git a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java index ca6b668ab5d..f9f75d33204 100644 --- a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java @@ -125,8 +125,8 @@ public void createNewFile() { public void edit(ProtectedTermsListItemModel file) { Optional termsFileType = OptionalUtil.orElse( - ExternalFileTypes.getInstance().getExternalFileTypeByExt("terms"), - ExternalFileTypes.getInstance().getExternalFileTypeByExt("txt") + ExternalFileTypes.getInstance().getExternalFileTypeByExt("terms", preferences.getFilePreferences()), + ExternalFileTypes.getInstance().getExternalFileTypeByExt("txt", preferences.getFilePreferences()) ); String fileName = file.getTermsList().getLocation(); diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index 6cd38ef32d8..aa6bc619426 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -13,6 +13,7 @@ import java.util.Optional; import java.util.TreeSet; +import javafx.collections.FXCollections; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.ButtonType; @@ -46,6 +47,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -75,11 +77,11 @@ void setUp(@TempDir Path tempFolder) throws Exception { taskExecutor = mock(TaskExecutor.class); dialogService = mock(DialogService.class); - when(externalFileType.getExternalFileTypes()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); - when(externalFileType.getExternalFileTypeByMimeType("application/pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF)); - when(externalFileType.getExternalFileTypeByMimeType(contains("text/html"))).thenReturn(Optional.of(StandardExternalFileType.URL)); - when(externalFileType.getExternalFileTypeByExt("pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF)); - when(externalFileType.getExternalFileTypeByExt("html")).thenReturn(Optional.of(StandardExternalFileType.URL)); + when(filePreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); + when(externalFileType.getExternalFileTypeByMimeType("application/pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); + when(externalFileType.getExternalFileTypeByMimeType(contains("text/html"), eq(filePreferences))).thenReturn(Optional.of(StandardExternalFileType.URL)); + when(externalFileType.getExternalFileTypeByExt("pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); + when(externalFileType.getExternalFileTypeByExt("html", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.URL)); when(preferences.getFilePreferences()).thenReturn(filePreferences); when(preferences.getXmpPreferences()).thenReturn(mock(XmpPreferences.class)); tempFile = tempFolder.resolve("temporaryFile"); @@ -214,7 +216,7 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { BackgroundTask task = viewModel.prepareDownloadTask(tempFile.getParent(), new URLDownload("http://arxiv.org/pdf/1207.0408v1")); task.onSuccess(destination -> { - LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, Collections.singletonList(tempFile.getParent()), externalFileType); + LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, Collections.singletonList(tempFile.getParent()), externalFileType, filePreferences); assertEquals("asdf.pdf", newLinkedFile.getLink()); assertEquals("PDF", newLinkedFile.getFileType()); }); @@ -308,7 +310,7 @@ void isSamePathWithPattern() throws IOException { // Tests if added parameters to mimeType gets parsed to correct format. @Test void mimeTypeStringWithParameterIsReturnedAsWithoutParameter() { - Optional test = externalFileType.getExternalFileTypeByMimeType("text/html; charset=UTF-8"); + Optional test = externalFileType.getExternalFileTypeByMimeType("text/html; charset=UTF-8", filePreferences); String actual = test.get().toString(); assertEquals("URL", actual); } From 30c5a97372df732859cf21161300744d68728a09 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 15:23:59 +0200 Subject: [PATCH 08/13] Made util functions static --- .../externalfiletype/ExternalFileTypes.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index e04e21aa648..6aa8b1fc28d 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -25,7 +25,7 @@ public enum ExternalFileTypes { // modifications, in order to indicate a removed default file type: private static final String FILE_TYPE_REMOVED_FLAG = "REMOVED"; - private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; + private static final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; /** * @deprecated use {@link FilePreferences#getExternalFileTypes()} instead. @@ -53,7 +53,7 @@ public Set getExternalFileTypes() { * @param name The file type name. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeByName(String name, FilePreferences filePreferences) { + public static Optional getExternalFileTypeByName(String name, FilePreferences filePreferences) { Optional externalFileType = filePreferences.getExternalFileTypes().stream().filter(type -> type.getName().equals(name)).findFirst(); if (externalFileType.isPresent()) { return externalFileType; @@ -68,7 +68,7 @@ public Optional getExternalFileTypeByName(String name, FilePre * @param extension The file extension. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeByExt(String extension, FilePreferences filePreferences) { + public static Optional getExternalFileTypeByExt(String extension, FilePreferences filePreferences) { String extensionCleaned = extension.replace(".", "").replace("*", ""); return filePreferences.getExternalFileTypes().stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); } @@ -79,7 +79,7 @@ public Optional getExternalFileTypeByExt(String extension, Fil * @param extension The file extension. * @return true if an ExternalFileType with the extension exists, false otherwise */ - public boolean isExternalFileTypeByExt(String extension, FilePreferences filePreferences) { + public static boolean isExternalFileTypeByExt(String extension, FilePreferences filePreferences) { return filePreferences.getExternalFileTypes().stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); } @@ -89,7 +89,7 @@ public boolean isExternalFileTypeByExt(String extension, FilePreferences filePre * @param filename The name of the file whose type to look up. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeForName(String filename, FilePreferences filePreferences) { + public static Optional getExternalFileTypeForName(String filename, FilePreferences filePreferences) { int longestFound = -1; ExternalFileType foundType = null; for (ExternalFileType type : filePreferences.getExternalFileTypes()) { @@ -109,7 +109,7 @@ public Optional getExternalFileTypeForName(String filename, Fi * @return The ExternalFileType registered, or null if none. For the mime type "text/html", a valid file type is * guaranteed to be returned. */ - public Optional getExternalFileTypeByMimeType(String mimeType, FilePreferences filePreferences) { + public static Optional getExternalFileTypeByMimeType(String mimeType, FilePreferences filePreferences) { // Ignores parameters according to link: (https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) if (mimeType.indexOf(';') != -1) { mimeType = mimeType.substring(0, mimeType.indexOf(';')).trim(); @@ -126,13 +126,13 @@ public Optional getExternalFileTypeByMimeType(String mimeType, } } - public Optional getExternalFileTypeByFile(Path file, FilePreferences filePreferences) { + public static Optional getExternalFileTypeByFile(Path file, FilePreferences filePreferences) { final String filePath = file.toString(); final Optional extension = FileHelper.getFileExtension(filePath); - return extension.flatMap(ext -> this.getExternalFileTypeByExt(ext, filePreferences)); + return extension.flatMap(ext -> getExternalFileTypeByExt(ext, filePreferences)); } - public Optional getExternalFileTypeByLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType, FilePreferences filePreferences) { + public static Optional getExternalFileTypeByLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType, FilePreferences filePreferences) { Optional type = getExternalFileTypeByName(linkedFile.getFileType(), filePreferences); boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); @@ -145,7 +145,7 @@ public Optional getExternalFileTypeByLinkedFile(LinkedFile lin // No type could be found from mime type. Try based on the extension: return FileHelper.getFileExtension(linkedFile.getLink()) - .flatMap(extension -> this.getExternalFileTypeByExt(extension, filePreferences)); + .flatMap(extension -> getExternalFileTypeByExt(extension, filePreferences)); } else { return type; } From 8af2e32027670307c7c1969d29647d1aea5e336e Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 16:03:52 +0200 Subject: [PATCH 09/13] Removed call to getExternalFileTypes --- .../org/jabref/cli/ArgumentProcessor.java | 4 +- .../org/jabref/gui/EntryTypeViewModel.java | 2 - src/main/java/org/jabref/gui/JabRefFrame.java | 3 +- src/main/java/org/jabref/gui/LibraryTab.java | 13 ++---- .../BibtexExtractorViewModel.java | 2 - .../jabref/gui/entryeditor/EntryEditor.java | 5 +-- .../externalfiles/AutoLinkFilesAction.java | 4 +- .../externalfiles/AutoSetFileLinksUtil.java | 12 +++--- .../externalfiles/DownloadFullTextAction.java | 4 +- .../ExternalFilesEntryLinker.java | 6 +-- .../externalfiles/FileExtensionViewModel.java | 6 +-- .../gui/externalfiles/ImportHandler.java | 4 +- .../UnlinkedFilesDialogView.java | 3 +- .../UnlinkedFilesDialogViewModel.java | 9 ++-- .../externalfiletype/ExternalFileTypes.java | 9 ---- .../gui/fieldeditors/LinkedFileViewModel.java | 14 +++---- .../LinkedFilesEditorViewModel.java | 30 +++++--------- .../importer/GenerateEntryFromIdAction.java | 11 +++-- .../gui/importer/ImportEntriesViewModel.java | 5 +-- .../gui/linkedfile/AttachFileAction.java | 2 - .../linkedfile/LinkedFileEditDialogView.java | 5 +-- .../LinkedFilesEditDialogViewModel.java | 17 ++++---- .../org/jabref/gui/maintable/CellFactory.java | 5 +-- .../org/jabref/gui/maintable/MainTable.java | 5 +-- .../gui/maintable/MainTableColumnFactory.java | 8 +--- .../gui/maintable/OpenExternalFileAction.java | 7 +--- .../gui/maintable/OpenFolderAction.java | 9 +--- .../gui/maintable/columns/FileColumn.java | 20 +++------ .../preferences/table/TableTabViewModel.java | 9 ++-- .../org/jabref/gui/preview/PreviewPanel.java | 2 +- .../jabref/gui/search/GlobalSearchBar.java | 3 +- .../gui/search/GlobalSearchResultDialog.java | 7 +--- .../jabref/gui/search/SearchResultsTable.java | 5 +-- .../AutoSetFileLinksUtilTest.java | 14 ++++--- .../fieldeditors/LinkedFileViewModelTest.java | 41 +++++++++---------- 35 files changed, 110 insertions(+), 195 deletions(-) diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index f1aaa0653d7..e99e62fe40f 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -15,7 +15,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.externalfiles.AutoSetFileLinksUtil; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.undo.NamedCompound; import org.jabref.logic.JabRefException; import org.jabref.logic.bibtex.FieldWriterPreferences; @@ -640,8 +639,7 @@ private void automaticallySetFileLinks(List loaded) { AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( parserResult.getDatabaseContext(), preferencesService.getFilePreferences(), - preferencesService.getAutoLinkPreferences(), - ExternalFileTypes.getInstance()); + preferencesService.getAutoLinkPreferences()); util.linkAssociatedFiles(database.getEntries(), new NamedCompound("")); } } diff --git a/src/main/java/org/jabref/gui/EntryTypeViewModel.java b/src/main/java/org/jabref/gui/EntryTypeViewModel.java index 16782b19c00..da5e0f5a091 100644 --- a/src/main/java/org/jabref/gui/EntryTypeViewModel.java +++ b/src/main/java/org/jabref/gui/EntryTypeViewModel.java @@ -15,7 +15,6 @@ import javafx.concurrent.Worker; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.importer.NewEntryAction; import org.jabref.logic.importer.FetcherException; import org.jabref.logic.importer.IdBasedFetcher; @@ -159,7 +158,6 @@ public void runFetcherWorker() { ImportHandler handler = new ImportHandler( libraryTab.getBibDatabaseContext(), - ExternalFileTypes.getInstance(), preferencesService, Globals.getFileUpdateMonitor(), libraryTab.getUndoManager(), diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 834f7beb325..9f4a1810ce6 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -81,7 +81,6 @@ import org.jabref.gui.externalfiles.AutoLinkFilesAction; import org.jabref.gui.externalfiles.DownloadFullTextAction; import org.jabref.gui.externalfiles.FindUnlinkedFilesAction; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.help.AboutAction; import org.jabref.gui.help.ErrorConsoleAction; import org.jabref.gui.help.HelpAction; @@ -1140,7 +1139,7 @@ private void trackOpenNewDatabase(LibraryTab libraryTab) { public LibraryTab addTab(BibDatabaseContext databaseContext, boolean raisePanel) { Objects.requireNonNull(databaseContext); - LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext, ExternalFileTypes.getInstance()); + LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext); addTab(libraryTab, raisePanel); return libraryTab; } diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index 139cdf31fd0..f19a58c3181 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -29,7 +29,6 @@ import org.jabref.gui.collab.DatabaseChangeMonitor; import org.jabref.gui.dialogs.AutosaveUiManager; import org.jabref.gui.entryeditor.EntryEditor; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.importer.actions.OpenDatabaseAction; import org.jabref.gui.maintable.MainTable; import org.jabref.gui.maintable.MainTableDataModel; @@ -83,7 +82,6 @@ public class LibraryTab extends Tab { private static final Logger LOGGER = LoggerFactory.getLogger(LibraryTab.class); private final JabRefFrame frame; private final CountingUndoManager undoManager; - private final ExternalFileTypes externalFileTypes; private final DialogService dialogService; private final PreferencesService preferencesService; private final StateManager stateManager; @@ -119,11 +117,9 @@ public LibraryTab(JabRefFrame frame, PreferencesService preferencesService, StateManager stateManager, ThemeManager themeManager, - BibDatabaseContext bibDatabaseContext, - ExternalFileTypes externalFileTypes) { + BibDatabaseContext bibDatabaseContext) { this.frame = Objects.requireNonNull(frame); this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.undoManager = frame.getUndoManager(); this.dialogService = frame.getDialogService(); this.preferencesService = Objects.requireNonNull(preferencesService); @@ -152,7 +148,7 @@ public LibraryTab(JabRefFrame frame, this.getDatabase().registerListener(new UpdateTimestampListener(preferencesService)); - this.entryEditor = new EntryEditor(this, externalFileTypes); + this.entryEditor = new EntryEditor(this); // set LibraryTab ID for drag'n'drop // ID content doesn't matter, we only need different tabs to have different ID @@ -253,7 +249,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) { this.getDatabase().registerListener(new UpdateTimestampListener(preferencesService)); - this.entryEditor = new EntryEditor(this, externalFileTypes); + this.entryEditor = new EntryEditor(this); Platform.runLater(() -> { EasyBind.subscribe(changedProperty, this::updateTabTitle); @@ -484,7 +480,6 @@ private void createMainTable() { preferencesService, dialogService, stateManager, - externalFileTypes, Globals.getKeyPrefs()); // Add the listener that binds selection to state manager (TODO: should be replaced by proper JavaFX binding as soon as table is implemented in JavaFX) @@ -802,7 +797,7 @@ public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService prefere BibDatabaseContext context = new BibDatabaseContext(); context.setDatabasePath(file); - LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context, ExternalFileTypes.getInstance()); + LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context); newTab.setDataLoadingTask(dataLoadingTask); dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted) diff --git a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java index 3472e69d466..e58edc21a56 100644 --- a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java +++ b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java @@ -13,7 +13,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.StateManager; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.importer.FetcherException; @@ -50,7 +49,6 @@ public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext, this.taskExecutor = taskExecutor; this.importHandler = new ImportHandler( bibdatabaseContext, - ExternalFileTypes.getInstance(), preferencesService, fileUpdateMonitor, undoManager, diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index 5066e762b1c..bc6d741b7e9 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -113,7 +113,7 @@ public class EntryEditor extends BorderPane { private final List entryEditorTabs = new LinkedList<>(); - public EntryEditor(LibraryTab libraryTab, ExternalFileTypes externalFileTypes) { + public EntryEditor(LibraryTab libraryTab) { this.libraryTab = libraryTab; this.databaseContext = libraryTab.getBibDatabaseContext(); @@ -122,8 +122,7 @@ public EntryEditor(LibraryTab libraryTab, ExternalFileTypes externalFileTypes) { .load(); this.entryEditorPreferences = preferencesService.getEntryEditorPreferences(); - this.fileLinker = new ExternalFilesEntryLinker(externalFileTypes, preferencesService.getFilePreferences(), - databaseContext); + this.fileLinker = new ExternalFilesEntryLinker(preferencesService.getFilePreferences(), databaseContext); EasyBind.subscribe(tabbed.getSelectionModel().selectedItemProperty(), tab -> { EntryEditorTab activeTab = (EntryEditorTab) tab; diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java b/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java index 6c51f8889dd..689d79dca69 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java @@ -9,7 +9,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.undo.NamedCompound; import org.jabref.gui.util.BindingsHelper; import org.jabref.gui.util.TaskExecutor; @@ -51,8 +50,7 @@ public void execute() { final AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( database, preferences.getFilePreferences(), - preferences.getAutoLinkPreferences(), - ExternalFileTypes.getInstance()); + preferences.getAutoLinkPreferences()); final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links")); Task linkFilesTask = new Task<>() { diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java index 821e1d10837..4d1f35e3b40 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java @@ -56,17 +56,15 @@ public List getFileExceptions() { private final List directories; private final AutoLinkPreferences autoLinkPreferences; private final FilePreferences filePreferences; - private final ExternalFileTypes externalFileTypes; - public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { - this(databaseContext.getFileDirectories(filePreferences), filePreferences, autoLinkPreferences, externalFileTypes); + public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences) { + this(databaseContext.getFileDirectories(filePreferences), filePreferences, autoLinkPreferences); } - private AutoSetFileLinksUtil(List directories, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { + private AutoSetFileLinksUtil(List directories, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences) { this.directories = directories; this.autoLinkPreferences = autoLinkPreferences; this.filePreferences = filePreferences; - this.externalFileTypes = externalFileTypes; } public LinkFilesResult linkAssociatedFiles(List entries, NamedCompound ce) { @@ -109,7 +107,7 @@ public LinkFilesResult linkAssociatedFiles(List entries, NamedCompound public List findAssociatedNotLinkedFiles(BibEntry entry) throws IOException { List linkedFiles = new ArrayList<>(); - List extensions = externalFileTypes.getExternalFileTypes().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); + List extensions = filePreferences.getExternalFileTypes().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); // Run the search operation FileFinder fileFinder = FileFinders.constructFromConfiguration(autoLinkPreferences); @@ -130,7 +128,7 @@ public List findAssociatedNotLinkedFiles(BibEntry entry) throws IOEx if (!fileAlreadyLinked) { Optional type = FileHelper.getFileExtension(foundFile) - .map(extension -> externalFileTypes.getExternalFileTypeByExt(extension, filePreferences)) + .map(extension -> ExternalFileTypes.getExternalFileTypeByExt(extension, filePreferences)) .orElse(Optional.of(new UnknownExternalFileType(""))); String strType = type.isPresent() ? type.get().getName() : ""; diff --git a/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java b/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java index 53be917cea1..be8b74ede44 100644 --- a/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java +++ b/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java @@ -14,7 +14,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.logic.importer.FulltextFetchers; import org.jabref.logic.l10n.Localization; @@ -146,8 +145,7 @@ private void addLinkedFileFromURL(BibDatabaseContext databaseContext, URL url, B databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferences, - ExternalFileTypes.getInstance()); + preferences); onlineFile.download(); } else { diff --git a/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java b/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java index fbb28a36559..7f9591c60f4 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java +++ b/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java @@ -26,14 +26,12 @@ public class ExternalFilesEntryLinker { private static final Logger LOGGER = LoggerFactory.getLogger(ExternalFilesEntryLinker.class); - private final ExternalFileTypes externalFileTypes; private final FilePreferences filePreferences; private final BibDatabaseContext bibDatabaseContext; private final MoveFilesCleanup moveFilesCleanup; private final RenamePdfCleanup renameFilesCleanup; - public ExternalFilesEntryLinker(ExternalFileTypes externalFileTypes, FilePreferences filePreferences, BibDatabaseContext bibDatabaseContext) { - this.externalFileTypes = externalFileTypes; + public ExternalFilesEntryLinker(FilePreferences filePreferences, BibDatabaseContext bibDatabaseContext) { this.filePreferences = filePreferences; this.bibDatabaseContext = bibDatabaseContext; this.moveFilesCleanup = new MoveFilesCleanup(bibDatabaseContext, filePreferences); @@ -62,7 +60,7 @@ public void moveLinkedFilesToFileDir(BibEntry entry) { public void addFilesToEntry(BibEntry entry, List files) { for (Path file : files) { FileUtil.getFileExtension(file).ifPresent(ext -> { - ExternalFileType type = externalFileTypes.getExternalFileTypeByExt(ext, filePreferences) + ExternalFileType type = ExternalFileTypes.getExternalFileTypeByExt(ext, filePreferences) .orElse(new UnknownExternalFileType(ext)); Path relativePath = FileUtil.relativize(file, bibDatabaseContext.getFileDirectories(filePreferences)); LinkedFile linkedfile = new LinkedFile("", relativePath, type.getName()); diff --git a/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java b/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java index 5ab38a18847..6344babd905 100644 --- a/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java @@ -17,13 +17,11 @@ public class FileExtensionViewModel { private final String description; private final List extensions; - private final ExternalFileTypes externalFileTypes; private final FilePreferences filePreferences; - FileExtensionViewModel(FileType fileType, ExternalFileTypes externalFileTypes, FilePreferences filePreferences) { + FileExtensionViewModel(FileType fileType, FilePreferences filePreferences) { this.description = Localization.lang("%0 file", fileType.getName()); this.extensions = fileType.getExtensionsWithDot(); - this.externalFileTypes = externalFileTypes; this.filePreferences = filePreferences; } @@ -32,7 +30,7 @@ public String getDescription() { } public JabRefIcon getIcon() { - return externalFileTypes.getExternalFileTypeByExt(extensions.get(0), filePreferences) + return ExternalFileTypes.getExternalFileTypeByExt(extensions.get(0), filePreferences) .map(ExternalFileType::getIcon) .orElse(null); } diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index 7de10cf7b35..bed282c4523 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -15,7 +15,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.StateManager; import org.jabref.gui.duplicationFinder.DuplicateResolverDialog; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.undo.UndoableInsertEntries; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.DefaultTaskExecutor; @@ -51,7 +50,6 @@ public class ImportHandler { private final DialogService dialogService; public ImportHandler(BibDatabaseContext database, - ExternalFileTypes externalFileTypes, PreferencesService preferencesService, FileUpdateMonitor fileupdateMonitor, UndoManager undoManager, @@ -64,7 +62,7 @@ public ImportHandler(BibDatabaseContext database, this.stateManager = stateManager; this.dialogService = dialogService; - this.linker = new ExternalFilesEntryLinker(externalFileTypes, preferencesService.getFilePreferences(), database); + this.linker = new ExternalFilesEntryLinker(preferencesService.getFilePreferences(), database); this.contentImporter = new ExternalFilesContentImporter( preferencesService.getGeneralPreferences(), preferencesService.getImporterPreferences(), diff --git a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java index 2d1af899d12..668e507090b 100644 --- a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java +++ b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java @@ -31,7 +31,6 @@ import org.jabref.gui.actions.ActionFactory; import org.jabref.gui.actions.SimpleCommand; import org.jabref.gui.actions.StandardActions; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.BaseDialog; @@ -106,7 +105,7 @@ public UnlinkedFilesDialogView() { @FXML private void initialize() { - viewModel = new UnlinkedFilesDialogViewModel(dialogService, ExternalFileTypes.getInstance(), undoManager, fileUpdateMonitor, preferencesService, stateManager, taskExecutor); + viewModel = new UnlinkedFilesDialogViewModel(dialogService, undoManager, fileUpdateMonitor, preferencesService, stateManager, taskExecutor); progressDisplay.progressProperty().bind(viewModel.progressValueProperty()); progressText.textProperty().bind(viewModel.progressTextProperty()); diff --git a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java index 92a9372c742..6ba7fa1ee86 100644 --- a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java @@ -30,7 +30,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.DirectoryDialogConfiguration; import org.jabref.gui.util.FileDialogConfiguration; @@ -81,7 +80,6 @@ public class UnlinkedFilesDialogViewModel { private final FunctionBasedValidator scanDirectoryValidator; public UnlinkedFilesDialogViewModel(DialogService dialogService, - ExternalFileTypes externalFileTypes, UndoManager undoManager, FileUpdateMonitor fileUpdateMonitor, PreferencesService preferences, @@ -93,7 +91,6 @@ public UnlinkedFilesDialogViewModel(DialogService dialogService, this.bibDatabase = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null")); importHandler = new ImportHandler( bibDatabase, - externalFileTypes, preferences, fileUpdateMonitor, undoManager, @@ -101,9 +98,9 @@ public UnlinkedFilesDialogViewModel(DialogService dialogService, dialogService); this.fileFilterList = FXCollections.observableArrayList( - new FileExtensionViewModel(StandardFileType.ANY_FILE, externalFileTypes, preferences.getFilePreferences()), - new FileExtensionViewModel(StandardFileType.BIBTEX_DB, externalFileTypes, preferences.getFilePreferences()), - new FileExtensionViewModel(StandardFileType.PDF, externalFileTypes, preferences.getFilePreferences())); + new FileExtensionViewModel(StandardFileType.ANY_FILE, preferences.getFilePreferences()), + new FileExtensionViewModel(StandardFileType.BIBTEX_DB, preferences.getFilePreferences()), + new FileExtensionViewModel(StandardFileType.PDF, preferences.getFilePreferences())); this.dateFilterList = FXCollections.observableArrayList(DateRange.values()); diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 6aa8b1fc28d..14ac6e56b4f 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -10,7 +10,6 @@ import java.util.Optional; import java.util.Set; -import org.jabref.gui.Globals; import org.jabref.logic.bibtex.FileFieldWriter; import org.jabref.model.entry.LinkedFile; import org.jabref.model.strings.StringUtil; @@ -39,14 +38,6 @@ public static List getDefaultExternalFileTypes() { return Arrays.asList(StandardExternalFileType.values()); } - /** - * @deprecated use {@link FilePreferences#getExternalFileTypes()} instead. - */ - @Deprecated - public Set getExternalFileTypes() { - return Globals.prefs.getFilePreferences().getExternalFileTypes(); - } - /** * Look up the external file type registered with this name, if any. * diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index aa85437be0d..b2f9e1749d7 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -82,7 +82,6 @@ public class LinkedFileViewModel extends AbstractViewModel { private final TaskExecutor taskExecutor; private final PreferencesService preferences; private final LinkedFileHandler linkedFileHandler; - private final ExternalFileTypes externalFileTypes; private final Validator fileExistsValidator; @@ -91,8 +90,7 @@ public LinkedFileViewModel(LinkedFile linkedFile, BibDatabaseContext databaseContext, TaskExecutor taskExecutor, DialogService dialogService, - PreferencesService preferences, - ExternalFileTypes externalFileTypes) { + PreferencesService preferences) { this.linkedFile = linkedFile; this.preferences = preferences; @@ -101,7 +99,6 @@ public LinkedFileViewModel(LinkedFile linkedFile, this.entry = entry; this.dialogService = dialogService; this.taskExecutor = taskExecutor; - this.externalFileTypes = externalFileTypes; fileExistsValidator = new FunctionBasedValidator<>( linkedFile.linkProperty(), @@ -176,7 +173,7 @@ public Optional findIn(List directories) { } public JabRefIcon getTypeIcon() { - return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, preferences.getFilePreferences()) + return ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, preferences.getFilePreferences()) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE); } @@ -199,7 +196,7 @@ public Observable[] getObservables() { public void open() { try { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByLinkedFile(linkedFile, true, preferences.getFilePreferences()); + Optional type = ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, true, preferences.getFilePreferences()); boolean successful = JabRefDesktop.openExternalFileAnyFormat(databaseContext, preferences, linkedFile.getLink(), type); if (!successful) { dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink())); @@ -447,7 +444,6 @@ public void download() { LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile( destination, databaseContext.getFileDirectories(preferences.getFilePreferences()), - externalFileTypes, preferences.getFilePreferences()); List linkedFiles = entry.getFiles(); @@ -527,7 +523,7 @@ private Optional inferFileTypeFromMimeType(URLDownload urlDown if (mimeType != null) { LOGGER.debug("MIME Type suggested: " + mimeType); - return externalFileTypes.getExternalFileTypeByMimeType(mimeType, preferences.getFilePreferences()); + return ExternalFileTypes.getExternalFileTypeByMimeType(mimeType, preferences.getFilePreferences()); } else { return Optional.empty(); } @@ -535,7 +531,7 @@ private Optional inferFileTypeFromMimeType(URLDownload urlDown private Optional inferFileTypeFromURL(String url) { return URLUtil.getSuffix(url, preferences.getFilePreferences()) - .flatMap(extension -> externalFileTypes.getExternalFileTypeByExt(extension, preferences.getFilePreferences())); + .flatMap(extension -> ExternalFileTypes.getExternalFileTypeByExt(extension, preferences.getFilePreferences())); } public LinkedFile getFile() { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java index 0cccc94186d..44a3f15d538 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java @@ -49,7 +49,6 @@ public class LinkedFilesEditorViewModel extends AbstractEditorViewModel { private final BibDatabaseContext databaseContext; private final TaskExecutor taskExecutor; private final PreferencesService preferences; - private final ExternalFileTypes externalFileTypes = ExternalFileTypes.getInstance(); public LinkedFilesEditorViewModel(Field field, SuggestionProvider suggestionProvider, DialogService dialogService, @@ -88,11 +87,10 @@ private static String getStringRepresentation(List files) { * * TODO: Move this method to {@link LinkedFile} as soon as {@link CustomExternalFileType} lives in model. */ - public static LinkedFile fromFile(Path file, List fileDirectories, ExternalFileTypes externalFileTypesFile, FilePreferences filePreferences) { + public static LinkedFile fromFile(Path file, List fileDirectories, FilePreferences filePreferences) { String fileExtension = FileHelper.getFileExtension(file).orElse(""); - ExternalFileType suggestedFileType = externalFileTypesFile - .getExternalFileTypeByExt(fileExtension, filePreferences) - .orElse(new UnknownExternalFileType(fileExtension)); + ExternalFileType suggestedFileType = ExternalFileTypes.getExternalFileTypeByExt(fileExtension, filePreferences) + .orElse(new UnknownExternalFileType(fileExtension)); Path relativePath = FileUtil.relativize(file, fileDirectories); return new LinkedFile("", relativePath, suggestedFileType.getName()); } @@ -100,15 +98,14 @@ public static LinkedFile fromFile(Path file, List fileDirectories, Externa public LinkedFileViewModel fromFile(Path file, FilePreferences filePreferences) { List fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences()); - LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes, filePreferences); + LinkedFile linkedFile = fromFile(file, fileDirectories, filePreferences); return new LinkedFileViewModel( linkedFile, entry, databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes); + preferences); } public boolean isFulltextLookupInProgress() { @@ -123,8 +120,7 @@ private List parseToFileViewModel(String stringValue) { databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes)) + preferences)) .collect(Collectors.toList()); } @@ -146,15 +142,14 @@ public void addNewFile() { List fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences()); dialogService.showFileOpenDialogAndGetMultipleFiles(fileDialogConfiguration).forEach(newFile -> { - LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes, preferences.getFilePreferences()); + LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, preferences.getFilePreferences()); files.add(new LinkedFileViewModel( newLinkedFile, entry, databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes)); + preferences)); }); } @@ -179,8 +174,7 @@ private List findAssociatedNotLinkedFiles(BibEntry entry) { AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( databaseContext, preferences.getFilePreferences(), - preferences.getAutoLinkPreferences(), - ExternalFileTypes.getInstance()); + preferences.getAutoLinkPreferences()); try { List linkedFiles = util.findAssociatedNotLinkedFiles(entry); for (LinkedFile linkedFile : linkedFiles) { @@ -190,8 +184,7 @@ private List findAssociatedNotLinkedFiles(BibEntry entry) { databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes); + preferences); newLinkedFile.markAsAutomaticallyFound(); result.add(newLinkedFile); } @@ -242,8 +235,7 @@ private void addFromURL(URL url) { databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes); + preferences); files.add(onlineFile); onlineFile.download(); } diff --git a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java index 4c5c8a5747f..8b1b4b60c4b 100644 --- a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java +++ b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java @@ -8,7 +8,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.SimpleCommand; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.JabRefException; @@ -31,7 +30,13 @@ public class GenerateEntryFromIdAction extends SimpleCommand { private final PopOver entryFromIdPopOver; private final StateManager stateManager; - public GenerateEntryFromIdAction(LibraryTab libraryTab, DialogService dialogService, PreferencesService preferencesService, TaskExecutor taskExecutor, PopOver entryFromIdPopOver, String identifier, StateManager stateManager) { + public GenerateEntryFromIdAction(LibraryTab libraryTab, + DialogService dialogService, + PreferencesService preferencesService, + TaskExecutor taskExecutor, + PopOver entryFromIdPopOver, + String identifier, + StateManager stateManager) { this.libraryTab = libraryTab; this.dialogService = dialogService; this.preferencesService = preferencesService; @@ -62,7 +67,7 @@ public void execute() { Optional result = bibEntry; if (result.isPresent()) { final BibEntry entry = result.get(); - ImportHandler handler = new ImportHandler(libraryTab.getBibDatabaseContext(), ExternalFileTypes.getInstance(), preferencesService, Globals.getFileUpdateMonitor(), libraryTab.getUndoManager(), stateManager, dialogService); + ImportHandler handler = new ImportHandler(libraryTab.getBibDatabaseContext(), preferencesService, Globals.getFileUpdateMonitor(), libraryTab.getUndoManager(), stateManager, dialogService); handler.importEntryWithDuplicateCheck(libraryTab.getBibDatabaseContext(), entry); } else { dialogService.notify("No entry found or import canceled"); diff --git a/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java b/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java index b139a4573c4..65e0f3e8864 100644 --- a/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java +++ b/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java @@ -16,7 +16,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.duplicationFinder.DuplicateResolverDialog; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; @@ -148,8 +147,7 @@ public void importEntries(List entriesToImport, boolean shouldDownload databaseContext, taskExecutor, dialogService, - preferences, - ExternalFileTypes.getInstance()).download()); + preferences).download()); } } @@ -165,7 +163,6 @@ public void importEntries(List entriesToImport, boolean shouldDownload private void buildImportHandlerThenImportEntries(List entriesToImport) { ImportHandler importHandler = new ImportHandler( databaseContext, - ExternalFileTypes.getInstance(), preferences, fileUpdateMonitor, undoManager, diff --git a/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java b/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java index d7eae9b4729..8dad9f0399c 100644 --- a/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java +++ b/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java @@ -8,7 +8,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModel; import org.jabref.gui.undo.UndoableFieldChange; import org.jabref.gui.util.FileDialogConfiguration; @@ -65,7 +64,6 @@ public void execute() { LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile( newFile, databaseContext.getFileDirectories(filePreferences), - ExternalFileTypes.getInstance(), filePreferences); LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(linkedFile); diff --git a/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java b/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java index f8ad3e1f7bc..6ed39ce1ae9 100644 --- a/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java +++ b/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BaseDialog; import org.jabref.model.entry.LinkedFile; import org.jabref.preferences.PreferencesService; @@ -32,12 +31,10 @@ public class LinkedFileEditDialogView extends BaseDialog { private LinkedFilesEditDialogViewModel viewModel; private final LinkedFile linkedFile; - private final ExternalFileTypes externalFileTypes; public LinkedFileEditDialogView(LinkedFile linkedFile) { this.linkedFile = linkedFile; - this.externalFileTypes = ExternalFileTypes.getInstance(); ViewLoader.view(this) .load() .setAsContent(this.getDialogPane()); @@ -55,7 +52,7 @@ public LinkedFileEditDialogView(LinkedFile linkedFile) { @FXML private void initialize() { - viewModel = new LinkedFilesEditDialogViewModel(linkedFile, stateManager.getActiveDatabase().get(), dialogService, preferences.getFilePreferences(), externalFileTypes); + viewModel = new LinkedFilesEditDialogViewModel(linkedFile, stateManager.getActiveDatabase().get(), dialogService, preferences.getFilePreferences()); fileType.itemsProperty().bindBidirectional(viewModel.externalFileTypeProperty()); description.textProperty().bindBidirectional(viewModel.descriptionProperty()); link.textProperty().bindBidirectional(viewModel.linkProperty()); diff --git a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java index 3dafe95d25d..a46e908c358 100644 --- a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java +++ b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java @@ -41,14 +41,15 @@ public class LinkedFilesEditDialogViewModel extends AbstractViewModel { private final BibDatabaseContext database; private final DialogService dialogService; private final FilePreferences filePreferences; - private final ExternalFileTypes externalFileTypes; - public LinkedFilesEditDialogViewModel(LinkedFile linkedFile, BibDatabaseContext database, DialogService dialogService, FilePreferences filePreferences, ExternalFileTypes externalFileTypes) { + public LinkedFilesEditDialogViewModel(LinkedFile linkedFile, + BibDatabaseContext database, + DialogService dialogService, + FilePreferences filePreferences) { this.database = database; this.dialogService = dialogService; this.filePreferences = filePreferences; - this.externalFileTypes = externalFileTypes; - allExternalFileTypes.set(FXCollections.observableArrayList(externalFileTypes.getExternalFileTypes())); + allExternalFileTypes.set(FXCollections.observableArrayList(filePreferences.getExternalFileTypes())); monadicSelectedExternalFileType = EasyBind.wrapNullable(selectedExternalFileType); setValues(linkedFile); @@ -58,12 +59,14 @@ private void setExternalFileTypeByExtension(String link) { if (!link.isEmpty()) { // Check if this looks like a remote link: if (REMOTE_LINK_PATTERN.matcher(link).matches()) { - externalFileTypes.getExternalFileTypeByExt("html", filePreferences).ifPresent(selectedExternalFileType::setValue); + ExternalFileTypes.getExternalFileTypeByExt("html", filePreferences) + .ifPresent(selectedExternalFileType::setValue); } // Try to guess the file type: String theLink = link.trim(); - externalFileTypes.getExternalFileTypeForName(theLink, filePreferences).ifPresent(selectedExternalFileType::setValue); + ExternalFileTypes.getExternalFileTypeForName(theLink, filePreferences) + .ifPresent(selectedExternalFileType::setValue); } } @@ -101,7 +104,7 @@ public void setValues(LinkedFile linkedFile) { selectedExternalFileType.setValue(null); // See what is a reasonable selection for the type combobox: - Optional fileType = externalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, filePreferences); + Optional fileType = ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, filePreferences); if (fileType.isPresent() && !(fileType.get() instanceof UnknownExternalFileType)) { selectedExternalFileType.setValue(fileType.get()); } else if ((linkedFile.getLink() != null) && (!linkedFile.getLink().isEmpty())) { diff --git a/src/main/java/org/jabref/gui/maintable/CellFactory.java b/src/main/java/org/jabref/gui/maintable/CellFactory.java index a937acf9e7d..13c65f6a336 100644 --- a/src/main/java/org/jabref/gui/maintable/CellFactory.java +++ b/src/main/java/org/jabref/gui/maintable/CellFactory.java @@ -8,7 +8,6 @@ import javafx.scene.Node; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.specialfields.SpecialFieldViewModel; @@ -22,7 +21,7 @@ public class CellFactory { private final Map TABLE_ICONS = new HashMap<>(); - public CellFactory(ExternalFileTypes externalFileTypes, PreferencesService preferencesService, UndoManager undoManager) { + public CellFactory(PreferencesService preferencesService, UndoManager undoManager) { JabRefIcon icon; icon = IconTheme.JabRefIcons.PDF_FILE; // icon.setToo(Localization.lang("Open") + " PDF"); @@ -56,7 +55,7 @@ public CellFactory(ExternalFileTypes externalFileTypes, PreferencesService prefe // icon.setToolTipText(Localization.lang("Open file")); TABLE_ICONS.put(StandardField.FILE, icon); - for (ExternalFileType fileType : externalFileTypes.getExternalFileTypes()) { + for (ExternalFileType fileType : preferencesService.getFilePreferences().getExternalFileTypes()) { icon = fileType.getIcon(); // icon.setToolTipText(Localization.lang("Open %0 file", fileType.getName())); TABLE_ICONS.put(fileType.getField(), icon); diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index 32a80afb754..2244575b228 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -32,7 +32,6 @@ import org.jabref.gui.actions.StandardActions; import org.jabref.gui.edit.EditAction; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.keyboard.KeyBindingRepository; import org.jabref.gui.maintable.columns.LibraryColumn; @@ -73,7 +72,6 @@ public MainTable(MainTableDataModel model, PreferencesService preferencesService, DialogService dialogService, StateManager stateManager, - ExternalFileTypes externalFileTypes, KeyBindingRepository keyBindingRepository) { super(); @@ -86,7 +84,7 @@ public MainTable(MainTableDataModel model, MainTablePreferences mainTablePreferences = preferencesService.getMainTablePreferences(); importHandler = new ImportHandler( - database, externalFileTypes, + database, preferencesService, Globals.getFileUpdateMonitor(), undoManager, @@ -103,7 +101,6 @@ public MainTable(MainTableDataModel model, database, preferencesService, preferencesService.getColumnPreferences(), - externalFileTypes, libraryTab.getUndoManager(), dialogService, stateManager).createColumns()); diff --git a/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java b/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java index ece234f9ff4..52cf6a2f04f 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java +++ b/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java @@ -23,7 +23,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.maintable.columns.FieldColumn; import org.jabref.gui.maintable.columns.FileColumn; @@ -54,7 +53,6 @@ public class MainTableColumnFactory { private final PreferencesService preferencesService; private final ColumnPreferences columnPreferences; - private final ExternalFileTypes externalFileTypes; private final BibDatabaseContext database; private final CellFactory cellFactory; private final UndoManager undoManager; @@ -64,16 +62,14 @@ public class MainTableColumnFactory { public MainTableColumnFactory(BibDatabaseContext database, PreferencesService preferencesService, ColumnPreferences abstractColumnPrefs, - ExternalFileTypes externalFileTypes, UndoManager undoManager, DialogService dialogService, StateManager stateManager) { this.database = Objects.requireNonNull(database); this.preferencesService = Objects.requireNonNull(preferencesService); this.columnPreferences = abstractColumnPrefs; - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.dialogService = dialogService; - this.cellFactory = new CellFactory(externalFileTypes, preferencesService, undoManager); + this.cellFactory = new CellFactory(preferencesService, undoManager); this.undoManager = undoManager; this.stateManager = stateManager; } @@ -233,7 +229,6 @@ private TableColumn private TableColumn> createFilesColumn(MainTableColumnModel columnModel) { return new FileColumn(columnModel, database, - externalFileTypes, dialogService, preferencesService); } @@ -244,7 +239,6 @@ private TableColumn> createFilesColumn( private TableColumn> createExtraFileColumn(MainTableColumnModel columnModel) { return new FileColumn(columnModel, database, - externalFileTypes, dialogService, preferencesService, columnModel.getQualifier()); diff --git a/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java b/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java index 9f669a18431..499f07a3526 100644 --- a/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java +++ b/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java @@ -8,7 +8,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.BibEntry; @@ -68,8 +67,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance()); + preferencesService); linkedFileViewModelList.add(linkedFileViewModel); } @@ -93,8 +91,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance()); + preferencesService); linkedFileViewModel.open(); } }); diff --git a/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java b/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java index 8ce16f62f1b..92c8404ca39 100644 --- a/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java +++ b/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java @@ -5,7 +5,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; @@ -49,9 +48,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance() - ); + preferencesService); linkedFileViewModel.openFolder(); }); } else { @@ -61,9 +58,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance() - ); + preferencesService); linkedFileViewModel.openFolder(); } }); diff --git a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java index 9ab7a17318e..5aaeeb207a0 100644 --- a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java +++ b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java @@ -32,7 +32,6 @@ */ public class FileColumn extends MainTableColumn> { - private final ExternalFileTypes externalFileTypes; private final DialogService dialogService; private final BibDatabaseContext database; private final PreferencesService preferencesService; @@ -42,11 +41,9 @@ public class FileColumn extends MainTableColumn> { */ public FileColumn(MainTableColumnModel model, BibDatabaseContext database, - ExternalFileTypes externalFileTypes, DialogService dialogService, PreferencesService preferencesService) { super(model); - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.database = Objects.requireNonNull(database); this.dialogService = dialogService; this.preferencesService = preferencesService; @@ -68,8 +65,7 @@ public FileColumn(MainTableColumnModel model, entry.getEntry(), database, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - externalFileTypes); + preferencesService); linkedFileViewModel.open(); } }) @@ -81,22 +77,19 @@ public FileColumn(MainTableColumnModel model, */ public FileColumn(MainTableColumnModel model, BibDatabaseContext database, - ExternalFileTypes externalFileTypes, DialogService dialogService, PreferencesService preferencesService, String fileType) { super(model); - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.database = Objects.requireNonNull(database); this.dialogService = dialogService; this.preferencesService = preferencesService; setCommonSettings(); - this.setGraphic(externalFileTypes - .getExternalFileTypeByName(fileType, preferencesService.getFilePreferences()) - .map(ExternalFileType::getIcon).orElse(IconTheme.JabRefIcons.FILE) - .getGraphicNode()); + this.setGraphic(ExternalFileTypes.getExternalFileTypeByName(fileType, preferencesService.getFilePreferences()) + .map(ExternalFileType::getIcon).orElse(IconTheme.JabRefIcons.FILE) + .getGraphicNode()); new ValueTableCellFactory>() .withGraphic(linkedFiles -> createFileIcon(linkedFiles.stream().filter(linkedFile -> @@ -131,8 +124,7 @@ private ContextMenu createFileMenu(BibEntryTableViewModel entry, List linkedFiles) { if (linkedFiles.size() > 1) { return IconTheme.JabRefIcons.FILE_MULTIPLE.getGraphicNode(); } else if (linkedFiles.size() == 1) { - return externalFileTypes.getExternalFileTypeByLinkedFile(linkedFiles.get(0), true, preferencesService.getFilePreferences()) + return ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFiles.get(0), true, preferencesService.getFilePreferences()) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE) .getGraphicNode(); diff --git a/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java b/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java index 81b66abe78e..ecae4a7d091 100644 --- a/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java @@ -14,7 +14,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.maintable.ColumnPreferences; import org.jabref.gui.maintable.MainTableColumnModel; import org.jabref.gui.maintable.MainTableNameFormatPreferences; @@ -180,10 +179,10 @@ private void removeSpecialFieldColumns() { } private void insertExtraFileColumns() { - ExternalFileTypes.getInstance().getExternalFileTypes().stream() - .map(ExternalFileType::getName) - .map(name -> new MainTableColumnModel(MainTableColumnModel.Type.EXTRAFILE, name)) - .forEach(item -> availableColumnsProperty.getValue().add(item)); + preferences.getFilePreferences().getExternalFileTypes().stream() + .map(ExternalFileType::getName) + .map(name -> new MainTableColumnModel(MainTableColumnModel.Type.EXTRAFILE, name)) + .forEach(item -> availableColumnsProperty.getValue().add(item)); } private void removeExtraFileColumns() { diff --git a/src/main/java/org/jabref/gui/preview/PreviewPanel.java b/src/main/java/org/jabref/gui/preview/PreviewPanel.java index f16b27d8b13..1372db4ec18 100644 --- a/src/main/java/org/jabref/gui/preview/PreviewPanel.java +++ b/src/main/java/org/jabref/gui/preview/PreviewPanel.java @@ -61,7 +61,7 @@ public PreviewPanel(BibDatabaseContext database, this.stateManager = stateManager; this.previewPreferences = preferences.getPreviewPreferences(); this.indexingTaskManager = indexingTaskManager; - this.fileLinker = new ExternalFilesEntryLinker(externalFileTypes, preferences.getFilePreferences(), database); + this.fileLinker = new ExternalFilesEntryLinker(preferences.getFilePreferences(), database); PreviewPreferences previewPreferences = preferences.getPreviewPreferences(); previewView = new PreviewViewer(database, dialogService, stateManager, themeManager); diff --git a/src/main/java/org/jabref/gui/search/GlobalSearchBar.java b/src/main/java/org/jabref/gui/search/GlobalSearchBar.java index d6b3bfc0364..d42bcd34cb7 100644 --- a/src/main/java/org/jabref/gui/search/GlobalSearchBar.java +++ b/src/main/java/org/jabref/gui/search/GlobalSearchBar.java @@ -47,7 +47,6 @@ import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding; import org.jabref.gui.autocompleter.PersonNameStringConverter; import org.jabref.gui.autocompleter.SuggestionProvider; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.keyboard.KeyBindingRepository; @@ -239,7 +238,7 @@ private void initSearchModifierButtons() { initSearchModifierButton(openGlobalSearchButton); openGlobalSearchButton.setOnAction(evt -> { globalSearchActive.setValue(true); - globalSearchResultDialog = new GlobalSearchResultDialog(ExternalFileTypes.getInstance(), undoManager); + globalSearchResultDialog = new GlobalSearchResultDialog(undoManager); performSearch(); globalSearchResultDialog.showAndWait(); globalSearchActive.setValue(false); diff --git a/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java b/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java index 59843b2e4cb..b23dc8ab2df 100644 --- a/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java +++ b/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.maintable.columns.SpecialFieldColumn; import org.jabref.gui.preview.PreviewViewer; @@ -28,7 +27,6 @@ public class GlobalSearchResultDialog extends BaseDialog { @FXML private SplitPane container; @FXML private ToggleButton keepOnTop; - private final ExternalFileTypes externalFileTypes; private final UndoManager undoManager; @Inject private PreferencesService preferencesService; @@ -38,9 +36,8 @@ public class GlobalSearchResultDialog extends BaseDialog { private GlobalSearchResultDialogViewModel viewModel; - public GlobalSearchResultDialog(ExternalFileTypes externalFileTypes, UndoManager undoManager) { + public GlobalSearchResultDialog(UndoManager undoManager) { this.undoManager = undoManager; - this.externalFileTypes = externalFileTypes; setTitle(Localization.lang("Search results from open libraries")); ViewLoader.view(this) @@ -57,7 +54,7 @@ private void initialize() { previewViewer.setLayout(preferencesService.getPreviewPreferences().getSelectedPreviewLayout()); SearchResultsTableDataModel model = new SearchResultsTableDataModel(viewModel.getSearchDatabaseContext(), preferencesService, stateManager); - SearchResultsTable resultsTable = new SearchResultsTable(model, viewModel.getSearchDatabaseContext(), preferencesService, undoManager, dialogService, stateManager, externalFileTypes); + SearchResultsTable resultsTable = new SearchResultsTable(model, viewModel.getSearchDatabaseContext(), preferencesService, undoManager, dialogService, stateManager); resultsTable.getColumns().removeIf(col -> col instanceof SpecialFieldColumn); resultsTable.getSelectionModel().selectFirst(); diff --git a/src/main/java/org/jabref/gui/search/SearchResultsTable.java b/src/main/java/org/jabref/gui/search/SearchResultsTable.java index 09083921f7a..a0f3ac20ea6 100644 --- a/src/main/java/org/jabref/gui/search/SearchResultsTable.java +++ b/src/main/java/org/jabref/gui/search/SearchResultsTable.java @@ -10,7 +10,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.maintable.BibEntryTableViewModel; import org.jabref.gui.maintable.MainTable; import org.jabref.gui.maintable.MainTableColumnFactory; @@ -28,8 +27,7 @@ public SearchResultsTable(SearchResultsTableDataModel model, PreferencesService preferencesService, UndoManager undoManager, DialogService dialogService, - StateManager stateManager, - ExternalFileTypes externalFileTypes) { + StateManager stateManager) { super(); MainTablePreferences mainTablePreferences = preferencesService.getMainTablePreferences(); @@ -38,7 +36,6 @@ public SearchResultsTable(SearchResultsTableDataModel model, database, preferencesService, preferencesService.getSearchDialogColumnPreferences(), - externalFileTypes, undoManager, dialogService, stateManager).createColumns(); diff --git a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java index d7fc174a767..5ad939179d9 100644 --- a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java +++ b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.TreeSet; +import javafx.collections.FXCollections; + import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.logic.util.io.AutoLinkPreferences; import org.jabref.model.database.BibDatabaseContext; @@ -25,14 +27,13 @@ public class AutoSetFileLinksUtilTest { - private final FilePreferences fileDirPrefs = mock(FilePreferences.class); + private final FilePreferences filePreferences = mock(FilePreferences.class); private final AutoLinkPreferences autoLinkPrefs = new AutoLinkPreferences( AutoLinkPreferences.CitationKeyDependency.START, "", false, ';'); private final BibDatabaseContext databaseContext = mock(BibDatabaseContext.class); - private final ExternalFileTypes externalFileTypes = mock(ExternalFileTypes.class); private final BibEntry entry = new BibEntry(StandardEntryType.Article); private Path path = null; @@ -41,7 +42,8 @@ public void setUp(@TempDir Path folder) throws Exception { path = folder.resolve("CiteKey.pdf"); Files.createFile(path); entry.setCitationKey("CiteKey"); - when(externalFileTypes.getExternalFileTypes()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); + when(filePreferences.getExternalFileTypes()) + .thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); } @Test @@ -49,15 +51,15 @@ public void testFindAssociatedNotLinkedFilesSuccess() throws Exception { // Due to mocking the externalFileType class, the file extension will not be found when(databaseContext.getFileDirectories(any())).thenReturn(Collections.singletonList(path.getParent())); List expected = Collections.singletonList(new LinkedFile("", Path.of("CiteKey.pdf"), "")); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, fileDirPrefs, autoLinkPrefs, externalFileTypes); + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, filePreferences, autoLinkPrefs); List actual = util.findAssociatedNotLinkedFiles(entry); assertEquals(expected, actual); } @Test public void testFindAssociatedNotLinkedFilesForEmptySearchDir() throws Exception { - when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, fileDirPrefs, autoLinkPrefs, externalFileTypes); + when(filePreferences.shouldStoreFilesRelativeToBibFile()).thenReturn(false); + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, filePreferences, autoLinkPrefs); List actual = util.findAssociatedNotLinkedFiles(entry); assertEquals(Collections.emptyList(), actual); } diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index aa6bc619426..d3a8917636a 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -46,8 +46,6 @@ import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.contains; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -64,7 +62,6 @@ class LinkedFileViewModelTest { private BibDatabaseContext databaseContext; private TaskExecutor taskExecutor; private DialogService dialogService; - private final ExternalFileTypes externalFileType = mock(ExternalFileTypes.class); private final FilePreferences filePreferences = mock(FilePreferences.class); private final PreferencesService preferences = mock(PreferencesService.class); private CookieManager cookieManager; @@ -78,10 +75,10 @@ void setUp(@TempDir Path tempFolder) throws Exception { dialogService = mock(DialogService.class); when(filePreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); - when(externalFileType.getExternalFileTypeByMimeType("application/pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); - when(externalFileType.getExternalFileTypeByMimeType(contains("text/html"), eq(filePreferences))).thenReturn(Optional.of(StandardExternalFileType.URL)); - when(externalFileType.getExternalFileTypeByExt("pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); - when(externalFileType.getExternalFileTypeByExt("html", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.URL)); +// when(ExternalFileTypes.getExternalFileTypeByMimeType("application/pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); +// when(ExternalFileTypes.getExternalFileTypeByMimeType(contains("text/html"), eq(filePreferences))).thenReturn(Optional.of(StandardExternalFileType.URL)); +// when(ExternalFileTypes.getExternalFileTypeByExt("pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); +// when(ExternalFileTypes.getExternalFileTypeByExt("html", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.URL)); when(preferences.getFilePreferences()).thenReturn(filePreferences); when(preferences.getXmpPreferences()).thenReturn(mock(XmpPreferences.class)); tempFile = tempFolder.resolve("temporaryFile"); @@ -108,7 +105,7 @@ void deleteWhenFilePathNotPresentReturnsTrue() { linkedFile = spy(new LinkedFile("", Path.of("nonexistent file"), "")); doReturn(Optional.empty()).when(linkedFile).findIn(any(BibDatabaseContext.class), any(FilePreferences.class)); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -126,7 +123,7 @@ void deleteWhenRemoveChosenReturnsTrueButDoesNotDeletesFile() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(3))); // first vararg - remove button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -144,7 +141,7 @@ void deleteWhenDeleteChosenReturnsTrueAndDeletesFile() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -162,7 +159,7 @@ void deleteMissingFileReturnsTrue() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -179,7 +176,7 @@ void deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(5))); // third vararg - cancel button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertFalse(removed); @@ -197,7 +194,7 @@ void downloadHtmlFileCausesWarningDisplay() throws MalformedURLException { String fileType = StandardExternalFileType.URL.getName(); linkedFile = new LinkedFile(url, fileType); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); viewModel.download(); @@ -212,11 +209,11 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); when(filePreferences.getFileDirectoryPattern()).thenReturn(""); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); BackgroundTask task = viewModel.prepareDownloadTask(tempFile.getParent(), new URLDownload("http://arxiv.org/pdf/1207.0408v1")); task.onSuccess(destination -> { - LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, Collections.singletonList(tempFile.getParent()), externalFileType, filePreferences); + LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, Collections.singletonList(tempFile.getParent()), filePreferences); assertEquals("asdf.pdf", newLinkedFile.getLink()); assertEquals("PDF", newLinkedFile.getFileType()); }); @@ -238,7 +235,7 @@ void downloadHtmlWhenLinkedFilePointsToHtml() throws MalformedURLException { databaseContext.setDatabasePath(tempFile); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); viewModel.download(); @@ -262,7 +259,7 @@ void isNotSamePath() { when(filePreferences.getFileDirectoryPattern()).thenReturn(""); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(Path.of("/home"))); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertFalse(viewModel.isGeneratedPathSameAsOriginal()); } @@ -274,7 +271,7 @@ void isSamePath() { when(filePreferences.getFileDirectoryPattern()).thenReturn(""); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent())); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertTrue(viewModel.isGeneratedPathSameAsOriginal()); } @@ -287,7 +284,7 @@ void isNotSamePathWithPattern() { when(filePreferences.getFileDirectoryPattern()).thenReturn("[entrytype]"); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent())); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertFalse(viewModel.isGeneratedPathSameAsOriginal()); } @@ -303,14 +300,14 @@ void isSamePathWithPattern() throws IOException { LinkedFileHandler fileHandler = new LinkedFileHandler(linkedFile, entry, databaseContext, filePreferences); fileHandler.moveToDefaultDirectory(); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertTrue(viewModel.isGeneratedPathSameAsOriginal()); } // Tests if added parameters to mimeType gets parsed to correct format. @Test void mimeTypeStringWithParameterIsReturnedAsWithoutParameter() { - Optional test = externalFileType.getExternalFileTypeByMimeType("text/html; charset=UTF-8", filePreferences); + Optional test = ExternalFileTypes.getExternalFileTypeByMimeType("text/html; charset=UTF-8", filePreferences); String actual = test.get().toString(); assertEquals("URL", actual); } @@ -325,7 +322,7 @@ void downloadPdfFileWhenLinkedFilePointsToPdfUrl() throws MalformedURLException databaseContext.setDatabasePath(tempFile); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); viewModel.download(); // Loop through downloaded files to check for filetype='pdf' From 6f16da2899489f320fd672c2f49bef832f02f7ce Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 16:16:49 +0200 Subject: [PATCH 10/13] Removed singleton --- .../org/jabref/gui/desktop/JabRefDesktop.java | 2 +- .../java/org/jabref/gui/desktop/os/Linux.java | 2 +- src/main/java/org/jabref/gui/desktop/os/OSX.java | 2 +- .../java/org/jabref/gui/desktop/os/Windows.java | 2 +- .../gui/entryeditor/DeprecatedFieldsTab.java | 4 +--- .../org/jabref/gui/entryeditor/EntryEditor.java | 15 +++++++-------- .../jabref/gui/entryeditor/FieldsEditorTab.java | 6 +----- .../gui/entryeditor/OptionalFields2Tab.java | 3 --- .../jabref/gui/entryeditor/OptionalFieldsTab.java | 3 --- .../gui/entryeditor/OptionalFieldsTabBase.java | 3 --- .../jabref/gui/entryeditor/OtherFieldsTab.java | 3 --- .../org/jabref/gui/entryeditor/PreviewTab.java | 8 ++------ .../jabref/gui/entryeditor/RequiredFieldsTab.java | 4 +--- .../gui/entryeditor/UserDefinedFieldsTab.java | 6 +----- .../gui/externalfiletype/ExternalFileTypes.java | 11 ++--------- .../java/org/jabref/gui/fieldeditors/URLUtil.java | 2 +- .../openoffice/StyleSelectDialogViewModel.java | 2 +- .../ProtectedTermsTabViewModel.java | 4 ++-- .../java/org/jabref/gui/preview/PreviewPanel.java | 2 -- 19 files changed, 23 insertions(+), 61 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index 57731fafa6c..517ffc1689d 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -219,7 +219,7 @@ public static void openFolderAndSelectFile(Path fileLink, PreferencesService pre * @param url the URL to open */ public static void openBrowser(String url) throws IOException { - Optional fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt("html", Globals.prefs.getFilePreferences()); + Optional fileType = ExternalFileTypes.getExternalFileTypeByExt("html", Globals.prefs.getFilePreferences()); openExternalFilePlatformIndependent(fileType, url); } diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index b81bd105a60..289efb4f576 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -49,7 +49,7 @@ private void nativeOpenFile(String filePath) { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); + Optional type = ExternalFileTypes.getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); String viewer; if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { diff --git a/src/main/java/org/jabref/gui/desktop/os/OSX.java b/src/main/java/org/jabref/gui/desktop/os/OSX.java index 2465cded8c3..d7a3c6cd710 100644 --- a/src/main/java/org/jabref/gui/desktop/os/OSX.java +++ b/src/main/java/org/jabref/gui/desktop/os/OSX.java @@ -15,7 +15,7 @@ public class OSX implements NativeDesktop { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); + Optional type = ExternalFileTypes.getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { openFileWithApplication(filePath, type.get().getOpenWithApplication()); } else { diff --git a/src/main/java/org/jabref/gui/desktop/os/Windows.java b/src/main/java/org/jabref/gui/desktop/os/Windows.java index ebc5946a3cb..d85722740d6 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -16,7 +16,7 @@ public class Windows implements NativeDesktop { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); + Optional type = ExternalFileTypes.getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { openFileWithApplication(filePath, type.get().getOpenWithApplication()); diff --git a/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java index 7127c0af919..d5bc20da9e6 100644 --- a/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java @@ -12,7 +12,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -39,10 +38,9 @@ public DeprecatedFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { - super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); + super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, taskExecutor, journalAbbreviationRepository, indexingTaskManager); this.entryTypesManager = entryTypesManager; setText(Localization.lang("Deprecated fields")); diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index bc6d741b7e9..b5f2d72ca39 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -34,7 +34,6 @@ import org.jabref.gui.entryeditor.fileannotationtab.FileAnnotationTab; import org.jabref.gui.entryeditor.fileannotationtab.FulltextSearchResultsTab; import org.jabref.gui.externalfiles.ExternalFilesEntryLinker; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.help.HelpAction; import org.jabref.gui.importer.GrobidOptInDialogHelper; import org.jabref.gui.keyboard.KeyBinding; @@ -241,22 +240,22 @@ private void navigateToNextEntry() { private List createTabs() { // Preview tab - entryEditorTabs.add(new PreviewTab(databaseContext, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), ExternalFileTypes.getInstance())); + entryEditorTabs.add(new PreviewTab(databaseContext, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager())); // Required fields - entryEditorTabs.add(new RequiredFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new RequiredFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); // Optional fields - entryEditorTabs.add(new OptionalFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); - entryEditorTabs.add(new OptionalFields2Tab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); - entryEditorTabs.add(new DeprecatedFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new OptionalFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new OptionalFields2Tab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new DeprecatedFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); // Other fields - entryEditorTabs.add(new OtherFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new OtherFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); // General fields from preferences for (Map.Entry> tab : entryEditorPreferences.getEntryEditorTabList().entrySet()) { - entryEditorTabs.add(new UserDefinedFieldsTab(tab.getKey(), tab.getValue(), databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new UserDefinedFieldsTab(tab.getKey(), tab.getValue(), databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), taskExecutor, journalAbbreviationRepository)); } // Special tabs diff --git a/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java b/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java index 342a519d6b7..49fc1d8715b 100644 --- a/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java @@ -26,7 +26,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.FieldEditorFX; import org.jabref.gui.fieldeditors.FieldEditors; import org.jabref.gui.fieldeditors.FieldNameLabel; @@ -53,7 +52,6 @@ abstract class FieldsEditorTab extends EntryEditorTab { private final DialogService dialogService; private final PreferencesService preferences; private final ThemeManager themeManager; - private final ExternalFileTypes externalFileTypes; private final TaskExecutor taskExecutor; private final JournalAbbreviationRepository journalAbbreviationRepository; private final StateManager stateManager; @@ -71,7 +69,6 @@ public FieldsEditorTab(boolean compressed, PreferencesService preferences, StateManager stateManager, ThemeManager themeManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository, IndexingTaskManager indexingTaskManager) { @@ -82,7 +79,6 @@ public FieldsEditorTab(boolean compressed, this.dialogService = Objects.requireNonNull(dialogService); this.preferences = Objects.requireNonNull(preferences); this.themeManager = themeManager; - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.taskExecutor = Objects.requireNonNull(taskExecutor); this.journalAbbreviationRepository = Objects.requireNonNull(journalAbbreviationRepository); this.stateManager = stateManager; @@ -246,7 +242,7 @@ private void initPanel() { scrollPane.setFitToHeight(true); SplitPane container = new SplitPane(scrollPane); - previewPanel = new PreviewPanel(databaseContext, dialogService, externalFileTypes, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); + previewPanel = new PreviewPanel(databaseContext, dialogService, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); EasyBind.subscribe(preferences.getPreviewPreferences().showPreviewAsExtraTabProperty(), show -> { if (show) { container.getItems().remove(previewPanel); diff --git a/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java b/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java index 07090e543ee..97c38da58dd 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java @@ -5,7 +5,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.journals.JournalAbbreviationRepository; @@ -25,7 +24,6 @@ public OptionalFields2Tab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super( @@ -40,7 +38,6 @@ public OptionalFields2Tab(BibDatabaseContext databaseContext, themeManager, indexingTaskManager, entryTypesManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository ); diff --git a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java index 0035e806258..93a6ff4b6f1 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java @@ -5,7 +5,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.journals.JournalAbbreviationRepository; @@ -25,7 +24,6 @@ public OptionalFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super( @@ -40,7 +38,6 @@ public OptionalFieldsTab(BibDatabaseContext databaseContext, themeManager, indexingTaskManager, entryTypesManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository ); diff --git a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java index 52c88a8c809..af1a5f9f0b6 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java +++ b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -40,7 +39,6 @@ public OptionalFieldsTabBase(String title, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super(true, @@ -51,7 +49,6 @@ public OptionalFieldsTabBase(String title, preferences, stateManager, themeManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); diff --git a/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java index 08802ceb27c..0fe1281c077 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java @@ -14,7 +14,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -44,7 +43,6 @@ public OtherFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super(false, @@ -55,7 +53,6 @@ public OtherFieldsTab(BibDatabaseContext databaseContext, preferences, stateManager, themeManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); diff --git a/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java b/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java index b66b3c49a18..4212506b8f6 100644 --- a/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java @@ -2,7 +2,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.preview.PreviewPanel; import org.jabref.gui.theme.ThemeManager; @@ -19,7 +18,6 @@ public class PreviewTab extends EntryEditorTab { private final StateManager stateManager; private final ThemeManager themeManager; private final IndexingTaskManager indexingTaskManager; - private final ExternalFileTypes externalFileTypes; private PreviewPanel previewPanel; public PreviewTab(BibDatabaseContext databaseContext, @@ -27,15 +25,13 @@ public PreviewTab(BibDatabaseContext databaseContext, PreferencesService preferences, StateManager stateManager, ThemeManager themeManager, - IndexingTaskManager indexingTaskManager, - ExternalFileTypes externalFileTypes) { + IndexingTaskManager indexingTaskManager) { this.databaseContext = databaseContext; this.dialogService = dialogService; this.preferences = preferences; this.stateManager = stateManager; this.themeManager = themeManager; this.indexingTaskManager = indexingTaskManager; - this.externalFileTypes = externalFileTypes; setGraphic(IconTheme.JabRefIcons.TOGGLE_ENTRY_PREVIEW.getGraphicNode()); setText(Localization.lang("Preview")); @@ -63,7 +59,7 @@ public boolean shouldShow(BibEntry entry) { @Override protected void bindToEntry(BibEntry entry) { if (previewPanel == null) { - previewPanel = new PreviewPanel(databaseContext, dialogService, externalFileTypes, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); + previewPanel = new PreviewPanel(databaseContext, dialogService, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); setContent(previewPanel); } diff --git a/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java index 5c078dea5b5..3451f2c8f53 100644 --- a/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -40,11 +39,10 @@ public RequiredFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super(false, databaseContext, suggestionProviders, undoManager, dialogService, - preferences, stateManager, themeManager, externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); + preferences, stateManager, themeManager, taskExecutor, journalAbbreviationRepository, indexingTaskManager); this.entryTypesManager = entryTypesManager; setText(Localization.lang("Required fields")); diff --git a/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java index bc084a8e77b..aa4e65b48e3 100644 --- a/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java @@ -8,7 +8,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -16,7 +15,6 @@ import org.jabref.logic.pdf.search.indexing.IndexingTaskManager; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; -import org.jabref.model.entry.BibEntryTypesManager; import org.jabref.model.entry.field.Field; import org.jabref.preferences.PreferencesService; @@ -33,11 +31,9 @@ public UserDefinedFieldsTab(String name, StateManager stateManager, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, - BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { - super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); + super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, taskExecutor, journalAbbreviationRepository, indexingTaskManager); this.fields = new LinkedHashSet<>(fields); diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 14ac6e56b4f..3cba8ebd4fa 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -17,21 +17,14 @@ import org.jabref.preferences.FilePreferences; // Do not make this class final, as it otherwise can't be mocked for tests -public enum ExternalFileTypes { - INSTANCE; +public class ExternalFileTypes { // This String is used in the encoded list in prefs of external file type // modifications, in order to indicate a removed default file type: private static final String FILE_TYPE_REMOVED_FLAG = "REMOVED"; - private static final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; - /** - * @deprecated use {@link FilePreferences#getExternalFileTypes()} instead. - */ - @Deprecated - public static ExternalFileTypes getInstance() { - return INSTANCE; + private ExternalFileTypes() { } public static List getDefaultExternalFileTypes() { diff --git a/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java b/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java index 010bf682115..1558f020f1a 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java +++ b/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java @@ -106,7 +106,7 @@ public static Optional getSuffix(final String link, FilePreferences file } else { suffix = strippedLink.substring(strippedLinkIndex + 1); } - if (!ExternalFileTypes.getInstance().isExternalFileTypeByExt(suffix, filePreferences)) { + if (!ExternalFileTypes.isExternalFileTypeByExt(suffix, filePreferences)) { // If the suffix doesn't seem to give any reasonable file type, try // with the non-stripped link: int index = link.lastIndexOf('.'); diff --git a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java index 9b44d980c9d..b78e69aeba9 100644 --- a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java +++ b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java @@ -93,7 +93,7 @@ public void deleteStyle() { public void editStyle() { OOBibStyle style = selectedItem.getValue().getStyle(); - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt("jstyle", preferencesService.getFilePreferences()); + Optional type = ExternalFileTypes.getExternalFileTypeByExt("jstyle", preferencesService.getFilePreferences()); try { JabRefDesktop.openExternalFileAnyFormat(new BibDatabaseContext(), preferencesService, style.getPath(), type); } catch (IOException e) { diff --git a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java index f9f75d33204..62c09375df6 100644 --- a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java @@ -125,8 +125,8 @@ public void createNewFile() { public void edit(ProtectedTermsListItemModel file) { Optional termsFileType = OptionalUtil.orElse( - ExternalFileTypes.getInstance().getExternalFileTypeByExt("terms", preferences.getFilePreferences()), - ExternalFileTypes.getInstance().getExternalFileTypeByExt("txt", preferences.getFilePreferences()) + ExternalFileTypes.getExternalFileTypeByExt("terms", preferences.getFilePreferences()), + ExternalFileTypes.getExternalFileTypeByExt("txt", preferences.getFilePreferences()) ); String fileName = file.getTermsList().getLocation(); diff --git a/src/main/java/org/jabref/gui/preview/PreviewPanel.java b/src/main/java/org/jabref/gui/preview/PreviewPanel.java index 1372db4ec18..94cf57cd1e3 100644 --- a/src/main/java/org/jabref/gui/preview/PreviewPanel.java +++ b/src/main/java/org/jabref/gui/preview/PreviewPanel.java @@ -19,7 +19,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.externalfiles.ExternalFilesEntryLinker; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.keyboard.KeyBindingRepository; @@ -50,7 +49,6 @@ public class PreviewPanel extends VBox { public PreviewPanel(BibDatabaseContext database, DialogService dialogService, - ExternalFileTypes externalFileTypes, KeyBindingRepository keyBindingRepository, PreferencesService preferences, StateManager stateManager, From e598d500c5c373990b424f1000e44e6d4eec4e28 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:14:11 +0200 Subject: [PATCH 11/13] Added tests --- .../ExternalFileTypesTest.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java diff --git a/src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java b/src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java new file mode 100644 index 00000000000..dadcba319fc --- /dev/null +++ b/src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java @@ -0,0 +1,121 @@ +package org.jabref.gui.externalfiletype; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Optional; +import java.util.Set; + +import javafx.collections.FXCollections; + +import org.jabref.gui.icon.IconTheme; +import org.jabref.model.entry.LinkedFile; +import org.jabref.preferences.FilePreferences; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class ExternalFileTypesTest { + private static final Set TEST_LIST = Set.of( + StandardExternalFileType.PDF, + StandardExternalFileType.URL, + StandardExternalFileType.JPG, + StandardExternalFileType.TXT); + + private static final String TEST_STRINGLIST = "PostScript:REMOVED;" + + "Word:REMOVED;" + + "Word 2007+:REMOVED;" + + "OpenDocument text:REMOVED;" + + "Excel:REMOVED;" + + "Excel 2007+:REMOVED;" + + "OpenDocument spreadsheet:REMOVED;" + + "PowerPoint:REMOVED;" + + "PowerPoint 2007+:REMOVED;" + + "OpenDocument presentation:REMOVED;" + + "Rich Text Format:REMOVED;" + + "PNG image:REMOVED;" + + "GIF image:REMOVED;" + + "Djvu:REMOVED;" + + "LaTeX:REMOVED;" + + "CHM:REMOVED;" + + "TIFF image:REMOVED;" + + "MHT:REMOVED;" + + "ePUB:REMOVED"; + + private final FilePreferences filePreferences = mock(FilePreferences.class); + + @BeforeEach + void setUp() { + when(filePreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(TEST_LIST)); + } + + @Test + void getExternalFileTypeByName() { + assertEquals(Optional.of(StandardExternalFileType.PDF), ExternalFileTypes.getExternalFileTypeByName("PDF", filePreferences)); + } + + @Test + void getExternalFileTypeByExt() { + assertEquals(Optional.of(StandardExternalFileType.URL), ExternalFileTypes.getExternalFileTypeByExt("html", filePreferences)); + } + + @Test + void isExternalFileTypeByExt() { + assertTrue(ExternalFileTypes.isExternalFileTypeByExt("html", filePreferences)); + assertFalse(ExternalFileTypes.isExternalFileTypeByExt("tst", filePreferences)); + } + + @Test + void getExternalFileTypeForName() { + assertEquals(Optional.of(StandardExternalFileType.JPG), ExternalFileTypes.getExternalFileTypeForName("testfile.jpg", filePreferences)); + } + + @Test + void getExternalFileTypeByMimeType() { + assertEquals(Optional.of(StandardExternalFileType.TXT), ExternalFileTypes.getExternalFileTypeByMimeType("text/plain", filePreferences)); + } + + @Test + void getExternalFileTypeByFile() { + Path testfile = Path.of("testfile.txt"); + assertEquals(Optional.of(StandardExternalFileType.TXT), ExternalFileTypes.getExternalFileTypeByFile(testfile, filePreferences)); + } + + @Test + void getExternalFileTypeByLinkedFile() { + LinkedFile testfile = new LinkedFile("A testfile", "https://testserver.com/testfile.pdf", "PDF"); + assertEquals(Optional.of(StandardExternalFileType.PDF), ExternalFileTypes.getExternalFileTypeByLinkedFile(testfile, false, filePreferences)); + } + + @Test + void toStringList() { + String testString = ExternalFileTypes.toStringList(TEST_LIST); + + assertEquals(TEST_STRINGLIST, testString); + } + + @Test + void fromString() { + Set testList = ExternalFileTypes.fromString(TEST_STRINGLIST); + + assertEquals(TEST_LIST, testList); + } + + @Test + void externalFileTypetoStringArray() { + ExternalFileType type = new CustomExternalFileType( + "testEntry", + "tst", + "text/plain", + "emacs", + "close", + IconTheme.JabRefIcons.CLOSE); + + assertEquals("[testEntry, tst, text/plain, emacs, CLOSE]", Arrays.toString(type.toStringArray())); + } +} From 463d1fb896cc156fee75138007280a432d27d33c Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:16:18 +0200 Subject: [PATCH 12/13] Cleanup --- .../org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index d3a8917636a..e11100032c4 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -75,10 +75,6 @@ void setUp(@TempDir Path tempFolder) throws Exception { dialogService = mock(DialogService.class); when(filePreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); -// when(ExternalFileTypes.getExternalFileTypeByMimeType("application/pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); -// when(ExternalFileTypes.getExternalFileTypeByMimeType(contains("text/html"), eq(filePreferences))).thenReturn(Optional.of(StandardExternalFileType.URL)); -// when(ExternalFileTypes.getExternalFileTypeByExt("pdf", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.PDF)); -// when(ExternalFileTypes.getExternalFileTypeByExt("html", filePreferences)).thenReturn(Optional.of(StandardExternalFileType.URL)); when(preferences.getFilePreferences()).thenReturn(filePreferences); when(preferences.getXmpPreferences()).thenReturn(mock(XmpPreferences.class)); tempFile = tempFolder.resolve("temporaryFile"); From de57738588ce020f6e8f5333bd081604e1b3e096 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:31:02 +0200 Subject: [PATCH 13/13] Fixed test --- .../org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java index 5ad939179d9..b429b879454 100644 --- a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java +++ b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java @@ -48,9 +48,8 @@ public void setUp(@TempDir Path folder) throws Exception { @Test public void testFindAssociatedNotLinkedFilesSuccess() throws Exception { - // Due to mocking the externalFileType class, the file extension will not be found when(databaseContext.getFileDirectories(any())).thenReturn(Collections.singletonList(path.getParent())); - List expected = Collections.singletonList(new LinkedFile("", Path.of("CiteKey.pdf"), "")); + List expected = Collections.singletonList(new LinkedFile("", Path.of("CiteKey.pdf"), "PDF")); AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, filePreferences, autoLinkPrefs); List actual = util.findAssociatedNotLinkedFiles(entry); assertEquals(expected, actual);