Search in sources :

Example 6 with FileListTableModel

use of org.jabref.gui.filelist.FileListTableModel 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)

Example 7 with FileListTableModel

use of org.jabref.gui.filelist.FileListTableModel in project jabref by JabRef.

the class EntryFromFileCreator method addFileInfo.

private void addFileInfo(BibEntry entry, File file) {
    Optional<ExternalFileType> fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt(externalFileType.getFieldName());
    List<Path> possibleFilePaths = JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    Path shortenedFileName = FileUtil.shortenFileName(file.toPath(), possibleFilePaths);
    FileListEntry fileListEntry = new FileListEntry("", shortenedFileName.toString(), fileType);
    FileListTableModel model = new FileListTableModel();
    model.addEntry(0, fileListEntry);
    entry.setField(FieldName.FILE, model.getStringRepresentation());
}
Also used : ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) Path(java.nio.file.Path) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) FileListEntry(org.jabref.gui.filelist.FileListEntry)

Example 8 with FileListTableModel

use of org.jabref.gui.filelist.FileListTableModel in project jabref by JabRef.

the class MainTableSelectionListener method showIconRightClickMenu.

/**
     * Process popup trigger events occurring on an icon cell in the table. Show a menu where the user can choose which
     * external resource to open for the entry. If no relevant external resources exist, let the normal popup trigger
     * handler do its thing instead.
     *
     * @param e The mouse event defining this popup trigger.
     * @param row The row where the event occurred.
     * @param column the MainTableColumn associated with this table cell.
     */
private void showIconRightClickMenu(MouseEvent e, int row, MainTableColumn column) {
    BibEntry entry = tableRows.get(row);
    JPopupMenu menu = new JPopupMenu();
    boolean showDefaultPopup = true;
    // field that can specify a list of links:
    if (!column.getBibtexFields().isEmpty()) {
        for (String field : column.getBibtexFields()) {
            if (FieldName.FILE.equals(field)) {
                // We use a FileListTableModel to parse the field content:
                FileListTableModel fileList = new FileListTableModel();
                entry.getField(field).ifPresent(fileList::setContent);
                for (int i = 0; i < fileList.getRowCount(); i++) {
                    FileListEntry flEntry = fileList.getEntry(i);
                    if (column.isFileFilter() && (!flEntry.getType().get().getName().equalsIgnoreCase(column.getColumnName()))) {
                        continue;
                    }
                    String description = flEntry.getDescription();
                    if ((description == null) || (description.trim().isEmpty())) {
                        description = flEntry.getLink();
                    }
                    menu.add(new ExternalFileMenuItem(panel.frame(), entry, description, flEntry.getLink(), flEntry.getType().get().getIcon(), panel.getBibDatabaseContext(), flEntry.getType()));
                    showDefaultPopup = false;
                }
            } else {
                if (SpecialField.isSpecialField(column.getColumnName())) {
                    // full pop should be shown as left click already shows short popup
                    showDefaultPopup = true;
                } else {
                    Optional<String> content = entry.getField(field);
                    if (content.isPresent()) {
                        Icon icon;
                        JLabel iconLabel = GUIGlobals.getTableIcon(field);
                        if (iconLabel == null) {
                            icon = IconTheme.JabRefIcon.FILE.getIcon();
                        } else {
                            icon = iconLabel.getIcon();
                        }
                        menu.add(new ExternalFileMenuItem(panel.frame(), entry, content.get(), content.get(), icon, panel.getBibDatabaseContext(), field));
                        if (field.equals(FieldName.DOI)) {
                            menu.add(new CopyDoiUrlAction(content.get()));
                        }
                        showDefaultPopup = false;
                    }
                }
            }
        }
        if (showDefaultPopup) {
            processPopupTrigger(e, row);
        } else {
            menu.show(table, e.getX(), e.getY());
        }
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) ExternalFileMenuItem(org.jabref.gui.externalfiletype.ExternalFileMenuItem) FileListEntry(org.jabref.gui.filelist.FileListEntry) JLabel(javax.swing.JLabel) JPopupMenu(javax.swing.JPopupMenu) CopyDoiUrlAction(org.jabref.gui.actions.CopyDoiUrlAction) Icon(javax.swing.Icon)

Example 9 with FileListTableModel

use of org.jabref.gui.filelist.FileListTableModel in project jabref by JabRef.

the class PdfImporter method doXMPImport.

private void doXMPImport(String fileName, List<BibEntry> res) {
    List<BibEntry> localRes = new ArrayList<>();
    PdfXmpImporter importer = new PdfXmpImporter(Globals.prefs.getXMPPreferences());
    Path filePath = Paths.get(fileName);
    ParserResult result = importer.importDatabase(filePath, Globals.prefs.getDefaultEncoding());
    if (result.hasWarnings()) {
        frame.showMessage(result.getErrorMessage());
    }
    localRes.addAll(result.getDatabase().getEntries());
    BibEntry entry;
    if (localRes.isEmpty()) {
        // import failed -> generate default entry
        LOGGER.info("Import failed");
        createNewBlankEntry(fileName).ifPresent(res::add);
        return;
    }
    // only one entry is imported
    entry = localRes.get(0);
    // insert entry to database and link file
    panel.getDatabase().insertEntry(entry);
    panel.markBaseChanged();
    FileListTableModel tm = new FileListTableModel();
    Path toLink = Paths.get(fileName);
    // Get a list of file directories:
    List<Path> dirsS = panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    tm.addEntry(0, new FileListEntry(toLink.getFileName().toString(), FileUtil.shortenFileName(toLink, dirsS).toString(), ExternalFileTypes.getInstance().getExternalFileTypeByName("PDF")));
    entry.setField(FieldName.FILE, tm.getStringRepresentation());
    res.add(entry);
}
Also used : Path(java.nio.file.Path) BibEntry(org.jabref.model.entry.BibEntry) ParserResult(org.jabref.logic.importer.ParserResult) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) ArrayList(java.util.ArrayList) FileListEntry(org.jabref.gui.filelist.FileListEntry) PdfXmpImporter(org.jabref.logic.importer.fileformat.PdfXmpImporter)

Example 10 with FileListTableModel

use of org.jabref.gui.filelist.FileListTableModel in project jabref by JabRef.

the class MainTableSelectionListener method mouseClicked.

@Override
public void mouseClicked(MouseEvent e) {
    // First find the column on which the user has clicked.
    final int row = table.rowAtPoint(e.getPoint());
    // A double click on an entry should open the entry's editor.
    if (e.getClickCount() == 2) {
        BibEntry toShow = tableRows.get(row);
        editSignalled(toShow);
        return;
    }
    final int col = table.columnAtPoint(e.getPoint());
    // get the MainTableColumn which is currently visible at col
    int modelIndex = table.getColumnModel().getColumn(col).getModelIndex();
    MainTableColumn modelColumn = table.getMainTableColumn(modelIndex);
    // action will be taken when the button is released:
    if (OS.WINDOWS && (modelColumn.isIconColumn()) && (e.getButton() != MouseEvent.BUTTON1)) {
        return;
    }
    // Check if the clicked colum is a specialfield column
    if (modelColumn.isIconColumn() && (SpecialField.isSpecialField(modelColumn.getColumnName()))) {
        // handle specialfield
        handleSpecialFieldLeftClick(e, modelColumn.getColumnName());
    } else if (modelColumn.isIconColumn()) {
        // left click on icon field
        Object value = table.getValueAt(row, col);
        if (value == null) {
            // No icon here, so we do nothing.
            return;
        }
        final BibEntry entry = tableRows.get(row);
        final List<String> fieldNames = modelColumn.getBibtexFields();
        // Open it now. We do this in a thread, so the program won't freeze during the wait.
        JabRefExecutorService.INSTANCE.execute(() -> {
            panel.output(Localization.lang("External viewer called") + '.');
            // (is relevant for combinations such as "url/doi")
            for (String fieldName : fieldNames) {
                // Check if field is present, if not skip this field
                if (entry.hasField(fieldName)) {
                    String link = entry.getField(fieldName).get();
                    // field that can specify a list of links:
                    if (fieldName.equals(FieldName.FILE)) {
                        // We use a FileListTableModel to parse the field content:
                        FileListTableModel fileList = new FileListTableModel();
                        fileList.setContent(link);
                        FileListEntry flEntry = null;
                        // If there are one or more links of the correct type, open the first one:
                        if (modelColumn.isFileFilter()) {
                            for (int i = 0; i < fileList.getRowCount(); i++) {
                                if (fileList.getEntry(i).getType().toString().equals(modelColumn.getColumnName())) {
                                    flEntry = fileList.getEntry(i);
                                    break;
                                }
                            }
                        } else if (fileList.getRowCount() > 0) {
                            //If there are no file types specified open the first file
                            flEntry = fileList.getEntry(0);
                        }
                        if (flEntry != null) {
                            ExternalFileMenuItem item = new ExternalFileMenuItem(panel.frame(), entry, "", flEntry.getLink(), flEntry.getType().map(ExternalFileType::getIcon).orElse(null), panel.getBibDatabaseContext(), flEntry.getType());
                            item.doClick();
                        }
                    } else {
                        try {
                            JabRefDesktop.openExternalViewer(panel.getBibDatabaseContext(), link, fieldName);
                        } catch (IOException ex) {
                            panel.output(Localization.lang("Unable to open link."));
                            LOGGER.info("Unable to open link", ex);
                        }
                    }
                    // only open the first link
                    break;
                }
            }
        });
    } else if (modelColumn.getBibtexFields().contains(FieldName.CROSSREF)) {
        // Clicking on crossref column
        tableRows.get(row).getField(FieldName.CROSSREF).ifPresent(crossref -> panel.getDatabase().getEntryByKey(crossref).ifPresent(entry -> panel.highlightEntry(entry)));
    }
    panel.frame().updateEnabledState();
}
Also used : FocusListener(java.awt.event.FocusListener) KeyListener(java.awt.event.KeyListener) PreviewPanel(org.jabref.gui.PreviewPanel) FieldName(org.jabref.model.entry.FieldName) JabRefDesktop(org.jabref.gui.desktop.JabRefDesktop) IconTheme(org.jabref.gui.IconTheme) SpecialField(org.jabref.model.entry.specialfields.SpecialField) BasePanel(org.jabref.gui.BasePanel) SpecialFieldValue(org.jabref.model.entry.specialfields.SpecialFieldValue) SwingUtilities(javax.swing.SwingUtilities) SpecialFieldViewModel(org.jabref.gui.specialfields.SpecialFieldViewModel) Locale(java.util.Locale) SpecialFieldValueViewModel(org.jabref.gui.specialfields.SpecialFieldValueViewModel) Localization(org.jabref.logic.l10n.Localization) CopyDoiUrlAction(org.jabref.gui.actions.CopyDoiUrlAction) SpecialFieldMenuAction(org.jabref.gui.specialfields.SpecialFieldMenuAction) MouseListener(java.awt.event.MouseListener) Timer(javax.swing.Timer) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) BasePanelMode(org.jabref.gui.BasePanelMode) EventList(ca.odell.glazedlists.EventList) JPopupMenu(javax.swing.JPopupMenu) BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) IOException(java.io.IOException) Icon(javax.swing.Icon) JabRefGUI(org.jabref.JabRefGUI) EntryEditor(org.jabref.gui.entryeditor.EntryEditor) KeyEvent(java.awt.event.KeyEvent) MouseEvent(java.awt.event.MouseEvent) Globals(org.jabref.Globals) FileListEntry(org.jabref.gui.filelist.FileListEntry) RightClickMenu(org.jabref.gui.menus.RightClickMenu) ListEvent(ca.odell.glazedlists.event.ListEvent) Objects(java.util.Objects) List(java.util.List) GUIGlobals(org.jabref.gui.GUIGlobals) ListEventListener(ca.odell.glazedlists.event.ListEventListener) FocusEvent(java.awt.event.FocusEvent) JLabel(javax.swing.JLabel) OS(org.jabref.logic.util.OS) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) JabRefExecutorService(org.jabref.JabRefExecutorService) ExternalFileMenuItem(org.jabref.gui.externalfiletype.ExternalFileMenuItem) PreviewPreferences(org.jabref.preferences.PreviewPreferences) BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) ExternalFileMenuItem(org.jabref.gui.externalfiletype.ExternalFileMenuItem) FileListEntry(org.jabref.gui.filelist.FileListEntry) EventList(ca.odell.glazedlists.EventList) List(java.util.List) IOException(java.io.IOException)

Aggregations

FileListTableModel (org.jabref.gui.filelist.FileListTableModel)12 FileListEntry (org.jabref.gui.filelist.FileListEntry)9 BibEntry (org.jabref.model.entry.BibEntry)9 Optional (java.util.Optional)5 JLabel (javax.swing.JLabel)5 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)5 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)5 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 ExternalFileMenuItem (org.jabref.gui.externalfiletype.ExternalFileMenuItem)3 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)3 IOException (java.io.IOException)2 Icon (javax.swing.Icon)2 JPopupMenu (javax.swing.JPopupMenu)2 CopyDoiUrlAction (org.jabref.gui.actions.CopyDoiUrlAction)2 ExternalFileTypeEntryEditor (org.jabref.gui.externalfiletype.ExternalFileTypeEntryEditor)2 FileListEntryEditor (org.jabref.gui.filelist.FileListEntryEditor)2 EventList (ca.odell.glazedlists.EventList)1 ListEvent (ca.odell.glazedlists.event.ListEvent)1