xref: /trunk/main/chart2/source/inc/WeakListenerAdapter.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 #ifndef CHART2_WEAKLISTENERADAPTER_HXX
24 #define CHART2_WEAKLISTENERADAPTER_HXX
25 
26 #include <com/sun/star/uno/XWeak.hpp>
27 #include <com/sun/star/lang/XEventListener.hpp>
28 #include <com/sun/star/util/XModifyListener.hpp>
29 #include <com/sun/star/view/XSelectionChangeListener.hpp>
30 #include <cppuhelper/weakref.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 
33 namespace chart
34 {
35 // --------------------------------------------------------------------------------
36 
37 /** Adapter that enables adding listeners as weak UNO references.  Thus, adding
38     an object as listener to a broadcaster does not increase its reference
39     count.
40 
41     <p>The helper class, of course, is held as hard reference at the
42     broadcaster, but this should never be a problem as the adapter's life time
43     depends on no other object.</p>
44 
45     <p>Note that in order to remove an object as listener, you have to remove
46     the same wrapper that you added, i.e., you should store the adapter as a
47     member in the adaptee class for later use.</p>
48  */
49 template< class Listener >
50     class WeakListenerAdapter : public
51     ::cppu::WeakImplHelper1< Listener >
52 {
53 public:
WeakListenerAdapter(const::com::sun::star::uno::Reference<Listener> & xListener)54     explicit WeakListenerAdapter( const ::com::sun::star::uno::Reference< Listener > & xListener ) :
55             m_xListener( xListener )
56     {}
WeakListenerAdapter(const::com::sun::star::uno::WeakReference<Listener> & xListener)57     explicit WeakListenerAdapter( const ::com::sun::star::uno::WeakReference< Listener > & xListener ) :
58             m_xListener( xListener )
59     {}
~WeakListenerAdapter()60     virtual ~WeakListenerAdapter()
61     {}
62 
63 protected:
64     // ____ XEventListener (base of all listeners) ____
disposing(const::com::sun::star::lang::EventObject & Source)65     virtual void SAL_CALL disposing(
66         const ::com::sun::star::lang::EventObject& Source )
67     {
68         ::com::sun::star::uno::Reference<
69               ::com::sun::star::lang::XEventListener > xEventListener =
70           ::com::sun::star::uno::Reference<
71               ::com::sun::star::lang::XEventListener >(
72                   ::com::sun::star::uno::Reference< Listener >( m_xListener), ::com::sun::star::uno::UNO_QUERY );
73         if( xEventListener.is())
74             xEventListener->disposing( Source );
75     }
76 
getListener() const77     ::com::sun::star::uno::Reference< Listener > getListener() const
78     {
79         return m_xListener;
80     }
81 
82 private:
83     ::com::sun::star::uno::WeakReference< Listener > m_xListener;
84 };
85 
86 // --------------------------------------------------------------------------------
87 
88 class WeakModifyListenerAdapter :
89         public WeakListenerAdapter< ::com::sun::star::util::XModifyListener >
90 {
91 public:
92     explicit WeakModifyListenerAdapter(
93         const ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener > & xListener );
94     virtual ~WeakModifyListenerAdapter();
95 
96 protected:
97     // ____ XModifyListener ____
98     virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent );
99 };
100 
101 // --------------------------------------------------------------------------------
102 
103 class WeakSelectionChangeListenerAdapter :
104         public WeakListenerAdapter< ::com::sun::star::view::XSelectionChangeListener >
105 {
106 public:
107     explicit WeakSelectionChangeListenerAdapter(
108         const ::com::sun::star::uno::Reference< ::com::sun::star::view::XSelectionChangeListener > & xListener );
109     virtual ~WeakSelectionChangeListenerAdapter();
110 
111 protected:
112     // ____ XSelectionChangeListener ____
113     virtual void SAL_CALL selectionChanged(
114         const ::com::sun::star::lang::EventObject& aEvent );
115 };
116 
117 } //  namespace chart
118 
119 // CHART2_WEAKLISTENERADAPTER_HXX
120 #endif
121