From e1eb25c56d5712de4ed1609d7cd8381d8ab17aee Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sun, 1 May 2016 20:14:49 +0200 Subject: [PATCH] WIP: Refactor Open and Save Dialogs NewFileDialogs will be renamed when the work is done Atm I want to keep it parallel during --- .../net/sf/jabref/gui/NewFileDialogs.java | 64 +++++++++++++++++++ .../jabref/importer/AppendDatabaseAction.java | 9 +-- .../sf/jabref/importer/ImportMenuItem.java | 17 ++--- .../jabref/importer/OpenDatabaseAction.java | 50 +++++++++------ 4 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 src/main/java/net/sf/jabref/gui/NewFileDialogs.java 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 000000000000..43110276062f --- /dev/null +++ b/src/main/java/net/sf/jabref/gui/NewFileDialogs.java @@ -0,0 +1,64 @@ +package net.sf.jabref.gui; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import javax.swing.JFileChooser; +import javax.swing.JFrame; + +import net.sf.jabref.Globals; +import net.sf.jabref.JabRefPreferences; + +/** + * WIP: Will replaced the FileDialogs class + * + * + */ +public class NewFileDialogs { + + public static List getMultipleFiles(JFrame owner, String extension, boolean workingDir, + boolean updateWorkingDirSetting) { + + Path dir = Paths.get("."); + if (workingDir) { + dir = Paths.get(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)); + } + return getMultipleFiles(owner, dir, extension, updateWorkingDirSetting); + + } + + public static List getMultipleFiles(JFrame owner, Path directory, String extension, + boolean updateWorkingDirSetting) { + + OpenFileFilter off = null; + if (extension == null) { + off = new OpenFileFilter(); + } else if (!extension.isEmpty()) { + off = new OpenFileFilter(extension); + } + + JFileChooser chooser = new JFileChooser(directory.toFile()); + chooser.setMultiSelectionEnabled(true); + chooser.setFileFilter(off); + if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) { + + if (updateWorkingDirSetting) { + updateWorkingDirectorySetting(chooser.getSelectedFile().toString()); + } + return Arrays.stream(chooser.getSelectedFiles()).map(f -> f.toString()).collect(Collectors.toList()); + } + + return Collections.emptyList(); + + } + + private static void updateWorkingDirectorySetting(String dir) { + Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, dir); + + } + +} diff --git a/src/main/java/net/sf/jabref/importer/AppendDatabaseAction.java b/src/main/java/net/sf/jabref/importer/AppendDatabaseAction.java index a6d7508c9c90..e754d3c6d77e 100644 --- a/src/main/java/net/sf/jabref/importer/AppendDatabaseAction.java +++ b/src/main/java/net/sf/jabref/importer/AppendDatabaseAction.java @@ -28,9 +28,9 @@ import net.sf.jabref.JabRefPreferences; import net.sf.jabref.MetaData; import net.sf.jabref.gui.BasePanel; -import net.sf.jabref.gui.FileDialogs; import net.sf.jabref.gui.JabRefFrame; import net.sf.jabref.gui.MergeDialog; +import net.sf.jabref.gui.NewFileDialogs; import net.sf.jabref.gui.actions.BaseAction; import net.sf.jabref.gui.undo.NamedCompound; import net.sf.jabref.gui.undo.UndoableInsertEntry; @@ -80,11 +80,8 @@ public void action() { md.setLocationRelativeTo(panel); md.setVisible(true); if (md.isOkPressed()) { - List chosen = FileDialogs.getMultipleFiles(frame, - new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), - null, false); - //String chosenFile = Globals.getNewFile(frame, new File(Globals.prefs.get("workingDirectory")), - // null, JFileChooser.OPEN_DIALOG, false); + + List chosen = NewFileDialogs.getMultipleFiles(frame, null, true, false); if (chosen.isEmpty()) { return; } diff --git a/src/main/java/net/sf/jabref/importer/ImportMenuItem.java b/src/main/java/net/sf/jabref/importer/ImportMenuItem.java index 0b6e036f79fa..d59631c66220 100644 --- a/src/main/java/net/sf/jabref/importer/ImportMenuItem.java +++ b/src/main/java/net/sf/jabref/importer/ImportMenuItem.java @@ -17,7 +17,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -32,9 +31,9 @@ import net.sf.jabref.MetaData; import net.sf.jabref.gui.BasePanel; import net.sf.jabref.gui.EntryMarker; -import net.sf.jabref.gui.FileDialogs; import net.sf.jabref.gui.ImportInspectionDialog; import net.sf.jabref.gui.JabRefFrame; +import net.sf.jabref.gui.NewFileDialogs; import net.sf.jabref.gui.ParserResultWarningDialog; import net.sf.jabref.gui.undo.NamedCompound; import net.sf.jabref.gui.worker.AbstractWorker; @@ -103,9 +102,11 @@ class MyWorker extends AbstractWorker { @Override public void init() { importError = null; - filenames = FileDialogs.getMultipleFiles(frame, - new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), - importer == null ? null : importer.getExtensions(), true); + + filenames = NewFileDialogs.getMultipleFiles(frame, importer == null ? null : importer.getExtensions(), true, + true); + + System.out.println((filenames)); if (!filenames.isEmpty()) { frame.block(); @@ -114,10 +115,10 @@ public void init() { Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, filenames.get(0)); } - } + } - @Override - public void run() { + @Override + public void run() { if (!fileOk) { return; } diff --git a/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java b/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java index 0eda6fd7275c..4706c12abc64 100644 --- a/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java +++ b/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java @@ -41,9 +41,9 @@ import net.sf.jabref.exporter.AutoSaveManager; import net.sf.jabref.exporter.SaveSession; import net.sf.jabref.gui.BasePanel; -import net.sf.jabref.gui.FileDialogs; import net.sf.jabref.gui.IconTheme; import net.sf.jabref.gui.JabRefFrame; +import net.sf.jabref.gui.NewFileDialogs; import net.sf.jabref.gui.ParserResultWarningDialog; import net.sf.jabref.gui.actions.MnemonicAwareAction; import net.sf.jabref.gui.keyboard.KeyBinding; @@ -85,6 +85,7 @@ public class OpenDatabaseAction extends MnemonicAwareAction { POST_OPEN_ACTIONS.add(new HandleDuplicateWarnings()); } + public OpenDatabaseAction(JabRefFrame frame, boolean showDialog) { super(IconTheme.JabRefIcon.OPEN.getIcon()); this.frame = frame; @@ -99,12 +100,12 @@ public void actionPerformed(ActionEvent e) { List filesToOpen = new ArrayList<>(); if (showDialog) { - List chosenStrings = FileDialogs.getMultipleFiles(frame, - new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), ".bib", true); + List chosenStrings = NewFileDialogs.getMultipleFiles(frame, ".bib", true, false); + for (String chosen : chosenStrings) { - if (chosen != null) { - filesToOpen.add(new File(chosen)); - } + + filesToOpen.add(new File(chosen)); + } } else { LOGGER.info(Action.NAME + " " + e.getActionCommand()); @@ -114,11 +115,13 @@ public void actionPerformed(ActionEvent e) { openFiles(filesToOpen, true); } + class OpenItSwingHelper implements Runnable { private final BasePanel basePanel; private final boolean raisePanel; + OpenItSwingHelper(BasePanel basePanel, boolean raisePanel) { this.basePanel = basePanel; this.raisePanel = raisePanel; @@ -131,6 +134,7 @@ public void run() { } } + /** * Opens the given file. If null or 404, nothing happens * @@ -161,11 +165,12 @@ public void openFiles(List filesToOpen, boolean raisePanel) { int removed = 0; // Check if any of the files are already open: - for (Iterator iterator = filesToOpen.iterator(); iterator.hasNext(); ) { + for (Iterator iterator = filesToOpen.iterator(); iterator.hasNext();) { File file = iterator.next(); for (int i = 0; i < frame.getTabbedPane().getTabCount(); i++) { BasePanel basePanel = frame.getBasePanelAt(i); - if ((basePanel.getBibDatabaseContext().getDatabaseFile() != null) && basePanel.getBibDatabaseContext().getDatabaseFile().equals(file)) { + if ((basePanel.getBibDatabaseContext().getDatabaseFile() != null) + && basePanel.getBibDatabaseContext().getDatabaseFile().equals(file)) { iterator.remove(); removed++; // See if we removed the final one. If so, we must perhaps @@ -195,7 +200,8 @@ public void openFiles(List filesToOpen, boolean raisePanel) { // If no files are remaining to open, this could mean that a file was // already open. If so, we may have to raise the correct tab: else if (toRaise != null) { - frame.output(Localization.lang("File '%0' is already open.", toRaise.getBibDatabaseContext().getDatabaseFile().getPath())); + frame.output(Localization.lang("File '%0' is already open.", + toRaise.getBibDatabaseContext().getDatabaseFile().getPath())); frame.getTabbedPane().setSelectedComponent(toRaise); } @@ -219,12 +225,17 @@ private void openTheFile(File file, boolean raisePanel) { } else if (autoSaveFound) { // We have found a newer autosave, but we are not allowed to use it without // prompting. - int answer = JOptionPane.showConfirmDialog(null, - "" + Localization - .lang("An autosave file was found for this database. This could indicate " - + "that JabRef didn't shut down cleanly last time the file was used.") - + "
" + Localization.lang("Do you want to recover the database from the autosave file?") - + "", Localization.lang("Recover from autosave"), JOptionPane.YES_NO_OPTION); + int answer = JOptionPane + .showConfirmDialog( + null, "" + + Localization + .lang("An autosave file was found for this database. This could indicate " + + "that JabRef didn't shut down cleanly last time the file was used.") + + "
" + + Localization + .lang("Do you want to recover the database from the autosave file?") + + "", + Localization.lang("Recover from autosave"), JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION) { fileToLoad = AutoSaveManager.getAutoSaveFile(file); tryingAutosave = true; @@ -341,11 +352,14 @@ public BasePanel addNewDatabase(ParserResult result, final File file, boolean ra MetaData meta = result.getMetaData(); if (result.hasWarnings()) { - JabRefExecutorService.INSTANCE.execute(() -> ParserResultWarningDialog.showParserResultWarningDialog(result, frame)); + JabRefExecutorService.INSTANCE + .execute(() -> ParserResultWarningDialog.showParserResultWarningDialog(result, frame)); } - Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE))); - BasePanel basePanel = new BasePanel(frame, new BibDatabaseContext(database, meta, file, defaults), result.getEncoding()); + Defaults defaults = new Defaults( + BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE))); + BasePanel basePanel = new BasePanel(frame, new BibDatabaseContext(database, meta, file, defaults), + result.getEncoding()); // file is set to null inside the EventDispatcherThread SwingUtilities.invokeLater(new OpenItSwingHelper(basePanel, raisePanel));