// TODO: undo is not handled properly here, except for the entries
// added by
// the import inspection dialog.
if (bibtexResult != null) {
if (!openInNew) {
final BasePanel panel = (BasePanel) frame.getTabbedPane().getSelectedComponent();
BibtexDatabase toAddTo = panel.database();
// Use the import inspection dialog if it is enabled in preferences, and
// (there are more than one entry or the inspection dialog is also enabled
// for single entries):
if (Globals.prefs.getBoolean("useImportInspectionDialog") &&
(Globals.prefs.getBoolean("useImportInspectionDialogForSingle")
|| (bibtexResult.getDatabase().getEntryCount() > 1))) {
ImportInspectionDialog diag = new ImportInspectionDialog(frame, panel,
BibtexFields.DEFAULT_INSPECTION_FIELDS,
Globals.lang("Import"), openInNew);
diag.addEntries(bibtexResult.getDatabase().getEntries());
diag.entryListComplete();
Util.placeDialog(diag, frame);
diag.setVisible(true);
diag.toFront();
} else {
boolean generateKeys = Globals.prefs.getBoolean("generateKeysAfterInspection");
NamedCompound ce = new NamedCompound(Globals.lang("Import entries"));
// Check if we should unmark entries before adding the new ones:
if (Globals.prefs.getBoolean("unmarkAllEntriesBeforeImporting"))
for (BibtexEntry entry : toAddTo.getEntries()) {
Util.unmarkEntry(entry, toAddTo, ce);
}
for (BibtexEntry entry : bibtexResult.getDatabase().getEntries()){
try {
// Check if the entry is a duplicate of an existing one:
boolean keepEntry = true;
BibtexEntry duplicate = DuplicateCheck.containsDuplicate(toAddTo, entry);
if (duplicate != null) {
int answer = DuplicateResolverDialog.resolveDuplicateInImport
(frame, duplicate, entry);
// The upper entry is the
if (answer == DuplicateResolverDialog.DO_NOT_IMPORT)
keepEntry = false;
if (answer == DuplicateResolverDialog.IMPORT_AND_DELETE_OLD) {
// Remove the old one and import the new one.
toAddTo.removeEntry(duplicate.getId());
ce.addEdit(new UndoableRemoveEntry(toAddTo, duplicate, panel));
}
}
// Add the entry, if we are supposed to:
if (keepEntry) {
toAddTo.insertEntry(entry);
// Generate key, if we are supposed to:
if (generateKeys) {
LabelPatternUtil.makeLabel(Globals.prefs.getKeyPattern(), toAddTo, entry);
//System.out.println("gen:"+entry.getCiteKey());
}
// Let the autocompleters, if any, harvest words from the entry:
Util.updateCompletersForEntry(panel.getAutoCompleters(), entry);
ce.addEdit(new UndoableInsertEntry(toAddTo, entry, panel));
}
} catch (KeyCollisionException e) {
e.printStackTrace();
}
}
ce.end();
if (ce.hasEdits()) {
panel.undoManager.addEdit(ce);
panel.markBaseChanged();
}
}
}