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