1*25ea7f45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*25ea7f45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*25ea7f45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*25ea7f45SAndrew Rist  * distributed with this work for additional information
6*25ea7f45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*25ea7f45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*25ea7f45SAndrew Rist  * "License"); you may not use this file except in compliance
9*25ea7f45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*25ea7f45SAndrew Rist  *
11*25ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25ea7f45SAndrew Rist  *
13*25ea7f45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*25ea7f45SAndrew Rist  * software distributed under the License is distributed on an
15*25ea7f45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*25ea7f45SAndrew Rist  * KIND, either express or implied.  See the License for the
17*25ea7f45SAndrew Rist  * specific language governing permissions and limitations
18*25ea7f45SAndrew Rist  * under the License.
19*25ea7f45SAndrew Rist  *
20*25ea7f45SAndrew Rist  *************************************************************/
21*25ea7f45SAndrew Rist 
22*25ea7f45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_canvas.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <canvas/propertysethelper.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using namespace ::com::sun::star;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace canvas
32cdf0e10cSrcweir {
33cdf0e10cSrcweir     namespace
34cdf0e10cSrcweir     {
throwUnknown(const::rtl::OUString & aPropertyName)35cdf0e10cSrcweir         void throwUnknown( const ::rtl::OUString& aPropertyName )
36cdf0e10cSrcweir         {
37cdf0e10cSrcweir             throw beans::UnknownPropertyException(
38cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("PropertySetHelper: property ") +
39cdf0e10cSrcweir                 aPropertyName +
40cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii(" not found."),
41cdf0e10cSrcweir                 uno::Reference< uno::XInterface >()
42cdf0e10cSrcweir                 );
43cdf0e10cSrcweir         }
44cdf0e10cSrcweir 
throwVeto(const::rtl::OUString & aPropertyName)45cdf0e10cSrcweir         void throwVeto( const ::rtl::OUString& aPropertyName )
46cdf0e10cSrcweir         {
47cdf0e10cSrcweir             throw beans::PropertyVetoException(
48cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("PropertySetHelper: property ") +
49cdf0e10cSrcweir                 aPropertyName +
50cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii(" access was vetoed."),
51cdf0e10cSrcweir                 uno::Reference< uno::XInterface >() );
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir         struct EntryComparator
55cdf0e10cSrcweir         {
operator ()canvas::__anon0268b6330111::EntryComparator56cdf0e10cSrcweir             bool operator()( const PropertySetHelper::MapType::MapEntry& rLHS,
57cdf0e10cSrcweir                              const PropertySetHelper::MapType::MapEntry& rRHS )
58cdf0e10cSrcweir             {
59cdf0e10cSrcweir                 return strcmp( rLHS.maKey,
60cdf0e10cSrcweir                                rRHS.maKey ) < 0;
61cdf0e10cSrcweir             }
62cdf0e10cSrcweir         };
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
PropertySetHelper()65cdf0e10cSrcweir     PropertySetHelper::PropertySetHelper() :
66cdf0e10cSrcweir         mpMap(),
67cdf0e10cSrcweir         maMapEntries()
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
PropertySetHelper(const InputMap & rMap)71cdf0e10cSrcweir     PropertySetHelper::PropertySetHelper( const InputMap& rMap ) :
72cdf0e10cSrcweir         mpMap(),
73cdf0e10cSrcweir         maMapEntries()
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         initProperties(rMap);
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
initProperties(const InputMap & rMap)78cdf0e10cSrcweir     void PropertySetHelper::initProperties( const InputMap& rMap )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         mpMap.reset();
81cdf0e10cSrcweir         maMapEntries = rMap;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         std::sort( maMapEntries.begin(),
84cdf0e10cSrcweir                    maMapEntries.end(),
85cdf0e10cSrcweir                    EntryComparator() );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         if( !maMapEntries.empty() )
88cdf0e10cSrcweir             mpMap.reset( new MapType(&maMapEntries[0],
89cdf0e10cSrcweir                                      maMapEntries.size(),
90cdf0e10cSrcweir                                      true) );
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
addProperties(const InputMap & rMap)93cdf0e10cSrcweir     void PropertySetHelper::addProperties( const InputMap& rMap )
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         InputMap aMerged( getPropertyMap() );
96cdf0e10cSrcweir         aMerged.insert( aMerged.end(),
97cdf0e10cSrcweir                         rMap.begin(),
98cdf0e10cSrcweir                         rMap.end() );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         initProperties( aMerged );
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
isPropertyName(const::rtl::OUString & aPropertyName) const103cdf0e10cSrcweir     bool PropertySetHelper::isPropertyName( const ::rtl::OUString& aPropertyName ) const
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         if( !mpMap.get() )
106cdf0e10cSrcweir             return false;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         Callbacks aDummy;
109cdf0e10cSrcweir         return mpMap->lookup( aPropertyName,
110cdf0e10cSrcweir                               aDummy );
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
getPropertySetInfo() const113cdf0e10cSrcweir     uno::Reference< beans::XPropertySetInfo > PropertySetHelper::getPropertySetInfo() const
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         // we're a stealth property set
116cdf0e10cSrcweir         return uno::Reference< beans::XPropertySetInfo >();
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
setPropertyValue(const::rtl::OUString & aPropertyName,const uno::Any & aValue)119cdf0e10cSrcweir     void PropertySetHelper::setPropertyValue( const ::rtl::OUString& aPropertyName,
120cdf0e10cSrcweir                                               const uno::Any&        aValue )
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         Callbacks aCallbacks;
123cdf0e10cSrcweir         if( !mpMap.get() ||
124cdf0e10cSrcweir             !mpMap->lookup( aPropertyName,
125cdf0e10cSrcweir                             aCallbacks ) )
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             throwUnknown( aPropertyName );
128cdf0e10cSrcweir         }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         if( aCallbacks.setter.empty() )
131cdf0e10cSrcweir             throwVeto( aPropertyName );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         aCallbacks.setter(aValue);
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
getPropertyValue(const::rtl::OUString & aPropertyName) const136cdf0e10cSrcweir     uno::Any PropertySetHelper::getPropertyValue( const ::rtl::OUString& aPropertyName ) const
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         Callbacks aCallbacks;
139cdf0e10cSrcweir         if( !mpMap.get() ||
140cdf0e10cSrcweir             !mpMap->lookup( aPropertyName,
141cdf0e10cSrcweir                             aCallbacks ) )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             throwUnknown( aPropertyName );
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         if( !aCallbacks.getter.empty() )
147cdf0e10cSrcweir             return aCallbacks.getter();
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         // TODO(Q1): subtlety, empty getter method silently returns
150cdf0e10cSrcweir         // the empty any
151cdf0e10cSrcweir         return uno::Any();
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
addPropertyChangeListener(const::rtl::OUString & aPropertyName,const uno::Reference<beans::XPropertyChangeListener> &)154cdf0e10cSrcweir     void PropertySetHelper::addPropertyChangeListener( const ::rtl::OUString&                                  aPropertyName,
155cdf0e10cSrcweir                                                        const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         // check validity of property, but otherwise ignore the
158cdf0e10cSrcweir         // request
159cdf0e10cSrcweir         if( !isPropertyName( aPropertyName ) )
160cdf0e10cSrcweir             throwUnknown( aPropertyName );
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
removePropertyChangeListener(const::rtl::OUString &,const uno::Reference<beans::XPropertyChangeListener> &)163cdf0e10cSrcweir     void PropertySetHelper::removePropertyChangeListener( const ::rtl::OUString&                                  /*aPropertyName*/,
164cdf0e10cSrcweir                                                           const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
165cdf0e10cSrcweir     {
166cdf0e10cSrcweir         // ignore request, no listener added in the first place
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
addVetoableChangeListener(const::rtl::OUString & aPropertyName,const uno::Reference<beans::XVetoableChangeListener> &)169cdf0e10cSrcweir     void PropertySetHelper::addVetoableChangeListener( const ::rtl::OUString&                                  aPropertyName,
170cdf0e10cSrcweir                                                        const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         // check validity of property, but otherwise ignore the
173cdf0e10cSrcweir         // request
174cdf0e10cSrcweir         if( !isPropertyName( aPropertyName ) )
175cdf0e10cSrcweir             throwUnknown( aPropertyName );
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
removeVetoableChangeListener(const::rtl::OUString &,const uno::Reference<beans::XVetoableChangeListener> &)178cdf0e10cSrcweir     void PropertySetHelper::removeVetoableChangeListener( const ::rtl::OUString&                                  /*aPropertyName*/,
179cdf0e10cSrcweir                                                           const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         // ignore request, no listener added in the first place
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir }
184