public void run() {
BibtexEntry[] bes = mainTable.getSelectedEntries();
String field = "ps";
if ((bes != null) && (bes.length == 1)) {
FileListEntry entry = null;
FileListTableModel tm = new FileListTableModel();
tm.setContent(bes[0].getField("file"));
for (int i=0; i< tm.getRowCount(); i++) {
FileListEntry flEntry = tm.getEntry(i);
if (flEntry.getType().getName().toLowerCase().equals("pdf")
|| flEntry.getType().getName().toLowerCase().equals("ps")) {
entry = flEntry;
break;
}
}
if (entry != null) {
try {
Util.openExternalFileAnyFormat(metaData, entry.getLink(), entry.getType());
output(Globals.lang("External viewer called") + ".");
} catch (IOException e) {
output(Globals.lang("Could not open link"));
e.printStackTrace();
}
return;
}
// If we didn't find anything in the "file" field, check "ps" and "pdf" fields:
Object link = bes[0].getField("ps");
if (bes[0].getField("pdf") != null) {
link = bes[0].getField("pdf");
field = "pdf";
}
String filepath = null;
if (link != null) {
filepath = link.toString();
} else {
if (Globals.prefs.getBoolean("runAutomaticFileSearch")) {
/* The search can lead to an unexpected 100% CPU usage which is perceived
as a bug, if the search incidentally starts at a directory with lots
of stuff below. It is now disabled by default. */
// see if we can fall back to a filename based on the bibtex key
final Collection<BibtexEntry> entries = new ArrayList<BibtexEntry>();
entries.add(bes[0]);
ExternalFileType[] types = Globals.prefs.getExternalFileTypeSelection();
ArrayList<File> dirs = new ArrayList<File>();
if (metaData.getFileDirectory(GUIGlobals.FILE_FIELD) != null)
dirs.add(new File(metaData.getFileDirectory(GUIGlobals.FILE_FIELD)));
Collection<String> extensions = new ArrayList<String>();
for (int i = 0; i < types.length; i++) {
final ExternalFileType type = types[i];
extensions.add(type.getExtension());
}
// Run the search operation:
Map<BibtexEntry, java.util.List<File>> result;
if (Globals.prefs.getBoolean(JabRefPreferences.USE_REG_EXP_SEARCH_KEY)) {
String regExp = Globals.prefs.get(JabRefPreferences.REG_EXP_SEARCH_EXPRESSION_KEY);
result = RegExpFileSearch.findFilesForSet(entries, extensions, dirs, regExp);
}
else
result = Util.findAssociatedFiles(entries, extensions, dirs);
if (result.get(bes[0]) != null) {
List<File> res = result.get(bes[0]);
if (res.size() > 0) {
filepath = res.get(0).getPath();
int index = filepath.lastIndexOf('.');
if ((index >= 0) && (index < filepath.length()-1)) {
String extension = filepath.substring(index+1);
ExternalFileType type = Globals.prefs.getExternalFileTypeByExt(extension);
if (type != null) {
try {
Util.openExternalFileAnyFormat(metaData, filepath, type);
output(Globals.lang("External viewer called") + ".");
return;
} catch (IOException ex) {
output(Globals.lang("Error") + ": " + ex.getMessage());
}
}
}
// TODO: add code for opening the file
}
}
/*String basefile;
Object key = bes[0].getField(BibtexFields.KEY_FIELD);
if (key != null) {
basefile = key.toString();
final ExternalFileType[] types = Globals.prefs.getExternalFileTypeSelection();
final String sep = System.getProperty("file.separator");
String dir = metaData.getFileDirectory(GUIGlobals.FILE_FIELD);
if ((dir != null) && (dir.length() > 0)) {
if (dir.endsWith(sep)) {
dir = dir.substring(0, dir.length() - sep.length());
}
for (int i = 0; i < types.length; i++) {
String found = Util.findPdf(basefile, types[i].getExtension(),
dir, new OpenFileFilter("." + types[i].getExtension()));
if (found != null) {
filepath = dir + sep + found;
break;
}
}
}
}*/
}
}
if (filepath != null) {
//output(Globals.lang("Calling external viewer..."));
try {
Util.openExternalViewer(metaData(), filepath, field);
output(Globals.lang("External viewer called") + ".");
}
catch (IOException ex) {
output(Globals.lang("Error") + ": " + ex.getMessage());
}
} else
output(Globals.lang(
"No pdf or ps defined, and no file matching Bibtex key found") +
".");
} else
output(Globals.lang("No entries or multiple entries selected."));
}
}).start();
}
});
actions.put("openExternalFile", new BaseAction() {
public void action() {
(new Thread() {
public void run() {
BibtexEntry[] bes = mainTable.getSelectedEntries();
String field = GUIGlobals.FILE_FIELD;
if ((bes != null) && (bes.length == 1)) {
Object link = bes[0].getField(field);
if (link == null) {
runCommand("openFile"); // Fall back on PDF/PS fields???
return;
}
FileListTableModel tableModel = new FileListTableModel();
tableModel.setContent((String)link);
if (tableModel.getRowCount() == 0) {
runCommand("openFile"); // Fall back on PDF/PS fields???
return;
}
FileListEntry flEntry = tableModel.getEntry(0);
ExternalFileMenuItem item = new ExternalFileMenuItem
(frame(), bes[0], "",
flEntry.getLink(), flEntry.getType().getIcon(),
metaData(), flEntry.getType());
item.openLink();
} else
output(Globals.lang("No entries or multiple entries selected."));
}
}).start();