Skip to content
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.jabref.gui.actions.AutoLinkFilesAction;
import org.jabref.gui.actions.ConnectToSharedDatabaseAction;
import org.jabref.gui.actions.ErrorConsoleAction;
import org.jabref.gui.actions.ExportLinkedFilesAction;
import org.jabref.gui.actions.IntegrityCheckAction;
import org.jabref.gui.actions.LookupIdentifierAction;
import org.jabref.gui.actions.ManageKeywordsAction;
Expand Down Expand Up @@ -390,6 +391,7 @@ public void actionPerformed(ActionEvent e) {
Localization.menuTitle("Unabbreviate journal names"),
Localization.lang("Unabbreviate journal names of the selected entries"),
Globals.getKeyPrefs().getKey(KeyBinding.UNABBREVIATE));
private final AbstractAction exportLinkedFiles = new ExportLinkedFilesAction();
private final AbstractAction manageJournals = new ManageJournalsAction();
private final AbstractAction databaseProperties = new DatabasePropertiesAction();
private final AbstractAction bibtexKeyPattern = new BibtexKeyPatternAction();
Expand Down Expand Up @@ -1234,6 +1236,8 @@ private void fillMenu() {
tools.add(abbreviateIso);
tools.add(abbreviateMedline);
tools.add(unabbreviate);
tools.addSeparator();
tools.add(exportLinkedFiles);
mb.add(tools);

options.add(showPrefs);
Expand Down Expand Up @@ -1434,7 +1438,7 @@ dupliCheck, autoSetFile, newEntryAction, newSpec, customizeAction, plainTextImpo
twoEntriesOnlyActions.addAll(Arrays.asList(mergeEntries));

atLeastOneEntryActions.clear();
atLeastOneEntryActions.addAll(Arrays.asList(downloadFullText, lookupIdentifiers));
atLeastOneEntryActions.addAll(Arrays.asList(downloadFullText, lookupIdentifiers, exportLinkedFiles));

tabbedPane.addChangeListener(event -> updateEnabledState());

Expand Down
98 changes: 98 additions & 0 deletions src/main/java/org/jabref/gui/actions/ExportLinkedFilesAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.jabref.gui.actions;

import java.awt.event.ActionEvent;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;

import javax.swing.AbstractAction;

import javafx.scene.control.ProgressBar;

import org.jabref.Globals;
import org.jabref.JabRefGUI;
import org.jabref.gui.DialogService;
import org.jabref.gui.FXDialogService;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.util.FileHelper;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.JabRefPreferences;


public class ExportLinkedFilesAction extends AbstractAction {

private final TaskExecutor taskExecutor = Globals.taskExecutor;

public ExportLinkedFilesAction() {
super(Localization.lang("Export linked files"));
}

@Override
public void actionPerformed(ActionEvent e) {

DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder()
.withInitialDirectory(
Paths.get(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)))
.build();
List<BibEntry> entries = JabRefGUI.getMainFrame().getCurrentBasePanel().getSelectedEntries();

DialogService ds = new FXDialogService();
Optional<Path> path = DefaultTaskExecutor
.runInJavaFXThread(() -> ds.showDirectorySelectionDialog(dirDialogConfiguration));

BibDatabaseContext databaseContext = JabRefGUI.getMainFrame().getCurrentBasePanel().getDatabaseContext();

System.out.println("Files count " + entries.stream().flatMap(entry -> entry.getFiles().stream()).count());


int counter = 0;
BackgroundTask<Void> task = BackgroundTask.wrap(() -> {


for (BibEntry entry : entries) {

List<LinkedFile> files = entry.getFiles();
for (LinkedFile fileEntry : files) {

String fileName = fileEntry.getLink();

Optional<Path> fileToExport = FileHelper.expandFilename(fileName,
databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences()));

Optional<Path> newPath = OptionalUtil.combine(path, fileToExport, resolvePathFilename);

newPath.ifPresent(newFile -> {
System.out.println(newFile);
FileUtil.copyFile(fileToExport.get(), newFile, false);

});

}
}
return null;
});

System.out.println("Final counter " + counter);


ProgressBar bar = new ProgressBar();



}

BiFunction<Path, Path, Path> resolvePathFilename = (p, f) -> {
return p.resolve(f.getFileName());
};

}
6 changes: 0 additions & 6 deletions src/main/java/org/jabref/logic/exporter/ExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,6 @@ public void performExport(final BibDatabaseContext databaseContext, final String

}

@Override
public void performExport(final BibDatabaseContext databaseContext, Path file, final Charset encoding,
List<BibEntry> entries) throws Exception {
performExport(databaseContext, file.getFileName().toString(), encoding, entries);
}

/**
* See if there is a name formatter file bundled with this export format. If so, read
* all the name formatters so they can be used by the filter layouts.
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/org/jabref/logic/exporter/IExportFormat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.logic.exporter;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.List;

import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -38,18 +37,4 @@ public interface IExportFormat {
void performExport(BibDatabaseContext databaseContext, String file, Charset encoding, List<BibEntry> entries)
throws Exception;

/**
* Perform the Export.
* Gets the path as a java.nio.path instead of a string.
*
* @param databaseContext the database to export from.
* @param file the Path to the file to write to.The path should be an java.nio.Path
* @param encoding The encoding to use.
* @param entries A list containing all entries that
* should be exported. The list of entries must be non null
* @throws Exception
*/
void performExport(BibDatabaseContext databaseContext, Path file, Charset encoding, List<BibEntry> entries)
Copy link
Member

Choose a reason for hiding this comment

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

I actually prefer these export methods that take the file as a Path instead of string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I wanted to use it, too. However, the method with that signature never gets called which led to an NPE.
In ExportAction only the one with String is called.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe change ExpoortAction then? For now the Path method can be implemented as default and invoking the stringy one.

throws Exception;

}