1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "ItemConverter.hxx"
27cdf0e10cSrcweir #include "macros.hxx"
28cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
29cdf0e10cSrcweir #include <svl/itemprop.hxx>
30cdf0e10cSrcweir #include <svl/itemiter.hxx>
31cdf0e10cSrcweir // header for class SfxWhichIter
32cdf0e10cSrcweir #include <svl/whiter.hxx>
33cdf0e10cSrcweir #include <svx/svxids.hrc>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace comphelper
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 
ItemConverter(const uno::Reference<beans::XPropertySet> & rPropertySet,SfxItemPool & rItemPool)40cdf0e10cSrcweir ItemConverter::ItemConverter(
41cdf0e10cSrcweir     const uno::Reference< beans::XPropertySet > & rPropertySet,
42cdf0e10cSrcweir     SfxItemPool& rItemPool ) :
43cdf0e10cSrcweir         m_xPropertySet( rPropertySet ),
44cdf0e10cSrcweir         m_xPropertySetInfo( NULL ),
45cdf0e10cSrcweir         m_rItemPool( rItemPool ),
46cdf0e10cSrcweir         m_bIsValid( true )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     resetPropertySet( m_xPropertySet );
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
~ItemConverter()51cdf0e10cSrcweir ItemConverter::~ItemConverter()
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     stopAllComponentListening();
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
resetPropertySet(const uno::Reference<beans::XPropertySet> & xPropSet)56cdf0e10cSrcweir void ItemConverter::resetPropertySet(
57cdf0e10cSrcweir     const uno::Reference< beans::XPropertySet > & xPropSet )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     if( xPropSet.is())
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         stopAllComponentListening();
62cdf0e10cSrcweir         m_xPropertySet = xPropSet;
63cdf0e10cSrcweir         m_xPropertySetInfo = m_xPropertySet->getPropertySetInfo();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         uno::Reference< lang::XComponent > xComp( m_xPropertySet, uno::UNO_QUERY );
66cdf0e10cSrcweir         if( xComp.is())
67cdf0e10cSrcweir         {
68cdf0e10cSrcweir             // method of base class ::utl::OEventListenerAdapter
69cdf0e10cSrcweir             startComponentListening( xComp );
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
GetItemPool() const74cdf0e10cSrcweir SfxItemPool & ItemConverter::GetItemPool() const
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     return m_rItemPool;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
CreateEmptyItemSet() const79cdf0e10cSrcweir SfxItemSet ItemConverter::CreateEmptyItemSet() const
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     return SfxItemSet( GetItemPool(), GetWhichPairs() );
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
GetPropertySet() const84cdf0e10cSrcweir uno::Reference< beans::XPropertySet > ItemConverter::GetPropertySet() const
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     return m_xPropertySet;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
_disposing(const lang::EventObject & rSource)89cdf0e10cSrcweir void ItemConverter::_disposing( const lang::EventObject& rSource )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     if( rSource.Source == m_xPropertySet )
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         m_bIsValid = false;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
FillItemSet(SfxItemSet & rOutItemSet) const97cdf0e10cSrcweir void ItemConverter::FillItemSet( SfxItemSet & rOutItemSet ) const
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     const sal_uInt16 * pRanges = rOutItemSet.GetRanges();
100cdf0e10cSrcweir     tPropertyNameWithMemberId aProperty;
101cdf0e10cSrcweir     SfxItemPool & rPool = GetItemPool();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     OSL_ASSERT( pRanges != NULL );
104cdf0e10cSrcweir     OSL_ASSERT( m_xPropertySetInfo.is());
105cdf0e10cSrcweir     OSL_ASSERT( m_xPropertySet.is());
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     while( (*pRanges) != 0)
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         sal_uInt16 nBeg = (*pRanges);
110cdf0e10cSrcweir         ++pRanges;
111cdf0e10cSrcweir         sal_uInt16 nEnd = (*pRanges);
112cdf0e10cSrcweir         ++pRanges;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         OSL_ASSERT( nBeg <= nEnd );
115cdf0e10cSrcweir         for( sal_uInt16 nWhich = nBeg; nWhich <= nEnd; ++nWhich )
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             if( GetItemProperty( nWhich, aProperty ))
118cdf0e10cSrcweir             {
119cdf0e10cSrcweir                 // put the Property into the itemset
120cdf0e10cSrcweir                 SfxPoolItem * pItem = rPool.GetDefaultItem( nWhich ).Clone();
121cdf0e10cSrcweir 
122cdf0e10cSrcweir                 if( pItem )
123cdf0e10cSrcweir                 {
124cdf0e10cSrcweir                     try
125cdf0e10cSrcweir                     {
126cdf0e10cSrcweir                         if( ! pItem->PutValue( m_xPropertySet->getPropertyValue( aProperty.first ),
127cdf0e10cSrcweir                                                aProperty.second // nMemberId
128cdf0e10cSrcweir                                 ))
129cdf0e10cSrcweir                         {
130cdf0e10cSrcweir                             delete pItem;
131cdf0e10cSrcweir                         }
132cdf0e10cSrcweir                         else
133cdf0e10cSrcweir                         {
134cdf0e10cSrcweir                             rOutItemSet.Put( *pItem, nWhich );
135cdf0e10cSrcweir                             delete pItem;
136cdf0e10cSrcweir                         }
137cdf0e10cSrcweir                     }
138cdf0e10cSrcweir                     catch( beans::UnknownPropertyException ex )
139cdf0e10cSrcweir                     {
140cdf0e10cSrcweir                         delete pItem;
141cdf0e10cSrcweir                         OSL_ENSURE( false,
142cdf0e10cSrcweir                                     ::rtl::OUStringToOString(
143cdf0e10cSrcweir                                         ex.Message +
144cdf0e10cSrcweir                                         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
145cdf0e10cSrcweir                                                              " - unknown Property: " )) + aProperty.first,
146cdf0e10cSrcweir                                         RTL_TEXTENCODING_ASCII_US ).getStr());
147cdf0e10cSrcweir                     }
148cdf0e10cSrcweir                     catch( uno::Exception ex )
149cdf0e10cSrcweir                     {
150cdf0e10cSrcweir                         ASSERT_EXCEPTION( ex );
151cdf0e10cSrcweir                     }
152cdf0e10cSrcweir                 }
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir             else
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir                 try
157cdf0e10cSrcweir                 {
158cdf0e10cSrcweir                     FillSpecialItem( nWhich, rOutItemSet );
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir                 catch( uno::Exception ex )
161cdf0e10cSrcweir                 {
162cdf0e10cSrcweir                     ASSERT_EXCEPTION( ex );
163cdf0e10cSrcweir                 }
164cdf0e10cSrcweir             }
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
FillSpecialItem(sal_uInt16,SfxItemSet &) const169cdf0e10cSrcweir void ItemConverter::FillSpecialItem(
170cdf0e10cSrcweir     sal_uInt16 /*nWhichId*/, SfxItemSet & /*rOutItemSet*/ ) const
171cdf0e10cSrcweir     throw( uno::Exception )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" );
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
ApplySpecialItem(sal_uInt16,const SfxItemSet &)176cdf0e10cSrcweir bool ItemConverter::ApplySpecialItem(
177cdf0e10cSrcweir     sal_uInt16 /*nWhichId*/, const SfxItemSet & /*rItemSet*/ )
178cdf0e10cSrcweir     throw( uno::Exception )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     OSL_ENSURE( false, "ItemConverter: Unhandled special item found!" );
181cdf0e10cSrcweir     return false;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
ApplyItemSet(const SfxItemSet & rItemSet)184cdf0e10cSrcweir bool ItemConverter::ApplyItemSet( const SfxItemSet & rItemSet )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir     OSL_ASSERT( m_xPropertySet.is());
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     bool bItemsChanged = false;
189cdf0e10cSrcweir     SfxItemIter aIter( rItemSet );
190cdf0e10cSrcweir     const SfxPoolItem * pItem = aIter.FirstItem();
191cdf0e10cSrcweir     tPropertyNameWithMemberId aProperty;
192cdf0e10cSrcweir     uno::Any aValue;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     while( pItem )
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         if( rItemSet.GetItemState( pItem->Which(), sal_False ) == SFX_ITEM_SET )
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             if( GetItemProperty( pItem->Which(), aProperty ))
199cdf0e10cSrcweir             {
200cdf0e10cSrcweir                 pItem->QueryValue( aValue, aProperty.second /* nMemberId */ );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                 try
203cdf0e10cSrcweir                 {
204cdf0e10cSrcweir                     if( aValue != m_xPropertySet->getPropertyValue( aProperty.first ))
205cdf0e10cSrcweir                     {
206cdf0e10cSrcweir                         m_xPropertySet->setPropertyValue( aProperty.first, aValue );
207cdf0e10cSrcweir                         bItemsChanged = true;
208cdf0e10cSrcweir                     }
209cdf0e10cSrcweir                 }
210cdf0e10cSrcweir                 catch( beans::UnknownPropertyException ex )
211cdf0e10cSrcweir                 {
212cdf0e10cSrcweir                     OSL_ENSURE( false,
213cdf0e10cSrcweir                                 ::rtl::OUStringToOString(
214cdf0e10cSrcweir                                     ex.Message +
215cdf0e10cSrcweir                                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
216cdf0e10cSrcweir                                                          " - unknown Property: " )) + aProperty.first,
217cdf0e10cSrcweir                                     RTL_TEXTENCODING_ASCII_US).getStr());
218cdf0e10cSrcweir                 }
219cdf0e10cSrcweir                 catch( uno::Exception ex )
220cdf0e10cSrcweir                 {
221cdf0e10cSrcweir                     OSL_ENSURE( false, ::rtl::OUStringToOString(
222cdf0e10cSrcweir                                     ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
223cdf0e10cSrcweir                 }
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir             else
226cdf0e10cSrcweir             {
227cdf0e10cSrcweir                 bItemsChanged = ApplySpecialItem( pItem->Which(), rItemSet ) || bItemsChanged;
228cdf0e10cSrcweir             }
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         pItem = aIter.NextItem();
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     return bItemsChanged;
234cdf0e10cSrcweir }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir // --------------------------------------------------------------------------------
237cdf0e10cSrcweir 
InvalidateUnequalItems(SfxItemSet & rDestSet,const SfxItemSet & rSourceSet)238cdf0e10cSrcweir void ItemConverter::InvalidateUnequalItems( SfxItemSet  &rDestSet, const SfxItemSet &rSourceSet )
239cdf0e10cSrcweir {
240cdf0e10cSrcweir 	SfxWhichIter      aIter (rSourceSet);
241cdf0e10cSrcweir 	sal_uInt16            nWhich     = aIter.FirstWhich ();
242cdf0e10cSrcweir 	const SfxPoolItem *pPoolItem = NULL;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	while (nWhich)
245cdf0e10cSrcweir 	{
246cdf0e10cSrcweir 		if ((rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET) &&
247cdf0e10cSrcweir 			(rDestSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_SET))
248cdf0e10cSrcweir         {
249cdf0e10cSrcweir 			if (rSourceSet.Get(nWhich) != rDestSet.Get(nWhich))
250cdf0e10cSrcweir             {
251cdf0e10cSrcweir                 if( SID_CHAR_DLG_PREVIEW_STRING != nWhich )
252cdf0e10cSrcweir                 {
253cdf0e10cSrcweir                     rDestSet.InvalidateItem(nWhich);
254cdf0e10cSrcweir                 }
255cdf0e10cSrcweir             }
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir         else if( rSourceSet.GetItemState(nWhich, sal_True, &pPoolItem) == SFX_ITEM_DONTCARE )
258cdf0e10cSrcweir             rDestSet.InvalidateItem(nWhich);
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 		nWhich = aIter.NextWhich ();
261cdf0e10cSrcweir 	}
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir } //  namespace comphelper
265