List Info

Thread: intercepting onLoad events in java




intercepting onLoad events in java
user name
2007-01-16 14:47:12
Hello,
I have a java application which intercepts events from an
office bean 
using the GlobalEventBroadcaster interface.
The problem is the following: my application will have
different 
instances of the bean running, and the
GlobalEventBroadcaster intercepts 
all events coming from all sources if I have understood
properly, so I'm 
not able to understand which document has been loaded on a
onLoad event.
Now I'm able to find the name of the event, but I can't
understand which 
instance it is coming from.
My goal is to find at least the name of the file being
loaded, or better 
a unique ID identifying the instance of the bean which
generated the event.
Any idea?
Thank you
bye

Here's the code of an example class (you have to change the
URLs of the 
files to get it working):

import java.awt.Frame;
import java.net.MalformedURLException;

import org.eclipse.swt.widgets.Shell;

import com.sun.star.beans.Property;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.beans.OOoBean;
import com.sun.star.document.XEventBroadcaster;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;


public class TestConnection {

	static com.sun.star.uno.XComponentContext xContext = null;
	static long SLEEPTIME = 3000;
	/**
	 * param args
	 */
	public static void main(String[] args) {
		try {
//			 get the remote office component context
	       // xContext =
com.sun.star.comp.helper.Bootstrap.bootstrap();

	       // System.out.println("Connected to a running
office ...");
			
			Shell oShell = new Shell();
			Frame oFrame = new Frame();
			System.out.println("Creating bean");
			OOoBean oBean = new OOoBean();
			System.out.println("Created Bean");
			if (oBean.getOOoConnection()!=null)
				System.out.println("Connection is not null");
			else
				System.out.println("Connection is null");
			oFrame.add(oBean);
			oFrame.setVisible(true);
			oFrame.setSize(500, 400);
			
			//registro il listener per gli eventi sul controllo
office
			XComponentContext xRemoteContext = 
oBean.getOOoConnection().getComponentContext();
			String 
st2[]=xRemoteContext.getServiceManager().getAvailableService
Names();
			System.out.println(st2);
			Object xGlobalBroadCaster = 
xRemoteContext.getServiceManager().createInstanceWithContext
(
			"com.sun.star.frame.GlobalEventBroadcaster",
xRemoteContext);
			XEventBroadcaster xEventBroad = 
(XEventBroadcaster)UnoRuntime.queryInterface(XEventBroadcast
er.class, 
xGlobalBroadCaster);
			xEventBroad.addEventListener(new
com.sun.star.document.XEventListener() {
				public void
notifyEvent(com.sun.star.document.EventObject oEvent) {
//					 the control model which fired the event
					System.out.println("Evento da : " +
oEvent.Source + "  " + 
oEvent.EventName);
					XPropertySet xSourceProps = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,o
Event.Source);
					if (xSourceProps!=null) {
//						Property[] aoProps = 
xSourceProps.getPropertySetInfo().getProperties();
//						for(int iCount=0; iCount<aoProps.length;
iCount++) {
//							System.out.println("Property " + iCount +
": " + 
aoProps[iCount].Name);						
//						}
						//String sUid =
(String)xSourceProps.getPropertyValue("RuntimeUID"
);
					}
				}
				public void disposing(com.sun.star.lang.EventObject e)
{
				    System.out.println("On Dispose");
				}
			});
			
			//imposto le proprietà di apertura del documento.
			//In questo caso faccio in modo che non venga chiesto il
recovery in 
caso di crash.
			PropertyValue[] loadProps = new PropertyValue[1];
			loadProps[0] = new PropertyValue();
		    loadProps[0].Name = "NoRestore";
		    loadProps[0].Value = new Boolean(Boolean.TRUE);
			System.out.println(System.currentTimeMillis() + " -
Loading blank 
document");
			oBean.loadFromURL("private:factory/swriter",loa
dProps);
			//Thread.sleep(SLEEPTIME);
			String docUrl = 
createUNOFileURL("\\mobyv01\test\HP_DX2200_uT_P4_52
4.doc", oBean);
			System.out.println(System.currentTimeMillis() + " -
Loading document: 
" + docUrl);
			oBean.loadFromURL(docUrl, loadProps);
			//System.out.println(System.currentTimeMillis() + "
- Aquiring system 
window");
			//oBean.aquireSystemWindow();
			System.out.println(System.currentTimeMillis() + " -
Saving document");
		 
oBean.storeToURL(createUNOFileURL("\\mobyv01\test\p
rova.doc",oBean), 
null);
			System.out.println(System.currentTimeMillis() + " -
Document saved");		
			//Thread.sleep(SLEEPTIME);

			oBean.clear();
			oBean.loadFromURL(createUNOFileURL("\\mobyv01\tes
t\prova.doc", 
oBean), loadProps);
			//oBean.clear();
			System.out.println(System.currentTimeMillis() + " -
Finito");
			//System.out.println(System.currentTimeMillis() + "
- Stopping 
connection");
			//oBean.stopOOoConnection();
			//System.out.println("Connection stopped");
		} catch (Exception oEx) {
			System.out.println("Errore in creazione
officebean");
			oEx.printStackTrace();
		}
	}
	
	/**
      * Creating a correct File URL that OpenOffice can
handle. This is
      * necessary to be platform independent.
      *
      * param filelocation
      * return
      */
     public static String createUNOFileURL(String
filelocation, OOoBean 
oBean)
     {
         java.io.File newfile = new
java.io.File(filelocation);
         java.net.URL before = null;
         try
         {
             before = newfile.toURL();
         }
         catch (MalformedURLException e) {
             System.out.println(e);
         }

         String myUNOFileURL = "";
         try {
         // Create a URL, which can be used by UNO
         	myUNOFileURL = 
com.sun.star.uri.ExternalUriReferenceTranslator
         	 
.create(oBean.getOOoConnection().getComponentContext()).tran
slateToInternal(before.toExternalForm());
         } catch (Exception oEx) {
         	System.out.println("Errore: ");
         	oEx.printStackTrace();
         }
         if (myUNOFileURL.length() == 0 &&
filelocation.length() > 0)
         {
             System.out.println("File URL conversion
faild. Filelocation " +
                     "contains illegal characters:
" + filelocation);
         }
         return myUNOFileURL;
     }

}

------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribeapi.openoffice.org
For additional commands, e-mail: dev-helpapi.openoffice.org


Re: Re: intercepting onLoad events in java
user name
2007-01-17 10:50:53
news.gmane.org wrote:

> 		XModel oModel = 
>
(XModel)UnoRuntime.queryInterface(XModel.class,oEvent.Source
);
> 		if (oModel!=null) {
> 			System.out.println("Document URL is: " +
oModel.getURL());
> 		}
> 	}

You should be aware that getURL() will return an empty
string if the
document is an untitled one and as you can have more than of
untitled
document it doesn't qualify as an identifier.

You could use the document (reference) itself as a unique
identifier.
You then must register as a listener so you get a
notification when the
document is disposed so that you can release your
reference.

Best regards,
Mathias

-- 
Mathias Bauer (mba) - Project Lead OpenOffice.org Writer
OpenOffice.org Engineering at Sun: http://blogs.sun.com/Gu
llFOSS
Please don't reply to "nospamformbagmx.de".
I use it for the OOo lists and only rarely read other mails
sent to it.

------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribeapi.openoffice.org
For additional commands, e-mail: dev-helpapi.openoffice.org


Re: Re: intercepting onLoad events in java
user name
2007-01-18 11:26:35
news.gmane.org schrieb:

> Mathias Bauer ha scritto:
>> news.gmane.org wrote:
>> 
>>> 		XModel oModel = 
>>>
(XModel)UnoRuntime.queryInterface(XModel.class,oEvent.Source
);
>>> 		if (oModel!=null) {
>>> 			System.out.println("Document URL is:
" + oModel.getURL());
>>> 		}
>>> 	}
>> 
>> You should be aware that getURL() will return an
empty string if the
>> document is an untitled one and as you can have
more than of untitled
>> document it doesn't qualify as an identifier.
>> 
>> You could use the document (reference) itself as a
unique identifier.
>> You then must register as a listener so you get a
notification when the
>> document is disposed so that you can release your
reference.
> 
> The problem is: how do I get the document refernce from
the EventObject?
> I've tried this:
> 
> OfficeDocument oDocument = 
>
(OfficeDocument)UnoRuntime.queryInterface(OfficeDocument.cla
ss,oEvent.Source);
> 
> but I'm getting a ClassCastException.

OfficeDocument is not a valid type here. You have to use any
interface
from our documents - I would recommend to use
com.sun.star.XModel.

> And what about releasing the reference? Do you mean the
document 
> reference passed to the listener when constructing it?

I assumed that if you wanted to use the reference to the
document as an
identifier for it you will need to store it somewhere in a
list, a table
or whatsoever. In this case you must remove it from this
list again when
it gets disposed. For this purpose a listener comes in
handy.

Ciao,
Mathias

-- 
Mathias Bauer (mba) - Project Lead OpenOffice.org Writer
OpenOffice.org Engineering at Sun: http://blogs.sun.com/Gu
llFOSS
Please don't reply to "nospamformbagmx.de".
I use it for the OOo lists and only rarely read other mails
sent to it.

------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribeapi.openoffice.org
For additional commands, e-mail: dev-helpapi.openoffice.org


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )