Creating Services: XMultiServiceFactory


The CreateUnoService function creates services in the scope of the OOo application. The interface com.sun.star.lang.XMultiServiceFactory defines the object method createlnstance(), which allows an object to create a service in the scope of the object. The oSettings object in Listing 33 supports the service com.sun.star.text.DocumentSettings. The oSettings object is related to ThisComponent in that the document settings that it reflects are for ThisComponent-the current document-but not for any other document.

Listing 33: Create an object that supports the DocumentSettings service.
start example
 oSettings=ThisComponent.createInstance("com.sun.star.text.DocumentSettings") 
end example
 

The createlnstance() method returns an object that supports the requested service if it can; if it cannot, it returns NULL. The returned object may support other services as well (see Listing 34 and Figure 14 ).

click to expand
Figure 14: Some Writer document settings.
Listing 34: WriteDocumentSettings is found in the Generic module in this chapter's source code files as SC12.sxw.
start example
 Sub WriteDocumentSettings   Dim oSettings 'Settings object to create   Dim s$        'Utility string   Dim i%        'Utility index variable   Dim v         'This will contain an array of service names   REM Create an object that supports the DocumentSettings service   oSettings=ThisComponent.createInstance("com.sun.star.text.DocumentSettings")   v = oSettings.getSupportedServiceNames()   s = "**** specified Supported Services ****" & CHR$(10) & Join(v, CHR$(10))   s = s & CHR$(10) & CHR$(10) & "**** Tested Services ****" & CHR$(10)   REM Now check to see if this created object supports any other services.   v = Array("com.sun.star.comp.Writer.DocumentSettings",_             "com.sun.star.text.PrintSettings")   For i = 0 To UBound(v)     If oSettings.supportsService(v(i)) Then       s = s & "Supports service " & v(i) & CHR$(10)     End If   Next   MsgBox s, 0, "Some services for " & oSettings.getImplementationName()   REM What is the status of the PrintControls property?   Print oSettings.PrintControls   REM I could set this to True or False   'oSettings.PrintControls = True End Sub 
end example
 

A careful inspection of Listing 34 and Figure 14 reveal some unexpected behavior.

  • The requested service is listed as a supported service. This is expected.

  • The returned object supports three services, as returned by getSupportedServiceNames. One of the listed supported services, com.sun.star.comp.Writer.Settings, is not documented; at least I could not find it.

  • The method getlmplementationName() returns the service that uniquely identifies the returned service (com.sun.star.comp.Writer.DocumentSettings). The unique service name is not in the three listed supported service names.

Although not shown in Listing 34, the two "Tested Services" in Figure 14 cannot be created using the createlnstance() object method. I discovered the PrintSettings service entirely by accident . I inspected the dbg_properties property, which contained PrintControls. I then searched Google for "site:api.openoffice.org PrintSettings". The important things to notice are as follows :

  • There are different methods of inspecting an object; use all of them.

  • The documentation is not complete. You must manually inspect an object to see what it supports. However, undocumented methods and properties may be deprecated and removed later-so do this carefully .

  • You can search the Internet for services and properties.




OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net