use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class EntryMarker method unmarkEntry.
/**
* SIDE EFFECT: Unselects given entry
*/
public static void unmarkEntry(BibEntry be, boolean onlyMaxLevel, BibDatabase database, NamedCompound ce) {
if (be.hasField(FieldName.MARKED_INTERNAL)) {
String markerString = be.getField(FieldName.MARKED_INTERNAL).get();
if ("0".equals(markerString)) {
if (!onlyMaxLevel) {
unmarkOldStyle(be, database, ce);
}
return;
}
String newValue = null;
int index = markerString.indexOf(Globals.prefs.getWrappedUsername());
if (index >= 0) {
// Marked 1 for this user.
if (onlyMaxLevel) {
return;
} else {
newValue = markerString.substring(0, index) + markerString.substring(index + Globals.prefs.getWrappedUsername().length());
}
} else {
Matcher m = MARK_NUMBER_PATTERN.matcher(markerString);
if (m.find()) {
try {
int prevMarkLevel = Integer.parseInt(m.group(1));
if (!onlyMaxLevel || (prevMarkLevel == MARK_COLOR_LEVELS)) {
if (prevMarkLevel > 1) {
newValue = markerString.substring(0, m.start(1)) + markerString.substring(m.end(1));
} else {
String toRemove = Globals.prefs.getWrappedUsername().substring(0, Globals.prefs.getWrappedUsername().length() - 1) + ":1]";
index = markerString.indexOf(toRemove);
if (index >= 0) {
newValue = markerString.substring(0, index) + markerString.substring(index + toRemove.length());
}
}
} else {
return;
}
} catch (NumberFormatException ex) {
// Do nothing.
}
}
}
/*int piv = 0, hit;
StringBuffer sb = new StringBuffer();
while ((hit = s.indexOf(G047749118118
1110lobals.prefs.WRAPPED_USERNAME, piv)) >= 0) {
if (hit > 0)
sb.append(s.substring(piv, hit));
piv = hit + Globals.prefs.WRAPPED_USERNAME.length();
}
if (piv < s.length() - 1) {
sb.append(s.substring(piv));
}
String newVal = sb.length() > 0 ? sb.toString() : null;*/
ce.addEdit(new UndoableFieldChange(be, FieldName.MARKED_INTERNAL, be.getField(FieldName.MARKED_INTERNAL).get(), newValue));
if (newValue == null) {
be.clearField(FieldName.MARKED_INTERNAL);
} else {
be.setField(FieldName.MARKED_INTERNAL, newValue);
}
}
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class EntryMarker method unmarkOldStyle.
/**
* An entry is marked with a "0", not in the new style with user names. We
* want to unmark it as transparently as possible. Since this shouldn't
* happen too often, we do it by scanning the "owner" fields of the entire
* database, collecting all user names. We then mark the entry for all users
* except the current one. Thus only the user who unmarks will see that it
* is unmarked, and we get rid of the old-style marking.
*
* @param be
* @param ce
*/
private static void unmarkOldStyle(BibEntry be, BibDatabase database, NamedCompound ce) {
Set<Object> owners = new TreeSet<>();
for (BibEntry entry : database.getEntries()) {
entry.getField(FieldName.OWNER).ifPresent(owners::add);
}
owners.remove(Globals.prefs.get(JabRefPreferences.DEFAULT_OWNER));
StringBuilder sb = new StringBuilder();
for (Object owner : owners) {
sb.append('[');
sb.append(owner);
sb.append(']');
}
String newVal = sb.toString();
if (newVal.isEmpty()) {
ce.addEdit(new UndoableFieldChange(be, FieldName.MARKED_INTERNAL, be.getField(FieldName.MARKED_INTERNAL).orElse(null), null));
be.clearField(FieldName.MARKED_INTERNAL);
} else {
ce.addEdit(new UndoableFieldChange(be, FieldName.MARKED_INTERNAL, be.getField(FieldName.MARKED_INTERNAL).orElse(null), newVal));
be.setField(FieldName.MARKED_INTERNAL, newVal);
}
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class ManageKeywordsAction method updateKeywords.
private NamedCompound updateKeywords(List<BibEntry> entries, KeywordList keywordsToAdd, KeywordList keywordsToRemove) {
NamedCompound ce = new NamedCompound(Localization.lang("Update keywords"));
for (BibEntry entry : entries) {
KeywordList keywords = entry.getKeywords(Globals.prefs.getKeywordDelimiter());
// update keywords
keywords.removeAll(keywordsToRemove);
keywords.addAll(keywordsToAdd);
// put keywords back
Optional<FieldChange> change = entry.putKeywords(keywords, Globals.prefs.getKeywordDelimiter());
if (change.isPresent()) {
ce.addEdit(new UndoableFieldChange(change.get()));
}
if (Globals.prefs.isKeywordSyncEnabled()) {
SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, Globals.prefs.getKeywordDelimiter());
}
}
ce.end();
return ce;
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class JabRefDesktop method openExternalFileUnknown.
public static boolean openExternalFileUnknown(JabRefFrame frame, BibEntry entry, BibDatabaseContext databaseContext, String link, UnknownExternalFileType fileType) throws IOException {
String cancelMessage = Localization.lang("Unable to open file.");
String[] options = new String[] { Localization.lang("Define '%0'", fileType.getName()), Localization.lang("Change file type"), Localization.lang("Cancel") };
String defOption = options[0];
int answer = JOptionPane.showOptionDialog(frame, Localization.lang("This external link is of the type '%0', which is undefined. What do you want to do?", fileType.getName()), Localization.lang("Undefined file type"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, defOption);
if (answer == JOptionPane.CANCEL_OPTION) {
frame.output(cancelMessage);
return false;
} else if (answer == JOptionPane.YES_OPTION) {
// User wants to define the new file type. Show the dialog:
ExternalFileType newType = new ExternalFileType(fileType.getName(), "", "", "", "new", IconTheme.JabRefIcon.FILE.getSmallIcon());
ExternalFileTypeEntryEditor editor = new ExternalFileTypeEntryEditor(frame, newType);
editor.setVisible(true);
if (editor.okPressed()) {
// Get the old list of types, add this one, and update the list in prefs:
List<ExternalFileType> fileTypes = new ArrayList<>(ExternalFileTypes.getInstance().getExternalFileTypeSelection());
fileTypes.add(newType);
Collections.sort(fileTypes);
ExternalFileTypes.getInstance().setExternalFileTypes(fileTypes);
// Finally, open the file:
return openExternalFileAnyFormat(databaseContext, link, Optional.of(newType));
} else {
// Canceled:
frame.output(cancelMessage);
return false;
}
} else {
// User wants to change the type of this link.
// First get a model of all file links for this entry:
FileListTableModel tModel = new FileListTableModel();
Optional<String> oldValue = entry.getField(FieldName.FILE);
oldValue.ifPresent(tModel::setContent);
FileListEntry flEntry = null;
// Then find which one we are looking at:
for (int i = 0; i < tModel.getRowCount(); i++) {
FileListEntry iEntry = tModel.getEntry(i);
if (iEntry.getLink().equals(link)) {
flEntry = iEntry;
break;
}
}
if (flEntry == null) {
// This shouldn't happen, so I'm not sure what to put in here:
throw new RuntimeException("Could not find the file list entry " + link + " in " + entry);
}
FileListEntryEditor editor = new FileListEntryEditor(frame, flEntry, false, true, databaseContext);
editor.setVisible(true, false);
if (editor.okPressed()) {
// Store the changes and add an undo edit:
String newValue = tModel.getStringRepresentation();
UndoableFieldChange ce = new UndoableFieldChange(entry, FieldName.FILE, oldValue.orElse(null), newValue);
entry.setField(FieldName.FILE, newValue);
frame.getCurrentBasePanel().getUndoManager().addEdit(ce);
frame.getCurrentBasePanel().markBaseChanged();
// Finally, open the link:
return openExternalFileAnyFormat(databaseContext, flEntry.getLink(), flEntry.getType());
} else {
// Canceled:
frame.output(cancelMessage);
return false;
}
}
}
use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.
the class UndoableChangeEntriesOfGroup method getUndoableEdit.
public static AbstractUndoableEdit getUndoableEdit(GroupTreeNodeViewModel node, List<FieldChange> changes) {
boolean hasEntryChanges = false;
NamedCompound entryChangeCompound = new NamedCompound(Localization.lang("change entries of group"));
for (FieldChange fieldChange : changes) {
hasEntryChanges = true;
entryChangeCompound.addEdit(new UndoableFieldChange(fieldChange));
}
if (hasEntryChanges) {
entryChangeCompound.end();
return entryChangeCompound;
}
return null;
}
Aggregations