Search in sources :

Example 1 with XMultiPropertyStates

use of com.sun.star.beans.XMultiPropertyStates in project zotero-libreoffice-integration by zotero.

the class ReferenceMark method setText.

public void setText(String textString, boolean isRich) throws Exception {
    boolean isBibliography = getCode().startsWith(Document.BIBLIOGRAPHY_CODE);
    XTextCursor viewCursor = doc.getSelection();
    if (isBibliography) {
        prepareMultiline();
    }
    boolean viewCursorInField = false;
    try {
        if (textRangeCompare.compareRegionStarts(range, viewCursor) >= 0 && textRangeCompare.compareRegionEnds(range, viewCursor) <= 0) {
            viewCursorInField = true;
        }
    // One of these cursors is not in this text, so we're good
    } catch (com.sun.star.lang.IllegalArgumentException e) {
    }
    XTextCursor cursor = text.createTextCursorByRange(range);
    if (!isBibliography && range.getString().equals("")) {
        // One cannot simply overwrite an empty ReferenceMark
        text.removeTextContent(textContent);
    }
    range = cursor;
    if (!isBibliography) {
        // because of the text on either side of it
        if (isRich) {
            int previousLen = range.getString().length();
            text.insertControlCharacter(range, ControlCharacter.PARAGRAPH_BREAK, false);
            text.insertControlCharacter(range.getEnd(), ControlCharacter.PARAGRAPH_BREAK, false);
            cursor.collapseToStart();
            // Unless the cursor is already in the note
            if (viewCursorInField) {
                // LibreOffice crashes while inserting RTF if we don't move the viewCursor here.
                // Affects Ubuntu and maybe MacOS.
                // Don't ask me why it crashes though.
                viewCursor.gotoRange((XTextRange) cursor, false);
                viewCursor.goLeft((short) 1, false);
            }
            moveCursorRight(cursor, previousLen);
        }
    }
    XMultiPropertyStates rangePropStates = (XMultiPropertyStates) UnoRuntime.queryInterface(XMultiPropertyStates.class, cursor);
    rangePropStates.setPropertiesToDefault(PROPERTIES_CHANGE_TO_DEFAULT);
    if (isRich) {
        XPropertySet rangeProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, range);
        if (isBibliography) {
            /*// Add a new line to the start of the bibliography so that the paragraph format
				// for the first entry will be correct. Without the new line, when converting
				// citation styles, the first entry of the bibliography will keep the same paragraph
				// formatting as the previous citation style
				textString = "{\\rtf\\\n" + textString.substring(6);*/
            insertRTF(textString, cursor);
            rangeProps.setPropertyValue("ParaStyleName", "Bibliography 1");
            // Remove the new line from the bibliography (added above). Have to remove the
            // new line before the textSection and then adjust the range so the new line
            // starting the textSection is outside of the range so that the
            // paragraph formatting of the first entry remains unchanged. Also remove the
            // extra new line at the end of the textSection.
            String rangeString = cursor.getString();
            int previousLen = rangeString.length();
            int removeLastNewLine = 0;
            if (rangeString.codePointAt(previousLen - 1) == 10) {
                removeLastNewLine = 1;
                XTextCursor dupRange = text.createTextCursorByRange(range);
                dupRange.collapseToEnd();
                dupRange.goLeft((short) 1, true);
                dupRange.setString("");
            }
            cursor.collapseToStart();
            moveCursorRight(cursor, previousLen - removeLastNewLine);
        } else {
            String oldParaStyle = (String) rangeProps.getPropertyValue("ParaStyleName");
            insertRTF(textString, cursor);
            // Inserting RTF in LibreOffice 4 resets the style to the document default, so
            // we set it back to whatever it was before we inserted the RTF. However,
            // setting the paragraph style will reset superscript and other character
            // properties specified by the style, so we need to explicitly preserve these.
            Object[] oldPropertyValues = new Object[PROPERTIES_CHANGE_TO_DEFAULT.length];
            for (int i = 0; i < PROPERTIES_CHANGE_TO_DEFAULT.length; i++) {
                Object result = rangeProps.getPropertyValue(PROPERTIES_CHANGE_TO_DEFAULT[i]);
                oldPropertyValues[i] = result instanceof Any ? ((Any) result).getObject() : result;
            }
            rangeProps.setPropertyValue("ParaStyleName", oldParaStyle);
            for (int i = 0; i < PROPERTIES_CHANGE_TO_DEFAULT.length; i++) {
                if (oldPropertyValues[i] != null) {
                    rangeProps.setPropertyValue(PROPERTIES_CHANGE_TO_DEFAULT[i], oldPropertyValues[i]);
                }
            }
        }
    } else {
        range.setString(textString);
    }
    reattachMark();
    if (!isBibliography) {
        if (isRich) {
            // remove previously added paragraphs
            XTextCursor dupRange;
            dupRange = text.createTextCursorByRange(range);
            dupRange.collapseToEnd();
            dupRange.goRight((short) 1, true);
            String str = dupRange.getString();
            // getString() returns different newline characters for different OSes
            if (str.equals("\n") || str.equals("\r\n")) {
                dupRange.setString("");
            }
            dupRange.goLeft((short) 1, true);
            str = dupRange.getString();
            if (str.equals("\n") || str.equals("\r\n")) {
                dupRange.setString("");
            }
            if (viewCursorInField && !isNote) {
                // Restoring cursor position from crash-prevention jiggle
                viewCursor.gotoRange(dupRange, false);
                viewCursor.collapseToEnd();
            }
            dupRange = text.createTextCursorByRange(range);
            dupRange.collapseToStart();
            dupRange.goLeft((short) 1, true);
            dupRange.setString("");
        }
        getOutOfField();
    }
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XMultiPropertyStates(com.sun.star.beans.XMultiPropertyStates) XTextCursor(com.sun.star.text.XTextCursor) Any(com.sun.star.uno.Any)

Aggregations

XMultiPropertyStates (com.sun.star.beans.XMultiPropertyStates)1 XPropertySet (com.sun.star.beans.XPropertySet)1 XTextCursor (com.sun.star.text.XTextCursor)1 Any (com.sun.star.uno.Any)1