xref: /trunk/main/reportdesign/source/core/api/Groups.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 #include "Groups.hxx"
28 #include "Group.hxx"
29 #include <tools/debug.hxx>
30 #include "core_resource.hxx"
31 #ifndef REPORTDESIGN_CORE_RESOURCE_HRC_
32 #include "core_resource.hrc"
33 #endif
34 #include <boost/bind.hpp>
35 #include <algorithm>
36 // =============================================================================
37 namespace reportdesign
38 {
39 // =============================================================================
40     using namespace com::sun::star;
41 DBG_NAME( rpt_OGroups )
42 // -----------------------------------------------------------------------------
43 OGroups::OGroups(const uno::Reference< report::XReportDefinition >& _xParent,const uno::Reference< uno::XComponentContext >& context)
44 :GroupsBase(m_aMutex)
45 ,m_aContainerListeners(m_aMutex)
46 ,m_xContext(context)
47 ,m_xParent(_xParent)
48 {
49     DBG_CTOR( rpt_OGroups,NULL);
50 }
51 //--------------------------------------------------------------------------
52 // TODO: VirtualFunctionFinder: This is virtual function!
53 //
54 OGroups::~OGroups()
55 {
56     DBG_DTOR( rpt_OGroups,NULL);
57 }
58 //--------------------------------------------------------------------------
59 void OGroups::copyGroups(const uno::Reference< report::XGroups >& _xSource)
60 {
61     sal_Int32 nCount = _xSource->getCount();
62     for (sal_Int32 i = 0; i != nCount; ++i)
63     {
64         OGroup* pGroup = new OGroup(this,m_xContext);
65         m_aGroups.push_back(pGroup);
66         uno::Reference<report::XGroup> xGroup(_xSource->getByIndex(i),uno::UNO_QUERY);
67         pGroup->copyGroup(xGroup);
68     }
69 }
70 // -----------------------------------------------------------------------------
71 void SAL_CALL OGroups::dispose() throw(uno::RuntimeException)
72 {
73     cppu::WeakComponentImplHelperBase::dispose();
74 }
75 // -----------------------------------------------------------------------------
76 // TODO: VirtualFunctionFinder: This is virtual function!
77 //
78 void SAL_CALL OGroups::disposing()
79 {
80     ::std::for_each(m_aGroups.begin(),m_aGroups.end(),::boost::mem_fn(&com::sun::star::report::XGroup::dispose));
81     m_aGroups.clear();
82     lang::EventObject aDisposeEvent( static_cast< ::cppu::OWeakObject* >( this ) );
83     m_aContainerListeners.disposeAndClear( aDisposeEvent );
84     m_xContext.clear();
85 }
86 // -----------------------------------------------------------------------------
87 // XGroups
88 uno::Reference< report::XReportDefinition > SAL_CALL OGroups::getReportDefinition() throw (uno::RuntimeException)
89 {
90     return m_xParent;
91 }
92 // -----------------------------------------------------------------------------
93 uno::Reference< report::XGroup > SAL_CALL OGroups::createGroup(  ) throw (uno::RuntimeException)
94 {
95     return new OGroup(this,m_xContext);
96 }
97 // -----------------------------------------------------------------------------
98 // XIndexContainer
99 void SAL_CALL OGroups::insertByIndex( ::sal_Int32 Index, const uno::Any& aElement ) throw (lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
100 {
101     {
102         ::osl::MutexGuard aGuard(m_aMutex);
103         sal_Bool bAdd = (Index == static_cast<sal_Int32>(m_aGroups.size()));
104         if ( !bAdd )
105             checkIndex(Index);
106         uno::Reference< report::XGroup > xGroup(aElement,uno::UNO_QUERY);
107         if ( !xGroup.is() )
108             throw lang::IllegalArgumentException(RPT_RESSTRING(RID_STR_ARGUMENT_IS_NULL,m_xContext->getServiceManager()),*this,2);
109 
110         if ( bAdd )
111             m_aGroups.push_back(xGroup);
112         else
113         {
114             TGroups::iterator aPos = m_aGroups.begin();
115             ::std::advance(aPos,Index);
116             m_aGroups.insert(aPos, xGroup);
117         }
118     }
119     // notify our container listeners
120     container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::makeAny(Index), aElement, uno::Any());
121     m_aContainerListeners.notifyEach(&container::XContainerListener::elementInserted,aEvent);
122 }
123 
124 // -----------------------------------------------------------------------------
125 void SAL_CALL OGroups::removeByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
126 {
127     uno::Reference< report::XGroup > xGroup;
128     {
129         ::osl::MutexGuard aGuard(m_aMutex);
130         checkIndex(Index);
131         TGroups::iterator aPos = m_aGroups.begin();
132         ::std::advance(aPos,Index);
133         xGroup = *aPos;
134         m_aGroups.erase(aPos);
135     }
136     container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::makeAny(Index), uno::makeAny(xGroup), uno::Any());
137     m_aContainerListeners.notifyEach(&container::XContainerListener::elementRemoved,aEvent);
138 }
139 // -----------------------------------------------------------------------------
140 // XIndexReplace
141 void SAL_CALL OGroups::replaceByIndex( ::sal_Int32 Index, const uno::Any& Element ) throw (lang::IllegalArgumentException, lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
142 {
143     uno::Any aOldElement;
144     {
145         ::osl::MutexGuard aGuard(m_aMutex);
146         checkIndex(Index);
147         uno::Reference< report::XGroup > xGroup(Element,uno::UNO_QUERY);
148         if ( !xGroup.is() )
149             throw lang::IllegalArgumentException(RPT_RESSTRING(RID_STR_ARGUMENT_IS_NULL,m_xContext->getServiceManager()),*this,2);
150         TGroups::iterator aPos = m_aGroups.begin();
151         ::std::advance(aPos,Index);
152         aOldElement <<= *aPos;
153         *aPos = xGroup;
154     }
155 
156     container::ContainerEvent aEvent(static_cast<container::XContainer*>(this), uno::makeAny(Index), Element, aOldElement);
157     m_aContainerListeners.notifyEach(&container::XContainerListener::elementReplaced,aEvent);
158 }
159 // -----------------------------------------------------------------------------
160 // XIndexAccess
161 ::sal_Int32 SAL_CALL OGroups::getCount(  ) throw (uno::RuntimeException)
162 {
163     ::osl::MutexGuard aGuard(m_aMutex);
164     return m_aGroups.size();
165 }
166 // -----------------------------------------------------------------------------
167 uno::Any SAL_CALL OGroups::getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
168 {
169     ::osl::MutexGuard aGuard(m_aMutex);
170     checkIndex(Index);
171     TGroups::iterator aPos = m_aGroups.begin();
172     ::std::advance(aPos,Index);
173     return uno::makeAny(*aPos);
174 }
175 // -----------------------------------------------------------------------------
176 // XElementAccess
177 uno::Type SAL_CALL OGroups::getElementType(  ) throw (uno::RuntimeException)
178 {
179     return ::getCppuType(static_cast< uno::Reference<report::XGroup>*>(NULL));
180 }
181 // -----------------------------------------------------------------------------
182 ::sal_Bool SAL_CALL OGroups::hasElements(  ) throw (uno::RuntimeException)
183 {
184     ::osl::MutexGuard aGuard(m_aMutex);
185     return !m_aGroups.empty();
186 }
187 // -----------------------------------------------------------------------------
188 // XChild
189 uno::Reference< uno::XInterface > SAL_CALL OGroups::getParent(  ) throw (uno::RuntimeException)
190 {
191     return m_xParent;
192 }
193 // -----------------------------------------------------------------------------
194 void SAL_CALL OGroups::setParent( const uno::Reference< uno::XInterface >& /*Parent*/ ) throw (lang::NoSupportException, uno::RuntimeException)
195 {
196     throw lang::NoSupportException();
197 }
198 // -----------------------------------------------------------------------------
199 // XContainer
200 void SAL_CALL OGroups::addContainerListener( const uno::Reference< container::XContainerListener >& xListener ) throw (uno::RuntimeException)
201 {
202     m_aContainerListeners.addInterface(xListener);
203 }
204 // -----------------------------------------------------------------------------
205 void SAL_CALL OGroups::removeContainerListener( const uno::Reference< container::XContainerListener >& xListener ) throw (uno::RuntimeException)
206 {
207     m_aContainerListeners.removeInterface(xListener);
208 }
209 // -----------------------------------------------------------------------------
210 void OGroups::checkIndex(sal_Int32 _nIndex)
211 {
212     if ( _nIndex < 0 || static_cast<sal_Int32>(m_aGroups.size()) <= _nIndex )
213         throw lang::IndexOutOfBoundsException();
214 }
215 // =============================================================================
216 }
217 // =============================================================================
218