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 get a XComponentLoader instance without a desktop

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


Joined: 01 May 2004
Posts: 4
Location: Hartford, CT, USA

PostPosted: Sat May 01, 2004 6:06 pm    Post subject: How to get a XComponentLoader instance without a desktop Reply with quote

I want to load an spreadsheet, set values in some cells and get the values of the recaculated cells and close the file. Here is the code I use (adapted from the example)

xRemoteServiceManager = this.getRemoteServiceManager(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
Object desktop = xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", xRemoteContext);
XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
XComponentLoader.class, desktop);

PropertyValue[] loadProps = new PropertyValue[0];
XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);

XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
XSpreadsheetDocument.class, xSpreadsheetComponent);

XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
xSpreadsheets.insertNewByName("MySheet", (short)0);
com.sun.star.uno.Type elemType = xSpreadsheets.getElementType();

System.out.println(elemType.getTypeName());
Object sheet = xSpreadsheets.getByName("MySheet");
XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
XSpreadsheet.class, sheet);

XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
xCell.setValue(21);
xCell = xSpreadsheet.getCellByPosition(0, 1);
xCell.setValue(21);
xCell = xSpreadsheet.getCellByPosition(0, 2);
xCell.setFormula("=sum(A1:A2)");

System.out.println(xCell.getValue());
xCell = xSpreadsheet.getCellByPosition(0, 1);
xCell.setValue(41);
xCell = xSpreadsheet.getCellByPosition(0, 2);
System.out.println(xCell.getValue());

But doing so also launchs an application window. It seems because that the loader is created from the desktop. Is there a way of doing what I want to do without launching the window?

I am new here. Appreciate any help. Thanks.
Back to top
View user's profile Send private message
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Sat May 01, 2004 6:43 pm    Post subject: Reply with quote

Include the property Hidden with value True in loadprops
Back to top
View user's profile Send private message
czhao
Newbie
Newbie


Joined: 01 May 2004
Posts: 4
Location: Hartford, CT, USA

PostPosted: Sun May 02, 2004 1:56 pm    Post subject: Reply with quote

That works. No window is launched. However, I believe the visual objects are still created, but they are hidden. Is there any consideration/effort to separate the processing logic from UI components? OOo has great potential for server based document processing.
Back to top
View user's profile Send private message
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Sun May 02, 2004 2:09 pm    Post subject: Reply with quote

'visual objects'??? you are referencing the model here not the view.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


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

PostPosted: Sun May 02, 2004 2:11 pm    Post subject: Reply with quote

To expand on dfrench's comment, which I agree with.

I believe, that the model is being created, but not a controller or frame. Hence you definitely cannot use the dispatcher to execute user interface actions against the hidden window as discussed in another post here recently.

I predict the following....
* If you use Hidden=False, which creates a visible window, then calling getCurrentController() and getFrame() from the controller both work.
* If you use Hidden=True, which creates an invisible window, then calling one or both of these returns null.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
czhao
Newbie
Newbie


Joined: 01 May 2004
Posts: 4
Location: Hartford, CT, USA

PostPosted: Sun May 02, 2004 5:04 pm    Post subject: Reply with quote

DannyB,

Your second prediction is not true. Here is the prove. Code:

xRemoteServiceManager = this.getRemoteServiceManager(
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
Object desktop = xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", xRemoteContext);
XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
XComponentLoader.class, desktop);

PropertyValue[] loadProps = new PropertyValue[1];
loadProps [0] = new PropertyValue();
loadProps [0].Name = "Hidden";
loadProps [0].Value = new Boolean(true);

XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL("private:factory/scalc", "_blank", 0, loadProps);

XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
XSpreadsheetDocument.class, xSpreadsheetComponent);

XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(XModel.class, xSpreadsheetComponent);
XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)UnoRuntime.queryInterface(
XSpreadsheetView.class, xSpreadsheetController);
System.out.println("model: "+xSpreadsheetModel);
System.out.println("controller: "+xSpreadsheetController);
System.out.println("view: "+xSpreadsheetView);
System.out.println("frame: "+xSpreadsheetController.getFrame());


And the output:

model: [Proxy:28910606,d8a1be4;msci[0];7552a19c6c11d8ad84b74299c230,Type[com.sun.star.frame.XModel]]
controller: [Proxy:32392776,8cf95b0;msci[0];7552a19c6c11d8ad84b74299c230,Type[co
m.sun.star.frame.XController]]
view: [Proxy:4729123,8cf95b0;msci[0];7552a19c6c11d8ad84b74299c230,Type[com.sun.star.sheet.XSpreadsheetView]]
frame: [Proxy:3086625,8cfd92c;msci[0];7552a19c6c11d8ad84b74299c230,Type[com.sun.star.frame.XFrame]]

So controller, view and frame are created with the model, but they are hidden. Therefore the question remains: can we create a model without the view to avoid the overhead.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


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

PostPosted: Sun May 02, 2004 6:47 pm    Post subject: Reply with quote

czhao wrote:
DannyB,
Your second prediction is not true.


Interesting. (As spock would say.)

(BTW, I would accept your assertion of my second prediction failing as a simple fact. No need for elaborate proof. Especially since I originally stated it as "I believe....", rather than stating "It is the case that...")

Based on past evidence, it has been shown that there are problems trying to use the dispatch mechanism on hidden windows. So I wonder exactly where it breaks down? I was assuming that it broke down at the Frame, or possibly even at the Controller.

Wild speculations....
* Maybe the controller and/or frame doesn't support all services or interfaces that it would on a vislble window?
* Maybe you can get a frame, but then the dispatch mechanism doesn't work on that frame?

If I had time, I wouold run a few quick experiments in Basic.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Mon May 03, 2004 5:22 pm    Post subject: Reply with quote

A document that is loaded in hidden mode is not running in the same way that a regular document would run. So, even though it is possible to make the document visible and obtain things that may not seem logical to obtain from a hidden window, it is considered dangerous to do so. Things might seem to run just fine for a bit and then suddenly OOo will crash.

A dispatch may act in an unusual way because it is the Frame the supports the dispatch. The frame is part of the visible display and when loaded in hidden mode....
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
pajowett3
General User
General User


Joined: 30 Mar 2004
Posts: 42
Location: Australia

PostPosted: Mon May 03, 2004 11:10 pm    Post subject: Reply with quote

Hi Czhao.

If you are interested in performance, you might wish to try
XModel.lockControllers()
and
XActionLockable.addActionLock()

I have noticed some problems with using lockControllers for processing Writer documents (inserting into tables and updating indexes), though where I can use it, it does speed things up a lot.

addActionLock() is something I found in one of the SDK examples which had a comment it was for performance. It isn't available for writer so I don't know if it helps.


Regards.
Paulj.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


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

PostPosted: Tue May 04, 2004 9:08 am    Post subject: Reply with quote

Wherever it was that I read about the Dispatcher not working on hidden documents, was equally sure that using only the API would work perfectly on hidden documents.

Therefore, always try to use the API.

Avoid trying to use the dispatcher, except as a workaround for things that you are unable to do using the API.

(BTW, the previous two sentences, although not a direct quote, were the advice of the post I had read.)
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
elisaturrini
Newbie
Newbie


Joined: 31 Jan 2007
Posts: 1

PostPosted: Wed Jan 31, 2007 10:02 am    Post subject: Reply with quote

Hi all,

I've a similar problem. I'd like to execute a macro from a hidden document.
How can I do that avoiding to use the dispatcher?

thanks,
Elisa
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