Skip to content

Commit

Permalink
Removed Globals from BibDatabaseContext
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Aug 25, 2016
1 parent 40ae061 commit a480dee
Show file tree
Hide file tree
Showing 40 changed files with 196 additions and 116 deletions.
18 changes: 4 additions & 14 deletions src/main/java/net/sf/jabref/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
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 @@ -136,11 +133,11 @@ public boolean isBiblatexMode() {
* @param fieldName The field type
* @return The default directory for this field type.
*/
public List<String> getFileDirectory(String fieldName) {
public List<String> getFileDirectory(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 +149,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.getFieldDirectory().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 +186,6 @@ private String getFileDirectoryPath(String directoryName) {
return dir;
}

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

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

import java.util.Optional;

public class FileDirectoryPreferences {
private final String user;
private final String fieldDirectory;
private final boolean bibLocationAsPrimary;
private final String fieldName;

public FileDirectoryPreferences(String fieldName, String user, String fieldDirectory, boolean bibLocationAsPrimary) {
this.fieldName = fieldName;
this.user = user;
this.fieldDirectory = fieldDirectory;
this.bibLocationAsPrimary = bibLocationAsPrimary;
}

public String getUser() {
return user;
}

public Optional<String> getFieldDirectory() {
return Optional.ofNullable(fieldDirectory);
}

public boolean isBibLocationAsPrimary() {
return bibLocationAsPrimary;
}

public String getFieldName() {
return fieldName;
}
}
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 @@ -54,7 +55,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
.expandFilename(pdf,
panel.getBibDatabaseContext().getFileDirectory(
Globals.prefs.getFileDirectoryPreferences(FieldName.PDF)))
.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,16 @@ 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)
.ifPresent(
pdf -> FileUtil
.expandFilename(pdf,
panel.getBibDatabaseContext().getFileDirectory(
Globals.prefs.getFileDirectoryPreferences(FieldName.PDF)))
.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
4 changes: 2 additions & 2 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 @@ -352,7 +352,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
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/gui/actions/CleanupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) {
BibDatabaseContext bibDatabaseContext = panel.getBibDatabaseContext();
CleanupWorker cleaner = new CleanupWorker(bibDatabaseContext,
Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN),
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader),
Globals.prefs.getFileDirectoryPreferences());
List<FieldChange> changes = cleaner.cleanup(preset, entry);

unsuccessfulRenames = cleaner.getUnsuccessfulRenames();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void openExternalViewer(BibDatabaseContext databaseContext, String
String fieldName = initialFieldName;
if (FieldName.PS.equals(fieldName) || FieldName.PDF.equals(fieldName)) {
// Find the default directory for this field type:
List<String> dir = databaseContext.getFileDirectory(fieldName);
List<String> dir = databaseContext.getFileDirectory(Globals.prefs.getFileDirectoryPreferences(fieldName));

Optional<File> file = FileUtil.expandFilename(link, dir);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/exporter/ExportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void actionPerformed(ActionEvent e) {
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext()
.getFileDirectory();
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());

// Make sure we remember which filter was used, to set
// the default for next time:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void run() {
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
Globals.prefs.fileDirForDatabase = frame.getCurrentBasePanel().getBibDatabaseContext()
.getFileDirectory();
.getFileDirectory(Globals.prefs.getFileDirectoryPreferences());

File tmp = null;
try {
Expand Down
Loading

0 comments on commit a480dee

Please sign in to comment.