use of com.sun.star.text.XTextCursor in project jabref by JabRef.
the class OOBibBase method combineCiteMarkers.
public void combineCiteMarkers(List<BibDatabase> databases, OOBibStyle style) throws IOException, WrappedTargetException, NoSuchElementException, IllegalArgumentException, UndefinedCharacterFormatException, UnknownPropertyException, PropertyVetoException, CreationException, BibEntryNotFoundException {
XNameAccess nameAccess = getReferenceMarks();
// TODO: doesn't work for citations in footnotes/tables
List<String> names = getSortedReferenceMarks(nameAccess);
final XTextRangeCompare compare = UnoRuntime.queryInterface(XTextRangeCompare.class, text);
int piv = 0;
boolean madeModifications = false;
while (piv < (names.size() - 1)) {
XTextRange range1 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv))).getAnchor().getEnd();
XTextRange range2 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv + 1))).getAnchor().getStart();
if (range1.getText() != range2.getText()) {
piv++;
continue;
}
XTextCursor mxDocCursor = range1.getText().createTextCursorByRange(range1);
mxDocCursor.goRight((short) 1, true);
boolean couldExpand = true;
while (couldExpand && (compare.compareRegionEnds(mxDocCursor, range2) > 0)) {
couldExpand = mxDocCursor.goRight((short) 1, true);
}
String cursorText = mxDocCursor.getString();
// Check if the string contains no line breaks and only whitespace:
if ((cursorText.indexOf('\n') == -1) && cursorText.trim().isEmpty()) {
// marks are removed, preventing damage to the user's document:
if (style.isFormatCitations()) {
XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, mxDocCursor);
String charStyle = style.getCitationCharacterFormat();
try {
xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
} catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
// will result in an error message for the user:
throw new UndefinedCharacterFormatException(charStyle);
}
}
List<String> keys = parseRefMarkName(names.get(piv));
keys.addAll(parseRefMarkName(names.get(piv + 1)));
removeReferenceMark(names.get(piv));
removeReferenceMark(names.get(piv + 1));
List<BibEntry> entries = new ArrayList<>();
for (String key : keys) {
for (BibDatabase database : databases) {
Optional<BibEntry> entry = database.getEntryByKey(key);
if (entry.isPresent()) {
entries.add(entry.get());
break;
}
}
}
Collections.sort(entries, new FieldComparator(FieldName.YEAR));
String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
// Insert bookmark:
String bName = getUniqueReferenceMarkName(keyString, OOBibBase.AUTHORYEAR_PAR);
insertReferenceMark(bName, "tmp", mxDocCursor, true, style);
names.set(piv + 1, bName);
madeModifications = true;
}
piv++;
}
if (madeModifications) {
updateSortedReferenceMarks();
refreshCiteMarkers(databases, style);
}
}
use of com.sun.star.text.XTextCursor in project jabref by JabRef.
the class OOBibBase method italicizeOrBold.
private void italicizeOrBold(XTextCursor position, boolean italicize, int start, int end) throws UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException {
XTextRange range = position.getStart();
XTextCursor cursor = position.getText().createTextCursorByRange(range);
cursor.goRight((short) start, false);
cursor.goRight((short) (end - start), true);
XPropertySet xcp = UnoRuntime.queryInterface(XPropertySet.class, cursor);
if (italicize) {
xcp.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.ITALIC);
} else {
xcp.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD);
}
}
Aggregations