use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class FileLinksUpgradeWarning method upgradePdfPsToFile.
/**
* Collect file links from the given set of fields, and add them to the list contained in the field
* GUIGlobals.FILE_FIELD.
*
* @param database The database to modify.
* @param fields The fields to find links in.
* @return A CompoundEdit specifying the undo operation for the whole operation.
*/
private static NamedCompound upgradePdfPsToFile(BibDatabase database) {
NamedCompound ce = new NamedCompound(Localization.lang("Move external links to 'file' field"));
UpgradePdfPsToFileCleanup cleanupJob = new UpgradePdfPsToFileCleanup();
for (BibEntry entry : database.getEntries()) {
List<FieldChange> changes = cleanupJob.cleanup(entry);
for (FieldChange change : changes) {
ce.addEdit(new UndoableFieldChange(change));
}
}
ce.end();
return ce;
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class EntryMarker method markEntry.
/**
* @param increment whether the given increment should be added to the current one. Currently never used in JabRef. Could be used to increase marking color ("Mark in specific color").
*/
public static void markEntry(BibEntry be, int markIncrement, boolean increment, NamedCompound ce) {
int prevMarkLevel;
String newValue = null;
if (be.hasField(FieldName.MARKED_INTERNAL)) {
String markerString = be.getField(FieldName.MARKED_INTERNAL).get();
int index = markerString.indexOf(Globals.prefs.getWrappedUsername());
if (index >= 0) {
// Already marked 1 for this user.
prevMarkLevel = 1;
newValue = markerString.substring(0, index) + markerString.substring(index + Globals.prefs.getWrappedUsername().length()) + Globals.prefs.getWrappedUsername().substring(0, Globals.prefs.getWrappedUsername().length() - 1) + ":" + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + "]";
} else {
Matcher m = MARK_NUMBER_PATTERN.matcher(markerString);
if (m.find()) {
try {
prevMarkLevel = Integer.parseInt(m.group(1));
newValue = markerString.substring(0, m.start(1)) + (increment ? Math.min(MAX_MARKING_LEVEL, prevMarkLevel + markIncrement) : markIncrement) + markerString.substring(m.end(1));
} catch (NumberFormatException ex) {
// Do nothing.
}
}
}
}
if (newValue == null) {
newValue = Globals.prefs.getWrappedUsername().substring(0, Globals.prefs.getWrappedUsername().length() - 1) + ":" + markIncrement + "]";
}
ce.addEdit(new UndoableFieldChange(be, FieldName.MARKED_INTERNAL, be.getField(FieldName.MARKED_INTERNAL).orElse(null), newValue));
be.setField(FieldName.MARKED_INTERNAL, newValue);
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class MassSetFieldAction method massRenameField.
/**
* Move contents from one field to another for a Collection of entries.
*
* @param entries The entries to do this operation for.
* @param field The field to move contents from.
* @param newField The field to move contents into.
* @param overwriteValues If true, overwrites any existing values in the new field. If false, makes no change for
* entries with existing value in the new field.
* @return A CompoundEdit for the entire operation.
*/
private static UndoableEdit massRenameField(Collection<BibEntry> entries, String field, String newField, boolean overwriteValues) {
NamedCompound ce = new NamedCompound(Localization.lang("Rename field"));
for (BibEntry entry : entries) {
Optional<String> valToMove = entry.getField(field);
// If there is no value, do nothing:
if ((!valToMove.isPresent()) || valToMove.get().isEmpty()) {
continue;
}
// If we are not allowed to overwrite values, check if there is a
// non-empty value already for this entry for the new field:
Optional<String> valInNewField = entry.getField(newField);
if (!overwriteValues && (valInNewField.isPresent()) && !valInNewField.get().isEmpty()) {
continue;
}
entry.setField(newField, valToMove.get());
ce.addEdit(new UndoableFieldChange(entry, newField, valInNewField.orElse(null), valToMove.get()));
entry.clearField(field);
ce.addEdit(new UndoableFieldChange(entry, field, valToMove.get(), null));
}
ce.end();
return ce;
}
use of org.jabref.gui.undo.UndoableFieldChange 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.UndoableFieldChange 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;
}
}
Aggregations