Search in sources :

Example 1 with XTextContent

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

the class OOBibBase method insertReferenceMark.

private void insertReferenceMark(String name, String citationText, XTextCursor position, boolean withText, OOBibStyle style) throws UnknownPropertyException, WrappedTargetException, PropertyVetoException, IllegalArgumentException, UndefinedCharacterFormatException, CreationException {
    // Check if there is "page info" stored for this citation. If so, insert it into
    // the citation text before inserting the citation:
    Optional<String> pageInfo = getCustomProperty(name);
    String citText;
    if ((pageInfo.isPresent()) && !pageInfo.get().isEmpty()) {
        citText = style.insertPageInfo(citationText, pageInfo.get());
    } else {
        citText = citationText;
    }
    Object bookmark;
    try {
        bookmark = mxDocFactory.createInstance("com.sun.star.text.ReferenceMark");
    } catch (Exception e) {
        throw new CreationException(e.getMessage());
    }
    // Name the reference
    XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, bookmark);
    xNamed.setName(name);
    if (withText) {
        position.setString(citText);
        XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, position);
        // Set language to [None]:
        xCursorProps.setPropertyValue("CharLocale", new Locale("zxx", "", ""));
        if (style.isFormatCitations()) {
            String charStyle = style.getCitationCharacterFormat();
            try {
                xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
            } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                throw new UndefinedCharacterFormatException(charStyle);
            }
        }
    } else {
        position.setString("");
    }
    // get XTextContent interface
    XTextContent xTextContent = UnoRuntime.queryInterface(XTextContent.class, bookmark);
    position.getText().insertTextContent(position, xTextContent, true);
    // Check if we should italicize the "et al." string in citations:
    boolean italicize = style.getBooleanCitProperty(OOBibStyle.ITALIC_ET_AL);
    if (italicize) {
        String etAlString = style.getStringCitProperty(OOBibStyle.ET_AL_STRING);
        int index = citText.indexOf(etAlString);
        if (index >= 0) {
            italicizeOrBold(position, true, index, index + etAlString.length());
        }
    }
    position.collapseToEnd();
}
Also used : Locale(com.sun.star.lang.Locale) XNamed(com.sun.star.container.XNamed) WrappedTargetException(com.sun.star.lang.WrappedTargetException) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) 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) Point(com.sun.star.awt.Point) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) XTextContent(com.sun.star.text.XTextContent) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Example 2 with XTextContent

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

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

the class OOBibBase method removeReferenceMark.

private void removeReferenceMark(String name) throws NoSuchElementException, WrappedTargetException {
    XNameAccess xReferenceMarks = getReferenceMarks();
    if (xReferenceMarks.hasByName(name)) {
        Object referenceMark = xReferenceMarks.getByName(name);
        XTextContent bookmark = UnoRuntime.queryInterface(XTextContent.class, referenceMark);
        text.removeTextContent(bookmark);
    }
}
Also used : XTextContent(com.sun.star.text.XTextContent) XNameAccess(com.sun.star.container.XNameAccess)

Example 4 with XTextContent

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

the class OOBibBase method insertBookMark.

private XTextContent insertBookMark(String name, XTextCursor position) throws IllegalArgumentException, CreationException {
    Object bookmark;
    try {
        bookmark = mxDocFactory.createInstance("com.sun.star.text.Bookmark");
    } catch (Exception e) {
        throw new CreationException(e.getMessage());
    }
    // name the bookmark
    XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, bookmark);
    xNamed.setName(name);
    // get XTextContent interface
    XTextContent xTextContent = UnoRuntime.queryInterface(XTextContent.class, bookmark);
    // insert bookmark at the end of the document
    // instead of mxDocText.getEnd you could use a text cursor's XTextRange interface or any XTextRange
    text.insertTextContent(position, xTextContent, true);
    position.collapseToEnd();
    return xTextContent;
}
Also used : XNamed(com.sun.star.container.XNamed) XTextContent(com.sun.star.text.XTextContent) 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 5 with XTextContent

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

the class OOBibBase method getCitationContext.

public String getCitationContext(XNameAccess nameAccess, String refMarkName, int charBefore, int charAfter, boolean htmlMarkup) throws NoSuchElementException, WrappedTargetException {
    Object referenceMark = nameAccess.getByName(refMarkName);
    XTextContent bookmark = UnoRuntime.queryInterface(XTextContent.class, referenceMark);
    XTextCursor cursor = bookmark.getAnchor().getText().createTextCursorByRange(bookmark.getAnchor());
    String citPart = cursor.getString();
    int flex = 8;
    for (int i = 0; i < charBefore; i++) {
        try {
            cursor.goLeft((short) 1, true);
            if ((i >= (charBefore - flex)) && Character.isWhitespace(cursor.getString().charAt(0))) {
                break;
            }
        } catch (IndexOutOfBoundsException ex) {
            LOGGER.warn("Problem going left", ex);
        }
    }
    int length = cursor.getString().length();
    int added = length - citPart.length();
    cursor.collapseToStart();
    for (int i = 0; i < (charAfter + length); i++) {
        try {
            cursor.goRight((short) 1, true);
            if (i >= ((charAfter + length) - flex)) {
                String strNow = cursor.getString();
                if (Character.isWhitespace(strNow.charAt(strNow.length() - 1))) {
                    break;
                }
            }
        } catch (IndexOutOfBoundsException ex) {
            LOGGER.warn("Problem going right", ex);
        }
    }
    String result = cursor.getString();
    if (htmlMarkup) {
        result = result.substring(0, added) + "<b>" + citPart + "</b>" + result.substring(length);
    }
    return result.trim();
}
Also used : XTextContent(com.sun.star.text.XTextContent) XTextCursor(com.sun.star.text.XTextCursor) Point(com.sun.star.awt.Point)

Aggregations

XTextContent (com.sun.star.text.XTextContent)8 Point (com.sun.star.awt.Point)4 PropertyVetoException (com.sun.star.beans.PropertyVetoException)4 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)4 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)4 WrappedTargetException (com.sun.star.lang.WrappedTargetException)4 IllegalTypeException (com.sun.star.beans.IllegalTypeException)3 NotRemoveableException (com.sun.star.beans.NotRemoveableException)3 PropertyExistException (com.sun.star.beans.PropertyExistException)3 BootstrapException (com.sun.star.comp.helper.BootstrapException)3 NoSuchElementException (com.sun.star.container.NoSuchElementException)3 XNameAccess (com.sun.star.container.XNameAccess)3 XNamed (com.sun.star.container.XNamed)3 DisposedException (com.sun.star.lang.DisposedException)3 XTextCursor (com.sun.star.text.XTextCursor)3 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 MalformedURLException (java.net.MalformedURLException)3 UndefinedParagraphFormatException (org.jabref.logic.openoffice.UndefinedParagraphFormatException)3 XPropertySet (com.sun.star.beans.XPropertySet)2