Add helper to retrieve frame, document etc
This commit is contained in:
parent
11848ebf9b
commit
0049811773
1 changed files with 42 additions and 0 deletions
42
source/org/libreoffice/example/helper/DocumentHelper.java
Normal file
42
source/org/libreoffice/example/helper/DocumentHelper.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
package org.libreoffice.example.helper;
|
||||
|
||||
import com.sun.star.frame.XDesktop;
|
||||
import com.sun.star.frame.XFrame;
|
||||
import com.sun.star.frame.XModel;
|
||||
import com.sun.star.lang.XComponent;
|
||||
import com.sun.star.lang.XMultiComponentFactory;
|
||||
import com.sun.star.text.XTextDocument;
|
||||
import com.sun.star.uno.UnoRuntime;
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
public class DocumentHelper {
|
||||
|
||||
/** Returns the curerent XDesktop */
|
||||
public static XDesktop getCurrentDesktop(XComponentContext xContext) {
|
||||
XMultiComponentFactory xMCF = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class,
|
||||
xContext.getServiceManager());
|
||||
Object desktop = null;
|
||||
try {
|
||||
desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
|
||||
}
|
||||
|
||||
/** Returns the current XComponent */
|
||||
private static XComponent getCurrentComponent(XComponentContext xContext) {
|
||||
return (XComponent) getCurrentDesktop(xContext).getCurrentComponent();
|
||||
}
|
||||
|
||||
/** Returns the current frame */
|
||||
public static XFrame getCurrentFrame(XComponentContext xContext) {
|
||||
XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, getCurrentComponent(xContext));
|
||||
return xModel.getCurrentController().getFrame();
|
||||
}
|
||||
|
||||
/** Returns the current text document (if any) */
|
||||
public static XTextDocument getCurrentDocument(XComponentContext xContext) {
|
||||
return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, getCurrentComponent(xContext));
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue