Skip to content

Commit

Permalink
WIP: Refactor Open and Save Dialogs
Browse files Browse the repository at this point in the history
NewFileDialogs will be renamed when the work is done
Atm I want to keep it parallel during
  • Loading branch information
Siedlerchr committed May 1, 2016
1 parent 71eca69 commit e1eb25c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 32 deletions.
64 changes: 64 additions & 0 deletions src/main/java/net/sf/jabref/gui/NewFileDialogs.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,11 +80,8 @@ public void action() {
md.setLocationRelativeTo(panel);
md.setVisible(true);
if (md.isOkPressed()) {
List<String> 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<String> chosen = NewFileDialogs.getMultipleFiles(frame, null, true, false);
if (chosen.isEmpty()) {
return;
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/net/sf/jabref/importer/ImportMenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
50 changes: 32 additions & 18 deletions src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -99,12 +100,12 @@ public void actionPerformed(ActionEvent e) {
List<File> filesToOpen = new ArrayList<>();

if (showDialog) {
List<String> chosenStrings = FileDialogs.getMultipleFiles(frame,
new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), ".bib", true);
List<String> 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());
Expand All @@ -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;
Expand All @@ -131,6 +134,7 @@ public void run() {
}
}


/**
* Opens the given file. If null or 404, nothing happens
*
Expand Down Expand Up @@ -161,11 +165,12 @@ public void openFiles(List<File> filesToOpen, boolean raisePanel) {
int removed = 0;

// Check if any of the files are already open:
for (Iterator<File> iterator = filesToOpen.iterator(); iterator.hasNext(); ) {
for (Iterator<File> 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
Expand Down Expand Up @@ -195,7 +200,8 @@ public void openFiles(List<File> 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);
}

Expand All @@ -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,
"<html>" + 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.")
+ "<br>" + Localization.lang("Do you want to recover the database from the autosave file?")
+ "</html>", Localization.lang("Recover from autosave"), JOptionPane.YES_NO_OPTION);
int answer = JOptionPane
.showConfirmDialog(
null, "<html>"
+ 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.")
+ "<br>"
+ Localization
.lang("Do you want to recover the database from the autosave file?")
+ "</html>",
Localization.lang("Recover from autosave"), JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION) {
fileToLoad = AutoSaveManager.getAutoSaveFile(file);
tryingAutosave = true;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit e1eb25c

Please sign in to comment.