1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <uielement/itemcontainer.hxx>
21 #include <uielement/constitemcontainer.hxx>
22 #include <comphelper/servicehelper.hxx>
25 using namespace com::sun::star::uno
;
26 using namespace com::sun::star::lang
;
27 using namespace com::sun::star::beans
;
28 using namespace com::sun::star::container
;
30 const char WRONG_TYPE_EXCEPTION
[] = "Type must be com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >";
35 // XInterface, XTypeProvider
37 ItemContainer::ItemContainer( const ShareableMutex
& rMutex
) :
38 m_aShareMutex( rMutex
)
42 ItemContainer::ItemContainer( const ConstItemContainer
& rConstItemContainer
, const ShareableMutex
& rMutex
) : m_aShareMutex( rMutex
)
44 copyItemContainer( rConstItemContainer
.m_aItemVector
, rMutex
);
47 ItemContainer::ItemContainer( const Reference
< XIndexAccess
>& rSourceContainer
, const ShareableMutex
& rMutex
) :
48 m_aShareMutex( rMutex
)
50 if ( rSourceContainer
.is() )
52 sal_Int32 nCount
= rSourceContainer
->getCount();
55 for ( sal_Int32 i
= 0; i
< nCount
; i
++ )
57 Sequence
< PropertyValue
> aPropSeq
;
58 if ( rSourceContainer
->getByIndex( i
) >>= aPropSeq
)
60 sal_Int32 nContainerIndex
= -1;
61 Reference
< XIndexAccess
> xIndexAccess
;
62 for ( sal_Int32 j
= 0; j
< aPropSeq
.getLength(); j
++ )
64 if ( aPropSeq
[j
].Name
== "ItemDescriptorContainer" )
66 aPropSeq
[j
].Value
>>= xIndexAccess
;
72 if ( xIndexAccess
.is() && nContainerIndex
>= 0 )
73 aPropSeq
[nContainerIndex
].Value
<<= deepCopyContainer( xIndexAccess
, rMutex
);
75 m_aItemVector
.push_back( aPropSeq
);
79 catch ( const IndexOutOfBoundsException
& )
85 ItemContainer::~ItemContainer()
90 void ItemContainer::copyItemContainer( const std::vector
< Sequence
< PropertyValue
> >& rSourceVector
, const ShareableMutex
& rMutex
)
92 const sal_uInt32 nCount
= rSourceVector
.size();
93 for ( sal_uInt32 i
= 0; i
< nCount
; ++i
)
95 sal_Int32 nContainerIndex
= -1;
96 Sequence
< PropertyValue
> aPropSeq( rSourceVector
[i
] );
97 Reference
< XIndexAccess
> xIndexAccess
;
98 for ( sal_Int32 j
= 0; j
< aPropSeq
.getLength(); j
++ )
100 if ( aPropSeq
[j
].Name
== "ItemDescriptorContainer" )
102 aPropSeq
[j
].Value
>>= xIndexAccess
;
108 if ( xIndexAccess
.is() && nContainerIndex
>= 0 )
109 aPropSeq
[nContainerIndex
].Value
<<= deepCopyContainer( xIndexAccess
, rMutex
);
111 m_aItemVector
.push_back( aPropSeq
);
115 Reference
< XIndexAccess
> ItemContainer::deepCopyContainer( const Reference
< XIndexAccess
>& rSubContainer
, const ShareableMutex
& rMutex
)
117 Reference
< XIndexAccess
> xReturn
;
118 if ( rSubContainer
.is() )
120 ConstItemContainer
* pSource
= ConstItemContainer::GetImplementation( rSubContainer
);
121 ItemContainer
* pSubContainer( 0 );
123 pSubContainer
= new ItemContainer( *pSource
, rMutex
);
125 pSubContainer
= new ItemContainer( rSubContainer
, rMutex
);
126 xReturn
= Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( pSubContainer
), UNO_QUERY
);
134 class theItemContainerUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theItemContainerUnoTunnelId
> {};
137 const Sequence
< sal_Int8
>& ItemContainer::GetUnoTunnelId() throw()
139 return theItemContainerUnoTunnelId::get().getSeq();
142 ItemContainer
* ItemContainer::GetImplementation( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& rxIFace
) throw()
144 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XUnoTunnel
> xUT( rxIFace
, ::com::sun::star::uno::UNO_QUERY
);
145 return xUT
.is() ? reinterpret_cast< ItemContainer
* >(sal::static_int_cast
< sal_IntPtr
>(
146 xUT
->getSomething( ItemContainer::GetUnoTunnelId() ))) : NULL
;
150 sal_Bool SAL_CALL
ItemContainer::hasElements()
151 throw ( RuntimeException
, std::exception
)
153 ShareGuard
aLock( m_aShareMutex
);
154 return ( !m_aItemVector
.empty() );
158 sal_Int32 SAL_CALL
ItemContainer::getCount()
159 throw ( RuntimeException
, std::exception
)
161 ShareGuard
aLock( m_aShareMutex
);
162 return m_aItemVector
.size();
165 Any SAL_CALL
ItemContainer::getByIndex( sal_Int32 Index
)
166 throw ( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
168 ShareGuard
aLock( m_aShareMutex
);
169 if ( sal_Int32( m_aItemVector
.size()) > Index
)
170 return makeAny( m_aItemVector
[Index
] );
172 throw IndexOutOfBoundsException( OUString(), (OWeakObject
*)this );
176 void SAL_CALL
ItemContainer::insertByIndex( sal_Int32 Index
, const Any
& aItem
)
177 throw ( IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
179 Sequence
< PropertyValue
> aSeq
;
180 if ( aItem
>>= aSeq
)
182 ShareGuard
aLock( m_aShareMutex
);
183 if ( sal_Int32( m_aItemVector
.size()) == Index
)
184 m_aItemVector
.push_back( aSeq
);
185 else if ( sal_Int32( m_aItemVector
.size()) >Index
)
187 std::vector
< Sequence
< PropertyValue
> >::iterator aIter
= m_aItemVector
.begin();
189 m_aItemVector
.insert( aIter
, aSeq
);
192 throw IndexOutOfBoundsException( OUString(), (OWeakObject
*)this );
195 throw IllegalArgumentException( OUString( WRONG_TYPE_EXCEPTION
),
196 (OWeakObject
*)this, 2 );
199 void SAL_CALL
ItemContainer::removeByIndex( sal_Int32 nIndex
)
200 throw ( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
202 ShareGuard
aLock( m_aShareMutex
);
203 if ( (sal_Int32
)m_aItemVector
.size() > nIndex
)
205 m_aItemVector
.erase(m_aItemVector
.begin() + nIndex
);
208 throw IndexOutOfBoundsException( OUString(), (OWeakObject
*)this );
211 void SAL_CALL
ItemContainer::replaceByIndex( sal_Int32 Index
, const Any
& aItem
)
212 throw ( IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
214 Sequence
< PropertyValue
> aSeq
;
215 if ( aItem
>>= aSeq
)
217 ShareGuard
aLock( m_aShareMutex
);
218 if ( sal_Int32( m_aItemVector
.size()) > Index
)
219 m_aItemVector
[Index
] = aSeq
;
221 throw IndexOutOfBoundsException( OUString(), (OWeakObject
*)this );
224 throw IllegalArgumentException( OUString( WRONG_TYPE_EXCEPTION
),
225 (OWeakObject
*)this, 2 );
228 } // namespace framework
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */