use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class BasePanel method autoGenerateKeysBeforeSaving.
/**
* If the relevant option is set, autogenerate keys for all entries that are lacking keys.
*/
public void autoGenerateKeysBeforeSaving() {
if (Globals.prefs.getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING)) {
NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys"));
for (BibEntry bes : bibDatabaseContext.getDatabase().getEntries()) {
Optional<String> oldKey = bes.getCiteKeyOptional();
if (!(oldKey.isPresent()) || oldKey.get().isEmpty()) {
BibtexKeyPatternUtil.makeAndSetLabel(bibDatabaseContext.getMetaData().getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), bibDatabaseContext.getDatabase(), bes, Globals.prefs.getBibtexKeyPatternPreferences());
bes.getCiteKeyOptional().ifPresent(newKey -> ce.addEdit(new UndoableKeyChange(bes, oldKey.orElse(""), newKey)));
}
}
// Store undo information, if any:
if (ce.hasEdits()) {
ce.end();
getUndoManager().addEdit(ce);
}
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class BasePanel method registerUndoableChanges.
public void registerUndoableChanges(SaveSession session) {
NamedCompound ce = new NamedCompound(Localization.lang("Save actions"));
for (FieldChange change : session.getFieldChanges()) {
ce.addEdit(new UndoableFieldChange(change));
}
ce.end();
if (ce.hasEdits()) {
getUndoManager().addEdit(ce);
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class BasePanel method delete.
/**
* Removes the selected entries from the database
* @param cut If false the user will get asked if he really wants to delete the entries, and it will be localized
* as "deleted".
* If true the action will be localized as "cut"
*/
private void delete(boolean cut) {
List<BibEntry> entries = mainTable.getSelectedEntries();
if (entries.isEmpty()) {
return;
}
if (!cut && !showDeleteConfirmationDialog(entries.size())) {
return;
}
// select the next entry to stay at the same place as before (or the previous if we're already at the end)
if (mainTable.getSelectedRow() != (mainTable.getRowCount() - 1)) {
selectNextEntry();
} else {
selectPreviousEntry();
}
NamedCompound compound;
if (cut) {
compound = new NamedCompound((entries.size() > 1 ? Localization.lang("cut entries") : Localization.lang("cut entry")));
} else {
compound = new NamedCompound((entries.size() > 1 ? Localization.lang("delete entries") : Localization.lang("delete entry")));
}
for (BibEntry entry : entries) {
compound.addEdit(new UndoableRemoveEntry(bibDatabaseContext.getDatabase(), entry, BasePanel.this));
bibDatabaseContext.getDatabase().removeEntry(entry);
ensureNotShowingBottomPanel(entry);
}
compound.end();
getUndoManager().addEdit(compound);
markBaseChanged();
frame.output(formatOutputMessage(cut ? Localization.lang("Cut") : Localization.lang("Deleted"), entries.size()));
// prevent the main table from loosing focus
mainTable.requestFocus();
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class CleanupAction method run.
@Override
public void run() {
if (canceled) {
return;
}
CleanupPresetPanel presetPanel = new CleanupPresetPanel(panel.getBibDatabaseContext(), CleanupPreset.loadFromPreferences(preferences));
int choice = showDialog(presetPanel);
if (choice != JOptionPane.OK_OPTION) {
canceled = true;
return;
}
CleanupPreset cleanupPreset = presetPanel.getCleanupPreset();
cleanupPreset.storeInPreferences(preferences);
if (cleanupPreset.isRenamePDF() && Globals.prefs.getBoolean(JabRefPreferences.ASK_AUTO_NAMING_PDFS_AGAIN)) {
CheckBoxMessage cbm = new CheckBoxMessage(Localization.lang("Auto-generating PDF-Names does not support undo. Continue?"), Localization.lang("Disable this confirmation dialog"), false);
int answer = JOptionPane.showConfirmDialog(frame, cbm, Localization.lang("Autogenerate PDF Names"), JOptionPane.YES_NO_OPTION);
if (cbm.isSelected()) {
Globals.prefs.putBoolean(JabRefPreferences.ASK_AUTO_NAMING_PDFS_AGAIN, false);
}
if (answer == JOptionPane.NO_OPTION) {
canceled = true;
return;
}
}
for (BibEntry entry : panel.getSelectedEntries()) {
// undo granularity is on entry level
NamedCompound ce = new NamedCompound(Localization.lang("Cleanup entry"));
doCleanup(cleanupPreset, entry, ce);
ce.end();
if (ce.hasEdits()) {
modifiedEntriesCount++;
panel.getUndoManager().addEdit(ce);
}
}
}
use of org.jabref.gui.undo.NamedCompound in project jabref by JabRef.
the class SynchronizeFileField method run.
@Override
public void run() {
if (!goOn) {
panel.output(Localization.lang("This operation requires one or more entries to be selected."));
return;
}
entriesChangedCount = 0;
panel.frame().setProgressBarValue(0);
panel.frame().setProgressBarVisible(true);
// autoSet takes 10 (?) times longer than checkExisting
int weightAutoSet = 10;
int progressBarMax = (autoSet ? weightAutoSet * sel.size() : 0) + (checkExisting ? sel.size() : 0);
panel.frame().setProgressBarMaximum(progressBarMax);
int progress = 0;
final NamedCompound ce = new NamedCompound(Localization.lang("Automatically set file links"));
Set<BibEntry> changedEntries = new HashSet<>();
// First we try to autoset fields
if (autoSet) {
List<BibEntry> entries = new ArrayList<>(sel);
// Start the automatically setting process:
Runnable r = AutoSetLinks.autoSetLinks(entries, ce, changedEntries, null, panel.getBibDatabaseContext(), null, null);
JabRefExecutorService.INSTANCE.executeAndWait(r);
}
progress += sel.size() * weightAutoSet;
panel.frame().setProgressBarValue(progress);
// The following loop checks all external links that are already set.
if (checkExisting) {
boolean removeAllBroken = false;
mainLoop: for (BibEntry aSel : sel) {
panel.frame().setProgressBarValue(progress++);
final Optional<String> old = aSel.getField(FieldName.FILE);
// Check if a extension is set:
if (old.isPresent() && !(old.get().isEmpty())) {
FileListTableModel tableModel = new FileListTableModel();
tableModel.setContentDontGuessTypes(old.get());
for (int j = 0; j < tableModel.getRowCount(); j++) {
FileListEntry flEntry = tableModel.getEntry(j);
LinkedFile field = flEntry.toParsedFileField();
// See if the link looks like an URL:
if (field.isOnlineLink()) {
// Don't check the remote file.
continue;
// TODO: should there be an option to check remote links?
}
// A variable to keep track of whether this link gets deleted:
boolean deleted = false;
// Get an absolute path representation:
Optional<Path> file = field.findIn(panel.getBibDatabaseContext(), Globals.prefs.getFileDirectoryPreferences());
if ((!file.isPresent()) || !Files.exists(file.get())) {
int answer;
if (removeAllBroken) {
// We should delete this link.
answer = 2;
} else {
answer = JOptionPane.showOptionDialog(panel.frame(), Localization.lang("<HTML>Could not find file '%0'<BR>linked from entry '%1'</HTML>", flEntry.getLink(), aSel.getCiteKeyOptional().orElse(Localization.lang("undefined"))), Localization.lang("Broken link"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, brokenLinkOptions, brokenLinkOptions[0]);
}
switch(answer) {
case 1:
// Assign new file.
FileListEntryEditor flEditor = new FileListEntryEditor(panel.frame(), flEntry, false, true, panel.getBibDatabaseContext());
flEditor.setVisible(true, true);
break;
case 2:
// Clear field:
tableModel.removeEntry(j);
// Make sure we don't investigate this link further.
deleted = true;
// Step back in the iteration, because we removed an entry.
j--;
break;
case 3:
// Clear field:
tableModel.removeEntry(j);
// Make sure we don't investigate this link further.
deleted = true;
// Step back in the iteration, because we removed an entry.
j--;
// Notify for further cases.
removeAllBroken = true;
break;
default:
// Cancel
break mainLoop;
}
}
// Unless we deleted this link, see if its file type is recognized:
if (!deleted && flEntry.getType().isPresent() && (flEntry.getType().get() instanceof UnknownExternalFileType)) {
String[] options = new String[] { Localization.lang("Define '%0'", flEntry.getType().get().getName()), Localization.lang("Change file type"), Localization.lang("Cancel") };
String defOption = options[0];
int answer = JOptionPane.showOptionDialog(panel.frame(), Localization.lang("One or more file links are of the type '%0', which is undefined. What do you want to do?", flEntry.getType().get().getName()), Localization.lang("Undefined file type"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, defOption);
if (answer == JOptionPane.CANCEL_OPTION) {
// User doesn't want to handle this unknown link type.
} else if (answer == JOptionPane.YES_OPTION) {
// User wants to define the new file type. Show the dialog:
ExternalFileType newType = new ExternalFileType(flEntry.getType().get().getName(), "", "", "", "new", IconTheme.JabRefIcon.FILE.getSmallIcon());
ExternalFileTypeEntryEditor editor = new ExternalFileTypeEntryEditor(panel.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);
panel.getMainTable().repaint();
}
} else {
// User wants to change the type of this link.
// First get a model of all file links for this entry:
FileListEntryEditor editor = new FileListEntryEditor(panel.frame(), flEntry, false, true, panel.getBibDatabaseContext());
editor.setVisible(true, false);
}
}
}
if (!tableModel.getStringRepresentation().equals(old.orElse(null))) {
// The table has been modified. Store the change:
String toSet = tableModel.getStringRepresentation();
if (toSet.isEmpty()) {
ce.addEdit(new UndoableFieldChange(aSel, FieldName.FILE, old.orElse(null), null));
aSel.clearField(FieldName.FILE);
} else {
ce.addEdit(new UndoableFieldChange(aSel, FieldName.FILE, old.orElse(null), toSet));
aSel.setField(FieldName.FILE, toSet);
}
changedEntries.add(aSel);
}
}
}
}
if (!changedEntries.isEmpty()) {
// Add the undo edit:
ce.end();
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
entriesChangedCount = changedEntries.size();
}
}
Aggregations