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 "gridcolumnproptranslator.hxx"
22 #include <com/sun/star/beans/XPropertySetInfo.hpp>
23 #include <com/sun/star/awt/TextAlign.hpp>
24 #include <com/sun/star/style/ParagraphAdjust.hpp>
25 #include <osl/diagnose.h>
26 #include <cppuhelper/implbase1.hxx>
30 //........................................................................
33 //........................................................................
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::awt
;
37 using namespace ::com::sun::star
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::style
;
44 //----------------------------------------------------------------
45 OUString
getParaAlignProperty()
47 return OUString( "ParaAdjust" );
50 //----------------------------------------------------------------
51 OUString
getAlignProperty()
53 return OUString( "Align" );
56 //----------------------------------------------------------------
57 sal_Int32
findStringElement( const Sequence
< OUString
>& _rNames
, const OUString
& _rName
)
59 const OUString
* pStart
= _rNames
.getConstArray();
60 const OUString
* pEnd
= _rNames
.getConstArray() + _rNames
.getLength();
61 const OUString
* pPos
= ::std::find( pStart
, pEnd
, _rName
);
67 //----------------------------------------------------------------
68 struct AlignmentTranslationEntry
70 ParagraphAdjust nParagraphValue
;
71 sal_Int16 nControlValue
;
73 AlignmentTranslations
[] =
75 // note that order matters:
76 // valueAlignToParaAdjust and valueParaAdjustToAlign search this map from the _beginning_
77 // and use the first matching entry
78 { ParagraphAdjust_LEFT
, awt::TextAlign::LEFT
},
79 { ParagraphAdjust_CENTER
, awt::TextAlign::CENTER
},
80 { ParagraphAdjust_RIGHT
, awt::TextAlign::RIGHT
},
81 { ParagraphAdjust_BLOCK
, awt::TextAlign::RIGHT
},
82 { ParagraphAdjust_STRETCH
, awt::TextAlign::LEFT
},
83 { ParagraphAdjust_MAKE_FIXED_SIZE
, awt::TextAlign::LEFT
},
84 { ParagraphAdjust_MAKE_FIXED_SIZE
, -1 }
87 //----------------------------------------------------------------
88 void valueAlignToParaAdjust(Any
& rValue
)
92 const AlignmentTranslationEntry
* pTranslation
= AlignmentTranslations
;
93 while (-1 != pTranslation
->nControlValue
)
95 if ( nValue
== pTranslation
->nControlValue
)
97 rValue
<<= pTranslation
->nParagraphValue
;
102 OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
105 //----------------------------------------------------------------
106 void valueParaAdjustToAlign(Any
& rValue
)
108 sal_Int32 nValue
= 0;
110 const AlignmentTranslationEntry
* pTranslation
= AlignmentTranslations
;
111 while ( ParagraphAdjust_MAKE_FIXED_SIZE
!= pTranslation
->nParagraphValue
)
113 if ( nValue
== pTranslation
->nParagraphValue
)
115 rValue
<<= pTranslation
->nControlValue
;
120 OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
123 //====================================================================
124 //= OMergedPropertySetInfo
125 //====================================================================
126 typedef ::cppu::WeakAggImplHelper1
< XPropertySetInfo
127 > OMergedPropertySetInfo_Base
;
128 class OMergedPropertySetInfo
: public OMergedPropertySetInfo_Base
131 Reference
< XPropertySetInfo
> m_xMasterInfo
;
134 OMergedPropertySetInfo( const Reference
< XPropertySetInfo
>& _rxMasterInfo
);
137 virtual ~OMergedPropertySetInfo();
140 virtual ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::Property
> SAL_CALL
getProperties( ) throw (::com::sun::star::uno::RuntimeException
);
141 virtual ::com::sun::star::beans::Property SAL_CALL
getPropertyByName( const OUString
& aName
) throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::uno::RuntimeException
);
142 virtual ::sal_Bool SAL_CALL
hasPropertyByName( const OUString
& Name
) throw (::com::sun::star::uno::RuntimeException
);
145 //----------------------------------------------------------------
146 OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference
< XPropertySetInfo
>& _rxMasterInfo
)
147 :m_xMasterInfo( _rxMasterInfo
)
149 OSL_ENSURE( m_xMasterInfo
.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
152 //----------------------------------------------------------------
153 OMergedPropertySetInfo::~OMergedPropertySetInfo()
157 //----------------------------------------------------------------
158 Sequence
< Property
> SAL_CALL
OMergedPropertySetInfo::getProperties( ) throw (RuntimeException
)
160 // add a "ParaAdjust" property to the master properties
161 Sequence
< Property
> aProperties
;
162 if ( m_xMasterInfo
.is() )
163 aProperties
= m_xMasterInfo
->getProperties();
165 sal_Int32 nOldLength
= aProperties
.getLength();
166 aProperties
.realloc( nOldLength
+ 1 );
167 aProperties
[ nOldLength
] = getPropertyByName( getParaAlignProperty() );
172 //----------------------------------------------------------------
173 Property SAL_CALL
OMergedPropertySetInfo::getPropertyByName( const OUString
& aName
) throw (UnknownPropertyException
, RuntimeException
)
175 if ( aName
== getParaAlignProperty() )
176 return Property( getParaAlignProperty(), -1,
177 ::getCppuType( static_cast< const ParagraphAdjust
* >( NULL
) ), 0 );
179 if ( !m_xMasterInfo
.is() )
182 return m_xMasterInfo
->getPropertyByName( aName
);
185 //----------------------------------------------------------------
186 ::sal_Bool SAL_CALL
OMergedPropertySetInfo::hasPropertyByName( const OUString
& Name
) throw (RuntimeException
)
188 if ( Name
== getParaAlignProperty() )
191 if ( !m_xMasterInfo
.is() )
194 return m_xMasterInfo
->hasPropertyByName( Name
);
199 //====================================================================
200 //= OGridColumnPropertyTranslator
201 //====================================================================
202 //--------------------------------------------------------------------
203 OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference
< XMultiPropertySet
>& _rxGridColumn
)
204 :m_xGridColumn( _rxGridColumn
)
206 OSL_ENSURE( m_xGridColumn
.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
209 //--------------------------------------------------------------------
210 OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
214 //--------------------------------------------------------------------
215 Reference
< XPropertySetInfo
> SAL_CALL
OGridColumnPropertyTranslator::getPropertySetInfo( ) throw (RuntimeException
)
217 Reference
< XPropertySetInfo
> xColumnPropInfo
;
218 if ( m_xGridColumn
.is() )
219 xColumnPropInfo
= m_xGridColumn
->getPropertySetInfo();
220 return new OMergedPropertySetInfo( xColumnPropInfo
);
223 //--------------------------------------------------------------------
224 void SAL_CALL
OGridColumnPropertyTranslator::setPropertyValue( const OUString
& _rPropertyName
, const Any
& aValue
) throw (UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
)
226 // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
227 // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
229 if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName
) )
230 throw UnknownPropertyException( _rPropertyName
, *this );
232 Sequence
< OUString
> aNames( &_rPropertyName
, 1 );
233 Sequence
< Any
> aValues( &aValue
, 1 );
234 setPropertyValues( aNames
, aValues
);
237 //--------------------------------------------------------------------
238 Any SAL_CALL
OGridColumnPropertyTranslator::getPropertyValue( const OUString
& PropertyName
) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
240 Sequence
< OUString
> aNames( &PropertyName
, 1 );
241 Sequence
< Any
> aValues
= getPropertyValues( aNames
);
242 OSL_ENSURE( aValues
.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
243 if ( aValues
.getLength() == 1 )
248 //--------------------------------------------------------------------
249 void SAL_CALL
OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
251 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
254 //--------------------------------------------------------------------
255 void SAL_CALL
OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
257 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
260 //--------------------------------------------------------------------
261 void SAL_CALL
OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
263 OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
266 //--------------------------------------------------------------------
267 void SAL_CALL
OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
269 OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
272 //--------------------------------------------------------------------
273 void SAL_CALL
OGridColumnPropertyTranslator::setPropertyValues( const Sequence
< OUString
>& aPropertyNames
, const Sequence
< Any
>& aValues
) throw (PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
)
275 if ( !m_xGridColumn
.is() )
278 // if there's ever the need for more than one property being translated, then we should
279 // certainly have a more clever implementation than this ...
281 Sequence
< OUString
> aTranslatedNames( aPropertyNames
);
282 Sequence
< Any
> aTranslatedValues( aValues
);
284 sal_Int32 nParaAlignPos
= findStringElement( aTranslatedNames
, getParaAlignProperty() );
285 if ( nParaAlignPos
!= -1 )
287 aTranslatedNames
[ nParaAlignPos
] = getAlignProperty();
288 valueParaAdjustToAlign( aTranslatedValues
[ nParaAlignPos
] );
291 m_xGridColumn
->setPropertyValues( aTranslatedNames
, aTranslatedValues
);
294 //--------------------------------------------------------------------
295 Sequence
< Any
> SAL_CALL
OGridColumnPropertyTranslator::getPropertyValues( const Sequence
< OUString
>& aPropertyNames
) throw (RuntimeException
)
297 Sequence
< Any
> aValues( aPropertyNames
.getLength() );
298 if ( !m_xGridColumn
.is() )
301 Sequence
< OUString
> aTranslatedNames( aPropertyNames
);
302 sal_Int32 nAlignPos
= findStringElement( aTranslatedNames
, getParaAlignProperty() );
303 if ( nAlignPos
!= -1 )
304 aTranslatedNames
[ nAlignPos
] = getAlignProperty();
306 aValues
= m_xGridColumn
->getPropertyValues( aPropertyNames
);
307 if ( nAlignPos
!= -1 )
308 valueAlignToParaAdjust( aValues
[ nAlignPos
] );
313 //--------------------------------------------------------------------
314 void SAL_CALL
OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& ) throw (RuntimeException
)
316 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
319 //--------------------------------------------------------------------
320 void SAL_CALL
OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference
< XPropertiesChangeListener
>& ) throw (RuntimeException
)
322 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
325 //--------------------------------------------------------------------
326 void SAL_CALL
OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& ) throw (RuntimeException
)
328 OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
331 //........................................................................
332 } // namespace xmloff
333 //........................................................................
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */