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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "gridcolumnproptranslator.hxx"
31 
32 /** === begin UNO includes === **/
33 #include <com/sun/star/beans/XPropertySetInfo.hpp>
34 #include <com/sun/star/awt/TextAlign.hpp>
35 #include <com/sun/star/style/ParagraphAdjust.hpp>
36 /** === end UNO includes === **/
37 #include <osl/diagnose.h>
38 #include <cppuhelper/implbase1.hxx>
39 
40 #include <algorithm>
41 
42 //........................................................................
43 namespace xmloff
44 {
45 //........................................................................
46 
47     using namespace ::com::sun::star::uno;
48     using namespace ::com::sun::star::awt;
49     using namespace ::com::sun::star::lang;
50     using namespace ::com::sun::star::beans;
51     using namespace ::com::sun::star::style;
52 
53     namespace
54     {
55 	    //----------------------------------------------------------------
56         ::rtl::OUString getParaAlignProperty()
57         {
58             return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ParaAdjust" ) );
59         }
60 
61 	    //----------------------------------------------------------------
62         ::rtl::OUString getAlignProperty()
63         {
64             return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Align" ) );
65         }
66 
67 	    //----------------------------------------------------------------
68         sal_Int32 findStringElement( const Sequence< ::rtl::OUString >& _rNames, const ::rtl::OUString& _rName )
69         {
70             const ::rtl::OUString* pStart = _rNames.getConstArray();
71             const ::rtl::OUString* pEnd = _rNames.getConstArray() + _rNames.getLength();
72             const ::rtl::OUString* pPos = ::std::find( pStart, pEnd, _rName );
73             if ( pPos != pEnd )
74                 return pPos - pStart;
75             return -1;
76         }
77 
78 	    //----------------------------------------------------------------
79         struct AlignmentTranslationEntry
80         {
81             ParagraphAdjust nParagraphValue;
82             sal_Int16       nControlValue;
83         }
84         AlignmentTranslations[] =
85         {
86             // note that order matters:
87             // valueAlignToParaAdjust and valueParaAdjustToAlign search this map from the _beginning_
88             // and use the first matching entry
89             { ParagraphAdjust_LEFT,             TextAlign::LEFT     },
90             { ParagraphAdjust_CENTER,           TextAlign::CENTER   },
91             { ParagraphAdjust_RIGHT,            TextAlign::RIGHT    },
92             { ParagraphAdjust_BLOCK,            TextAlign::RIGHT    },
93             { ParagraphAdjust_STRETCH,          TextAlign::LEFT     },
94             { ParagraphAdjust_MAKE_FIXED_SIZE,  TextAlign::LEFT     },
95             { ParagraphAdjust_MAKE_FIXED_SIZE,  -1 }
96         };
97 
98 	    //----------------------------------------------------------------
99         void valueAlignToParaAdjust(Any& rValue)
100         {
101             sal_Int16 nValue = 0;
102             rValue >>= nValue;
103             const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
104             while (-1 != pTranslation->nControlValue)
105             {
106                 if ( nValue == pTranslation->nControlValue )
107                 {
108                     rValue <<= pTranslation->nParagraphValue;
109                     return;
110                 }
111                 ++pTranslation;
112             }
113             OSL_ENSURE( sal_False, "valueAlignToParaAdjust: unreachable!" );
114         }
115 
116 	    //----------------------------------------------------------------
117         void valueParaAdjustToAlign(Any& rValue)
118         {
119             sal_Int32 nValue = 0;
120             rValue >>= nValue;
121             const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
122             while ( ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
123             {
124                 if ( nValue == pTranslation->nParagraphValue)
125                 {
126                     rValue <<= pTranslation->nControlValue;
127                     return;
128                 }
129                 ++pTranslation;
130             }
131             OSL_ENSURE( sal_False, "valueParaAdjustToAlign: unreachable!" );
132         }
133 
134         //====================================================================
135 	    //= OMergedPropertySetInfo
136 	    //====================================================================
137         typedef ::cppu::WeakAggImplHelper1  <   XPropertySetInfo
138                                             >   OMergedPropertySetInfo_Base;
139         class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
140         {
141         private:
142             Reference< XPropertySetInfo >   m_xMasterInfo;
143 
144         public:
145             OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
146 
147         protected:
148             virtual ~OMergedPropertySetInfo();
149 
150             // XPropertySetInfo
151             virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > SAL_CALL getProperties(  ) throw (::com::sun::star::uno::RuntimeException);
152             virtual ::com::sun::star::beans::Property SAL_CALL getPropertyByName( const ::rtl::OUString& aName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
153             virtual ::sal_Bool SAL_CALL hasPropertyByName( const ::rtl::OUString& Name ) throw (::com::sun::star::uno::RuntimeException);
154         };
155 
156         //----------------------------------------------------------------
157         OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
158             :m_xMasterInfo( _rxMasterInfo )
159         {
160             OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
161         }
162 
163         //----------------------------------------------------------------
164         OMergedPropertySetInfo::~OMergedPropertySetInfo()
165         {
166         }
167 
168         //----------------------------------------------------------------
169         Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties(  ) throw (RuntimeException)
170         {
171             // add a "ParaAdjust" property to the master properties
172             Sequence< Property > aProperties;
173             if ( m_xMasterInfo.is() )
174                 aProperties = m_xMasterInfo->getProperties();
175 
176             sal_Int32 nOldLength = aProperties.getLength();
177             aProperties.realloc( nOldLength + 1 );
178             aProperties[ nOldLength ] = getPropertyByName( getParaAlignProperty() );
179 
180             return aProperties;
181         }
182 
183         //----------------------------------------------------------------
184         Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const ::rtl::OUString& aName ) throw (UnknownPropertyException, RuntimeException)
185         {
186             if ( aName == getParaAlignProperty() )
187                 return Property( getParaAlignProperty(), -1,
188                     ::getCppuType( static_cast< const ParagraphAdjust* >( NULL ) ), 0 );
189 
190             if ( !m_xMasterInfo.is() )
191                 return Property();
192 
193             return m_xMasterInfo->getPropertyByName( aName );
194         }
195 
196         //----------------------------------------------------------------
197         ::sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const ::rtl::OUString& Name ) throw (RuntimeException)
198         {
199             if ( Name == getParaAlignProperty() )
200                 return sal_True;
201 
202             if ( !m_xMasterInfo.is() )
203                 return sal_False;
204 
205             return m_xMasterInfo->hasPropertyByName( Name );
206         }
207     }
208 
209 
210 	//====================================================================
211 	//= OGridColumnPropertyTranslator
212 	//====================================================================
213 	//--------------------------------------------------------------------
214     OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
215         :m_xGridColumn( _rxGridColumn )
216     {
217         OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
218     }
219 
220 	//--------------------------------------------------------------------
221     OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
222     {
223     }
224 
225     //--------------------------------------------------------------------
226     Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo(  ) throw (RuntimeException)
227     {
228         Reference< XPropertySetInfo > xColumnPropInfo;
229         if ( m_xGridColumn.is() )
230             xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
231         return new OMergedPropertySetInfo( xColumnPropInfo );
232     }
233 
234     //--------------------------------------------------------------------
235     void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& aValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
236     {
237         // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
238         // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
239 
240         if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
241             throw UnknownPropertyException( _rPropertyName, *this );
242 
243         Sequence< ::rtl::OUString > aNames( &_rPropertyName, 1 );
244         Sequence< Any >             aValues( &aValue, 1 );
245         setPropertyValues( aNames, aValues );
246     }
247 
248     //--------------------------------------------------------------------
249     Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const ::rtl::OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
250     {
251         Sequence< ::rtl::OUString > aNames( &PropertyName, 1 );
252         Sequence< Any > aValues = getPropertyValues( aNames );
253         OSL_ENSURE( aValues.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
254         if ( aValues.getLength() == 1 )
255             return aValues[0];
256         return Any();
257     }
258 
259     //--------------------------------------------------------------------
260     void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
261     {
262         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
263     }
264 
265     //--------------------------------------------------------------------
266     void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
267     {
268         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
269     }
270 
271     //--------------------------------------------------------------------
272     void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const ::rtl::OUString&, const Reference< XVetoableChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
273     {
274         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
275     }
276 
277     //--------------------------------------------------------------------
278     void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const ::rtl::OUString&, const Reference< XVetoableChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
279     {
280         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
281     }
282 
283     //--------------------------------------------------------------------
284     void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< ::rtl::OUString >& aPropertyNames, const Sequence< Any >& aValues ) throw (PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
285     {
286         if ( !m_xGridColumn.is() )
287             return;
288 
289         // if there's ever the need for more than one property being translated, then we should
290         // certainly have a more clever implementation than this ...
291 
292         Sequence< ::rtl::OUString > aTranslatedNames( aPropertyNames );
293         Sequence< Any >             aTranslatedValues( aValues );
294 
295         sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
296         if ( nParaAlignPos != -1 )
297         {
298             aTranslatedNames[ nParaAlignPos ] = getAlignProperty();
299             valueParaAdjustToAlign( aTranslatedValues[ nParaAlignPos ] );
300         }
301 
302         m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
303     }
304 
305     //--------------------------------------------------------------------
306     Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< ::rtl::OUString >& aPropertyNames ) throw (RuntimeException)
307     {
308         Sequence< Any > aValues( aPropertyNames.getLength() );
309         if ( !m_xGridColumn.is() )
310             return aValues;
311 
312         Sequence< ::rtl::OUString > aTranslatedNames( aPropertyNames );
313         sal_Int32 nAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
314         if ( nAlignPos != -1 )
315             aTranslatedNames[ nAlignPos ] = getAlignProperty();
316 
317         aValues = m_xGridColumn->getPropertyValues( aPropertyNames );
318         if ( nAlignPos != -1 )
319             valueAlignToParaAdjust( aValues[ nAlignPos ] );
320 
321         return aValues;
322     }
323 
324     //--------------------------------------------------------------------
325     void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< ::rtl::OUString >&, const Reference< XPropertiesChangeListener >& ) throw (RuntimeException)
326     {
327         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
328     }
329 
330     //--------------------------------------------------------------------
331     void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& ) throw (RuntimeException)
332     {
333         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
334     }
335 
336     //--------------------------------------------------------------------
337     void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< ::rtl::OUString >&, const Reference< XPropertiesChangeListener >& ) throw (RuntimeException)
338     {
339         OSL_ENSURE( sal_False, "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
340     }
341 
342 //........................................................................
343 } // namespace xmloff
344 //........................................................................
345 
346