Page 1 of 1

Add an event listener using document events API

Posted: Wed Jan 14, 2009 11:33 am
by psierra
I have leveraged the example within the OpenOffice developer guide (http://wiki.services.openoffice.org/wik ... ent_Events) to be able to capture events from an openoffice document. The first try i did return me an error:

// ERROR: com.sun.start.lang.IllegalArgumentException: cannor coerce argument type during corereflection call!

Code: Select all

// Start Open Office through Javascript using a test document stored in C:
function startOpenOffice()
{
		var smgr = new ActiveXObject("com.sun.star.ServiceManager");
		var objDesktop= smgr.createInstance("com.sun.star.frame.Desktop")
		var iHandler = smgr.createInstance("com.sun.star.task.InteractionHandler"); 
		var arg1 = smgr.Bridge_GetStruct("com.sun.star.beans.PropertyValue"); 
		arg1.Name = "InteractionHandler"; 
		arg1.Value = iHandler; 
		var args = new Array(); 
		args[0] = arg1; 

		sCurWordDocPath = objDesktop.loadComponentFromURL("file:///C:/letter.htm","_blank",0, args);

/* JScript event listener */
 		var xContext = smgr.getPropertyValue( "DefaultContext" );
 		var xCoreReflection = xContext.getValueByName( "/singletons/com.sun.star.reflection.theCoreReflection" );
 		var xClass = xCoreReflection.forName( "com.sun.star.document.XEventBroadcaster" );
 		var xMethod = xClass.getMethod( "addEventListener" );
		var xListener = xCoreReflection.forName("com.sun.star.lang.XEventListener");
		
		var arg1 = smgr.Bridge_GetStruct("com.sun.star.document.EventObject");
		arg1.EventName = "thedocumentListener_"; 
		arg1.Source = sCurWordDocPath; 
		var args = new Array(); 
		args[0] = arg1; 
		var value = smgr.Bridge_GetValueObject();
 		value.InitInOutParam("[]any", args);
		xMethod.invoke(sCurWordDocPath,value);
}
Then, i modify the way to "invoke" the addEventListener method instanciating the one which corresponds to the document(sCurWordDocPath) and no error is displayed although i am not able to capture the "disposing" event. source code below:

Code: Select all

// Replace xMethod.invoke(sCurWordDocPath,value) -> sCurWordDocPath.addEventListener(args);
// value is an object conversion to Sequence
// args is an array of struct containing the EventName and Source

// NO ERROR VERSION ALTHOUGH NO disposing event

// Start Open Office through Javascript using a test document stored in C:
function startOpenOffice()
{
		var smgr = new ActiveXObject("com.sun.star.ServiceManager");
		var objDesktop= smgr.createInstance("com.sun.star.frame.Desktop")
		var iHandler = smgr.createInstance("com.sun.star.task.InteractionHandler"); 
		var arg1 = smgr.Bridge_GetStruct("com.sun.star.beans.PropertyValue"); 
		arg1.Name = "InteractionHandler"; 
		arg1.Value = iHandler; 
		var args = new Array(); 
		args[0] = arg1; 

		sCurWordDocPath = objDesktop.loadComponentFromURL("file:///C:/letter.htm","_blank",0, args);

/* JScript event listener */
 		var xContext = smgr.getPropertyValue( "DefaultContext" );
 		var xCoreReflection = xContext.getValueByName( "/singletons/com.sun.star.reflection.theCoreReflection" );
 		var xClass = xCoreReflection.forName( "com.sun.star.document.XEventBroadcaster" );
 		var xMethod = xClass.getMethod( "addEventListener" );
		var xListener = xCoreReflection.forName("com.sun.star.lang.XEventListener");
		
		var arg1 = smgr.Bridge_GetStruct("com.sun.star.document.EventObject");
		arg1.EventName = "thedocumentListener_"; 
		arg1.Source = sCurWordDocPath; 
		var args = new Array(); 
		args[0] = arg1; 
		var value = smgr.Bridge_GetValueObject();
 		value.InitInOutParam("[]any", args);
		xMethod.invoke(sCurWordDocPath,value);
}

function thedocumentListener_disposing()
{
	alert("DISPOSING");
}
function disposing()
{
	alert("DISPOSING");
}
Could you please let me know what i am missing?
Any help will be highly appreciated.
Thanks!

[Moderation (Villeroy): Added code tags to make it readable]

Re: Add an event listener using document events API

Posted: Wed Jan 14, 2009 3:10 pm
by Villeroy
Unforturnately, there are two equally named XEventListener interfaces. One is the "mother of all events" css.lang.XEventListener with method "disposing()" whereas you should implement css.document.XEventListener, inheriting from the former with additional method "notifyEvent()" which passes the name of the event ("EventName") together with the caller ("Source")
http://api.openoffice.org/docs/common/r ... vents.html
http://api.openoffice.org/docs/common/r ... bject.html

Normally, you would package your document-event listener as an extension and register the respective event names to be called automagically.

Re: Add an event listener using document events API

Posted: Wed Jan 14, 2009 5:57 pm
by psierra
Hi Villeroy,

thanks for your prompt response!! i have updated my method based on your recommendation but i am still missing something. I put you the updated version here, could you please provide me an example about how to add the XEventListener to the document?

var xListener = smgr.createInstance("com.sun.star.document.XEventListener");
var xEvent = smgr.Bridge_GetStruct("com.sun.star.document.EventObject");
sCurWordDocPath.addEventListener(xListener);

//HOW TO MAKE "thedocumentListener_" the source of the notifyEvent??

function thedocumentListener_notifyEvent(oEvent)
{
alert("NOTIFY");
}

function NotifyEvent()
{
alert("NOTIFY");
}

Re: Add an event listener using document events API

Posted: Wed Jan 14, 2009 6:24 pm
by Villeroy
If I recall correctly, you add this type of listener to the frame rather than document, so the listener is called from the frame every time a component is loaded, closed, saved etc.
But I'm walking on thin ice here and I can not provide any useful example code.

Re: Add an event listener using document events API

Posted: Wed Jan 14, 2009 6:42 pm
by psierra
Villeroy,

I understand it is not easy to provide examples back. On the other hand, i have just copy/paste the updated version here and as you can see, i am creating the Event at document level ("sCurWordDocPath"). Any other ideas?

var smgr = new ActiveXObject("com.sun.star.ServiceManager");
var objDesktop= smgr.createInstance("com.sun.star.frame.Desktop")
var iHandler = smgr.createInstance("com.sun.star.task.InteractionHandler");
var arg1 = smgr.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
arg1.Name = "InteractionHandler";
arg1.Value = iHandler;
var args = new Array();
args[0] = arg1;

sCurWordDocPath = objDesktop.loadComponentFromURL("file:///C:/letter.htm","_blank",0, args);
var xContext = smgr.getPropertyValue( "DefaultContext" );
var xCoreReflection = xContext.getValueByName( "/singletons/com.sun.star.reflection.theCoreReflection" );
var xClass = xCoreReflection.forName( "com.sun.star.document.XEventBroadcaster" );
var xMethod = xClass.getMethod( "addEventListener" );
var xListener = smgr.createInstance("com.sun.star.document.XEventListener");

var arg1 = smgr.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
arg1.Name = "thedocumentListener_";
arg1.Value = xListener;
var args1 = new Array();
args1[0] = arg1;

sCurWordDocPath.addEventListener(args1);

Re: Add an event listener using document events API

Posted: Tue Mar 31, 2009 11:04 am
by phazzy
Hi ,

You managed to get somewhere with that event listener? I'm trying to do something similar
Best regards,

phazzy

Re: Add an event listener using document events API

Posted: Tue Mar 31, 2009 1:01 pm
by phazzy
Hi,

I have found a solution to this problem:
Here is the code:

Code: Select all

<script type="text/javascript">

      var objServiceManager = new ActiveXObject("com.sun.star.ServiceManager");
      var objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop");
      var args = new Array();
      
      var objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args);
      
      var listener = new XEventListener_Impl();
      
      function XEventListener_Impl()
      {
  	  this._environment= "JScript";
  	  this._implementedInterfaces= new Array( "com.sun.star.lang.XEventListener");
      
  	  //XEventListener
  	  this.disposing = XEventListener_disposing;
      }	
      
      function XEventListener_disposing( source)
      {
         alert("JScript Event Listener \n The document was closed");
      }

    </script>
I hope this helps someone.
All the best,

phazzy