1*b1cdbd2cSJim Jagielski<?xml version="1.0" encoding="UTF-8"?>
2*b1cdbd2cSJim Jagielski<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3*b1cdbd2cSJim Jagielski<script:module xmlns:script="http://openoffice.org/2000/script" script:name="container_XContainer" script:language="StarBasic">
4*b1cdbd2cSJim Jagielski
5*b1cdbd2cSJim Jagielski
6*b1cdbd2cSJim Jagielski'*************************************************************************
7*b1cdbd2cSJim Jagielski'
8*b1cdbd2cSJim Jagielski'  Licensed to the Apache Software Foundation (ASF) under one
9*b1cdbd2cSJim Jagielski'  or more contributor license agreements.  See the NOTICE file
10*b1cdbd2cSJim Jagielski'  distributed with this work for additional information
11*b1cdbd2cSJim Jagielski'  regarding copyright ownership.  The ASF licenses this file
12*b1cdbd2cSJim Jagielski'  to you under the Apache License, Version 2.0 (the
13*b1cdbd2cSJim Jagielski'  "License"); you may not use this file except in compliance
14*b1cdbd2cSJim Jagielski'  with the License.  You may obtain a copy of the License at
15*b1cdbd2cSJim Jagielski'
16*b1cdbd2cSJim Jagielski'    http://www.apache.org/licenses/LICENSE-2.0
17*b1cdbd2cSJim Jagielski'
18*b1cdbd2cSJim Jagielski'  Unless required by applicable law or agreed to in writing,
19*b1cdbd2cSJim Jagielski'  software distributed under the License is distributed on an
20*b1cdbd2cSJim Jagielski'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21*b1cdbd2cSJim Jagielski'  KIND, either express or implied.  See the License for the
22*b1cdbd2cSJim Jagielski'  specific language governing permissions and limitations
23*b1cdbd2cSJim Jagielski'  under the License.
24*b1cdbd2cSJim Jagielski'
25*b1cdbd2cSJim Jagielski'*************************************************************************
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielski
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielski'*************************************************************************
32*b1cdbd2cSJim Jagielski' This Interface/Service test depends on the following GLOBAL variables,
33*b1cdbd2cSJim Jagielski' which must be specified in the object creation:
34*b1cdbd2cSJim Jagielski
35*b1cdbd2cSJim Jagielski'      Global oElementToInsert As Object
36*b1cdbd2cSJim Jagielski'      Global oContainer As Object in case if the component tested does
37*b1cdbd2cSJim Jagielski'                                  not support XNameContainer
38*b1cdbd2cSJim Jagielski
39*b1cdbd2cSJim Jagielski'*************************************************************************
40*b1cdbd2cSJim Jagielski
41*b1cdbd2cSJim JagielskiDim ElIns1 As Integer
42*b1cdbd2cSJim JagielskiDim ElIns2 As Integer
43*b1cdbd2cSJim JagielskiDim ElRem1 As Integer
44*b1cdbd2cSJim JagielskiDim ElRem2 As Integer
45*b1cdbd2cSJim JagielskiDim ElRep1 As Integer
46*b1cdbd2cSJim JagielskiDim ElRep2 As Integer
47*b1cdbd2cSJim Jagielski
48*b1cdbd2cSJim Jagielski
49*b1cdbd2cSJim JagielskiSub RunTest()
50*b1cdbd2cSJim Jagielski
51*b1cdbd2cSJim Jagielski'*************************************************************************
52*b1cdbd2cSJim Jagielski' INTERFACE:
53*b1cdbd2cSJim Jagielski' com.sun.star.container.XContainer
54*b1cdbd2cSJim Jagielski'*************************************************************************
55*b1cdbd2cSJim JagielskiOn Error Goto ErrHndl
56*b1cdbd2cSJim Jagielski    Dim bOK As Boolean
57*b1cdbd2cSJim Jagielski    Dim oListener1 as Object
58*b1cdbd2cSJim Jagielski    Dim oListener2 as Object
59*b1cdbd2cSJim Jagielski
60*b1cdbd2cSJim Jagielski    oListener1 = createUNOListener("CB1_", "com.sun.star.container.XContainerListener")
61*b1cdbd2cSJim Jagielski    oListener2 = createUNOListener("CB2_", "com.sun.star.container.XContainerListener")
62*b1cdbd2cSJim Jagielski    bOK = NOT isNULL(oListener1) AND NOT isNULL(oListener2)
63*b1cdbd2cSJim Jagielski    Out.Log("Listeners creation : " + bOK)
64*b1cdbd2cSJim Jagielski
65*b1cdbd2cSJim Jagielski    Test.StartMethod("addContainerListener()")
66*b1cdbd2cSJim Jagielski    bOK = true
67*b1cdbd2cSJim Jagielski    oObj.addContainerListener(oListener1)
68*b1cdbd2cSJim Jagielski    Out.Log("Listener1 was added")
69*b1cdbd2cSJim Jagielski    oObj.addContainerListener(oListener2)
70*b1cdbd2cSJim Jagielski    Out.Log("Listener2 was added")
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski    insertElement()
73*b1cdbd2cSJim Jagielski
74*b1cdbd2cSJim Jagielski    Dim bInsOK As Boolean
75*b1cdbd2cSJim Jagielski    bInsOK = ElIns1 >= 1 AND ElIns2 >= 1
76*b1cdbd2cSJim Jagielski    Out.Log("... " + bInsOK)
77*b1cdbd2cSJim Jagielski    bOK = bOK AND bInsOK
78*b1cdbd2cSJim Jagielski
79*b1cdbd2cSJim Jagielski    removeElement()
80*b1cdbd2cSJim Jagielski
81*b1cdbd2cSJim Jagielski    bRemOK = ElRem1 >= 1 AND ElRem2 >= 1
82*b1cdbd2cSJim Jagielski    Out.Log("... " + bRemOK)
83*b1cdbd2cSJim Jagielski    bOK = bOK AND bRemOK
84*b1cdbd2cSJim Jagielski
85*b1cdbd2cSJim Jagielski    Dim bNothingToReplace as Boolean
86*b1cdbd2cSJim Jagielski    bNothingToReplace = replaceElement()
87*b1cdbd2cSJim Jagielski
88*b1cdbd2cSJim Jagielski    bRepOK = (ElRep1 >= 1 AND ElRep2 >= 1) OR bNothingToReplace
89*b1cdbd2cSJim Jagielski    Out.Log("... " + bRepOK)
90*b1cdbd2cSJim Jagielski    bOK = bOK AND bRepOK
91*b1cdbd2cSJim Jagielski
92*b1cdbd2cSJim Jagielski    Test.MethodTested("addContainerListener()", bOK)
93*b1cdbd2cSJim Jagielski
94*b1cdbd2cSJim Jagielski
95*b1cdbd2cSJim Jagielski    Test.StartMethod("removeContainerListener()")
96*b1cdbd2cSJim Jagielski    bOK = true
97*b1cdbd2cSJim Jagielski    oObj.removeContainerListener(oListener1)
98*b1cdbd2cSJim Jagielski    Out.Log("Listener1 was removed")
99*b1cdbd2cSJim Jagielski
100*b1cdbd2cSJim Jagielski    insertElement()
101*b1cdbd2cSJim Jagielski
102*b1cdbd2cSJim Jagielski    bInsOK = ElIns1 = 0 AND ElIns2 >= 1
103*b1cdbd2cSJim Jagielski    Out.Log("... " + bInsOK)
104*b1cdbd2cSJim Jagielski    bOK = bOK AND bInsOK
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski    removeElement()
107*b1cdbd2cSJim Jagielski
108*b1cdbd2cSJim Jagielski    bRemOK = ElRem1 = 0 AND ElRem2 >= 1
109*b1cdbd2cSJim Jagielski    Out.Log("... " + bRemOK)
110*b1cdbd2cSJim Jagielski    bOK = bOK AND bRemOK
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski    bNothingToReplace = replaceElement()
113*b1cdbd2cSJim Jagielski
114*b1cdbd2cSJim Jagielski    bRepOK = (ElRep1 = 0 AND ElRep2 >= 1) or bNothingToReplace
115*b1cdbd2cSJim Jagielski    Out.Log("... " + bRepOK)
116*b1cdbd2cSJim Jagielski    bOK = bOK AND bRepOK
117*b1cdbd2cSJim Jagielski    Test.MethodTested("removeContainerListener()", bOK)
118*b1cdbd2cSJim Jagielski
119*b1cdbd2cSJim Jagielski    oObj.removeContainerListener(oListener2)
120*b1cdbd2cSJim Jagielski    Out.Log("Listener2 was removed")
121*b1cdbd2cSJim Jagielski
122*b1cdbd2cSJim JagielskiExit Sub
123*b1cdbd2cSJim JagielskiErrHndl:
124*b1cdbd2cSJim Jagielski    Test.Exception()
125*b1cdbd2cSJim Jagielski    bOK = false
126*b1cdbd2cSJim Jagielski    resume next
127*b1cdbd2cSJim JagielskiEnd Sub
128*b1cdbd2cSJim Jagielski
129*b1cdbd2cSJim JagielskiSub insertElement()
130*b1cdbd2cSJim Jagielski    Out.Log("Inserting element ... ")
131*b1cdbd2cSJim Jagielski    ResetCounters()
132*b1cdbd2cSJim Jagielski    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
133*b1cdbd2cSJim Jagielski        oObj.InsertByName(cIfcShortName, oElementToInsert)
134*b1cdbd2cSJim Jagielski    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
135*b1cdbd2cSJim Jagielski        oContainer.InsertByName(cIfcShortName, oElementToInsert)
136*b1cdbd2cSJim Jagielski    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
137*b1cdbd2cSJim Jagielski        oContainer.addControl("NewControl", oElementToInsert)
138*b1cdbd2cSJim Jagielski    else
139*b1cdbd2cSJim Jagielski        Out.LOG("There is nothig to trigger the Listener!")
140*b1cdbd2cSJim Jagielski    end if
141*b1cdbd2cSJim Jagielskiend Sub
142*b1cdbd2cSJim Jagielski
143*b1cdbd2cSJim Jagielski
144*b1cdbd2cSJim JagielskiSub removeElement()
145*b1cdbd2cSJim Jagielski    Out.Log("Removing element ... ")
146*b1cdbd2cSJim Jagielski    ResetCounters()
147*b1cdbd2cSJim Jagielski    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
148*b1cdbd2cSJim Jagielski        oObj.RemoveByName(cIfcShortName)
149*b1cdbd2cSJim Jagielski    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
150*b1cdbd2cSJim Jagielski        oContainer.RemoveByName(cIfcShortName)
151*b1cdbd2cSJim Jagielski    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
152*b1cdbd2cSJim Jagielski        oContainer.removeControl(oElementToInsert)
153*b1cdbd2cSJim Jagielski    end if
154*b1cdbd2cSJim Jagielskiend Sub
155*b1cdbd2cSJim Jagielski
156*b1cdbd2cSJim JagielskiFunction replaceElement() as Boolean
157*b1cdbd2cSJim Jagielski    Out.Log("Replacing element ... ")
158*b1cdbd2cSJim Jagielski    ResetCounters()
159*b1cdbd2cSJim Jagielski    Dim bNothingToReplace as Boolean
160*b1cdbd2cSJim Jagielski    bNothingToReplace = FALSE
161*b1cdbd2cSJim Jagielski    Dim old As Variant
162*b1cdbd2cSJim Jagielski    if hasUnoInterfaces(oObj, "com.sun.star.container.XIndexReplace") then
163*b1cdbd2cSJim Jagielski        old = oObj.getByIndex(0)
164*b1cdbd2cSJim Jagielski        oObj.ReplaceByIndex(0, oElementToInsert)
165*b1cdbd2cSJim Jagielski        oObj.ReplaceByIndex(0, old)
166*b1cdbd2cSJim Jagielski    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XIndexReplace") then
167*b1cdbd2cSJim Jagielski        old = oContainer.getByIndex(0)
168*b1cdbd2cSJim Jagielski        oContainer.ReplaceByIndex(0, oElementToInsert)
169*b1cdbd2cSJim Jagielski        oContainer.ReplaceByIndex(0, old)
170*b1cdbd2cSJim Jagielski    elseif (hasUnoInterfaces(oContainer, "com.sun.star.container.XNameAccess") and _
171*b1cdbd2cSJim Jagielski            hasUnoInterfaces(oContainer, "com.sun.star.container.XNameReplace")) then
172*b1cdbd2cSJim Jagielski        Dim cNames() as String
173*b1cdbd2cSJim Jagielski        cNames = oObj.getElementNames()
174*b1cdbd2cSJim Jagielski        old = oContainer.getByName(cNames(0))
175*b1cdbd2cSJim Jagielski        oContainer.ReplaceByName(cNames(0), oElementToInsert)
176*b1cdbd2cSJim Jagielski        oContainer.ReplaceByName(cNames(0), old)
177*b1cdbd2cSJim Jagielski    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
178*b1cdbd2cSJim Jagielski        bNothingToReplace = TRUE
179*b1cdbd2cSJim Jagielski    else
180*b1cdbd2cSJim Jagielski        Out.LOG("There is nothig to trigger the Listener!")
181*b1cdbd2cSJim Jagielski    end if
182*b1cdbd2cSJim Jagielski    replaceElement() = bNothingToReplace
183*b1cdbd2cSJim Jagielskiend Function
184*b1cdbd2cSJim Jagielski
185*b1cdbd2cSJim JagielskiSub CB1_elementInserted(ev As Object)
186*b1cdbd2cSJim Jagielski    Out.Log("CB1 called: element was inserted")
187*b1cdbd2cSJim Jagielski    ElIns1 = ElIns1 + 1
188*b1cdbd2cSJim JagielskiEnd Sub
189*b1cdbd2cSJim Jagielski
190*b1cdbd2cSJim JagielskiSub CB1_elementRemoved(ev As Object)
191*b1cdbd2cSJim Jagielski    Out.Log("CB1 called: element was removed")
192*b1cdbd2cSJim Jagielski    ElRem1 = ElRem1 + 1
193*b1cdbd2cSJim JagielskiEnd Sub
194*b1cdbd2cSJim Jagielski
195*b1cdbd2cSJim JagielskiSub CB1_elementReplaced(ev As Object)
196*b1cdbd2cSJim Jagielski    Out.Log("CB1 called: element was replaced")
197*b1cdbd2cSJim Jagielski    ElRep1 = ElRep1 + 1
198*b1cdbd2cSJim JagielskiEnd Sub
199*b1cdbd2cSJim Jagielski
200*b1cdbd2cSJim JagielskiSub CB2_elementInserted(ev As Object)
201*b1cdbd2cSJim Jagielski    Out.Log("CB2 called: element was inserted")
202*b1cdbd2cSJim Jagielski    ElIns2 = ElIns2 + 1
203*b1cdbd2cSJim JagielskiEnd Sub
204*b1cdbd2cSJim Jagielski
205*b1cdbd2cSJim JagielskiSub CB2_elementRemoved(ev As Object)
206*b1cdbd2cSJim Jagielski    Out.Log("CB2 called: element was removed")
207*b1cdbd2cSJim Jagielski    ElRem2 = ElRem2 + 1
208*b1cdbd2cSJim JagielskiEnd Sub
209*b1cdbd2cSJim Jagielski
210*b1cdbd2cSJim JagielskiSub CB2_elementReplaced(ev As Object)
211*b1cdbd2cSJim Jagielski    Out.Log("CB2 called: element was replaced")
212*b1cdbd2cSJim Jagielski    ElRep2 = ElRep2 + 1
213*b1cdbd2cSJim JagielskiEnd Sub
214*b1cdbd2cSJim Jagielski
215*b1cdbd2cSJim JagielskiSub ResetCounters()
216*b1cdbd2cSJim Jagielski    ElIns1 = 0
217*b1cdbd2cSJim Jagielski    ElIns2 = 0
218*b1cdbd2cSJim Jagielski    ElRem1 = 0
219*b1cdbd2cSJim Jagielski    ElRem2 = 0
220*b1cdbd2cSJim Jagielski    ElRep1 = 0
221*b1cdbd2cSJim Jagielski    ElRep2 = 0
222*b1cdbd2cSJim JagielskiEnd Sub
223*b1cdbd2cSJim Jagielski</script:module>
224