1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PropertyHelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "PropertyHelper.hxx"
34 #include "ContainerHelper.hxx"
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/container/XNameContainer.hpp>
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::beans
;
45 using ::rtl::OUString
;
46 using ::com::sun::star::uno::Any
;
47 using ::com::sun::star::uno::Reference
;
48 using ::com::sun::star::uno::Sequence
;
52 struct lcl_EqualsElement
: public ::std::unary_function
< OUString
, bool >
54 explicit lcl_EqualsElement( const Any
& rValue
, const Reference
< container::XNameAccess
> & xAccess
)
55 : m_aValue( rValue
), m_xAccess( xAccess
)
57 OSL_ASSERT( m_xAccess
.is());
60 bool operator() ( const OUString
& rName
)
64 return (m_xAccess
->getByName( rName
) == m_aValue
);
66 catch( const uno::Exception
& ex
)
68 ASSERT_EXCEPTION( ex
);
75 Reference
< container::XNameAccess
> m_xAccess
;
78 struct lcl_StringMatches
: public ::std::unary_function
< OUString
,bool >
80 lcl_StringMatches( const OUString
& rCmpStr
) :
84 bool operator() ( const OUString
& rStr
)
86 return rStr
.match( m_aCmpStr
);
93 struct lcl_OUStringRestToInt32
: public ::std::unary_function
< OUString
, sal_Int32
>
95 lcl_OUStringRestToInt32( sal_Int32 nPrefixLength
) :
96 m_nPrefixLength( nPrefixLength
)
98 sal_Int32
operator() ( const OUString
& rStr
)
100 if( m_nPrefixLength
> rStr
.getLength() )
102 return rStr
.copy( m_nPrefixLength
).toInt32( 10 /* radix */ );
105 sal_Int32 m_nPrefixLength
;
108 /** adds a fill gradient, fill hatch, fill bitmap, fill transparency gradient,
109 line dash or line marker to the corresponding name container with a unique
113 The prefix used for automated name generation.
115 @param rPreferredName
116 If this string is not empty it is used as name if it is unique in the
117 table. Otherwise a new name is generated using pPrefix.
119 @return the new name under which the property was stored in the table
121 OUString
lcl_addNamedPropertyUniqueNameToTable(
123 const Reference
< container::XNameContainer
> & xNameContainer
,
124 const OUString
& rPrefix
,
125 const OUString
& rPreferredName
)
127 if( ! xNameContainer
.is() ||
128 ! rValue
.hasValue() ||
129 ( rValue
.getValueType() != xNameContainer
->getElementType()))
130 return rPreferredName
;
134 Reference
< container::XNameAccess
> xNameAccess( xNameContainer
, uno::UNO_QUERY_THROW
);
135 ::std::vector
< OUString
> aNames( ::chart::ContainerHelper::SequenceToVector( xNameAccess
->getElementNames()));
136 ::std::vector
< OUString
>::const_iterator
aIt(
137 ::std::find_if( aNames
.begin(), aNames
.end(), lcl_EqualsElement( rValue
, xNameAccess
)));
139 // element not found in container
140 if( aIt
== aNames
.end())
142 OUString aUniqueName
;
144 // check if preferred name is already used
145 if( rPreferredName
.getLength())
147 aIt
= ::std::find( aNames
.begin(), aNames
.end(), rPreferredName
);
148 if( aIt
== aNames
.end())
149 aUniqueName
= rPreferredName
;
152 if( ! aUniqueName
.getLength())
154 // create a unique id using the prefix plus a number
155 ::std::vector
< sal_Int32
> aNumbers
;
156 ::std::vector
< OUString
>::iterator
aNonConstIt(
157 ::std::partition( aNames
.begin(), aNames
.end(), lcl_StringMatches( rPrefix
)));
158 ::std::transform( aNames
.begin(), aNonConstIt
,
159 back_inserter( aNumbers
),
160 lcl_OUStringRestToInt32( rPrefix
.getLength() ));
161 ::std::vector
< sal_Int32
>::const_iterator
aMaxIt(
162 ::std::max_element( aNumbers
.begin(), aNumbers
.end()));
164 sal_Int32 nIndex
= 1;
165 if( aMaxIt
!= aNumbers
.end())
166 nIndex
= (*aMaxIt
) + 1;
168 aUniqueName
= rPrefix
+ OUString::valueOf( nIndex
);
171 OSL_ASSERT( aUniqueName
.getLength());
172 xNameContainer
->insertByName( aUniqueName
, rValue
);
176 // element found => return name
179 catch( const uno::Exception
& ex
)
181 ASSERT_EXCEPTION( ex
);
184 return rPreferredName
;
187 } // anonymous namespace
191 namespace PropertyHelper
194 OUString
addLineDashUniqueNameToTable(
196 const Reference
< lang::XMultiServiceFactory
> & xFact
,
197 const OUString
& rPreferredName
)
201 Reference
< container::XNameContainer
> xNameCnt(
202 xFact
->createInstance( C2U( "com.sun.star.drawing.DashTable" )),
205 return lcl_addNamedPropertyUniqueNameToTable(
206 rValue
, xNameCnt
, C2U( "ChartDash " ), rPreferredName
);
211 OUString
addGradientUniqueNameToTable(
213 const Reference
< lang::XMultiServiceFactory
> & xFact
,
214 const OUString
& rPreferredName
)
218 Reference
< container::XNameContainer
> xNameCnt(
219 xFact
->createInstance( C2U( "com.sun.star.drawing.GradientTable" )),
222 return lcl_addNamedPropertyUniqueNameToTable(
223 rValue
, xNameCnt
, C2U( "ChartGradient " ), rPreferredName
);
229 OUString
addTransparencyGradientUniqueNameToTable(
231 const Reference
< lang::XMultiServiceFactory
> & xFact
,
232 const OUString
& rPreferredName
)
236 Reference
< container::XNameContainer
> xNameCnt(
237 xFact
->createInstance( C2U( "com.sun.star.drawing.TransparencyGradientTable" )),
240 return lcl_addNamedPropertyUniqueNameToTable(
241 rValue
, xNameCnt
, C2U( "ChartTransparencyGradient " ), rPreferredName
);
246 OUString
addHatchUniqueNameToTable(
248 const Reference
< lang::XMultiServiceFactory
> & xFact
,
249 const OUString
& rPreferredName
)
253 Reference
< container::XNameContainer
> xNameCnt(
254 xFact
->createInstance( C2U( "com.sun.star.drawing.HatchTable" )),
257 return lcl_addNamedPropertyUniqueNameToTable(
258 rValue
, xNameCnt
, C2U( "ChartHatch " ), rPreferredName
);
263 OUString
addBitmapUniqueNameToTable(
265 const Reference
< lang::XMultiServiceFactory
> & xFact
,
266 const OUString
& rPreferredName
)
270 Reference
< container::XNameContainer
> xNameCnt(
271 xFact
->createInstance( C2U( "com.sun.star.drawing.BitmapTable" )),
274 return lcl_addNamedPropertyUniqueNameToTable(
275 rValue
, xNameCnt
, C2U( "ChartBitmap " ), rPreferredName
);
280 // ----------------------------------------
282 void setPropertyValueAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
284 tPropertyValueMap::iterator
aIt( rOutMap
.find( key
));
285 if( aIt
== rOutMap
.end())
286 rOutMap
.insert( tPropertyValueMap::value_type( key
, rAny
));
288 (*aIt
).second
= rAny
;
292 void setPropertyValue
< ::com::sun::star::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const ::com::sun::star::uno::Any
& rAny
)
294 setPropertyValueAny( rOutMap
, key
, rAny
);
297 void setPropertyValueDefaultAny( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const uno::Any
& rAny
)
299 OSL_ENSURE( rOutMap
.end() == rOutMap
.find( key
), "Default already exists for property" );
300 setPropertyValue( rOutMap
, key
, rAny
);
304 void setPropertyValueDefault
< ::com::sun::star::uno::Any
>( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
, const ::com::sun::star::uno::Any
& rAny
)
306 setPropertyValueDefaultAny( rOutMap
, key
, rAny
);
310 void setEmptyPropertyValueDefault( tPropertyValueMap
& rOutMap
, tPropertyValueMapKey key
)
312 setPropertyValueDefault( rOutMap
, key
, uno::Any());
315 } // namespace PropertyHelper