merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / framework / configuration / ConfigurationControllerBroadcaster.cxx
blob6677c435e6b694989c411c8f35a6f050222043cf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationControllerBroadcaster.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
33 #include "ConfigurationControllerBroadcaster.hxx"
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <com/sun/star/lang/DisposedException.hpp>
36 #include <tools/debug.hxx>
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::drawing::framework;
41 using rtl::OUString;
43 namespace sd { namespace framework {
45 ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster (
46 const Reference<XConfigurationController>& rxController)
47 : mxConfigurationController(rxController),
48 maListenerMap()
55 void ConfigurationControllerBroadcaster::AddListener(
56 const Reference<XConfigurationChangeListener>& rxListener,
57 const ::rtl::OUString& rsEventType,
58 const Any& rUserData)
60 if ( ! rxListener.is())
61 throw lang::IllegalArgumentException(
62 OUString::createFromAscii("invalid listener"),
63 mxConfigurationController,
64 0);
66 if (maListenerMap.find(rsEventType) == maListenerMap.end())
67 maListenerMap[rsEventType] = ListenerList();
68 ListenerDescriptor aDescriptor;
69 aDescriptor.mxListener = rxListener;
70 aDescriptor.maUserData = rUserData;
71 maListenerMap[rsEventType].push_back(aDescriptor);
77 void ConfigurationControllerBroadcaster::RemoveListener(
78 const Reference<XConfigurationChangeListener>& rxListener)
80 if ( ! rxListener.is())
81 throw lang::IllegalArgumentException(
82 OUString::createFromAscii("invalid listener"),
83 mxConfigurationController,
84 0);
86 ListenerMap::iterator iMap;
87 ListenerList::iterator iList;
88 for (iMap=maListenerMap.begin(); iMap!=maListenerMap.end(); ++iMap)
90 for (iList=iMap->second.begin(); iList!=iMap->second.end(); ++iList)
92 if (iList->mxListener == rxListener)
94 iMap->second.erase(iList);
95 break;
104 void ConfigurationControllerBroadcaster::NotifyListeners (
105 const ListenerList& rList,
106 const ConfigurationChangeEvent& rEvent)
108 // Create a local copy of the event in which the user data is modified
109 // for every listener.
110 ConfigurationChangeEvent aEvent (rEvent);
112 ListenerList::const_iterator iListener;
113 for (iListener=rList.begin(); iListener!=rList.end(); ++iListener)
117 aEvent.UserData = iListener->maUserData;
118 iListener->mxListener->notifyConfigurationChange(aEvent);
120 catch (lang::DisposedException& rException)
122 // When the exception comes from the listener itself then
123 // unregister it.
124 if (rException.Context == iListener->mxListener)
125 RemoveListener(iListener->mxListener);
127 catch(RuntimeException&)
129 DBG_ASSERT(false,
130 "ConfigurationController: caught exception while notifying listeners");
138 void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent& rEvent)
140 // Notify the specialized listeners.
141 ListenerMap::const_iterator iMap (maListenerMap.find(rEvent.Type));
142 if (iMap != maListenerMap.end())
144 // Create a local list of the listeners to avoid problems with
145 // concurrent changes and to be able to remove disposed listeners.
146 ListenerList aList (iMap->second.begin(), iMap->second.end());
147 NotifyListeners(aList,rEvent);
150 // Notify the universal listeners.
151 iMap = maListenerMap.find(OUString());
152 if (iMap != maListenerMap.end())
154 // Create a local list of the listeners to avoid problems with
155 // concurrent changes and to be able to remove disposed listeners.
156 ListenerList aList (iMap->second.begin(), iMap->second.end());
157 NotifyListeners(aList,rEvent);
164 void ConfigurationControllerBroadcaster::NotifyListeners (
165 const OUString& rsEventType,
166 const Reference<XResourceId>& rxResourceId,
167 const Reference<XResource>& rxResourceObject)
169 ConfigurationChangeEvent aEvent;
170 aEvent.Type = rsEventType;
171 aEvent.ResourceId = rxResourceId;
172 aEvent.ResourceObject = rxResourceObject;
175 NotifyListeners(aEvent);
177 catch (lang::DisposedException)
186 void ConfigurationControllerBroadcaster::DisposeAndClear (void)
188 lang::EventObject aEvent;
189 aEvent.Source = mxConfigurationController;
190 while (maListenerMap.size() > 0)
192 ListenerMap::iterator iMap (maListenerMap.begin());
193 if (iMap == maListenerMap.end())
194 break;
196 // When the first vector is empty then remove it from the map.
197 if (iMap->second.size() == 0)
199 maListenerMap.erase(iMap);
200 continue;
202 else
204 Reference<lang::XEventListener> xListener (
205 iMap->second.front().mxListener, UNO_QUERY);
206 if (xListener.is())
208 // Tell the listener that the configuration controller is
209 // being disposed and remove the listener (for all event
210 // types).
213 RemoveListener(iMap->second.front().mxListener);
214 xListener->disposing(aEvent);
216 catch (RuntimeException&)
218 DBG_ASSERT(false,
219 "ConfigurationController: caught exception while notifying dispose");
222 else
224 // Remove just this reference to the listener.
225 iMap->second.erase(iMap->second.begin());
234 } } // end of namespace sd::framework