1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 '* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 '* Copyright 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: t_treelist_tools.inc,v $
13 '* last change: $Author: jsk $ $Date: 2008-06-20 07:59:34 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : joerg.skottke@sun.com
36 '* short description : Helpers for accessing treelists
38 '\******************************************************************************
40 private const DEBUG_ENABLE = false
42 function hGetNodeCount( oControl as object ) as integer
44 '///<h3>Retrieve the number of visible (open) nodes from a treelist</h3>
47 '///+<li>Treelist control object (Object)</li>
49 '///+<li>Object must exist in current context</li>
54 '///+<li>Number of items in treelist</li>
56 '///+<li>0 on any error</li>
57 '///+<li>> 0 Number of items</li>
60 '///<u>Description</u>:
64 const CFN = "hGetNodeCount::"
67 '///+<li>Verify that the control exists</li>
68 if ( oControl.exists( 5 ) ) then
70 '///+<li>Verify that the control is enabled</li>
71 if ( oControl.isEnabled() ) then
73 if ( DEBUG_ENABLE ) then
74 printlog( CFN & "Regular access, control available and enabled" )
77 '///+<li>get the number of items from the control</li>
78 iCount = oControl.getItemCount()
80 printlog( CFN & "Failure: Control claims to be disabled." )
85 printlog( CFN & "Forcing access to non existing control" )
86 iCount = oControl.getItemCount()
88 printlog( CFN & "Failure: Control not available." )
92 if ( DEBUG_ENABLE ) then
93 printlog( CFN & "Exit with nodecount = " & iCount )
95 hGetNodeCount = iCount
100 '*******************************************************************************
102 function hSelectTopNode( oControl as object ) as boolean
104 '///<h3>Move selection to the first node in a treelist</h3>
106 '///+<li>Type the "Home"-key in a treelist, to select index 1</li>
107 '///+<li>Verify that the top node has been selected</li>
110 const CFN = "hSelectTopNode::"
115 hSelectTopNode() = true
117 hSelectTopNode() = false
120 if ( DEBUG_ENABLE ) then
121 printlog( CFN & "Selected Node: " & oControl.getText() )
126 '*******************************************************************************
128 function hSelectNextNode( oControl as object ) as integer
130 '///<h3>Move one node down in a treelist</h3>
132 '///+<li>Returns new position or 0 on error</li>
135 const CFN = "hSelectNextNode::"
137 dim iPosBefore as integer
138 dim iPosAfter as integer
140 iPosBefore = oControl.getSelIndex()
142 oControl.typeKeys( "<DOWN>" )
144 iPosAfter = oControl.getSelIndex()
146 if ( iPosAfter = iPosBefore ) then
147 hSelectNextNode() = 0
150 if ( iPosAfter = ( iPosBefore + 1 ) ) then
151 hSelectnextNode() = iPosAfter
154 printlog( CFN & "Selected node: " & oControl.getText() )
158 '*******************************************************************************
160 function hGetNodeName( oControl as object , iNode as integer ) as string
162 '///<h3>Retrieve the name of a node in a treelist specified by index</h3>
164 '///+<li>Returns the name of the node or empty string on error</li>
166 const CFN = "hGetNodeName::"
167 dim iItemCount as integer
169 iItemCount = hGetNodeCount( oControl )
170 if ( iNode > iItemCount ) then
171 warnlog( CFN & "Selected node out of range, aborting" )
174 oControl.select( iNode )
175 hGetNodeName() = oControl.getSelText()
180 '*******************************************************************************
182 function hExpandNode( oControl as object, iNode as integer ) as integer
184 '///<h3>Expand a node in a treelist specified by index</h3>
186 '///+<li>Returns new nodecount or 0 on error</li>
189 const CFN = "hExpandNode::"
191 dim iOldNodeCount as integer
193 if ( DEBUG_ENABLE ) then
194 printlog( CFN & "Enter with option (Control): " & oControl.name() )
195 printlog( CFN & "Enter with option (iNode): " & iNode )
198 iOldNodeCount = hGetNodeCount( oControl )
199 if ( iNode <= iOldNodeCount ) then
200 if ( iNode > 0 ) then
201 oControl.select( iNode )
204 oControl.typekeys( "<RIGHT>" )
205 hExpandNode() = hGetNodeCount( oControl )
207 warnlog( "#i84194# Treelist access failed (Keyboard navigation)" )
216 '*******************************************************************************
218 function hExpandAllNodes( oControl as object ) as integer
220 '///<h3>Expand all nodes of treelist</h3>
222 '///+<li>Run through all items in the treelist, expand every node</li>
223 '///+<li>Returns the number of all nodes in the treelist</li>
227 dim iNodeCurCount as integer
228 dim iNodeRefCount as integer
229 dim iIteration as integer
231 const CFN = "hExpandAllNodes::"
233 hSelectTopNode( oControl )
234 iNodeCurCount = hGetNodeCount( oControl )
238 if ( DEBUG_ENABLE ) then
239 printlog( CFN & "Initial iNodeCurCount: " & iNodeCurCount )
240 printlog( CFN & "Initial iNodeRefCount: " & iNodeRefCount )
243 do while ( iNodeRefCount < iNodeCurCount )
245 iIteration = iIteration + 1
246 iNodeRefCount = iNodeCurCount
248 for iNode = iNodeCurCount to 1 step -1
249 hExpandNode( oControl , iNode )
252 iNodeCurCount = hGetNodeCount( oControl )
254 if ( DEBUG_ENABLE ) then
256 printlog( CFN & "Exit iteration....: " & iIteration )
257 printlog( CFN & "Exit iNodeCurCount: " & iNodeCurCount )
258 printlog( CFN & "Exit iNodeRefCount: " & iNodeRefCount )
263 if ( DEBUG_ENABLE ) then
264 printlog( CFN & "Exit with " & iNodeCurCount & _
265 " items after " & iIteration & " iterations" )
267 hExpandAllNodes() = iNodeCurCount
271 '*******************************************************************************
273 function hGetVisibleNodeNames( oControl as object , lsList() as string ) as integer
275 '///<h3>Retrieve the names of all nodes in a treelist</h3>
277 '///+<li>Expand all nodes of a treelist</li>
278 '///+<li>Store all node names into an array</li>
279 '///+<li>Return the number of nodes read (listcount), 0 on error</li>
282 ' Get the list of all visible (expanded) nodes and store it
283 ' in lsList(). if _id > ubound lsList() an error is given and lsList remains
284 ' empty, thus returning "0"
286 const CFN = "hGetVisibleNodeNames::"
288 dim iNodeCount as integer
289 dim iThisNode as integer
291 printlog( CFN & "Enter" )
293 iNodeCount = hGetNodeCount( oControl )
295 ' Test whether the array provided is large enough to hold all items
296 ' from the treelist/list. If the array is ok fill it.
297 if ( iNodeCount > ubound( lsList() ) ) then
298 warnlog( "Array to small to hold: " & iNodeCount & " items" )
300 hSelectTopNode( oControl )
301 for iThisNode = 1 to iNodeCount
302 listappend( lsList() , hGetNodeName( oControl , iThisNode )
306 hGetVisibleNodeNames() = listcount( lsList() )
307 printlog( CFN & "Exit" )
311 '*******************************************************************************
313 function hSelectNode( oControl as object , _id as integer ) as string
315 '///<h3>Select a node in a treelist by index</h3>
317 '///+<li>Return the name of the selected node</li>
320 oControl.select( _id ) : hSelectNode() = hGetNodeName( oControl , _id )
324 '*******************************************************************************
326 function hSelectNodeByName( oControl as object , _name as string ) as integer
328 '///<h3>Select a node by name in treelist (first occurrence)</h3>
330 '///+<li>Try to find a node by name - directly</li>
331 '///+<li>If the node is not visible, expand all and search the tree</li>
332 '///+<li>Returns index of requested node or 0 on failure</li>
335 const CFN = "hSelectNodeByName::"
337 dim iThisNode as integer
338 dim iCurrentNode as integer
339 dim iNodeCount as integer
341 dim cNodeName as string
343 printlog( CFN & "Enter with option (NodeName): " & _name )
347 ' First we try to jump to the node directly, if this fails we use the
348 ' slower but safer method (expand all nodes and step through)
350 oControl.select( _name )
351 iThisNode = oControl.getSelIndex()
352 hSelectNodeByName() = iThisNode
354 printlog( CFN & "Node not visible: Using search method" )
355 iNodeCount = hExpandAllNodes( oControl )
357 for iCurrentNode = 1 to iNodeCount
358 oControl.select( iCurrentNode )
359 cNodeName = oControl.getSelText()
361 if ( cNodeName = _name ) then
362 iThisNode = iCurrentNode
368 if ( iThisNode = 0 ) then
369 printlog( CFN & "Exit: Node not found." )
371 printlog( CFN & "Exit: Node selected at pos: " & iThisNode )
374 hSelectNodeByName() = iThisNode
378 '*******************************************************************************
380 function hSelectTheLastNode( oControl as object ) as integer
382 '///<h3>Select the (absolute) last node in a treelist</h3>
384 '///+<li>Expand all nodes</li>
385 '///+<li>Go to the last node, select it</li>
386 '///+<li>Return the number of the last node in the treelist</li>
389 const CFN = "hSelectTheLastNode::"
390 dim iCurrentNodeCount as integer
392 printlog( CFN & "Enter with option (control): " & oControl.name() )
394 iCurrentNodeCount = -1
396 do while( iCurrentNodeCount < hGetNodeCount( oControl ) )
398 iCurrentNodeCount = hGetNodeCount( oControl )
399 hExpandNode( oControl, iCurrentNodeCount )
401 if ( DEBUG_ENABLE ) then
402 printlog( CFN & "Nodename.....: " & oControl.getText() )
403 printlog( CFN & "Node position: " & iCurrentNodeCount )
408 printlog( CFN & "Exit with result: " & iCurrentNodeCount )
409 hSelectTheLastNode() = iCurrentNodeCount
413 '*******************************************************************************
415 function hVerifyNodeName( oControl as object , cName as string ) as boolean
417 '///<h3>Compare the name of the current node from a treelist to a reference string</h3>
419 '///+<li>Returns true on success, false on failure</li>
422 hVerifyNodeName() = false
423 if ( oControl.getSelText() = cName ) then hVerifyNodeName() = true
427 '*******************************************************************************
429 function hWaitForTreelist( oTreeList as object, cContext as string, iItemCount as integer ) as boolean
431 '///<h3>Wait for a treelist to get populated (java related delay)</h3>
432 '///<b>IMPORTANT: Do not use unless absolutely necessary</b>
433 '///+<p>Retrieve the number of items from the treelist until a specified
434 '///+ number has been reached.</p>
436 const CFN = "hWaitForTreelist::"
438 dim iObjects as integer
446 qaerrorlog( CFN & "Stupid function, do not use" )
449 do while ( iObjects < iItemCount )
452 iObjects = hGetNodeCount( oTreeList )
454 if ( iObjects >= iItemCount ) then
460 if ( iTry = 10 ) then
461 qaerrorlog( CFN & "Requested number of items never reached" )
468 hWaitForTreelist() = brc
472 '******************************************************************************
474 function hGetListItems( oControl as object, aList() as string ) as integer
476 '///<h3>Retrieve the names of all nodes from a plain (linear) list</h3>
478 '///+<li>Cycle through a list, append all entries to an array</li>
479 '///+<li>Returns number of items or 0 on error</li>
482 const CFN = "hGetListItems::"
484 printlog( CFN & "Enter with option (control): " & oControl.name() )
486 dim iItemCount as integer
487 dim iCurrentItem as integer
488 dim cCurrentItem as string
490 iItemCount = oControl.getItemCount()
491 if ( iItemCount > ubound( aList() ) ) then
492 printlog( CFN & "Array too small, needed: " & iItemCount )
497 for iCurrentItem = 1 to iItemCount
499 oControl.select( iCurrentItem )
500 cCurrentItem = oControl.getSelText()
501 hListAppend( cCurrentItem, aList() )
505 printlog( CFN & "Exit with number of items: " & iItemCount )
506 hGetListItems() = iItemCount