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();
}
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);
}
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);
}
}
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;
}
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();
}
Aggregations