summaryrefslogtreecommitdiff
path: root/source/text/sbasic/shared/03132000.xhp
blob: 85b0e1d77a73f4a0eda507a76d6886277b780613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?xml version="1.0" encoding="UTF-8"?>


<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 -->
	
<helpdocument version="1.0">
<meta>
<topic id="textsbasicshared03132000xml" indexer="include" status="PUBLISH">
<title id="tit" xml-lang="en-US">CreateUnoListener Function [Runtime]</title>
<filename>/text/sbasic/shared/03132000.xhp</filename>
</topic>
<history>
<created date="2003-10-31T00:00:00">Sun Microsystems, Inc.</created>
<lastedited date="2004-08-24T11:11:10">converted from old format - fpe
dedr: fixed #i31078#</lastedited>
</history>
</meta>
<body>
<section id="createunolistener">
<bookmark xml-lang="en-US" branch="index" id="bm_id3155150"><bookmark_value>CreateUnoListener function</bookmark_value>
</bookmark>
<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>
<paragraph role="paragraph" id="par_id3149346" xml-lang="en-US" l10n="U" oldref="52">Creates a Listener instance.</paragraph>
</section>
<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>
<paragraph role="heading" id="hd_id3148685" xml-lang="en-US" level="2" l10n="U" oldref="50">Syntax:</paragraph>
<bascode>
<paragraph role="bascode" id="par_id3143228" xml-lang="en-US" oldref="49">oListener = CreateUnoListener( Prefixname, ListenerInterfaceName )</paragraph>
</bascode>
<paragraph role="heading" id="hd_id3147574" xml-lang="en-US" level="2" l10n="U" oldref="48">Example:</paragraph>
<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>
<bascode>
<paragraph role="bascode" localize="false" xml-lang="en-US">Dim oListener</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">oListener = CreateUnoListener( "ContListener_","com.sun.star.container.XContainerListener" )</paragraph>
</bascode>
<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>
<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>
<bascode>
<paragraph role="bascode" localize="false" xml-lang="en-US">Dim oLib</paragraph>
<paragraph role="bascode" id="par_id3154940" xml-lang="en-US" l10n="U" oldref="41">oLib = BasicLibraries.Library1 ' Library1 must exist!</paragraph>
<paragraph role="bascode" id="par_id3150359" xml-lang="en-US" l10n="U" oldref="40">oLib.addContainerListener( oListener ) ' Register the listener</paragraph>
</bascode>
<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>
<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>
<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>
<list type="unordered">
<listitem>
<paragraph role="listitem" id="par_id3151176" xml-lang="en-US" l10n="U" oldref="36">disposing:</paragraph>
</listitem>
<listitem>
<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>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3156212" xml-lang="en-US" l10n="U" oldref="34">elementInserted:</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3159254" xml-lang="en-US" l10n="U" oldref="33">Method of the com.sun.star.container.XContainerListener interface</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3147287" xml-lang="en-US" l10n="U" oldref="32">elementRemoved:</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3146119" xml-lang="en-US" l10n="U" oldref="31">Method of the com.sun.star.container.XContainerListener interface</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3153951" xml-lang="en-US" l10n="U" oldref="30">elementReplaced:</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3154013" xml-lang="en-US" l10n="U" oldref="29">Method of the com.sun.star.container.XContainerListener interface</paragraph>
</listitem>
</list>
<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>
<list type="unordered">
<listitem>
<paragraph role="listitem" id="par_id3155411" xml-lang="en-US" l10n="U" oldref="27">ContListener_disposing</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3146923" xml-lang="en-US" l10n="U" oldref="26">ContListener_elementInserted</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3147318" xml-lang="en-US" l10n="U" oldref="25">ContListener_elementRemoved</paragraph>
</listitem>
<listitem>
<paragraph role="listitem" id="par_id3152578" xml-lang="en-US" l10n="U" oldref="24">ContListener_elementReplaced</paragraph>
</listitem>
</list>
<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>
<bascode>
<paragraph role="bascode" localize="false" xml-lang="en-US">Sub ContListener_disposing( oEvent )</paragraph>
<paragraph role="bascode" id="par_id3153876" xml-lang="en-US" l10n="U" oldref="21">    MsgBox "disposing"</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">    MsgBox oEvent.Dbg_Properties</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US"> </paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">Sub ContListener_elementInserted( oEvent )</paragraph>
<paragraph role="bascode" id="par_id3154098" xml-lang="en-US" l10n="U" oldref="17">    MsgBox "elementInserted"</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">    MsgBox oEvent.Dbg_Properties</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US"> </paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">Sub ContListener_elementRemoved( oEvent )</paragraph>
<paragraph role="bascode" id="par_id3153947" xml-lang="en-US" l10n="U" oldref="13">    MsgBox "elementRemoved"</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">    MsgBox oEvent.Dbg_Properties</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US"> </paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">Sub ContListener_elementReplaced( oEvent )</paragraph>
<paragraph role="bascode" id="par_id3148915" xml-lang="en-US" l10n="U" oldref="9">    MsgBox "elementReplaced"</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">    MsgBox oEvent.Dbg_Properties</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
</bascode>
<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>
<bascode>
<paragraph role="bascode" id="par_id3150042" xml-lang="en-US" l10n="U" oldref="5">' Minimal implementation of Sub disposing</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">Sub ContListener_disposing</paragraph>
<paragraph role="bascode" localize="false" xml-lang="en-US">End Sub</paragraph>
</bascode>
<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>
</body>
</helpdocument>