Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Open and Save Dialogs #1336

Merged
merged 8 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
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,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;
Expand All @@ -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;
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;


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> 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());
});
};

}
55 changes: 25 additions & 30 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,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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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> path = new NewFileDialogs(frame, file.getPath()).saveNewFile();
if (path.isPresent()) {
chosenFile = path.get().toString();
} else {
repeat = true;
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now files are always overwritten?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, the overwrite dialog is implemented as part of the JFileChooser (overriden method), so it needs not to be handled everywhere

}
}
newFile = new File(chosenFile);

}

if (!newFile.equals(file)) {
Expand All @@ -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,
Expand Down
Loading