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         throw (::com::sun::star::uno::RuntimeException)
68     {
69         ::com::sun::star::uno::Reference<
70               ::com::sun::star::lang::XEventListener > xEventListener =
71           ::com::sun::star::uno::Reference<
72               ::com::sun::star::lang::XEventListener >(
73                   ::com::sun::star::uno::Reference< Listener >( m_xListener), ::com::sun::star::uno::UNO_QUERY );
74         if( xEventListener.is())
75             xEventListener->disposing( Source );
76     }
77 
getListener() const78     ::com::sun::star::uno::Reference< Listener > getListener() const
79     {
80         return m_xListener;
81     }
82 
83 private:
84     ::com::sun::star::uno::WeakReference< Listener > m_xListener;
85 };
86 
87 // --------------------------------------------------------------------------------
88 
89 class WeakModifyListenerAdapter :
90         public WeakListenerAdapter< ::com::sun::star::util::XModifyListener >
91 {
92 public:
93 	explicit WeakModifyListenerAdapter(
94         const ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener > & xListener );
95 	virtual ~WeakModifyListenerAdapter();
96 
97 protected:
98     // ____ XModifyListener ____
99     virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent )
100         throw (::com::sun::star::uno::RuntimeException);
101 };
102 
103 // --------------------------------------------------------------------------------
104 
105 class WeakSelectionChangeListenerAdapter :
106         public WeakListenerAdapter< ::com::sun::star::view::XSelectionChangeListener >
107 {
108 public:
109     explicit WeakSelectionChangeListenerAdapter(
110         const ::com::sun::star::uno::Reference< ::com::sun::star::view::XSelectionChangeListener > & xListener );
111 	virtual ~WeakSelectionChangeListenerAdapter();
112 
113 protected:
114     // ____ XSelectionChangeListener ____
115     virtual void SAL_CALL selectionChanged(
116         const ::com::sun::star::lang::EventObject& aEvent )
117         throw (::com::sun::star::uno::RuntimeException);
118 };
119 
120 } //  namespace chart
121 
122 // CHART2_WEAKLISTENERADAPTER_HXX
123 #endif
124