Line data Source code
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 :
20 : #include "gridcolumnproptranslator.hxx"
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>
26 : #include <cppuhelper/implbase1.hxx>
27 :
28 : #include <algorithm>
29 :
30 : //........................................................................
31 : namespace xmloff
32 : {
33 : //........................................................................
34 :
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;
41 :
42 : namespace
43 : {
44 : //----------------------------------------------------------------
45 0 : ::rtl::OUString getParaAlignProperty()
46 : {
47 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ParaAdjust" ) );
48 : }
49 :
50 : //----------------------------------------------------------------
51 0 : ::rtl::OUString getAlignProperty()
52 : {
53 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Align" ) );
54 : }
55 :
56 : //----------------------------------------------------------------
57 0 : sal_Int32 findStringElement( const Sequence< ::rtl::OUString >& _rNames, const ::rtl::OUString& _rName )
58 : {
59 0 : const ::rtl::OUString* pStart = _rNames.getConstArray();
60 0 : const ::rtl::OUString* pEnd = _rNames.getConstArray() + _rNames.getLength();
61 0 : const ::rtl::OUString* pPos = ::std::find( pStart, pEnd, _rName );
62 0 : if ( pPos != pEnd )
63 0 : return pPos - pStart;
64 0 : return -1;
65 : }
66 :
67 : //----------------------------------------------------------------
68 : struct AlignmentTranslationEntry
69 : {
70 : ParagraphAdjust nParagraphValue;
71 : sal_Int16 nControlValue;
72 : }
73 : AlignmentTranslations[] =
74 : {
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 }
85 : };
86 :
87 : //----------------------------------------------------------------
88 0 : void valueAlignToParaAdjust(Any& rValue)
89 : {
90 0 : sal_Int16 nValue = 0;
91 0 : rValue >>= nValue;
92 0 : const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
93 0 : while (-1 != pTranslation->nControlValue)
94 : {
95 0 : if ( nValue == pTranslation->nControlValue )
96 : {
97 0 : rValue <<= pTranslation->nParagraphValue;
98 0 : return;
99 : }
100 0 : ++pTranslation;
101 : }
102 : OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
103 : }
104 :
105 : //----------------------------------------------------------------
106 0 : void valueParaAdjustToAlign(Any& rValue)
107 : {
108 0 : sal_Int32 nValue = 0;
109 0 : rValue >>= nValue;
110 0 : const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
111 0 : while ( ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
112 : {
113 0 : if ( nValue == pTranslation->nParagraphValue)
114 : {
115 0 : rValue <<= pTranslation->nControlValue;
116 0 : return;
117 : }
118 0 : ++pTranslation;
119 : }
120 : OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
121 : }
122 :
123 : //====================================================================
124 : //= OMergedPropertySetInfo
125 : //====================================================================
126 : typedef ::cppu::WeakAggImplHelper1 < XPropertySetInfo
127 : > OMergedPropertySetInfo_Base;
128 : class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
129 : {
130 : private:
131 : Reference< XPropertySetInfo > m_xMasterInfo;
132 :
133 : public:
134 : OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
135 :
136 : protected:
137 : virtual ~OMergedPropertySetInfo();
138 :
139 : // XPropertySetInfo
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 ::rtl::OUString& aName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
142 : virtual ::sal_Bool SAL_CALL hasPropertyByName( const ::rtl::OUString& Name ) throw (::com::sun::star::uno::RuntimeException);
143 : };
144 :
145 : //----------------------------------------------------------------
146 0 : OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
147 0 : :m_xMasterInfo( _rxMasterInfo )
148 : {
149 : OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
150 0 : }
151 :
152 : //----------------------------------------------------------------
153 0 : OMergedPropertySetInfo::~OMergedPropertySetInfo()
154 : {
155 0 : }
156 :
157 : //----------------------------------------------------------------
158 0 : Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties( ) throw (RuntimeException)
159 : {
160 : // add a "ParaAdjust" property to the master properties
161 0 : Sequence< Property > aProperties;
162 0 : if ( m_xMasterInfo.is() )
163 0 : aProperties = m_xMasterInfo->getProperties();
164 :
165 0 : sal_Int32 nOldLength = aProperties.getLength();
166 0 : aProperties.realloc( nOldLength + 1 );
167 0 : aProperties[ nOldLength ] = getPropertyByName( getParaAlignProperty() );
168 :
169 0 : return aProperties;
170 : }
171 :
172 : //----------------------------------------------------------------
173 0 : Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const ::rtl::OUString& aName ) throw (UnknownPropertyException, RuntimeException)
174 : {
175 0 : if ( aName == getParaAlignProperty() )
176 : return Property( getParaAlignProperty(), -1,
177 0 : ::getCppuType( static_cast< const ParagraphAdjust* >( NULL ) ), 0 );
178 :
179 0 : if ( !m_xMasterInfo.is() )
180 0 : return Property();
181 :
182 0 : return m_xMasterInfo->getPropertyByName( aName );
183 : }
184 :
185 : //----------------------------------------------------------------
186 0 : ::sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const ::rtl::OUString& Name ) throw (RuntimeException)
187 : {
188 0 : if ( Name == getParaAlignProperty() )
189 0 : return sal_True;
190 :
191 0 : if ( !m_xMasterInfo.is() )
192 0 : return sal_False;
193 :
194 0 : return m_xMasterInfo->hasPropertyByName( Name );
195 : }
196 : }
197 :
198 :
199 : //====================================================================
200 : //= OGridColumnPropertyTranslator
201 : //====================================================================
202 : //--------------------------------------------------------------------
203 0 : OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
204 0 : :m_xGridColumn( _rxGridColumn )
205 : {
206 : OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
207 0 : }
208 :
209 : //--------------------------------------------------------------------
210 0 : OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
211 : {
212 0 : }
213 :
214 : //--------------------------------------------------------------------
215 0 : Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo( ) throw (RuntimeException)
216 : {
217 0 : Reference< XPropertySetInfo > xColumnPropInfo;
218 0 : if ( m_xGridColumn.is() )
219 0 : xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
220 0 : return new OMergedPropertySetInfo( xColumnPropInfo );
221 : }
222 :
223 : //--------------------------------------------------------------------
224 0 : void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& aValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
225 : {
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.
228 :
229 0 : if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
230 0 : throw UnknownPropertyException( _rPropertyName, *this );
231 :
232 0 : Sequence< ::rtl::OUString > aNames( &_rPropertyName, 1 );
233 0 : Sequence< Any > aValues( &aValue, 1 );
234 0 : setPropertyValues( aNames, aValues );
235 0 : }
236 :
237 : //--------------------------------------------------------------------
238 0 : Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const ::rtl::OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
239 : {
240 0 : Sequence< ::rtl::OUString > aNames( &PropertyName, 1 );
241 0 : Sequence< Any > aValues = getPropertyValues( aNames );
242 : OSL_ENSURE( aValues.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
243 0 : if ( aValues.getLength() == 1 )
244 0 : return aValues[0];
245 0 : return Any();
246 : }
247 :
248 : //--------------------------------------------------------------------
249 0 : void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
250 : {
251 : OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
252 0 : }
253 :
254 : //--------------------------------------------------------------------
255 0 : void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
256 : {
257 : OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
258 0 : }
259 :
260 : //--------------------------------------------------------------------
261 0 : void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const ::rtl::OUString&, const Reference< XVetoableChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
262 : {
263 : OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
264 0 : }
265 :
266 : //--------------------------------------------------------------------
267 0 : void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const ::rtl::OUString&, const Reference< XVetoableChangeListener >& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
268 : {
269 : OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
270 0 : }
271 :
272 : //--------------------------------------------------------------------
273 0 : void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< ::rtl::OUString >& aPropertyNames, const Sequence< Any >& aValues ) throw (PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
274 : {
275 0 : if ( !m_xGridColumn.is() )
276 0 : return;
277 :
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 ...
280 :
281 0 : Sequence< ::rtl::OUString > aTranslatedNames( aPropertyNames );
282 0 : Sequence< Any > aTranslatedValues( aValues );
283 :
284 0 : sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
285 0 : if ( nParaAlignPos != -1 )
286 : {
287 0 : aTranslatedNames[ nParaAlignPos ] = getAlignProperty();
288 0 : valueParaAdjustToAlign( aTranslatedValues[ nParaAlignPos ] );
289 : }
290 :
291 0 : m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
292 : }
293 :
294 : //--------------------------------------------------------------------
295 0 : Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< ::rtl::OUString >& aPropertyNames ) throw (RuntimeException)
296 : {
297 0 : Sequence< Any > aValues( aPropertyNames.getLength() );
298 0 : if ( !m_xGridColumn.is() )
299 : return aValues;
300 :
301 0 : Sequence< ::rtl::OUString > aTranslatedNames( aPropertyNames );
302 0 : sal_Int32 nAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
303 0 : if ( nAlignPos != -1 )
304 0 : aTranslatedNames[ nAlignPos ] = getAlignProperty();
305 :
306 0 : aValues = m_xGridColumn->getPropertyValues( aPropertyNames );
307 0 : if ( nAlignPos != -1 )
308 0 : valueAlignToParaAdjust( aValues[ nAlignPos ] );
309 :
310 0 : return aValues;
311 : }
312 :
313 : //--------------------------------------------------------------------
314 0 : void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< ::rtl::OUString >&, const Reference< XPropertiesChangeListener >& ) throw (RuntimeException)
315 : {
316 : OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
317 0 : }
318 :
319 : //--------------------------------------------------------------------
320 0 : void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& ) throw (RuntimeException)
321 : {
322 : OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
323 0 : }
324 :
325 : //--------------------------------------------------------------------
326 0 : void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< ::rtl::OUString >&, const Reference< XPropertiesChangeListener >& ) throw (RuntimeException)
327 : {
328 : OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
329 0 : }
330 :
331 : //........................................................................
332 : } // namespace xmloff
333 : //........................................................................
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|