merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / misc / propertystorage.cxx
blob6c2a485809084655d149eb7b72c8a7eab33634d0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertystorage.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "propertystorage.hxx"
36 /** === begin UNO includes === **/
37 /** === end UNO includes === **/
39 #include <svtools/itemset.hxx>
40 #include <svtools/stritem.hxx>
41 #include <svtools/eitem.hxx>
43 #include <memory>
45 //........................................................................
46 namespace dbaui
48 //........................................................................
50 /** === begin UNO using === **/
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::XInterface;
53 using ::com::sun::star::uno::UNO_QUERY;
54 using ::com::sun::star::uno::Exception;
55 using ::com::sun::star::uno::RuntimeException;
56 using ::com::sun::star::uno::Any;
57 using ::com::sun::star::uno::makeAny;
58 /** === end UNO using === **/
60 //====================================================================
61 //= PropertyStorage
62 //====================================================================
63 //--------------------------------------------------------------------
64 PropertyStorage::~PropertyStorage()
68 //====================================================================
69 //= helper
70 //====================================================================
71 namespace
73 //----------------------------------------------------------------
74 template < class ITEMTYPE, class UNOTYPE >
75 class ItemAdapter
77 public:
78 static bool trySet( SfxItemSet& _rSet, ItemId _nItemId, const Any& _rValue )
80 const SfxPoolItem& rItem( _rSet.Get( _nItemId ) );
81 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &rItem );
82 if ( !pTypedItem )
83 return false;
85 UNOTYPE aValue( pTypedItem->GetValue() );
86 OSL_VERIFY( _rValue >>= aValue );
87 // TODO: one could throw an IllegalArgumentException here - finally, this method
88 // is (to be) used from within an XPropertySet::setPropertyValue implementation,
89 // where this would be the appropriate reaction on wrong value types
90 ::std::auto_ptr< ITEMTYPE > pClone( dynamic_cast< ITEMTYPE* >( pTypedItem->Clone() ) );
91 pClone->SetValue( aValue );
92 _rSet.Put( *pClone );
93 return true;
96 static bool tryGet( const SfxPoolItem& _rItem, Any& _out_rValue )
98 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &_rItem );
99 if ( !pTypedItem )
100 return false;
102 _out_rValue <<= UNOTYPE( pTypedItem->GetValue() );
103 return true;
108 //====================================================================
109 //= SetItemPropertyStorage
110 //====================================================================
111 //--------------------------------------------------------------------
112 void SetItemPropertyStorage::getPropertyValue( Any& _out_rValue ) const
114 const SfxPoolItem& rItem( m_rItemSet.Get( m_nItemID ) );
116 // try some known item types
117 if ( ItemAdapter< SfxBoolItem, sal_Bool >::tryGet( rItem, _out_rValue )
118 || ItemAdapter< SfxStringItem, ::rtl::OUString >::tryGet( rItem, _out_rValue )
120 return;
122 OSL_ENSURE( false, "SetItemPropertyStorage::getPropertyValue: unsupported item type!" );
125 //--------------------------------------------------------------------
126 void SetItemPropertyStorage::setPropertyValue( const Any& _rValue )
128 // try some known item types
129 if ( ItemAdapter< SfxBoolItem, sal_Bool >::trySet( m_rItemSet, m_nItemID, _rValue )
130 || ItemAdapter< SfxStringItem, ::rtl::OUString >::trySet( m_rItemSet, m_nItemID, _rValue )
132 return;
134 OSL_ENSURE( false, "SetItemPropertyStorage::setPropertyValue: unsupported item type!" );
137 //........................................................................
138 } // namespace dbaui
139 //........................................................................