Search in sources :

Example 1 with XTextViewCursorSupplier

use of com.sun.star.text.XTextViewCursorSupplier 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);
}
Also used : XModel(com.sun.star.frame.XModel) XPropertySet(com.sun.star.beans.XPropertySet) XTextViewCursorSupplier(com.sun.star.text.XTextViewCursorSupplier) Language(org.languagetool.Language) XTextViewCursor(com.sun.star.text.XTextViewCursor) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XTextViewCursorSupplier

use of com.sun.star.text.XTextViewCursorSupplier 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;
}
Also used : XTextRange(com.sun.star.text.XTextRange) ArrayList(java.util.ArrayList) Point(com.sun.star.awt.Point) Point(com.sun.star.awt.Point) XTextViewCursorSupplier(com.sun.star.text.XTextViewCursorSupplier) XFootnote(com.sun.star.text.XFootnote) XTextContent(com.sun.star.text.XTextContent) TreeSet(java.util.TreeSet) XTextViewCursor(com.sun.star.text.XTextViewCursor)

Aggregations

XTextViewCursor (com.sun.star.text.XTextViewCursor)2 XTextViewCursorSupplier (com.sun.star.text.XTextViewCursorSupplier)2 Point (com.sun.star.awt.Point)1 XPropertySet (com.sun.star.beans.XPropertySet)1 XModel (com.sun.star.frame.XModel)1 XFootnote (com.sun.star.text.XFootnote)1 XTextContent (com.sun.star.text.XTextContent)1 XTextRange (com.sun.star.text.XTextRange)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Nullable (org.jetbrains.annotations.Nullable)1 Language (org.languagetool.Language)1