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