Search in sources :

Example 1 with XParagraphCursor

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

Example 2 with XParagraphCursor

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

the class OOUtil method insertOOFormattedTextAtCurrentLocation.

/**
     * Insert a text with formatting indicated by HTML-like tags, into a text at
     * the position given by a cursor.
     * @param text The text to insert in.
     * @param cursor The cursor giving the insert location.
     * @param lText The marked-up text to insert.
     * @param parStyle The name of the paragraph style to use.
     * @throws WrappedTargetException
     * @throws PropertyVetoException
     * @throws UnknownPropertyException
     * @throws IllegalArgumentException
     */
public static void insertOOFormattedTextAtCurrentLocation(XText text, XTextCursor cursor, String lText, String parStyle) throws UndefinedParagraphFormatException, UnknownPropertyException, PropertyVetoException, WrappedTargetException, IllegalArgumentException {
    XParagraphCursor parCursor = UnoRuntime.queryInterface(XParagraphCursor.class, cursor);
    XPropertySet props = UnoRuntime.queryInterface(XPropertySet.class, parCursor);
    try {
        props.setPropertyValue(PARA_STYLE_NAME, parStyle);
    } catch (IllegalArgumentException ex) {
        throw new UndefinedParagraphFormatException(parStyle);
    }
    List<Formatting> formatting = new ArrayList<>();
    // We need to extract formatting. Use a simple regexp search iteration:
    int piv = 0;
    Matcher m = OOUtil.HTML_TAG.matcher(lText);
    while (m.find()) {
        String currentSubstring = lText.substring(piv, m.start());
        if (!currentSubstring.isEmpty()) {
            OOUtil.insertTextAtCurrentLocation(text, cursor, currentSubstring, formatting);
        }
        String tag = m.group();
        // Handle tags:
        if ("<b>".equals(tag)) {
            formatting.add(Formatting.BOLD);
        } else if ("</b>".equals(tag)) {
            formatting.remove(Formatting.BOLD);
        } else if ("<i>".equals(tag) || "<em>".equals(tag)) {
            formatting.add(Formatting.ITALIC);
        } else if ("</i>".equals(tag) || "</em>".equals(tag)) {
            formatting.remove(Formatting.ITALIC);
        } else if ("<tt>".equals(tag)) {
            formatting.add(Formatting.MONOSPACE);
        } else if ("</tt>".equals(tag)) {
            formatting.remove(Formatting.MONOSPACE);
        } else if ("<smallcaps>".equals(tag)) {
            formatting.add(Formatting.SMALLCAPS);
        } else if ("</smallcaps>".equals(tag)) {
            formatting.remove(Formatting.SMALLCAPS);
        } else if ("<sup>".equals(tag)) {
            formatting.add(Formatting.SUPERSCRIPT);
        } else if ("</sup>".equals(tag)) {
            formatting.remove(Formatting.SUPERSCRIPT);
        } else if ("<sub>".equals(tag)) {
            formatting.add(Formatting.SUBSCRIPT);
        } else if ("</sub>".equals(tag)) {
            formatting.remove(Formatting.SUBSCRIPT);
        } else if ("<u>".equals(tag)) {
            formatting.add(Formatting.UNDERLINE);
        } else if ("</u>".equals(tag)) {
            formatting.remove(Formatting.UNDERLINE);
        } else if ("<s>".equals(tag)) {
            formatting.add(Formatting.STRIKEOUT);
        } else if ("</s>".equals(tag)) {
            formatting.remove(Formatting.STRIKEOUT);
        }
        piv = m.end();
    }
    if (piv < lText.length()) {
        OOUtil.insertTextAtCurrentLocation(text, cursor, lText.substring(piv), formatting);
    }
    cursor.collapseToEnd();
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) Matcher(java.util.regex.Matcher) XParagraphCursor(com.sun.star.text.XParagraphCursor) ArrayList(java.util.ArrayList) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Example 3 with XParagraphCursor

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

the class OOUtil method insertTextAtCurrentLocation.

public static void insertTextAtCurrentLocation(XText text, XTextCursor cursor, String string, String parStyle) throws WrappedTargetException, PropertyVetoException, UnknownPropertyException, UndefinedParagraphFormatException {
    text.insertString(cursor, string, true);
    XParagraphCursor parCursor = UnoRuntime.queryInterface(XParagraphCursor.class, cursor);
    // Access the property set of the cursor, and set the currently selected text
    // (which is the string we just inserted) to be bold
    XPropertySet props = UnoRuntime.queryInterface(XPropertySet.class, parCursor);
    try {
        props.setPropertyValue(PARA_STYLE_NAME, parStyle);
    } catch (IllegalArgumentException ex) {
        throw new UndefinedParagraphFormatException(parStyle);
    }
    cursor.collapseToEnd();
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XParagraphCursor(com.sun.star.text.XParagraphCursor) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Example 4 with XParagraphCursor

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

the class Document method insertField.

public ReferenceMark insertField(String fieldType, int noteType) throws Exception {
    // duplicate selection cursor
    XTextViewCursor selectionCursor = getSelection();
    XTextCursor rangeToInsert = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, selectionCursor.getText().createTextCursorByRange(selectionCursor));
    return insertMarkAtRange(fieldType, noteType, rangeToInsert, null, null);
}
Also used : XParagraphCursor(com.sun.star.text.XParagraphCursor) XTextCursor(com.sun.star.text.XTextCursor) XTextViewCursor(com.sun.star.text.XTextViewCursor)

Aggregations

XParagraphCursor (com.sun.star.text.XParagraphCursor)4 XPropertySet (com.sun.star.beans.XPropertySet)3 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)2 XTextViewCursor (com.sun.star.text.XTextViewCursor)2 XEnumeration (com.sun.star.container.XEnumeration)1 XEnumerationAccess (com.sun.star.container.XEnumerationAccess)1 XText (com.sun.star.text.XText)1 XTextCursor (com.sun.star.text.XTextCursor)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1