Skip to content

Commit

Permalink
Fix #1771 Show all supported import types as default
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Aug 18, 2016
1 parent 63baf6f commit 06d0753
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 107 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- [#1751](https://github.com/JabRef/jabref/issues/1751) Added tooltip to web search button
- [#1758](https://github.com/JabRef/jabref/issues/1758) Added a button to open Database Properties dialog help
- Improve focus of the maintable after a sidepane gets closed (Before it would focus the toolbar or it would focus the wrong entry)
- File open dialogs now use default extensions as primary file filter

### Fixed
- Fixed [#1632](https://github.com/JabRef/jabref/issues/1632): User comments (@Comment) with or without brackets are now kept
Expand Down Expand Up @@ -72,6 +73,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1716](https://github.com/JabRef/jabref/issues/1716): `@`-Symbols stored in BibTeX fields no longer break the database
- Fixed [#1499](https://github.com/JabRef/jabref/issues/1499): {} braces are now treated correctly in in author/editor
- Fixed [#1531](https://github.com/JabRef/jabref/issues/1531): `\relax` can be used for abbreviation of author names
- Fixed [#1771](https://github.com/JabRef/jabref/issues/1771): Show all supported import types as default


### Removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class ExternalFileTypeEntryEditor {
appDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY);
}

Optional<Path> path = new FileDialog(fParent, appDir).openDialogAndGetSelectedFile();
Optional<Path> path = new FileDialog(fParent, appDir).showDialogAndGetSelectedFile();
path.ifPresent(applicationDir -> {
if (applicationDir.getParent() != null) {
Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, applicationDir.getParent().toString());
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/net/sf/jabref/gui/FileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import net.sf.jabref.Globals;
Expand Down Expand Up @@ -57,7 +58,6 @@ public void approveSelection() {

private final JFrame parent;
private final String directory;
private List<FileNameExtensionFilter> fileFilters = new ArrayList<>();
private Collection<FileExtensions> extensions = EnumSet.noneOf(FileExtensions.class);

/**
Expand Down Expand Up @@ -111,7 +111,6 @@ public FileDialog withExtensions(Collection<FileExtensions> fileExtensions) {
for (FileExtensions ext : fileExtensions) {
FileNameExtensionFilter extFilter = new FileNameExtensionFilter(ext.getDescription(), ext.getExtensions());
fileChooser.addChoosableFileFilter(extFilter);
fileFilters.add(extFilter);
}

return this;
Expand All @@ -130,6 +129,25 @@ public void setDefaultExtension(FileExtensions extension) {
.ifPresent(fileChooser::setFileFilter);
}

/**
* Returns the currently selected file filter.
*
* @return FileFilter
*/
public FileFilter getFileFilter() {
return fileChooser.getFileFilter();
}

/**
* Sets a custom file filter.
* Only use when withExtension() does not suffice.
*
* @param filter the custom file filter
*/
public void setFileFiler(FileFilter filter) {
fileChooser.setFileFilter(filter);
}

/**
* Updates the working directory preference
* @return FileDialog
Expand Down Expand Up @@ -161,7 +179,7 @@ public List<String> showDialogAndGetMultipleFiles() {
* Shows an {@link JFileChooser#OPEN_DIALOG} and allows to select a single file/folder
* @return The path of the selected file/folder or {@link Optional#empty()} if dialog is aborted
*/
public Optional<Path> openDialogAndGetSelectedFile() {
public Optional<Path> showDialogAndGetSelectedFile() {
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);

if (showDialogAndIsAccepted()) {
Expand Down Expand Up @@ -198,5 +216,4 @@ private boolean showDialogAndIsAccepted() {
private static String getWorkingDir() {
return Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY);
}

}
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/FileListEntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public boolean okPressed() {
workingDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY);
}

Optional<Path> path = new FileDialog(this.frame, workingDir).openDialogAndGetSelectedFile();
Optional<Path> path = new FileDialog(this.frame, workingDir).showDialogAndGetSelectedFile();

path.ifPresent(selection -> {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/actions/BrowseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public void actionPerformed(ActionEvent e) {
private String askUser() {
if (dirsOnly) {
Path path = new FileDialog(frame, comp.getText()).dirsOnly().withExtensions(extensions)
.openDialogAndGetSelectedFile().orElse(Paths.get(""));
.showDialogAndGetSelectedFile().orElse(Paths.get(""));
String file = path.toString();

return file;
} else {
Path path = new FileDialog(frame, comp.getText()).withExtensions(extensions)
.openDialogAndGetSelectedFile().orElse(Paths.get(""));
.showDialogAndGetSelectedFile().orElse(Paths.get(""));
String file = path.toString();

return file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ImportCustomizationDialog(final JabRefFrame frame) {
CustomImporter importer = new CustomImporter();

Optional<Path> selectedFile = new FileDialog(frame).withExtension(FileExtensions.CLASS)
.openDialogAndGetSelectedFile();
.showDialogAndGetSelectedFile();

if (selectedFile.isPresent() && (selectedFile.get().getParent() != null)) {
importer.setBasePath(selectedFile.get().getParent().toString());
Expand Down Expand Up @@ -137,7 +137,7 @@ public ImportCustomizationDialog(final JabRefFrame frame) {
JButton addFromJarButton = new JButton(Localization.lang("Add from JAR"));
addFromJarButton.addActionListener(e -> {
Optional<Path> jarZipFile = new FileDialog(frame)
.withExtensions(EnumSet.of(FileExtensions.ZIP, FileExtensions.JAR)).openDialogAndGetSelectedFile();
.withExtensions(EnumSet.of(FileExtensions.ZIP, FileExtensions.JAR)).showDialogAndGetSelectedFile();

if (jarZipFile.isPresent()) {
try (ZipFile zipFile = new ZipFile(jarZipFile.get().toFile(), ZipFile.OPEN_READ)) {
Expand Down
136 changes: 52 additions & 84 deletions src/main/java/net/sf/jabref/gui/importer/ImportFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,37 @@

import java.awt.event.ActionEvent;
import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import net.sf.jabref.Globals;
import net.sf.jabref.gui.FileDialog;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.actions.MnemonicAwareAction;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.importer.fileformat.ImportFormat;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.FileExtensions;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ImportFormats {

private static final Log LOGGER = LogFactory.getLog(ImportFormats.class);


private static JFileChooser createImportFileChooser(String currentDir) {

SortedSet<ImportFormat> importers = Globals.IMPORT_FORMAT_READER.getImportFormats();

String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_IMPORT);
FileFilter defaultFilter = null;
JFileChooser fc = new JFileChooser(currentDir);
Set<ImportFileFilter> filters = new TreeSet<>();
for (ImportFormat format : importers) {
ImportFileFilter filter = new ImportFileFilter(format);
filters.add(filter);
if (format.getFormatName().equals(lastUsedFormat)) {
defaultFilter = filter;
}
}
for (ImportFileFilter filter : filters) {
fc.addChoosableFileFilter(filter);
}

if (defaultFilter == null) {
fc.setFileFilter(fc.getAcceptAllFileFilter());
} else {
fc.setFileFilter(defaultFilter);
}
return fc;
}

/**
* Create an AbstractAction for performing an Import operation.
* @param frame The JabRefFrame of this JabRef instance.
Expand All @@ -84,64 +60,56 @@ public static AbstractAction getImportAction(JabRefFrame frame, boolean openInNe
class ImportAction extends MnemonicAwareAction {

private final JabRefFrame frame;
private final boolean openInNew;

private final boolean newDatabase;

public ImportAction(JabRefFrame frame, boolean openInNew) {
public ImportAction(JabRefFrame frame, boolean newDatabase) {
this.frame = frame;
this.openInNew = openInNew;

putValue(Action.NAME, openInNew ? Localization.menuTitle("Import into new database") : Localization
.menuTitle("Import into current database"));
putValue(Action.ACCELERATOR_KEY,
openInNew ? Globals.getKeyPrefs().getKey(KeyBinding.IMPORT_INTO_NEW_DATABASE) : Globals
.getKeyPrefs().getKey(KeyBinding.IMPORT_INTO_CURRENT_DATABASE));
this.newDatabase = newDatabase;

if (newDatabase) {
putValue(Action.NAME, Localization.menuTitle("Import into new database"));
putValue(Action.ACCELERATOR_KEY, Globals.getKeyPrefs().getKey(KeyBinding.IMPORT_INTO_NEW_DATABASE));
} else {
putValue(Action.NAME, Localization.menuTitle("Import into current database"));
putValue(Action.ACCELERATOR_KEY, Globals.getKeyPrefs().getKey(KeyBinding.IMPORT_INTO_CURRENT_DATABASE));
}
}

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = createImportFileChooser(
Globals.prefs.get(JabRefPreferences.IMPORT_WORKING_DIRECTORY));
int result = fileChooser.showOpenDialog(frame);

if (result != JFileChooser.APPROVE_OPTION) {
return;
}

File file = fileChooser.getSelectedFile();
if (file == null) {
return;
}

FileFilter ff = fileChooser.getFileFilter();
ImportFormat format = null;
if (ff instanceof ImportFileFilter) {
format = ((ImportFileFilter) ff).getImportFormat();
}

try {
if (!file.exists()) {
// Warn that the file doesn't exists:
JOptionPane.showMessageDialog(frame,
Localization.lang("File not found") + ": '" + file.getName() + "'.",
Localization.lang("Import"), JOptionPane.ERROR_MESSAGE);
return;
SortedSet<ImportFormat> importers = Globals.IMPORT_FORMAT_READER.getImportFormats();
List<FileExtensions> extensions = importers.stream().map(p -> p.getExtensions()).collect(Collectors.toList());
FileDialog dialog = new FileDialog(frame, Globals.prefs.get(JabRefPreferences.IMPORT_WORKING_DIRECTORY));
// Add file filter for all supported types
List<String> flatExtensions = extensions.stream().flatMap(extList -> Stream.of(extList.getExtensions())).collect(Collectors.toList());
dialog.setFileFiler(new FileNameExtensionFilter(Localization.lang("Available import formats"), flatExtensions.toArray(new String[flatExtensions.size()])));
// Add filters for extensions
dialog.withExtensions(extensions);

Optional<Path> selectedFile = dialog.showDialogAndGetSelectedFile();

selectedFile.ifPresent(sel -> {
try {
File file = sel.toFile();

if (!file.exists()) {
JOptionPane.showMessageDialog(frame,
Localization.lang("File not found") + ": '" + file.getName() + "'.",
Localization.lang("Import"), JOptionPane.ERROR_MESSAGE);
return;
}

Optional<ImportFormat> format = importers.stream()
.filter(i -> Objects.equals(i.getExtensions().getDescription(), dialog.getFileFilter().getDescription()))
.findFirst();
ImportMenuItem importMenu = new ImportMenuItem(frame, newDatabase, format.orElse(null));
importMenu.automatedImport(Collections.singletonList(file.getAbsolutePath()));
// Set last working dir for import
Globals.prefs.put(JabRefPreferences.IMPORT_WORKING_DIRECTORY, file.getParent());
} catch (Exception ex) {
LOGGER.warn("Cannot import file", ex);
}
ImportMenuItem imi = new ImportMenuItem(frame, openInNew, format);
imi.automatedImport(Collections.singletonList(file.getAbsolutePath()));

// Make sure we remember which filter was used, to set the default
// for next time:
if (format == null) {
Globals.prefs.put(JabRefPreferences.LAST_USED_IMPORT, "__all");
} else {
Globals.prefs.put(JabRefPreferences.LAST_USED_IMPORT, format.getFormatName());
}
Globals.prefs.put(JabRefPreferences.IMPORT_WORKING_DIRECTORY, file.getParent());
} catch (Exception ex) {
LOGGER.warn("Problem with import format", ex);
}

});
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/main/java/net/sf/jabref/gui/importer/ImportMenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

/*
* TODO: could separate the "menu item" functionality from the importing functionality
*
*/
public class ImportMenuItem extends JMenuItem implements ActionListener {

Expand All @@ -56,7 +55,6 @@ public class ImportMenuItem extends JMenuItem implements ActionListener {
private final ImportFormat importer;
private IOException importError;


public ImportMenuItem(JabRefFrame frame, boolean openInNew) {
this(frame, openInNew, null);
}
Expand Down Expand Up @@ -98,7 +96,6 @@ class MyWorker extends AbstractWorker {
private ParserResult bibtexResult; // Contains the merged import results
private boolean fileOk;


@Override
public void init() {
importError = null;
Expand Down Expand Up @@ -134,10 +131,6 @@ public void run() {
frame.output(Localization.lang("Importing in %0 format", importer.getFormatName()) + "...");
// Specific importer:
ParserResult pr = importer.importDatabase(file, Globals.prefs.getDefaultEncoding());
if (pr.hasWarnings()) {
frame.showMessage(pr.getErrorMessage());
}

imports.add(new ImportFormatReader.UnknownFormatImport(importer.getFormatName(), pr));
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public ManageJournalsPanel(final JabRefFrame frame) {
});

browseOld.addActionListener(e -> {
Optional<Path> path = new FileDialog(frame, personalFile.getText()).openDialogAndGetSelectedFile();
Optional<Path> path = new FileDialog(frame, personalFile.getText()).showDialogAndGetSelectedFile();

path.ifPresent(fileName -> {
personalFile.setText(fileName.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public LoadAction() {
public void actionPerformed(ActionEvent e) {
try {
Optional<Path> path = new FileDialog(frame).withExtension(FileExtensions.TXT)
.openDialogAndGetSelectedFile();
.showDialogAndGetSelectedFile();
if (path.isPresent()) {
File newFile = path.get().toFile();
document.remove(0, document.getLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public PreferencesDialog(JabRefFrame parent) {
importPreferences.addActionListener(e -> {
FileDialog dialog = new FileDialog(frame, System.getProperty("user.home")).withExtension(FileExtensions.XML);
dialog.setDefaultExtension(FileExtensions.XML);
Optional<Path> fileName = dialog.openDialogAndGetSelectedFile();
Optional<Path> fileName = dialog.showDialogAndGetSelectedFile();

if (fileName.isPresent()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.commons.logging.LogFactory;

public class ImportFormatReader {
private static final Log LOGGER = LogFactory.getLog(ImportFormatReader.class);

public static final String BIBTEX_FORMAT = "BibTeX";

Expand All @@ -61,8 +62,6 @@ public class ImportFormatReader {
*/
private final SortedSet<ImportFormat> formats = new TreeSet<>();

private static final Log LOGGER = LogFactory.getLog(ImportFormatReader.class);

private ImportFormatPreferences importFormatPreferences;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ public class JabRefPreferences {
public static final String SHOW_FILE_LINKS_UPGRADE_WARNING = "showFileLinksUpgradeWarning";
public static final String SIDE_PANE_WIDTH = "sidePaneWidth";
public static final String LAST_USED_EXPORT = "lastUsedExport";
public static final String LAST_USED_IMPORT = "lastUsedImport";
public static final String FLOAT_MARKED_ENTRIES = "floatMarkedEntries";
public static final String CITE_COMMAND = "citeCommand";
public static final String EXTERNAL_JOURNAL_LISTS = "externalJournalLists";
Expand Down

0 comments on commit 06d0753

Please sign in to comment.