Skip to content

Commit

Permalink
Refactor export code to fix #3576 (#3578)
Browse files Browse the repository at this point in the history
* Refactor exports to fix #3576

* Add changelog

* Fix build

* Implement feedback and fix tests

* Include feedback

* Fix checkstyle...again...joy!
  • Loading branch information
tobiasdiez authored Jan 2, 2018
1 parent c438554 commit d381839
Show file tree
Hide file tree
Showing 98 changed files with 808 additions and 898 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#


### Fixed
- We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576)

### Removed
- We removed the Look and Feels from jgoodies, because these are not compatible with Java 9
Expand Down
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 @@ -48,6 +49,7 @@ public class Globals {
* Manager for the state of the GUI.
*/
public static StateManager stateManager = new StateManager();
public static ExporterFactory exportFactory;
// Key binding preferences
private static KeyBindingRepository keyBindingRepository;
// Background tasks
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 @@ -147,12 +143,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.BibtexKeyGenerator;
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 @@ -249,7 +249,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 @@ -260,14 +260,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 @@ -437,18 +437,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 @@ -462,12 +461,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 @@ -116,7 +116,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 @@ -2221,8 +2221,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
20 changes: 9 additions & 11 deletions src/main/java/org/jabref/gui/DialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 @@ -50,13 +49,15 @@ public interface DialogService {

/**
* Create and display error dialog displaying the given exception.
* @param message the error message
*
* @param message the error message
* @param exception the exception causing the error
*/
void showErrorDialogAndWait(String message, Throwable exception);

/**
* Create and display error dialog displaying the given exception.
*
* @param exception the exception causing the error
*/
default void showErrorDialogAndWait(Exception exception) {
Expand All @@ -65,6 +66,7 @@ default void showErrorDialogAndWait(Exception exception) {

/**
* Create and display error dialog displaying the given message.
*
* @param message the error message
*/
void showErrorDialogAndWait(String message);
Expand Down Expand Up @@ -107,7 +109,7 @@ default void showErrorDialogAndWait(Exception exception) {
* @return Optional with the pressed Button as ButtonType
*/
Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String title, String content,
ButtonType... buttonTypes);
ButtonType... buttonTypes);

/**
* This will create and display a new dialog showing a custom {@link DialogPane}
Expand All @@ -127,12 +129,14 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String

/**
* Constructs and shows a canceable {@link ProgressDialog}. Clicking cancel will cancel the underlying service and close the dialog
*
* @param task The {@link Task} which executes the work and for which to show the dialog
*/
<V> void showCanceableProgressDialogAndWait(Task<V> task);

/**
* Notify the user in an non-blocking way (i.e., update status message instead of showing a dialog).
*
* @param message the message to show.
*/
void notify(String message);
Expand All @@ -141,6 +145,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 @@ -151,6 +156,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 @@ -176,14 +182,6 @@ Optional<ButtonType> showCustomButtonDialogAndWait(Alert.AlertType type, String
*/
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);

/**
* Displays a Print Dialog. Allow the user to update job state such as printer and settings. These changes will be
* available in the appropriate properties after the print dialog has returned. The print dialog is also used to
Expand Down
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 @@ -154,13 +154,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 @@ -184,8 +186,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 @@ -23,7 +23,7 @@
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.DefaultTaskExecutor;
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 @@ -167,11 +167,11 @@ public void updateLayout(PreviewPreferences previewPreferences) {
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 @@ -225,7 +225,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 (citationStyleFuture.isPresent()) {
citationStyleFuture.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 d381839

Please sign in to comment.