Skip to content

Commit

Permalink
Refactor exports to fix #3576
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Dec 25, 2017
1 parent dd83589 commit ce03e05
Show file tree
Hide file tree
Showing 95 changed files with 741 additions and 817 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.FileUpdateMonitor;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class Globals {
private static GlobalFocusListener focusListener;
private static FileUpdateMonitor fileUpdateMonitor;
private static TelemetryClient telemetryClient;
public static ExporterFactory exportFactory;

private Globals() {
}
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/org/jabref/JabRefMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref;

import java.net.Authenticator;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
Expand All @@ -13,13 +12,10 @@

import org.jabref.cli.ArgumentProcessor;
import org.jabref.gui.remote.JabRefMessageHandler;
import org.jabref.logic.exporter.ExportFormat;
import org.jabref.logic.exporter.ExportFormats;
import org.jabref.logic.exporter.SavePreferences;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.formatter.casechanger.ProtectTermsFormatter;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.net.ProxyAuthenticator;
import org.jabref.logic.net.ProxyPreferences;
import org.jabref.logic.net.ProxyRegisterer;
Expand Down Expand Up @@ -146,12 +142,7 @@ private static void start(String[] args) {
Globals.prefs.getXMPPreferences());
EntryTypes.loadCustomEntryTypes(preferences.loadCustomEntryTypes(BibDatabaseMode.BIBTEX),
preferences.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX));
Map<String, ExportFormat> customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs,
Globals.journalAbbreviationLoader);
LayoutFormatterPreferences layoutPreferences = Globals.prefs
.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs);
ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);
Globals.exportFactory = ExporterFactory.create(Globals.prefs, Globals.journalAbbreviationLoader);

// Initialize protected terms loader
Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences());
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.exporter.BibDatabaseWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.ExportFormat;
import org.jabref.logic.exporter.ExportFormats;
import org.jabref.logic.exporter.Exporter;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.exporter.FileSaveSession;
import org.jabref.logic.exporter.IExportFormat;
import org.jabref.logic.exporter.SaveException;
import org.jabref.logic.exporter.SavePreferences;
import org.jabref.logic.exporter.SaveSession;
import org.jabref.logic.exporter.TemplateExporter;
import org.jabref.logic.importer.ImportException;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.importer.OpenDatabase;
Expand Down Expand Up @@ -250,7 +250,7 @@ private boolean exportMatches(List<ParserResult> loaded) {
formatName = data[2];
break;
case 2:
//default ExportFormat: HTML table (with Abstract & BibTeX)
//default exporter: HTML table (with Abstract & BibTeX)
formatName = "tablerefsabsbib";
break;
default:
Expand All @@ -261,14 +261,14 @@ private boolean exportMatches(List<ParserResult> loaded) {
}

//export new database
IExportFormat format = ExportFormats.getExportFormat(formatName);
if (format == null) {
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(formatName);
if (!exporter.isPresent()) {
System.err.println(Localization.lang("Unknown export format") + ": " + formatName);
} else {
// We have an ExportFormat instance:
// We have an TemplateExporter instance:
try {
System.out.println(Localization.lang("Exporting") + ": " + data[1]);
format.performExport(databaseContext, data[1],
exporter.get().export(databaseContext, Paths.get(data[1]),
databaseContext.getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()),
matches);
} catch (Exception ex) {
Expand Down Expand Up @@ -438,18 +438,17 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
Globals.prefs.fileDirForDatabase = databaseContext
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
IExportFormat format = ExportFormats.getExportFormat(data[1]);
if (format == null) {
Optional<Exporter> exporter = Globals.exportFactory.getExporterByName(data[1]);
if (!exporter.isPresent()) {
System.err.println(Localization.lang("Unknown export format") + ": " + data[1]);
} else {
// We have an ExportFormat instance:
// We have an exporter:
try {
format.performExport(pr.getDatabaseContext(), data[0],
exporter.get().export(pr.getDatabaseContext(), Paths.get(data[0]),
pr.getDatabaseContext().getMetaData().getEncoding()
.orElse(Globals.prefs.getDefaultEncoding()),
pr.getDatabaseContext().getDatabase().getEntries());
} catch (Exception ex) {

System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': "
+ Throwables.getStackTraceAsString(ex));
}
Expand All @@ -463,12 +462,12 @@ private void importPreferences() {
Globals.prefs.importPreferences(cli.getPreferencesImport());
EntryTypes.loadCustomEntryTypes(Globals.prefs.loadCustomEntryTypes(BibDatabaseMode.BIBTEX),
Globals.prefs.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX));
Map<String, ExportFormat> customFormats = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs,
Map<String, TemplateExporter> customExporters = Globals.prefs.customExports.getCustomExportFormats(Globals.prefs,
Globals.journalAbbreviationLoader);
LayoutFormatterPreferences layoutPreferences = Globals.prefs
.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
SavePreferences savePreferences = SavePreferences.loadForExportFromPreferences(Globals.prefs);
ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);
Globals.exportFactory = ExporterFactory.create(customExporters, layoutPreferences, savePreferences);
} catch (JabRefException ex) {
LOGGER.error("Cannot import preferences", ex);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;

import org.jabref.Globals;
import org.jabref.logic.exporter.ExportFormats;
import org.jabref.logic.l10n.Localization;

import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -246,7 +245,7 @@ public void printUsage() {
String importFormats = Globals.IMPORT_FORMAT_READER.getImportFormatList();
String importFormatsList = String.format("%s:%n%s%n", Localization.lang("Available import formats"), importFormats);

String outFormats = ExportFormats.getConsoleExportList(70, 20, "");
String outFormats = Globals.exportFactory.getExportersAsString(70, 20, "");
String outFormatsList = String.format("%s: %s%n", Localization.lang("Available export formats"), outFormats);

String footer = '\n' + importFormatsList + outFormatsList + "\nPlease report issues at https://github.com/JabRef/jabref/issues.";
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
import org.jabref.logic.layout.LayoutHelper;
import org.jabref.logic.pdf.FileAnnotationCache;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.util.FileExtensions;
import org.jabref.logic.util.FileType;
import org.jabref.logic.util.UpdateField;
import org.jabref.logic.util.io.FileFinder;
import org.jabref.logic.util.io.FileFinders;
Expand Down Expand Up @@ -2225,8 +2225,8 @@ public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) {
@Override
public void action() throws SaveException {
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.withDefaultExtension(FileExtensions.BIBTEX_DB)
.addExtensionFilter(FileExtensions.BIBTEX_DB)
.withDefaultExtension(FileType.BIBTEX_DB)
.addExtensionFilter(FileType.BIBTEX_DB)
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();

DialogService ds = new FXDialogService();
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/org/jabref/gui/DialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.stage.FileChooser;

import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.FileDialogConfiguration;
Expand Down Expand Up @@ -140,6 +139,7 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
* Shows a new file save dialog. The method doesn't return until the
* displayed file save dialog is dismissed. The return value specifies the
* file chosen by the user or an empty {@link Optional} if no selection has been made.
* After a file was selected, the given file dialog configuration is updated with the selected extension type (if any).
*
* @return the selected file or an empty {@link Optional} if no file has been selected
*/
Expand All @@ -150,6 +150,7 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
* displayed open dialog is dismissed. The return value specifies
* the file chosen by the user or an empty {@link Optional} if no selection has been
* made.
* After a file was selected, the given file dialog configuration is updated with the selected extension type (if any).
*
* @return the selected file or an empty {@link Optional} if no file has been selected
*/
Expand All @@ -174,13 +175,4 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
* @return the selected directory or an empty {@link Optional} if no directory has been selected
*/
Optional<Path> showDirectorySelectionDialog(DirectoryDialogConfiguration directoryDialogConfiguration);

/**
* Gets the configured {@link FileChooser}, should only be necessary in rare use cases.
* For normal usage use the show-Methods which directly return the selected file(s)
* @param fileDialogConfiguration
* @return A configured instance of the {@link FileChooser}
*/
FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration);

}
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/gui/FXDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ public void notify(String message) {
public Optional<Path> showFileSaveDialog(FileDialogConfiguration fileDialogConfiguration) {
FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration);
File file = chooser.showSaveDialog(null);
Optional.ofNullable(chooser.getSelectedExtensionFilter()).ifPresent(fileDialogConfiguration::setSelectedExtensionFilter);
return Optional.ofNullable(file).map(File::toPath);
}

@Override
public Optional<Path> showFileOpenDialog(FileDialogConfiguration fileDialogConfiguration) {
FileChooser chooser = getConfiguredFileChooser(fileDialogConfiguration);
File file = chooser.showOpenDialog(null);
Optional.ofNullable(chooser.getSelectedExtensionFilter()).ifPresent(fileDialogConfiguration::setSelectedExtensionFilter);
return Optional.ofNullable(file).map(File::toPath);
}

Expand All @@ -183,8 +185,7 @@ private DirectoryChooser getConfiguredDirectoryChooser(DirectoryDialogConfigurat
return chooser;
}

@Override
public FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration) {
private FileChooser getConfiguredFileChooser(FileDialogConfiguration fileDialogConfiguration) {
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().addAll(fileDialogConfiguration.getExtensionFilters());
chooser.setSelectedExtensionFilter(fileDialogConfiguration.getDefaultExtension());
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.worker.CitationStyleWorker;
import org.jabref.logic.citationstyle.CitationStyle;
import org.jabref.logic.exporter.ExportFormats;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutHelper;
Expand Down Expand Up @@ -205,11 +205,11 @@ public void updateLayout() {
if (CitationStyle.isCitationStyleFile(style)) {
if (basePanel.isPresent()) {
layout = Optional.empty();
CitationStyle citationStyle = CitationStyle.createCitationStyleFromFile(style);
if (citationStyle != null) {
CitationStyle.createCitationStyleFromFile(style)
.ifPresent(citationStyle -> {
basePanel.get().getCitationStyleCache().setCitationStyle(citationStyle);
basePanel.get().output(Localization.lang("Preview style changed to: %0", citationStyle.getTitle()));
}
});
}
} else {
updatePreviewLayout(previewPreferences.getPreviewStyle());
Expand Down Expand Up @@ -262,7 +262,7 @@ public BibEntry getEntry() {
}

public void update() {
ExportFormats.entryNumber = 1; // Set entry number in case that is included in the preview layout.
ExporterFactory.entryNumber = 1; // Set entry number in case that is included in the preview layout.

if (citationStyleWorker.isPresent()) {
citationStyleWorker.get().cancel(true);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/auximport/FromAuxDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.jabref.logic.auxparser.AuxParser;
import org.jabref.logic.auxparser.AuxParserResult;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.FileExtensions;
import org.jabref.logic.util.FileType;
import org.jabref.model.database.BibDatabase;
import org.jabref.preferences.JabRefPreferences;

Expand Down Expand Up @@ -159,8 +159,8 @@ private void initPanels() {
JButton browseAuxFileButton = new JButton(Localization.lang("Browse"));

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(FileExtensions.AUX)
.withDefaultExtension(FileExtensions.AUX)
.addExtensionFilter(FileType.AUX)
.withDefaultExtension(FileType.AUX)
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)).build();
DialogService ds = new FXDialogService();

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/exporter/CustomExportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.FileExtensions;
import org.jabref.logic.util.FileType;
import org.jabref.preferences.JabRefPreferences;

import com.jgoodies.forms.builder.ButtonBarBuilder;
Expand Down Expand Up @@ -95,8 +95,8 @@ public CustomExportDialog(final JabRefFrame parent) {
JButton browse = new JButton(Localization.lang("Browse"));

FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
.addExtensionFilter(FileExtensions.LAYOUT)
.withDefaultExtension(FileExtensions.LAYOUT)
.addExtensionFilter(FileType.LAYOUT)
.withDefaultExtension(FileType.LAYOUT)
.withInitialDirectory(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)).build();
DialogService ds = new FXDialogService();
browse.addActionListener(
Expand Down
Loading

0 comments on commit ce03e05

Please sign in to comment.