Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove obsolete usage of File in DatabaseContext #6135

Merged
merged 3 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"));

Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -306,8 +305,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) {
Expand Down Expand Up @@ -355,7 +354,7 @@ private void tearDownJabRef(List<String> 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);
}
}
Expand Down Expand Up @@ -394,7 +393,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);
Expand Down Expand Up @@ -953,15 +952,11 @@ private List<String> collectDatabaseFilePaths() {
List<String> 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;
Expand All @@ -977,10 +972,10 @@ public void updateAllTabTitles() {
List<String> paths = getUniquePathParts();
for (int i = 0; i < getBasePanelCount(); i++) {
String uniqPath = paths.get(i);
Optional<File> file = getBasePanelAt(i).getBibDatabaseContext().getDatabaseFile();
Optional<Path> 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);
Expand All @@ -991,7 +986,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)));
}
}

Expand Down Expand Up @@ -1047,7 +1042,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();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jabref.logic.layout;

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;
Expand Down Expand Up @@ -366,10 +366,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;
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/org/jabref/model/database/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> getDatabaseFile() {
return file.map(Path::toFile);
}

public void setDatabasePath(Path file) {
this.file = Optional.ofNullable(file);
}

public Optional<Path> getDatabasePath() {
return file;
}
Expand Down Expand Up @@ -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<File> databaseFile = getDatabaseFile();
Optional<Path> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.preferences;

import java.io.File;
import java.nio.file.Path;
import java.util.Objects;

public class LastFocusedTabPreferences {
Expand All @@ -11,21 +11,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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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;
Expand All @@ -27,15 +27,15 @@ 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));
}

@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));

Expand Down