1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_CANVAS_PROPERTYSETHELPER_HXX
29 #define INCLUDED_CANVAS_PROPERTYSETHELPER_HXX
30 
31 #include <com/sun/star/beans/XPropertySetInfo.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <canvas/canvastools.hxx>
34 
35 #include <boost/function.hpp>
36 #include <vector>
37 #include <memory>
38 
39 
40 namespace canvas
41 {
42     /** Really simplistic XPropertySet helper for properties.
43 
44         This class provides easy access to properties, referenced via
45         ASCII strings. The name/property modification callbacks pairs
46         are passed into this class via a vector. Each time a property
47         is set or queried, the corresponding getter or setter callback
48         is called.
49 
50         Use this class as a delegate for the corresponding
51         XPropertySet methods, and take care of UNO XInterface and lock
52         handling by yourself.
53 
54         The core responsibility of this this class is the name/value
55         mapping for property sets.
56      */
57     class PropertySetHelper
58     {
59     public:
60         typedef boost::function0< ::com::sun::star::uno::Any >            GetterType;
61         typedef boost::function1<void, const ::com::sun::star::uno::Any&> SetterType;
62         struct Callbacks
63         {
64             GetterType getter;
65             SetterType setter;
66         };
67         typedef tools::ValueMap< Callbacks >     MapType;
68         typedef std::vector< MapType::MapEntry > InputMap;
69 
70         class MakeMap : public InputMap
71         {
72         public:
73             MakeMap(const char*        name,
74                     const GetterType&  getter,
75                     const SetterType&  setter)
76             {
77                 MapType::MapEntry aEntry={name, {getter, setter}};
78                 this->push_back(aEntry);
79             }
80             MakeMap(const char*       name,
81                     const GetterType& getter)
82             {
83                 MapType::MapEntry aEntry={name, {getter, SetterType()}};
84                 this->push_back(aEntry);
85             }
86             MakeMap& operator()(const char*        name,
87                                 const GetterType&  getter,
88                                 const SetterType&  setter)
89             {
90                 MapType::MapEntry aEntry={name, {getter, setter}};
91                 this->push_back(aEntry);
92                 return *this;
93             }
94             MakeMap& operator()(const char*       name,
95                                 const GetterType& getter)
96             {
97                 MapType::MapEntry aEntry={name, {getter, SetterType()}};
98                 this->push_back(aEntry);
99                 return *this;
100             }
101         };
102 
103         /** Create helper with zero properties
104          */
105         PropertySetHelper();
106 
107         /** Create helper with given name/value map
108          */
109         explicit PropertySetHelper( const InputMap& rMap );
110 
111         /** Init helper with new name/value map
112 
113             @param rMap
114             Vector of name/function pointers. Each name is offered as
115             a property, and reading/writing to this property is passed
116             on to the given function pointer.
117          */
118         void initProperties( const InputMap& rMap );
119 
120         /** Add given properties to helper
121 
122             @param rMap
123             Vector of name/function pointers. Each name is offered as
124             a property, and reading/writing to this property is passed
125             on to the given function pointer. These name/function
126             pairs are added to the already existing ones.
127          */
128         void addProperties( const InputMap& rMap );
129 
130         /** Checks whether the given string corresponds to a valid
131             property name.
132 
133             @return true, if the given name maps to a known property.
134          */
135         bool isPropertyName( const ::rtl::OUString& aPropertyName ) const;
136 
137         /** Request the currently active map
138          */
139         const InputMap& getPropertyMap() const { return maMapEntries; }
140 
141         // XPropertySet implementation
142         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > getPropertySetInfo() const;
143         void setPropertyValue( const ::rtl::OUString&            aPropertyName,
144                                const ::com::sun::star::uno::Any& aValue );
145         ::com::sun::star::uno::Any getPropertyValue( const ::rtl::OUString& PropertyName ) const;
146         void addPropertyChangeListener( const ::rtl::OUString& aPropertyName,
147                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
148         void removePropertyChangeListener( const ::rtl::OUString& aPropertyName,
149                                            const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
150         void addVetoableChangeListener( const ::rtl::OUString& aPropertyName,
151                                         const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
152         void removeVetoableChangeListener( const ::rtl::OUString& aPropertyName,
153                                            const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
154 
155     private:
156         std::auto_ptr<MapType>  mpMap;
157         InputMap                maMapEntries;
158     };
159 }
160 
161 #endif /* INCLUDED_CANVAS_PROPERTYSETHELPER_HXX */
162