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_Datasheet" 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 SFDatabases library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
14 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
15 ''' SF_Datasheet
16 ''' ============
17 ''' A datasheet is the visual representation of tabular data produced by a database.
18 ''' In the user interface of LibreOffice it is the result of the opening of
19 ''' a table or a query. In this case the concerned Base document must be open.
21 ''' In the context of ScriptForge, a datasheet may be opened automatically by script code :
22 ''' - either by reproducing the behaviour of the user interface
23 ''' - or at any moment. In this case the Base document may or may not be opened.
24 ''' Additionally, any SELECT SQL statement may define the datasheet display.
26 ''' The proposed API allows for either datasheets (opened manually of by code) in particular
27 ''' to know which cell is selected and its content.
29 ''' Service invocation:
30 ''' 1) From an open Base document
31 ''' Set ui = CreateScriptService(
"UI
")
32 ''' Set oBase = ui.getDocument(
"/home/user/Documents/myDb.odb
")
33 ''' Set oSheet = oBase.OpenTable(
"Customers
")
' or OpenQuery(...)
34 ''' ' May be executed also when the given table is already open
35 ''' 2) Independently from a Base document
36 ''' Set oDatabase = CreateScriptService(
"Database
",
"/home/user/Documents/myDb.odb
")
37 ''' Set oSheet = oDatabase.OpenTable(
"Customers
")
39 ''' Detailed user documentation:
40 ''' https://help.libreoffice.org/latest/en-US/text/sbasic/shared/
03/sf_datasheet.html?DbPAR=BASIC
41 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
43 REM ================================================================== EXCEPTIONS
45 Private Const DOCUMENTDEADERROR =
"DOCUMENTDEADERROR
"
47 REM ============================================================= PRIVATE MEMBERS
49 Private [Me] As Object
50 Private [_Parent] As Object
' Base instance when opened from a Base document by code
51 ' or Database instance when opened without Base document
52 Private ObjectType As String
' Must be DATASHEET
53 Private ServiceName As String
55 Private _Component As Object
' com.sun.star.lang.XComponent - org.openoffice.comp.dbu.ODatasourceBrowser
56 Private _Frame As Object
' com.sun.star.frame.XFrame
57 Private _ParentBase As Object
' The parent SF_Base instance (may be void)
58 Private _ParentDatabase As Object
' The parent SF_Database instance (must not be void)
59 Private _SheetType As String
' TABLE, QUERY or SQL
60 Private _ParentType As String
' BASE or DATABASE
61 Private _BaseFileName As String
' URL format of parent Base file
62 Private _Command As String
' Table name, query name or SQL statement
63 Private _DirectSql As Boolean
' When True, SQL processed by RDBMS
64 Private _TabControllerModel As Object
' com.sun.star.awt.XTabControllerModel - com.sun.star.comp.forms.ODatabaseForm
65 Private _ControlModel As Object
' com.sun.star.awt.XControlModel - com.sun.star.form.OGridControlModel
66 Private _ControlView As Object
' com.sun.star.awt.XControl - org.openoffice.comp.dbu.ODatasourceBrowser
67 Private _ColumnHeaders As Variant
' List of column headers as an array of strings
69 ' Cache for static toolbar descriptions
70 Private _Toolbars As Object
' SF_Dictionary instance to hold toolbars stored in application or in document
72 REM ============================================================ MODULE CONSTANTS
74 REM ====================================================== CONSTRUCTOR/DESTRUCTOR
76 REM -----------------------------------------------------------------------------
77 Private Sub Class_Initialize()
79 Set [_Parent] = Nothing
80 ObjectType =
"DATASHEET
"
81 ServiceName =
"SFDatabases.Datasheet
"
82 Set _Component = Nothing
84 Set _ParentBase = Nothing
85 Set _ParentDatabase = Nothing
86 _SheetType =
""
87 _ParentType =
""
88 _BaseFileName =
""
89 _Command =
""
91 Set _TabControllerModel = Nothing
92 Set _ControlModel = Nothing
93 Set _ControlView = Nothing
94 _ColumnHeaders = Array()
95 Set _Toolbars = Nothing
96 End Sub
' SFDatabases.SF_Datasheet Constructor
98 REM -----------------------------------------------------------------------------
99 Private Sub Class_Terminate()
100 Call Class_Initialize()
101 End Sub
' SFDatabases.SF_Datasheet Destructor
103 REM -----------------------------------------------------------------------------
104 Public Function Dispose() As Variant
105 Call Class_Terminate()
106 Set Dispose = Nothing
107 End Function
' SFDatabases.SF_Datasheet Explicit Destructor
109 REM ================================================================== PROPERTIES
111 REM -----------------------------------------------------------------------------
112 Property Get ColumnHeaders() As Variant
113 ''' Returns the list of column headers of the datasheet as an array of strings
114 ColumnHeaders = _PropertyGet(
"ColumnHeaders
")
115 End Property
' SFDatabases.SF_Datasheet.ColumnHeaders
117 REM -----------------------------------------------------------------------------
118 Property Get CurrentColumn() As String
119 ''' Returns the currently selected column by its name
120 CurrentColumn = _PropertyGet(
"CurrentColumn
")
121 End Property
' SFDatabases.SF_Datasheet.CurrentColumn
123 REM -----------------------------------------------------------------------------
124 Property Get CurrentRow() As Long
125 ''' Returns the currently selected row by its number
>=
1
126 CurrentRow = _PropertyGet(
"CurrentRow
")
127 End Property
' SFDatabases.SF_Datasheet.CurrentRow
129 REM -----------------------------------------------------------------------------
130 Property Get DatabaseFileName() As String
131 ''' Returns the file name of the Base file in FSO.FileNaming format
132 DatabaseFileName = _PropertyGet(
"DatabaseFileName
")
133 End Property
' SFDatabases.SF_Datasheet.DatabaseFileName
135 REM -----------------------------------------------------------------------------
136 Property Get Filter() As Variant
137 ''' The Filter is a SQL WHERE clause without the WHERE keyword
138 Filter = _PropertyGet(
"Filter
")
139 End Property
' SFDatabases.SF_Datasheet.Filter (get)
141 REM -----------------------------------------------------------------------------
142 Property Let Filter(Optional ByVal pvFilter As Variant)
143 ''' Set the updatable property Filter
144 ''' Table and field names may be surrounded by square brackets
145 ''' When the argument is the zero-length string, the actual filter is removed
146 _PropertySet(
"Filter
", pvFilter)
147 End Property
' SFDatabases.SF_Datasheet.Filter (let)
149 REM -----------------------------------------------------------------------------
150 Property Get LastRow() As Long
151 ''' Returns the total number of rows
152 ''' The process may imply to move the cursor to the last available row.
153 ''' Afterwards the cursor is reset to the current row.
154 LastRow = _PropertyGet(
"LastRow
")
155 End Property
' SFDatabases.SF_Datasheet.LastRow
157 REM -----------------------------------------------------------------------------
158 Property Get OrderBy() As Variant
159 ''' The Order is a SQL ORDER BY clause without the ORDER BY keywords
160 OrderBy = _PropertyGet(
"OrderBy
")
161 End Property
' SFDocuments.SF_Form.OrderBy (get)
163 REM -----------------------------------------------------------------------------
164 Property Let OrderBy(Optional ByVal pvOrderBy As Variant)
165 ''' Set the updatable property OrderBy
166 ''' Table and field names may be surrounded by square brackets
167 ''' When the argument is the zero-length string, the actual sort is removed
168 _PropertySet(
"OrderBy
", pvOrderBy)
169 End Property
' SFDocuments.SF_Form.OrderBy (let)
171 REM -----------------------------------------------------------------------------
172 Property Get ParentDatabase() As Object
173 ''' Returns the database instance to which the datasheet belongs
174 Set ParentDatabase = _PropertyGet(
"ParentDatabase
")
175 End Property
' SFDatabases.SF_Datasheet.ParentDatabase
177 REM -----------------------------------------------------------------------------
178 Property Get Source() As String
179 ''' Returns the source of the data: table name, query name or sql statement
180 Source = _PropertyGet(
"Source
")
181 End Property
' SFDatabases.SF_Datasheet.Source
183 REM -----------------------------------------------------------------------------
184 Property Get SourceType() As String
185 ''' Returns thetype of source of the data: TABLE, QUERY or SQL
186 SourceType = _PropertyGet(
"SourceType
")
187 End Property
' SFDatabases.SF_Datasheet.SourceType
189 REM -----------------------------------------------------------------------------
190 Property Get XComponent() As Object
191 ''' Returns the com.sun.star.lang.XComponent UNO object representing the datasheet
192 XComponent = _PropertyGet(
"XComponent
")
193 End Property
' SFDocuments.SF_Document.XComponent
195 REM -----------------------------------------------------------------------------
196 Property Get XControlModel() As Object
197 ''' Returns the com.sun.star.lang.XControl UNO object representing the datasheet
198 XControlModel = _PropertyGet(
"XControlModel
")
199 End Property
' SFDocuments.SF_Document.XControlModel
201 REM -----------------------------------------------------------------------------
202 Property Get XTabControllerModel() As Object
203 ''' Returns the com.sun.star.lang.XTabControllerModel UNO object representing the datasheet
204 XTabControllerModel = _PropertyGet(
"XTabControllerModel
")
205 End Property
' SFDocuments.SF_Document.XTabControllerModel
207 REM ===================================================================== METHODS
209 REM -----------------------------------------------------------------------------
210 Public Sub Activate()
211 ''' Make the actual datasheet active
212 ''' Args:
213 ''' Returns:
214 ''' Examples:
215 ''' oSheet.Activate()
217 Dim oContainer As Object
' com.sun.star.awt.XWindow
218 Const cstThisSub =
"SFDatabases.Datasheet.Activate
"
219 Const cstSubArgs =
""
221 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
224 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
225 If Not _IsStillAlive() Then GoTo Finally
228 Set oContainer = _Component.Frame.ContainerWindow
230 If .isVisible() = False Then .setVisible(True)
233 .toFront()
' Force window change in Linux
234 Wait
1 ' Bypass desynchro issue in Linux
238 SF_Utils._ExitFunction(cstThisSub)
242 End Sub
' SFDatabases.SF_Datasheet.Activate
244 REM -----------------------------------------------------------------------------
245 Public Function CloseDatasheet() As Boolean
246 ''' Close the actual datasheet
247 ''' Args:
248 ''' Returns:
249 ''' True when successful
250 ''' Examples:
251 ''' oSheet.CloseDatasheet()
253 Dim bClose As Boolean
' Return value
254 Const cstThisSub =
"SFDatabases.Datasheet.CloseDatasheet
"
255 Const cstSubArgs =
""
257 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
261 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
262 If Not _IsStillAlive() Then GoTo Finally
265 With _TabControllerModel
267 .Filter =
""
276 CloseDatasheet = bClose
277 SF_Utils._ExitFunction(cstThisSub)
281 End Function
' SFDatabases.SF_Datasheet.CloseDatasheet
283 REM -----------------------------------------------------------------------------
284 Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
285 , Optional ByVal Before As Variant _
286 , Optional ByVal SubmenuChar As Variant _
288 ''' Create a new menu entry in the datasheet
's menubar
289 ''' The menu is not intended to be saved neither in the LibreOffice global environment, nor elsewhere
290 ''' The method returns a SFWidgets.Menu instance. Its methods let define the menu further.
291 ''' Args:
292 ''' MenuHeader: the name/header of the menu
293 ''' Before: the place where to put the new menu on the menubar (string or number
>=
1)
294 ''' When not found =
> last position
295 ''' SubmenuChar: the delimiter used in menu trees. Default =
">"
296 ''' Returns:
297 ''' A SFWidgets.Menu instance or Nothing
298 ''' Examples:
299 ''' Dim oMenu As Object
300 ''' Set oMenu = oDoc.CreateMenu(
"My menu
", Before :=
"Styles
")
301 ''' With oMenu
302 ''' .AddItem(
"Item
1", Command :=
".uno:About
")
303 ''' '...
304 ''' .Dispose()
' When definition is complete, the menu instance may be disposed
305 ''' End With
306 ''' ' ...
308 Dim oMenu As Object
' return value
309 Const cstThisSub =
"SFDatabases.Datasheet.CreateMenu
"
310 Const cstSubArgs =
"MenuHeader, [Before=
""""], [SubmenuChar=
"">""]
"
312 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
316 If IsMissing(Before) Or IsEmpty(Before) Then Before =
""
317 If IsMissing(SubmenuChar) Or IsEmpty(SubmenuChar) Then SubmenuChar =
""
319 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
320 If Not _IsStillAlive() Then GoTo Finally
321 If Not ScriptForge.SF_Utils._Validate(MenuHeader,
"MenuHeader
", V_STRING) Then GoTo Finally
322 If Not ScriptForge.SF_Utils._Validate(Before,
"Before
", V_STRING) Then GoTo Finally
323 If Not ScriptForge.SF_Utils._Validate(SubmenuChar,
"SubmenuChar
", V_STRING) Then GoTo Finally
327 Set oMenu = ScriptForge.SF_Services.CreateScriptService(
"SFWidgets.Menu
", _Component, MenuHeader, Before, SubmenuChar)
330 Set CreateMenu = oMenu
331 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
335 End Function
' SFDatabases.SF_Document.CreateMenu
337 REM -----------------------------------------------------------------------------
338 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
339 ''' Return the actual value of the given property
340 ''' Args:
341 ''' PropertyName: the name of the property as a string
342 ''' Returns:
343 ''' The actual value of the propRATTCerty
344 ''' If the property does not exist, returns Null
346 Const cstThisSub =
"SFDatabases.Datasheet.GetProperty
"
347 Const cstSubArgs =
""
349 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
353 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
354 If Not ScriptForge.SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
358 GetProperty = _PropertyGet(PropertyName)
361 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
365 End Function
' SFDatabases.SF_Datasheet.GetProperty
367 REM -----------------------------------------------------------------------------
368 Public Function GetText(Optional ByVal Column As Variant) As String
369 ''' Get the text in the given column of the current row.
370 ''' Args:
371 ''' Column: the name of the column as a string or its position (
>=
1). Default = the current column
372 ''' If the argument exceeds the number of columns, the last column is selected.
373 ''' Returns:
374 ''' The text in the cell as a string as how it is displayed
375 ''' Note that the position of the cursor is left unchanged.
376 ''' Examples:
377 ''' oSheet.GetText(
"ShipCity
"))
' Extract the text on the current row from the column
"ShipCity
"
379 Dim sText As String
' Return Text
380 Dim lCol As Long
' Numeric index of Column in lists of columns
381 Dim lMaxCol As Long
' Index of last column
382 Const cstThisSub =
"SFDatabases.Datasheet.GetText
"
383 Const cstSubArgs =
"[Column=
0]
"
385 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
389 If IsMissing(Column) Or IsEmpty(Column) Then Column =
0
390 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
391 If Not _IsStillAlive() Then GoTo Finally
392 If VarType(Column)
<> V_STRING Then
393 If Not ScriptForge.SF_Utils._Validate(Column,
"Column
", ScriptForge.V_NUMERIC) Then GoTo Catch
395 If Not ScriptForge.SF_Utils._Validate(Column,
"Column
", V_STRING, _ColumnHeaders) Then GoTo Catch
400 ' Position the column - The index to be passed starts at
0
402 If VarType(Column) = V_STRING Then
403 lCol = ScriptForge.SF_Array.IndexOf(_ColumnHeaders, Column, CaseSensitive := False)
406 If Column
>=
1 Then
408 If Column
> lMaxCol +
1 Then lCol = lMaxCol Else lCol = Column -
1
410 lCol = .getCurrentColumnPosition()
414 If lCol
>=
0 Then sText = .getByIndex(lCol).Text
419 SF_Utils._ExitFunction(cstThisSub)
423 End Function
' SFDatabases.SF_Datasheet.GetText
425 REM -----------------------------------------------------------------------------
426 Public Function GetValue(Optional ByVal Column As Variant) As Variant
427 ''' Get the value in the given column of the current row.
428 ''' Args:
429 ''' Column: the name of the column as a string or its position (
>=
1). Default = the current column
430 ''' If the argument exceeds the number of columns, the last column is selected.
431 ''' Returns:
432 ''' The value in the cell as a valid Basic type
433 ''' Typical types are: STRING, INTEGER, LONG, FLOAT, DOUBLE, DATE, NULL
434 ''' Binary types are returned as a LONG giving their length, not their content
435 ''' An EMPTY return value means that the value could not be retrieved.
436 ''' Note that the position of the cursor is left unchanged.
437 ''' Examples:
438 ''' oSheet.GetValue(
"ShipCity
"))
' Extract the value on the current row from the column
"ShipCity
"
440 Dim vValue As Variant
' Return value
441 Dim lCol As Long
' Numeric index of Column in lists of columns
442 Dim lMaxCol As Long
' Index of last column
443 Const cstThisSub =
"SFDatabases.Datasheet.GetValue
"
444 Const cstSubArgs =
"[Column=
0]
"
446 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
450 If IsMissing(Column) Or IsEmpty(Column) Then Column =
0
451 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
452 If Not _IsStillAlive() Then GoTo Finally
453 If VarType(Column)
<> V_STRING Then
454 If Not ScriptForge.SF_Utils._Validate(Column,
"Column
", ScriptForge.V_NUMERIC) Then GoTo Catch
456 If Not ScriptForge.SF_Utils._Validate(Column,
"Column
", V_STRING, _ColumnHeaders) Then GoTo Catch
461 ' Position the column - The index to be passed starts at
1
462 If VarType(Column) = V_STRING Then
463 lCol = ScriptForge.SF_Array.IndexOf(_ColumnHeaders, Column, CaseSensitive := False) +
1
467 If Column
>=
1 Then
469 If Column
> lMaxCol Then lCol = lMaxCol Else lCol = Column
471 lCol = .getCurrentColumnPosition() +
1
476 ' The _TabControllerModel acts exactly as a result set, from which the generic _GetColumnValue can extract the searched value
477 If lCol
>=
1 Then vValue = _ParentDatabase._GetColumnValue(_TabControllerModel, lCol)
481 SF_Utils._ExitFunction(cstThisSub)
485 End Function
' SFDatabases.SF_Datasheet.GetValue
487 REM -----------------------------------------------------------------------------
488 Public Function GoToCell(Optional ByVal Row As Variant _
489 , Optional ByVal Column As Variant _
491 ''' Set the cursor on the given row and the given column.
492 ''' If the requested row exceeds the number of available rows, the cursor is set on the last row.
493 ''' If the requested column exceeds the number of available columns, the selected column is the last one.
494 ''' Args:
495 ''' Row: the row number (
>=
1) as a numeric value. Default= no change
496 ''' Column: the name of the column as a string or its position (
>=
1). Default = the current column
497 ''' Returns:
498 ''' True when successful
499 ''' Examples:
500 ''' oSheet.GoToCell(
1000000,
"ShipCity
"))
' Set the cursor on he last row, column
"ShipCity
"
502 Dim bGoTo As Boolean
' Return value
503 Dim lCol As Long
' Numeric index of Column in list of columns
504 Dim lMaxCol As Long
' Index of last column
505 Const cstThisSub =
"SFDatabases.Datasheet.GoToCell
"
506 Const cstSubArgs =
"[Row=
0], [Column=
0]
"
508 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
512 If IsMissing(Row) Or IsEmpty(Row) Then Row =
0
513 If IsMissing(Column) Or IsEmpty(Column) Then Column =
0
514 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
515 If Not _IsStillAlive() Then GoTo Finally
516 If Not ScriptForge.SF_Utils._Validate(Row,
"Row
", ScriptForge.V_NUMERIC) Then GoTo Catch
517 If VarType(Column)
<> V_STRING Then
518 If Not ScriptForge.SF_Utils._Validate(Column,
"Column
", ScriptForge.V_NUMERIC) Then GoTo Catch
520 If Not ScriptForge.SF_Utils._Validate(Column,
"Column
", V_STRING, _ColumnHeaders) Then GoTo Catch
525 ' Position the row
526 With _TabControllerModel
527 If Row
<=
0 Then Row = .Row Else .absolute(Row)
528 ' Does Row exceed the total number of rows ?
529 If .IsRowCountFinal And Row
> .RowCount Then .absolute(.RowCount)
532 ' Position the column
534 If VarType(Column) = V_STRING Then
535 lCol = ScriptForge.SF_Array.IndexOf(_ColumnHeaders, Column, CaseSensitive := False)
538 If Column
>=
1 Then
540 If Column
> lMaxCol +
1 Then lCol = lMaxCol Else lCol = Column -
1
543 If lCol
>=
0 Then .setCurrentColumnPosition(lCol)
550 SF_Utils._ExitFunction(cstThisSub)
554 End Function
' SFDatabases.SF_Datasheet.GoToCell
556 REM -----------------------------------------------------------------------------
557 Public Function Methods() As Variant
558 ''' Return the list of public methods of the Model service as an array
561 "Activate
" _
562 ,
"CloseDatasheet
" _
563 ,
"CreateMenu
" _
564 ,
"GetText
" _
565 ,
"GetValue
" _
566 ,
"GoToCell
" _
567 ,
"RemoveMenu
" _
570 End Function
' SFDatabases.SF_Datasheet.Methods
572 REM -----------------------------------------------------------------------------
573 Public Function Properties() As Variant
574 ''' Return the list or properties of the Model class as an array
576 Properties = Array( _
577 "ColumnHeaders
" _
578 ,
"CurrentColumn
" _
579 ,
"CurrentRow
" _
580 ,
"DatabaseFileName
" _
581 ,
"Filter
" _
582 ,
"LastRow
" _
583 ,
"OrderBy
" _
584 ,
"ParentDatabase
" _
585 ,
"Source
" _
586 ,
"SourceType
" _
587 ,
"XComponent
" _
588 ,
"XControlModel
" _
589 ,
"XTabControllerModel
" _
592 End Function
' SFDatabases.SF_Datasheet.Properties
594 REM -----------------------------------------------------------------------------
595 Public Function RemoveMenu(Optional ByVal MenuHeader As Variant) As Boolean
596 ''' Remove a menu entry in the document
's menubar
597 ''' The removal is not intended to be saved neither in the LibreOffice global environment, nor in the document
598 ''' Args:
599 ''' MenuHeader: the name/header of the menu, without tilde
"~
", as a case-sensitive string
600 ''' Returns:
601 ''' True when successful
602 ''' Examples:
603 ''' oDoc.RemoveMenu(
"File
")
604 ''' ' ...
606 Dim bRemove As Boolean
' Return value
607 Dim oLayout As Object
' com.sun.star.comp.framework.LayoutManager
608 Dim oMenuBar As Object
' com.sun.star.awt.XMenuBar or stardiv.Toolkit.VCLXMenuBar
609 Dim sName As String
' Menu name
610 Dim iMenuId As Integer
' Menu identifier
611 Dim iMenuPosition As Integer
' Menu position
>=
0
613 Const cstTilde =
"~
"
615 Const cstThisSub =
"SFDatabases.Datasheet.RemoveMenu
"
616 Const cstSubArgs =
"MenuHeader
"
618 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
622 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
623 If Not _IsStillAlive() Then GoTo Finally
624 If Not ScriptForge.SF_Utils._Validate(MenuHeader,
"MenuHeader
", V_STRING) Then GoTo Finally
628 Set oLayout = _Component.Frame.LayoutManager
629 Set oMenuBar = oLayout.getElement(
"private:resource/menubar/menubar
").XMenuBar
631 ' Search the menu identifier to remove by its name, Mark its position
634 For i =
0 To .ItemCount -
1
635 iMenuId = .getItemId(i)
636 sName = Replace(.getItemText(iMenuId), cstTilde,
"")
637 If MenuHeader= sName Then
642 ' Remove the found menu item
643 If iMenuPosition
>=
0 Then
644 .removeItem(iMenuPosition,
1)
651 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
655 End Function
' SFDatabases.SF_Datasheet.RemoveMenu
657 REM -----------------------------------------------------------------------------
658 Public Function SetProperty(Optional ByVal PropertyName As Variant _
659 , Optional ByRef Value As Variant _
661 ''' Set a new value to the given property
662 ''' Args:
663 ''' PropertyName: the name of the property as a string
664 ''' Value: its new value
665 ''' Exceptions
666 ''' ARGUMENTERROR The property does not exist
668 Const cstThisSub =
"SFDatabases.Datasheet.SetProperty
"
669 Const cstSubArgs =
"PropertyName, Value
"
671 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
675 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
676 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
680 SetProperty = _PropertySet(PropertyName, Value)
683 SF_Utils._ExitFunction(cstThisSub)
687 End Function
' SFDatabases.SF_Datasheet.SetProperty
689 REM -----------------------------------------------------------------------------
690 Public Function Toolbars(Optional ByVal ToolbarName As Variant) As Variant
691 ''' Returns either a list of the available toolbar names in the actual document
692 ''' or a Toolbar object instance.
693 ''' [Function identical with SFDocuments.SF_Document.Toolbars()]
694 ''' Args:
695 ''' ToolbarName: the usual name of one of the available toolbars
696 ''' Returns:
697 ''' A zero-based array of toolbar names when the argument is absent,
698 ''' or a new Toolbar object instance from the SF_Widgets library.
700 Const cstThisSub =
"SFDatabases.Datasheet.Toolbars
"
701 Const cstSubArgs =
"[ToolbarName=
""""]
"
703 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
706 If IsMissing(ToolbarName) Or IsEmpty(ToolbarName) Then ToolbarName =
""
707 If IsNull(_Toolbars) Then _Toolbars = ScriptForge.SF_UI._ListToolbars(_Component)
708 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
709 If Not _IsStillAlive() Then GoTo Finally
710 If VarType(ToolbarName) = V_STRING Then
711 If Len(ToolbarName)
> 0 Then
712 If Not ScriptForge.SF_Utils._Validate(ToolbarName,
"ToolbarName
", V_STRING, _Toolbars.Keys()) Then GoTo Finally
715 If Not ScriptForge.SF_Utils._Validate(ToolbarName,
"ToolbarName
", V_STRING) Then GoTo Finally
' Manage here the VarType error
720 If Len(ToolbarName) =
0 Then
721 Toolbars = _Toolbars.Keys()
723 Toolbars = CreateScriptService(
"SFWidgets.Toolbar
", _Toolbars.Item(ToolbarName))
727 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
731 End Function
' SF_Databases.SF_Datasheet.Toolbars
733 REM =========================================================== PRIVATE FUNCTIONS
735 REM -----------------------------------------------------------------------------
736 Public Sub _Initialize()
737 ''' Called immediately after instance creation to complete the initial values
738 ''' An eventual error must be trapped in the calling routine to cancel the instance creation
740 Dim iType As Integer
' One of the com.sun.star.sdb.CommandType constants
741 Dim oColumn As Object
' A single column
742 Dim oColumnDescriptor As Object
' A single column descriptor
743 Dim FSO As Object : Set FSO = ScriptForge.SF_FileSystem
747 If IsNull([_Parent]) Then _ParentType =
"" Else _ParentType = [_Parent].ObjectType
750 ' The existence of _Component.Selection must be checked upfront
751 _Command = ScriptForge.SF_Utils._GetPropertyValue(.Selection,
"Command
")
753 iType = ScriptForge.SF_Utils._GetPropertyValue(.Selection,
"CommandType
")
755 Case com.sun.star.sdb.CommandType.TABLE : _SheetType =
"TABLE
"
756 Case com.sun.star.sdb.CommandType.QUERY : _SheetType =
"QUERY
"
757 Case com.sun.star.sdb.CommandType.COMMAND : _SheetType =
"SQL
"
760 _BaseFileName = ScriptForge.SF_Utils._GetPropertyValue(.Selection,
"DataSourceName
")
761 _DirectSql = Not ScriptForge.SF_Utils._GetPropertyValue(.Selection,
"EscapeProcessing
")
763 ' Useful UNO objects
765 Set _ControlView = .CurrentControl
766 Set _TabControllerModel = .com_sun_star_awt_XTabController_getModel()
767 Set _ControlModel = _ControlView.getModel()
770 With _TabControllerModel
771 ' Retrieve the parent database instance
772 Select Case _ParentType
773 Case
"BASE
"
774 Set _ParentDatabase = [_Parent].GetDatabase(.User, .Password)
775 Set _ParentBase = [_Parent]
776 Case
"DATABASE
"
777 Set _ParentDatabase = [_Parent]
778 Set _ParentBase = Nothing
779 Case
"" ' Derive the DATABASE instance from what can be found in the Component
780 Set _ParentDatabase = ScriptForge.SF_Services.CreateScriptService(
"SFDatabases.Database
" _
781 , FSO._ConvertFromUrl(_BaseFileName), , , .User, .Password)
782 _ParentType =
"DATABASE
"
783 Set _ParentBase = Nothing
785 ' Load column headers
786 _ColumnHeaders = .getColumns().getElementNames()
791 End Sub
' SFDatabases.SF_Datasheet._Initialize
793 REM -----------------------------------------------------------------------------
794 Private Function _IsStillAlive(Optional ByVal pbError As Boolean) As Boolean
795 ''' Returns True if the datasheet has not been closed manually or incidentally since the last use
796 ''' If dead the actual instance is disposed. The execution is cancelled when pbError = True (default)
797 ''' Args:
798 ''' pbError: if True (default), raise a fatal error
800 Dim bAlive As Boolean
' Return value
801 Dim sName As String
' Used in error message
803 On Local Error GoTo Catch
' Anticipate DisposedException errors or alike
804 If IsMissing(pbError) Then pbError = True
807 ' Check existence of datasheet
808 bAlive = Not IsNull(_Component.ComponentWindow)
811 If pbError And Not bAlive Then
814 If pbError Then ScriptForge.SF_Exception.RaiseFatal(DOCUMENTDEADERROR, sName)
816 _IsStillAlive = bAlive
822 End Function
' SFDatabases.SF_Datasheet._IsStillAlive
824 REM -----------------------------------------------------------------------------
825 Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
826 ''' Return the value of the named property
827 ''' Args:
828 ''' psProperty: the name of the property
830 Dim lRow As Long
' Actual row number
831 Dim cstThisSub As String
832 Const cstSubArgs =
""
834 cstThisSub =
"SFDatabases.Datasheet.get
" & psProperty
835 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
837 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
838 If Not _IsStillAlive(False) Then GoTo Finally
840 Select Case psProperty
841 Case
"ColumnHeaders
"
842 _PropertyGet = _ColumnHeaders
843 Case
"CurrentColumn
"
844 _PropertyGet = _ColumnHeaders(_ControlView.getCurrentColumnPosition())
845 Case
"CurrentRow
"
846 _PropertyGet = _TabControllerModel.Row
847 Case
"DatabaseFileName
"
848 _PropertyGet = ScriptForge.SF_FileSystem._ConvertFromUrl(_BaseFileName)
849 Case
"Filter
"
850 _PropertyGet = _TabControllerModel.Filter
851 Case
"LastRow
"
852 With _TabControllerModel
853 If .IsRowCountFinal Then
854 _PropertyGet = .RowCount
859 _PropertyGet = .RowCount
866 Case
"OrderBy
"
867 _PropertyGet = _TabControllerModel.Order
868 Case
"ParentDatabase
"
869 Set _PropertyGet = _ParentDatabase
870 Case
"Source
"
871 _PropertyGet = _Command
872 Case
"SourceType
"
873 _PropertyGet = _SheetType
874 Case
"XComponent
"
875 Set _PropertyGet = _Component
876 Case
"XControlModel
"
877 Set _PropertyGet = _ControlModel
878 Case
"XTabControllerModel
"
879 Set _PropertyGet = _TabControllerModel
885 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
889 End Function
' SFDatabases.SF_Datasheet._PropertyGet
891 REM -----------------------------------------------------------------------------
892 Private Function _PropertySet(Optional ByVal psProperty As String _
893 , Optional ByVal pvValue As Variant _
895 ''' Set the new value of the named property
896 ''' Args:
897 ''' psProperty: the name of the property
898 ''' pvValue: the new value of the given property
899 ''' Returns:
900 ''' True if successful
902 Dim bSet As Boolean
' Return value
903 Dim cstThisSub As String
904 Const cstSubArgs =
"Value
"
906 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
909 cstThisSub =
"SFDatabases.Datasheet.set
" & psProperty
910 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
911 If Not _IsStillAlive() Then GoTo Finally
914 Select Case UCase(psProperty)
915 Case UCase(
"Filter
")
916 If Not ScriptForge.SF_Utils._Validate(pvValue,
"Filter
", V_STRING) Then GoTo Finally
917 With _TabControllerModel
918 If Len(pvValue)
> 0 Then .Filter = _ParentDatabase._ReplaceSquareBrackets(pvValue) Else .Filter =
""
919 .ApplyFilter = ( Len(pvValue)
> 0 )
922 Case UCase(
"OrderBy
")
923 If Not ScriptForge.SF_Utils._Validate(pvValue,
"OrderBy
", V_STRING) Then GoTo Finally
924 With _TabControllerModel
925 If Len(pvValue)
> 0 Then .Order = _ParentDatabase._ReplaceSquareBrackets(pvValue) Else .Order =
""
934 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
938 End Function
' SFDatabases.SF_Datasheet._PropertySet
940 REM -----------------------------------------------------------------------------
941 Private Function _Repr() As String
942 ''' Convert the Datasheet instance to a readable string, typically for debugging purposes (DebugPrint ...)
943 ''' Args:
944 ''' Return:
945 ''' "[DATASHEET]: tablename,base file url
"
947 _Repr =
"[DATASHEET]:
" & _Command
& ",
" & _BaseFileName
949 End Function
' SFDatabases.SF_Datasheet._Repr
951 REM ============================================ END OF SFDATABASES.SF_DATASHEET