Search in sources :

Example 1 with UndoableFieldChange

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);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 2 with UndoableFieldChange

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);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) TreeSet(java.util.TreeSet) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 3 with UndoableFieldChange

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;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound) KeywordList(org.jabref.model.entry.KeywordList) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 4 with UndoableFieldChange

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;
        }
    }
}
Also used : FileListTableModel(org.jabref.gui.filelist.FileListTableModel) ExternalFileTypeEntryEditor(org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor) Optional(java.util.Optional) FileListEntry(org.jabref.gui.filelist.FileListEntry) Eprint(org.jabref.model.entry.identifier.Eprint) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) FileListEntryEditor(org.jabref.gui.filelist.FileListEntryEditor) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with UndoableFieldChange

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;
}
Also used : NamedCompound(org.jabref.gui.undo.NamedCompound) FieldChange(org.jabref.model.FieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Aggregations

UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)23 BibEntry (org.jabref.model.entry.BibEntry)13 NamedCompound (org.jabref.gui.undo.NamedCompound)11 FieldChange (org.jabref.model.FieldChange)8 FileListTableModel (org.jabref.gui.filelist.FileListTableModel)5 FileListEntry (org.jabref.gui.filelist.FileListEntry)4 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)3 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 ExternalFileTypeEntryEditor (org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor)2 FileListEntryEditor (org.jabref.gui.filelist.FileListEntryEditor)2 Subscribe (com.google.common.eventbus.Subscribe)1 ActionEvent (java.awt.event.ActionEvent)1 File (java.io.File)1 StringReader (java.io.StringReader)1