/*
* If this panel belongs in an entry editor, note which entry is
* currently shown:
*/
final BibtexEntry targetEntry;
if (entryEditor != null)
targetEntry = entryEditor.getEntry();
else
targetEntry = entry;
(new Thread() {
public String getPlannedFileName(String res) {
String suffix = off.getSuffix(res);
if (suffix == null)
suffix = "." + fieldName.toLowerCase();
String plannedName = null;
if (getKey() != null)
plannedName = getKey() + suffix;
else {
plannedName = JOptionPane.showInputDialog(parent, Globals
.lang("BibTeX key not set. Enter a name for the downloaded file"));
if (plannedName != null && !off.accept(plannedName))
plannedName += suffix;
}
/*
* [ 1548875 ] download pdf produces unsupported filename
*
* http://sourceforge.net/tracker/index.php?func=detail&aid=1548875&group_id=92314&atid=600306
*
*/
if (Globals.ON_WIN) {
plannedName = plannedName.replaceAll(
"\\?|\\*|\\<|\\>|\\||\\\"|\\:|\\.$|\\[|\\]", "");
} else if (Globals.ON_MAC) {
plannedName = plannedName.replaceAll(":", "");
}
return plannedName;
}
public void run() {
String originalText = fieldEditor.getText();
fieldEditor.setEnabled(false);
boolean updateEditor = true;
try {
fieldEditor.setText(Globals.lang("Downloading..."));
output(Globals.lang("Downloading..."));
String plannedName = getPlannedFileName(res);
// Find the default directory for this field type:
String directory = metaData.getFileDirectory(fieldName);
if (!new File(directory).exists()) {
JOptionPane.showMessageDialog(parent, Globals.lang(
"Could not find directory for %0-files: %1", fieldName, directory),
Globals.lang("Download file"), JOptionPane.ERROR_MESSAGE);
Globals.logger(Globals.lang("Could not find directory for %0-files: %1",
fieldName, directory));
return;
}
File file = new File(new File(directory), plannedName);
URL url = new URL(res);
URLDownload udl = new URLDownload(parent, url, file);
try {
udl.download();
} catch (IOException e2) {
JOptionPane.showMessageDialog(parent, Globals.lang("Invalid URL")+": "
+ e2.getMessage(), Globals.lang("Download file"),
JOptionPane.ERROR_MESSAGE);
Globals.logger("Error while downloading " + url.toString());
return;
}
output(Globals.lang("Download completed"));
String textToSet = file.getPath();
if (textToSet.startsWith(directory)) {
// Construct path relative to pdf base dir
textToSet = textToSet.substring(directory.length(), textToSet.length());
// Remove leading path separator
if (textToSet.startsWith(File.separator)) {
textToSet = textToSet.substring(File.separator.length());
}
}
/*
* Check if we should update the editor text field, or
* update the target entry directly:
*/
if (entryEditor == null || entryEditor.getEntry() != targetEntry) {
/*
* Editor has probably changed to show a different
* entry. So we must update the target entry directly
* and not set the text of the editor.
*/
targetEntry.setField(fieldName, textToSet);
fieldEditor.setText(textToSet);
fieldEditor.setEnabled(true);
updateEditor = false;
} else {
/*