Page 1 of 1

[Solved] How to save content before xDesktop.terminate ?

Posted: Mon Dec 03, 2018 11:19 pm
by Hs95
Hi,
How to save content into new file before xDesktop.terminate() ?

Code: Select all

  XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
                  XDesktop.class, oDesktop) 
/// some operations on content
xDesktop.termiante();

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 1:37 am
by Lupp
I cannot recognise something like code above. What language / programming environment?

If you opened a document from a file you should have access via an object variable, say myDoc.
myDoc.Store() will then store it to the file it was opened from. myDoc.Close(True) will close the file.
A document (component) created by the factory needs to be saved by myDoc.StoreAsUrl(...).

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 2:14 am
by Hs95
Sorry for lack of informations. I'm using Java. I want to write something by cursor and save changes. Here is my code

Code: Select all

XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);
          XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);
          Object initialObject = urlResolver.resolve(
                  "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
          XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(
                  XMultiComponentFactory.class, initialObject);
          XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(
                  XPropertySet.class, xOfficeFactory);
          Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
          XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime.queryInterface(
                  XComponentContext.class, oDefaultContext);

          Object oDesktop = xOfficeFactory.createInstanceWithContext(
                  "com.sun.star.frame.Desktop", xOfficeComponentContext);
          XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
                  XDesktop.class, oDesktop);
          XComponent xCurrentComponent = xDesktop.getCurrentComponent();
          XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xCurrentComponent);

          XController xController = xModel.getCurrentController();
          XTextViewCursorSupplier xViewCursorSupplier =
                  (XTextViewCursorSupplier)UnoRuntime.queryInterface(
                          XTextViewCursorSupplier.class, xController);

          XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
          XPropertySet xCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, xViewCursor);
          XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
                  XPageCursor.class, xViewCursor);

          xPageCursor.jumpToFirstPage();
          System.out.println("The current page number is " + xPageCursor.getPage());
          xPageCursor.jumpToNextPage();
          System.out.println("The next page number is " + xPageCursor.getPage());
          xPageCursor.jumpToLastPage();
          System.out.println("The last page number is" + xPageCursor.getPage());
          XText xDocumentText = xViewCursor.getText();
          XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());
          XParagraphCursor xParagraphCursor = (XParagraphCursor)UnoRuntime.queryInterface(
                  XParagraphCursor.class, xModelCursor);

          xParagraphCursor.gotoEndOfParagraph(false);
          xParagraphCursor.setString("Hello");
          xDesktop.terminate();


      }

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 2:48 am
by JeJe

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 3:31 am
by Hs95
Ok. But how to get Xstorable from xDesktop ?

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 4:32 am
by FJCC
XStoreable comes from the document, not from xDesktop. I don't do Java but I think you can get xStorable from your xModel.

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 10:53 am
by JeJe
I don't know Java either but you've already used getCurrentComponent which gives you the document. So you use store or storeAsURL with that object.

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 11:17 am
by Hs95
This objects don't give me oportunity to use this methods. Only xStoreable can do this

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 11:26 am
by RoryOF
The paper at this location
http://wi.wu-wien.ac.at:8002/rgf/diplom ... mation.pdf
shows an example using xStoreable

A quick Google search gave me this, as well as several other examples. Can no one search any more?

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 6:18 pm
by FJCC
I recorded the following Java code with the MRI extension. The object that privides xModel also provides xStorable. I hope that helps.

Code: Select all

import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XStorable;
import com.sun.star.io.IOException;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XModel xModel = UnoRuntime.queryInterface(
			XModel.class, oInitialTarget);
		XController xController = xModel.getCurrentController();
		
		XStorable xStorable = UnoRuntime.queryInterface(
			XStorable.class, oInitialTarget);
		xStorable.store();
		
	}
	catch (IOException e1)
	{
		// store
		e1.printStackTrace();
	}
}

Re: How to save content before xDesktop.terminate ?

Posted: Tue Dec 04, 2018 7:32 pm
by Hs95
Thank you. Evertyhting work now.