Search in sources :

Example 6 with XTextCursor

use of com.sun.star.text.XTextCursor 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 7 with XTextCursor

use of com.sun.star.text.XTextCursor in project jabref by JabRef.

the class OOBibBase method italicizeOrBold.

private void italicizeOrBold(XTextCursor position, boolean italicize, int start, int end) throws UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException {
    XTextRange range = position.getStart();
    XTextCursor cursor = position.getText().createTextCursorByRange(range);
    cursor.goRight((short) start, false);
    cursor.goRight((short) (end - start), true);
    XPropertySet xcp = UnoRuntime.queryInterface(XPropertySet.class, cursor);
    if (italicize) {
        xcp.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.ITALIC);
    } else {
        xcp.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD);
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XTextRange(com.sun.star.text.XTextRange) XTextCursor(com.sun.star.text.XTextCursor)

Aggregations

XTextCursor (com.sun.star.text.XTextCursor)7 Point (com.sun.star.awt.Point)3 PropertyVetoException (com.sun.star.beans.PropertyVetoException)3 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)3 XPropertySet (com.sun.star.beans.XPropertySet)3 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)3 WrappedTargetException (com.sun.star.lang.WrappedTargetException)3 XTextContent (com.sun.star.text.XTextContent)3 XNameAccess (com.sun.star.container.XNameAccess)2 XTextRange (com.sun.star.text.XTextRange)2 XTextSection (com.sun.star.text.XTextSection)2 XTextSectionsSupplier (com.sun.star.text.XTextSectionsSupplier)2 ArrayList (java.util.ArrayList)2 BibDatabase (org.jabref.model.database.BibDatabase)2 BibEntry (org.jabref.model.entry.BibEntry)2 IllegalTypeException (com.sun.star.beans.IllegalTypeException)1 NotRemoveableException (com.sun.star.beans.NotRemoveableException)1 PropertyExistException (com.sun.star.beans.PropertyExistException)1 BootstrapException (com.sun.star.comp.helper.BootstrapException)1 NoSuchElementException (com.sun.star.container.NoSuchElementException)1