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()
47 OUString
getAlignProperty()
52 sal_Int32
findStringElement( const Sequence
< OUString
>& _rNames
, const OUString
& _rName
)
54 const OUString
* pPos
= ::std::find( _rNames
.begin(), _rNames
.end(), _rName
);
55 if ( pPos
!= _rNames
.end() )
56 return pPos
- _rNames
.begin();
60 struct AlignmentTranslationEntry
62 ParagraphAdjust nParagraphValue
;
63 sal_Int16 nControlValue
;
65 const AlignmentTranslations
[] =
67 // note that order matters:
68 // valueAlignToParaAdjust and valueParaAdjustToAlign search this map from the _beginning_
69 // and use the first matching entry
70 { ParagraphAdjust_LEFT
, awt::TextAlign::LEFT
},
71 { ParagraphAdjust_CENTER
, awt::TextAlign::CENTER
},
72 { ParagraphAdjust_RIGHT
, awt::TextAlign::RIGHT
},
73 { ParagraphAdjust_BLOCK
, awt::TextAlign::RIGHT
},
74 { ParagraphAdjust_STRETCH
, awt::TextAlign::LEFT
},
75 { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE
, awt::TextAlign::LEFT
},
76 { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE
, -1 }
79 void valueAlignToParaAdjust(Any
& rValue
)
83 const AlignmentTranslationEntry
* pTranslation
= AlignmentTranslations
;
84 while (-1 != pTranslation
->nControlValue
)
86 if ( nValue
== pTranslation
->nControlValue
)
88 rValue
<<= pTranslation
->nParagraphValue
;
93 OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
96 void valueParaAdjustToAlign(Any
& rValue
)
100 const AlignmentTranslationEntry
* pTranslation
= AlignmentTranslations
;
101 while ( ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE
!= pTranslation
->nParagraphValue
)
103 if ( static_cast<ParagraphAdjust
>(nValue
) == pTranslation
->nParagraphValue
)
105 rValue
<<= pTranslation
->nControlValue
;
110 OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
113 //= OMergedPropertySetInfo
114 typedef ::cppu::WeakAggImplHelper1
< XPropertySetInfo
115 > OMergedPropertySetInfo_Base
;
116 class OMergedPropertySetInfo
: public OMergedPropertySetInfo_Base
119 Reference
< XPropertySetInfo
> m_xMasterInfo
;
122 explicit OMergedPropertySetInfo( const Reference
< XPropertySetInfo
>& _rxMasterInfo
);
125 virtual ~OMergedPropertySetInfo() override
;
128 virtual css::uno::Sequence
< css::beans::Property
> SAL_CALL
getProperties( ) override
;
129 virtual css::beans::Property SAL_CALL
getPropertyByName( const OUString
& aName
) override
;
130 virtual sal_Bool SAL_CALL
hasPropertyByName( const OUString
& Name
) override
;
133 OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference
< XPropertySetInfo
>& _rxMasterInfo
)
134 :m_xMasterInfo( _rxMasterInfo
)
136 OSL_ENSURE( m_xMasterInfo
.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
139 OMergedPropertySetInfo::~OMergedPropertySetInfo()
143 Sequence
< Property
> SAL_CALL
OMergedPropertySetInfo::getProperties( )
145 // add a "ParaAdjust" property to the master properties
146 Sequence
< Property
> aProperties
;
147 if ( m_xMasterInfo
.is() )
148 aProperties
= m_xMasterInfo
->getProperties();
150 sal_Int32 nOldLength
= aProperties
.getLength();
151 aProperties
.realloc( nOldLength
+ 1 );
152 aProperties
.getArray()[ nOldLength
] = getPropertyByName( getParaAlignProperty() );
157 Property SAL_CALL
OMergedPropertySetInfo::getPropertyByName( const OUString
& aName
)
159 if ( aName
== getParaAlignProperty() )
160 return Property( getParaAlignProperty(), -1,
161 ::cppu::UnoType
<ParagraphAdjust
>::get(), 0 );
163 if ( !m_xMasterInfo
.is() )
166 return m_xMasterInfo
->getPropertyByName( aName
);
169 sal_Bool SAL_CALL
OMergedPropertySetInfo::hasPropertyByName( const OUString
& Name
)
171 if ( Name
== getParaAlignProperty() )
174 if ( !m_xMasterInfo
.is() )
177 return m_xMasterInfo
->hasPropertyByName( Name
);
181 //= OGridColumnPropertyTranslator
182 OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference
< XMultiPropertySet
>& _rxGridColumn
)
183 :m_xGridColumn( _rxGridColumn
)
185 OSL_ENSURE( m_xGridColumn
.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
188 OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
192 Reference
< XPropertySetInfo
> SAL_CALL
OGridColumnPropertyTranslator::getPropertySetInfo( )
194 Reference
< XPropertySetInfo
> xColumnPropInfo
;
195 if ( m_xGridColumn
.is() )
196 xColumnPropInfo
= m_xGridColumn
->getPropertySetInfo();
197 return new OMergedPropertySetInfo( xColumnPropInfo
);
200 void SAL_CALL
OGridColumnPropertyTranslator::setPropertyValue( const OUString
& _rPropertyName
, const Any
& aValue
)
202 // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
203 // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
205 if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName
) )
206 throw UnknownPropertyException( _rPropertyName
, *this );
208 Sequence
< OUString
> aNames( &_rPropertyName
, 1 );
209 Sequence
< Any
> aValues( &aValue
, 1 );
210 setPropertyValues( aNames
, aValues
);
213 Any SAL_CALL
OGridColumnPropertyTranslator::getPropertyValue( const OUString
& PropertyName
)
215 Sequence
< OUString
> aNames( &PropertyName
, 1 );
216 Sequence
< Any
> aValues
= getPropertyValues( aNames
);
217 OSL_ENSURE( aValues
.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
218 if ( aValues
.getLength() == 1 )
223 void SAL_CALL
OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& )
225 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
228 void SAL_CALL
OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString
&, const Reference
< XPropertyChangeListener
>& )
230 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
233 void SAL_CALL
OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& )
235 OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
238 void SAL_CALL
OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString
&, const Reference
< XVetoableChangeListener
>& )
240 OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
243 void SAL_CALL
OGridColumnPropertyTranslator::setPropertyValues( const Sequence
< OUString
>& aPropertyNames
, const Sequence
< Any
>& aValues
)
245 if ( !m_xGridColumn
.is() )
248 // if there's ever the need for more than one property being translated, then we should
249 // certainly have a more clever implementation than this ...
251 Sequence
< OUString
> aTranslatedNames( aPropertyNames
);
252 Sequence
< Any
> aTranslatedValues( aValues
);
254 sal_Int32 nParaAlignPos
= findStringElement( aTranslatedNames
, getParaAlignProperty() );
255 if ( nParaAlignPos
!= -1 )
257 if (aTranslatedNames
.getLength() != aTranslatedValues
.getLength())
258 throw css::lang::IllegalArgumentException(
259 "lengths do not match", getXWeak(), -1);
260 aTranslatedNames
.getArray()[ nParaAlignPos
] = getAlignProperty();
261 valueParaAdjustToAlign( aTranslatedValues
.getArray()[ nParaAlignPos
] );
264 m_xGridColumn
->setPropertyValues( aTranslatedNames
, aTranslatedValues
);
267 Sequence
< Any
> SAL_CALL
OGridColumnPropertyTranslator::getPropertyValues( const Sequence
< OUString
>& aPropertyNames
)
269 Sequence
< Any
> aValues( aPropertyNames
.getLength() );
270 if ( !m_xGridColumn
.is() )
273 Sequence
< OUString
> aTranslatedNames( aPropertyNames
);
274 sal_Int32 nAlignPos
= findStringElement( aTranslatedNames
, getParaAlignProperty() );
275 if ( nAlignPos
!= -1 )
276 aTranslatedNames
.getArray()[ nAlignPos
] = getAlignProperty();
278 aValues
= m_xGridColumn
->getPropertyValues( aPropertyNames
);
279 if ( nAlignPos
!= -1 )
280 valueAlignToParaAdjust( aValues
.getArray()[ nAlignPos
] );
285 void SAL_CALL
OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& )
287 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
290 void SAL_CALL
OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference
< XPropertiesChangeListener
>& )
292 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
295 void SAL_CALL
OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence
< OUString
>&, const Reference
< XPropertiesChangeListener
>& )
297 OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
300 } // namespace xmloff
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */