SF_Register.xba 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 =======================================================================================================================
  8. Option Compatible
  9. Option Explicit
  10. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  11. &apos;&apos;&apos; SF_Register
  12. &apos;&apos;&apos; ===========
  13. &apos;&apos;&apos; The ScriptForge framework includes
  14. &apos;&apos;&apos; the master ScriptForge library
  15. &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
  16. &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
  17. &apos;&apos;&apos;
  18. &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
  19. &apos;&apos;&apos; - RegisterScriptServices
  20. &apos;&apos;&apos; Register the list of services implemented by the current library
  21. &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
  22. REM ================================================================== EXCEPTIONS
  23. REM ============================================================== PUBLIC METHODS
  24. REM -----------------------------------------------------------------------------
  25. Public Sub RegisterScriptServices() As Variant
  26. &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
  27. &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
  28. &apos;&apos;&apos;
  29. &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
  30. &apos;&apos;&apos; with 2 arguments:
  31. &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
  32. &apos;&apos;&apos; ServiceReference: the reference as an object
  33. &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
  34. &apos;&apos;&apos; GlobalScope.Library.Module
  35. &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
  36. &apos;&apos;&apos; containing the New statement creating the instance
  37. &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
  38. With GlobalScope.ScriptForge.SF_Services
  39. .RegisterService(&quot;Document&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Reference to the function initializing the service
  40. .RegisterService(&quot;Calc&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Same references, distinction is made inside the function
  41. .RegisterService(&quot;Base&quot;, &quot;SFDocuments.SF_Register._NewDocument&quot;) &apos; Same references, distinction is made inside the function
  42. .RegisterEventManager(&quot;DocumentEvent&quot;, &quot;SFDocuments.SF_Register._EventManager&quot;) &apos; Reference to the events manager
  43. &apos;TODO
  44. End With
  45. End Sub &apos; SFDocuments.SF_Register.RegisterScriptServices
  46. REM =========================================================== PRIVATE FUNCTIONS
  47. REM -----------------------------------------------------------------------------
  48. Public Function _EventManager(Optional ByRef pvArgs As Variant) As Object
  49. &apos;&apos;&apos; Returns a Document or Calc object corresponding with the active component
  50. &apos;&apos;&apos; which triggered the event in argument
  51. &apos;&apos;&apos; This method should be triggered only thru the invocation of CreateScriptService
  52. &apos;&apos;&apos; Args:
  53. &apos;&apos;&apos; pvEvent: com.sun.star.document.DocumentEvent
  54. &apos;&apos;&apos; Returns:
  55. &apos;&apos;&apos; the output of a Document, Calc, ... service or Nothing
  56. &apos;&apos;&apos; Example:
  57. &apos;&apos;&apos; Sub TriggeredByEvent(ByRef poEvent As Object)
  58. &apos;&apos;&apos; Dim oDoc As Object
  59. &apos;&apos;&apos; Set oDoc = CreateScriptService(&quot;SFDocuments.DocumentEvent&quot;, poEvent)
  60. &apos;&apos;&apos; If Not IsNull(oDoc) Then
  61. &apos;&apos;&apos; &apos; ... (a valid document has been identified)
  62. &apos;&apos;&apos; End Sub
  63. Dim oSource As Object &apos; Return value
  64. Dim vEvent As Variant &apos; Alias of pvArgs(0)
  65. &apos; Never abort while an event is processed
  66. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Finally
  67. Set oSource = Nothing
  68. Check:
  69. If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
  70. If UBound(pvArgs) &gt;= 0 Then vEvent = pvArgs(0) Else Set vEvent = Empty
  71. If VarType(vEvent) &lt;&gt; ScriptForge.V_OBJECT Then GoTo Finally
  72. Try:
  73. If ScriptForge.SF_Session.UnoObjectType(vEvent) = &quot;com.sun.star.document.DocumentEvent&quot; Then
  74. If ScriptForge.SF_Session.UnoObjectType(vEvent.Source) = &quot;SwXTextDocument&quot; Then
  75. Set oSource = SF_Register._NewDocument(vEvent.Source)
  76. End If
  77. End If
  78. Finally:
  79. Set _EventManager = oSource
  80. Exit Function
  81. End Function &apos; SFDocuments.SF_Documents._EventManager
  82. REM -----------------------------------------------------------------------------
  83. Public Function _NewDocument(Optional ByVal pvArgs As Variant) As Object
  84. &apos;&apos;&apos; Create a new instance of the (super) SF_Document class or of one of its subclasses (SF_Calc, ...)
  85. &apos; Args:
  86. &apos;&apos;&apos; WindowName: see the definition of WindowName in the description of the UI service
  87. &apos;&apos;&apos; If absent, the document is presumed to be in the active window
  88. &apos;&apos;&apos; If WindowName is an object, it must be a component
  89. &apos;&apos;&apos; (com.sun.star.lang.XComponent or com.sun.star.comp.dba.ODatabaseDocument)
  90. &apos;&apos;&apos; Returns: the instance or Nothing
  91. Dim oDocument As Object &apos; Return value
  92. Dim oSuperDocument As Object &apos; Companion superclass document
  93. Dim vWindowName As Variant &apos; Alias of pvArgs(0)
  94. Dim oEnum As Object &apos; com.sun.star.container.XEnumeration
  95. Dim oComp As Object &apos; com.sun.star.lang.XComponent
  96. Dim vWindow As Window &apos; A single component
  97. Dim oUi As Object &apos; &quot;UI&quot; service
  98. Dim bFound As Boolean &apos; True if the document is found on the desktop
  99. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  100. Check:
  101. If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
  102. If Not IsArray(pvArgs) Then pvArgs = Array(pvArgs) &apos; Needed when _NewDocument called from _EventManager
  103. If UBound(pvArgs) &gt;= 0 Then vWindowName = pvArgs(0) Else vWindowName = &quot;&quot;
  104. If Not ScriptForge.SF_Utils._Validate(vWindowName, &quot;WindowName&quot;, Array(V_STRING, ScriptForge.V_OBJECT)) Then GoTo Finally
  105. Set oDocument = Nothing
  106. Try:
  107. Set oUi = ScriptForge.SF_Register.CreateScriptService(&quot;UI&quot;)
  108. Select Case VarType(vWindowName)
  109. Case V_STRING
  110. If Len(vWindowName) &gt; 0 Then
  111. bFound = False
  112. Set oEnum = StarDesktop.Components().createEnumeration
  113. Do While oEnum.hasMoreElements
  114. Set oComp = oEnum.nextElement
  115. vWindow = oUi._IdentifyWindow(oComp)
  116. With vWindow
  117. &apos; Does the current window match the argument ?
  118. If (Len(.WindowFileName) &gt; 0 And .WindowFileName = ScriptForge.SF_FileSystem._ConvertToUrl(vWindowName)) _
  119. Or (Len(.WindowName) &gt; 0 And .WindowName = vWindowName) _
  120. Or (Len(.WindowTitle) &gt; 0 And .WindowTitle = vWindowName) Then
  121. bFound = True
  122. Exit Do
  123. End If
  124. End With
  125. Loop
  126. Else
  127. bFound = True
  128. vWindow = oUi._IdentifyWindow(StarDesktop.CurrentComponent)
  129. End If
  130. Case ScriptForge.V_OBJECT &apos; com.sun.star.lang.XComponent
  131. bFound = True
  132. vWindow = oUi._IdentifyWindow(vWindowName)
  133. End Select
  134. If bFound And Not IsNull(vWindow.Frame) And Len(vWindow.DocumentType) &gt; 0 Then
  135. &apos; Create the right subclass and associate to it a new instance of the superclass
  136. Select Case vWindow.DocumentType
  137. Case &quot;Base&quot;
  138. Set oDocument = New SF_Base
  139. Set oSuperDocument = New SF_Document
  140. Set oDocument.[_Super] = oSuperDocument &apos; Now both super and subclass are twinned
  141. Case &quot;Calc&quot;
  142. Set oDocument = New SF_Calc
  143. Set oSuperDocument = New SF_Document
  144. Set oDocument.[_Super] = oSuperDocument &apos; Now both super and subclass are twinned
  145. Case Else &apos; Only superclass
  146. Set oDocument = New SF_Document
  147. Set oSuperDocument = oDocument
  148. End Select
  149. With oDocument &apos; Initialize attributes of subclass
  150. Set .[Me] = oDocument
  151. Set ._Component = vWindow.Component
  152. &apos; Initialize specific attributes
  153. Select Case vWindow.DocumentType
  154. Case &quot;Base&quot;
  155. Set ._DataSource = ._Component.DataSource
  156. Case Else
  157. End Select
  158. End With
  159. With oSuperDocument &apos; Initialize attributes of superclass
  160. Set .[Me] = oSuperDocument
  161. Set ._Component = vWindow.Component
  162. Set ._Frame = vWindow.Frame
  163. ._WindowName = vWindow.WindowName
  164. ._WindowTitle = vWindow.WindowTitle
  165. ._WindowFileName = vWindow.WindowFileName
  166. ._DocumentType = vWindow.DocumentType
  167. End With
  168. End If
  169. Finally:
  170. Set _NewDocument = oDocument
  171. Exit Function
  172. Catch:
  173. GoTo Finally
  174. End Function &apos; SFDocuments.SF_Register._NewDocument
  175. REM ============================================== END OF SFDOCUMENTS.SF_REGISTER
  176. </script:module>