use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class ChangeScanner method run.
@Override
public void run() {
try {
// Parse the temporary file.
Path tempFile = Globals.getFileUpdateMonitor().getTempFile(panel.fileMonitorHandle());
ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences();
ParserResult result = OpenDatabase.loadDatabase(tempFile.toFile(), importFormatPreferences);
databaseInTemp = result.getDatabase();
metadataInTemp = result.getMetaData();
// Parse the modified file.
result = OpenDatabase.loadDatabase(file, importFormatPreferences);
BibDatabase databaseOnDisk = result.getDatabase();
MetaData metadataOnDisk = result.getMetaData();
// Sort both databases according to a common sort key.
EntryComparator comparator = new EntryComparator(false, true, SORT_BY[2]);
comparator = new EntryComparator(false, true, SORT_BY[1], comparator);
comparator = new EntryComparator(false, true, SORT_BY[0], comparator);
EntrySorter sorterInTemp = databaseInTemp.getSorter(comparator);
comparator = new EntryComparator(false, true, SORT_BY[2]);
comparator = new EntryComparator(false, true, SORT_BY[1], comparator);
comparator = new EntryComparator(false, true, SORT_BY[0], comparator);
EntrySorter sorterOnDisk = databaseOnDisk.getSorter(comparator);
comparator = new EntryComparator(false, true, SORT_BY[2]);
comparator = new EntryComparator(false, true, SORT_BY[1], comparator);
comparator = new EntryComparator(false, true, SORT_BY[0], comparator);
EntrySorter sorterInMem = databaseInMemory.getSorter(comparator);
// Start looking at changes.
scanMetaData(metadataInMemory, metadataInTemp, metadataOnDisk);
scanPreamble(databaseInMemory, databaseInTemp, databaseOnDisk);
scanStrings(databaseInMemory, databaseInTemp, databaseOnDisk);
scanEntries(sorterInMem, sorterInTemp, sorterOnDisk);
scanGroups(metadataInTemp, metadataOnDisk);
} catch (IOException ex) {
LOGGER.warn("Problem running", ex);
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class ArgumentProcessor method automaticallySetFileLinks.
private void automaticallySetFileLinks(List<ParserResult> loaded) {
for (ParserResult parserResult : loaded) {
BibDatabase database = parserResult.getDatabase();
LOGGER.info(Localization.lang("Automatically setting file links"));
AutoSetLinks.autoSetLinks(database.getEntries(), parserResult.getDatabaseContext());
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class DroppedFileHandler method showLinkMoveCopyRenameDialog.
//
// @return true if user pushed "OK", false otherwise
//
private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileType fileType, BibEntry entry, BibDatabase database) {
String dialogTitle = Localization.lang("Link to file %0", linkFileName);
Optional<Path> dir = panel.getBibDatabaseContext().getFirstExistingFileDir(Globals.prefs.getFileDirectoryPreferences());
if (!dir.isPresent()) {
destDirLabel.setText(Localization.lang("File directory is not set or does not exist!"));
copyRadioButton.setEnabled(false);
moveRadioButton.setEnabled(false);
renameToTextBox.setEnabled(false);
renameCheckBox.setEnabled(false);
linkInPlace.setSelected(true);
} else {
destDirLabel.setText(Localization.lang("File directory is '%0':", dir.get().toString()));
copyRadioButton.setEnabled(true);
moveRadioButton.setEnabled(true);
renameToTextBox.setEnabled(true);
renameCheckBox.setEnabled(true);
}
ChangeListener cl = arg0 -> {
renameCheckBox.setEnabled(!linkInPlace.isSelected());
renameToTextBox.setEnabled(!linkInPlace.isSelected());
};
linkInPlace.setText(Localization.lang("Leave file in its current directory"));
copyRadioButton.setText(Localization.lang("Copy file to file directory"));
moveRadioButton.setText(Localization.lang("Move file to file directory"));
renameCheckBox.setText(Localization.lang("Rename file to").concat(": "));
LayoutFormatterPreferences layoutPrefs = Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
// Determine which name to suggest:
String targetName = FileUtil.createFileNameFromPattern(database, entry, Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), layoutPrefs);
String fileDirPattern = Globals.prefs.get(JabRefPreferences.IMPORT_FILEDIRPATTERN);
String targetDirName = "";
if (!fileDirPattern.isEmpty()) {
targetDirName = FileUtil.createFileNameFromPattern(database, entry, fileDirPattern, layoutPrefs);
}
if (targetDirName.isEmpty()) {
renameToTextBox.setText(targetName.concat(".").concat(fileType.getExtension()));
} else {
renameToTextBox.setText(targetDirName.concat("/").concat(targetName.concat(".").concat(fileType.getExtension())));
}
linkInPlace.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_LEAVE));
copyRadioButton.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_COPY));
moveRadioButton.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_MOVE));
renameCheckBox.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_RENAME));
linkInPlace.addChangeListener(cl);
cl.stateChanged(new ChangeEvent(linkInPlace));
try {
Object[] messages = { Localization.lang("How would you like to link to '%0'?", linkFileName), optionsPanel };
int reply = JOptionPane.showConfirmDialog(frame, messages, dialogTitle, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (reply == JOptionPane.OK_OPTION) {
// store user's choice
frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_LEAVE, linkInPlace.isSelected());
frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_COPY, copyRadioButton.isSelected());
frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_MOVE, moveRadioButton.isSelected());
frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_RENAME, renameCheckBox.isSelected());
return true;
} else {
return false;
}
} finally {
linkInPlace.removeChangeListener(cl);
}
}
use of org.jabref.model.database.BibDatabase 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();
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class MrDLibFetcher method performSearch.
@Override
public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
Optional<String> title = entry.getLatexFreeField(FieldName.TITLE);
if (title.isPresent()) {
String response = makeServerRequest(title.get());
MrDLibImporter importer = new MrDLibImporter();
ParserResult parserResult = new ParserResult();
try {
if (importer.isRecognizedFormat(new BufferedReader(new StringReader(response)))) {
parserResult = importer.importDatabase(new BufferedReader(new StringReader(response)));
} else {
// For displaying An ErrorMessage
BibEntry errorBibEntry = new BibEntry();
errorBibEntry.setField("html_representation", Localization.lang("Error_while_fetching_from_%0", "Mr.DLib"));
BibDatabase errorBibDataBase = new BibDatabase();
errorBibDataBase.insertEntry(errorBibEntry);
parserResult = new ParserResult(errorBibDataBase);
}
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw new FetcherException("XML Parser IOException.");
}
return parserResult.getDatabase().getEntries();
} else {
// without a title there is no reason to ask MrDLib
return new ArrayList<>(0);
}
}
Aggregations