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="container_XContainer" script:language="StarBasic">
4
5
6'*************************************************************************
7'
8' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9'
10' Copyright 2000, 2010 Oracle and/or its affiliates.
11'
12' OpenOffice.org - a multi-platform office productivity suite
13'
14' This file is part of OpenOffice.org.
15'
16' OpenOffice.org is free software: you can redistribute it and/or modify
17' it under the terms of the GNU Lesser General Public License version 3
18' only, as published by the Free Software Foundation.
19'
20' OpenOffice.org is distributed in the hope that it will be useful,
21' but WITHOUT ANY WARRANTY; without even the implied warranty of
22' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23' GNU Lesser General Public License version 3 for more details
24' (a copy is included in the LICENSE file that accompanied this code).
25'
26' You should have received a copy of the GNU Lesser General Public License
27' version 3 along with OpenOffice.org.  If not, see
28' <http://www.openoffice.org/license.html>
29' for a copy of the LGPLv3 License.
30'
31'*************************************************************************
32*****
33'*************************************************************************
34
35
36
37'*************************************************************************
38' This Interface/Service test depends on the following GLOBAL variables,
39' which must be specified in the object creation:
40
41'      Global oElementToInsert As Object
42'      Global oContainer As Object in case if the component tested does
43'                                  not support XNameContainer
44
45'*************************************************************************
46
47Dim ElIns1 As Integer
48Dim ElIns2 As Integer
49Dim ElRem1 As Integer
50Dim ElRem2 As Integer
51Dim ElRep1 As Integer
52Dim ElRep2 As Integer
53
54
55Sub RunTest()
56
57'*************************************************************************
58' INTERFACE:
59' com.sun.star.container.XContainer
60'*************************************************************************
61On Error Goto ErrHndl
62    Dim bOK As Boolean
63    Dim oListener1 as Object
64    Dim oListener2 as Object
65
66    oListener1 = createUNOListener("CB1_", "com.sun.star.container.XContainerListener")
67    oListener2 = createUNOListener("CB2_", "com.sun.star.container.XContainerListener")
68    bOK = NOT isNULL(oListener1) AND NOT isNULL(oListener2)
69    Out.Log("Listeners creation : " + bOK)
70
71    Test.StartMethod("addContainerListener()")
72    bOK = true
73    oObj.addContainerListener(oListener1)
74    Out.Log("Listener1 was added")
75    oObj.addContainerListener(oListener2)
76    Out.Log("Listener2 was added")
77
78    insertElement()
79
80    Dim bInsOK As Boolean
81    bInsOK = ElIns1 >= 1 AND ElIns2 >= 1
82    Out.Log("... " + bInsOK)
83    bOK = bOK AND bInsOK
84
85    removeElement()
86
87    bRemOK = ElRem1 >= 1 AND ElRem2 >= 1
88    Out.Log("... " + bRemOK)
89    bOK = bOK AND bRemOK
90
91    Dim bNothingToReplace as Boolean
92    bNothingToReplace = replaceElement()
93
94    bRepOK = (ElRep1 >= 1 AND ElRep2 >= 1) OR bNothingToReplace
95    Out.Log("... " + bRepOK)
96    bOK = bOK AND bRepOK
97
98    Test.MethodTested("addContainerListener()", bOK)
99
100
101    Test.StartMethod("removeContainerListener()")
102    bOK = true
103    oObj.removeContainerListener(oListener1)
104    Out.Log("Listener1 was removed")
105
106    insertElement()
107
108    bInsOK = ElIns1 = 0 AND ElIns2 >= 1
109    Out.Log("... " + bInsOK)
110    bOK = bOK AND bInsOK
111
112    removeElement()
113
114    bRemOK = ElRem1 = 0 AND ElRem2 >= 1
115    Out.Log("... " + bRemOK)
116    bOK = bOK AND bRemOK
117
118    bNothingToReplace = replaceElement()
119
120    bRepOK = (ElRep1 = 0 AND ElRep2 >= 1) or bNothingToReplace
121    Out.Log("... " + bRepOK)
122    bOK = bOK AND bRepOK
123    Test.MethodTested("removeContainerListener()", bOK)
124
125    oObj.removeContainerListener(oListener2)
126    Out.Log("Listener2 was removed")
127
128Exit Sub
129ErrHndl:
130    Test.Exception()
131    bOK = false
132    resume next
133End Sub
134
135Sub insertElement()
136    Out.Log("Inserting element ... ")
137    ResetCounters()
138    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
139        oObj.InsertByName(cIfcShortName, oElementToInsert)
140    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
141        oContainer.InsertByName(cIfcShortName, oElementToInsert)
142    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
143        oContainer.addControl("NewControl", oElementToInsert)
144    else
145        Out.LOG("There is nothig to trigger the Listener!")
146    end if
147end Sub
148
149
150Sub removeElement()
151    Out.Log("Removing element ... ")
152    ResetCounters()
153    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
154        oObj.RemoveByName(cIfcShortName)
155    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
156        oContainer.RemoveByName(cIfcShortName)
157    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
158        oContainer.removeControl(oElementToInsert)
159    end if
160end Sub
161
162Function replaceElement() as Boolean
163    Out.Log("Replacing element ... ")
164    ResetCounters()
165    Dim bNothingToReplace as Boolean
166    bNothingToReplace = FALSE
167    Dim old As Variant
168    if hasUnoInterfaces(oObj, "com.sun.star.container.XIndexReplace") then
169        old = oObj.getByIndex(0)
170        oObj.ReplaceByIndex(0, oElementToInsert)
171        oObj.ReplaceByIndex(0, old)
172    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XIndexReplace") then
173        old = oContainer.getByIndex(0)
174        oContainer.ReplaceByIndex(0, oElementToInsert)
175        oContainer.ReplaceByIndex(0, old)
176    elseif (hasUnoInterfaces(oContainer, "com.sun.star.container.XNameAccess") and _
177            hasUnoInterfaces(oContainer, "com.sun.star.container.XNameReplace")) then
178        Dim cNames() as String
179        cNames = oObj.getElementNames()
180        old = oContainer.getByName(cNames(0))
181        oContainer.ReplaceByName(cNames(0), oElementToInsert)
182        oContainer.ReplaceByName(cNames(0), old)
183    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
184        bNothingToReplace = TRUE
185    else
186        Out.LOG("There is nothig to trigger the Listener!")
187    end if
188    replaceElement() = bNothingToReplace
189end Function
190
191Sub CB1_elementInserted(ev As Object)
192    Out.Log("CB1 called: element was inserted")
193    ElIns1 = ElIns1 + 1
194End Sub
195
196Sub CB1_elementRemoved(ev As Object)
197    Out.Log("CB1 called: element was removed")
198    ElRem1 = ElRem1 + 1
199End Sub
200
201Sub CB1_elementReplaced(ev As Object)
202    Out.Log("CB1 called: element was replaced")
203    ElRep1 = ElRep1 + 1
204End Sub
205
206Sub CB2_elementInserted(ev As Object)
207    Out.Log("CB2 called: element was inserted")
208    ElIns2 = ElIns2 + 1
209End Sub
210
211Sub CB2_elementRemoved(ev As Object)
212    Out.Log("CB2 called: element was removed")
213    ElRem2 = ElRem2 + 1
214End Sub
215
216Sub CB2_elementReplaced(ev As Object)
217    Out.Log("CB2 called: element was replaced")
218    ElRep2 = ElRep2 + 1
219End Sub
220
221Sub ResetCounters()
222    ElIns1 = 0
223    ElIns2 = 0
224    ElRem1 = 0
225    ElRem2 = 0
226    ElRep1 = 0
227    ElRep2 = 0
228End Sub
229</script:module>
230