diff --git a/CHANGELOG.md b/CHANGELOG.md
index f35093a83f7..00c6d9370a2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- [#1345](https://github.com/JabRef/jabref/issues/1345) Cleanup ISSN
- It is now possible to add your own lists of protected terms, see Options -> Manage protected terms
- Automatically generated group names are now converted from LaTeX to Unicode
+- Unified dialogs for opening/saving files
### Fixed
- Fixed [#1632](https://github.com/JabRef/jabref/issues/1632) User comments (@Comment) with or without brackets are now kept
@@ -50,8 +51,9 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1643](https://github.com/JabRef/jabref/issues/1643): Searching with double quotes in a specific field ignores the last character
- Fixed [#1288](https://github.com/JabRef/jabref/issues/1288): Newly opened bib-file is not focused
- Fixed [#1669](https://github.com/JabRef/jabref/issues/1669): Dialog for manual connection to OpenOffice/LibreOffice works again on Linux
-- Fixed [#16682](https://github.com/JabRef/jabref/issues/1682): An entry now must have a BibTeX key to be cited in OpenOffice/LibreOffice
-
+- Fixed [#1682](https://github.com/JabRef/jabref/issues/1682): An entry now must have a BibTeX key to be cited in OpenOffice/LibreOffice
+- Fixed [#1324](https://github.com/JabRef/jabref/issues/1324): Save-Dialog for Lookup fulltext document now opens in the specified working directory
+- Fixed [#1609](https://github.com/JabRef/jabref/issues/1324): Adding a file to an entry opened dialog in the parent folder of the working directory
### Removed
- It is not longer possible to choose to convert HTML sub- and superscripts to equations
diff --git a/src/main/java/net/sf/jabref/external/ExternalFileTypeEditor.java b/src/main/java/net/sf/jabref/external/ExternalFileTypeEditor.java
index 374b1778bca..8f2ca3deff2 100644
--- a/src/main/java/net/sf/jabref/external/ExternalFileTypeEditor.java
+++ b/src/main/java/net/sf/jabref/external/ExternalFileTypeEditor.java
@@ -21,6 +21,7 @@
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
+
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -58,6 +59,7 @@
* Editor for external file types.
*/
public class ExternalFileTypeEditor extends JDialog {
+
private JFrame frame;
private JDialog dialog;
private List fileTypes;
@@ -111,6 +113,7 @@ private void init() {
dispose();
});
Action cancelAction = new AbstractAction() {
+
@Override
public void actionPerformed(ActionEvent e) {
dispose();
@@ -134,9 +137,10 @@ public void actionPerformed(ActionEvent e) {
//}
});
- add.addActionListener(e -> {
+ add.addActionListener(e -> {
// Generate a new file type:
- ExternalFileType type = new ExternalFileType("", "", "", "", "new", IconTheme.JabRefIcon.FILE.getSmallIcon());
+ ExternalFileType type = new ExternalFileType("", "", "", "", "new",
+ IconTheme.JabRefIcon.FILE.getSmallIcon());
// Show the file type editor:
getEditor(type).setVisible(true);
if (entryEditor.okPressed()) {
@@ -246,6 +250,7 @@ public static AbstractAction getAction(JDialog dialog) {
return new EditExternalFileTypesAction(dialog);
}
+
class EditListener implements ActionListener {
@Override
@@ -276,6 +281,7 @@ public Component getTableCellRendererComponent(JTable tab, Object value, boolean
}
private class FileTypeTableModel extends AbstractTableModel {
+
@Override
public int getColumnCount() {
return 5;
@@ -354,6 +360,7 @@ public void mouseReleased(MouseEvent e) {
}
public static class EditExternalFileTypesAction extends MnemonicAwareAction {
+
private JabRefFrame frame;
private JDialog dialog;
private ExternalFileTypeEditor editor;
diff --git a/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java b/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java
index b584e79ea44..aa738413506 100644
--- a/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java
+++ b/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java
@@ -17,16 +17,14 @@
import java.awt.BorderLayout;
import java.awt.Dimension;
-import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-import java.io.File;
-import java.util.Collections;
+import java.nio.file.Path;
+import java.util.Optional;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
-import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@@ -36,8 +34,8 @@
import javax.swing.event.DocumentListener;
import net.sf.jabref.Globals;
-import net.sf.jabref.gui.FileDialogs;
import net.sf.jabref.gui.IconTheme;
+import net.sf.jabref.gui.NewFileDialogs;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.preferences.JabRefPreferences;
@@ -89,8 +87,8 @@ private void init(ExternalFileType inEntry) {
bg.add(other);
FormBuilder builder = FormBuilder.create();
- builder.layout(new FormLayout
- ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p"));
+ builder.layout(new FormLayout("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref",
+ "p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p"));
builder.add(Localization.lang("Icon")).xy(1, 1);
builder.add(icon).xy(3, 1);
builder.add(Localization.lang("Name")).xy(1, 3);
@@ -128,6 +126,7 @@ private void init(ExternalFileType inEntry) {
ok.addActionListener(e -> {
okPressed = true;
+
storeSettings(ExternalFileTypeEntryEditor.this.entry);
diag.dispose();
@@ -177,8 +176,7 @@ public void changedUpdate(DocumentEvent documentEvent) {
diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
diag.pack();
- BrowseListener browse = new BrowseListener(application);
- browseBut.addActionListener(browse);
+ browseBut.addActionListener(browsePressed);
if (dParent == null) {
diag.setLocationRelativeTo(fParent);
@@ -248,31 +246,19 @@ public boolean okPressed() {
}
- static class BrowseListener implements ActionListener {
-
- private final JTextField comp;
-
-
- public BrowseListener(JTextField comp) {
- this.comp = comp;
+ private final ActionListener browsePressed = e -> {
+ String appDir = application.getText().trim();
+ if (appDir.isEmpty()) {
+ appDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY);
}
- @Override
- public void actionPerformed(ActionEvent e) {
- File initial = new File(comp.getText().trim());
- if (comp.getText().trim().isEmpty()) {
- // Nothing in the field. Go to the last file dir used:
- initial = new File(Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY));
- }
- String chosen = FileDialogs.getNewFile(null, initial, Collections.emptyList(),
- JFileChooser.OPEN_DIALOG, false);
- if (chosen != null) {
- File newFile = new File(chosen);
- // Store the directory for next time:
- Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, newFile.getParent());
- comp.setText(newFile.getPath());
- comp.requestFocus();
+ Optional path = new NewFileDialogs(fParent, appDir).openDlgAndGetSelectedFile();
+ path.ifPresent(applicationDir -> {
+ if (applicationDir.getParent() != null) {
+ Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, applicationDir.getParent().toString());
}
- }
- }
+ application.setText(applicationDir.toString());
+ });
+ };
+
}
diff --git a/src/main/java/net/sf/jabref/external/MoveFileAction.java b/src/main/java/net/sf/jabref/external/MoveFileAction.java
index b4bc662daa2..1a2fb893875 100644
--- a/src/main/java/net/sf/jabref/external/MoveFileAction.java
+++ b/src/main/java/net/sf/jabref/external/MoveFileAction.java
@@ -18,18 +18,19 @@
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
-import java.util.Collections;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
+import java.util.Optional;
import javax.swing.AbstractAction;
-import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import net.sf.jabref.Globals;
-import net.sf.jabref.gui.FileDialogs;
import net.sf.jabref.gui.FileListEntry;
import net.sf.jabref.gui.JabRefFrame;
+import net.sf.jabref.gui.NewFileDialogs;
import net.sf.jabref.gui.entryeditor.EntryEditor;
import net.sf.jabref.gui.fieldeditors.FileListEditor;
import net.sf.jabref.gui.util.component.CheckBoxMessage;
@@ -44,6 +45,7 @@
* Action for moving or renaming a file that is linked to from an entry in JabRef.
*/
public class MoveFileAction extends AbstractAction {
+
private static final Log LOGGER = LogFactory.getLog(MoveFileAction.class);
private final JabRefFrame frame;
@@ -54,6 +56,7 @@ public class MoveFileAction extends AbstractAction {
private static final String MOVE_RENAME = Localization.lang("Move/Rename file");
+
public MoveFileAction(JabRefFrame frame, EntryEditor eEditor, FileListEditor editor, boolean toFileDir) {
this.frame = frame;
this.eEditor = eEditor;
@@ -89,7 +92,8 @@ public void actionPerformed(ActionEvent event) {
}
}
if (found < 0) {
- JOptionPane.showMessageDialog(frame, Localization.lang("File_directory_is_not_set_or_does_not_exist!"), MOVE_RENAME, JOptionPane.ERROR_MESSAGE);
+ JOptionPane.showMessageDialog(frame, Localization.lang("File_directory_is_not_set_or_does_not_exist!"),
+ MOVE_RENAME, JOptionPane.ERROR_MESSAGE);
return;
}
File file = new File(ln);
@@ -123,8 +127,7 @@ public void actionPerformed(ActionEvent event) {
answer = JOptionPane.showConfirmDialog(frame, Localization.lang("Move file to file directory?"),
MOVE_RENAME, JOptionPane.YES_NO_OPTION);
} else {
- answer = JOptionPane.showConfirmDialog(frame, cbm, MOVE_RENAME,
- JOptionPane.YES_NO_OPTION);
+ answer = JOptionPane.showConfirmDialog(frame, cbm, MOVE_RENAME, JOptionPane.YES_NO_OPTION);
}
if (answer != JOptionPane.YES_OPTION) {
return;
@@ -143,23 +146,15 @@ public void actionPerformed(ActionEvent event) {
}
chosenFile = sb.toString();
} else {
- chosenFile = FileDialogs.getNewFile(frame, file, Collections.singletonList(extension),
- JFileChooser.SAVE_DIALOG, false);
- }
- if (chosenFile == null) {
- return; // canceled
- }
- newFile = new File(chosenFile);
- // Check if the file already exists:
- if (newFile.exists() && (JOptionPane.showConfirmDialog(frame,
- Localization.lang("'%0' exists. Overwrite file?", newFile.getName()), MOVE_RENAME,
- JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION)) {
- if (toFileDir) {
- return;
+ Optional path = new NewFileDialogs(frame, file.getPath()).saveNewFile();
+ if (path.isPresent()) {
+ chosenFile = path.get().toString();
} else {
- repeat = true;
+ return;
}
}
+ newFile = new File(chosenFile);
+
}
if (!newFile.equals(file)) {
@@ -170,29 +165,29 @@ public void actionPerformed(ActionEvent event) {
}
if (success) {
// Remove the original file:
- if (!file.delete()) {
- LOGGER.info("Cannot delete original file");
- }
+ Files.deleteIfExists(file.toPath());
+
// Relativise path, if possible.
String canPath = new File(dirs.get(found)).getCanonicalPath();
if (newFile.getCanonicalPath().startsWith(canPath)) {
- if ((newFile.getCanonicalPath().length() > canPath.length()) &&
- (newFile.getCanonicalPath().charAt(canPath.length()) == File.separatorChar)) {
+ if ((newFile.getCanonicalPath().length() > canPath.length())
+ && (newFile.getCanonicalPath().charAt(canPath.length()) == File.separatorChar)) {
String newLink = newFile.getCanonicalPath().substring(1 + canPath.length());
- editor.getTableModel().setEntry(selected, new FileListEntry(entry.description, newLink, entry.type));
+ editor.getTableModel().setEntry(selected,
+ new FileListEntry(entry.description, newLink, entry.type));
} else {
String newLink = newFile.getCanonicalPath().substring(canPath.length());
- editor.getTableModel().setEntry(selected, new FileListEntry(entry.description, newLink, entry.type));
+ editor.getTableModel().setEntry(selected,
+ new FileListEntry(entry.description, newLink, entry.type));
}
} else {
String newLink = newFile.getCanonicalPath();
- editor.getTableModel().setEntry(selected, new FileListEntry(entry.description, newLink, entry.type));
+ editor.getTableModel().setEntry(selected,
+ new FileListEntry(entry.description, newLink, entry.type));
}
eEditor.updateField(editor);
- //JOptionPane.showMessageDialog(frame, Globals.lang("File moved"),
- // Globals.lang("Move/Rename file"), JOptionPane.INFORMATION_MESSAGE);
frame.output(Localization.lang("File moved"));
} else {
JOptionPane.showMessageDialog(frame, Localization.lang("Move file failed"), MOVE_RENAME,
diff --git a/src/main/java/net/sf/jabref/gui/BasePanel.java b/src/main/java/net/sf/jabref/gui/BasePanel.java
index 16adce2909f..aa18e8ffa25 100644
--- a/src/main/java/net/sf/jabref/gui/BasePanel.java
+++ b/src/main/java/net/sf/jabref/gui/BasePanel.java
@@ -29,6 +29,7 @@
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -44,7 +45,6 @@
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
-import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
@@ -223,6 +223,7 @@ public class BasePanel extends JPanel implements ClipboardOwner, FileUpdateListe
private final SearchBar searchBar;
private ContentAutoCompleters autoCompleters;
+
public BasePanel(JabRefFrame frame, BibDatabaseContext bibDatabaseContext) {
Objects.requireNonNull(frame);
Objects.requireNonNull(bibDatabaseContext);
@@ -447,7 +448,7 @@ public void run() {
// First check if any entries have keys set already. If so, possibly remove
// them from consideration, or warn about overwriting keys.
// This is a partial clone of net.sf.jabref.gui.entryeditor.EntryEditor.GenerateKeyAction.actionPerformed(ActionEvent)
- for (final Iterator i = entries.iterator(); i.hasNext(); ) {
+ for (final Iterator i = entries.iterator(); i.hasNext();) {
bes = i.next();
if (bes.hasCiteKey()) {
if (Globals.prefs.getBoolean(JabRefPreferences.AVOID_OVERWRITING_KEY)) {
@@ -688,9 +689,10 @@ public void update() {
new SpecialFieldAction(frame, Quality.getInstance(),
Quality.getInstance().getValues().get(0).getFieldValue().get(), true,
Localization.lang("Toggle quality assured")));
- actions.put(Printed.getInstance().getValues().get(0).getActionName(), new SpecialFieldAction(frame,
- Printed.getInstance(), Printed.getInstance().getValues().get(0).getFieldValue().get(), true,
- Localization.lang("Toggle print status")));
+ actions.put(Printed.getInstance().getValues().get(0).getActionName(),
+ new SpecialFieldAction(frame, Printed.getInstance(),
+ Printed.getInstance().getValues().get(0).getFieldValue().get(), true,
+ Localization.lang("Toggle print status")));
for (SpecialFieldValue prio : Priority.getInstance().getValues()) {
actions.put(prio.getActionName(), prio.getAction(this.frame));
@@ -1039,7 +1041,8 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset enc,
try {
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs).withEncoding(enc)
.withSaveType(saveType);
- BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(FileSaveSession::new);
+ BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter<>(
+ FileSaveSession::new);
if (selectedOnly) {
session = databaseWriter.savePartOfDatabase(bibDatabaseContext, mainTable.getSelectedEntries(), prefs);
} else {
@@ -1195,6 +1198,7 @@ public SearchBar getSearchBar() {
private class GroupTreeListener {
private final Runnable task = new Runnable() {
+
@Override
public void run() {
// Update group display (for example to reflect that the number of contained entries has changed)
@@ -1209,12 +1213,14 @@ public void run() {
* Guarded by "task"
*/
private TimerTask timerTask = new TimerTask() {
+
@Override
public void run() {
task.run();
}
};
+
@Subscribe
public void listen(EntryAddedEvent addedEntryEvent) {
// if the added entry is an undo don't add it to the current group
@@ -1246,6 +1252,7 @@ private void scheduleUpdate() {
synchronized (task) {
timerTask.cancel();
timerTask = new TimerTask() {
+
@Override
public void run() {
task.run();
@@ -1404,7 +1411,8 @@ public void actionPerformed(ActionEvent e) {
public void keyPressed(KeyEvent e) {
final int keyCode = e.getKeyCode();
final TreePath path = frame.getGroupSelector().getSelectionPath();
- final GroupTreeNodeViewModel node = path == null ? null : (GroupTreeNodeViewModel) path.getLastPathComponent();
+ final GroupTreeNodeViewModel node = path == null ? null : (GroupTreeNodeViewModel) path
+ .getLastPathComponent();
if (e.isControlDown()) {
switch (keyCode) {
@@ -1464,7 +1472,8 @@ public void setupMainPanel() {
adjustSplitter(); // restore last splitting state (before mainTable is created as creation affects the stored size of the entryEditors)
// check whether a mainTable already existed and a floatSearch was active
- boolean floatSearchActive = (mainTable != null) && (this.tableModel.getSearchState() == MainTableDataModel.DisplayOption.FLOAT);
+ boolean floatSearchActive = (mainTable != null)
+ && (this.tableModel.getSearchState() == MainTableDataModel.DisplayOption.FLOAT);
createMainTable();
@@ -1743,8 +1752,8 @@ public void entryEditorClosing(EntryEditor editor) {
* Closes the entry editor or preview panel if it is showing the given entry.
*/
public void ensureNotShowingBottomPanel(BibEntry entry) {
- if (((mode == BasePanelMode.SHOWING_EDITOR) && (currentEditor.getEntry() == entry)) ||
- ((mode == BasePanelMode.SHOWING_PREVIEW) && (currentPreview.getEntry() == entry))) {
+ if (((mode == BasePanelMode.SHOWING_EDITOR) && (currentEditor.getEntry() == entry))
+ || ((mode == BasePanelMode.SHOWING_PREVIEW) && (currentPreview.getEntry() == entry))) {
hideBottomComponent();
}
}
@@ -2307,19 +2316,13 @@ public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) {
@Override
public void action() throws SaveException {
+ Optional chosenFile = new NewFileDialogs(frame).withExtension(FileExtensions.BIBTEX_DB).saveNewFile();
- String chosenFile = FileDialogs.getNewFile(frame,
- new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Collections.singletonList(".bib"),
- JFileChooser.SAVE_DIALOG, false);
- if (chosenFile != null) {
- File expFile = new File(chosenFile);
- if (!expFile.exists() || (JOptionPane.showConfirmDialog(frame,
- Localization.lang("'%0' exists. Overwrite file?", expFile.getName()),
- Localization.lang("Save database"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)) {
- saveDatabase(expFile, true, Globals.prefs.getDefaultEncoding(), saveType);
- frame.getFileHistory().newFile(expFile.getPath());
- frame.output(Localization.lang("Saved selected to '%0'.", expFile.getPath()));
- }
+ if (chosenFile.isPresent()) {
+ Path path = chosenFile.get();
+ saveDatabase(path.toFile(), true, Globals.prefs.getDefaultEncoding(), saveType);
+ frame.getFileHistory().newFile(path.toString());
+ frame.output(Localization.lang("Saved selected to '%0'.", path.toString()));
}
}
}
@@ -2329,6 +2332,7 @@ private static class SearchAndOpenFile {
private final BibEntry entry;
private final BasePanel basePanel;
+
public SearchAndOpenFile(final BibEntry entry, final BasePanel basePanel) {
this.entry = entry;
this.basePanel = basePanel;
@@ -2394,6 +2398,7 @@ public Optional searchAndOpen() {
}
}
+
/**
* Set the preview active state for all BasePanel instances.
*
diff --git a/src/main/java/net/sf/jabref/gui/EntryCustomizationDialog.java b/src/main/java/net/sf/jabref/gui/EntryCustomizationDialog.java
index 3deb0b374f2..9c24e3b197d 100644
--- a/src/main/java/net/sf/jabref/gui/EntryCustomizationDialog.java
+++ b/src/main/java/net/sf/jabref/gui/EntryCustomizationDialog.java
@@ -324,7 +324,7 @@ private void applyChanges() {
private void typeDeletion(String name) {
Optional type = EntryTypes.getType(name, bibDatabaseContext.getMode());
- if (type.isPresent() && type.get() instanceof CustomEntryType) {
+ if (type.isPresent() && (type.get() instanceof CustomEntryType)) {
if (! EntryTypes.getStandardType(name, bibDatabaseContext.getMode()).isPresent()) {
int reply = JOptionPane.showConfirmDialog
(frame, Localization.lang("All entries of this "
@@ -378,7 +378,6 @@ public void actionPerformed(ActionEvent e) {
} else if (e.getSource() == apply) {
applyChanges();
} else if (e.getSource() == typeComp) {
- //System.out.println("add: "+e.getActionCommand());
typeComp.selectField(e.getActionCommand());
}
}
diff --git a/src/main/java/net/sf/jabref/gui/FileDialogs.java b/src/main/java/net/sf/jabref/gui/FileDialogs.java
deleted file mode 100644
index 0a050e0b890..00000000000
--- a/src/main/java/net/sf/jabref/gui/FileDialogs.java
+++ /dev/null
@@ -1,209 +0,0 @@
-/* Copyright (C) 2003-2015 JabRef contributors.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
-package net.sf.jabref.gui;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
-
-import javax.swing.JComponent;
-import javax.swing.JFileChooser;
-import javax.swing.JFrame;
-
-import net.sf.jabref.Globals;
-import net.sf.jabref.logic.util.OS;
-import net.sf.jabref.preferences.JabRefPreferences;
-
-public class FileDialogs {
-
- /**
- * Will return the names of multiple files selected in the given directory
- * and the given extensions.
- *
- * Will return an empty String array if no entry is found.
- *
- * @param owner
- * @param directory
- * @param extensions
- * @param updateWorkingdirectory
- * @return an array of selected file paths, or an empty array if no selection is made.
- */
- public static List getMultipleFiles(JFrame owner, File directory, List extensions,
- boolean updateWorkingdirectory) {
-
- Objects.requireNonNull(extensions);
-
- OpenFileFilter off;
- if (extensions.isEmpty()) {
- off = new OpenFileFilter();
- } else {
- off = new OpenFileFilter(extensions);
- }
-
- Object files = FileDialogs.getNewFileImpl(owner, directory, extensions, null, off, JFileChooser.OPEN_DIALOG,
- updateWorkingdirectory, false, true, null);
-
- if (files instanceof String[]) {
- return Arrays.asList((String[]) files);
- }
- // Fix for:
- // http://sourceforge.net/tracker/index.php?func=detail&aid=1538769&group_id=92314&atid=600306
- if (files != null) {
- return Collections.singletonList((String) files);
- }
- return Collections.emptyList();
- }
-
- public static String getNewFile(JFrame owner, File directory, List extensions, int dialogType, boolean updateWorkingDirectory) {
- return FileDialogs.getNewFile(owner, directory, extensions, null, dialogType, updateWorkingDirectory, false, null);
- }
-
- public static String getNewFile(JFrame owner, File directory, List extensions, int dialogType, boolean updateWorkingDirectory, JComponent accessory) {
- return FileDialogs.getNewFile(owner, directory, extensions, null, dialogType, updateWorkingDirectory, false, accessory);
- }
-
- public static String getNewFile(JFrame owner, File directory, List extensions, String description, int dialogType, boolean updateWorkingDirectory) {
- return FileDialogs.getNewFile(owner, directory, extensions, description, dialogType, updateWorkingDirectory, false, null);
- }
-
- public static String getNewDir(JFrame owner, File directory, List extensions, int dialogType, boolean updateWorkingDirectory) {
- return FileDialogs.getNewFile(owner, directory, extensions, null, dialogType, updateWorkingDirectory, true, null);
- }
-
- public static String getNewDir(JFrame owner, File directory, List extensions, String description, int dialogType, boolean updateWorkingDirectory) {
- return FileDialogs.getNewFile(owner, directory, extensions, description, dialogType, updateWorkingDirectory, true, null);
- }
-
- private static String getNewFile(JFrame owner, File directory, List extensions, String description, int dialogType, boolean updateWorkingDirectory, boolean dirOnly, JComponent accessory) {
-
- OpenFileFilter off;
-
- if (extensions.isEmpty()) {
- off = new OpenFileFilter();
- } else {
- off = new OpenFileFilter(extensions);
- }
-
- return (String) FileDialogs.getNewFileImpl(owner, directory, extensions, description, off, dialogType, updateWorkingDirectory, dirOnly, false, accessory);
- }
-
- private static Object getNewFileImpl(JFrame owner, File directory, List extensions, String description,
- OpenFileFilter off, int dialogType, boolean updateWorkingDirectory, boolean dirOnly,
- boolean multipleSelection, JComponent accessory) {
-
- // Added the !dirOnly condition below as a workaround to the native file dialog
- // not supporting directory selection:
- if (!dirOnly && OS.OS_X) {
- return FileDialogs.getNewFileForMac(owner, directory, dialogType, updateWorkingDirectory);
- }
-
- JFileChooser fc;
- try {
- fc = new JFileChooser(directory);//JabRefFileChooser(directory);
- if (accessory != null) {
- fc.setAccessory(accessory);
- }
- } catch (InternalError errl) {
- // This try/catch clause was added because a user reported an
- // InternalError getting thrown on WinNT, presumably because of a
- // bug in JGoodies Windows PLAF. This clause can be removed if the
- // bug is fixed, but for now we just resort to the native file
- // dialog, using the same method as is always used on Mac:
- return FileDialogs.getNewFileForMac(owner, directory, dialogType, updateWorkingDirectory);
- }
-
- if (dirOnly) {
- fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- }
-
- fc.setMultiSelectionEnabled(multipleSelection);
-
- fc.addChoosableFileFilter(off);
- fc.setDialogType(dialogType);
- int dialogResult;
- if (dialogType == JFileChooser.OPEN_DIALOG) {
- dialogResult = fc.showOpenDialog(owner);
- } else if (dialogType == JFileChooser.SAVE_DIALOG) {
- dialogResult = fc.showSaveDialog(owner);
- } else {
- dialogResult = fc.showDialog(owner, description);
- }
-
- // the getSelectedFile method returns a valid fileselection
- // (if something is selected) indepentently from dialog return status
- if (dialogResult != JFileChooser.APPROVE_OPTION) {
- return null;
- }
-
- // okay button
- File selectedFile = fc.getSelectedFile();
- if (selectedFile == null) { // cancel
- return null;
- }
-
- // If this is a save dialog, and the user has not chosen "All files" as
- // filter
- // we enforce the given extension. But only if extension is not null.
- if ((!extensions.isEmpty()) && (dialogType == JFileChooser.SAVE_DIALOG) && (fc.getFileFilter() == off) && !off.accept(selectedFile)) {
-
- // add the first extension if there are multiple extensions
- selectedFile = new File(selectedFile.getPath() + extensions.get(0));
- }
-
- if (updateWorkingDirectory) {
- Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, selectedFile.getPath());
- }
-
- if (multipleSelection) {
- File[] files = fc.getSelectedFiles();
- String[] filenames = new String[files.length];
- for (int i = 0; i < files.length; i++) {
- filenames[i] = files[i].getAbsolutePath();
- }
- return filenames;
- } else {
- return selectedFile.getAbsolutePath();
- }
- }
-
- private static String getNewFileForMac(JFrame owner, File directory, int dialogType,
- boolean updateWorkingDirectory) {
-
- java.awt.FileDialog fc = new java.awt.FileDialog(owner);
-
- if (directory != null) {
- fc.setDirectory(directory.getParent());
- }
- if (dialogType == JFileChooser.OPEN_DIALOG) {
- fc.setMode(java.awt.FileDialog.LOAD);
- } else {
- fc.setMode(java.awt.FileDialog.SAVE);
- }
-
- fc.setVisible(true);
-
- if (fc.getFile() == null) {
- return null;
- } else {
- if (updateWorkingDirectory) {
- Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, fc.getDirectory() + fc.getFile());
- }
- return fc.getDirectory() + fc.getFile();
- }
- }
-}
diff --git a/src/main/java/net/sf/jabref/gui/FileExtensions.java b/src/main/java/net/sf/jabref/gui/FileExtensions.java
new file mode 100644
index 00000000000..0e1e49c15d8
--- /dev/null
+++ b/src/main/java/net/sf/jabref/gui/FileExtensions.java
@@ -0,0 +1,68 @@
+package net.sf.jabref.gui;
+
+import net.sf.jabref.logic.l10n.Localization;
+
+/**
+ *
+ *
+ *This enum contains all kind of file extensions for open and save dialogs
+ *Important: Enter the extension without a dot!
+ *The dot is added implicit
+ */
+public enum FileExtensions {
+
+ //important: No dot before the extension!
+ BIBTEX_DB(
+ String.format("%1s %2s", "BibTex", Localization.lang("Database")), "bib"),
+ BIBTEXML(
+ Localization.lang("%0 file", "BibTeXML"), "bibx"),
+ AUX(
+ Localization.lang("%0 file", "AUX"), "aux"),
+ SILVER_PLATTERN(
+ Localization.lang("%0 file", "SilverPlatter"), "dat"),
+ PUBMED(
+ Localization.lang("%0 file", "PubMed"), "fcgi"),
+ RIS(
+ Localization.lang("%0 file", "RIS"), "ris"),
+ ENDNOTE(
+ Localization.lang("%0 file", "Endnote/Refer"), "ref"),
+ JSTYLE(
+ Localization.lang("Style file"), "jstyle"),
+ LAYOUT(
+ Localization.lang("Custom layout file"), "layout"),
+ XML(
+ Localization.lang("%0 file", "XML"), "xml"),
+ TERMS(
+ Localization.lang("Protected terms file"), "terms"),
+ TXT(
+ Localization.lang("%0 file", Localization.lang("plain text")), "txt"),
+ CLASS(
+ Localization.lang("%0 file", "CLASS"), "class"),
+ JAR(
+ Localization.lang("%0 file", "JAR"), "jar"),
+ ZIP(
+ Localization.lang("%0 file", "ZIP"), "zip");
+
+ private final String[] extension;
+ private final String description;
+
+
+ private FileExtensions(String description, String... extension) {
+ this.extension = extension;
+ this.description = description;
+ }
+
+ //Array because a) is varags and b) gets passed as varags parameter to FileExtensionNameFilter
+ public String[] getExtensions() {
+ return extension;
+ }
+
+ public String getDescription() {
+
+ return description;
+ }
+
+ public String getFirstExtensionWithDot() {
+ return "." + extension[0].trim();
+ }
+}
diff --git a/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java b/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java
index 7d6ebfb45d5..b158594c965 100644
--- a/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java
+++ b/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java
@@ -22,8 +22,8 @@
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.Collection;
-import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
@@ -37,13 +37,10 @@
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
-import javax.swing.JFileChooser;
-import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
-import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@@ -89,19 +86,24 @@ public class FileListEntryEditor {
private ConfirmCloseFileListEntryEditor externalConfirm;
private FileListEntry entry;
- private final BibDatabaseContext databaseContext;
+ //Do not make this variable final, as then the lambda action listener will fail on compiöe
+ private BibDatabaseContext databaseContext;
private boolean okPressed;
private boolean okDisabledExternally;
private boolean openBrowseWhenShown;
private boolean dontOpenBrowseUntilDisposed;
+ //Do not make this variable final, as then the lambda action listener will fail on compile
+ private JabRefFrame frame;
+
private static final Pattern REMOTE_LINK_PATTERN = Pattern.compile("[a-z]+://.*");
- public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar,
- boolean showOpenButton, BibDatabaseContext databaseContext) {
+ public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar, boolean showOpenButton,
+ BibDatabaseContext databaseContext) {
this.entry = entry;
this.databaseContext = databaseContext;
+ this.frame = frame;
ActionListener okAction = e -> {
// If OK button is disabled, ignore this event:
@@ -127,27 +129,28 @@ public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showP
}
});
- FormBuilder builder = FormBuilder.create().layout(new FormLayout
- ("left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p"));
+ FormBuilder builder = FormBuilder.create().layout(new FormLayout(
+ "left:pref, 4dlu, fill:150dlu, 4dlu, fill:pref, 4dlu, fill:pref", "p, 2dlu, p, 2dlu, p"));
builder.add(Localization.lang("Link")).xy(1, 1);
builder.add(link).xy(3, 1);
- final BrowseListener browse = new BrowseListener(frame, link);
+ //final BrowseListener browse = new BrowseListener(frame, link); //TODO: Maybe use browse action
+
final JButton browseBut = new JButton(Localization.lang("Browse"));
- browseBut.addActionListener(browse);
+ browseBut.addActionListener(browsePressed);
builder.add(browseBut).xy(5, 1);
JButton open = new JButton(Localization.lang("Open"));
if (showOpenButton) {
builder.add(open).xy(7, 1);
}
builder.add(Localization.lang("Description")).xy(1, 3);
- builder.add(description).xyw(3,3,3);
+ builder.add(description).xyw(3, 3, 3);
builder.getPanel().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
builder.add(Localization.lang("File type")).xy(1, 5);
builder.add(types).xyw(3, 5, 3);
if (showProgressBar) {
builder.appendRows("2dlu, p");
- builder.add(downloadLabel).xy(1,7);
- builder.add(prog).xyw(3,7,3);
+ builder.add(downloadLabel).xy(1, 7);
+ builder.add(prog).xyw(3, 7, 3);
}
ButtonBarBuilder bb = new ButtonBarBuilder();
@@ -167,6 +170,7 @@ public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showP
open.addActionListener(e -> openFile());
AbstractAction cancelAction = new AbstractAction() {
+
@Override
public void actionPerformed(ActionEvent e) {
diag.dispose();
@@ -210,7 +214,7 @@ public void changedUpdate(DocumentEvent documentEvent) {
public void windowActivated(WindowEvent event) {
if (openBrowseWhenShown && !dontOpenBrowseUntilDisposed) {
dontOpenBrowseUntilDisposed = true;
- SwingUtilities.invokeLater(() -> browse.actionPerformed(new ActionEvent(browseBut, 0, "")));
+ // SwingUtilities.invokeLater(() -> browse.actionPerformed(new ActionEvent(browseBut, 0, "")));
}
}
@@ -288,8 +292,7 @@ public boolean isVisible() {
private void setValues(FileListEntry entry) {
description.setText(entry.description);
link.setText(entry.link);
- //if (link.getText().length() > 0)
- // checkExtension();
+
Collection list = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
types.setModel(new DefaultComboBoxModel<>(list.toArray(new ExternalFileType[list.size()])));
@@ -345,41 +348,31 @@ public boolean okPressed() {
}
- class BrowseListener implements ActionListener {
- private final JFrame parent;
- private final JTextField comp;
-
- public BrowseListener(JFrame parent, JTextField comp) {
- this.parent = parent;
- this.comp = comp;
+ private final ActionListener browsePressed = e -> {
+ String filePath = link.getText().trim();
+ Optional file = FileUtil.expandFilename(this.databaseContext, filePath);
+ String workingDir;
+ // no file set yet or found
+ if (file.isPresent()) {
+ workingDir = file.get().getPath();
+ } else {
+ workingDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY);
}
- @Override
- public void actionPerformed(ActionEvent e) {
- String filePath = comp.getText().trim();
- Optional file = FileUtil.expandFilename(databaseContext, filePath);
- File workingDir;
- // no file set yet or found
- if (file.isPresent()) {
- workingDir = new File(file.get().getParent());
- } else {
- workingDir = new File(Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY));
- }
- String selection = FileDialogs.getNewFile(parent, workingDir, Collections.emptyList(),
- JFileChooser.OPEN_DIALOG, false);
- if (selection != null) {
- File newFile = new File(selection);
- // Store the directory for next time:
- Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, newFile.getParent());
-
- // If the file is below the file directory, make the path relative:
- List fileDirs = databaseContext.getFileDirectory();
- newFile = FileUtil.shortenFileName(newFile, fileDirs);
-
- comp.setText(newFile.getPath());
- comp.requestFocus();
- }
- }
- }
+ Optional path = new NewFileDialogs(this.frame, workingDir).openDlgAndGetSelectedFile();
+
+ path.ifPresent(selection -> {
+ File newFile = selection.toFile();
+ // Store the directory for next time:
+ Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, newFile.getPath());
+
+ // If the file is below the file directory, make the path relative:
+ List fileDirs = this.databaseContext.getFileDirectory();
+ newFile = FileUtil.shortenFileName(newFile, fileDirs);
+
+ link.setText(newFile.getPath());
+ link.requestFocus();
+ });
+ };
}
diff --git a/src/main/java/net/sf/jabref/gui/NewFileDialogs.java b/src/main/java/net/sf/jabref/gui/NewFileDialogs.java
new file mode 100644
index 00000000000..18d2b98d22b
--- /dev/null
+++ b/src/main/java/net/sf/jabref/gui/NewFileDialogs.java
@@ -0,0 +1,184 @@
+package net.sf.jabref.gui;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import javax.swing.JFileChooser;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+import javax.swing.filechooser.FileNameExtensionFilter;
+
+import net.sf.jabref.Globals;
+import net.sf.jabref.logic.l10n.Localization;
+import net.sf.jabref.preferences.JabRefPreferences;
+
+public class NewFileDialogs {
+
+ /**
+ * Custom confirmation dialog
+ * http://stackoverflow.com/a/3729157
+ */
+ private final JFileChooser fileChooser = new JFileChooser() {
+
+ @Override
+ public void approveSelection() {
+ File file = getSelectedFile();
+ if (file.exists() && (getDialogType() == SAVE_DIALOG)) {
+ int result = JOptionPane.showConfirmDialog(this,
+ Localization.lang("'%0' exists. Overwrite file?", file.getName()),
+ Localization.lang("Existing file"), JOptionPane.YES_NO_CANCEL_OPTION);
+ switch (result) {
+ case JOptionPane.YES_OPTION:
+ super.approveSelection();
+ return;
+ case JOptionPane.NO_OPTION:
+ return;
+ case JOptionPane.CLOSED_OPTION:
+ return;
+ case JOptionPane.CANCEL_OPTION:
+ cancelSelection();
+ return;
+ default:
+ return;
+ }
+ }
+ super.approveSelection();
+ }
+ };
+
+ private final JFrame parent;
+ private final String directory;
+ private FileNameExtensionFilter extFilter;
+ private Collection extensions = EnumSet.noneOf(FileExtensions.class);
+
+
+ /**
+ * Creates a new filedialog showing the current working dir {@link JabRefPreferences#WORKING_DIRECTORY}
+ * @param parent The parent frame associated with this dialog
+ */
+ public NewFileDialogs(JFrame parent) {
+ this(parent, getWorkingDir());
+ }
+
+ /**
+ * Creates a new dialog in the given directory
+ * @param parent The parent frame associated with this dialog
+ * @param dir The starting directory to show in the dialog
+ */
+ public NewFileDialogs(JFrame parent, String dir) {
+ Objects.requireNonNull(dir, "Directory must not be null");
+
+ this.parent = parent;
+ this.directory = dir;
+ fileChooser.setCurrentDirectory(Paths.get(dir).toFile());
+ }
+
+ /**
+ * Show only directories instead of files and folders
+ * @return NewFileDialogs
+ */
+ public NewFileDialogs dirsOnly() {
+ fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+ return this;
+ }
+
+ /**
+ * Add a single extension as filter
+ * @param singleExt The extension
+ * @return NewFileDialogs
+ */
+ public NewFileDialogs withExtension(FileExtensions singleExt) {
+ withExtensions(EnumSet.of(singleExt));
+ return this;
+ }
+
+ public NewFileDialogs withExtensions(Collection fileExtensions) {
+ this.extensions = fileExtensions;
+
+ for (FileExtensions ext : fileExtensions) {
+ extFilter = new FileNameExtensionFilter(ext.getDescription(), ext.getExtensions());
+ fileChooser.addChoosableFileFilter(extFilter);
+ }
+
+ return this;
+ }
+
+ /**
+ * Updates the working directory preference
+ * @return NewFileDialogs
+ */
+ public NewFileDialogs updateWorkingDirPref() {
+ Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, this.directory);
+ return this;
+ }
+
+ /**
+ * Shows an {@link JFileChooser#OPEN_DIALOG} and allows to select multiple files
+ * @return List containing the paths of all files or an empty list if dialog is canceled
+ */
+ public List showDlgAndGetMultipleFiles() {
+ fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
+ fileChooser.setMultiSelectionEnabled(true);
+
+ if (showDialogAndIsAccepted()) {
+ List files = Arrays.stream(fileChooser.getSelectedFiles()).map(File::toString)
+ .collect(Collectors.toList());
+
+ return files;
+ }
+
+ return Collections.emptyList();
+
+ }
+
+ /**
+ * 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 openDlgAndGetSelectedFile() {
+ fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
+
+ if (showDialogAndIsAccepted()) {
+ return Optional.of(fileChooser.getSelectedFile().toPath());
+ }
+ return Optional.empty();
+ }
+
+ /**
+ *
+ * Shows an {@link JFileChooser#SAVE_DIALOG} and allows to save a new file
+ * If an extension is provided, adds the extension to the file
+ * Selecting an existing file will show an overwrite dialog
+ * @return The path of the new file, or {@link Optional#empty()} if dialog is aborted
+ */
+ public Optional saveNewFile() {
+ fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+ if (showDialogAndIsAccepted()) {
+ File file = fileChooser.getSelectedFile();
+ if (!extensions.isEmpty() && !fileChooser.accept(file)) {
+
+ return Optional.of(Paths.get(file.getPath() + extensions.iterator().next().getFirstExtensionWithDot()));
+ }
+ return Optional.of(file.toPath());
+ }
+ return Optional.empty();
+ }
+
+ private boolean showDialogAndIsAccepted() {
+ return fileChooser.showDialog(parent, null) == JFileChooser.APPROVE_OPTION;
+ }
+
+ private static String getWorkingDir() {
+ return Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY);
+ }
+
+}
diff --git a/src/main/java/net/sf/jabref/gui/OpenFileFilter.java b/src/main/java/net/sf/jabref/gui/OpenFileFilter.java
deleted file mode 100644
index c5a8fbb9150..00000000000
--- a/src/main/java/net/sf/jabref/gui/OpenFileFilter.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/* Copyright (C) 2003-2015 JabRef contributors.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-package net.sf.jabref.gui;
-
-import java.io.File;
-import java.io.FilenameFilter;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-public class OpenFileFilter extends javax.swing.filechooser.FileFilter implements FilenameFilter {
-
- private final Set extSet = new HashSet<>();
- private final String desc;
-
-
- public OpenFileFilter(List extensions) {
- StringBuilder buf = new StringBuilder();
- int numExt = extensions.size();
-
- if (numExt > 0) {
- buf.append('*');
- buf.append(extensions.get(0));
-
- extSet.add(extensions.get(0));
- }
-
- for (int curExt = 1; curExt < numExt; curExt++) {
- buf.append(", *");
- buf.append(extensions.get(curExt));
-
- extSet.add(extensions.get(curExt));
- }
-
- desc = buf.toString();
- }
-
- public OpenFileFilter() {
- this(Arrays.asList(
- ".bib",
- ".dat", // silverplatter ending
- ".txt", // windows puts ".txt" extensions and for scifinder
- ".ris",
- ".ref", // refer/endnote format
- ".fcgi", // default for pubmed
- ".bibx", // default for BibTeXML
- ".xml"
- ));
- }
-
- @Override
- public boolean accept(File file) {
- if (file.isDirectory()) {
- return true;
- }
-
- return accept(file.getName());
- }
-
- private boolean accept(String filename) {
-
- String lowerCaseFileName = filename.toLowerCase();
- int dotPos = lowerCaseFileName.lastIndexOf('.');
-
- if (dotPos == -1) {
- return false;
- }
-
- int dotDotPos = lowerCaseFileName.lastIndexOf('.', dotPos - 1); // for dot.dot extensions
-
- return extSet.contains(lowerCaseFileName.substring(dotPos)) ||
- ((dotDotPos >= 0) && extSet.contains(lowerCaseFileName.substring(dotDotPos)));
- }
-
- @Override
- public String getDescription() {
- return desc;
- }
-
- @Override
- public boolean accept(File dir, String name) {
- return accept(new File(dir.getPath() + name));
- }
-}
diff --git a/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java b/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java
index 88f5757e662..61ed52997f9 100644
--- a/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java
+++ b/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java
@@ -17,57 +17,60 @@
import java.awt.event.ActionEvent;
import java.io.File;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Collections;
-import java.util.List;
+import java.util.EnumSet;
+import java.util.Set;
import javax.swing.AbstractAction;
-import javax.swing.JComponent;
-import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
-import net.sf.jabref.gui.FileDialogs;
-import net.sf.jabref.gui.util.FocusRequester;
+import net.sf.jabref.gui.FileExtensions;
+import net.sf.jabref.gui.NewFileDialogs;
import net.sf.jabref.logic.l10n.Localization;
-
/**
* Action used to produce a "Browse" button for one of the text fields.
*/
public final class BrowseAction extends AbstractAction {
+
private final JFrame frame;
private final JTextField comp;
- private final boolean dir;
- private final JComponent focusTarget;
- private final List extension;
+ private final boolean dirsOnly;
+ private final Set extensions;
+
public static BrowseAction buildForDir(JFrame frame, JTextField tc) {
- return new BrowseAction(frame, tc, true, null, Collections.emptyList());
+ return new BrowseAction(frame, tc, true, Collections.emptySet());
}
public static BrowseAction buildForDir(JTextField tc) {
- return new BrowseAction(null, tc, true, null, Collections.emptyList());
+ return new BrowseAction(null, tc, true, Collections.emptySet());
}
public static BrowseAction buildForFile(JTextField tc) {
- return new BrowseAction(null, tc, false, null, Collections.emptyList());
+
+ return new BrowseAction(null, tc, false, Collections.emptySet());
}
- public static BrowseAction buildForFile(JTextField tc, JComponent focusTarget, List extension) {
- return new BrowseAction(null, tc, false, focusTarget, extension);
-}
+ public static BrowseAction buildForFile(JTextField tc, FileExtensions extensions) {
- public static BrowseAction buildForDir(JTextField tc, JComponent focusTarget) {
- return new BrowseAction(null, tc, true, focusTarget, null);
+ return new BrowseAction(null, tc, false, EnumSet.of(extensions));
}
- private BrowseAction(JFrame frame, JTextField tc, boolean dir, JComponent focusTarget, List extension) {
+ public static BrowseAction buildForFile(JTextField tc, Set extensions) {
+ return new BrowseAction(null, tc, false, extensions);
+ }
+
+
+ private BrowseAction(JFrame frame, JTextField tc, boolean dirsOnly, Set extensions) {
super(Localization.lang("Browse"));
this.frame = frame;
- this.dir = dir;
+ this.dirsOnly = dirsOnly;
this.comp = tc;
- this.focusTarget = focusTarget;
- this.extension = extension;
+ this.extensions = extensions;
}
@Override
@@ -77,19 +80,23 @@ public void actionPerformed(ActionEvent e) {
if (chosen != null) {
File newFile = new File(chosen);
comp.setText(newFile.getPath());
- if (focusTarget != null) {
- new FocusRequester(focusTarget);
- }
+
}
}
private String askUser() {
- if (dir) {
- return FileDialogs.getNewDir(frame, new File(comp.getText()), extension,
- JFileChooser.OPEN_DIALOG, false);
+ if (dirsOnly) {
+ Path path = new NewFileDialogs(frame, comp.getText()).dirsOnly().withExtensions(extensions)
+ .openDlgAndGetSelectedFile().orElse(Paths.get(""));
+ String file = path.toString();
+
+ return file;
} else {
- return FileDialogs.getNewFile(frame, new File(comp.getText()), extension,
- JFileChooser.OPEN_DIALOG, false);
+ Path path = new NewFileDialogs(frame, comp.getText()).withExtensions(extensions)
+ .openDlgAndGetSelectedFile().orElse(Paths.get(""));
+ String file = path.toString();
+
+ return file;
}
}
}
diff --git a/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java b/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java
index 809fbbf4c3b..a00264be8be 100644
--- a/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java
+++ b/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java
@@ -38,8 +38,6 @@
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
-import java.io.File;
-import java.util.Collections;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
@@ -49,7 +47,6 @@
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
-import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
@@ -60,8 +57,9 @@
import net.sf.jabref.Globals;
import net.sf.jabref.gui.BasePanel;
-import net.sf.jabref.gui.FileDialogs;
+import net.sf.jabref.gui.FileExtensions;
import net.sf.jabref.gui.JabRefFrame;
+import net.sf.jabref.gui.actions.BrowseAction;
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.auxparser.AuxParser;
import net.sf.jabref.logic.auxparser.AuxParserResult;
@@ -73,6 +71,7 @@
import com.jgoodies.forms.layout.FormLayout;
public class FromAuxDialog extends JDialog {
+
private final JPanel statusPanel = new JPanel();
private final JPanel buttons = new JPanel();
private final JButton generateButton = new JButton();
@@ -95,8 +94,7 @@ public class FromAuxDialog extends JDialog {
private final JabRefFrame parentFrame;
- public FromAuxDialog(JabRefFrame frame, String title, boolean modal,
- JTabbedPane viewedDBs) {
+ public FromAuxDialog(JabRefFrame frame, String title, boolean modal, JTabbedPane viewedDBs) {
super(frame, title, modal);
parentTabbedPane = viewedDBs;
@@ -141,9 +139,11 @@ private void jbInit() {
this.setTitle(Localization.lang("AUX file import"));
JLabel desc = new JLabel("" + Localization.lang("AUX file import") + "
"
+ Localization.lang("This feature generates a new database based on which entries "
- + "are needed in an existing LaTeX document.") + "
"
- + "" + Localization.lang("You need to select one of your open databases from which to choose "
- + "entries, as well as the AUX file produced by LaTeX when compiling your document.") + "
");
+ + "are needed in an existing LaTeX document.")
+ + "
" + ""
+ + Localization.lang("You need to select one of your open databases from which to choose "
+ + "entries, as well as the AUX file produced by LaTeX when compiling your document.")
+ + "