Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / tools / propertysethelper.cxx
blob477fda9ee19cc666b2caf031c153c10f8afec160
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <canvas/propertysethelper.hxx>
23 using namespace ::com::sun::star;
25 namespace canvas
27 namespace
29 void throwUnknown( const OUString& aPropertyName )
31 throw beans::UnknownPropertyException(
32 "PropertySetHelper: property " +
33 aPropertyName + " not found."
37 void throwVeto( const OUString& aPropertyName )
39 throw beans::PropertyVetoException(
40 "PropertySetHelper: property " +
41 aPropertyName + " access was vetoed." );
44 struct EntryComparator
46 bool operator()( const PropertySetHelper::MapType::MapEntry& rLHS,
47 const PropertySetHelper::MapType::MapEntry& rRHS )
49 return strcmp( rLHS.maKey,
50 rRHS.maKey ) < 0;
55 PropertySetHelper::PropertySetHelper() :
56 mpMap(),
57 maMapEntries()
61 void PropertySetHelper::initProperties( const InputMap& rMap )
63 mpMap.reset();
64 maMapEntries = rMap;
66 std::sort( maMapEntries.begin(),
67 maMapEntries.end(),
68 EntryComparator() );
70 if( !maMapEntries.empty() )
71 mpMap.reset( new MapType(&maMapEntries[0],
72 maMapEntries.size(),
73 true) );
76 void PropertySetHelper::addProperties( const InputMap& rMap )
78 InputMap aMerged( getPropertyMap() );
79 aMerged.insert( aMerged.end(),
80 rMap.begin(),
81 rMap.end() );
83 initProperties( aMerged );
86 bool PropertySetHelper::isPropertyName( const OUString& aPropertyName ) const
88 if( !mpMap.get() )
89 return false;
91 Callbacks aDummy;
92 return mpMap->lookup( aPropertyName,
93 aDummy );
96 uno::Reference< beans::XPropertySetInfo > PropertySetHelper::getPropertySetInfo() const
98 // we're a stealth property set
99 return uno::Reference< beans::XPropertySetInfo >();
102 void PropertySetHelper::setPropertyValue( const OUString& aPropertyName,
103 const uno::Any& aValue )
105 Callbacks aCallbacks;
106 if( !mpMap.get() ||
107 !mpMap->lookup( aPropertyName,
108 aCallbacks ) )
110 throwUnknown( aPropertyName );
113 if( aCallbacks.setter.empty() )
114 throwVeto( aPropertyName );
116 aCallbacks.setter(aValue);
119 uno::Any PropertySetHelper::getPropertyValue( const OUString& aPropertyName ) const
121 Callbacks aCallbacks;
122 if( !mpMap.get() ||
123 !mpMap->lookup( aPropertyName,
124 aCallbacks ) )
126 throwUnknown( aPropertyName );
129 if( !aCallbacks.getter.empty() )
130 return aCallbacks.getter();
132 // TODO(Q1): subtlety, empty getter method silently returns
133 // the empty any
134 return uno::Any();
137 void PropertySetHelper::addPropertyChangeListener( const OUString& aPropertyName,
138 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
140 // check validity of property, but otherwise ignore the
141 // request
142 if( !isPropertyName( aPropertyName ) )
143 throwUnknown( aPropertyName );
146 void PropertySetHelper::addVetoableChangeListener( const OUString& aPropertyName,
147 const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
149 // check validity of property, but otherwise ignore the
150 // request
151 if( !isPropertyName( aPropertyName ) )
152 throwUnknown( aPropertyName );
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */