LibreOffice Module xmloff (master) 1
gridcolumnproptranslator.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
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>
27
28#include <algorithm>
29
30namespace xmloff
31{
32
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;
39
40 namespace
41 {
42 OUString getParaAlignProperty()
43 {
44 return "ParaAdjust";
45 }
46
47 OUString getAlignProperty()
48 {
49 return "Align";
50 }
51
52 sal_Int32 findStringElement( const Sequence< OUString >& _rNames, const OUString& _rName )
53 {
54 const OUString* pPos = ::std::find( _rNames.begin(), _rNames.end(), _rName );
55 if ( pPos != _rNames.end() )
56 return pPos - _rNames.begin();
57 return -1;
58 }
59
60 struct AlignmentTranslationEntry
61 {
62 ParagraphAdjust nParagraphValue;
63 sal_Int16 nControlValue;
64 }
65 const AlignmentTranslations[] =
66 {
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 }
77 };
78
79 void valueAlignToParaAdjust(Any& rValue)
80 {
81 sal_Int16 nValue = 0;
82 rValue >>= nValue;
83 const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
84 while (-1 != pTranslation->nControlValue)
85 {
86 if ( nValue == pTranslation->nControlValue )
87 {
88 rValue <<= pTranslation->nParagraphValue;
89 return;
90 }
91 ++pTranslation;
92 }
93 OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
94 }
95
96 void valueParaAdjustToAlign(Any& rValue)
97 {
98 sal_Int32 nValue = 0;
99 rValue >>= nValue;
100 const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
101 while ( ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
102 {
103 if ( static_cast<ParagraphAdjust>(nValue) == pTranslation->nParagraphValue)
104 {
105 rValue <<= pTranslation->nControlValue;
106 return;
107 }
108 ++pTranslation;
109 }
110 OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
111 }
112
113 //= OMergedPropertySetInfo
114 typedef ::cppu::WeakAggImplHelper1 < XPropertySetInfo
115 > OMergedPropertySetInfo_Base;
116 class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
117 {
118 private:
119 Reference< XPropertySetInfo > m_xMasterInfo;
120
121 public:
122 explicit OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
123
124 protected:
125 virtual ~OMergedPropertySetInfo() override;
126
127 // XPropertySetInfo
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;
131 };
132
133 OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
134 :m_xMasterInfo( _rxMasterInfo )
135 {
136 OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
137 }
138
139 OMergedPropertySetInfo::~OMergedPropertySetInfo()
140 {
141 }
142
143 Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties( )
144 {
145 // add a "ParaAdjust" property to the master properties
146 Sequence< Property > aProperties;
147 if ( m_xMasterInfo.is() )
148 aProperties = m_xMasterInfo->getProperties();
149
150 sal_Int32 nOldLength = aProperties.getLength();
151 aProperties.realloc( nOldLength + 1 );
152 aProperties.getArray()[ nOldLength ] = getPropertyByName( getParaAlignProperty() );
153
154 return aProperties;
155 }
156
157 Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const OUString& aName )
158 {
159 if ( aName == getParaAlignProperty() )
160 return Property( getParaAlignProperty(), -1,
162
163 if ( !m_xMasterInfo.is() )
164 return Property();
165
166 return m_xMasterInfo->getPropertyByName( aName );
167 }
168
169 sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const OUString& Name )
170 {
171 if ( Name == getParaAlignProperty() )
172 return true;
173
174 if ( !m_xMasterInfo.is() )
175 return false;
176
177 return m_xMasterInfo->hasPropertyByName( Name );
178 }
179 }
180
181 //= OGridColumnPropertyTranslator
182 OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
183 :m_xGridColumn( _rxGridColumn )
184 {
185 OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
186 }
187
189 {
190 }
191
192 Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo( )
193 {
194 Reference< XPropertySetInfo > xColumnPropInfo;
195 if ( m_xGridColumn.is() )
196 xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
197 return new OMergedPropertySetInfo( xColumnPropInfo );
198 }
199
200 void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const OUString& _rPropertyName, const Any& aValue )
201 {
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.
204
205 if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
206 throw UnknownPropertyException( _rPropertyName, *this );
207
208 Sequence< OUString > aNames( &_rPropertyName, 1 );
209 Sequence< Any > aValues( &aValue, 1 );
210 setPropertyValues( aNames, aValues );
211 }
212
213 Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const OUString& PropertyName )
214 {
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 )
219 return aValues[0];
220 return Any();
221 }
222
223 void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
224 {
225 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
226 }
227
228 void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
229 {
230 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
231 }
232
233 void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
234 {
235 OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
236 }
237
238 void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
239 {
240 OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
241 }
242
243 void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< OUString >& aPropertyNames, const Sequence< Any >& aValues )
244 {
245 if ( !m_xGridColumn.is() )
246 return;
247
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 ...
250
251 Sequence< OUString > aTranslatedNames( aPropertyNames );
252 Sequence< Any > aTranslatedValues( aValues );
253
254 sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
255 if ( nParaAlignPos != -1 )
256 {
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 ] );
262 }
263
264 m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
265 }
266
267 Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< OUString >& aPropertyNames )
268 {
269 Sequence< Any > aValues( aPropertyNames.getLength() );
270 if ( !m_xGridColumn.is() )
271 return aValues;
272
273 Sequence< OUString > aTranslatedNames( aPropertyNames );
274 sal_Int32 nAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
275 if ( nAlignPos != -1 )
276 aTranslatedNames.getArray()[ nAlignPos ] = getAlignProperty();
277
278 aValues = m_xGridColumn->getPropertyValues( aPropertyNames );
279 if ( nAlignPos != -1 )
280 valueAlignToParaAdjust( aValues.getArray()[ nAlignPos ] );
281
282 return aValues;
283 }
284
285 void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
286 {
287 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
288 }
289
290 void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
291 {
292 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
293 }
294
295 void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
296 {
297 OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
298 }
299
300} // namespace xmloff
301
302/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
virtual void SAL_CALL setPropertyValues(const css::uno::Sequence< OUString > &aPropertyNames, const css::uno::Sequence< css::uno::Any > &aValues) override
virtual void SAL_CALL addPropertiesChangeListener(const css::uno::Sequence< OUString > &aPropertyNames, const css::uno::Reference< css::beans::XPropertiesChangeListener > &xListener) override
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
css::uno::Reference< css::beans::XMultiPropertySet > m_xGridColumn
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual css::uno::Sequence< css::uno::Any > SAL_CALL getPropertyValues(const css::uno::Sequence< OUString > &aPropertyNames) override
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
virtual void SAL_CALL removePropertiesChangeListener(const css::uno::Reference< css::beans::XPropertiesChangeListener > &xListener) override
virtual void SAL_CALL firePropertiesChangeEvent(const css::uno::Sequence< OUString > &aPropertyNames, const css::uno::Reference< css::beans::XPropertiesChangeListener > &xListener) override
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
sal_Int16 nValue
sal_Int16 nControlValue
ParagraphAdjust nParagraphValue
Reference< XPropertySetInfo > m_xMasterInfo
unsigned char sal_Bool