xref: /AOO41X/main/odk/examples/cpp/complextoolbarcontrols/ListenerHelper.cxx (revision 6b3ad84fba3dab1b753e0059435711ac8482622a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #include "ListenerHelper.h"
26 
27 using com::sun::star::frame::XFrame;
28 using com::sun::star::frame::XDispatch;
29 using com::sun::star::frame::XStatusListener;
30 using com::sun::star::lang::XEventListener;
31 using com::sun::star::lang::EventObject;
32 using com::sun::star::uno::Reference;
33 using com::sun::star::uno::RuntimeException;
34 using com::sun::star::frame::FeatureStateEvent;
35 
36 static AllListeners aListeners;
37 
AddListener(const Reference<XFrame> & xFrame,const Reference<XStatusListener> xControl,const::rtl::OUString & aCommand)38 void ListenerHelper::AddListener(
39     const Reference < XFrame >& xFrame,
40     const Reference < XStatusListener > xControl,
41     const ::rtl::OUString& aCommand )
42 {
43     sal_uInt32 i=0;
44     sal_uInt32 nSize = aListeners.size();
45     for ( i=0; i<nSize; i++ )
46         if ( aListeners[i].xFrame == xFrame )
47             break;
48 
49     OSL_ENSURE( i<nSize, "No dispatch found for this listener!" );
50     aListeners[i].aContainer[aCommand].push_back( xControl );
51 }
52 
RemoveListener(const Reference<XFrame> & xFrame,const Reference<XStatusListener> xControl,const::rtl::OUString & aCommand)53 void ListenerHelper::RemoveListener(
54     const Reference < XFrame >& xFrame,
55     const Reference < XStatusListener > xControl,
56     const ::rtl::OUString& aCommand )
57 {
58     sal_uInt32 nSize = aListeners.size();
59     for ( sal_uInt32 i=0; i<nSize; i++ )
60     {
61         if ( aListeners[i].xFrame == xFrame )
62         {
63             StatusListeners& aL = aListeners[i].aContainer[aCommand];
64             StatusListeners::iterator aIter = aL.begin();
65             while ( aIter != aL.end() )
66             {
67                 if ( (*aIter) == xControl )
68                 {
69                     aL.erase( aIter );
70                     break;
71                 }
72 
73                 aIter++;
74             }
75         }
76     }
77 }
78 
Notify(const Reference<XFrame> & xFrame,const::rtl::OUString & aCommand,FeatureStateEvent & rEvent)79 void ListenerHelper::Notify(
80         const Reference < XFrame >& xFrame,
81         const ::rtl::OUString& aCommand,
82         FeatureStateEvent& rEvent )
83 {
84     sal_uInt32 nSize = aListeners.size();
85     for ( sal_uInt32 i=0; i<nSize; i++ )
86     {
87         if ( aListeners[i].xFrame == xFrame )
88         {
89             rEvent.Source = aListeners[i].xDispatch;
90             StatusListeners& aL = aListeners[i].aContainer[aCommand];
91             StatusListeners::iterator aIter = aL.begin();
92             while ( aIter != aL.end() )
93             {
94                 (*aIter)->statusChanged( rEvent );
95                 aIter++;
96             }
97         }
98     }
99 }
100 
GetDispatch(const Reference<XFrame> & xFrame,const::rtl::OUString & aCommand)101 com::sun::star::uno::Reference < XDispatch > ListenerHelper::GetDispatch(
102         const Reference < XFrame >& xFrame,
103         const ::rtl::OUString& aCommand )
104 {
105     sal_uInt32 nSize = aListeners.size();
106     for ( sal_uInt32 i=0; i<nSize; i++ )
107     {
108         if ( aListeners[i].xFrame == xFrame )
109             return aListeners[i].xDispatch;
110     }
111 
112     return Reference < XDispatch >();
113 }
114 
AddDispatch(const Reference<XDispatch> xDispatch,const Reference<XFrame> & xFrame,const::rtl::OUString & aCommand)115 void ListenerHelper::AddDispatch(
116         const Reference < XDispatch > xDispatch,
117         const Reference < XFrame >& xFrame,
118         const ::rtl::OUString& aCommand )
119 {
120     ListenerItem aItem;
121     aItem.xFrame = xFrame;
122     aItem.xDispatch = xDispatch;
123     aListeners.push_back( aItem );
124     xFrame->addEventListener( new ListenerItemEventListener( xFrame ) );
125 }
126 
disposing(const EventObject & aEvent)127 void SAL_CALL ListenerItemEventListener::disposing( const EventObject& aEvent) throw (RuntimeException)
128 {
129     AllListeners::iterator aIter = aListeners.begin();
130     while ( aIter != aListeners.end() )
131     {
132         if ( (*aIter).xFrame == mxFrame )
133         {
134             aListeners.erase( aIter );
135             break;
136         }
137 
138         aIter++;
139     }
140 }
141