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

Removed Globals from BibDatabaseContext #1856

Merged
merged 2 commits into from
Aug 26, 2016
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
### Fixed

### Removed

- The non-supported feature of being able to define file directories for any extension is removed. Still, it should work for older databases using the legacy `ps` and `pdf` fields, although we strongly encourage using the `file` field.



Expand Down
52 changes: 24 additions & 28 deletions src/main/java/net/sf/jabref/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import java.util.Objects;
import java.util.Optional;

import net.sf.jabref.logic.layout.format.FileLinkPreferences;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.database.BibDatabaseModeDetection;
import net.sf.jabref.model.database.DatabaseLocation;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.JabRefPreferences;
import net.sf.jabref.shared.DBMSSynchronizer;

/**
Expand Down Expand Up @@ -120,27 +118,32 @@ public boolean isBiblatexMode() {
return getMode() == BibDatabaseMode.BIBLATEX;
}

public List<String> getFileDirectory(FileDirectoryPreferences preferences) {
return getFileDirectory(FieldName.FILE, preferences);
}

/**
* Look up the directory set up for the given field type for this database.
* If no directory is set up, return that defined in global preferences.
* There can be up to three directory definitions for these files:
* the database's metadata can specify a general directory and/or a user-specific directory
* or the preferences can specify one.
* <p>
* The settings are prioritized in the following order and the first defined setting is used:
* 1. metadata user-specific directory
* 2. metadata general directory
* 3. preferences directory
* 4. BIB file directory
*
* @param fieldName The field type
* @return The default directory for this field type.
*/
public List<String> getFileDirectory(String fieldName) {
* Look up the directory set up for the given field type for this database.
* If no directory is set up, return that defined in global preferences.
* There can be up to three directory definitions for these files:
* the database's metadata can specify a general directory and/or a user-specific directory
* or the preferences can specify one.
* <p>
* The settings are prioritized in the following order and the first defined setting is used:
* 1. metadata user-specific directory
* 2. metadata general directory
* 3. preferences directory
* 4. BIB file directory
*
* @param
* @param fieldName The field type
* @return The default directory for this field type.
*/
public List<String> getFileDirectory(String fieldName, FileDirectoryPreferences preferences) {
List<String> fileDirs = new ArrayList<>();

// 1. metadata user-specific directory
Optional<String> userFileDirectory = metaData.getUserFileDirectory(Globals.prefs.getUser());
Optional<String> userFileDirectory = metaData.getUserFileDirectory(preferences.getUser());
if(userFileDirectory.isPresent()) {
fileDirs.add(getFileDirectoryPath(userFileDirectory.get()));
}
Expand All @@ -152,16 +155,13 @@ public List<String> getFileDirectory(String fieldName) {
}

// 3. preferences directory
String dir = Globals.prefs.get(fieldName + FileLinkPreferences.DIR_SUFFIX); // FILE_DIR
if (dir != null) {
fileDirs.add(dir);
}
preferences.getFileDirectory(fieldName).ifPresent(fileDirs::add);

// 4. BIB file directory
if (getDatabaseFile() != null) {
String parentDir = getDatabaseFile().getParent();
// Check if we should add it as primary file dir (first in the list) or not:
if (Globals.prefs.getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR)) {
if (preferences.isBibLocationAsPrimary()) {
fileDirs.add(0, parentDir);
} else {
fileDirs.add(parentDir);
Expand Down Expand Up @@ -192,10 +192,6 @@ private String getFileDirectoryPath(String directoryName) {
return dir;
}

public List<String> getFileDirectory() {
return getFileDirectory(FieldName.FILE);
}

public DBMSSynchronizer getDBSynchronizer() {
return this.dbmsSynchronizer;
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/net/sf/jabref/FileDirectoryPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.sf.jabref;

import java.util.Map;
import java.util.Optional;

import net.sf.jabref.model.entry.FieldName;

public class FileDirectoryPreferences {
private final String user;
private final Map<String, String> fieldFileDirectories;
private final boolean bibLocationAsPrimary;


public FileDirectoryPreferences(String user, Map<String, String> fieldFileDirectories,
boolean bibLocationAsPrimary) {
this.user = user;
this.fieldFileDirectories = fieldFileDirectories;
this.bibLocationAsPrimary = bibLocationAsPrimary;
}

public String getUser() {
return user;
}

public Optional<String> getFileDirectory(String field) {
return Optional.ofNullable(fieldFileDirectories.get(field));
}

public Optional<String> getFileDirectory() {
return getFileDirectory(FieldName.FILE);
}

public boolean isBibLocationAsPrimary() {
return bibLocationAsPrimary;
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
}
BibDatabaseContext databaseContext = pr.getDatabaseContext();
databaseContext.setDatabaseFile(theFile);
Globals.prefs.fileDirForDatabase = databaseContext.getFileDirectory();
Globals.prefs.fileDirForDatabase = databaseContext
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
IExportFormat format = ExportFormats.getExportFormat(data[1]);
if (format == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/external/AutoSetLinks.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCom
public void run() {
// determine directories to search in
List<File> dirs = new ArrayList<>();
List<String> dirsS = databaseContext.getFileDirectory();
List<String> dirsS = databaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
dirs.addAll(dirsS.stream().map(File::new).collect(Collectors.toList()));

// determine extensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void download(URL url, final DownloadCallback callback) throws IOExceptio
}

String suggestedName = getSuggestedFileName(suffix);
List<String> fDirectory = databaseContext.getFileDirectory();
List<String> fDirectory = databaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
String directory;
if (fDirectory.isEmpty()) {
directory = null;
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/net/sf/jabref/external/DroppedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ public void handleDroppedfile(String fileName, ExternalFileType fileType, BibEnt
String destFilename;

if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(new File(fileName), panel.getBibDatabaseContext().getFileDirectory()).toString();
destFilename = FileUtil
.shortenFileName(new File(fileName),
panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences()))
.toString();
} else {
destFilename = renameCheckBox.isSelected() ? renameToTextBox.getText() : new File(fileName).getName();
if (copyRadioButton.isSelected()) {
Expand Down Expand Up @@ -190,7 +193,10 @@ public void linkPdfToEntry(String fileName, BibEntry entry) {
NamedCompound edits = new NamedCompound(Localization.lang("Drop %0", fileType.getExtension()));

if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(new File(fileName), panel.getBibDatabaseContext().getFileDirectory()).toString();
destFilename = FileUtil
.shortenFileName(new File(fileName),
panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences()))
.toString();
} else {
destFilename = renameCheckBox.isSelected() ? renameToTextBox.getText() : new File(fileName).getName();
if (copyRadioButton.isSelected()) {
Expand Down Expand Up @@ -264,7 +270,10 @@ private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCo
String destFilename;

if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(new File(fileName), panel.getBibDatabaseContext().getFileDirectory()).toString();
destFilename = FileUtil
.shortenFileName(new File(fileName),
panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences()))
.toString();
} else {
if (renameCheckBox.isSelected()) {
destFilename = fileName;
Expand Down Expand Up @@ -301,7 +310,7 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy
BibDatabase database) {

String dialogTitle = Localization.lang("Link to file %0", linkFileName);
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
int found = -1;
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
Expand Down Expand Up @@ -388,7 +397,8 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
// If avoidDuplicate==true, we should check if this file is already linked:
if (avoidDuplicate) {
// For comparison, find the absolute filename:
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
String absFilename;
if (new File(filename).isAbsolute() || dirs.isEmpty()) {
absFilename = filename;
Expand Down Expand Up @@ -448,7 +458,7 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
*/
private boolean doMove(String fileName, String destFilename,
NamedCompound edits) {
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
int found = -1;
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
Expand Down Expand Up @@ -498,7 +508,7 @@ private boolean doMove(String fileName, String destFilename,
*/
private boolean doCopy(String fileName, String toFile, NamedCompound edits) {

List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
int found = -1;
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/sf/jabref/external/FindFullTextAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.swing.JOptionPane;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.FileListTableModel;
import net.sf.jabref.gui.undo.UndoableFieldChange;
Expand Down Expand Up @@ -53,7 +54,8 @@ public void run() {
@Override
public void update() {
if (result.isPresent()) {
List<String> dirs = basePanel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = basePanel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (dirs.isEmpty()) {
JOptionPane.showMessageDialog(basePanel.frame(),
Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences")
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/external/MoveFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void actionPerformed(ActionEvent event) {
}

// Get an absolute path representation:
List<String> dirs = frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectory();
List<String> dirs = frame.getCurrentBasePanel().getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
int found = -1;
for (int i = 0; i < dirs.size(); i++) {
if (new File(dirs.get(i)).exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public void run() {
tableModel.setContentDontGuessTypes(old.get());

// We need to specify which directories to search in for Util.expandFilename:
List<String> dirsS = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirsS = panel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
List<File> dirs = new ArrayList<>();
for (String dirs1 : dirsS) {
dirs.add(new File(dirs1));
Expand Down Expand Up @@ -365,7 +366,7 @@ public void setVisible(boolean visible) {
canceled = true;
}

List<String> dirs = databaseContext.getFileDirectory();
List<String> dirs = databaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (dirs.isEmpty()) {
autoSetNone.setSelected(true);
autoSetNone.setEnabled(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
import net.sf.jabref.gui.FileListTableModel;
import net.sf.jabref.logic.util.io.FileUtil;
Expand All @@ -32,7 +33,8 @@ public TransferableFileLinkSelection(BasePanel panel, List<BibEntry> selection)
selection.get(0).getFieldOptional(FieldName.FILE).ifPresent(tm::setContent);
if (tm.getRowCount() > 0) {
// Find the default directory for this field type, if any:
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
FileUtil.expandFilename(tm.getEntry(0).link, dirs).ifPresent(fileList::add);
}

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/net/sf/jabref/external/WriteXMPAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,16 @@ public void run() {
List<File> files = new ArrayList<>();

// First check the (legacy) "pdf" field:
entry.getFieldOptional(FieldName.PDF).ifPresent(pdf ->
FileUtil.expandFilename(pdf, panel.getBibDatabaseContext().getFileDirectory("pdf"))
.ifPresent(files::add));
entry.getFieldOptional(FieldName.PDF)
.ifPresent(
pdf -> FileUtil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting here and below looks a bit weird...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of them was sorted out with the new code, but not the other...

.expandFilename(pdf,
panel.getBibDatabaseContext().getFileDirectory(FieldName.PDF,
Globals.prefs.getFileDirectoryPreferences()))
.ifPresent(files::add));
// Then check the "file" field:
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (entry.hasField(FieldName.FILE)) {
FileListTableModel tm = new FileListTableModel();
entry.getFieldOptional(FieldName.FILE).ifPresent(tm::setContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public void actionPerformed(ActionEvent actionEvent) {
List<File> files = new ArrayList<>();

// First check the (legacy) "pdf" field:
entry.getFieldOptional(FieldName.PDF).ifPresent(pdf -> FileUtil
.expandFilename(pdf, panel.getBibDatabaseContext().getFileDirectory("pdf")).ifPresent(files::add));
entry.getFieldOptional(FieldName.PDF)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does indeed, but that is what Eclipse gives me when reformatting the whole expression...

.ifPresent(pdf -> FileUtil.expandFilename(pdf, panel.getBibDatabaseContext()
.getFileDirectory(FieldName.PDF, Globals.prefs.getFileDirectoryPreferences()))
.ifPresent(files::add));

// Then check the "file" field:
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory();
List<String> dirs = panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (entry.hasField(FieldName.FILE)) {
FileListTableModel tm = new FileListTableModel();
entry.getFieldOptional(FieldName.FILE).ifPresent(tm::setContent);
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public void update() {

actions.put(Actions.OPEN_FOLDER, (BaseAction) () -> JabRefExecutorService.INSTANCE.execute(() -> {
final List<File> files = FileUtil.getListOfLinkedFiles(mainTable.getSelectedEntries(),
bibDatabaseContext.getFileDirectory());
bibDatabaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences()));
for (final File f : files) {
try {
JabRefDesktop.openFolderAndSelectFile(f.getAbsolutePath());
Expand Down Expand Up @@ -2358,12 +2358,10 @@ public Optional<String> searchAndOpen() {

final Set<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
final List<File> dirs = new ArrayList<>();
if (!basePanel.getBibDatabaseContext().getFileDirectory().isEmpty()) {
final List<String> mdDirs = basePanel.getBibDatabaseContext().getFileDirectory();
for (final String mdDir : mdDirs) {
dirs.add(new File(mdDir));

}
final List<String> mdDirs = basePanel.getBibDatabaseContext()
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
for (final String mdDir : mdDirs) {
dirs.add(new File(mdDir));
}
final List<String> extensions = new ArrayList<>();
for (final ExternalFileType type : types) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/gui/FileListEntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private void storeSettings(FileListEntry entry) {
String link = "";
// See if we should trim the file link to be relative to the file directory:
try {
List<String> dirs = databaseContext.getFileDirectory();
List<String> dirs = databaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
if (dirs.isEmpty()) {
link = this.link.getText().trim();
} else {
Expand Down Expand Up @@ -335,7 +335,8 @@ public boolean okPressed() {

private final ActionListener browsePressed = e -> {
String filePath = link.getText().trim();
Optional<File> file = FileUtil.expandFilename(this.databaseContext, filePath);
Optional<File> file = FileUtil.expandFilename(this.databaseContext, filePath,
Globals.prefs.getFileDirectoryPreferences());
String workingDir;
// no file set yet or found
if (file.isPresent()) {
Expand All @@ -352,7 +353,7 @@ public boolean okPressed() {
Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, newFile.getPath());

// If the file is below the file directory, make the path relative:
List<String> fileDirs = this.databaseContext.getFileDirectory();
List<String> fileDirs = this.databaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());
newFile = FileUtil.shortenFileName(newFile, fileDirs);

link.setText(newFile.getPath());
Expand Down
Loading