Hi Joel,

Joel Cordeiro escribió:
Hi,

does anyone know how to get the actual language of the thesaurus with the
api? I would like to show automatically the synonyms of the selected word on
the context menu. Actually i am just working with the portuguese language,
but i would like to make a generic extension, so it uses the language that
is selected on the thesaurus.

the language is not "selected on the thesaurus", rather when you press
Ctr + F7 (or Tools - Language - Thesaurus), the language of the
thesaurus displayed in the dialog will depend on the language of the
word where the view cursor was inserted. The same goes for the context
menu when you right click on a spelling error.

So you need to know the language of the selected text/ the word where
the insertion point is: the language is retrieve as a character property
(http://api.openoffice.org/docs/common/ref/com/sun/star/style/CharacterProperties.html#CharLocale)
in the form of a com.sun.star.lang.Locale struct:

    public ContextMenuInterceptorAction notifyContextMenuExecute(
            com.sun.star.ui.ContextMenuExecuteEvent aEvent) throws
RuntimeException {

        try {
            XSelectionSupplier xSelectionSupplier = aEvent.Selection;
            Object aSelection = xSelectionSupplier.getSelection();

            if (aSelection != null && !AnyConverter.isVoid(aSelection)) {

                XServiceInfo xServiceInfo = (XServiceInfo)
UnoRuntime.queryInterface(
                        XServiceInfo.class, aSelection);
                if (xServiceInfo != null && xServiceInfo.supportsService(
                        "com.sun.star.text.TextRanges")) {
                    XIndexAccess xIndexAccess = (XIndexAccess)

UnoRuntime.queryInterface(XIndexAccess.class, xServiceInfo);
                    int nCount = xIndexAccess.getCount();

                    XTextRange xTextRange = null;
                    XTextCursor xTextCursor = null;
                    XPropertySet xPropertySet = null;

                    // check that there is at least an insertion point...
                    if (nCount > 0) {
                        if (nCount == 1) {
                            try {
                                // create a text cursor and check if it
is collapsed
                                xTextRange = (XTextRange)
UnoRuntime.queryInterface(
                                        XTextRange.class,
xIndexAccess.getByIndex(0));
                                xTextCursor =
xTextRange.getText().createTextCursorByRange(xTextRange);
                                if (xTextCursor.isCollapsed()) {
                                    System.out.println("\tXTextCursor
is collapsed: nothing selected, " +
                                            "it is only the insertion
point\n");
                                } else {

System.out.println(String.format("\tone text range selected:\n" +
                                            "\tSelected text range's
string : \"%s\"",
                                            xTextRange.getString()));
                                // do your job...
                                }
                                xPropertySet = (XPropertySet)
UnoRuntime.queryInterface(
                                        XPropertySet.class, xTextRange);
                                Object oLocale =
xPropertySet.getPropertyValue("CharLocale");
                                if ( oLocale != null &&
!AnyConverter.isVoid(oLocale)) {
                                    Locale aLocale = (Locale)
AnyConverter.toObject(
                                            Locale.class, oLocale);

System.out.printf("\tLocale.Language = %s\n\tLocale.Country = %s\n",
                                            aLocale.Language,
aLocale.Country);
                                }

                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            for (int i = 1; i < nCount; i++) {
                                try {
                                    xTextRange = (XTextRange)
UnoRuntime.queryInterface(
                                            XTextRange.class,
xIndexAccess.getByIndex(i));
                                    // do your job...

                                xPropertySet = (XPropertySet)
UnoRuntime.queryInterface(
                                        XPropertySet.class, xTextRange);
                                Object oLocale =
xPropertySet.getPropertyValue("CharLocale");
                                if ( oLocale != null &&
!AnyConverter.isVoid(oLocale)) {
                                    Locale aLocale = (Locale)
AnyConverter.toObject(
                                            Locale.class, oLocale);
                                    System.out.printf("Locale.Language
= %s\nLocale.Country = %s\n",
                                            aLocale.Language,
aLocale.Country);
                                }

                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }

                //....

                return
com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED;
            }
        } catch (com.sun.star.beans.UnknownPropertyException ex) {
            // do something useful
            // we used a unknown property
        } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
            // do something useful
            // we used an invalid index for accessing a container
        } catch (com.sun.star.uno.Exception ex) {
            // something strange has happend!
        } catch (java.lang.Exception ex) {
            // catch java exceptions and something useful
        }

        return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED;
    }



Regards
Ariel

--
Ariel Constenla-Haile
La Plata, Argentina

[EMAIL PROTECTED]
http://www.ArielConstenlaHaile.com.ar/ooo/



"Aus der Kriegsschule des Lebens
                - Was mich nicht umbringt,
        macht mich härter."
                Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to