cols = mainTable.getSelectedColumns();
if ((cols.length == 1) && (rows.length == 1)) {
// Copy single value.
Object o = mainTable.getValueAt(rows[0], cols[0]);
if (o != null) {
StringSelection ss = new StringSelection(o.toString());
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(ss, BasePanel.this);
output(Globals.lang("Copied cell contents")+".");
}
}
}
}
});
actions.put("cut", new BaseAction() {
public void action() throws Throwable {
runCommand("copy");
BibtexEntry[] bes = mainTable.getSelectedEntries();
//int row0 = mainTable.getSelectedRow();
if ((bes != null) && (bes.length > 0)) {
// Create a CompoundEdit to make the action undoable.
NamedCompound ce = new NamedCompound
(Globals.lang(bes.length > 1 ? "cut entries" : "cut entry"));
// Loop through the array of entries, and delete them.
for (int i=0; i<bes.length; i++) {
database.removeEntry(bes[i].getId());
ensureNotShowing(bes[i]);
ce.addEdit(new UndoableRemoveEntry
(database, bes[i], BasePanel.this));
}
//entryTable.clearSelection();
frame.output(Globals.lang("Cut_pr")+" "+
(bes.length>1 ? bes.length
+" "+ Globals.lang("entries")
: Globals.lang("entry"))+".");
ce.end();
undoManager.addEdit(ce);
markBaseChanged();
// Reselect the entry in the first prev. selected position:
/*if (row0 >= entryTable.getRowCount())
row0 = entryTable.getRowCount()-1;
if (row0 >= 0)
entryTable.addRowSelectionInterval(row0, row0);*/
}
}
});
actions.put("delete", new BaseAction() {
public void action() {
BibtexEntry[] bes = mainTable.getSelectedEntries();
if ((bes != null) && (bes.length > 0)) {
boolean goOn = showDeleteConfirmationDialog(bes.length);
if (!goOn) {
return;
}
else {
// Create a CompoundEdit to make the action undoable.
NamedCompound ce = new NamedCompound
(Globals.lang(bes.length > 1 ? "delete entries" : "delete entry"));
// Loop through the array of entries, and delete them.
for (int i = 0; i < bes.length; i++) {
database.removeEntry(bes[i].getId());
ensureNotShowing(bes[i]);
ce.addEdit(new UndoableRemoveEntry(database, bes[i], BasePanel.this));
}
markBaseChanged();
frame.output(Globals.lang("Deleted") + " " +
(bes.length > 1 ? bes.length
+ " " + Globals.lang("entries")
: Globals.lang("entry")) + ".");
ce.end();
undoManager.addEdit(ce);
//entryTable.clearSelection();
}
// Reselect the entry in the first prev. selected position:
/*if (row0 >= entryTable.getRowCount())
row0 = entryTable.getRowCount()-1;
if (row0 >= 0) {
final int toSel = row0;
//
SwingUtilities.invokeLater(new Runnable() {
public void run() {
entryTable.addRowSelectionInterval(toSel, toSel);
//entryTable.ensureVisible(toSel);
}
});
*/
}
}
});
// The action for pasting entries or cell contents.
// Edited by Seb Wills <saw27@mrao.cam.ac.uk> on 14-Apr-04:
// - more robust detection of available content flavors (doesn't only look at first one offered)
// - support for parsing string-flavor clipboard contents which are bibtex entries.
// This allows you to (a) paste entire bibtex entries from a text editor, web browser, etc
// (b) copy and paste entries between multiple instances of JabRef (since
// only the text representation seems to get as far as the X clipboard, at least on my system)
actions.put("paste", new BaseAction() {
public void action() {
// Get clipboard contents, and see if TransferableBibtexEntry is among the content flavors offered
Transferable content = Toolkit.getDefaultToolkit()
.getSystemClipboard().getContents(null);
if (content != null) {
BibtexEntry[] bes = null;
if (content.isDataFlavorSupported(TransferableBibtexEntry.entryFlavor)) {
// We have determined that the clipboard data is a set of entries.
try {
bes = (BibtexEntry[])(content.getTransferData(TransferableBibtexEntry.entryFlavor));
} catch (UnsupportedFlavorException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
BibtexParser bp = new BibtexParser
(new java.io.StringReader( (String) (content.getTransferData(
DataFlavor.stringFlavor))));
BibtexDatabase db = bp.parse().getDatabase();
Util.pr("Parsed " + db.getEntryCount() + " entries from clipboard text");
if(db.getEntryCount()>0) {
bes = db.getEntries().toArray(new BibtexEntry[db.getEntryCount()]);
}
} catch (UnsupportedFlavorException ex) {
ex.printStackTrace();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
// finally we paste in the entries (if any), which either came from TransferableBibtexEntries
// or were parsed from a string
if ((bes != null) && (bes.length > 0)) {
NamedCompound ce = new NamedCompound
(Globals.lang(bes.length > 1 ? "paste entries" : "paste entry"));
for (int i=0; i<bes.length; i++) {
try {
BibtexEntry be = (BibtexEntry)(bes[i].clone());
Util.setAutomaticFields(be,
Globals.prefs.getBoolean("overwriteOwner"),
Globals.prefs.getBoolean("overwriteTimeStamp"));
// We have to clone the
// entries, since the pasted
// entries must exist
// independently of the copied
// ones.
be.setId(Util.createNeutralId());
database.insertEntry(be);
ce.addEdit(new UndoableInsertEntry
(database, be, BasePanel.this));
} catch (KeyCollisionException ex) {
Util.pr("KeyCollisionException... this shouldn't happen.");
}
}
ce.end();
undoManager.addEdit(ce);
//entryTable.clearSelection();
//entryTable.revalidate();
output(Globals.lang("Pasted")+" "+
(bes.length>1 ? bes.length+" "+
Globals.lang("entries") : "1 "+Globals.lang("entry"))
+".");
markBaseChanged();
}
}
}
});
actions.put("selectAll", new BaseAction() {
public void action() {
mainTable.selectAll();
}
});
// The action for opening the preamble editor
actions.put("editPreamble", new BaseAction() {
public void action() {
if (preambleEditor == null) {
PreambleEditor form = new PreambleEditor
(frame, BasePanel.this, database, Globals.prefs);
Util.placeDialog(form, frame);
form.setVisible(true);
preambleEditor = form;
} else {
preambleEditor.setVisible(true);
}
}
});
// The action for opening the string editor
actions.put("editStrings", new BaseAction() {
public void action() {
if (stringDialog == null) {
StringDialog form = new StringDialog
(frame, BasePanel.this, database, Globals.prefs);
Util.placeDialog(form, frame);
form.setVisible(true);
stringDialog = form;
} else {
stringDialog.setVisible(true);
}
}
});
// The action for toggling the groups interface
actions.put("toggleGroups", new BaseAction() {
public void action() {
sidePaneManager.toggle("groups");
frame.groupToggle.setSelected(sidePaneManager.isComponentVisible("groups"));
}
});
// action for collecting database strings from user
actions.put("dbConnect", new DbConnectAction(this));
// action for exporting database to external SQL database
actions.put("dbExport", new AbstractWorker () {
String errorMessage = null;
boolean connectToDB = false;
// run first, in EDT:
public void init() {
DBStrings dbs = metaData.getDBStrings();
// get DBStrings from user if necessary
if (!dbs.isConfigValid()) {
// init DB strings if necessary
if (! dbs.isInitialized()) {
dbs.initialize();
}
// show connection dialog
DBConnectDialog dbd = new DBConnectDialog(frame(), dbs);
Util.placeDialog(dbd, BasePanel.this );
dbd.setVisible(true);
connectToDB = dbd.getConnectToDB();
// store database strings
if (connectToDB) {
dbs = dbd.getDBStrings();
metaData.setDBStrings(dbs);
dbd.dispose();
}
} else {
connectToDB = true;
}
}
// run second, on a different thread:
public void run() {
if (connectToDB) {
DBStrings dbs = metaData.getDBStrings();
try {
frame.output(Globals.lang("Attempting SQL export..."));
SQLutil.exportDatabase(database, metaData, null, dbs);
dbs.isConfigValid(true);
} catch (Exception ex) {
errorMessage = SQLutil.getExceptionMessage(ex,SQLutil.DBTYPE.MYSQL);
dbs.isConfigValid(false);
}
metaData.setDBStrings(dbs);
}
}
// run third, on EDT:
public void update() {
String url = SQLutil.createJDBCurl(metaData.getDBStrings());
// if no error, report success
if (errorMessage == null) {
if (connectToDB) {
frame.output(Globals.lang("%0 export successful", url));
}
}
// show an error dialog if an error occurred
else {
String preamble = "Could not export to SQL database for the following reason:";
frame.output(Globals.lang(preamble)
+ " " + errorMessage);
JOptionPane.showMessageDialog(frame, Globals.lang(preamble)
+ "\n" + errorMessage, Globals.lang("Export to SQL database"),
JOptionPane.ERROR_MESSAGE);
errorMessage = null;
}
}
});
// The action for auto-generating keys.
actions.put("makeKey", new AbstractWorker() {
//int[] rows;
List<BibtexEntry> entries;
int numSelected;
boolean cancelled = false;
// Run first, in EDT:
public void init() {
entries = new ArrayList<BibtexEntry>(Arrays.asList(getSelectedEntries()));
//rows = entryTable.getSelectedRows() ;
numSelected = entries.size();
if (entries.size() == 0) { // None selected. Inform the user to select entries first.
JOptionPane.showMessageDialog(frame, Globals.lang("First select the entries you want keys to be generated for."),
Globals.lang("Autogenerate BibTeX key"), JOptionPane.INFORMATION_MESSAGE);
return ;
}
frame.block();
output(Globals.lang("Generating BibTeX key for")+" "+
numSelected+" "+(numSelected>1 ? Globals.lang("entries")
: Globals.lang("entry"))+"...");
}
// Run second, on a different thread:
public void run() {
BibtexEntry bes = null ;
NamedCompound ce = new NamedCompound(Globals.lang("autogenerate keys"));
// First check if any entries have keys set already. If so, possibly remove
// them from consideration, or warn about overwriting keys.
loop: for (Iterator<BibtexEntry> i=entries.iterator(); i.hasNext();) {
bes = i.next();
if (bes.getField(BibtexFields.KEY_FIELD) != null) {
if (Globals.prefs.getBoolean("avoidOverwritingKey"))
// Rmove the entry, because its key is already set:
i.remove();
else if (Globals.prefs.getBoolean("warnBeforeOverwritingKey")) {
// Ask if the user wants to cancel the operation:
CheckBoxMessage cbm = new CheckBoxMessage(Globals.lang("One or more keys will be overwritten. Continue?"),
Globals.lang("Disable this confirmation dialog"), false);
int answer = JOptionPane.showConfirmDialog(frame, cbm, Globals.lang("Overwrite keys"),
JOptionPane.YES_NO_OPTION);
if (cbm.isSelected())
Globals.prefs.putBoolean("warnBeforeOverwritingKey", false);
if (answer == JOptionPane.NO_OPTION) {
// Ok, break off the operation.
cancelled = true;
return;
}
// No need to check more entries, because the user has already confirmed
// that it's ok to overwrite keys:
break loop;
}
}
}
HashMap<BibtexEntry, Object> oldvals = new HashMap<BibtexEntry, Object>();
// Iterate again, removing already set keys. This is skipped if overwriting
// is disabled, since all entries with keys set will have been removed.
if (!Globals.prefs.getBoolean("avoidOverwritingKey")) for (Iterator<BibtexEntry> i=entries.iterator(); i.hasNext();) {
bes = i.next();
// Store the old value:
oldvals.put(bes, bes.getField(BibtexFields.KEY_FIELD));
database.setCiteKeyForEntry(bes.getId(), null);
}
// Finally, set the new keys:
for (Iterator<BibtexEntry> i=entries.iterator(); i.hasNext();) {
bes = i.next();
bes = LabelPatternUtil.makeLabel(Globals.prefs.getKeyPattern(), database, bes);
ce.addEdit(new UndoableKeyChange
(database, bes.getId(), (String)oldvals.get(bes),
bes.getField(BibtexFields.KEY_FIELD)));
}
ce.end();
undoManager.addEdit(ce);
}
// Run third, on EDT:
public void update() {
if (cancelled) {
frame.unblock();
return;
}
markBaseChanged() ;
numSelected = entries.size();
////////////////////////////////////////////////////////////////////////////////
// Prevent selection loss for autogenerated BibTeX-Keys
////////////////////////////////////////////////////////////////////////////////
for (Iterator<BibtexEntry> i=entries.iterator(); i.hasNext();) {
final BibtexEntry bibEntry = i.next();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int row = mainTable.findEntry( bibEntry );
if (row >= 0 && mainTable.getSelectedRowCount() < entries.size())
mainTable.addRowSelectionInterval(row, row);
}
});
}
////////////////////////////////////////////////////////////////////////////////
output(Globals.lang("Generated BibTeX key for")+" "+
numSelected+" "+(numSelected!=1 ? Globals.lang("entries")
: Globals.lang("entry")));
frame.unblock();
}
});
actions.put("search", new BaseAction() {
public void action() {
//sidePaneManager.togglePanel("search");
sidePaneManager.show("search");
//boolean on = sidePaneManager.isPanelVisible("search");
frame.searchToggle.setSelected(true);
if (true)
frame.searchManager.startSearch();
}
});
actions.put("toggleSearch", new BaseAction() {
public void action() {
//sidePaneManager.togglePanel("search");
sidePaneManager.toggle("search");
boolean on = sidePaneManager.isComponentVisible("search");
frame.searchToggle.setSelected(on);
if (on)
frame.searchManager.startSearch();
}
});
actions.put("incSearch", new BaseAction() {
public void action() {
sidePaneManager.show("search");
frame.searchToggle.setSelected(true);
frame.searchManager.startIncrementalSearch();
}
});
// The action for copying the selected entry's key.
actions.put("copyKey", new BaseAction() {
public void action() {
BibtexEntry[] bes = mainTable.getSelectedEntries();
if ((bes != null) && (bes.length > 0)) {
storeCurrentEdit();
//String[] keys = new String[bes.length];
Vector<Object> keys = new Vector<Object>();
// Collect all non-null keys.
for (int i=0; i<bes.length; i++)
if (bes[i].getField(BibtexFields.KEY_FIELD) != null)
keys.add(bes[i].getField(BibtexFields.KEY_FIELD));
if (keys.size() == 0) {
output("None of the selected entries have BibTeX keys.");
return;
}
StringBuffer sb = new StringBuffer((String)keys.elementAt(0));
for (int i=1; i<keys.size(); i++) {
sb.append(',');
sb.append((String)keys.elementAt(i));
}
StringSelection ss = new StringSelection(sb.toString());
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(ss, BasePanel.this);
if (keys.size() == bes.length)
// All entries had keys.
output(Globals.lang((bes.length > 1) ? "Copied keys"
: "Copied key")+".");
else
output(Globals.lang("Warning")+": "+(bes.length-keys.size())
+" "+Globals.lang("out of")+" "+bes.length+" "+
Globals.lang("entries have undefined BibTeX key")+".");
}
}
});
// The action for copying a cite for the selected entry.
actions.put("copyCiteKey", new BaseAction() {
public void action() {
BibtexEntry[] bes = mainTable.getSelectedEntries();
if ((bes != null) && (bes.length > 0)) {
storeCurrentEdit();
//String[] keys = new String[bes.length];
Vector<Object> keys = new Vector<Object>();
// Collect all non-null keys.
for (int i=0; i<bes.length; i++)
if (bes[i].getField(BibtexFields.KEY_FIELD) != null)
keys.add(bes[i].getField(BibtexFields.KEY_FIELD));
if (keys.size() == 0) {
output("None of the selected entries have BibTeX keys.");
return;
}
StringBuffer sb = new StringBuffer((String)keys.elementAt(0));
for (int i=1; i<keys.size(); i++) {
sb.append(',');
sb.append((String)keys.elementAt(i));
}
StringSelection ss = new StringSelection
("\\cite{"+sb.toString()+"}");
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(ss, BasePanel.this);
if (keys.size() == bes.length)