1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef CHART2_WEAKLISTENERADAPTER_HXX
28 #define CHART2_WEAKLISTENERADAPTER_HXX
29 
30 #include <com/sun/star/uno/XWeak.hpp>
31 #include <com/sun/star/lang/XEventListener.hpp>
32 #include <com/sun/star/util/XModifyListener.hpp>
33 #include <com/sun/star/view/XSelectionChangeListener.hpp>
34 #include <cppuhelper/weakref.hxx>
35 #include <cppuhelper/implbase1.hxx>
36 
37 namespace chart
38 {
39 // --------------------------------------------------------------------------------
40 
41 /** Adapter that enables adding listeners as weak UNO references.  Thus, adding
42     an object as listener to a broadcaster does not increase its reference
43     count.
44 
45     <p>The helper class, of course, is held as hard reference at the
46     broadcaster, but this should never be a problem as the adapter's life time
47     depends on no other object.</p>
48 
49     <p>Note that in order to remove an object as listener, you have to remove
50     the same wrapper that you added, i.e., you should store the adapter as a
51     member in the adaptee class for later use.</p>
52  */
53 template< class Listener >
54     class WeakListenerAdapter : public
55     ::cppu::WeakImplHelper1< Listener >
56 {
57 public:
58 	explicit WeakListenerAdapter( const ::com::sun::star::uno::Reference< Listener > & xListener ) :
59             m_xListener( xListener )
60     {}
61 	explicit WeakListenerAdapter( const ::com::sun::star::uno::WeakReference< Listener > & xListener ) :
62             m_xListener( xListener )
63     {}
64 	virtual ~WeakListenerAdapter()
65     {}
66 
67 protected:
68     // ____ XEventListener (base of all listeners) ____
69     virtual void SAL_CALL disposing(
70         const ::com::sun::star::lang::EventObject& Source )
71         throw (::com::sun::star::uno::RuntimeException)
72     {
73         ::com::sun::star::uno::Reference<
74               ::com::sun::star::lang::XEventListener > xEventListener =
75           ::com::sun::star::uno::Reference<
76               ::com::sun::star::lang::XEventListener >(
77                   ::com::sun::star::uno::Reference< Listener >( m_xListener), ::com::sun::star::uno::UNO_QUERY );
78         if( xEventListener.is())
79             xEventListener->disposing( Source );
80     }
81 
82     ::com::sun::star::uno::Reference< Listener > getListener() const
83     {
84         return m_xListener;
85     }
86 
87 private:
88     ::com::sun::star::uno::WeakReference< Listener > m_xListener;
89 };
90 
91 // --------------------------------------------------------------------------------
92 
93 class WeakModifyListenerAdapter :
94         public WeakListenerAdapter< ::com::sun::star::util::XModifyListener >
95 {
96 public:
97 	explicit WeakModifyListenerAdapter(
98         const ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener > & xListener );
99 	virtual ~WeakModifyListenerAdapter();
100 
101 protected:
102     // ____ XModifyListener ____
103     virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent )
104         throw (::com::sun::star::uno::RuntimeException);
105 };
106 
107 // --------------------------------------------------------------------------------
108 
109 class WeakSelectionChangeListenerAdapter :
110         public WeakListenerAdapter< ::com::sun::star::view::XSelectionChangeListener >
111 {
112 public:
113     explicit WeakSelectionChangeListenerAdapter(
114         const ::com::sun::star::uno::Reference< ::com::sun::star::view::XSelectionChangeListener > & xListener );
115 	virtual ~WeakSelectionChangeListenerAdapter();
116 
117 protected:
118     // ____ XSelectionChangeListener ____
119     virtual void SAL_CALL selectionChanged(
120         const ::com::sun::star::lang::EventObject& aEvent )
121         throw (::com::sun::star::uno::RuntimeException);
122 };
123 
124 } //  namespace chart
125 
126 // CHART2_WEAKLISTENERADAPTER_HXX
127 #endif
128