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
Reworked BrowseAction, added Enum for file extensions

Use a more factory like pattern to create fileDialogs
Avoids unnecessary constructor overloads

Reworked FileExtensions to accept mutiple Extensions for one description
Added OverwriteExistingFile in JFileChooser

Refactored rest of the dialogs
Removed obsolte keys

Remove comment
  • Loading branch information
Siedlerchr committed Aug 6, 2016
1 parent 381b569 commit 39dbf77
Show file tree
Hide file tree
Showing 41 changed files with 611 additions and 826 deletions.
11 changes: 9 additions & 2 deletions src/main/java/net/sf/jabref/external/ExternalFileTypeEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,7 @@
* Editor for external file types.
*/
public class ExternalFileTypeEditor extends JDialog {

private JFrame frame;
private JDialog dialog;
private List<ExternalFileType> fileTypes;
Expand Down Expand Up @@ -111,6 +113,7 @@ private void init() {
dispose();
});
Action cancelAction = new AbstractAction() {

@Override
public void actionPerformed(ActionEvent e) {
dispose();
Expand All @@ -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()) {
Expand Down Expand Up @@ -246,6 +250,7 @@ public static AbstractAction getAction(JDialog dialog) {
return new EditExternalFileTypesAction(dialog);
}


class EditListener implements ActionListener {

@Override
Expand Down Expand Up @@ -276,6 +281,7 @@ public Component getTableCellRendererComponent(JTable tab, Object value, boolean
}

private class FileTypeTableModel extends AbstractTableModel {

@Override
public int getColumnCount() {
return 5;
Expand Down Expand Up @@ -354,6 +360,7 @@ public void mouseReleased(MouseEvent e) {
}

public static class EditExternalFileTypesAction extends MnemonicAwareAction {

private JabRefFrame frame;
private JDialog dialog;
private ExternalFileTypeEditor editor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

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 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;
Expand All @@ -35,9 +32,10 @@
import javax.swing.event.DocumentEvent;
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -128,6 +126,7 @@ private void init(ExternalFileType inEntry) {

ok.addActionListener(e -> {
okPressed = true;

storeSettings(ExternalFileTypeEntryEditor.this.entry);
diag.dispose();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -248,31 +246,19 @@ public boolean okPressed() {
}


static class BrowseListener implements ActionListener {

private final JTextField comp;
ActionListener browsePressed = e -> {
String appDir = application.getText().trim();
if (appDir.isEmpty()) {
appDir = Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY);
}

Path applicationDir = new NewFileDialogs(fParent, appDir).getSelectedFile();

public BrowseListener(JTextField comp) {
this.comp = comp;
if (applicationDir.getParent() != null) {
Globals.prefs.put(JabRefPreferences.FILE_WORKING_DIRECTORY, applicationDir.getParent().toString());
}
application.setText(applicationDir.toString());

};

@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();
}
}
}
}
52 changes: 23 additions & 29 deletions src/main/java/net/sf/jabref/external/MoveFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.nio.file.Files;
import java.util.List;
import java.util.Locale;

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;
Expand All @@ -44,6 +43,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;
Expand All @@ -54,6 +54,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;
Expand Down Expand Up @@ -89,7 +90,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);
Expand Down Expand Up @@ -123,8 +125,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;
Expand All @@ -143,23 +144,16 @@ public void actionPerformed(ActionEvent event) {
}
chosenFile = sb.toString();
} else {
chosenFile = FileDialogs.getNewFile(frame, file, Collections.singletonList(extension),
JFileChooser.SAVE_DIALOG, false);
System.out.println("Entry extension " + extension);
chosenFile = new NewFileDialogs(frame, file.getPath()).saveNewFile().toString();


}
if (chosenFile == null) {
if (chosenFile.isEmpty()) {
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;
} else {
repeat = true;
}
}

}

if (!newFile.equals(file)) {
Expand All @@ -170,29 +164,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,
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2366,21 +2366,26 @@ public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) {
@Override
public void action() throws SaveException {

String chosenFile = FileDialogs.getNewFile(frame,
new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Collections.singletonList(".bib"),
JFileChooser.SAVE_DIALOG, false);
if (chosenFile != null) {
// String chosenFile = FileDialogs.getNewFile(frame,
// new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Collections.singletonList(".bib"),
// JFileChooser.SAVE_DIALOG, false);

Path chosenFile = new NewFileDialogs(frame).withExtension(FileExtensions.BIBTEX_DB).saveNewFile();
File expFile = chosenFile.toFile();
/* 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()));
}
}
}
}




private static class SearchAndOpenFile {

Expand Down
Loading

0 comments on commit 39dbf77

Please sign in to comment.