Search in sources :

Example 1 with XText

use of com.sun.star.text.XText in project zotero-libreoffice-integration by zotero.

the class Document method cursorInField.

public ReferenceMark cursorInField(String fieldType) throws Exception {
    // create two text cursors containing the selection
    XTextViewCursor selectionCursor = getSelection();
    XText text = selectionCursor.getText();
    XParagraphCursor paragraphCursor1 = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, text.createTextCursorByRange(selectionCursor));
    XParagraphCursor paragraphCursor2 = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, text.createTextCursorByRange(selectionCursor));
    // extend one cursor to the beginning of the paragraph and one to the end
    paragraphCursor1.goLeft((short) 1, false);
    paragraphCursor1.gotoStartOfParagraph(true);
    paragraphCursor2.gotoEndOfParagraph(true);
    // get enumerator corresponding to first cursor
    XEnumerationAccess enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, paragraphCursor1);
    Object nextElement = enumeratorAccess.createEnumeration().nextElement();
    enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, nextElement);
    XEnumeration enumerator = enumeratorAccess.createEnumeration();
    while (enumerator.hasMoreElements()) {
        // look for a ReferenceMark or Bookmark
        XPropertySet textProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, enumerator.nextElement());
        String textPropertyType = (String) textProperties.getPropertyValue("TextPortionType");
        if (textPropertyType.equals(fieldType)) {
            ReferenceMark mark = mMarkManager.getMark(textProperties.getPropertyValue(fieldType), fieldType);
            if (mark != null) {
                // check second enumerator for the same field
                enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, paragraphCursor2);
                nextElement = enumeratorAccess.createEnumeration().nextElement();
                enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, nextElement);
                XEnumeration enumerator2 = enumeratorAccess.createEnumeration();
                while (enumerator2.hasMoreElements()) {
                    textProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, enumerator2.nextElement());
                    textPropertyType = (String) textProperties.getPropertyValue("TextPortionType");
                    if (textPropertyType.equals(fieldType)) {
                        if (mark == mMarkManager.getMark(textProperties.getPropertyValue(fieldType), fieldType)) {
                            return mark;
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XParagraphCursor(com.sun.star.text.XParagraphCursor) XText(com.sun.star.text.XText) XEnumeration(com.sun.star.container.XEnumeration) XTextViewCursor(com.sun.star.text.XTextViewCursor) XEnumerationAccess(com.sun.star.container.XEnumerationAccess)

Aggregations

XPropertySet (com.sun.star.beans.XPropertySet)1 XEnumeration (com.sun.star.container.XEnumeration)1 XEnumerationAccess (com.sun.star.container.XEnumerationAccess)1 XParagraphCursor (com.sun.star.text.XParagraphCursor)1 XText (com.sun.star.text.XText)1 XTextViewCursor (com.sun.star.text.XTextViewCursor)1