Skip to content

Commit

Permalink
Show save file dialog when using get fulltext
Browse files Browse the repository at this point in the history
Show approrpriate dialog title
Fix for #2572
  • Loading branch information
Siedlerchr committed Mar 8, 2017
1 parent 960e2cd commit 49290cd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/jabref/gui/FileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
@Deprecated
public class FileDialog {

private static final Log LOGGER = LogFactory.getLog(FileDialog.class);

private final FileChooser fileChooser;
Expand Down Expand Up @@ -69,9 +70,9 @@ public FileDialog(Component parent, Path dir) {
dir = null;
}
fileChooser = new FileChooser();

configurationBuilder = new FileDialogConfiguration.Builder();
configurationBuilder = configurationBuilder.withInitialDirectory(dir);

}

/**
Expand Down Expand Up @@ -113,6 +114,14 @@ public FileChooser.ExtensionFilter getFileFilter() {
return fileChooser.getSelectedExtensionFilter();
}

/**
* Sets the initial file name, useful for saving dialogs
* @param fileName
*/
public void setInitialFileName(String fileName) {
fileChooser.setInitialFileName(fileName);
}

/**
* Sets a custom file filter.
* Only use when withExtension() does not suffice.
Expand All @@ -122,6 +131,7 @@ public FileChooser.ExtensionFilter getFileFilter() {
public void setFileFilter(FileChooser.ExtensionFilter filter) {
fileChooser.getExtensionFilters().add(filter);
fileChooser.setSelectedExtensionFilter(filter);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void download(URL url, final DownloadCallback callback) throws IOExceptio
final String suggestDir = directory == null ? System.getProperty("user.home") : directory;
File file = new File(new File(suggestDir), suggestedName);
FileListEntry fileListEntry = new FileListEntry("", file.getCanonicalPath(), suggestedType);
editor = new FileListEntryEditor(frame, fileListEntry, true, false, databaseContext);
editor = new FileListEntryEditor(frame, fileListEntry, true, false, databaseContext, true);
editor.getProgressBar().setIndeterminate(true);
editor.setOkEnabled(false);
editor.setExternalConfirm(closeEntry -> {
Expand Down
40 changes: 33 additions & 7 deletions src/main/java/org/jabref/gui/filelist/FileListEntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -59,6 +60,7 @@
* label that can be hidden when the download is complete.
*/
public class FileListEntryEditor {

private static final Log LOGGER = LogFactory.getLog(FileListEntryEditor.class);

private JDialog diag;
Expand All @@ -81,9 +83,16 @@ public class FileListEntryEditor {

//Do not make this variable final, as then the lambda action listener will fail on compile
private JabRefFrame frame;
private boolean showSaveDialog = false;

private static final Pattern REMOTE_LINK_PATTERN = Pattern.compile("[a-z]+://.*");

public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar, boolean showOpenButton,
BibDatabaseContext databaseContext, boolean showSaveDialog) {
this(frame, entry, showProgressBar, showOpenButton, databaseContext);

this.showSaveDialog = showSaveDialog;
}

public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showProgressBar, boolean showOpenButton,
BibDatabaseContext databaseContext) {
Expand Down Expand Up @@ -117,8 +126,7 @@ public FileListEntryEditor(JabRefFrame frame, FileListEntry entry, boolean showP

FormLayout fileDialog = new FormLayout(
"left:pref, 4dlu, fill:400dlu, 4dlu, fill:pref, 4dlu, fill:pref",
"p, 8dlu, p, 8dlu, p"
);
"p, 8dlu, p, 8dlu, p");
FormBuilder builder = FormBuilder.create().layout(fileDialog);
builder.add(Localization.lang("Link")).xy(1, 1);
builder.add(link).xy(3, 1);
Expand Down Expand Up @@ -158,6 +166,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();
Expand Down Expand Up @@ -190,7 +199,7 @@ public void changedUpdate(DocumentEvent documentEvent) {

});

diag = new JDialog(frame, Localization.lang("Save file"), true);
diag = new JDialog(frame, Localization.lang("Select files"), true);
diag.getContentPane().add(builder.getPanel(), BorderLayout.CENTER);
diag.getContentPane().add(bb.getPanel(), BorderLayout.SOUTH);
diag.pack();
Expand Down Expand Up @@ -269,6 +278,13 @@ public void setVisible(boolean visible, boolean openBrowse) {
if (visible) {
okPressed = false;
}
String title;
if (showSaveDialog) {
title = Localization.lang("Save file");
} else {
title = Localization.lang("Select files");
}
diag.setTitle(title);
diag.setVisible(visible);
}

Expand Down Expand Up @@ -335,8 +351,8 @@ public boolean okPressed() {
}

private final ActionListener browsePressed = e -> {
String filePath = link.getText().trim();
Optional<File> file = FileUtil.expandFilename(this.databaseContext, filePath,
String fileText = link.getText().trim();
Optional<File> file = FileUtil.expandFilename(this.databaseContext, fileText,
Globals.prefs.getFileDirectoryPreferences());
String workingDir;
// no file set yet or found
Expand All @@ -346,15 +362,25 @@ public boolean okPressed() {
workingDir = Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY);
}

Optional<Path> path = new FileDialog(this.frame, filePath).saveNewFile();
String fileName = Paths.get(fileText).getFileName().toString();

FileDialog fileDlg = new FileDialog(this.frame, workingDir);
Optional<Path> path;
if (showSaveDialog) {
fileDlg.setInitialFileName(fileName);
path = fileDlg.saveNewFile();
} else {
path = fileDlg.showDialogAndGetSelectedFile();
}

path.ifPresent(selection -> {
File newFile = selection.toFile();
// Store the directory for next time:
Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, newFile.getPath());

// If the file is below the file directory, make the path relative:
List<String> fileDirs = this.databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
List<String> fileDirs = this.databaseContext
.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
newFile = FileUtil.shortenFileName(newFile, fileDirs);

link.setText(newFile.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ public void actionPerformed(ActionEvent actionEvent) {
}
entry = selectionModel.getSelected().get(0);
FileListEntry flEntry = new FileListEntry("", "");
FileListEntryEditor editor = new FileListEntryEditor(frame, flEntry, false, true, bibDatabaseContext);
FileListEntryEditor editor = new FileListEntryEditor(frame, flEntry, false, true, bibDatabaseContext, true);
editor.setVisible(true, true);
if (editor.okPressed()) {
FileListTableModel localModel = new FileListTableModel();
Expand Down

0 comments on commit 49290cd

Please sign in to comment.