I created a project of type "Add-on for Apache OpenOffice", with some menu items contextualized in Office Writer.
Now I would like to know how to get a reference to the Current Document from the class of the add-on.
It would be useful to have an example of code for this, it's pretty much impossible to find examples at least for the new API anyways.
In the "dispatch" method that is automatically created in my project class, I can intercept the clicks on the menu items, but then I'm not sure how to get a reference to the current active document.
I'm finding some examples that use XScriptContext, but I have the impression that the new SDK / API doesn't use XSCriptContext but rather XComponentContext...
From the examples that I have seen, I believe I'm supposed to use UnoRuntime.queryInterface(), but I don't know how to do this with XComponentContext.
Well here's the code that is automatically created by the Add-on project wizard, where you see the comment "//Now how do I get a reference to the active document?" I would like to start interacting with the current document (inserting paragraphs, inserting text, etc.):
Code: Select all
package com.example;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public final class BibleGetIO extends WeakBase
implements com.sun.star.frame.XDispatchProvider,
com.sun.star.frame.XDispatch,
com.sun.star.lang.XServiceInfo,
com.sun.star.lang.XInitialization {
private final XComponentContext m_xContext;
private com.sun.star.frame.XFrame m_xFrame;
private static final String m_implementationName = BibleGetIO.class.getName();
private static final String[] m_serviceNames = {"com.sun.star.frame.ProtocolHandler"};
public BibleGetIO(XComponentContext context) {
m_xContext = context;
};
public static XSingleComponentFactory __getComponentFactory(String sImplementationName) {
XSingleComponentFactory xFactory = null;
if (sImplementationName.equals(m_implementationName)) {
xFactory = Factory.createComponentFactory(BibleGetIO.class, m_serviceNames);
}
return xFactory;
}
public static boolean __writeRegistryServiceInfo(XRegistryKey xRegistryKey) {
return Factory.writeRegistryServiceInfo(m_implementationName,
m_serviceNames,
xRegistryKey);
}
// com.sun.star.frame.XDispatchProvider:
public com.sun.star.frame.XDispatch queryDispatch(com.sun.star.util.URL aURL,
String sTargetFrameName,
int iSearchFlags) {
if (aURL.Protocol.compareTo("de.bibleget.biblegetio:") == 0) {
if (aURL.Path.compareTo("CitazioneDaInput") == 0) {
return this;
}
}
return null;
}
// com.sun.star.frame.XDispatchProvider:
public com.sun.star.frame.XDispatch[] queryDispatches(
com.sun.star.frame.DispatchDescriptor[] seqDescriptors) {
int nCount = seqDescriptors.length;
com.sun.star.frame.XDispatch[] seqDispatcher
= new com.sun.star.frame.XDispatch[seqDescriptors.length];
for (int i = 0; i < nCount; ++i) {
seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
seqDescriptors[i].FrameName,
seqDescriptors[i].SearchFlags);
}
return seqDispatcher;
}
public void dispatch(com.sun.star.util.URL aURL,
com.sun.star.beans.PropertyValue[] aArguments) {
if (aURL.Protocol.compareTo("com.example.biblegetio:") == 0) {
if (aURL.Path.compareTo("QuotationFromInput") == 0) {
// add your own code here
System.out.println("You clicked on menu item QuotationFromInput");
//Now how do I get a reference to the active document?
return;
}
}
}
public void addStatusListener(com.sun.star.frame.XStatusListener xControl,
com.sun.star.util.URL aURL) {
// add your own code here
}
public void removeStatusListener(com.sun.star.frame.XStatusListener xControl,
com.sun.star.util.URL aURL) {
// add your own code here
}
// com.sun.star.lang.XServiceInfo:
public String getImplementationName() {
return m_implementationName;
}
public boolean supportsService(String sService) {
int len = m_serviceNames.length;
for (int i = 0; i < len; i++) {
if (sService.equals(m_serviceNames[i])) {
return true;
}
}
return false;
}
public String[] getSupportedServiceNames() {
return m_serviceNames;
}
// com.sun.star.lang.XInitialization:
public void initialize(Object[] object)
throws com.sun.star.uno.Exception {
if (object.length > 0) {
m_xFrame = (com.sun.star.frame.XFrame) UnoRuntime.queryInterface(
com.sun.star.frame.XFrame.class, object[0]);
}
}
}