Update git submodules
[LibreOffice.git] / framework / source / fwi / uielement / rootitemcontainer.cxx
blob6eb7fd900c15651c5bb0631b51f5e500496260d4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
21 #include <comphelper/servicehelper.hxx>
22 #include <comphelper/sequence.hxx>
23 #include <uielement/rootitemcontainer.hxx>
24 #include <uielement/itemcontainer.hxx>
25 #include <uielement/constitemcontainer.hxx>
26 #include <properties.h>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <rtl/ref.hxx>
31 using namespace cppu;
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::beans;
35 using namespace com::sun::star::container;
37 constexpr OUString WRONG_TYPE_EXCEPTION
38 = u"Type must be css::uno::Sequence< css::beans::PropertyValue >"_ustr;
40 const int PROPHANDLE_UINAME = 1;
41 constexpr OUString PROPNAME_UINAME = u"UIName"_ustr;
43 namespace framework
46 RootItemContainer::RootItemContainer()
47 : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
48 , ::cppu::OPropertySetHelper ( *static_cast< ::cppu::OBroadcastHelper* >(this) )
52 RootItemContainer::RootItemContainer( const Reference< XIndexAccess >& rSourceContainer )
53 : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
54 , ::cppu::OPropertySetHelper ( *static_cast< ::cppu::OBroadcastHelper* >(this) )
56 // We also have to copy the UIName property
57 try
59 Reference< XPropertySet > xPropSet( rSourceContainer, UNO_QUERY );
60 if ( xPropSet.is() )
62 xPropSet->getPropertyValue(u"UIName"_ustr) >>= m_aUIName;
65 catch ( const Exception& )
69 if ( !rSourceContainer.is() )
70 return;
72 sal_Int32 nCount = rSourceContainer->getCount();
73 try
75 for ( sal_Int32 i = 0; i < nCount; i++ )
77 Sequence< PropertyValue > aPropSeq;
78 if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
80 sal_Int32 nContainerIndex = -1;
81 Reference< XIndexAccess > xIndexAccess;
82 for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
84 if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
86 aPropSeq[j].Value >>= xIndexAccess;
87 nContainerIndex = j;
88 break;
92 if ( xIndexAccess.is() && nContainerIndex >= 0 )
93 aPropSeq.getArray()[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess );
95 m_aItemVector.push_back( aPropSeq );
99 catch ( const IndexOutOfBoundsException& )
104 RootItemContainer::~RootItemContainer()
108 Any SAL_CALL RootItemContainer::queryInterface( const Type& _rType )
110 Any aRet = RootItemContainer_BASE::queryInterface( _rType );
111 if ( !aRet.hasValue() )
112 aRet = OPropertySetHelper::queryInterface( _rType );
113 return aRet;
116 Sequence< Type > SAL_CALL RootItemContainer::getTypes( )
118 return comphelper::concatSequences(
119 RootItemContainer_BASE::getTypes(),
120 ::cppu::OPropertySetHelper::getTypes()
124 Reference< XIndexAccess > RootItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer )
126 Reference< XIndexAccess > xReturn;
127 if ( rSubContainer.is() )
129 ConstItemContainer* pSource = dynamic_cast<ConstItemContainer*>( rSubContainer.get() );
130 rtl::Reference<ItemContainer> pSubContainer;
131 if ( pSource )
132 pSubContainer = new ItemContainer( *pSource, m_aShareMutex );
133 else
134 pSubContainer = new ItemContainer( rSubContainer, m_aShareMutex );
135 xReturn = pSubContainer;
138 return xReturn;
141 // XElementAccess
142 sal_Bool SAL_CALL RootItemContainer::hasElements()
144 ShareGuard aLock( m_aShareMutex );
145 return ( !m_aItemVector.empty() );
148 // XIndexAccess
149 sal_Int32 SAL_CALL RootItemContainer::getCount()
151 ShareGuard aLock( m_aShareMutex );
152 return m_aItemVector.size();
155 Any SAL_CALL RootItemContainer::getByIndex( sal_Int32 Index )
157 ShareGuard aLock( m_aShareMutex );
158 if ( sal_Int32( m_aItemVector.size()) <= Index )
159 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
161 return Any( m_aItemVector[Index] );
164 // XIndexContainer
165 void SAL_CALL RootItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
167 Sequence< PropertyValue > aSeq;
168 if ( !(aItem >>= aSeq) )
169 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, static_cast<OWeakObject *>(this), 2 );
171 ShareGuard aLock( m_aShareMutex );
172 if ( sal_Int32( m_aItemVector.size()) == Index )
173 m_aItemVector.push_back( aSeq );
174 else if ( sal_Int32( m_aItemVector.size()) >Index )
176 std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
177 aIter += Index;
178 m_aItemVector.insert( aIter, aSeq );
180 else
181 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
184 void SAL_CALL RootItemContainer::removeByIndex( sal_Int32 nIndex )
186 ShareGuard aLock( m_aShareMutex );
187 if ( static_cast<sal_Int32>(m_aItemVector.size()) <= nIndex )
188 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
190 m_aItemVector.erase(m_aItemVector.begin() + nIndex);
193 void SAL_CALL RootItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem )
195 Sequence< PropertyValue > aSeq;
196 if ( !(aItem >>= aSeq) )
197 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, static_cast<OWeakObject *>(this), 2 );
199 ShareGuard aLock( m_aShareMutex );
200 if ( sal_Int32( m_aItemVector.size()) <= Index )
201 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
203 m_aItemVector[Index] = std::move(aSeq);
206 Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithContext( const Reference< XComponentContext >& )
208 return static_cast<OWeakObject *>(new ItemContainer( m_aShareMutex ));
211 Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithArgumentsAndContext( const Sequence< Any >&, const Reference< XComponentContext >& )
213 return static_cast<OWeakObject *>(new ItemContainer( m_aShareMutex ));
216 // XPropertySet helper
217 sal_Bool SAL_CALL RootItemContainer::convertFastPropertyValue( Any& aConvertedValue ,
218 Any& aOldValue ,
219 sal_Int32 nHandle ,
220 const Any& aValue )
222 // Initialize state with sal_False !!!
223 // (Handle can be invalid)
224 bool bReturn = false;
226 switch( nHandle )
228 case PROPHANDLE_UINAME:
229 bReturn = PropHelper::willPropertyBeChanged(
230 css::uno::Any(m_aUIName),
231 aValue,
232 aOldValue,
233 aConvertedValue);
234 break;
237 // Return state of operation.
238 return bReturn;
241 void SAL_CALL RootItemContainer::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
242 const css::uno::Any& aValue )
244 switch( nHandle )
246 case PROPHANDLE_UINAME:
247 aValue >>= m_aUIName;
248 break;
252 void SAL_CALL RootItemContainer::getFastPropertyValue( css::uno::Any& aValue ,
253 sal_Int32 nHandle ) const
255 switch( nHandle )
257 case PROPHANDLE_UINAME:
258 aValue <<= m_aUIName;
259 break;
263 ::cppu::IPropertyArrayHelper& SAL_CALL RootItemContainer::getInfoHelper()
265 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
266 // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
267 // "true" say: Table is sorted by name.
268 static ::cppu::OPropertyArrayHelper ourInfoHelper( impl_getStaticPropertyDescriptor(), true );
270 return ourInfoHelper;
273 css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL RootItemContainer::getPropertySetInfo()
275 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
276 // (Use method "getInfoHelper()".)
277 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
279 return xInfo;
282 css::uno::Sequence< css::beans::Property > RootItemContainer::impl_getStaticPropertyDescriptor()
284 // Create a property array to initialize sequence!
285 // Table of all predefined properties of this class. It's used from OPropertySetHelper-class!
286 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
287 // It's necessary for methods of OPropertySetHelper.
288 // ATTENTION:
289 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
291 return
293 css::beans::Property( PROPNAME_UINAME, PROPHANDLE_UINAME ,
294 cppu::UnoType<OUString>::get(),
295 css::beans::PropertyAttribute::TRANSIENT )
299 } // namespace framework
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */