Tag: cws_dev300_odbmacros3
User: fs      
Date: 2008-08-02 13:14:50+0000
Modified:
   dba/dbaccess/qa/complex/dbaccess/DatabaseDocument.java

Log:
 #i76128# testDocumentEvents: test the in-document version of OnLoad

File Changes:

Directory: /dba/dbaccess/qa/complex/dbaccess/
=============================================

File [changed]: DatabaseDocument.java
Url: 
http://dba.openoffice.org/source/browse/dba/dbaccess/qa/complex/dbaccess/DatabaseDocument.java?r1=1.1.2.6&r2=1.1.2.7
Delta lines:  +107 -14
----------------------
--- DatabaseDocument.java       2008-08-02 11:49:13+0000        1.1.2.6
+++ DatabaseDocument.java       2008-08-02 13:14:48+0000        1.1.2.7
@@ -7,7 +7,7 @@
  * OpenOffice.org - a multi-platform office productivity suite
  *
  * $RCSfile: DatabaseDocument.java,v $
- * $Revision: 1.1.2.6 $
+ * $Revision: 1.1.2.7 $
  *
  * This file is part of OpenOffice.org.
  *
@@ -35,16 +35,18 @@
 import com.sun.star.document.DocumentEvent;
 import com.sun.star.lang.XEventListener;
 import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.script.XStorageBasedLibraryContainer;
 import com.sun.star.uno.Exception;
+import com.sun.star.uno.Type;
 import com.sun.star.uno.UnoRuntime;
 import com.sun.star.frame.XStorable;
-import com.sun.star.frame.FrameSearchFlag;
 import com.sun.star.beans.PropertyValue;
 import com.sun.star.beans.XPropertySet;
+import com.sun.star.container.XNameContainer;
 import com.sun.star.container.XSet;
 import com.sun.star.document.XDocumentEventBroadcaster;
 import com.sun.star.document.XDocumentEventListener;
-import com.sun.star.document.XEventBroadcaster;
+import com.sun.star.document.XEmbeddedScripts;
 import com.sun.star.document.XEventsSupplier;
 import com.sun.star.document.XScriptInvocationContext;
 import com.sun.star.frame.DoubleInitializationException;
@@ -61,6 +63,7 @@
 import com.sun.star.lang.NotInitializedException;
 import com.sun.star.lang.XServiceInfo;
 import com.sun.star.lang.XSingleComponentFactory;
+import com.sun.star.lang.XTypeProvider;
 import com.sun.star.script.provider.XScriptProviderSupplier;
 import com.sun.star.sdb.XFormDocumentsSupplier;
 import com.sun.star.sdb.XOfficeDatabaseDocument;
@@ -89,7 +92,7 @@
     /** a helper class which can be used by the Basic scripts in our test 
documents
      *  to notify us of events in this document
      */
-    private class CallbackComponent implements XDocumentEventListener
+    private class CallbackComponent implements XDocumentEventListener, 
XTypeProvider
     {
         public void documentEventOccured( DocumentEvent _event )
         {
@@ -100,6 +103,22 @@
         {
             // not interested in
         }
+
+        public Type[] getTypes()
+        {
+            Class interfaces[] = getClass().getInterfaces();
+            Type types[] = new Type[interfaces.length];
+            for(int i = 0; i < interfaces.length; ++ i)
+            {
+                types[i] = new Type(interfaces[i]);
+            }
+            return types;
+        }
+
+        public byte[] getImplementationId()
+        {
+            return getClass().toString().getBytes();
+        }
     };
 
     private static String getCallbackComponentServiceName()
@@ -168,7 +187,8 @@
         return new String[]
         {
             "testLoadable",
-            "testDocumentEvents"
+            "testDocumentEvents",
+            "testGlobalEvents"
         };
     }
 
@@ -278,6 +298,19 @@
     }
 
     // 
--------------------------------------------------------------------------------------------------------
+    private String impl_correctFileURL( String _URL )
+    {
+        String returnURL = _URL;
+        if ( ( returnURL.indexOf( "file:/" ) == 0 ) && ( returnURL.indexOf( 
"file:///" ) == -1 ) )
+        {
+            // for some reason, the URLs here in Java start with "file:/" 
only, instead of "file:///"
+            // Some of the office code doesn't like this ...
+            returnURL = "file:///" + returnURL.substring( 6 );
+        }
+        return returnURL;
+    }
+
+    // 
--------------------------------------------------------------------------------------------------------
     private String impl_copyTempFile( String _sourceURL ) throws IOException
     {
         String targetURL = createTempFileURL();
@@ -287,14 +320,7 @@
         }
         catch ( URISyntaxException e ) { }
 
-        if ( ( targetURL.indexOf( "file:/" ) == 0 ) && ( targetURL.indexOf( 
"file:///" ) == -1 ) )
-        {
-            // for some reason, the URLs here in Java start with "file:/" 
only, instead of "file:///"
-            // Some of the office code doesn't like this ...
-            targetURL = "file:///" + targetURL.substring( 6 );
-        }
-
-        return targetURL;
+        return impl_correctFileURL( targetURL );
     }
 
     // 
--------------------------------------------------------------------------------------------------------
@@ -312,7 +338,6 @@
     // 
--------------------------------------------------------------------------------------------------------
     private void impl_closeDocument( XModel _databaseDoc ) throws 
CloseVetoException, IOException, Exception
     {
-        // close the document after rememberinbg its URL
         XCloseable closeDoc = (XCloseable)UnoRuntime.queryInterface( 
XCloseable.class,
             _databaseDoc );
         closeDoc.close( true );
@@ -413,8 +438,76 @@
     }
 
     // 
--------------------------------------------------------------------------------------------------------
+    private PropertyValue[] impl_getMacroExecLoadArgs()
+    {
+        return new PropertyValue[] {
+            new PropertyValue( "PickListEntry", 0, false, 
PropertyState.DIRECT_VALUE ),
+            new PropertyValue( "MacroExecutionMode", 0, 
com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE_NO_WARN, 
PropertyState.DIRECT_VALUE )
+        };
+    }
+
+    // 
--------------------------------------------------------------------------------------------------------
     public void testDocumentEvents() throws Exception, IOException
     {
+        // create an empty document
+        XModel databaseDoc = impl_createEmptyEmbeddedHSQLDocument();
+
+        // create Basic library/module therein
+        XEmbeddedScripts embeddedScripts = (XEmbeddedScripts) 
UnoRuntime.queryInterface( XEmbeddedScripts.class,
+            databaseDoc );
+        XStorageBasedLibraryContainer basicLibs = 
embeddedScripts.getBasicLibraries();
+        XNameContainer newLib = basicLibs.createLibrary( "EventHandlers" );
+        String eventHandlerCode =
+            "Option Explicit\n" +
+            "\n" +
+            "Sub OnLoad\n" +
+            "  Dim oCallback as Object\n" +
+            "  oCallback = createUnoService( \"" + 
getCallbackComponentServiceName() + "\" )\n" +
+            "\n" +
+            "  ' as long as the Document is not passed to the Basic callbacks, 
we need to create\n" +
+            "  ' one ourself\n" +
+            "  Dim oEvent as new com.sun.star.document.DocumentEvent\n" +
+            "  oEvent.EventName = \"OnLoad\"\n" +
+            "  oEvent.Source = ThisComponent\n" +
+            "\n" +
+            "  oCallback.documentEventOccured( oEvent )\n" +
+            "End Sub\n";
+        newLib.insertByName( "all", eventHandlerCode );
+
+        // bind the macro to the OnLoad event
+        String macroURI = 
"vnd.sun.star.script:EventHandlers.all.OnLoad?language=Basic&location=document";
+        XEventsSupplier eventsSupplier = 
(XEventsSupplier)UnoRuntime.queryInterface( XEventsSupplier.class,
+            databaseDoc );
+        eventsSupplier.getEvents().replaceByName( "OnLoad", new 
PropertyValue[] {
+            new PropertyValue( "EventType", 0, "Script", 
PropertyState.DIRECT_VALUE ),
+            new PropertyValue( "Script", 0, macroURI, 
PropertyState.DIRECT_VALUE )
+        } );
+
+        // store the document, and close it
+        String documentURL = databaseDoc.getURL();
+        documentURL = impl_correctFileURL( documentURL );
+        XStorable storeDoc = (XStorable) UnoRuntime.queryInterface( 
XStorable.class,
+            databaseDoc );
+        storeDoc.store();
+        impl_closeDocument( databaseDoc );
+
+        // reload it
+        XComponentLoader loader = (XComponentLoader)UnoRuntime.queryInterface( 
XComponentLoader.class,
+            getORB().createInstance( "com.sun.star.frame.Desktop" ) );
+
+        String context = "OnLoad";
+        impl_startObservingEvents( context );
+        databaseDoc = (XModel)UnoRuntime.queryInterface( XModel.class,
+            loader.loadComponentFromURL( documentURL, "_blank", 0, 
impl_getMacroExecLoadArgs() ) );
+        impl_stopObservingEvents( m_documentEvents, new String[] { "OnLoad" }, 
context );
+
+        // close the document
+        impl_closeDocument( databaseDoc );
+    }
+
+    // 
--------------------------------------------------------------------------------------------------------
+    public void testGlobalEvents() throws Exception, IOException
+    {
         XModel databaseDoc = impl_createEmptyEmbeddedHSQLDocument();
         XStorable storeDoc = (XStorable) UnoRuntime.queryInterface( 
XStorable.class,
             databaseDoc );




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to