Skip to content

Commit

Permalink
Merge pull request #1774 from JabRef/import-dialogs
Browse files Browse the repository at this point in the history
Fix #1771 Show all supported import types as default file filter
  • Loading branch information
stefan-kolb committed Aug 19, 2016
2 parents 39fee47 + 47d069f commit c9921ba
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 123 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
26 changes: 21 additions & 5 deletions src/main/java/net/sf/jabref/gui/FileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -16,6 +15,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 +57,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 +110,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 +128,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 setFileFilter(FileFilter filter) {
fileChooser.setFileFilter(filter);
}

/**
* Updates the working directory preference
* @return FileDialog
Expand Down Expand Up @@ -161,7 +178,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 +215,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
34 changes: 19 additions & 15 deletions src/main/java/net/sf/jabref/gui/importer/ImportFileFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
package net.sf.jabref.gui.importer;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
Expand All @@ -24,36 +28,36 @@
import net.sf.jabref.logic.util.FileExtensions;

class ImportFileFilter extends FileFilter implements Comparable<ImportFileFilter> {

private final ImportFormat format;
private final String name;
private final FileNameExtensionFilter extensionFilter;

private final String description;
private final FileNameExtensionFilter fileFilter;

public ImportFileFilter(ImportFormat format) {
this.format = format;
FileExtensions extensions = format.getExtensions();
this.name = extensions.getDescription();
this.extensionFilter = new FileNameExtensionFilter(extensions.getDescription(), extensions.getExtensions());
this.description = extensions.getDescription();
fileFilter = new FileNameExtensionFilter(extensions.getDescription(), extensions.getExtensions());
}

public ImportFormat getImportFormat() {
return format;
public ImportFileFilter(String description, Collection<ImportFormat> formats) {
this.description = description;

List<FileExtensions> extensions = formats.stream().map(p -> p.getExtensions()).collect(Collectors.toList());
List<String> flatExtensions = extensions.stream().flatMap(extList -> Stream.of(extList.getExtensions())).collect(Collectors.toList());
fileFilter = new FileNameExtensionFilter(description, flatExtensions.toArray(new String[flatExtensions.size()]));
}

@Override
public boolean accept(File file) {
return (file != null) && (file.isDirectory() || extensionFilter.accept(file));
return (file != null) && (file.isDirectory() || fileFilter.accept(file));
}

@Override
public String getDescription() {
return name;
return description;
}

@Override
public int compareTo(ImportFileFilter o) {
return name.compareTo(o.name);
return description.compareTo(o.description);
}

@Override
Expand All @@ -62,14 +66,14 @@ public boolean equals(Object o) {
return true;
}
if (o instanceof ImportFileFilter) {
return name.equals(((ImportFileFilter) o).name);
return description.equals(((ImportFileFilter) o).description);
}
return false;
}

@Override
public int hashCode() {
return name.hashCode();
return description.hashCode();
}

}
134 changes: 50 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,60 +17,34 @@

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 javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

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 @@ -83,64 +57,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
ImportFileFilter allImports = new ImportFileFilter(Localization.lang("Available import formats"), importers);
dialog.setFileFilter(allImports);
// 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
Loading

0 comments on commit c9921ba

Please sign in to comment.