Question

I've been developing an OpenOffice Writer extension. Currently, I need to retrieve the current document object. The following should return the current component as a Writer document.

XMultiComponentFactory factory = context.getServiceManager();
XDesktop desktop = (XDesktop) factory.createInstanceWithContext("com.sun.star.frame.Desktop", context);
XTextDocument document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, desktop.getCurrentComponent());

return document;

My problem happens when the second line is run. The following exception is thrown:

java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to com.sun.star.frame.XDesktop

Why is the createInstanceWithContext returning something which does not conform to the requested interface? Is there a way to fix it?

Was it helpful?

Solution

Looking at the docs here, there is a two step process to get the interface:

          Object desktop = xRemoteServiceManager.createInstanceWithContext (
              "com.sun.star.frame.Desktop", xRemoteContext);
          XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top