use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class AutoLinkFilesAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent event) {
List<BibEntry> entries = JabRefGUI.getMainFrame().getCurrentBasePanel().getSelectedEntries();
if (entries.isEmpty()) {
JabRefGUI.getMainFrame().getCurrentBasePanel().output(Localization.lang("This operation requires one or more entries to be selected."));
return;
}
JDialog diag = new JDialog(JabRefGUI.getMainFrame(), true);
final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));
Runnable runnable = AutoSetLinks.autoSetLinks(entries, nc, null, null, JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext(), e -> {
if (e.getID() > 0) {
if (nc.hasEdits()) {
nc.end();
JabRefGUI.getMainFrame().getCurrentBasePanel().getUndoManager().addEdit(nc);
JabRefGUI.getMainFrame().getCurrentBasePanel().markBaseChanged();
}
JabRefGUI.getMainFrame().output(Localization.lang("Finished automatically setting external links."));
} else {
JabRefGUI.getMainFrame().output(Localization.lang("Finished automatically setting external links.") + " " + Localization.lang("No files found."));
}
}, diag);
JabRefExecutorService.INSTANCE.execute(runnable);
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class SearchFixDuplicateLabels method update.
@Override
public void update() {
List<BibEntry> toGenerateFor = new ArrayList<>();
for (Map.Entry<String, List<BibEntry>> dupeEntry : dupes.entrySet()) {
ResolveDuplicateLabelDialog rdld = new ResolveDuplicateLabelDialog(panel, dupeEntry.getKey(), dupeEntry.getValue());
rdld.show();
if (rdld.isOkPressed()) {
List<JCheckBox> cbs = rdld.getCheckBoxes();
for (int i = 0; i < cbs.size(); i++) {
if (cbs.get(i).isSelected()) {
// The checkbox for entry i has been selected, so we should generate a new key for it:
toGenerateFor.add(dupeEntry.getValue().get(i));
}
}
} else if (rdld.isCancelPressed()) {
break;
}
}
// Do the actual generation:
if (!toGenerateFor.isEmpty()) {
NamedCompound ce = new NamedCompound(Localization.lang("Resolve duplicate BibTeX keys"));
for (BibEntry entry : toGenerateFor) {
String oldKey = entry.getCiteKeyOptional().orElse(null);
BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData().getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences());
ce.addEdit(new UndoableKeyChange(entry, oldKey, entry.getCiteKeyOptional().get()));
}
ce.end();
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
}
panel.output(Localization.lang("Finished resolving duplicate BibTeX keys. %0 entries modified.", String.valueOf(toGenerateFor.size())));
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class MassSetFieldAction method massSetField.
/**
* Set a given field to a given value for all entries in a Collection. This method DOES NOT update any UndoManager,
* but returns a relevant CompoundEdit that should be registered by the caller.
*
* @param entries The entries to set the field for.
* @param field The name of the field to set.
* @param text The value to set. This value can be null, indicating that the field should be cleared.
* @param overwriteValues Indicate whether the value should be set even if an entry already has the field set.
* @return A CompoundEdit for the entire operation.
*/
private static UndoableEdit massSetField(Collection<BibEntry> entries, String field, String text, boolean overwriteValues) {
NamedCompound ce = new NamedCompound(Localization.lang("Set field"));
for (BibEntry entry : entries) {
Optional<String> oldVal = entry.getField(field);
// value already for this entry:
if (!overwriteValues && (oldVal.isPresent()) && !oldVal.get().isEmpty()) {
continue;
}
if (text == null) {
entry.clearField(field);
} else {
entry.setField(field, text);
}
ce.addEdit(new UndoableFieldChange(entry, field, oldVal.orElse(null), text));
}
ce.end();
return ce;
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class EntryEditor method storeSource.
private boolean storeSource() {
BibtexParser bibtexParser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
try {
ParserResult parserResult = bibtexParser.parse(new StringReader(source.getText()));
BibDatabase database = parserResult.getDatabase();
if (database.getEntryCount() > 1) {
throw new IllegalStateException("More than one entry found.");
}
if (!database.hasEntries()) {
if (parserResult.hasWarnings()) {
// put the warning into as exception text -> it will be displayed to the user
throw new IllegalStateException(parserResult.warnings().get(0));
} else {
throw new IllegalStateException("No entries found.");
}
}
NamedCompound compound = new NamedCompound(Localization.lang("source edit"));
BibEntry newEntry = database.getEntries().get(0);
String newKey = newEntry.getCiteKeyOptional().orElse(null);
boolean entryChanged = false;
boolean emptyWarning = (newKey == null) || newKey.isEmpty();
if (newKey != null) {
entry.setCiteKey(newKey);
} else {
entry.clearCiteKey();
}
// First, remove fields that the user has removed.
for (Entry<String, String> field : entry.getFieldMap().entrySet()) {
String fieldName = field.getKey();
String fieldValue = field.getValue();
if (InternalBibtexFields.isDisplayableField(fieldName) && !newEntry.hasField(fieldName)) {
compound.addEdit(new UndoableFieldChange(entry, fieldName, fieldValue, null));
entry.clearField(fieldName);
entryChanged = true;
}
}
// Then set all fields that have been set by the user.
for (Entry<String, String> field : newEntry.getFieldMap().entrySet()) {
String fieldName = field.getKey();
String oldValue = entry.getField(fieldName).orElse(null);
String newValue = field.getValue();
if (!Objects.equals(oldValue, newValue)) {
// Test if the field is legally set.
new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()).format(newValue, fieldName);
compound.addEdit(new UndoableFieldChange(entry, fieldName, oldValue, newValue));
entry.setField(fieldName, newValue);
entryChanged = true;
}
}
// See if the user has changed the entry type:
if (!Objects.equals(newEntry.getType(), entry.getType())) {
compound.addEdit(new UndoableChangeType(entry, entry.getType(), newEntry.getType()));
entry.setType(newEntry.getType());
entryChanged = true;
}
compound.end();
if (!entryChanged) {
return true;
}
panel.getUndoManager().addEdit(compound);
if (panel.getDatabase().getDuplicationChecker().isDuplicateCiteKeyExisting(entry)) {
warnDuplicateBibtexkey();
} else if (emptyWarning) {
warnEmptyBibtexkey();
} else {
panel.output(Localization.lang("Stored entry") + '.');
}
lastSourceStringAccepted = source.getText();
// Update UI
// TODO: we need to repaint the entryeditor if fields that are not displayed have been added
panel.updateEntryEditorIfShowing();
lastSourceAccepted = true;
updateSource = true;
// TODO: does updating work properly after source stored?
panel.markBaseChanged();
panel.highlightEntry(entry);
return true;
} catch (InvalidFieldValueException | IOException ex) {
// The source couldn't be parsed, so the user is given an
// error message, and the choice to keep or revert the contents
// of the source text field.
updateSource = false;
lastSourceAccepted = false;
tabbed.setSelectedComponent(srcPanel);
Object[] options = { Localization.lang("Edit"), Localization.lang("Revert to original source") };
if (!SwingUtilities.isEventDispatchThread()) {
int answer = JOptionPane.showOptionDialog(frame, Localization.lang("Error") + ": " + ex.getMessage(), Localization.lang("Problem with parsing entry"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
if (answer != 0) {
updateSource = true;
lastSourceAccepted = true;
updateSource();
}
}
LOGGER.debug("Incorrect source", ex);
return false;
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class DroppedFileHandler method handleDroppedfile.
/**
* @param fileName The name of the dragged file.
* @param fileType The FileType associated with the file.
* @param entry The target entry for the drop.
*/
public void handleDroppedfile(String fileName, ExternalFileType fileType, BibEntry entry) {
NamedCompound edits = new NamedCompound(Localization.lang("Drop %0", fileType.getExtension()));
if (tryXmpImport(fileName, fileType, edits)) {
edits.end();
panel.getUndoManager().addEdit(edits);
return;
}
// Show dialog
if (!showLinkMoveCopyRenameDialog(fileName, fileType, entry, panel.getDatabase())) {
return;
}
/*
* Ok, we're ready to go. See first if we need to do a file copy before
* linking:
*/
boolean success = true;
String destFilename;
if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(Paths.get(fileName), panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences())).toString();
} else {
destFilename = renameCheckBox.isSelected() ? renameToTextBox.getText() : Paths.get(fileName).toString();
if (copyRadioButton.isSelected()) {
success = doCopy(fileName, destFilename, edits);
} else if (moveRadioButton.isSelected()) {
success = doMove(fileName, destFilename, edits);
}
}
if (success) {
doLink(entry, fileType, destFilename, false, edits);
panel.markBaseChanged();
panel.updateEntryEditorIfShowing();
}
edits.end();
panel.getUndoManager().addEdit(edits);
}
Aggregations