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().getAvailableServiceNames();
                        System.out.println(st2);
Object xGlobalBroadCaster = xRemoteContext.getServiceManager().createInstanceWithContext(
                        "com.sun.star.frame.GlobalEventBroadcaster", 
xRemoteContext);
XEventBroadcaster xEventBroad = (XEventBroadcaster)UnoRuntime.queryInterface(XEventBroadcaster.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,oEvent.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",loadProps);
                        //Thread.sleep(SLEEPTIME);
String docUrl = createUNOFileURL("\\\\mobyv01\\test\\HP_DX2200_uT_P4_524.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\\prova.doc",oBean), null);
                        System.out.println(System.currentTimeMillis() + " - Document 
saved");         
                        //Thread.sleep(SLEEPTIME);

                        oBean.clear();
oBean.loadFromURL(createUNOFileURL("\\\\mobyv01\\test\\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()).translateToInternal(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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to