Search in sources :

Example 1 with XTextCursor

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

the class OOBibBase method createBibTextSection2.

private void createBibTextSection2(boolean end) throws IllegalArgumentException, CreationException {
    XTextCursor mxDocCursor = text.createTextCursor();
    if (end) {
        mxDocCursor.gotoEnd(false);
    }
    OOUtil.insertParagraphBreak(text, mxDocCursor);
    // Create a new TextSection from the document factory and access it's XNamed interface
    XNamed xChildNamed;
    try {
        xChildNamed = UnoRuntime.queryInterface(XNamed.class, mxDocFactory.createInstance("com.sun.star.text.TextSection"));
    } catch (Exception e) {
        throw new CreationException(e.getMessage());
    }
    // Set the new sections name to 'Child_Section'
    xChildNamed.setName(OOBibBase.BIB_SECTION_NAME);
    // Access the Child_Section's XTextContent interface and insert it into the document
    XTextContent xChildSection = UnoRuntime.queryInterface(XTextContent.class, xChildNamed);
    text.insertTextContent(mxDocCursor, xChildSection, false);
}
Also used : XNamed(com.sun.star.container.XNamed) XTextContent(com.sun.star.text.XTextContent) XTextCursor(com.sun.star.text.XTextCursor) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) PropertyVetoException(com.sun.star.beans.PropertyVetoException) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) IllegalTypeException(com.sun.star.beans.IllegalTypeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) WrappedTargetException(com.sun.star.lang.WrappedTargetException) BootstrapException(com.sun.star.comp.helper.BootstrapException) DisposedException(com.sun.star.lang.DisposedException) NoSuchElementException(com.sun.star.container.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) UndefinedParagraphFormatException(org.jabref.logic.openoffice.UndefinedParagraphFormatException) IOException(java.io.IOException) NotRemoveableException(com.sun.star.beans.NotRemoveableException) PropertyExistException(com.sun.star.beans.PropertyExistException)

Example 2 with XTextCursor

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

the class OOBibBase method clearBibTextSectionContent2.

private void clearBibTextSectionContent2() throws NoSuchElementException, WrappedTargetException, IllegalArgumentException, CreationException {
    // Check if the section exists:
    XTextSectionsSupplier supplier = UnoRuntime.queryInterface(XTextSectionsSupplier.class, mxDoc);
    if (supplier.getTextSections().hasByName(OOBibBase.BIB_SECTION_NAME)) {
        XTextSection section = (XTextSection) ((Any) supplier.getTextSections().getByName(OOBibBase.BIB_SECTION_NAME)).getObject();
        // Clear it:
        XTextCursor cursor = text.createTextCursorByRange(section.getAnchor());
        cursor.gotoRange(section.getAnchor(), false);
        cursor.setString("");
    } else {
        createBibTextSection2(atEnd);
    }
}
Also used : XTextSectionsSupplier(com.sun.star.text.XTextSectionsSupplier) XTextSection(com.sun.star.text.XTextSection) XTextCursor(com.sun.star.text.XTextCursor)

Example 3 with XTextCursor

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

the class OOBibBase method populateBibTextSection.

private void populateBibTextSection(Map<BibEntry, BibDatabase> entries, OOBibStyle style) throws NoSuchElementException, WrappedTargetException, PropertyVetoException, UnknownPropertyException, UndefinedParagraphFormatException, IllegalArgumentException, CreationException {
    XTextSectionsSupplier supplier = UnoRuntime.queryInterface(XTextSectionsSupplier.class, mxDoc);
    XTextSection section = (XTextSection) ((Any) supplier.getTextSections().getByName(OOBibBase.BIB_SECTION_NAME)).getObject();
    XTextCursor cursor = text.createTextCursorByRange(section.getAnchor());
    OOUtil.insertTextAtCurrentLocation(text, cursor, (String) style.getProperty(OOBibStyle.TITLE), (String) style.getProperty(OOBibStyle.REFERENCE_HEADER_PARAGRAPH_FORMAT));
    insertFullReferenceAtCursor(cursor, entries, style, (String) style.getProperty(OOBibStyle.REFERENCE_PARAGRAPH_FORMAT));
    insertBookMark(OOBibBase.BIB_SECTION_END_NAME, cursor);
}
Also used : XTextSectionsSupplier(com.sun.star.text.XTextSectionsSupplier) XTextSection(com.sun.star.text.XTextSection) XTextCursor(com.sun.star.text.XTextCursor)

Example 4 with XTextCursor

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

Example 5 with XTextCursor

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

the class ReferenceMark method getOutOfField.

private void getOutOfField() {
    try {
        XTextCursor cursor = doc.getSelection();
        if (cursor.isCollapsed() && textRangeCompare.compareRegionEnds(cursor, range) == 0) {
            cursor.gotoRange(range.getEnd(), false);
            XDispatchProvider dispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, doc.controller);
            XDispatchHelper dispatchHelper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, doc.factory.createInstance("com.sun.star.frame.DispatchHelper"));
            dispatchHelper.executeDispatch(dispatchProvider, ".uno:ResetAttributes", "", 0, new PropertyValue[] {});
        }
    } catch (Exception e) {
    }
}
Also used : XDispatchHelper(com.sun.star.frame.XDispatchHelper) XTextCursor(com.sun.star.text.XTextCursor) XDispatchProvider(com.sun.star.frame.XDispatchProvider) WrappedTargetException(com.sun.star.lang.WrappedTargetException) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

XTextCursor (com.sun.star.text.XTextCursor)15 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)4 XPropertySet (com.sun.star.beans.XPropertySet)4 WrappedTargetException (com.sun.star.lang.WrappedTargetException)4 Point (com.sun.star.awt.Point)3 PropertyVetoException (com.sun.star.beans.PropertyVetoException)3 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)3 XTextContent (com.sun.star.text.XTextContent)3 XNameAccess (com.sun.star.container.XNameAccess)2 XComponent (com.sun.star.lang.XComponent)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 XMultiPropertyStates (com.sun.star.beans.XMultiPropertyStates)1