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>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::awt
;
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::beans
;
38 using namespace ::com::sun::star::style
;
42 OUString
getParaAlignProperty()
44 return OUString( "ParaAdjust" );
47 OUString
getAlignProperty()
49 return OUString( "Align" );
52 sal_Int32
findStringElement( const Sequence
< OUString
>& _rNames
, const OUString
& _rName
)
54 const OUString
* pStart
= _rNames
.getConstArray();
55 const OUString
* pEnd
= _rNames
.getConstArray() + _rNames
.getLength();
56 const OUString
* pPos
= ::std::find( pStart
, pEnd
, _rName
);
62 struct AlignmentTranslationEntry
64 ParagraphAdjust nParagraphValue
;
65 sal_Int16 nControlValue
;
67 AlignmentTranslations
[] =
69 // note that order matters:
70 // valueAlignToParaAdjust and valueParaAdjustToAlign search this map from the _beginning_
71 // and use the first matching entry
72 { ParagraphAdjust_LEFT
, awt::TextAlign::LEFT
},
73 { ParagraphAdjust_CENTER
, awt::TextAlign::CENTER
},
74 { ParagraphAdjust_RIGHT
, awt::TextAlign::RIGHT
},
75 { ParagraphAdjust_BLOCK
, awt::TextAlign::RIGHT
},
76 { ParagraphAdjust_STRETCH
, awt::TextAlign::LEFT
},
77 { ParagraphAdjust_MAKE_FIXED_SIZE
, awt::TextAlign::LEFT
},
78 { ParagraphAdjust_MAKE_FIXED_SIZE
, -1 }
81 void valueAlignToParaAdjust(Any
& rValue
)
85 const AlignmentTranslationEntry
* pTranslation
= AlignmentTranslations
;
86 while (-1 != pTranslation
->nControlValue
)
88 if ( nValue
== pTranslation
->nControlValue
)
90 rValue
<<= pTranslation
->nParagraphValue
;
95 OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
98 void valueParaAdjustToAlign(Any
& rValue
)
100 sal_Int32 nValue
= 0;
102 const AlignmentTranslationEntry
* pTranslation
= AlignmentTranslations
;
103 while ( ParagraphAdjust_MAKE_FIXED_SIZE
!= pTranslation
->nParagraphValue
)
105 if ( nValue
== pTranslation
->nParagraphValue
)
107 rValue
<<= pTranslation
->nControlValue
;
112 OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
115 //= OMergedPropertySetInfo
116 typedef ::cppu::WeakAggImplHelper1
< XPropertySetInfo
117 > OMergedPropertySetInfo_Base
;
118 class OMergedPropertySetInfo
: public OMergedPropertySetInfo_Base
121 Reference
< XPropertySetInfo
> m_xMasterInfo
;
124 explicit OMergedPropertySetInfo( const Reference
< XPropertySetInfo
>& _rxMasterInfo
);
127 virtual ~OMergedPropertySetInfo();
130 virtual css::uno::Sequence
< css::beans::Property
> SAL_CALL
getProperties( ) throw (css::uno::RuntimeException
, std::exception
) override
;
131 virtual css::beans::Property SAL_CALL
getPropertyByName( const OUString
& aName
) throw (css::beans::UnknownPropertyException
, css::uno::RuntimeException
, std::exception
) override
;
132 virtual sal_Bool SAL_CALL
hasPropertyByName( const OUString
& Name
) throw (css::uno::RuntimeException
, std::exception
) override
;
135 OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference
< XPropertySetInfo
>& _rxMasterInfo
)
136 :m_xMasterInfo( _rxMasterInfo
)
138 OSL_ENSURE( m_xMasterInfo
.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
141 OMergedPropertySetInfo::~OMergedPropertySetInfo()
145 Sequence
< Property
> SAL_CALL
OMergedPropertySetInfo::getProperties( ) throw (RuntimeException
, std::exception
)
147 // add a "ParaAdjust" property to the master properties
148 Sequence
< Property
> aProperties
;
149 if ( m_xMasterInfo
.is() )
150 aProperties
= m_xMasterInfo
->getProperties();
152 sal_Int32 nOldLength
= aProperties
.getLength();
153 aProperties
.realloc( nOldLength
+ 1 );
154 aProperties
[ nOldLength
] = getPropertyByName( getParaAlignProperty() );
159 Property SAL_CALL
OMergedPropertySetInfo::getPropertyByName( const OUString
& aName
) throw (UnknownPropertyException
, RuntimeException
, std::exception
)
161 if ( aName
== getParaAlignProperty() )
162 return Property( getParaAlignProperty(), -1,
163 ::cppu::UnoType
<ParagraphAdjust
>::get(), 0 );
165 if ( !m_xMasterInfo
.is() )
168 return m_xMasterInfo
->getPropertyByName( aName
);
171 sal_Bool SAL_CALL
OMergedPropertySetInfo::hasPropertyByName( const OUString
& Name
) throw (RuntimeException
, std::exception
)
173 if ( Name
== getParaAlignProperty() )
176 if ( !m_xMasterInfo
.is() )
179 return m_xMasterInfo
->hasPropertyByName( Name
);
183 //= OGridColumnPropertyTranslator
184 OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference
< XMultiPropertySet
>& _rxGridColumn
)
185 :m_xGridColumn( _rxGridColumn
)
187 OSL_ENSURE( m_xGridColumn
.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
190 OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
194 Reference
< XPropertySetInfo
> SAL_CALL
OGridColumnPropertyTranslator::getPropertySetInfo( ) throw (RuntimeException
, std::exception
)
196 Reference
< XPropertySetInfo
> xColumnPropInfo
;
197 if ( m_xGridColumn
.is() )
198 xColumnPropInfo
= m_xGridColumn
->getPropertySetInfo();
199 return new OMergedPropertySetInfo( xColumnPropInfo
);
202 void SAL_CALL
OGridColumnPropertyTranslator::setPropertyValue( const OUString
& _rPropertyName
, const Any
& aValue
) throw (UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
, std::exception
)
204 // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
205 // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
207 if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName
) )
208 throw UnknownPropertyException( _rPropertyName
, *this );
210 Sequence
< OUString
> aNames( &_rPropertyName
, 1 );
211 Sequence
< Any
> aValues( &aValue
, 1 );
212 setPropertyValues( aNames
, aValues
);
215 Any SAL_CALL
OGridColumnPropertyTranslator::getPropertyValue( const OUString
& PropertyName
) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
217 Sequence
< OUString
> aNames( &PropertyName
, 1 );
218 Sequence
< Any
> aValues
= getPropertyValues( aNames
);
219 OSL_ENSURE( aValues
.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
220 if ( aValues
.getLength() == 1 )
225 void SAL_CALL
OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
227 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
230 void SAL_CALL
OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
232 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
235 void SAL_CALL
OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
237 OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
240 void SAL_CALL
OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
, std::exception
)
242 OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
245 void SAL_CALL
OGridColumnPropertyTranslator::setPropertyValues( const Sequence
< OUString
>& aPropertyNames
, const Sequence
< Any
>& aValues
) throw (PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
, std::exception
)
247 if ( !m_xGridColumn
.is() )
250 // if there's ever the need for more than one property being translated, then we should
251 // certainly have a more clever implementation than this ...
253 Sequence
< OUString
> aTranslatedNames( aPropertyNames
);
254 Sequence
< Any
> aTranslatedValues( aValues
);
256 sal_Int32 nParaAlignPos
= findStringElement( aTranslatedNames
, getParaAlignProperty() );
257 if ( nParaAlignPos
!= -1 )
259 aTranslatedNames
[ nParaAlignPos
] = getAlignProperty();
260 valueParaAdjustToAlign( aTranslatedValues
[ nParaAlignPos
] );
263 m_xGridColumn
->setPropertyValues( aTranslatedNames
, aTranslatedValues
);
266 Sequence
< Any
> SAL_CALL
OGridColumnPropertyTranslator::getPropertyValues( const Sequence
< OUString
>& aPropertyNames
) throw (RuntimeException
, std::exception
)
268 Sequence
< Any
> aValues( aPropertyNames
.getLength() );
269 if ( !m_xGridColumn
.is() )
272 Sequence
< OUString
> aTranslatedNames( aPropertyNames
);
273 sal_Int32 nAlignPos
= findStringElement( aTranslatedNames
, getParaAlignProperty() );
274 if ( nAlignPos
!= -1 )
275 aTranslatedNames
[ nAlignPos
] = getAlignProperty();
277 aValues
= m_xGridColumn
->getPropertyValues( aPropertyNames
);
278 if ( nAlignPos
!= -1 )
279 valueAlignToParaAdjust( aValues
[ nAlignPos
] );
284 void SAL_CALL
OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& ) throw (RuntimeException
, std::exception
)
286 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
289 void SAL_CALL
OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference
< XPropertiesChangeListener
>& ) throw (RuntimeException
, std::exception
)
291 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
294 void SAL_CALL
OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& ) throw (RuntimeException
, std::exception
)
296 OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
299 } // namespace xmloff
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */