Search in sources :

Example 1 with XModel

use of com.sun.star.frame.XModel 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 XModel

use of com.sun.star.frame.XModel in project jabref by JabRef.

the class OOBibBase method selectDocument.

public void selectDocument() throws UnknownPropertyException, WrappedTargetException, IndexOutOfBoundsException, NoSuchElementException, NoDocumentException {
    List<XTextDocument> textDocumentList = getTextDocuments();
    XTextDocument selected;
    if (textDocumentList.isEmpty()) {
        // No text documents found.
        throw new NoDocumentException("No Writer documents found");
    } else if (textDocumentList.size() == 1) {
        // Get the only one
        selected = textDocumentList.get(0);
    } else {
        // Bring up a dialog
        selected = selectComponent(textDocumentList);
    }
    if (selected == null) {
        return;
    }
    xCurrentComponent = UnoRuntime.queryInterface(XComponent.class, selected);
    mxDoc = selected;
    UnoRuntime.queryInterface(XDocumentIndexesSupplier.class, xCurrentComponent);
    XModel xModel = UnoRuntime.queryInterface(XModel.class, xCurrentComponent);
    XController xController = xModel.getCurrentController();
    xViewCursorSupplier = UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
    // get a reference to the body text of the document
    text = mxDoc.getText();
    // Access the text document's multi service factory:
    mxDocFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, mxDoc);
    XDocumentPropertiesSupplier supp = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, mxDoc);
    userProperties = supp.getDocumentProperties().getUserDefinedProperties();
    propertySet = UnoRuntime.queryInterface(XPropertySet.class, userProperties);
}
Also used : XPropertySet(com.sun.star.beans.XPropertySet) XDocumentPropertiesSupplier(com.sun.star.document.XDocumentPropertiesSupplier) XTextViewCursorSupplier(com.sun.star.text.XTextViewCursorSupplier) XTextDocument(com.sun.star.text.XTextDocument) XComponent(com.sun.star.lang.XComponent) XMultiServiceFactory(com.sun.star.lang.XMultiServiceFactory) XController(com.sun.star.frame.XController) XModel(com.sun.star.frame.XModel)

Aggregations

XPropertySet (com.sun.star.beans.XPropertySet)2 XModel (com.sun.star.frame.XModel)2 XTextViewCursorSupplier (com.sun.star.text.XTextViewCursorSupplier)2 XDocumentPropertiesSupplier (com.sun.star.document.XDocumentPropertiesSupplier)1 XController (com.sun.star.frame.XController)1 XComponent (com.sun.star.lang.XComponent)1 XMultiServiceFactory (com.sun.star.lang.XMultiServiceFactory)1 XTextDocument (com.sun.star.text.XTextDocument)1 XTextViewCursor (com.sun.star.text.XTextViewCursor)1 Nullable (org.jetbrains.annotations.Nullable)1 Language (org.languagetool.Language)1