public String readPreviousWordFromDocument() {
XMultiComponentFactory xmcf = m_xContext.getServiceManager();
Object desktop;
try {
desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
// retrieve the current component and access the controller
XComponent xCurrentComponent = xDesktop.getCurrentComponent();
// get the XModel interface from the component
XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xCurrentComponent);
// the model knows its controller
XController xController = xModel.getCurrentController();
XTextViewCursorSupplier xViewCursorSupplier =
(XTextViewCursorSupplier) UnoRuntime.queryInterface(
XTextViewCursorSupplier.class, xController);
// get the cursor
XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
// query its XPropertySet interface, we want to set character and paragraph properties
XPropertySet xCursorPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xViewCursor);
// set the appropriate properties for character and paragraph style
xCursorPropertySet.setPropertyValue("CharStyleName", "Quotation");
xCursorPropertySet.setPropertyValue("ParaStyleName", "Quotations");
XPageCursor xPageCursor = (XPageCursor) UnoRuntime.queryInterface(
XPageCursor.class, xViewCursor);
XText xDocumentText = xViewCursor.getText();
XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());
XWordCursor xWordCurser = (XWordCursor) UnoRuntime.queryInterface(
XWordCursor.class, xModelCursor);
xWordCurser.gotoPreviousWord(true);
String lastWord = xWordCurser.getString();
return lastWord;
} catch (Exception e) {
return "";
}
}
In the above code by changing the Cursor (eg:- xWordCurser ,xParagraphCursor etc.. ) you can shift between word word or else paragraphs.
If you want to change which word ...simply change by changing the following line
" xWordCurser.gotoPreviousWord(true);"
No comments:
Post a Comment