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);
}
});
}
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;
}
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();
}
}
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);
}
}
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();
}
Aggregations