use of com.sun.star.text.XParagraphCursor in project zotero-libreoffice-integration by zotero.
the class Document method cursorInField.
public ReferenceMark cursorInField(String fieldType) throws Exception {
// create two text cursors containing the selection
XTextViewCursor selectionCursor = getSelection();
XText text = selectionCursor.getText();
XParagraphCursor paragraphCursor1 = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, text.createTextCursorByRange(selectionCursor));
XParagraphCursor paragraphCursor2 = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, text.createTextCursorByRange(selectionCursor));
// extend one cursor to the beginning of the paragraph and one to the end
paragraphCursor1.goLeft((short) 1, false);
paragraphCursor1.gotoStartOfParagraph(true);
paragraphCursor2.gotoEndOfParagraph(true);
// get enumerator corresponding to first cursor
XEnumerationAccess enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, paragraphCursor1);
Object nextElement = enumeratorAccess.createEnumeration().nextElement();
enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, nextElement);
XEnumeration enumerator = enumeratorAccess.createEnumeration();
while (enumerator.hasMoreElements()) {
// look for a ReferenceMark or Bookmark
XPropertySet textProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, enumerator.nextElement());
String textPropertyType = (String) textProperties.getPropertyValue("TextPortionType");
if (textPropertyType.equals(fieldType)) {
ReferenceMark mark = mMarkManager.getMark(textProperties.getPropertyValue(fieldType), fieldType);
if (mark != null) {
// check second enumerator for the same field
enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, paragraphCursor2);
nextElement = enumeratorAccess.createEnumeration().nextElement();
enumeratorAccess = (XEnumerationAccess) UnoRuntime.queryInterface(XEnumerationAccess.class, nextElement);
XEnumeration enumerator2 = enumeratorAccess.createEnumeration();
while (enumerator2.hasMoreElements()) {
textProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, enumerator2.nextElement());
textPropertyType = (String) textProperties.getPropertyValue("TextPortionType");
if (textPropertyType.equals(fieldType)) {
if (mark == mMarkManager.getMark(textProperties.getPropertyValue(fieldType), fieldType)) {
return mark;
}
}
}
}
}
}
return null;
}
use of com.sun.star.text.XParagraphCursor in project jabref by JabRef.
the class OOUtil method insertOOFormattedTextAtCurrentLocation.
/**
* Insert a text with formatting indicated by HTML-like tags, into a text at
* the position given by a cursor.
* @param text The text to insert in.
* @param cursor The cursor giving the insert location.
* @param lText The marked-up text to insert.
* @param parStyle The name of the paragraph style to use.
* @throws WrappedTargetException
* @throws PropertyVetoException
* @throws UnknownPropertyException
* @throws IllegalArgumentException
*/
public static void insertOOFormattedTextAtCurrentLocation(XText text, XTextCursor cursor, String lText, String parStyle) throws UndefinedParagraphFormatException, UnknownPropertyException, PropertyVetoException, WrappedTargetException, IllegalArgumentException {
XParagraphCursor parCursor = UnoRuntime.queryInterface(XParagraphCursor.class, cursor);
XPropertySet props = UnoRuntime.queryInterface(XPropertySet.class, parCursor);
try {
props.setPropertyValue(PARA_STYLE_NAME, parStyle);
} catch (IllegalArgumentException ex) {
throw new UndefinedParagraphFormatException(parStyle);
}
List<Formatting> formatting = new ArrayList<>();
// We need to extract formatting. Use a simple regexp search iteration:
int piv = 0;
Matcher m = OOUtil.HTML_TAG.matcher(lText);
while (m.find()) {
String currentSubstring = lText.substring(piv, m.start());
if (!currentSubstring.isEmpty()) {
OOUtil.insertTextAtCurrentLocation(text, cursor, currentSubstring, formatting);
}
String tag = m.group();
// Handle tags:
if ("<b>".equals(tag)) {
formatting.add(Formatting.BOLD);
} else if ("</b>".equals(tag)) {
formatting.remove(Formatting.BOLD);
} else if ("<i>".equals(tag) || "<em>".equals(tag)) {
formatting.add(Formatting.ITALIC);
} else if ("</i>".equals(tag) || "</em>".equals(tag)) {
formatting.remove(Formatting.ITALIC);
} else if ("<tt>".equals(tag)) {
formatting.add(Formatting.MONOSPACE);
} else if ("</tt>".equals(tag)) {
formatting.remove(Formatting.MONOSPACE);
} else if ("<smallcaps>".equals(tag)) {
formatting.add(Formatting.SMALLCAPS);
} else if ("</smallcaps>".equals(tag)) {
formatting.remove(Formatting.SMALLCAPS);
} else if ("<sup>".equals(tag)) {
formatting.add(Formatting.SUPERSCRIPT);
} else if ("</sup>".equals(tag)) {
formatting.remove(Formatting.SUPERSCRIPT);
} else if ("<sub>".equals(tag)) {
formatting.add(Formatting.SUBSCRIPT);
} else if ("</sub>".equals(tag)) {
formatting.remove(Formatting.SUBSCRIPT);
} else if ("<u>".equals(tag)) {
formatting.add(Formatting.UNDERLINE);
} else if ("</u>".equals(tag)) {
formatting.remove(Formatting.UNDERLINE);
} else if ("<s>".equals(tag)) {
formatting.add(Formatting.STRIKEOUT);
} else if ("</s>".equals(tag)) {
formatting.remove(Formatting.STRIKEOUT);
}
piv = m.end();
}
if (piv < lText.length()) {
OOUtil.insertTextAtCurrentLocation(text, cursor, lText.substring(piv), formatting);
}
cursor.collapseToEnd();
}
use of com.sun.star.text.XParagraphCursor in project jabref by JabRef.
the class OOUtil method insertTextAtCurrentLocation.
public static void insertTextAtCurrentLocation(XText text, XTextCursor cursor, String string, String parStyle) throws WrappedTargetException, PropertyVetoException, UnknownPropertyException, UndefinedParagraphFormatException {
text.insertString(cursor, string, true);
XParagraphCursor parCursor = UnoRuntime.queryInterface(XParagraphCursor.class, cursor);
// Access the property set of the cursor, and set the currently selected text
// (which is the string we just inserted) to be bold
XPropertySet props = UnoRuntime.queryInterface(XPropertySet.class, parCursor);
try {
props.setPropertyValue(PARA_STYLE_NAME, parStyle);
} catch (IllegalArgumentException ex) {
throw new UndefinedParagraphFormatException(parStyle);
}
cursor.collapseToEnd();
}
use of com.sun.star.text.XParagraphCursor in project zotero-libreoffice-integration by zotero.
the class Document method insertField.
public ReferenceMark insertField(String fieldType, int noteType) throws Exception {
// duplicate selection cursor
XTextViewCursor selectionCursor = getSelection();
XTextCursor rangeToInsert = (XParagraphCursor) UnoRuntime.queryInterface(XParagraphCursor.class, selectionCursor.getText().createTextCursorByRange(selectionCursor));
return insertMarkAtRange(fieldType, noteType, rangeToInsert, null, null);
}
Aggregations