1<?xml version="1.0" encoding="UTF-8"?>
2
3
4<!--***********************************************************
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements.  See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership.  The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License.  You may obtain a copy of the License at
13 *
14 *   http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied.  See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 ***********************************************************-->
24
25
26
27<helpdocument version="1.0">
28<meta>
29<topic id="textsbasicshared03132000xml" indexer="include" status="PUBLISH">
30<title id="tit" xml-lang="en-US">CreateUnoListener Function [Runtime]</title>
31<filename>/text/sbasic/shared/03132000.xhp</filename>
32</topic>
33<history>
34<created date="2003-10-31T00:00:00">Sun Microsystems, Inc.</created>
35<lastedited date="2004-08-24T11:11:10">converted from old format - fpe
36dedr: fixed #i31078#</lastedited>
37</history>
38</meta>
39<body>
40<section id="createunolistener">
41<bookmark xml-lang="en-US" branch="index" id="bm_id3155150"><bookmark_value>CreateUnoListener function</bookmark_value>
42</bookmark>
43<paragraph role="heading" id="hd_id3155150" xml-lang="en-US" level="1" l10n="U" oldref="53"><link href="text/sbasic/shared/03132000.xhp" name="CreateUnoListener Function [Runtime]">CreateUnoListener Function [Runtime]</link></paragraph>
44<paragraph role="paragraph" id="par_id3149346" xml-lang="en-US" l10n="U" oldref="52">Creates a Listener instance.</paragraph>
45</section>
46<paragraph role="paragraph" id="par_id3153681" xml-lang="en-US" l10n="U" oldref="51">Many Uno interfaces let you register listeners on a special listener interface. This allows you to listen for specific events and call up the appropriate listener method. The CreateUnoListener function waits for the called listener interface and then passes the interface an object that the interface supports. This object is then passed to the method to register the listener.</paragraph>
47<paragraph role="heading" id="hd_id3148685" xml-lang="en-US" level="2" l10n="U" oldref="50">Syntax:</paragraph>
48<paragraph role="paragraph" id="par_id3143228" xml-lang="en-US" l10n="U" oldref="49">oListener = CreateUnoListener( Prefixname, ListenerInterfaceName )</paragraph>
49<paragraph role="heading" id="hd_id3147574" xml-lang="en-US" level="2" l10n="U" oldref="48">Example:</paragraph>
50<paragraph role="paragraph" id="par_id3154046" xml-lang="en-US" l10n="U" oldref="47">The following example is based on a Basic library object.</paragraph>
51<paragraph role="paragraph" id="par_id3155136" xml-lang="en-US" l10n="U" oldref="46">Dim oListener</paragraph>
52<paragraph role="paragraph" id="par_id3148944" xml-lang="en-US" l10n="U" oldref="45">oListener = CreateUnoListener( "ContListener_","com.sun.star.container.XContainerListener" )</paragraph>
53<paragraph role="paragraph" id="par_id3149294" xml-lang="en-US" l10n="U" oldref="44">The CreateUnoListener method requires two parameters. The first is a prefix and is explained in detail below. The second parameter is the fully qualified name of the Listener interface that you want to use.</paragraph>
54<paragraph role="paragraph" id="par_id3149670" xml-lang="en-US" l10n="U" oldref="43">The Listener must then be added to the Broadcaster Object. This is done by calling the appropriate method for adding a Listener. These methods always follow the pattern "addFooListener", where "Foo" is the Listener Interface Type, without the 'X'. In this example, the addContainerListener method is called to register the XContainerListener:</paragraph>
55<paragraph role="paragraph" id="par_id3154164" xml-lang="en-US" l10n="U" oldref="42">Dim oLib</paragraph>
56<paragraph role="paragraph" id="par_id3154940" xml-lang="en-US" l10n="U" oldref="41">oLib = BasicLibraries.Library1 ' Library1 must exist!</paragraph>
57<paragraph role="paragraph" id="par_id3150359" xml-lang="en-US" l10n="U" oldref="40">oLib.addContainerListener( oListener ) ' Register the listener</paragraph>
58<paragraph role="paragraph" id="par_id3154138" xml-lang="en-US" l10n="U" oldref="39">The Listener is now registered. When an event occurs, the corresponding Listener calls the appropriate method from the com.sun.star.container.XContainerListener Interface.</paragraph>
59<paragraph role="paragraph" id="par_id3148922" xml-lang="en-US" l10n="U" oldref="38">The prefix calls registered Listeners from Basic-subroutines. The Basic run-time system searches for Basic-subroutines or functions that have the name "PrefixListenerMethode" and calls them when found. Otherwise, a run-time error occurs.</paragraph>
60<paragraph role="paragraph" id="par_id3150768" xml-lang="en-US" l10n="U" oldref="37">In this example, the Listener-Interface uses the following methods:</paragraph>
61<list type="unordered">
62<listitem>
63<paragraph role="listitem" id="par_id3151176" xml-lang="en-US" l10n="U" oldref="36">disposing:</paragraph>
64</listitem>
65<listitem>
66<paragraph role="listitem" id="par_id3145173" xml-lang="en-US" l10n="U" oldref="35">Listener base interface (com.sun.star.lang.XEventListener): base interface for all Listener Interfaces</paragraph>
67</listitem>
68<listitem>
69<paragraph role="listitem" id="par_id3156212" xml-lang="en-US" l10n="U" oldref="34">elementInserted:</paragraph>
70</listitem>
71<listitem>
72<paragraph role="listitem" id="par_id3159254" xml-lang="en-US" l10n="U" oldref="33">Method of the com.sun.star.container.XContainerListener interface</paragraph>
73</listitem>
74<listitem>
75<paragraph role="listitem" id="par_id3147287" xml-lang="en-US" l10n="U" oldref="32">elementRemoved:</paragraph>
76</listitem>
77<listitem>
78<paragraph role="listitem" id="par_id3146119" xml-lang="en-US" l10n="U" oldref="31">Method of the com.sun.star.container.XContainerListener interface</paragraph>
79</listitem>
80<listitem>
81<paragraph role="listitem" id="par_id3153951" xml-lang="en-US" l10n="U" oldref="30">elementReplaced:</paragraph>
82</listitem>
83<listitem>
84<paragraph role="listitem" id="par_id3154013" xml-lang="en-US" l10n="U" oldref="29">Method of the com.sun.star.container.XContainerListener interface</paragraph>
85</listitem>
86</list>
87<paragraph role="paragraph" id="par_id3147435" xml-lang="en-US" l10n="U" oldref="28">In this example, the prefix is ContListener_. The following subroutines must therefore be implemented in Basic:</paragraph>
88<list type="unordered">
89<listitem>
90<paragraph role="listitem" id="par_id3155411" xml-lang="en-US" l10n="U" oldref="27">ContListener_disposing</paragraph>
91</listitem>
92<listitem>
93<paragraph role="listitem" id="par_id3146923" xml-lang="en-US" l10n="U" oldref="26">ContListener_elementInserted</paragraph>
94</listitem>
95<listitem>
96<paragraph role="listitem" id="par_id3147318" xml-lang="en-US" l10n="U" oldref="25">ContListener_elementRemoved</paragraph>
97</listitem>
98<listitem>
99<paragraph role="listitem" id="par_id3152578" xml-lang="en-US" l10n="U" oldref="24">ContListener_elementReplaced</paragraph>
100</listitem>
101</list>
102<paragraph role="paragraph" id="par_id3150592" xml-lang="en-US" l10n="U" oldref="23">An event structure type that contains information about an event exists for every Listener type. When a Listener method is called, an instance of this event is passed to the method as a parameter. Basic Listener methods can also call these event objects, so long as the appropriate parameter is passed in the Sub declaration. For example:</paragraph>
103<paragraph role="paragraph" id="par_id3149582" xml-lang="en-US" l10n="U" oldref="22">Sub ContListener_disposing( oEvent )</paragraph>
104<paragraph role="paragraph" id="par_id3153876" xml-lang="en-US" l10n="U" oldref="21">MsgBox "disposing"</paragraph>
105<paragraph role="paragraph" id="par_id3149959" xml-lang="en-US" l10n="U" oldref="20">MsgBox oEvent.Dbg_Properties</paragraph>
106<paragraph role="paragraph" id="par_id3154490" xml-lang="en-US" l10n="U" oldref="19">End Sub</paragraph>
107<paragraph role="paragraph" id="par_id3156285" xml-lang="en-US" l10n="U" oldref="18">Sub ContListener_elementInserted( oEvent )</paragraph>
108<paragraph role="paragraph" id="par_id3154098" xml-lang="en-US" l10n="U" oldref="17">MsgBox "elementInserted"</paragraph>
109<paragraph role="paragraph" id="par_id3155601" xml-lang="en-US" l10n="U" oldref="16">MsgBox oEvent.Dbg_Properties</paragraph>
110<paragraph role="paragraph" id="par_id3153415" xml-lang="en-US" l10n="U" oldref="15">End Sub</paragraph>
111<paragraph role="paragraph" id="par_id3154272" xml-lang="en-US" l10n="U" oldref="14">Sub ContListener_elementRemoved( oEvent )</paragraph>
112<paragraph role="paragraph" id="par_id3153947" xml-lang="en-US" l10n="U" oldref="13">MsgBox "elementRemoved"</paragraph>
113<paragraph role="paragraph" id="par_id3146914" xml-lang="en-US" l10n="U" oldref="12">MsgBox oEvent.Dbg_Properties</paragraph>
114<paragraph role="paragraph" id="par_id3150749" xml-lang="en-US" l10n="U" oldref="11">End Sub</paragraph>
115<paragraph role="paragraph" id="par_id3145642" xml-lang="en-US" l10n="U" oldref="10">Sub ContListener_elementReplaced( oEvent )</paragraph>
116<paragraph role="paragraph" id="par_id3148915" xml-lang="en-US" l10n="U" oldref="9">MsgBox "elementReplaced"</paragraph>
117<paragraph role="paragraph" id="par_id3148995" xml-lang="en-US" l10n="U" oldref="8">MsgBox oEvent.Dbg_Properties</paragraph>
118<paragraph role="paragraph" id="par_id3148407" xml-lang="en-US" l10n="U" oldref="7">End Sub</paragraph>
119<paragraph role="paragraph" id="par_id3156056" xml-lang="en-US" l10n="U" oldref="6">You do not need to include the parameter of an event object if the object is not used:</paragraph>
120<paragraph role="paragraph" id="par_id3150042" xml-lang="en-US" l10n="U" oldref="5">' Minimal implementation of Sub disposing</paragraph>
121<paragraph role="paragraph" id="par_id3151249" xml-lang="en-US" l10n="U" oldref="4">Sub ContListener_disposing</paragraph>
122<paragraph role="paragraph" id="par_id3155333" xml-lang="en-US" l10n="U" oldref="3">End Sub</paragraph>
123<paragraph role="warning" id="par_id3150940" xml-lang="en-US" l10n="U" oldref="2">Listener methods must <emph>always</emph> be implemented to avoid Basic run-time errors.</paragraph>
124</body>
125</helpdocument>
126