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