Search in sources :

Example 1 with ExternalFileTypeEntryEditor

use of org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor 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 2 with ExternalFileTypeEntryEditor

use of org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor 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)

Aggregations

ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)2 ExternalFileTypeEntryEditor (org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor)2 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)2 FileListEntry (org.jabref.gui.filelist.FileListEntry)2 FileListEntryEditor (org.jabref.gui.filelist.FileListEntryEditor)2 FileListTableModel (org.jabref.gui.filelist.FileListTableModel)2 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)2 HashSet (java.util.HashSet)1 List (java.util.List)1 NamedCompound (org.jabref.gui.undo.NamedCompound)1 BibEntry (org.jabref.model.entry.BibEntry)1 LinkedFile (org.jabref.model.entry.LinkedFile)1 Eprint (org.jabref.model.entry.identifier.Eprint)1