From 7614bd78a0afd5f5f13726201c458dba9e852313 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sun, 15 Mar 2020 23:13:03 +0100 Subject: [PATCH 1/3] Remove obsolete usage of File in DatabaseContext --- src/main/java/org/jabref/gui/BasePanel.java | 12 +++------ src/main/java/org/jabref/gui/JabRefFrame.java | 26 ++++++++----------- .../org/jabref/logic/layout/LayoutEntry.java | 5 ++-- .../model/database/BibDatabaseContext.java | 24 +++++++---------- .../LastFocusedTabPreferences.java | 13 +++++----- .../LastFocusedTabPreferencesTest.java | 5 ++-- 6 files changed, 36 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/jabref/gui/BasePanel.java b/src/main/java/org/jabref/gui/BasePanel.java index 548fe11456e..330463448ed 100644 --- a/src/main/java/org/jabref/gui/BasePanel.java +++ b/src/main/java/org/jabref/gui/BasePanel.java @@ -70,7 +70,6 @@ public class BasePanel extends StackPane { private final FileAnnotationCache annotationCache; private final JabRefFrame frame; - // The undo manager. private final CountingUndoManager undoManager; private final SidePaneManager sidePaneManager; @@ -80,13 +79,10 @@ public class BasePanel extends StackPane { private final DialogService dialogService; private MainTable mainTable; private BasePanelPreferences preferences; - // To contain instantiated entry editors. This is to save time - // As most enums, this must not be null private BasePanelMode mode = BasePanelMode.SHOWING_NOTHING; private SplitPane splitPane; private DatabaseChangePane changePane; private boolean saving; - // AutoCompleter used in the search bar private PersonNameSuggestionProvider searchAutoCompleter; private boolean baseChanged; private boolean nonUndoableChange; @@ -152,10 +148,10 @@ public String getTabTitle() { boolean isAutosaveEnabled = Globals.prefs.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE); if (databaseLocation == DatabaseLocation.LOCAL) { - if (this.bibDatabaseContext.getDatabaseFile().isPresent()) { + if (this.bibDatabaseContext.getDatabasePath().isPresent()) { // check if file is modified String changeFlag = isModified() && !isAutosaveEnabled ? "*" : ""; - title.append(this.bibDatabaseContext.getDatabaseFile().get().getName()).append(changeFlag); + title.append(this.bibDatabaseContext.getDatabasePath().get().getFileName()).append(changeFlag); } else { title.append(Localization.lang("untitled")); @@ -558,8 +554,8 @@ public synchronized void markChangedOrUnChanged() { } } else if (baseChanged && !nonUndoableChange) { baseChanged = false; - if (getBibDatabaseContext().getDatabaseFile().isPresent()) { - frame.setTabTitle(this, getTabTitle(), getBibDatabaseContext().getDatabaseFile().get().getAbsolutePath()); + if (getBibDatabaseContext().getDatabasePath().isPresent()) { + frame.setTabTitle(this, getTabTitle(), getBibDatabaseContext().getDatabasePath().get().toAbsolutePath().toString()); } else { frame.setTabTitle(this, Localization.lang("untitled"), null); } diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index f904f291fb5..51f7e16350b 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -355,7 +355,7 @@ private void tearDownJabRef(List filenames) { prefs.remove(JabRefPreferences.LAST_EDITED); } else { prefs.putStringList(JabRefPreferences.LAST_EDITED, filenames); - File focusedDatabase = getCurrentBasePanel().getBibDatabaseContext().getDatabaseFile().orElse(null); + Path focusedDatabase = getCurrentBasePanel().getBibDatabaseContext().getDatabasePath().orElse(null); new LastFocusedTabPreferences(prefs).setLastFocusedTab(focusedDatabase); } } @@ -394,7 +394,7 @@ public boolean quit() { } AutosaveManager.shutdown(context); BackupManager.shutdown(context); - context.getDatabaseFile().map(File::getAbsolutePath).ifPresent(filenames::add); + context.getDatabasePath().map(Path::toAbsolutePath).map(Path::toString).ifPresent(filenames::add); } WaitForSaveFinishedDialog waitForSaveFinishedDialog = new WaitForSaveFinishedDialog(dialogService); @@ -953,15 +953,11 @@ private List collectDatabaseFilePaths() { List dbPaths = new ArrayList<>(getBasePanelCount()); for (BasePanel basePanel : getBasePanelList()) { - try { - // db file exists - if (basePanel.getBibDatabaseContext().getDatabaseFile().isPresent()) { - dbPaths.add(basePanel.getBibDatabaseContext().getDatabaseFile().get().getCanonicalPath()); - } else { - dbPaths.add(""); - } - } catch (IOException ex) { - LOGGER.error("Invalid database file path: " + ex.getMessage()); + // db file exists + if (basePanel.getBibDatabaseContext().getDatabasePath().isPresent()) { + dbPaths.add(basePanel.getBibDatabaseContext().getDatabasePath().get().toAbsolutePath().toString()); + } else { + dbPaths.add(""); } } return dbPaths; @@ -977,10 +973,10 @@ public void updateAllTabTitles() { List paths = getUniquePathParts(); for (int i = 0; i < getBasePanelCount(); i++) { String uniqPath = paths.get(i); - Optional file = getBasePanelAt(i).getBibDatabaseContext().getDatabaseFile(); + Optional file = getBasePanelAt(i).getBibDatabaseContext().getDatabasePath(); if (file.isPresent()) { - if (!uniqPath.equals(file.get().getName()) && uniqPath.contains(File.separator)) { + if (!uniqPath.equals(file.get().getFileName()) && uniqPath.contains(File.separator)) { // remove filename uniqPath = uniqPath.substring(0, uniqPath.lastIndexOf(File.separator)); tabbedPane.getTabs().get(i).setText(getBasePanelAt(i).getTabTitle() + " \u2014 " + uniqPath); @@ -991,7 +987,7 @@ public void updateAllTabTitles() { } else { tabbedPane.getTabs().get(i).setText(getBasePanelAt(i).getTabTitle()); } - tabbedPane.getTabs().get(i).setTooltip(new Tooltip(file.map(File::getAbsolutePath).orElse(null))); + tabbedPane.getTabs().get(i).setTooltip(new Tooltip(file.map(Path::toAbsolutePath).map(Path::toString).orElse(null))); } } @@ -1047,7 +1043,7 @@ private boolean readyForAutosave(BibDatabaseContext context) { return ((context.getLocation() == DatabaseLocation.SHARED) || ((context.getLocation() == DatabaseLocation.LOCAL) && Globals.prefs.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE))) && - context.getDatabaseFile().isPresent(); + context.getDatabasePath().isPresent(); } /** diff --git a/src/main/java/org/jabref/logic/layout/LayoutEntry.java b/src/main/java/org/jabref/logic/layout/LayoutEntry.java index a58245eb067..84c164dbd97 100644 --- a/src/main/java/org/jabref/logic/layout/LayoutEntry.java +++ b/src/main/java/org/jabref/logic/layout/LayoutEntry.java @@ -2,6 +2,7 @@ import java.io.File; import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -366,10 +367,8 @@ public String doLayout(BibDatabaseContext databaseContext, Charset encoding) { return encoding.displayName(); case LayoutHelper.IS_FILENAME: - return databaseContext.getDatabaseFile().map(File::getName).orElse(""); - case LayoutHelper.IS_FILEPATH: - return databaseContext.getDatabaseFile().map(File::getPath).orElse(""); + return databaseContext.getDatabasePath().map(Path::toAbsolutePath).map(Path::toString).orElse(""); default: break; diff --git a/src/main/java/org/jabref/model/database/BibDatabaseContext.java b/src/main/java/org/jabref/model/database/BibDatabaseContext.java index d2e10dec3c2..8866cdbbd15 100644 --- a/src/main/java/org/jabref/model/database/BibDatabaseContext.java +++ b/src/main/java/org/jabref/model/database/BibDatabaseContext.java @@ -74,21 +74,15 @@ public void setMode(BibDatabaseMode bibDatabaseMode) { metaData.setMode(bibDatabaseMode); } + public void setDatabasePath(Path file) { + this.file = Optional.ofNullable(file); + } + /** * Get the file where this database was last saved to or loaded from, if any. * * @return Optional of the relevant File, or Optional.empty() if none is defined. - * @deprecated use {@link #getDatabasePath()} instead */ - @Deprecated - public Optional getDatabaseFile() { - return file.map(Path::toFile); - } - - public void setDatabasePath(Path file) { - this.file = Optional.ofNullable(file); - } - public Optional getDatabasePath() { return file; } @@ -195,19 +189,19 @@ private String getFileDirectoryPath(String directoryName) { String dir = directoryName; // If this directory is relative, we try to interpret it as relative to // the file path of this BIB file: - Optional databaseFile = getDatabaseFile(); + Optional databaseFile = getDatabasePath(); if (!new File(dir).isAbsolute() && databaseFile.isPresent()) { - String relDir; + Path relDir; if (".".equals(dir)) { // if dir is only "current" directory, just use its parent (== real current directory) as path relDir = databaseFile.get().getParent(); } else { - relDir = databaseFile.get().getParent() + File.separator + dir; + relDir = databaseFile.get().getParent().resolve(dir); } // If this directory actually exists, it is very likely that the // user wants us to use it: - if (new File(relDir).exists()) { - dir = relDir; + if (Files.exists(relDir)) { + dir = relDir.toString(); } } return dir; diff --git a/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java b/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java index 793d27f0277..d7f34b4ba11 100644 --- a/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java +++ b/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java @@ -1,6 +1,7 @@ package org.jabref.preferences; import java.io.File; +import java.nio.file.Path; import java.util.Objects; public class LastFocusedTabPreferences { @@ -11,21 +12,21 @@ public LastFocusedTabPreferences(JabRefPreferences preferences) { this.preferences = Objects.requireNonNull(preferences); } - public void setLastFocusedTab(File file) { - if (file == null) { + public void setLastFocusedTab(Path path) { + if (path == null) { return; } - String filePath = file.getAbsolutePath(); + String filePath = path.toAbsolutePath().toString(); preferences.put(JabRefPreferences.LAST_FOCUSED, filePath); } - public boolean hadLastFocus(File file) { - if (file == null) { + public boolean hadLastFocus(Path path) { + if (path == null) { return false; } String lastFocusedDatabase = preferences.get(JabRefPreferences.LAST_FOCUSED); - return file.getAbsolutePath().equals(lastFocusedDatabase); + return path.toAbsolutePath().toString().equals(lastFocusedDatabase); } } diff --git a/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java b/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java index 3e39b69fa63..d2529b6f8ed 100644 --- a/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java +++ b/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java @@ -1,6 +1,7 @@ package org.jabref.preferences; import java.io.File; +import java.nio.file.Path; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -27,7 +28,7 @@ public static void restorePreferenceLastFocus() { @Test public void testLastFocusedTab() { LastFocusedTabPreferences prefs = new LastFocusedTabPreferences(JabRefPreferences.getInstance()); - File whatever = new File("whatever"); + Path whatever = Path.of("whatever"); prefs.setLastFocusedTab(whatever); assertTrue(prefs.hadLastFocus(whatever)); } @@ -35,7 +36,7 @@ public void testLastFocusedTab() { @Test public void testLastFocusedTabNull() { LastFocusedTabPreferences prefs = new LastFocusedTabPreferences(JabRefPreferences.getInstance()); - File whatever = new File("whatever"); + Path whatever = Path.of("whatever"); prefs.setLastFocusedTab(whatever); assertTrue(prefs.hadLastFocus(whatever)); From d7b677e6631dac44496605cee5aca3f1ef017756 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Tue, 17 Mar 2020 05:49:34 +0100 Subject: [PATCH 2/3] Add forgotten conversion --- src/main/java/org/jabref/gui/JabRefFrame.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 51f7e16350b..7e25c17425a 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -306,8 +306,8 @@ public void setWindowTitle() { if (panel.getBibDatabaseContext().getLocation() == DatabaseLocation.LOCAL) { String changeFlag = panel.isModified() && !isAutosaveEnabled ? "*" : ""; String databaseFile = panel.getBibDatabaseContext() - .getDatabaseFile() - .map(File::getPath) + .getDatabasePath() + .map(Path::toString) .orElse(Localization.lang("untitled")); //setTitle(FRAME_TITLE + " - " + databaseFile + changeFlag + modeInfo); } else if (panel.getBibDatabaseContext().getLocation() == DatabaseLocation.SHARED) { From 393ab038cd676cd555f3cdf853c03a2e6b9d3bd8 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Tue, 17 Mar 2020 05:56:40 +0100 Subject: [PATCH 3/3] Fix checkstyle --- src/main/java/org/jabref/gui/JabRefFrame.java | 1 - src/main/java/org/jabref/logic/layout/LayoutEntry.java | 1 - .../java/org/jabref/preferences/LastFocusedTabPreferences.java | 1 - .../org/jabref/preferences/LastFocusedTabPreferencesTest.java | 1 - 4 files changed, 4 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 7e25c17425a..ca09b4c1af8 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -1,7 +1,6 @@ package org.jabref.gui; import java.io.File; -import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; diff --git a/src/main/java/org/jabref/logic/layout/LayoutEntry.java b/src/main/java/org/jabref/logic/layout/LayoutEntry.java index 84c164dbd97..ec7d6067a7e 100644 --- a/src/main/java/org/jabref/logic/layout/LayoutEntry.java +++ b/src/main/java/org/jabref/logic/layout/LayoutEntry.java @@ -1,6 +1,5 @@ package org.jabref.logic.layout; -import java.io.File; import java.nio.charset.Charset; import java.nio.file.Path; import java.util.ArrayList; diff --git a/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java b/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java index d7f34b4ba11..974761e4fdc 100644 --- a/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java +++ b/src/main/java/org/jabref/preferences/LastFocusedTabPreferences.java @@ -1,6 +1,5 @@ package org.jabref.preferences; -import java.io.File; import java.nio.file.Path; import java.util.Objects; diff --git a/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java b/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java index d2529b6f8ed..b6cffbdf9f3 100644 --- a/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java +++ b/src/test/java/org/jabref/preferences/LastFocusedTabPreferencesTest.java @@ -1,6 +1,5 @@ package org.jabref.preferences; -import java.io.File; import java.nio.file.Path; import org.junit.jupiter.api.AfterAll;