use of com.sun.star.text.XTextViewCursor in project languagetool by languagetool-org.
the class Main method getLanguage.
/**
* Checks the language under the cursor. Used for opening the configuration dialog.
* @return the language under the visible cursor
*/
@Nullable
private Language getLanguage() {
XComponent xComponent = getXComponent();
Locale charLocale;
XPropertySet xCursorProps;
try {
XModel model = UnoRuntime.queryInterface(XModel.class, xComponent);
XTextViewCursorSupplier xViewCursorSupplier = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, model.getCurrentController());
XTextViewCursor xCursor = xViewCursorSupplier.getViewCursor();
if (xCursor.isCollapsed()) {
// no text selection
xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xCursor);
} else {
// text is selected, need to create another cursor
// as multiple languages can occur here - we care only
// about character under the cursor, which might be wrong
// but it applies only to the checking dialog to be removed
xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xCursor.getText().createTextCursorByRange(xCursor.getStart()));
}
// Thus we check the text itself:
if (new KhmerDetector().isThisLanguage(xCursor.getText().getString())) {
return Languages.getLanguageForShortCode("km");
}
if (new TamilDetector().isThisLanguage(xCursor.getText().getString())) {
return Languages.getLanguageForShortCode("ta");
}
Object obj = xCursorProps.getPropertyValue("CharLocale");
if (obj == null) {
return Languages.getLanguageForShortCode("en-US");
}
charLocale = (Locale) obj;
boolean langIsSupported = false;
for (Language element : Languages.get()) {
if (charLocale.Language.equalsIgnoreCase(LIBREOFFICE_SPECIAL_LANGUAGE_TAG) && element.getShortCodeWithCountryAndVariant().equalsIgnoreCase(charLocale.Variant)) {
langIsSupported = true;
break;
}
if (element.getShortCode().equals(charLocale.Language)) {
langIsSupported = true;
break;
}
}
if (!langIsSupported) {
String message = Tools.i18n(MESSAGES, "language_not_supported", charLocale.Language);
JOptionPane.showMessageDialog(null, message);
return null;
}
} catch (Throwable t) {
showError(t);
return null;
}
return getLanguage(charLocale);
}
use of com.sun.star.text.XTextViewCursor in project jabref by JabRef.
the class OOBibBase method insertEntry.
/**
* This method inserts a cite marker in the text for the given BibEntry,
* and may refresh the bibliography.
* @param entries The entries to cite.
* @param database The database the entry belongs to.
* @param style The bibliography style we are using.
* @param inParenthesis Indicates whether it is an in-text citation or a citation in parenthesis.
* This is not relevant if numbered citations are used.
* @param withText Indicates whether this should be a normal citation (true) or an empty
* (invisible) citation (false).
* @param sync Indicates whether the reference list should be refreshed.
* @throws IllegalTypeException
* @throws PropertyExistException
* @throws NotRemoveableException
* @throws UnknownPropertyException
* @throws UndefinedCharacterFormatException
* @throws NoSuchElementException
* @throws WrappedTargetException
* @throws IOException
* @throws PropertyVetoException
* @throws CreationException
* @throws BibEntryNotFoundException
* @throws UndefinedParagraphFormatException
*/
public void insertEntry(List<BibEntry> entries, BibDatabase database, List<BibDatabase> allBases, OOBibStyle style, boolean inParenthesis, boolean withText, String pageInfo, boolean sync) throws IllegalArgumentException, UnknownPropertyException, NotRemoveableException, PropertyExistException, IllegalTypeException, UndefinedCharacterFormatException, WrappedTargetException, NoSuchElementException, PropertyVetoException, IOException, CreationException, BibEntryNotFoundException, UndefinedParagraphFormatException {
try {
XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
if (entries.size() > 1) {
if (style.getBooleanCitProperty(OOBibStyle.MULTI_CITE_CHRONOLOGICAL)) {
entries.sort(yearAuthorTitleComparator);
} else {
entries.sort(entryComparator);
}
}
String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
// Insert bookmark:
String bName = getUniqueReferenceMarkName(keyString, withText ? inParenthesis ? OOBibBase.AUTHORYEAR_PAR : OOBibBase.AUTHORYEAR_INTEXT : OOBibBase.INVISIBLE_CIT);
// If we should store metadata for page info, do that now:
if (pageInfo != null) {
LOGGER.info("Storing page info: " + pageInfo);
setCustomProperty(bName, pageInfo);
}
xViewCursor.getText().insertString(xViewCursor, " ", false);
if (style.isFormatCitations()) {
XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xViewCursor);
String charStyle = style.getCitationCharacterFormat();
try {
xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
} catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
// Setting the character format failed, so we throw an exception that
// will result in an error message for the user. Before that,
// delete the space we inserted:
xViewCursor.goLeft((short) 1, true);
xViewCursor.setString("");
throw new UndefinedCharacterFormatException(charStyle);
}
}
xViewCursor.goLeft((short) 1, false);
Map<BibEntry, BibDatabase> databaseMap = new HashMap<>();
for (BibEntry entry : entries) {
databaseMap.put(entry, database);
}
String citeText = style.isNumberEntries() ? "-" : style.getCitationMarker(entries, databaseMap, inParenthesis, null, null);
insertReferenceMark(bName, citeText, xViewCursor, withText, style);
xViewCursor.collapseToEnd();
xViewCursor.goRight((short) 1, false);
XTextRange position = xViewCursor.getEnd();
if (sync) {
// To account for numbering and for uniqiefiers, we must refresh the cite markers:
updateSortedReferenceMarks();
refreshCiteMarkers(allBases, style);
// Insert it at the current position:
rebuildBibTextSection(allBases, style);
}
// Go back to the relevant position:
xViewCursor.gotoRange(position, false);
} catch (DisposedException ex) {
// or catch a DisposedException (which is in a OO JAR file).
throw new ConnectionLostException(ex.getMessage());
}
}
use of com.sun.star.text.XTextViewCursor in project zotero-libreoffice-integration by zotero.
the class Document method canInsertField.
public boolean canInsertField(String fieldType) throws Exception {
// first, check if cursor is in the bibliography (no sense offering to replace it)
XTextViewCursor selection = getSelection();
XTextSection section = (XTextSection) UnoRuntime.queryInterface(XTextSection.class, selection);
if (section != null) {
XNamed sectionNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, section);
String name = sectionNamed.getName();
for (String prefix : PREFIXES) {
if (name.contains(prefix)) {
return false;
}
}
}
// Also make sure that the cursor is not in any other place we can't insert a citation
String position = getRangePosition(selection);
if (position.equals("SwXTextFrame")) {
displayAlert("Citations in text frames will be formatted as if they appear at the end of the document.", 2, 0);
return true;
}
return (position.equals("SwXBodyText") || position.equals("SwXCell") || (!fieldType.equals("Bookmark") && position.equals("SwXFootnote")));
}
use of com.sun.star.text.XTextViewCursor 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.XTextViewCursor in project jabref by JabRef.
the class OOBibBase method getSortedReferenceMarks.
private List<String> getSortedReferenceMarks(final XNameAccess nameAccess) throws WrappedTargetException, NoSuchElementException {
XTextViewCursorSupplier cursorSupplier = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, mxDoc.getCurrentController());
XTextViewCursor viewCursor = cursorSupplier.getViewCursor();
XTextRange initialPos = viewCursor.getStart();
List<String> names = Arrays.asList(nameAccess.getElementNames());
List<Point> positions = new ArrayList<>(names.size());
for (String name : names) {
XTextContent textContent = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(name));
XTextRange range = textContent.getAnchor();
// Check if we are inside a footnote:
if (UnoRuntime.queryInterface(XFootnote.class, range.getText()) != null) {
// Find the linking footnote marker:
XFootnote footer = UnoRuntime.queryInterface(XFootnote.class, range.getText());
// The footnote's anchor gives the correct position in the text:
range = footer.getAnchor();
}
positions.add(findPosition(viewCursor, range));
}
Set<ComparableMark> set = new TreeSet<>();
for (int i = 0; i < positions.size(); i++) {
set.add(new ComparableMark(names.get(i), positions.get(i)));
}
List<String> result = new ArrayList<>(set.size());
for (ComparableMark mark : set) {
result.add(mark.getName());
}
viewCursor.gotoRange(initialPos, false);
return result;
}
Aggregations