1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"SF_Register" script:
language=
"StarBasic" script:
moduleType=
"normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === The SFDocuments library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
12 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
13 ''' SF_Register
14 ''' ===========
15 ''' The ScriptForge framework includes
16 ''' the master ScriptForge library
17 ''' a number of
"associated
" libraries SF*
18 ''' any user/contributor extension wanting to fit into the framework
20 ''' The main methods in this module allow the current library to cling to ScriptForge
21 ''' - RegisterScriptServices
22 ''' Register the list of services implemented by the current library
23 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
25 REM ================================================================== EXCEPTIONS
27 REM ============================================================== PUBLIC METHODS
29 REM -----------------------------------------------------------------------------
30 Public Sub RegisterScriptServices() As Variant
31 ''' Register into ScriptForge the list of the services implemented by the current library
32 ''' Each library pertaining to the framework must implement its own version of this method
34 ''' It consists in successive calls to the RegisterService() and RegisterEventManager() methods
35 ''' with
2 arguments:
36 ''' ServiceName: the name of the service as a case-insensitive string
37 ''' ServiceReference: the reference as an object
38 ''' If the reference refers to a module, then return the module as an object:
39 ''' GlobalScope.Library.Module
40 ''' If the reference is a class instance, then return a string referring to the method
41 ''' containing the New statement creating the instance
42 ''' "libraryname.modulename.function
"
44 With GlobalScope.ScriptForge.SF_Services
45 .RegisterService(
"Document
",
"SFDocuments.SF_Register._NewDocument
")
' Reference to the function initializing the service
46 .RegisterService(
"Calc
",
"SFDocuments.SF_Register._NewDocument
")
' Same references, distinction is made inside the function
47 .RegisterService(
"Base
",
"SFDocuments.SF_Register._NewDocument
")
' Same references, distinction is made inside the function
48 .RegisterEventManager(
"DocumentEvent
",
"SFDocuments.SF_Register._EventManager
")
' Reference to the events manager
52 End Sub
' SFDocuments.SF_Register.RegisterScriptServices
54 REM =========================================================== PRIVATE FUNCTIONS
56 REM -----------------------------------------------------------------------------
57 Public Function _EventManager(Optional ByRef pvArgs As Variant) As Object
58 ''' Returns a Document or Calc object corresponding with the active component
59 ''' which triggered the event in argument
60 ''' This method should be triggered only thru the invocation of CreateScriptService
61 ''' Args:
62 ''' pvEvent: com.sun.star.document.DocumentEvent
63 ''' Returns:
64 ''' the output of a Document, Calc, ... service or Nothing
65 ''' Example:
66 ''' Sub TriggeredByEvent(ByRef poEvent As Object)
67 ''' Dim oDoc As Object
68 ''' Set oDoc = CreateScriptService(
"SFDocuments.DocumentEvent
", poEvent)
69 ''' If Not IsNull(oDoc) Then
70 ''' ' ... (a valid document has been identified)
71 ''' End Sub
73 Dim oSource As Object
' Return value
74 Dim vEvent As Variant
' Alias of pvArgs(
0)
76 ' Never abort while an event is processed
77 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Finally
81 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
82 If UBound(pvArgs)
>=
0 Then vEvent = pvArgs(
0) Else Set vEvent = Empty
83 If VarType(vEvent)
<> ScriptForge.V_OBJECT Then GoTo Finally
86 If ScriptForge.SF_Session.UnoObjectType(vEvent) =
"com.sun.star.document.DocumentEvent
" Then
87 If ScriptForge.SF_Session.UnoObjectType(vEvent.Source) =
"SwXTextDocument
" Then
88 Set oSource = SF_Register._NewDocument(vEvent.Source)
93 Set _EventManager = oSource
95 End Function
' SFDocuments.SF_Documents._EventManager
97 REM -----------------------------------------------------------------------------
98 Public Function _NewDocument(Optional ByVal pvArgs As Variant) As Object
99 ''' Create a new instance of the (super) SF_Document class or of one of its subclasses (SF_Calc, ...)
101 ''' WindowName: see the definition of WindowName in the description of the UI service
102 ''' If absent, the document is presumed to be in the active window
103 ''' If WindowName is an object, it must be a component
104 ''' (com.sun.star.lang.XComponent or com.sun.star.comp.dba.ODatabaseDocument)
105 ''' Returns: the instance or Nothing
107 Dim oDocument As Object
' Return value
108 Dim oSuperDocument As Object
' Companion superclass document
109 Dim vWindowName As Variant
' Alias of pvArgs(
0)
110 Dim oEnum As Object
' com.sun.star.container.XEnumeration
111 Dim oComp As Object
' com.sun.star.lang.XComponent
112 Dim vWindow As Window
' A single component
113 Dim oUi As Object
' "UI
" service
114 Dim bFound As Boolean
' True if the document is found on the desktop
116 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
119 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
120 If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs)
' Needed when _NewDocument called from _EventManager
121 If UBound(pvArgs)
>=
0 Then vWindowName = pvArgs(
0) Else vWindowName =
""
122 If Not ScriptForge.SF_Utils._Validate(vWindowName,
"WindowName
", Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally
123 Set oDocument = Nothing
126 Set oUi = ScriptForge.SF_Register.CreateScriptService(
"UI
")
127 Select Case VarType(vWindowName)
129 If Len(vWindowName)
> 0 Then
131 Set oEnum = StarDesktop.Components().createEnumeration
132 Do While oEnum.hasMoreElements
133 Set oComp = oEnum.nextElement
134 vWindow = oUi._IdentifyWindow(oComp)
136 ' Does the current window match the argument ?
137 If (Len(.WindowFileName)
> 0 And .WindowFileName = ScriptForge.SF_FileSystem._ConvertToUrl(vWindowName)) _
138 Or (Len(.WindowName)
> 0 And .WindowName = vWindowName) _
139 Or (Len(.WindowTitle)
> 0 And .WindowTitle = vWindowName) Then
147 vWindow = oUi._IdentifyWindow(StarDesktop.CurrentComponent)
149 Case ScriptForge.V_OBJECT
' com.sun.star.lang.XComponent
151 vWindow = oUi._IdentifyWindow(vWindowName)
154 If bFound And Not IsNull(vWindow.Frame) And Len(vWindow.DocumentType)
> 0 Then
155 ' Create the right subclass and associate to it a new instance of the superclass
156 Select Case vWindow.DocumentType
157 Case
"Base
"
158 Set oDocument = New SF_Base
159 Set oSuperDocument = New SF_Document
160 Set oDocument.[_Super] = oSuperDocument
' Now both super and subclass are twinned
161 Case
"Calc
"
162 Set oDocument = New SF_Calc
163 Set oSuperDocument = New SF_Document
164 Set oDocument.[_Super] = oSuperDocument
' Now both super and subclass are twinned
165 Case Else
' Only superclass
166 Set oDocument = New SF_Document
167 Set oSuperDocument = oDocument
169 With oDocument
' Initialize attributes of subclass
170 Set .[Me] = oDocument
171 Set ._Component = vWindow.Component
172 ' Initialize specific attributes
173 Select Case vWindow.DocumentType
174 Case
"Base
"
175 Set ._DataSource = ._Component.DataSource
179 With oSuperDocument
' Initialize attributes of superclass
180 Set .[Me] = oSuperDocument
181 Set ._Component = vWindow.Component
182 Set ._Frame = vWindow.Frame
183 ._WindowName = vWindow.WindowName
184 ._WindowTitle = vWindow.WindowTitle
185 ._WindowFileName = vWindow.WindowFileName
186 ._DocumentType = vWindow.DocumentType
191 Set _NewDocument = oDocument
195 End Function
' SFDocuments.SF_Register._NewDocument
197 REM ============================================== END OF SFDOCUMENTS.SF_REGISTER