Search in sources :

Example 11 with UndoableFieldChange

use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.

the class ReplaceStringDialog method replaceField.

private int replaceField(BibEntry be, String fieldname, NamedCompound ce) {
    if (!be.hasField(fieldname)) {
        return 0;
    }
    String txt = be.getField(fieldname).get();
    StringBuilder sb = new StringBuilder();
    int ind;
    int piv = 0;
    int counter = 0;
    int len1 = fromString.length();
    while ((ind = txt.indexOf(fromString, piv)) >= 0) {
        counter++;
        // Text leading up to s1
        sb.append(txt.substring(piv, ind));
        // Insert s2
        sb.append(toString);
        piv = ind + len1;
    }
    sb.append(txt.substring(piv));
    String newStr = sb.toString();
    be.setField(fieldname, newStr);
    ce.addEdit(new UndoableFieldChange(be, fieldname, txt, newStr));
    return counter;
}
Also used : UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 12 with UndoableFieldChange

use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.

the class AutoSetLinks method autoSetLinks.

/**
     * Automatically add links for this set of entries, based on the globally stored list of external file types. The
     * entries are modified, and corresponding UndoEdit elements added to the NamedCompound given as argument.
     * Furthermore, all entries which are modified are added to the Set of entries given as an argument.
     * <p>
     * The entries' bibtex keys must have been set - entries lacking key are ignored. The operation is done in a new
     * thread, which is returned for the caller to wait for if needed.
     *
     * @param entries          A collection of BibEntry objects to find links for.
     * @param ce               A NamedCompound to add UndoEdit elements to.
     * @param changedEntries   MODIFIED, optional. A Set of BibEntry objects to which all modified entries is added.
     *                         This is used for status output and debugging
     * @param singleTableModel UGLY HACK. The table model to insert links into. Already existing links are not
     *                         duplicated or removed. This parameter has to be null if entries.count() != 1. The hack has been
     *                         introduced as a bibtexentry does not (yet) support the function getListTableModel() and the
     *                         FileListEntryEditor editor holds an instance of that table model and does not reconstruct it after the
     *                         search has succeeded.
     * @param databaseContext  The database providing the relevant file directory, if any.
     * @param callback         An ActionListener that is notified (on the event dispatch thread) when the search is finished.
     *                         The ActionEvent has id=0 if no new links were added, and id=1 if one or more links were added. This
     *                         parameter can be null, which means that no callback will be notified.
     * @param diag             An instantiated modal JDialog which will be used to display the progress of the automatically setting. This
     *                         parameter can be null, which means that no progress update will be shown.
     * @return the thread performing the automatically setting
     */
public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCompound ce, final Set<BibEntry> changedEntries, final FileListTableModel singleTableModel, final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
    final Collection<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
    if (diag != null) {
        final JProgressBar prog = new JProgressBar(SwingConstants.HORIZONTAL, 0, types.size() - 1);
        final JLabel label = new JLabel(Localization.lang("Searching for files"));
        prog.setIndeterminate(true);
        prog.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        diag.setTitle(Localization.lang("Automatically setting file links"));
        diag.getContentPane().add(prog, BorderLayout.CENTER);
        diag.getContentPane().add(label, BorderLayout.SOUTH);
        diag.pack();
        diag.setLocationRelativeTo(diag.getParent());
    }
    Runnable r = new Runnable() {

        @Override
        public void run() {
            // determine directories to search in
            final List<Path> dirs = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
            // determine extensions
            final List<String> extensions = types.stream().map(ExternalFileType::getExtension).collect(Collectors.toList());
            // Run the search operation:
            FileFinder fileFinder = FileFinders.constructFromConfiguration(Globals.prefs.getAutoLinkPreferences());
            Map<BibEntry, List<Path>> result = fileFinder.findAssociatedFiles(entries, dirs, extensions);
            boolean foundAny = false;
            // Iterate over the entries:
            for (Entry<BibEntry, List<Path>> entryFilePair : result.entrySet()) {
                FileListTableModel tableModel;
                Optional<String> oldVal = entryFilePair.getKey().getField(FieldName.FILE);
                if (singleTableModel == null) {
                    tableModel = new FileListTableModel();
                    oldVal.ifPresent(tableModel::setContent);
                } else {
                    assert entries.size() == 1;
                    tableModel = singleTableModel;
                }
                List<Path> files = entryFilePair.getValue();
                for (Path f : files) {
                    f = FileUtil.shortenFileName(f, dirs);
                    boolean alreadyHas = false;
                    //System.out.println("File: "+f.getPath());
                    for (int j = 0; j < tableModel.getRowCount(); j++) {
                        FileListEntry existingEntry = tableModel.getEntry(j);
                        //System.out.println("Comp: "+existingEntry.getLink());
                        if (Paths.get(existingEntry.getLink()).equals(f)) {
                            alreadyHas = true;
                            foundAny = true;
                            break;
                        }
                    }
                    if (!alreadyHas) {
                        foundAny = true;
                        Optional<ExternalFileType> type;
                        Optional<String> extension = FileHelper.getFileExtension(f);
                        if (extension.isPresent()) {
                            type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(extension.get());
                        } else {
                            type = Optional.of(new UnknownExternalFileType(""));
                        }
                        FileListEntry flEntry = new FileListEntry(f.getFileName().toString(), f.toString(), type);
                        tableModel.addEntry(tableModel.getRowCount(), flEntry);
                        String newVal = tableModel.getStringRepresentation();
                        if (newVal.isEmpty()) {
                            newVal = null;
                        }
                        if (ce != null) {
                            // store undo information
                            UndoableFieldChange change = new UndoableFieldChange(entryFilePair.getKey(), FieldName.FILE, oldVal.orElse(null), newVal);
                            ce.addEdit(change);
                        }
                        // hack: if table model is given, do NOT modify entry
                        if (singleTableModel == null) {
                            entryFilePair.getKey().setField(FieldName.FILE, newVal);
                        }
                        if (changedEntries != null) {
                            changedEntries.add(entryFilePair.getKey());
                        }
                    }
                }
            }
            // handle callbacks and dialog
            // FIXME: The ID signals if action was successful :/
            final int id = foundAny ? 1 : 0;
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    if (diag != null) {
                        diag.dispose();
                    }
                    if (callback != null) {
                        callback.actionPerformed(new ActionEvent(this, id, ""));
                    }
                }
            });
        }
    };
    SwingUtilities.invokeLater(() -> {
        // show dialog which will be hidden when the task is done
        if (diag != null) {
            diag.setVisible(true);
        }
    });
    return r;
}
Also used : ActionEvent(java.awt.event.ActionEvent) JProgressBar(javax.swing.JProgressBar) FileListEntry(org.jabref.gui.filelist.FileListEntry) FileFinder(org.jabref.logic.util.io.FileFinder) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) List(java.util.List) Path(java.nio.file.Path) BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) JLabel(javax.swing.JLabel)

Example 13 with UndoableFieldChange

use of org.jabref.gui.undo.UndoableFieldChange 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();
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) LinkedFile(org.jabref.model.entry.LinkedFile) ExternalFileTypeEntryEditor(org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor) Optional(java.util.Optional) ArrayList(java.util.ArrayList) FileListEntry(org.jabref.gui.filelist.FileListEntry) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) FileListEntryEditor(org.jabref.gui.filelist.FileListEntryEditor) NamedCompound(org.jabref.gui.undo.NamedCompound) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) HashSet(java.util.HashSet)

Example 14 with UndoableFieldChange

use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.

the class DroppedFileHandler method doLink.

/**
     * Make a extension to the file.
     *
     * @param entry    The entry to extension from.
     * @param fileType The FileType associated with the file.
     * @param filename The path to the file.
     * @param edits    An NamedCompound action this action is to be added to. If none
     *                 is given, the edit is added to the panel's undoManager.
     */
private void doLink(BibEntry entry, ExternalFileType fileType, String filename, boolean avoidDuplicate, NamedCompound edits) {
    Optional<String> oldValue = entry.getField(FieldName.FILE);
    FileListTableModel tm = new FileListTableModel();
    oldValue.ifPresent(tm::setContent);
    // If avoidDuplicate==true, we should check if this file is already linked:
    if (avoidDuplicate) {
        // For comparison, find the absolute filename:
        List<Path> dirs = panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
        String absFilename;
        if (new File(filename).isAbsolute() || dirs.isEmpty()) {
            absFilename = filename;
        } else {
            Optional<Path> file = FileHelper.expandFilenameAsPath(filename, dirs);
            if (file.isPresent()) {
                absFilename = file.get().toAbsolutePath().toString();
            } else {
                // This shouldn't happen based on the old code, so maybe one should set it something else?
                absFilename = "";
            }
        }
        LOGGER.debug("absFilename: " + absFilename);
        for (int i = 0; i < tm.getRowCount(); i++) {
            FileListEntry flEntry = tm.getEntry(i);
            // Find the absolute filename for this existing link:
            String absName = flEntry.toParsedFileField().findIn(dirs).map(Path::toAbsolutePath).map(Path::toString).orElse(null);
            LOGGER.debug("absName: " + absName);
            // If the filenames are equal, we don't need to link, so we simply return:
            if (absFilename.equals(absName)) {
                return;
            }
        }
    }
    tm.addEntry(tm.getRowCount(), new FileListEntry("", filename, fileType));
    String newValue = tm.getStringRepresentation();
    UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE, oldValue.orElse(null), newValue);
    entry.setField(FieldName.FILE, newValue);
    if (edits == null) {
        panel.getUndoManager().addEdit(edit);
    } else {
        edits.addEdit(edit);
    }
}
Also used : Path(java.nio.file.Path) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) FileListEntry(org.jabref.gui.filelist.FileListEntry) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) File(java.io.File)

Example 15 with UndoableFieldChange

use of org.jabref.gui.undo.UndoableFieldChange in project jabref by JabRef.

the class FindFullTextAction method update.

@Override
public void update() {
    List<Optional<URL>> remove = new ArrayList<>();
    for (Entry<Optional<URL>, BibEntry> download : downloads.entrySet()) {
        BibEntry entry = download.getValue();
        Optional<URL> result = download.getKey();
        if (result.isPresent()) {
            List<String> dirs = basePanel.getBibDatabaseContext().getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
            if (dirs.isEmpty()) {
                JOptionPane.showMessageDialog(basePanel.frame(), Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences") + " -> " + Localization.lang("File"), Localization.lang("Directory not found"), JOptionPane.ERROR_MESSAGE);
                return;
            }
            DownloadExternalFile def = new DownloadExternalFile(basePanel.frame(), basePanel.getBibDatabaseContext(), entry);
            try {
                def.download(result.get(), file -> {
                    FileListTableModel fileLinkModel = new FileListTableModel();
                    entry.getField(FieldName.FILE).ifPresent(fileLinkModel::setContent);
                    fileLinkModel.addEntry(0, file);
                    String newValue = fileLinkModel.getStringRepresentation();
                    UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE, entry.getField(FieldName.FILE).orElse(null), newValue);
                    entry.setField(FieldName.FILE, newValue);
                    basePanel.getUndoManager().addEdit(edit);
                    basePanel.markBaseChanged();
                });
            } catch (IOException e) {
                LOGGER.warn("Problem downloading file", e);
            }
            basePanel.output(Localization.lang("Finished downloading full text document for entry %0.", entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
        } else {
            String title = Localization.lang("Full text document download failed");
            String message = Localization.lang("Full text document download failed for entry %0.", entry.getCiteKeyOptional().orElse(Localization.lang("undefined")));
            basePanel.output(message);
            JOptionPane.showMessageDialog(basePanel.frame(), message, title, JOptionPane.ERROR_MESSAGE);
        }
        remove.add(result);
    }
    for (Optional<URL> result : remove) {
        downloads.remove(result);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) Optional(java.util.Optional) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) 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