Search in sources :

Example 1 with FieldComparator

use of org.jabref.logic.bibtex.comparator.FieldComparator in project jabref by JabRef.

the class MainTable method setupComparatorChooser.

/**
     * This method sets up what Comparators are used for the various table columns.
     * The ComparatorChooser enables and disables such Comparators as the user clicks
     * columns, but this is where the Comparators are defined. Also, the ComparatorChooser
     * is initialized with the sort order defined in Preferences.
     */
private void setupComparatorChooser() {
    // First column:
    List<Comparator> comparators = comparatorChooser.getComparatorsForColumn(0);
    comparators.clear();
    comparators.add(new FirstColumnComparator(panel.getBibDatabaseContext()));
    for (int i = 1; i < tableFormat.getColumnCount(); i++) {
        MainTableColumn tableColumn = tableFormat.getTableColumn(i);
        comparators = comparatorChooser.getComparatorsForColumn(i);
        comparators.clear();
        if (SpecialField.RANKING.getFieldName().equals(tableColumn.getColumnName())) {
            comparators.add(new RankingFieldComparator());
        } else if (tableColumn.isIconColumn()) {
            comparators.add(new IconComparator(tableColumn.getBibtexFields()));
        } else {
            comparators = comparatorChooser.getComparatorsForColumn(i);
            comparators.clear();
            comparators.add(new FieldComparator(tableFormat.getColumnName(i).toLowerCase(Locale.ROOT)));
        }
    }
    // Set initial sort columns:
    // Default sort order:
    String[] sortFields = new String[] { Globals.prefs.get(JabRefPreferences.TABLE_PRIMARY_SORT_FIELD), Globals.prefs.get(JabRefPreferences.TABLE_SECONDARY_SORT_FIELD), Globals.prefs.get(JabRefPreferences.TABLE_TERTIARY_SORT_FIELD) };
    boolean[] sortDirections = new boolean[] { Globals.prefs.getBoolean(JabRefPreferences.TABLE_PRIMARY_SORT_DESCENDING), Globals.prefs.getBoolean(JabRefPreferences.TABLE_SECONDARY_SORT_DESCENDING), Globals.prefs.getBoolean(JabRefPreferences.TABLE_TERTIARY_SORT_DESCENDING) };
    // descending
    model.getSortedForUserDefinedTableColumnSorting().getReadWriteLock().writeLock().lock();
    try {
        for (int i = 0; i < sortFields.length; i++) {
            int index = -1;
            //                if (!sortFields[i].startsWith(MainTableFormat.ICON_COLUMN_PREFIX))
            if (sortFields[i].startsWith("iconcol:")) {
                for (int j = 0; j < tableFormat.getColumnCount(); j++) {
                    if (sortFields[i].equals(tableFormat.getColumnName(j))) {
                        index = j;
                        break;
                    }
                }
            } else {
                index = tableFormat.getColumnIndex(sortFields[i]);
            }
            if (index >= 0) {
                comparatorChooser.appendComparator(index, 0, sortDirections[i]);
            }
        }
    } finally {
        model.getSortedForUserDefinedTableColumnSorting().getReadWriteLock().writeLock().unlock();
    }
    // Add action listener so we can remember the sort order:
    comparatorChooser.addSortActionListener(e -> {
        List<String> fields = getCurrentSortFields();
        List<Boolean> order = getCurrentSortOrder();
        int count = Math.min(fields.size(), order.size());
        if (count >= 1) {
            Globals.prefs.put(JabRefPreferences.TABLE_PRIMARY_SORT_FIELD, fields.get(0));
            Globals.prefs.putBoolean(JabRefPreferences.TABLE_PRIMARY_SORT_DESCENDING, order.get(0));
        }
        if (count >= 2) {
            Globals.prefs.put(JabRefPreferences.TABLE_SECONDARY_SORT_FIELD, fields.get(1));
            Globals.prefs.putBoolean(JabRefPreferences.TABLE_SECONDARY_SORT_DESCENDING, order.get(1));
        } else {
            Globals.prefs.put(JabRefPreferences.TABLE_SECONDARY_SORT_FIELD, "");
            Globals.prefs.putBoolean(JabRefPreferences.TABLE_SECONDARY_SORT_DESCENDING, false);
        }
        if (count >= 3) {
            Globals.prefs.put(JabRefPreferences.TABLE_TERTIARY_SORT_FIELD, fields.get(2));
            Globals.prefs.putBoolean(JabRefPreferences.TABLE_TERTIARY_SORT_DESCENDING, order.get(2));
        } else {
            Globals.prefs.put(JabRefPreferences.TABLE_TERTIARY_SORT_FIELD, "");
            Globals.prefs.putBoolean(JabRefPreferences.TABLE_TERTIARY_SORT_DESCENDING, false);
        }
    });
}
Also used : IconComparator(org.jabref.gui.util.comparator.IconComparator) RankingFieldComparator(org.jabref.gui.util.comparator.RankingFieldComparator) FirstColumnComparator(org.jabref.gui.util.comparator.FirstColumnComparator) Point(java.awt.Point) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) IconComparator(org.jabref.gui.util.comparator.IconComparator) FirstColumnComparator(org.jabref.gui.util.comparator.FirstColumnComparator) RankingFieldComparator(org.jabref.gui.util.comparator.RankingFieldComparator) Comparator(java.util.Comparator) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) RankingFieldComparator(org.jabref.gui.util.comparator.RankingFieldComparator)

Example 2 with FieldComparator

use of org.jabref.logic.bibtex.comparator.FieldComparator in project jabref by JabRef.

the class BibDatabaseWriter method getSaveComparators.

private static List<Comparator<BibEntry>> getSaveComparators(SavePreferences preferences, MetaData metaData) {
    List<Comparator<BibEntry>> comparators = new ArrayList<>();
    Optional<SaveOrderConfig> saveOrder = getSaveOrder(preferences, metaData);
    // Take care, using CrossRefEntry-Comparator, that referred entries occur after referring
    // ones. This is a necessary requirement for BibTeX to be able to resolve referenced entries correctly.
    comparators.add(new CrossRefEntryComparator());
    if (!saveOrder.isPresent()) {
        // entries will be sorted based on their internal IDs
        comparators.add(new IdComparator());
    } else {
        // use configured sorting strategy
        comparators.add(new FieldComparator(saveOrder.get().sortCriteria[0]));
        comparators.add(new FieldComparator(saveOrder.get().sortCriteria[1]));
        comparators.add(new FieldComparator(saveOrder.get().sortCriteria[2]));
        comparators.add(new FieldComparator(BibEntry.KEY_FIELD));
    }
    return comparators;
}
Also used : CrossRefEntryComparator(org.jabref.logic.bibtex.comparator.CrossRefEntryComparator) IdComparator(org.jabref.logic.bibtex.comparator.IdComparator) ArrayList(java.util.ArrayList) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) CrossRefEntryComparator(org.jabref.logic.bibtex.comparator.CrossRefEntryComparator) IdComparator(org.jabref.logic.bibtex.comparator.IdComparator) BibtexStringComparator(org.jabref.logic.bibtex.comparator.BibtexStringComparator) Comparator(java.util.Comparator) SaveOrderConfig(org.jabref.model.metadata.SaveOrderConfig)

Example 3 with FieldComparator

use of org.jabref.logic.bibtex.comparator.FieldComparator in project jabref by JabRef.

the class ImportInspectionDialog method setupComparatorChooser.

private void setupComparatorChooser() {
    // First column:
    List<Comparator> comparators = comparatorChooser.getComparatorsForColumn(0);
    comparators.clear();
    comparators = comparatorChooser.getComparatorsForColumn(1);
    comparators.clear();
    // Icon columns:
    for (int i = 2; i < PAD; i++) {
        comparators = comparatorChooser.getComparatorsForColumn(i);
        comparators.clear();
        if (i == FILE_COL) {
            comparators.add(new IconComparator(Collections.singletonList(FieldName.FILE)));
        } else if (i == URL_COL) {
            comparators.add(new IconComparator(Collections.singletonList(FieldName.URL)));
        }
    }
    // Remaining columns:
    for (int i = PAD; i < (PAD + INSPECTION_FIELDS.size()); i++) {
        comparators = comparatorChooser.getComparatorsForColumn(i);
        comparators.clear();
        comparators.add(new FieldComparator(INSPECTION_FIELDS.get(i - PAD)));
    }
    sortedList.getReadWriteLock().writeLock().lock();
    try {
        comparatorChooser.appendComparator(PAD, 0, false);
    } finally {
        sortedList.getReadWriteLock().writeLock().unlock();
    }
}
Also used : IconComparator(org.jabref.gui.util.comparator.IconComparator) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) IconComparator(org.jabref.gui.util.comparator.IconComparator) Comparator(java.util.Comparator)

Example 4 with FieldComparator

use of org.jabref.logic.bibtex.comparator.FieldComparator in project jabref by JabRef.

the class OOBibBase method combineCiteMarkers.

public void combineCiteMarkers(List<BibDatabase> databases, OOBibStyle style) throws IOException, WrappedTargetException, NoSuchElementException, IllegalArgumentException, UndefinedCharacterFormatException, UnknownPropertyException, PropertyVetoException, CreationException, BibEntryNotFoundException {
    XNameAccess nameAccess = getReferenceMarks();
    // TODO: doesn't work for citations in footnotes/tables
    List<String> names = getSortedReferenceMarks(nameAccess);
    final XTextRangeCompare compare = UnoRuntime.queryInterface(XTextRangeCompare.class, text);
    int piv = 0;
    boolean madeModifications = false;
    while (piv < (names.size() - 1)) {
        XTextRange range1 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv))).getAnchor().getEnd();
        XTextRange range2 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv + 1))).getAnchor().getStart();
        if (range1.getText() != range2.getText()) {
            piv++;
            continue;
        }
        XTextCursor mxDocCursor = range1.getText().createTextCursorByRange(range1);
        mxDocCursor.goRight((short) 1, true);
        boolean couldExpand = true;
        while (couldExpand && (compare.compareRegionEnds(mxDocCursor, range2) > 0)) {
            couldExpand = mxDocCursor.goRight((short) 1, true);
        }
        String cursorText = mxDocCursor.getString();
        // Check if the string contains no line breaks and only whitespace:
        if ((cursorText.indexOf('\n') == -1) && cursorText.trim().isEmpty()) {
            // marks are removed, preventing damage to the user's document:
            if (style.isFormatCitations()) {
                XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, mxDocCursor);
                String charStyle = style.getCitationCharacterFormat();
                try {
                    xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
                } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                    // will result in an error message for the user:
                    throw new UndefinedCharacterFormatException(charStyle);
                }
            }
            List<String> keys = parseRefMarkName(names.get(piv));
            keys.addAll(parseRefMarkName(names.get(piv + 1)));
            removeReferenceMark(names.get(piv));
            removeReferenceMark(names.get(piv + 1));
            List<BibEntry> entries = new ArrayList<>();
            for (String key : keys) {
                for (BibDatabase database : databases) {
                    Optional<BibEntry> entry = database.getEntryByKey(key);
                    if (entry.isPresent()) {
                        entries.add(entry.get());
                        break;
                    }
                }
            }
            Collections.sort(entries, new FieldComparator(FieldName.YEAR));
            String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
            // Insert bookmark:
            String bName = getUniqueReferenceMarkName(keyString, OOBibBase.AUTHORYEAR_PAR);
            insertReferenceMark(bName, "tmp", mxDocCursor, true, style);
            names.set(piv + 1, bName);
            madeModifications = true;
        }
        piv++;
    }
    if (madeModifications) {
        updateSortedReferenceMarks();
        refreshCiteMarkers(databases, style);
    }
}
Also used : XTextRange(com.sun.star.text.XTextRange) BibEntry(org.jabref.model.entry.BibEntry) WrappedTargetException(com.sun.star.lang.WrappedTargetException) ArrayList(java.util.ArrayList) XNameAccess(com.sun.star.container.XNameAccess) XTextCursor(com.sun.star.text.XTextCursor) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) XTextRangeCompare(com.sun.star.text.XTextRangeCompare) Point(com.sun.star.awt.Point) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) BibDatabase(org.jabref.model.database.BibDatabase) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Example 5 with FieldComparator

use of org.jabref.logic.bibtex.comparator.FieldComparator in project jabref by JabRef.

the class SearchResultFrame method setupComparatorChooser.

/**
     * Set up the comparators for each column, so the user can modify sort order
     * by clicking the column labels.
     * @param comparatorChooser The comparator chooser controlling the sort order.
     */
private void setupComparatorChooser(TableComparatorChooser<BibEntry> comparatorChooser) {
    List<Comparator> comparators;
    // Icon columns:
    for (int i = 0; i < PAD; i++) {
        comparators = comparatorChooser.getComparatorsForColumn(i);
        comparators.clear();
        if (i == FILE_COL) {
            comparators.add(new IconComparator(Collections.singletonList(FieldName.FILE)));
        } else if (i == URL_COL) {
            comparators.add(new IconComparator(Collections.singletonList(FieldName.URL)));
        } else if (i == DATABASE_COL) {
            comparators.add((entry1, entry2) -> {
                String databaseTitle1 = entryHome.get(entry1).getTabTitle();
                String databaseTitle2 = entryHome.get(entry2).getTabTitle();
                return databaseTitle1.compareTo(databaseTitle2);
            });
        }
    }
    // Remaining columns:
    for (int i = PAD; i < (PAD + FIELDS.length); i++) {
        comparators = comparatorChooser.getComparatorsForColumn(i);
        comparators.clear();
        comparators.add(new FieldComparator(FIELDS[i - PAD]));
    }
    sortedEntries.getReadWriteLock().writeLock().lock();
    comparatorChooser.appendComparator(PAD, 0, false);
    sortedEntries.getReadWriteLock().writeLock().unlock();
}
Also used : IconComparator(org.jabref.gui.util.comparator.IconComparator) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) IconComparator(org.jabref.gui.util.comparator.IconComparator) EntryComparator(org.jabref.logic.bibtex.comparator.EntryComparator) Comparator(java.util.Comparator)

Aggregations

FieldComparator (org.jabref.logic.bibtex.comparator.FieldComparator)5 Comparator (java.util.Comparator)4 IconComparator (org.jabref.gui.util.comparator.IconComparator)3 ArrayList (java.util.ArrayList)2 Point (com.sun.star.awt.Point)1 PropertyVetoException (com.sun.star.beans.PropertyVetoException)1 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)1 XPropertySet (com.sun.star.beans.XPropertySet)1 XNameAccess (com.sun.star.container.XNameAccess)1 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)1 WrappedTargetException (com.sun.star.lang.WrappedTargetException)1 XTextCursor (com.sun.star.text.XTextCursor)1 XTextRange (com.sun.star.text.XTextRange)1 XTextRangeCompare (com.sun.star.text.XTextRangeCompare)1 Point (java.awt.Point)1 FirstColumnComparator (org.jabref.gui.util.comparator.FirstColumnComparator)1 RankingFieldComparator (org.jabref.gui.util.comparator.RankingFieldComparator)1 BibtexStringComparator (org.jabref.logic.bibtex.comparator.BibtexStringComparator)1 CrossRefEntryComparator (org.jabref.logic.bibtex.comparator.CrossRefEntryComparator)1 EntryComparator (org.jabref.logic.bibtex.comparator.EntryComparator)1