use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class FileLinksUpgradeWarning method makeChanges.
/**
* This method performs the actual changes.
* @param panel
* @param pr
* @param fileDir The path to the file directory to set, or null if it should not be set.
*/
private void makeChanges(BasePanel panel, ParserResult pr, boolean upgradePrefs, boolean upgradeDatabase, String fileDir) {
if (upgradeDatabase) {
// Update file links links in the database:
NamedCompound ce = upgradePdfPsToFile(pr.getDatabase());
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
}
if (fileDir != null) {
Globals.prefs.put(FieldName.FILE + FileDirectoryPreferences.DIR_SUFFIX, fileDir);
}
if (upgradePrefs) {
// Exchange table columns:
Globals.prefs.putBoolean(JabRefPreferences.FILE_COLUMN, Boolean.TRUE);
// If we don't find the file field, insert it at the bottom of the first tab:
if (!showsFileInGenFields()) {
String gfs = Globals.prefs.get(JabRefPreferences.CUSTOM_TAB_FIELDS + "0");
StringBuilder sb = new StringBuilder(gfs);
if (!gfs.isEmpty()) {
sb.append(';');
}
sb.append(FieldName.FILE);
Globals.prefs.put(JabRefPreferences.CUSTOM_TAB_FIELDS + "0", sb.toString());
Globals.prefs.updateEntryEditorTabList();
}
panel.frame().setupAllTables();
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class BasePanel method changeType.
private void changeType(List<BibEntry> entries, String newType) {
if ((entries == null) || (entries.isEmpty())) {
LOGGER.error("At least one entry must be selected to be able to change the type.");
return;
}
if (entries.size() > 1) {
int choice = JOptionPane.showConfirmDialog(this, Localization.lang("Multiple entries selected. Do you want to change the type of all these to '%0'?", newType), Localization.lang("Change entry type"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.NO_OPTION) {
return;
}
}
NamedCompound compound = new NamedCompound(Localization.lang("Change entry type"));
for (BibEntry entry : entries) {
compound.addEdit(new UndoableChangeType(entry, entry.getType(), newType));
entry.setType(newType);
}
output(formatOutputMessage(Localization.lang("Changed type to '%0' for", newType), entries.size()));
compound.end();
getUndoManager().addEdit(compound);
markBaseChanged();
updateEntryEditorIfShowing();
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class DuplicateSearch method run.
@Override
public void run() {
panel.output(Localization.lang("Searching for duplicates..."));
bes = panel.getDatabase().getEntries();
if (bes.size() < 2) {
return;
}
SearcherRunnable st = new SearcherRunnable();
JabRefExecutorService.INSTANCE.executeInterruptableTask(st, "DuplicateSearcher");
int current = 0;
final List<BibEntry> toRemove = new ArrayList<>();
final List<BibEntry> toAdd = new ArrayList<>();
int duplicateCounter = 0;
boolean autoRemoveExactDuplicates = false;
synchronized (duplicates) {
while (!st.finished() || (current < duplicates.size())) {
if (current >= duplicates.size()) {
try {
duplicates.wait();
} catch (InterruptedException ignored) {
// Ignore
}
} else {
// duplicates found
List<BibEntry> be = duplicates.get(current);
current++;
if (!toRemove.contains(be.get(0)) && !toRemove.contains(be.get(1))) {
// Check if they are exact duplicates:
boolean askAboutExact = false;
if (DuplicateCheck.compareEntriesStrictly(be.get(0), be.get(1)) > 1) {
if (autoRemoveExactDuplicates) {
toRemove.add(be.get(1));
duplicateCounter++;
continue;
}
askAboutExact = true;
}
DuplicateCallBack cb = new DuplicateCallBack(JabRefGUI.getMainFrame(), be.get(0), be.get(1), askAboutExact ? DuplicateResolverType.DUPLICATE_SEARCH_WITH_EXACT : DuplicateResolverType.DUPLICATE_SEARCH);
((CallBack) Spin.over(cb)).update();
duplicateCounter++;
DuplicateResolverResult answer = cb.getSelected();
if ((answer == DuplicateResolverResult.KEEP_LEFT) || (answer == DuplicateResolverResult.AUTOREMOVE_EXACT)) {
toRemove.add(be.get(1));
if (answer == DuplicateResolverResult.AUTOREMOVE_EXACT) {
// Remember choice
autoRemoveExactDuplicates = true;
}
} else if (answer == DuplicateResolverResult.KEEP_RIGHT) {
toRemove.add(be.get(0));
} else if (answer == DuplicateResolverResult.BREAK) {
// thread killing
st.setFinished();
current = Integer.MAX_VALUE;
// correct counter
duplicateCounter--;
} else if (answer == DuplicateResolverResult.KEEP_MERGE) {
toRemove.addAll(be);
toAdd.add(cb.getMergedEntry());
}
}
}
}
}
final NamedCompound ce = new NamedCompound(Localization.lang("duplicate removal"));
final int dupliC = duplicateCounter;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Now, do the actual removal:
if (!toRemove.isEmpty()) {
for (BibEntry entry : toRemove) {
panel.getDatabase().removeEntry(entry);
ce.addEdit(new UndoableRemoveEntry(panel.getDatabase(), entry, panel));
}
panel.markBaseChanged();
}
// and adding merged entries:
if (!toAdd.isEmpty()) {
for (BibEntry entry : toAdd) {
panel.getDatabase().insertEntry(entry);
ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry, panel));
}
panel.markBaseChanged();
}
synchronized (duplicates) {
panel.output(Localization.lang("Duplicates found") + ": " + duplicates.size() + ' ' + Localization.lang("pairs processed") + ": " + dupliC);
}
ce.end();
panel.getUndoManager().addEdit(ce);
}
});
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class ManageKeywordsAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
BasePanel bp = frame.getCurrentBasePanel();
if (bp == null) {
return;
}
if (bp.getSelectedEntries().isEmpty()) {
bp.output(Localization.lang("Select at least one entry to manage keywords."));
return;
}
// Lazy creation of the dialog:
createDialog();
canceled = true;
fillKeyWordList();
diag.pack();
diag.setLocationRelativeTo(frame);
diag.setVisible(true);
if (canceled) {
return;
}
KeywordList keywordsToAdd = new KeywordList();
KeywordList userSelectedKeywords = new KeywordList();
// build keywordsToAdd and userSelectedKeywords in parallel
for (Enumeration<Keyword> keywords = keywordListModel.elements(); keywords.hasMoreElements(); ) {
Keyword keyword = keywords.nextElement();
userSelectedKeywords.add(keyword);
if (!sortedKeywordsOfAllEntriesBeforeUpdateByUser.contains(keyword)) {
keywordsToAdd.add(keyword);
}
}
KeywordList keywordsToRemove = new KeywordList();
for (Keyword kword : sortedKeywordsOfAllEntriesBeforeUpdateByUser) {
if (!userSelectedKeywords.contains(kword)) {
keywordsToRemove.add(kword);
}
}
if (keywordsToAdd.isEmpty() && keywordsToRemove.isEmpty()) {
// nothing to be done if nothing is new and nothing is obsolete
return;
}
if (Globals.prefs.isKeywordSyncEnabled() && !keywordsToAdd.isEmpty()) {
SpecialFieldsUtils.synchronizeSpecialFields(keywordsToAdd, keywordsToRemove);
}
NamedCompound ce = updateKeywords(bp.getSelectedEntries(), keywordsToAdd, keywordsToRemove);
bp.getUndoManager().addEdit(ce);
bp.markBaseChanged();
}
use of org.jabref.gui.undo.NamedCompound 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;
}
Aggregations