xref: /trunk/main/xmloff/source/chart/MultiPropertySetHandler.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _MULTI_PROPERTY_SET_HANDLER_HXX
29*cdf0e10cSrcweir #define _MULTI_PROPERTY_SET_HANDLER_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include    <rtl/ustring.hxx>
32*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/beans/XMultiPropertySet.hpp>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir /** @descr  MultiPropertySetHandler handles the two slightly different
37*cdf0e10cSrcweir         interfaces XPropertySet and XMultiPorpertySet for accessing
38*cdf0e10cSrcweir         properties of an object.
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir         It uses the classes PropertyWrapperBase and the template
41*cdf0e10cSrcweir         PropertyWrapper for a type safe access to single properties.
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir         The function class OUStringComparison is used by a STL map to
44*cdf0e10cSrcweir         sort the properties by names.
45*cdf0e10cSrcweir */
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir /** @descr  Base class for the templated property wrappers.
48*cdf0e10cSrcweir         Having a common base class allows to set a variable to the
49*cdf0e10cSrcweir         property's value without explicit knowledge of its type.
50*cdf0e10cSrcweir */
51*cdf0e10cSrcweir class   PropertyWrapperBase
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir public:
54*cdf0e10cSrcweir     /** @descr  Create a class instance and store the given name.
55*cdf0e10cSrcweir         @param  rName   The name of the property.
56*cdf0e10cSrcweir     */
57*cdf0e10cSrcweir     PropertyWrapperBase (const ::rtl::OUString & rName)
58*cdf0e10cSrcweir         :   msName (rName)
59*cdf0e10cSrcweir     {}
60*cdf0e10cSrcweir     virtual ~PropertyWrapperBase()
61*cdf0e10cSrcweir     {}
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /** @descr  Abstract interface of a method for setting a variables
64*cdf0e10cSrcweir             value to that of the property.
65*cdf0e10cSrcweir     */
66*cdf0e10cSrcweir     virtual void    SetValue    (const ::com::sun::star::uno::Any & rValue) = 0;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     const ::rtl::OUString msName;
69*cdf0e10cSrcweir };
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir /** @descr  For every property type there will be one instantiation of this
75*cdf0e10cSrcweir         template class with its own and type specific version of SetValue.
76*cdf0e10cSrcweir */
77*cdf0e10cSrcweir template<class T> class PropertyWrapper : public PropertyWrapperBase
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir public:
80*cdf0e10cSrcweir     /** @descr  Create a wrapper for a property of type T.
81*cdf0e10cSrcweir     */
82*cdf0e10cSrcweir     PropertyWrapper (const ::rtl::OUString & rName, T & rValue)
83*cdf0e10cSrcweir         :   PropertyWrapperBase (rName),
84*cdf0e10cSrcweir             mrValue (rValue)
85*cdf0e10cSrcweir     {}
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     /** descr   Set the given value inside an Any to the variable referenced
88*cdf0e10cSrcweir         by the data member.
89*cdf0e10cSrcweir     */
90*cdf0e10cSrcweir     virtual void    SetValue    (const ::com::sun::star::uno::Any & rValue)
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         rValue >>= mrValue;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir private:
96*cdf0e10cSrcweir     /// Reference to a variable.  Its value can be modified by a call to SetValue.
97*cdf0e10cSrcweir     T   &   mrValue;
98*cdf0e10cSrcweir };
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir /** @descr  Function object for comparing two OUStrings.
104*cdf0e10cSrcweir */
105*cdf0e10cSrcweir class   OUStringComparison
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir public:
108*cdf0e10cSrcweir     /// Compare two strings.  Returns true if the first is before the second.
109*cdf0e10cSrcweir     inline  bool    operator()  (const ::rtl::OUString & a, const ::rtl::OUString & b) const
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         return (a.compareTo (b) < 0);
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir };
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir /** @descr  This class lets you get the values from an object that either
119*cdf0e10cSrcweir         supports the interface XPropertySet or XMultiPropertySet.  If it
120*cdf0e10cSrcweir         supports both interfaces then XMultiPropertySet is preferred.
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         Using it works in three steps.
123*cdf0e10cSrcweir         1.  Create an instance and pass a reference to the object from which to
124*cdf0e10cSrcweir             get the property values.
125*cdf0e10cSrcweir         2.  Make all properties whose values you want to get known to the object
126*cdf0e10cSrcweir             by using the Add method.  This creates instances of a template class
127*cdf0e10cSrcweir             that stores the properties name and a reference to the variable in
128*cdf0e10cSrcweir             which to store its value.
129*cdf0e10cSrcweir         3.  Finally call GetProperties to store the properties values into the
130*cdf0e10cSrcweir             variables specified in step 2.  This uses either the XPropertySet or
131*cdf0e10cSrcweir             (preferred) the XMultiPropertySet interface.
132*cdf0e10cSrcweir */
133*cdf0e10cSrcweir class   MultiPropertySetHandler
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir public:
136*cdf0e10cSrcweir     /** @descr  Create a handler of the property set of the given
137*cdf0e10cSrcweir             object.
138*cdf0e10cSrcweir         @param  xObject A reference to any of the object's interfaces.
139*cdf0e10cSrcweir             not neccessarily XPropertySet or XMultiPropertySet.  It
140*cdf0e10cSrcweir             is casted later to one of the two of them.
141*cdf0e10cSrcweir     */
142*cdf0e10cSrcweir     MultiPropertySetHandler (::com::sun::star::uno::Reference<
143*cdf0e10cSrcweir         ::com::sun::star::uno::XInterface> xObject);
144*cdf0e10cSrcweir     ~MultiPropertySetHandler    (void);
145*cdf0e10cSrcweir     /** @descr  Add a property to handle.  The type given implicitely by the
146*cdf0e10cSrcweir             reference to a variable is used to create an instance of
147*cdf0e10cSrcweir             the PropertyWrapper template class.
148*cdf0e10cSrcweir         @param  sName   Name of the property.
149*cdf0e10cSrcweir         @param  rValue  Reference to a variable whose value is set by the
150*cdf0e10cSrcweir             call to GetProperties to the property's value.
151*cdf0e10cSrcweir     */
152*cdf0e10cSrcweir     template<class T> void  Add (const ::rtl::OUString & sName, T& rValue)
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         aPropertyList[sName] = new PropertyWrapper<T> (sName, rValue);
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     /** @descr  Try to get the values for all properties added with the Add
158*cdf0e10cSrcweir             method.  If possible it uses the XMultiPropertySet.  If that fails
159*cdf0e10cSrcweir             (i.e. for an UnknownPropertyExcption) or if the interface is not
160*cdf0e10cSrcweir             supported it uses the XPropertySet interface.
161*cdf0e10cSrcweir         @return If none of the two interfaces is supported or using them both
162*cdf0e10cSrcweir             fails then sal_False is returned.  Else True is returned.
163*cdf0e10cSrcweir     */
164*cdf0e10cSrcweir     inline  sal_Bool    GetProperties   (void);
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir private:
167*cdf0e10cSrcweir     /** @descr  Try to use the XMultiPropertySet interface to get the property
168*cdf0e10cSrcweir             values.
169*cdf0e10cSrcweir         @param  rNameList   A precomputed and sorted sequence of OUStrings
170*cdf0e10cSrcweir             containing the properties names.
171*cdf0e10cSrcweir         @return True if values could be derived.
172*cdf0e10cSrcweir     */
173*cdf0e10cSrcweir     inline  sal_Bool    MultiGet    (const ::com::sun::star::uno::Sequence<
174*cdf0e10cSrcweir         ::rtl::OUString> & rNameList);
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /** @descr  Try to use the XPropertySet interface to get the property
177*cdf0e10cSrcweir             values.
178*cdf0e10cSrcweir         @param  rNameList   A precomputed and sorted sequence of OUStrings
179*cdf0e10cSrcweir             containing the properties names.
180*cdf0e10cSrcweir         @return True if values could be derived.
181*cdf0e10cSrcweir     */
182*cdf0e10cSrcweir     inline  sal_Bool    SingleGet   (const ::com::sun::star::uno::Sequence<
183*cdf0e10cSrcweir         ::rtl::OUString> & rNameList);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     /** @descr  STL map that maps from property names to polymorphic instances of
186*cdf0e10cSrcweir             PropertyWrapper.  It uses OUStringComparison for sorting
187*cdf0e10cSrcweir             the property names.
188*cdf0e10cSrcweir     */
189*cdf0e10cSrcweir     ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison> aPropertyList;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     /// The object from which to get the property values.
192*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>    mxObject;
193*cdf0e10cSrcweir };
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir //=====  Inline implementation of the methods declared above  ==========================
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir MultiPropertySetHandler::MultiPropertySetHandler (::com::sun::star::uno::Reference<
200*cdf0e10cSrcweir     ::com::sun::star::uno::XInterface> xObject)
201*cdf0e10cSrcweir         :   mxObject (xObject)
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir MultiPropertySetHandler::~MultiPropertySetHandler (void)
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir     ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
211*cdf0e10cSrcweir     for (I=aPropertyList.begin(); I!=aPropertyList.end(); I++)
212*cdf0e10cSrcweir         delete I->second;
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir /*
217*cdf0e10cSrcweir template<class T> void  MultiPropertySetHandler::Add (const ::rtl::OUString & sName, T& pValue)
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir     aPropertyList[sName] = new PropertyWrapper<T> (sName, pValue);
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir */
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir sal_Bool    MultiPropertySetHandler::GetProperties  (void)
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir     ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
228*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::rtl::OUString> aNameList (aPropertyList.size());
229*cdf0e10cSrcweir     int i;
230*cdf0e10cSrcweir     for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); I++)
231*cdf0e10cSrcweir         aNameList[i++] = I->second->msName;
232*cdf0e10cSrcweir     if ( ! MultiGet(aNameList))
233*cdf0e10cSrcweir         if ( ! SingleGet(aNameList))
234*cdf0e10cSrcweir             return sal_False;
235*cdf0e10cSrcweir     return sal_True;
236*cdf0e10cSrcweir }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir sal_Bool    MultiPropertySetHandler::MultiGet   (const ::com::sun::star::uno::Sequence<
242*cdf0e10cSrcweir     ::rtl::OUString> & rNameList)
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet> xMultiSet (
245*cdf0e10cSrcweir         mxObject, ::com::sun::star::uno::UNO_QUERY);
246*cdf0e10cSrcweir     if (xMultiSet.is())
247*cdf0e10cSrcweir         try
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir             ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
250*cdf0e10cSrcweir             int i;
251*cdf0e10cSrcweir             ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> aValueList =
252*cdf0e10cSrcweir                 xMultiSet->getPropertyValues (rNameList);
253*cdf0e10cSrcweir             for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); I++)
254*cdf0e10cSrcweir                 I->second->SetValue (aValueList[i++]);
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir         catch (::com::sun::star::beans::UnknownPropertyException e)
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             return sal_False;
259*cdf0e10cSrcweir         }
260*cdf0e10cSrcweir     else
261*cdf0e10cSrcweir         return sal_False;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir     return sal_True;
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir sal_Bool    MultiPropertySetHandler::SingleGet  (const ::com::sun::star::uno::Sequence<
270*cdf0e10cSrcweir     ::rtl::OUString> & rNameList)
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xSingleSet (
273*cdf0e10cSrcweir         mxObject, ::com::sun::star::uno::UNO_QUERY);
274*cdf0e10cSrcweir     if (xSingleSet.is())
275*cdf0e10cSrcweir         try
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir             ::std::map< ::rtl::OUString, PropertyWrapperBase*, OUStringComparison>::iterator I;
278*cdf0e10cSrcweir             int i;
279*cdf0e10cSrcweir             for (I=aPropertyList.begin(),i=0; I!=aPropertyList.end(); I++)
280*cdf0e10cSrcweir                 I->second->SetValue (xSingleSet->getPropertyValue (rNameList[i++]));
281*cdf0e10cSrcweir         }
282*cdf0e10cSrcweir         catch (::com::sun::star::beans::UnknownPropertyException e)
283*cdf0e10cSrcweir         {
284*cdf0e10cSrcweir             return sal_False;
285*cdf0e10cSrcweir         }
286*cdf0e10cSrcweir     else
287*cdf0e10cSrcweir         return sal_False;
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     return sal_True;
290*cdf0e10cSrcweir }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir #endif
294*cdf0e10cSrcweir 
295