Page 1 of 1

[Solved] Register MouseEvents from Desktop Container

Posted: Fri Dec 25, 2015 10:00 am
by jelias
Hello guys.

I'm trying to develop a external Java application that interacts with OpenOffice. Basically I want to show the main toolbar buttons (new, save, ...) hints (when mouseover) in a other way, externally. I've been reading, trying and learning a lot OO SDK and UNO API last couple of days, developing a app that intercepts which button from toolbar would you moving the mouse, but until now I couldn't get this up.

Main code until now:

Code: Select all

XComponentContext xContext =  com.sun.star.comp.helper.Bootstrap.bootstrap();
XMultiComponentFactory xServiceManager = xContext.getServiceManager();
Object desktop = xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xContext );
XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, desktop);
XComponentLoader xComponentLoader =  (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, desktop );

// load a spreadsheet document
String loadURL = "private:factory/scalc"; PropertyValue[] loadProps = new PropertyValue[0];
XComponent document = xComponentLoader.loadComponentFromURL(loadURL, "_blank", 0, loadProps);

xFAL xFAL0 = new xFAL();

XDocumentEventBroadcaster xDEB =  (XDocumentEventBroadcaster)UnoRuntime.queryInterface(XDocumentEventBroadcaster.class, document );
xDEL xDEL0 = new xDEL();
xDEB.addDocumentEventListener(xDEL0);

xEL xEL0 = new xEL(); xEL xEL1 = new xEL();
xML xML0  = new xML(); xML xML1 = new xML();
xMM xMM0  = new xMM(); xMM xMM1 = new xMM();
xKL xKL0  = new xKL(); xKL xKL1 = new xKL();

XFrame frame = xDesktop.getCurrentFrame();

frame.getCreator().getActiveFrame().getContainerWindow().addMouseListener(xML0);
frame.getCreator().getActiveFrame().getContainerWindow().addMouseMotionListener(xMM0);
frame.getCreator().getActiveFrame().getContainerWindow().addKeyListener(xKL0);
frame.getCreator().getActiveFrame().getComponentWindow().addMouseListener(xML1);
frame.getCreator().getActiveFrame().getComponentWindow().addMouseMotionListener(xMM1);
frame.getCreator().getActiveFrame().getComponentWindow().addKeyListener(xKL1);

frame.getCreator().getActiveFrame().addFrameActionListener(xFAL0);

xWL xWL0 = new xWL();
frame.getCreator().getActiveFrame().getContainerWindow().addWindowListener(xWL0);
From all the component/container listeners above, just the WindowListener is registering the events. All mouse/key listeners are backing to me with nothing.

Can anyone help me with that code? Or suggest me a other way to go?
I'll appreciate any help.

Re: Register MouseEvents from Desktop Container

Posted: Fri Dec 25, 2015 2:05 pm
by Villeroy

Re: Register MouseEvents from Desktop Container

Posted: Fri Dec 25, 2015 7:15 pm
by jelias
Thanks for the quick answer. The ODFToolkit is very interesting, but I will keep trying to register the OO main GUI events. May be JAVA AWT can't do that (register mouse/key events) because the GUI is being rendered in GTK?

Re: Register MouseEvents from Desktop Container

Posted: Fri Dec 25, 2015 7:25 pm
by Villeroy

Re: Register MouseEvents from Desktop Container

Posted: Fri Dec 25, 2015 9:33 pm
by jelias
Thanks again Villeroy. I was trying to work with MRI since yesterday. With your link I could learn how to show up the generated MRI Java code and compare it with my. The only difference that I could notice is that it uses XWindow2 interface when I get the Container Window. I was following the SDK: https://wiki.openoffice.org/wiki/Docume ... Interfaces .
But the result was the same, no reponse from the listeners attached to Conatiner/Component Windows (either XWindow or XWindow2).

Re: Register MouseEvents from Desktop Container

Posted: Fri Dec 25, 2015 10:16 pm
by Villeroy
Topic Header wrote:Register MouseEvents from Desktop Container
The desktop is an abstract container of frames without windows nor controllers. The contained frames may have windows (container window and component window). Windows can have mouse listeners.

Re: Register MouseEvents from Desktop Container

Posted: Sat Dec 26, 2015 12:12 am
by jelias
Right! So, I tried

Code: Select all

xDesktop.getCurrentFrame().getContainerWindow().addMouseListener(xML0);
xDesktop.getCurrentFrame().getComponentWindow().addMouseListener(xML1);
But, until now, they didn't register any event.
Many thanks for your quick help.

Re: Register MouseEvents from Desktop Container

Posted: Sat Dec 26, 2015 12:43 pm
by Villeroy
Could be a bug or not. I would not change the GUI programmatically. If I would change anthing at all, I would always configure a different GUI for a particular document template. Open/LibreOffice is much more about templates, styles and XML configuration rather than programming.

Re: Register MouseEvents from Desktop Container

Posted: Sat Dec 26, 2015 7:44 pm
by jelias
OK, let me try a new approach. From an external app I can simulate a click on any of the main toolbar buttons, trough the OO SDK?

Re: Register MouseEvents from Desktop Container

Posted: Sat Dec 26, 2015 11:05 pm
by Villeroy
Use the dispatch framework as the macro recorder does. viewtopic.php?f=45&t=79540&p=365636#p365636

Re: Register MouseEvents from Desktop Container

Posted: Sun Dec 27, 2015 6:36 am
by jelias
Villeroy, many thanks for your tips. They guided me to a little sucess, 'cause now I have external control over the OO main UI.
Here the main piece of code. Hopefully it will help someone:

Code: Select all

XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
XMultiComponentFactory xServiceManager = xContext.getServiceManager();
Object desktop = xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xContext );
XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, desktop);
XComponentLoader xComponentLoader =  (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, desktop );

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

XFrame dFrame = xDesktop.getCurrentFrame();
while (dFrame == null) dFrame = xDesktop.getCurrentFrame();

Object dispatcher = xServiceManager.createInstanceWithContext("com.sun.star.frame.DispatchHelper", xContext );
XDispatchHelper dispatch = UnoRuntime.queryInterface(XDispatchHelper.class, dispatcher);
XDispatchProvider provider = UnoRuntime.queryInterface(XDispatchProvider.class, dFrame);

dispatch.executeDispatch(provider, ".uno:SaveAs", "", 0, null);

xTL xTL0 = new xTL(); xDesktop.addTerminateListener(xTL0);
synchronized(xTL0){ xTL0.wait(); }