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