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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_canvas.hxx"
30 
31 #include <canvas/propertysethelper.hxx>
32 
33 using namespace ::com::sun::star;
34 
35 namespace canvas
36 {
37     namespace
38     {
39         void throwUnknown( const ::rtl::OUString& aPropertyName )
40         {
41             throw beans::UnknownPropertyException(
42                 ::rtl::OUString::createFromAscii("PropertySetHelper: property ") +
43                 aPropertyName +
44                 ::rtl::OUString::createFromAscii(" not found."),
45                 uno::Reference< uno::XInterface >()
46                 );
47         }
48 
49         void throwVeto( const ::rtl::OUString& aPropertyName )
50         {
51             throw beans::PropertyVetoException(
52                 ::rtl::OUString::createFromAscii("PropertySetHelper: property ") +
53                 aPropertyName +
54                 ::rtl::OUString::createFromAscii(" access was vetoed."),
55                 uno::Reference< uno::XInterface >() );
56         }
57 
58         struct EntryComparator
59         {
60             bool operator()( const PropertySetHelper::MapType::MapEntry& rLHS,
61                              const PropertySetHelper::MapType::MapEntry& rRHS )
62             {
63                 return strcmp( rLHS.maKey,
64                                rRHS.maKey ) < 0;
65             }
66         };
67     }
68 
69     PropertySetHelper::PropertySetHelper() :
70         mpMap(),
71         maMapEntries()
72     {
73     }
74 
75     PropertySetHelper::PropertySetHelper( const InputMap& rMap ) :
76         mpMap(),
77         maMapEntries()
78     {
79         initProperties(rMap);
80     }
81 
82     void PropertySetHelper::initProperties( const InputMap& rMap )
83     {
84         mpMap.reset();
85         maMapEntries = rMap;
86 
87         std::sort( maMapEntries.begin(),
88                    maMapEntries.end(),
89                    EntryComparator() );
90 
91         if( !maMapEntries.empty() )
92             mpMap.reset( new MapType(&maMapEntries[0],
93                                      maMapEntries.size(),
94                                      true) );
95     }
96 
97     void PropertySetHelper::addProperties( const InputMap& rMap )
98     {
99         InputMap aMerged( getPropertyMap() );
100         aMerged.insert( aMerged.end(),
101                         rMap.begin(),
102                         rMap.end() );
103 
104         initProperties( aMerged );
105     }
106 
107     bool PropertySetHelper::isPropertyName( const ::rtl::OUString& aPropertyName ) const
108     {
109         if( !mpMap.get() )
110             return false;
111 
112         Callbacks aDummy;
113         return mpMap->lookup( aPropertyName,
114                               aDummy );
115     }
116 
117     uno::Reference< beans::XPropertySetInfo > PropertySetHelper::getPropertySetInfo() const
118     {
119         // we're a stealth property set
120         return uno::Reference< beans::XPropertySetInfo >();
121     }
122 
123     void PropertySetHelper::setPropertyValue( const ::rtl::OUString& aPropertyName,
124                                               const uno::Any&        aValue )
125     {
126         Callbacks aCallbacks;
127         if( !mpMap.get() ||
128             !mpMap->lookup( aPropertyName,
129                             aCallbacks ) )
130         {
131             throwUnknown( aPropertyName );
132         }
133 
134         if( aCallbacks.setter.empty() )
135             throwVeto( aPropertyName );
136 
137         aCallbacks.setter(aValue);
138     }
139 
140     uno::Any PropertySetHelper::getPropertyValue( const ::rtl::OUString& aPropertyName ) const
141     {
142         Callbacks aCallbacks;
143         if( !mpMap.get() ||
144             !mpMap->lookup( aPropertyName,
145                             aCallbacks ) )
146         {
147             throwUnknown( aPropertyName );
148         }
149 
150         if( !aCallbacks.getter.empty() )
151             return aCallbacks.getter();
152 
153         // TODO(Q1): subtlety, empty getter method silently returns
154         // the empty any
155         return uno::Any();
156     }
157 
158     void PropertySetHelper::addPropertyChangeListener( const ::rtl::OUString&                                  aPropertyName,
159                                                        const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
160     {
161         // check validity of property, but otherwise ignore the
162         // request
163         if( !isPropertyName( aPropertyName ) )
164             throwUnknown( aPropertyName );
165     }
166 
167     void PropertySetHelper::removePropertyChangeListener( const ::rtl::OUString&                                  /*aPropertyName*/,
168                                                           const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
169     {
170         // ignore request, no listener added in the first place
171     }
172 
173     void PropertySetHelper::addVetoableChangeListener( const ::rtl::OUString&                                  aPropertyName,
174                                                        const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
175     {
176         // check validity of property, but otherwise ignore the
177         // request
178         if( !isPropertyName( aPropertyName ) )
179             throwUnknown( aPropertyName );
180     }
181 
182     void PropertySetHelper::removeVetoableChangeListener( const ::rtl::OUString&                                  /*aPropertyName*/,
183                                                           const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
184     {
185         // ignore request, no listener added in the first place
186     }
187 }
188