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