use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.
the class BasePanel method paste.
private void paste() {
Collection<BibEntry> bes = new ClipBoardManager().extractBibEntriesFromClipboard();
// or were parsed from a string
if (!bes.isEmpty()) {
NamedCompound ce = new NamedCompound((bes.size() > 1 ? Localization.lang("paste entries") : Localization.lang("paste entry")));
// Store the first inserted bibtexentry.
// bes[0] does not work as bes[0] is first clonded,
// then inserted.
// This entry is used to open up an entry editor
// for the first inserted entry.
BibEntry firstBE = null;
for (BibEntry be1 : bes) {
BibEntry be = (BibEntry) be1.clone();
if (firstBE == null) {
firstBE = be;
}
UpdateField.setAutomaticFields(be, Globals.prefs.getUpdateFieldPreferences());
// We have to clone the
// entries, since the pasted
// entries must exist
// independently of the copied
// ones.
bibDatabaseContext.getDatabase().insertEntry(be);
ce.addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
}
ce.end();
getUndoManager().addEdit(ce);
output(formatOutputMessage(Localization.lang("Pasted"), bes.size()));
markBaseChanged();
highlightEntry(firstBE);
mainTable.requestFocus();
if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) {
selectionListener.editSignalled(firstBE);
}
}
}
use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.
the class BasePanel method newEntry.
/**
* This method is called from JabRefFrame when the user wants to create a new entry. If the argument is null, the
* user is prompted for an entry type.
*
* @param type The type of the entry to create.
* @return The newly created BibEntry or null the operation was canceled by the user.
*/
public BibEntry newEntry(EntryType type) {
EntryType actualType = type;
if (actualType == null) {
// Find out what type is wanted.
final EntryTypeDialog etd = new EntryTypeDialog(frame);
// We want to center the dialog, to make it look nicer.
etd.setLocationRelativeTo(frame);
etd.setVisible(true);
actualType = etd.getChoice();
}
if (actualType != null) {
// Only if the dialog was not canceled.
final BibEntry be = new BibEntry(actualType.getName());
try {
bibDatabaseContext.getDatabase().insertEntry(be);
// Set owner/timestamp if options are enabled:
List<BibEntry> list = new ArrayList<>();
list.add(be);
UpdateField.setAutomaticFields(list, true, true, Globals.prefs.getUpdateFieldPreferences());
// Create an UndoableInsertEntry object.
getUndoManager().addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));
output(Localization.lang("Added new '%0' entry.", actualType.getName().toLowerCase(Locale.ROOT)));
// and adjustment of the splitter.
if (mode != BasePanelMode.SHOWING_EDITOR) {
mode = BasePanelMode.WILL_SHOW_EDITOR;
}
highlightEntry(be);
// The database just changed.
markBaseChanged();
final EntryEditor entryEditor = getEntryEditor(be);
this.showEntryEditor(entryEditor);
entryEditor.requestFocus();
return be;
} catch (KeyCollisionException ex) {
LOGGER.info(ex.getMessage(), ex);
}
}
return null;
}
use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.
the class EntryAddChange method makeChange.
@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
diskEntry.setId(IdGenerator.next());
panel.getDatabase().insertEntry(diskEntry);
secondary.insertEntry(diskEntry);
undoEdit.addEdit(new UndoableInsertEntry(panel.getDatabase(), diskEntry, panel));
return true;
}
use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.
the class DroppedFileHandler method tryXmpImport.
// Done by MrDlib
private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCompound edits) {
if (!"pdf".equals(fileType.getExtension())) {
return false;
}
List<BibEntry> xmpEntriesInFile;
try {
xmpEntriesInFile = XMPUtil.readXMP(fileName, Globals.prefs.getXMPPreferences());
} catch (IOException e) {
LOGGER.warn("Problem reading XMP", e);
return false;
}
if ((xmpEntriesInFile == null) || xmpEntriesInFile.isEmpty()) {
return false;
}
JLabel confirmationMessage = new JLabel(Localization.lang("The PDF contains one or several BibTeX-records.") + "\n" + Localization.lang("Do you want to import these as new entries into the current library?"));
JPanel entriesPanel = new JPanel();
entriesPanel.setLayout(new BoxLayout(entriesPanel, BoxLayout.Y_AXIS));
xmpEntriesInFile.forEach(entry -> {
JTextArea entryArea = new JTextArea(entry.toString());
entryArea.setEditable(false);
entriesPanel.add(entryArea);
});
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.add(confirmationMessage, BorderLayout.NORTH);
contentPanel.add(entriesPanel, BorderLayout.CENTER);
int reply = JOptionPane.showConfirmDialog(frame, contentPanel, Localization.lang("XMP-metadata found in PDF: %0", fileName), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (reply == JOptionPane.CANCEL_OPTION) {
// The user canceled thus that we are done.
return true;
}
if (reply == JOptionPane.NO_OPTION) {
return false;
}
// reply == JOptionPane.YES_OPTION)
/*
* TODO Extract Import functionality from ImportMenuItem then we could
* do:
*
* ImportMenuItem importer = new ImportMenuItem(frame, (mainTable ==
* null), new PdfXmpImporter());
*
* importer.automatedImport(new String[] { fileName });
*/
boolean isSingle = xmpEntriesInFile.size() == 1;
BibEntry single = isSingle ? xmpEntriesInFile.get(0) : null;
boolean success = true;
String destFilename;
if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(Paths.get(fileName), panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences())).toString();
} else {
if (renameCheckBox.isSelected() || (single == null)) {
destFilename = fileName;
} else {
destFilename = single.getCiteKey() + "." + fileType.getExtension();
}
if (copyRadioButton.isSelected()) {
success = doCopy(fileName, destFilename, edits);
} else if (moveRadioButton.isSelected()) {
success = doMove(fileName, destFilename, edits);
}
}
if (success) {
for (BibEntry aXmpEntriesInFile : xmpEntriesInFile) {
aXmpEntriesInFile.setId(IdGenerator.next());
edits.addEdit(new UndoableInsertEntry(panel.getDatabase(), aXmpEntriesInFile, panel));
panel.getDatabase().insertEntry(aXmpEntriesInFile);
doLink(aXmpEntriesInFile, fileType, destFilename, true, edits);
}
panel.markBaseChanged();
panel.updateEntryEditorIfShowing();
}
return true;
}
use of org.jabref.gui.undo.UndoableInsertEntry in project jabref by JabRef.
the class AppendDatabaseAction method mergeFromBibtex.
private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {
BibDatabase fromDatabase = parserResult.getDatabase();
List<BibEntry> appendedEntries = new ArrayList<>();
List<BibEntry> originalEntries = new ArrayList<>();
BibDatabase database = panel.getDatabase();
NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
MetaData meta = parserResult.getMetaData();
if (importEntries) {
// Add entries
boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP);
for (BibEntry originalEntry : fromDatabase.getEntries()) {
BibEntry entry = (BibEntry) originalEntry.clone();
UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, Globals.prefs.getUpdateFieldPreferences());
database.insertEntry(entry);
appendedEntries.add(entry);
originalEntries.add(originalEntry);
ce.addEdit(new UndoableInsertEntry(database, entry, panel));
}
}
if (importStrings) {
for (BibtexString bs : fromDatabase.getStringValues()) {
if (!database.hasStringLabel(bs.getName())) {
database.addString(bs);
ce.addEdit(new UndoableInsertString(panel, database, bs));
}
}
}
if (importGroups) {
meta.getGroups().ifPresent(newGroups -> {
if (newGroups.getGroup() instanceof AllEntriesGroup) {
try {
ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT, Globals.prefs.getKeywordDelimiter());
newGroups.setGroup(group);
group.add(appendedEntries);
} catch (IllegalArgumentException e) {
LOGGER.error(e);
}
}
addGroups(newGroups, ce);
});
}
if (importSelectorWords) {
for (ContentSelector selector : meta.getContentSelectorList()) {
panel.getBibDatabaseContext().getMetaData().addContentSelector(selector);
}
}
ce.end();
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
}
Aggregations