UNO Service


A service abstractly defines an object by combining interfaces and properties. A UNO service typically consists of one or more interfaces and one or more UNO structures combined to encapsulate some useful functionality. A UNO interface defines how an object interacts with the outside world; a UNO structure defines a collection of data; and a UNO service combines them together. Like a UNO interface, a UNO service does not specify the implementation. It only specifies how to interact with the object.

Almost every UNO object is defined by a service, so UNO objects are called services. Strictly speaking, however, "a service" is only the definition. The UNO object is the actual object created as defined by the service. A service may include multiple services and interfaces. An interface usually defines a single aspect of a service and therefore is usually smaller in scope.

Sometimes, a service might have a name similar to an interface; look for the X in the name to determine if it is an interface or a service. For example, the com.sun.star.text.TextCursor service also implements other services and interfaces. When a service is defined, the creator of the definition marks some components as optional. Based on the XTextCursor interface, every TextCursor can move left and right a certain number of characters and jump to the start and end of the text. The TextCursor service, however, can optionally implement the com.sun.star.text.XWordCursor interface, which contains methods related to words.

The OOo Basic services are internally controlled and created by the process service manager-there's only one of these, and you can use it to create services. Use GetProcessServiceManager() to obtain a reference to the process service manager. You can then create a service from the process service manager by using its CreateInstance method, as shown in Listing 1 .

Listing 1: ManagerCreatesAService is found in the UNO module in this chapter's source code files as SC09.sxw.
start example
 Sub ManagerCreatesAService   Dim vFileAccess   Dim s As String   Dim vManager   vManager = GetProcessServiceManager()   vFileAccess = vManager.CreateInstance("com.sun.star.ucb.SimpleFileAccess")   s = vFileAccess.getContentType("http://www.pitonyak.org/AndrewMacro.sxw")   Print s End Sub 
end example
 

The code in Listing 1 obtains the process service manager, creates an instance of the SimpleFileAccess service, and then uses the created service. The CreateUnoService function is a shortcut for creating a UNO service (see Listing 2 ). The purpose of Listing 2 is to demonstrate the CreateUnoService function, showing that it's simpler than creating a service manager. Listing 2 also demonstrates some useful functionality, using a dialog to choose a file.

Listing 2: ChooseAFileName is found in the UNO module in this chapter's source code files as SC09.sxw.
start example
 Function ChooseAFileName() As String   Dim vFileDialog         'FilePicker service instance   Dim vFileAccess         'SimpleFileAccess service instance   Dim iAccept as Integer  'Response to the FilePicker   Dim sInitPath as String 'Hold the initial path   'Note: The following services MUST be called in the following order   'or Basic will not remove the FileDialog Service   vFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")   vFileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")   'Set the initial path here!   sInitPath = ConvertToUrl(CurDir)   If vFileAccess.Exists(sInitPath) Then     vFileDialog.SetDisplayDirectory(sInitPath)   End If   iAccept = vFileDialog.Execute()       'Run the file chooser dialog   If iAccept = 1 Then                   'What was the return value?     GetAFileName = vFileDialog.Files(0) 'Set file name if it was not canceled   End If   vFileDialog.Dispose()                 'Dispose of the dialog End Function 
end example
 

The code in Listing 2 creates two UNO services by using the function CreateUnoService. There are times, however, when the service manager is required. For example, the service manager has methods to create a service with arguments, CreateInstanceWithArguments, and to obtain a list of all supported services, getAvailableServiceNames(). The code in Listing 3 obtains a list of the supported service names ; there are 562 services on my computer.

Listing 3: HowManyServicesSupported is found in the UNO module in this chapter's source code files as SC09.sxw.
start example
 Sub HowManyServicesSupported   Dim vManager   Dim sServices   vManager = GetProcessServiceManager()   sServices = vManager.getAvailableServiceNames()   Print "Service manager supports ";UBound(sServices);" services" End Sub 
end example
 



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