Search in sources :

Example 1 with RankingFieldComparator

use of org.jabref.gui.util.comparator.RankingFieldComparator 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)

Aggregations

Point (java.awt.Point)1 Comparator (java.util.Comparator)1 FirstColumnComparator (org.jabref.gui.util.comparator.FirstColumnComparator)1 IconComparator (org.jabref.gui.util.comparator.IconComparator)1 RankingFieldComparator (org.jabref.gui.util.comparator.RankingFieldComparator)1 FieldComparator (org.jabref.logic.bibtex.comparator.FieldComparator)1