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

Refactor parsed file field #2723

Merged
merged 8 commits into from
Apr 18, 2017
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
9 changes: 5 additions & 4 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
import org.jabref.model.entry.event.EntryEventSource;
import org.jabref.model.entry.specialfields.SpecialField;
import org.jabref.model.entry.specialfields.SpecialFieldValue;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreviewPreferences;
import org.jabref.shared.DBMSSynchronizer;
Expand Down Expand Up @@ -522,11 +523,11 @@ public void update() {
actions.put(Actions.OPEN_EXTERNAL_FILE, (BaseAction) () -> openExternalFile());

actions.put(Actions.OPEN_FOLDER, (BaseAction) () -> JabRefExecutorService.INSTANCE.execute(() -> {
final List<File> files = FileUtil.getListOfLinkedFiles(mainTable.getSelectedEntries(),
final List<Path> files = FileUtil.getListOfLinkedFiles(mainTable.getSelectedEntries(),
bibDatabaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences()));
for (final File f : files) {
for (final Path f : files) {
try {
JabRefDesktop.openFolderAndSelectFile(f.getAbsolutePath());
JabRefDesktop.openFolderAndSelectFile(f.toAbsolutePath());
} catch (IOException e) {
LOGGER.info("Could not open folder", e);
}
Expand Down Expand Up @@ -2119,7 +2120,7 @@ public Optional<String> searchAndOpen() {
final List<File> res = result.get(entry);
if (!res.isEmpty()) {
final String filepath = res.get(0).getPath();
final Optional<String> extension = FileUtil.getFileExtension(filepath);
final Optional<String> extension = FileHelper.getFileExtension(filepath);
if (extension.isPresent()) {
Optional<ExternalFileType> type = ExternalFileTypes.getInstance()
.getExternalFileTypeByExt(extension.get());
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/jabref/gui/desktop/JabRefDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -31,12 +34,12 @@
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.OS;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.identifier.Eprint;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -66,16 +69,16 @@ public static void openExternalViewer(BibDatabaseContext databaseContext, String
// Find the default directory for this field type:
List<String> dir = databaseContext.getFileDirectories(fieldName, Globals.prefs.getFileDirectoryPreferences());

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

// Check that the file exists:
if (!file.isPresent() || !file.get().exists()) {
if (!file.isPresent() || !Files.exists(file.get())) {
throw new IOException("File not found (" + fieldName + "): '" + link + "'.");
}
link = file.get().getCanonicalPath();
link = file.get().toAbsolutePath().toString();

// Use the correct viewer even if pdf and ps are mixed up:
String[] split = file.get().getName().split("\\.");
String[] split = file.get().getFileName().toString().split("\\.");
if (split.length >= 2) {
if ("pdf".equalsIgnoreCase(split[split.length - 1])) {
fieldName = FieldName.PDF;
Expand Down Expand Up @@ -138,20 +141,19 @@ public static boolean openExternalFileAnyFormat(final BibDatabaseContext databas
}

// For other platforms we'll try to find the file type:
File file = new File(link);

Path file = Paths.get(link);
if (!httpLink) {
Optional<File> tmp = FileUtil.expandFilename(databaseContext, link,
Optional<Path> tmp = FileHelper.expandFilename(databaseContext, link,
Globals.prefs.getFileDirectoryPreferences());
if (tmp.isPresent()) {
file = tmp.get();
}
}

// Check if we have arrived at a file type, and either an http link or an existing file:
if ((httpLink || file.exists()) && (type.isPresent())) {
if (httpLink || Files.exists(file) && (type.isPresent())) {
// Open the file:
String filePath = httpLink ? link : file.getPath();
String filePath = httpLink ? link : file.toString();
openExternalFilePlatformIndependent(type, filePath);
return true;
} else {
Expand Down Expand Up @@ -253,7 +255,7 @@ public static boolean openExternalFileUnknown(JabRefFrame frame, BibEntry entry,
* @param fileLink the location of the file
* @throws IOException
*/
public static void openFolderAndSelectFile(String fileLink) throws IOException {
public static void openFolderAndSelectFile(Path fileLink) throws IOException {
NATIVE_DESKTOP.openFolderAndSelectFile(fileLink);
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.commons.logging.Log;
Expand All @@ -24,8 +23,8 @@ public void openFileWithApplication(String filePath, String application) throws
}

@Override
public void openFolderAndSelectFile(String filePath) throws IOException {
File file = Paths.get(filePath).toAbsolutePath().getParent().toFile();
public void openFolderAndSelectFile(Path filePath) throws IOException {
File file = filePath.toAbsolutePath().getParent().toFile();
Desktop.getDesktop().open(file);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void openFileWithApplication(String filePath, String application) throws
}

@Override
public void openFolderAndSelectFile(String filePath) throws IOException {
public void openFolderAndSelectFile(Path filePath) throws IOException {
String desktopSession = System.getenv("DESKTOP_SESSION").toLowerCase(Locale.ROOT);

String cmd;
Expand All @@ -60,7 +60,7 @@ public void openFolderAndSelectFile(String filePath) throws IOException {
} else if (desktopSession.contains("kde")) {
cmd = "dolphin --select " + filePath;
} else {
cmd = "xdg-open " + Paths.get(filePath).toAbsolutePath().getParent().toString();
cmd = "xdg-open " + filePath.toAbsolutePath().getParent().toString();
}

Runtime.getRuntime().exec(cmd);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface NativeDesktop {
*/
void openFileWithApplication(String filePath, String application) throws IOException;

void openFolderAndSelectFile(String filePath) throws IOException;
void openFolderAndSelectFile(Path file) throws IOException;

void openConsole(String absolutePath) throws IOException;

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jabref/gui/desktop/os/OSX.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public void openFileWithApplication(String filePath, String application) throws
}

@Override
public void openFolderAndSelectFile(String filePath) throws IOException {
File file = new File(filePath);
Desktop.getDesktop().open(file.getParentFile());
public void openFolderAndSelectFile(Path file) throws IOException {
Desktop.getDesktop().open(file.getParent().toFile());
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/desktop/os/Windows.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void openFileWithApplication(String filePath, String application) throws
}

@Override
public void openFolderAndSelectFile(String filePath) throws IOException {
new ProcessBuilder("explorer.exe", "/select,", filePath).start();
public void openFolderAndSelectFile(Path filePath) throws IOException {
new ProcessBuilder("explorer.exe", "/select,", filePath.toString()).start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import org.jabref.gui.util.OnlyIntegerFormatter;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.model.entry.ParsedFileField;
import org.jabref.model.entry.LinkedFile;

public class DocumentViewerController extends AbstractController<DocumentViewerViewModel> {

@FXML private ScrollBar scrollBar;
@FXML private ComboBox<ParsedFileField> fileChoice;
@FXML private ComboBox<LinkedFile> fileChoice;
@FXML private BorderPane mainPane;
@FXML private ToggleButton modeLive;
@FXML private TextField currentPage;
Expand Down Expand Up @@ -59,8 +59,8 @@ private void setupPageControls() {
}

private void setupFileChoice() {
ViewModelListCellFactory<ParsedFileField> cellFactory = new ViewModelListCellFactory<ParsedFileField>()
.withText(ParsedFileField::getLink);
ViewModelListCellFactory<LinkedFile> cellFactory = new ViewModelListCellFactory<LinkedFile>()
.withText(LinkedFile::getLink);
fileChoice.setButtonCell(cellFactory.call(null));
fileChoice.setCellFactory(cellFactory);
fileChoice.getSelectionModel().selectedItemProperty().addListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import org.jabref.Globals;
import org.jabref.gui.AbstractViewModel;
import org.jabref.gui.StateManager;
import org.jabref.logic.TypedBibEntry;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.ParsedFileField;
import org.jabref.model.entry.LinkedFile;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.fxmisc.easybind.EasyBind;
Expand All @@ -31,7 +29,7 @@ public class DocumentViewerViewModel extends AbstractViewModel {

private StateManager stateManager;
private ObjectProperty<DocumentViewModel> currentDocument = new SimpleObjectProperty<>();
private ListProperty<ParsedFileField> files = new SimpleListProperty<>();
private ListProperty<LinkedFile> files = new SimpleListProperty<>();
private BooleanProperty liveMode = new SimpleBooleanProperty();
private ObjectProperty<Integer> currentPage = new SimpleObjectProperty<>();
private IntegerProperty maxPages = new SimpleIntegerProperty();
Expand Down Expand Up @@ -79,7 +77,7 @@ public ObjectProperty<DocumentViewModel> currentDocumentProperty() {
return currentDocument;
}

public ListProperty<ParsedFileField> filesProperty() {
public ListProperty<LinkedFile> filesProperty() {
return files;
}

Expand All @@ -90,10 +88,9 @@ private void setCurrentEntries(List<BibEntry> entries) {
}
}

private void setCurrentEntry(BibEntry rawEntry) {
private void setCurrentEntry(BibEntry entry) {
stateManager.getActiveDatabase().ifPresent(database -> {
TypedBibEntry entry = new TypedBibEntry(rawEntry, database);
List<ParsedFileField> linkedFiles = entry.getFiles();
List<LinkedFile> linkedFiles = entry.getFiles();
// We don't need to switch to the first file, this is done automatically in the UI part
files.setValue(FXCollections.observableArrayList(linkedFiles));
});
Expand All @@ -107,10 +104,10 @@ private void setCurrentDocument(Path path) {
}
}

public void switchToFile(ParsedFileField file) {
public void switchToFile(LinkedFile file) {
if (file != null) {
stateManager.getActiveDatabase().ifPresent(database ->
FileUtil.toPath(file, database, Globals.prefs.getFileDirectoryPreferences())
file.findIn(database, Globals.prefs.getFileDirectoryPreferences())
.ifPresent(this::setCurrentDocument));
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/externalfiles/AutoSetLinks.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.JabRefPreferences;

public class AutoSetLinks {
Expand Down Expand Up @@ -149,7 +150,7 @@ public void run() {
if (!alreadyHas) {
foundAny = true;
Optional<ExternalFileType> type;
Optional<String> extension = FileUtil.getFileExtension(f);
Optional<String> extension = FileHelper.getFileExtension(f);
if (extension.isPresent()) {
type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension.get());
} else {
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/org/jabref/gui/externalfiles/DroppedFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.IdGenerator;
import org.jabref.model.util.FileHelper;
import org.jabref.preferences.JabRefPreferences;

import com.jgoodies.forms.builder.FormBuilder;
Expand Down Expand Up @@ -422,9 +423,9 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
if (new File(filename).isAbsolute() || dirs.isEmpty()) {
absFilename = filename;
} else {
Optional<File> file = FileUtil.expandFilename(filename, dirs);
Optional<Path> file = FileHelper.expandFilename(filename, dirs);
if (file.isPresent()) {
absFilename = file.get().getAbsolutePath();
absFilename = file.get().toAbsolutePath().toString();
} else {
absFilename = ""; // This shouldn't happen based on the old code, so maybe one should set it something else?
}
Expand All @@ -435,17 +436,12 @@ private void doLink(BibEntry entry, ExternalFileType fileType, String filename,
for (int i = 0; i < tm.getRowCount(); i++) {
FileListEntry flEntry = tm.getEntry(i);
// Find the absolute filename for this existing link:
String absName;
if (new File(flEntry.getLink()).isAbsolute() || dirs.isEmpty()) {
absName = flEntry.getLink();
} else {
Optional<File> file = FileUtil.expandFilename(flEntry.getLink(), dirs);
if (file.isPresent()) {
absName = file.get().getAbsolutePath();
} else {
absName = null;
}
}
String absName = flEntry.toParsedFileField()
.findIn(dirs)
.map(Path::toAbsolutePath)
.map(Path::toString)
.orElse(null);

LOGGER.debug("absName: " + absName);
// If the filenames are equal, we don't need to link, so we simply return:
if (absFilename.equals(absName)) {
Expand Down
25 changes: 7 additions & 18 deletions src/main/java/org/jabref/gui/externalfiles/MoveFileAction.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package org.jabref.gui.externalfiles;

import java.awt.event.ActionEvent;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import javax.swing.AbstractAction;
Expand All @@ -19,8 +15,7 @@
import org.jabref.gui.filelist.FileListEntry;
import org.jabref.logic.cleanup.MoveFilesCleanup;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.entry.ParsedFileField;
import org.jabref.model.entry.LinkedFile;

/**
* Action for moving a file that is linked from an entry in JabRef.
Expand All @@ -47,31 +42,25 @@ public void actionPerformed(ActionEvent event) {
}

FileListEntry entry = editor.getTableModel().getEntry(selected);
// Check if the current file exists:
String ln = entry.getLink();
ParsedFileField field = entry.toParsedFileField();
LinkedFile field = entry.toParsedFileField();

boolean httpLink = ln.toLowerCase(Locale.ENGLISH).startsWith("http");
if (httpLink) {
if (field.isOnlineLink()) {
// TODO: notify that this operation cannot be done on remote links
return;
}

// Get an absolute path representation:
List<String> dirs = frame.getCurrentBasePanel().getBibDatabaseContext()
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
Optional<Path> fileDir = frame.getCurrentBasePanel().getBibDatabaseContext()
.getFirstExistingFileDir(Globals.prefs.getFileDirectoryPreferences());
if (!fileDir.isPresent()) {
JOptionPane.showMessageDialog(frame, Localization.lang("File directory is not set or does not exist!"),
Localization.lang("Move file"), JOptionPane.ERROR_MESSAGE);
return;
}
Path file = Paths.get(ln);
if (!file.isAbsolute()) {
file = FileUtil.expandFilename(ln, dirs).map(File::toPath).orElse(null);
}

if ((file != null) && Files.exists(file)) {
// Check if the current file exists:
Optional<Path> file = field.findIn(frame.getCurrentBasePanel().getBibDatabaseContext(), Globals.prefs.getFileDirectoryPreferences());
if ((file.isPresent()) && Files.exists(file.get())) {

MoveFilesCleanup moveFiles = new MoveFilesCleanup(frame.getCurrentBasePanel().getBibDatabaseContext(),
Globals.prefs.getCleanupPreferences(Globals.journalAbbreviationLoader).getFileDirPattern(),
Expand Down
Loading