Search in sources :

Example 1 with XTextRange

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

the class OOBibBase method insertEntry.

/**
     * This method inserts a cite marker in the text for the given BibEntry,
     * and may refresh the bibliography.
     * @param entries The entries to cite.
     * @param database The database the entry belongs to.
     * @param style The bibliography style we are using.
     * @param inParenthesis Indicates whether it is an in-text citation or a citation in parenthesis.
     *   This is not relevant if numbered citations are used.
     * @param withText Indicates whether this should be a normal citation (true) or an empty
     *   (invisible) citation (false).
     * @param sync Indicates whether the reference list should be refreshed.
     * @throws IllegalTypeException
     * @throws PropertyExistException
     * @throws NotRemoveableException
     * @throws UnknownPropertyException
     * @throws UndefinedCharacterFormatException
     * @throws NoSuchElementException
     * @throws WrappedTargetException
     * @throws IOException
     * @throws PropertyVetoException
     * @throws CreationException
     * @throws BibEntryNotFoundException
     * @throws UndefinedParagraphFormatException
     */
public void insertEntry(List<BibEntry> entries, BibDatabase database, List<BibDatabase> allBases, OOBibStyle style, boolean inParenthesis, boolean withText, String pageInfo, boolean sync) throws IllegalArgumentException, UnknownPropertyException, NotRemoveableException, PropertyExistException, IllegalTypeException, UndefinedCharacterFormatException, WrappedTargetException, NoSuchElementException, PropertyVetoException, IOException, CreationException, BibEntryNotFoundException, UndefinedParagraphFormatException {
    try {
        XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
        if (entries.size() > 1) {
            if (style.getBooleanCitProperty(OOBibStyle.MULTI_CITE_CHRONOLOGICAL)) {
                entries.sort(yearAuthorTitleComparator);
            } else {
                entries.sort(entryComparator);
            }
        }
        String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
        // Insert bookmark:
        String bName = getUniqueReferenceMarkName(keyString, withText ? inParenthesis ? OOBibBase.AUTHORYEAR_PAR : OOBibBase.AUTHORYEAR_INTEXT : OOBibBase.INVISIBLE_CIT);
        // If we should store metadata for page info, do that now:
        if (pageInfo != null) {
            LOGGER.info("Storing page info: " + pageInfo);
            setCustomProperty(bName, pageInfo);
        }
        xViewCursor.getText().insertString(xViewCursor, " ", false);
        if (style.isFormatCitations()) {
            XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xViewCursor);
            String charStyle = style.getCitationCharacterFormat();
            try {
                xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
            } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                // Setting the character format failed, so we throw an exception that
                // will result in an error message for the user. Before that,
                // delete the space we inserted:
                xViewCursor.goLeft((short) 1, true);
                xViewCursor.setString("");
                throw new UndefinedCharacterFormatException(charStyle);
            }
        }
        xViewCursor.goLeft((short) 1, false);
        Map<BibEntry, BibDatabase> databaseMap = new HashMap<>();
        for (BibEntry entry : entries) {
            databaseMap.put(entry, database);
        }
        String citeText = style.isNumberEntries() ? "-" : style.getCitationMarker(entries, databaseMap, inParenthesis, null, null);
        insertReferenceMark(bName, citeText, xViewCursor, withText, style);
        xViewCursor.collapseToEnd();
        xViewCursor.goRight((short) 1, false);
        XTextRange position = xViewCursor.getEnd();
        if (sync) {
            // To account for numbering and for uniqiefiers, we must refresh the cite markers:
            updateSortedReferenceMarks();
            refreshCiteMarkers(allBases, style);
            // Insert it at the current position:
            rebuildBibTextSection(allBases, style);
        }
        // Go back to the relevant position:
        xViewCursor.gotoRange(position, false);
    } catch (DisposedException ex) {
        // or catch a DisposedException (which is in a OO JAR file).
        throw new ConnectionLostException(ex.getMessage());
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) XTextRange(com.sun.star.text.XTextRange) WrappedTargetException(com.sun.star.lang.WrappedTargetException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) BibDatabase(org.jabref.model.database.BibDatabase) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) XTextViewCursor(com.sun.star.text.XTextViewCursor) DisposedException(com.sun.star.lang.DisposedException)

Example 2 with XTextRange

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

the class OOBibBase method getSortedReferenceMarks.

private List<String> getSortedReferenceMarks(final XNameAccess nameAccess) throws WrappedTargetException, NoSuchElementException {
    XTextViewCursorSupplier cursorSupplier = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, mxDoc.getCurrentController());
    XTextViewCursor viewCursor = cursorSupplier.getViewCursor();
    XTextRange initialPos = viewCursor.getStart();
    List<String> names = Arrays.asList(nameAccess.getElementNames());
    List<Point> positions = new ArrayList<>(names.size());
    for (String name : names) {
        XTextContent textContent = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(name));
        XTextRange range = textContent.getAnchor();
        // Check if we are inside a footnote:
        if (UnoRuntime.queryInterface(XFootnote.class, range.getText()) != null) {
            // Find the linking footnote marker:
            XFootnote footer = UnoRuntime.queryInterface(XFootnote.class, range.getText());
            // The footnote's anchor gives the correct position in the text:
            range = footer.getAnchor();
        }
        positions.add(findPosition(viewCursor, range));
    }
    Set<ComparableMark> set = new TreeSet<>();
    for (int i = 0; i < positions.size(); i++) {
        set.add(new ComparableMark(names.get(i), positions.get(i)));
    }
    List<String> result = new ArrayList<>(set.size());
    for (ComparableMark mark : set) {
        result.add(mark.getName());
    }
    viewCursor.gotoRange(initialPos, false);
    return result;
}
Also used : XTextRange(com.sun.star.text.XTextRange) ArrayList(java.util.ArrayList) Point(com.sun.star.awt.Point) Point(com.sun.star.awt.Point) XTextViewCursorSupplier(com.sun.star.text.XTextViewCursorSupplier) XFootnote(com.sun.star.text.XFootnote) XTextContent(com.sun.star.text.XTextContent) TreeSet(java.util.TreeSet) XTextViewCursor(com.sun.star.text.XTextViewCursor)

Example 3 with XTextRange

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

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

XTextRange (com.sun.star.text.XTextRange)4 XPropertySet (com.sun.star.beans.XPropertySet)3 Point (com.sun.star.awt.Point)2 PropertyVetoException (com.sun.star.beans.PropertyVetoException)2 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)2 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)2 WrappedTargetException (com.sun.star.lang.WrappedTargetException)2 XTextCursor (com.sun.star.text.XTextCursor)2 XTextViewCursor (com.sun.star.text.XTextViewCursor)2 ArrayList (java.util.ArrayList)2 BibDatabase (org.jabref.model.database.BibDatabase)2 BibEntry (org.jabref.model.entry.BibEntry)2 XNameAccess (com.sun.star.container.XNameAccess)1 DisposedException (com.sun.star.lang.DisposedException)1 XFootnote (com.sun.star.text.XFootnote)1 XTextContent (com.sun.star.text.XTextContent)1 XTextRangeCompare (com.sun.star.text.XTextRangeCompare)1 XTextViewCursorSupplier (com.sun.star.text.XTextViewCursorSupplier)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1