OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

How to open and close staroffice from java?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
nmaves
General User
General User


Joined: 12 Sep 2003
Posts: 10
Location: Denver,CO

PostPosted: Fri Nov 07, 2003 7:05 am    Post subject: How to open and close staroffice from java? Reply with quote

I have a program that converts documents. It is based on the example that is sent with the sdk. For the program to work I need to have oo running. Currently I start it from a comannd line then run my java app. This works but I have no good way of shutting down oo. If I could start and stop it from java I could die a happy man Smile

Nathan
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
ewdyke
General User
General User


Joined: 11 Nov 2003
Posts: 11
Location: Williamsburg, Va

PostPosted: Wed Nov 12, 2003 11:54 am    Post subject: try terminate() Reply with quote

Did you try the termiante() method on the desktop? This works for me, java 1.4 OOo 1.1

Earnie!
_________________
Earnie Dyke
Information Systems Designer and Developer
Back to top
View user's profile Send private message Yahoo Messenger
nmaves
General User
General User


Joined: 12 Sep 2003
Posts: 10
Location: Denver,CO

PostPosted: Wed Nov 12, 2003 12:01 pm    Post subject: Reply with quote

I don't think that I have a descktop object?
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Nov 12, 2003 12:37 pm    Post subject: Reply with quote

Using the XMultiServiceFactory of the OOo ServiceManager, can you create an instance of a

com.sun.star.frame.Desktop?

Then call terminate() on it? (Via. one of its interfaces.)
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
nmaves
General User
General User


Joined: 12 Sep 2003
Posts: 10
Location: Denver,CO

PostPosted: Thu Nov 13, 2003 2:27 pm    Post subject: Reply with quote

How do I get an instance of XMultiServiceFactory.

I am trying to modify the Document Converted from the ooo sdk examples. Rember I have to start up oo from outside java. If there is I way to start it up from java that wouble be great as well.

Nathan
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Nov 13, 2003 7:08 pm    Post subject: Reply with quote

If I remember correctly, you do it something like this....

com.sun.star.lang.XMultiServiceFactor xMSF = UnoRuntime.queryInterface( com.sun.star.lang.XMultiServiceFactory.class, obj );

where obj is the object you want to obtain the XMultiServiceFactory interface from.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
nmaves
General User
General User


Joined: 12 Sep 2003
Posts: 10
Location: Denver,CO

PostPosted: Thu Nov 20, 2003 9:53 am    Post subject: Reply with quote

Now that I have a ServiceFactory obj how do I get a desktop obj. Sorry to be such a newbe at all of this.

Nathan
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Nov 20, 2003 11:08 am    Post subject: Reply with quote

Something like this, where xMSF is the XMultiServiceFactory interface of the ServiceManager service from the previous message.

Object oDesktop = xMSF.createInstance( "com.sun.star.frame.Desktop" );

The service you are creating is com.sun.star.frame.Desktop.
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/Desktop.html

You are starting from the com.sun.star.lang.ServiceManager.
http://api.openoffice.org/docs/common/ref/com/sun/star/lang/ServiceManager.html
As you can see from this link, the ServiceManager includes the MultiServiceFactory service. That means that any features of MultiServiceFactory are available in ServiceManager as well. Follow the white rabbit to MultiServiceFactory...
http://api.openoffice.org/docs/common/ref/com/sun/star/lang/MultiServiceFactory.html
As you can see, MultiServiceFactory implements three interfaces. One of them is XMultiServiceFactory. Now if you take the red pill...
http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiServiceFactory.html
you can see that the interface XMultiServiceFactory provides several methods, one of which is....

createInstance()

So, since you've got the XMultiServiceFactory interface of ServiceManager into a variable xMSF, you can call it's createInstance() method. In fact, the XMultiServiceFactory interface to the ServiceManager is how to get hold of new instances of lots of good services, but most importantly the Desktop service.

Services are similar to objects in java.

1. A service can inherit from zero or one other service. (which each can recursively apply these rules.)
2. A service can include zero or more services. (which each can recursively apply these rules.)
3. A service can export (which means implement) interfaces.
4. A service can have properties.

5. An interface can inherit from zero or one other interface. (which each can recursively apply these rules.)
6. An interface can include from zero or more interfaces. (which each can recursively apply these rules.)
7. An interface can implement methods.
8. An interface is always named with an X.

From these rules you can deduce that methods are ALWAYS found in interfaces, and properties are ALWAYS found in services.

If you follow the white rabbit, you'll see that from the above link to ServiceManager, you had to drill down several levels to find that you caould get to the createInstance() method.

Rule 3 says a service can implement interfaces. This is sort of like how a class in java implements an interface. But in Java, unlike other languages such as Basic, or Python, you must "typecast" to the right interface. You cannot do this via. a simple Java typecast. You must do it via. the static method UnoRuntime.queryInterface().

So, suppose I had an object oDrawDoc, which is a drawing document.

Code:
Object oDrawDoc = ......something that obtains drawing......


Now I need to call methods that are located in several interfaces. First I need to get those interfaces.

Code:
XFoo oDrawDoc_XFoo = UnoRuntime.queryInterface( XFoo.class, oDrawDoc );
XBar oDrawDoc_XBar = UnoRuntime.queryInterface( XBar.class, oDrawDoc );


XFoo and XBar are two interface names I made up. Suppose that a drawing document implemented these two interfaces, and suppose I need to first call a method from XFoo and then I needed to call a method from XBar. Both methods are part of the drawing document service, but the two methods are in different interfaces. This code fragment is how you would obtain the two different interfaces.

Here is how a Service differs from a real object. When I queryInterface to get a certian interface from the service, the Uno may give me an entirely different object within the implementation. That is, even though I have three variables....

Code:
Object oDrawDoc;
XFoo oDrawDoc_XFoo;
XBar oDrawDoc_XBar;


and they all "point" to the same service, they may in fact point to different concrete implementation objects. Or not. The queryInterface() may internally just do a simple java typecast. Or may have to get a whole different concrete object. In fact, which of these it does may change from release to release. But nonetheless, I can think of all three of these variables as pointing to the same "service". In java, the oDrawDoc_XFoo service is definitely of the correct interface, so I can call the documented methods on it.

Now let's talk about the typical idiom for opening documents. Typically, in Basic, for instance, you start with the service maanger. From there, get the desktop. Then from the desktop use the magic method

loadComponentFromURL().

In Basic, there is always a global function named GetProcessServiceManager() which returns the ServiceManager.
In Basic, there is always a global variable named StarDesktop which already has the Desktop object.
So in Basic, you typically get a new EMPTY document like this...

[/code]oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, Array() )[/code]

The Array() is an empty array, no elements. The type of elements, if any, need to be the struct com.sun.star.beans.PropertyValue. The URL creates a new drawing.

If I wanted to open a document, in Basic, I would do....

Code:
oDrawDoc = StarDesktop.loadComponentFromURL( "file:///home/dbrewer/Desktop/Drawing.sxd", "_blank", 0, Array() )


Now, let's translate to Java.

Code:
// Get the ServiceManager, plus any useful interfaces to it.
Object oServiceManager = ....get the service manager which you are already doing....;
XMultiServiceFactory oServiceManager_xMSF = UnoRuntime.queryInterface( XMultiServiceFactory.class, oServiceManager );

// Get the desktop, plus any useful interfaces to it.
Object oDesktop = oServiceManager_xMSF.createInstance( "com.sun.star.frame.Desktop" );
XComponentLoader oDesktop_xCmpLoad = UnoRuntime.queryInterface( XComponentLoader.class, oDesktop );

// Create a new drawing document, plus useful interfaces to it.
Object oDrawDoc = oDesktop.xCmpLoad.loadComponentFromURL( "private:factory/sdraw",
     "_blank", 0, new com.sun.star.beans.PropertyValue[0] );
XDrawPagesSupplier oDrawDoc_xDPS = UnoRuntime.queryInterface( XDrawPagesSupplier.class, oDrawDoc );
XPrintable oDrawDoc_xPrint = UnoRuntime( XPrintable.class, oDrawDoc );


(Gee, I sure hope that was the right way to create an empty array of PropertyValue in java -- didn't try it.)

Did you notice how com.sun.star.frame.Desktop
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/Desktop.html
implements the XComponentLoader
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLoader.html
which has the method

loadComponentFromURL()

This gets you to the point of opening or creating a document, along with some idea of how deep the rabit hole goes when navigating the API documentation. (Hint: drill down deeply just in order to find one useful property or method.)
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
nmaves
General User
General User


Joined: 12 Sep 2003
Posts: 10
Location: Denver,CO

PostPosted: Thu Nov 20, 2003 1:01 pm    Post subject: Reply with quote

Thanks for the detailed reply. This is all a little too much to take in at one time. Is there a basic turtorial anywhere that can help me just get started with all of this.

My ultimate goal is to modify the Convert program in the examples to only the docs that I want.

Is it possible to start up openoffice from java. Right now I have to start it up outside of java then connect to from java. My problem is that I can not shut down oo after the convert program runs. It leaves defuct processes on my server and the next time the convert program runs it has trouble using the defunct oo process.

any thoughts on design.
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Nov 20, 2003 3:11 pm    Post subject: Reply with quote

If your java program runs on the same machine as OOo, then I suppose your java program could launch "soffice" like any other executable. There is some system class or toolkit that I remember can run shell commands. I don't remember it, because I've never wanted to do that.

As for closing OOo through the API, I know it used to be possible. But I don't think anyone has been able to do it in 1.1. There have been several threads posted on this, and I don't seem to recall a resolution.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group