Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <canvas/propertysethelper.hxx>
22 :
23 : using namespace ::com::sun::star;
24 :
25 : namespace canvas
26 : {
27 : namespace
28 : {
29 0 : void throwUnknown( const ::rtl::OUString& aPropertyName )
30 : {
31 : throw beans::UnknownPropertyException(
32 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PropertySetHelper: property " )) +
33 0 : aPropertyName +
34 0 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " not found." )),
35 : uno::Reference< uno::XInterface >()
36 0 : );
37 : }
38 :
39 0 : void throwVeto( const ::rtl::OUString& aPropertyName )
40 : {
41 : throw beans::PropertyVetoException(
42 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PropertySetHelper: property " )) +
43 0 : aPropertyName +
44 0 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " access was vetoed." )),
45 0 : uno::Reference< uno::XInterface >() );
46 : }
47 :
48 : struct EntryComparator
49 : {
50 0 : bool operator()( const PropertySetHelper::MapType::MapEntry& rLHS,
51 : const PropertySetHelper::MapType::MapEntry& rRHS )
52 : {
53 : return strcmp( rLHS.maKey,
54 0 : rRHS.maKey ) < 0;
55 : }
56 : };
57 : }
58 :
59 0 : PropertySetHelper::PropertySetHelper() :
60 : mpMap(),
61 0 : maMapEntries()
62 : {
63 0 : }
64 :
65 0 : void PropertySetHelper::initProperties( const InputMap& rMap )
66 : {
67 0 : mpMap.reset();
68 0 : maMapEntries = rMap;
69 :
70 : std::sort( maMapEntries.begin(),
71 : maMapEntries.end(),
72 0 : EntryComparator() );
73 :
74 0 : if( !maMapEntries.empty() )
75 0 : mpMap.reset( new MapType(&maMapEntries[0],
76 0 : maMapEntries.size(),
77 0 : true) );
78 0 : }
79 :
80 0 : void PropertySetHelper::addProperties( const InputMap& rMap )
81 : {
82 0 : InputMap aMerged( getPropertyMap() );
83 : aMerged.insert( aMerged.end(),
84 : rMap.begin(),
85 0 : rMap.end() );
86 :
87 0 : initProperties( aMerged );
88 0 : }
89 :
90 0 : bool PropertySetHelper::isPropertyName( const ::rtl::OUString& aPropertyName ) const
91 : {
92 0 : if( !mpMap.get() )
93 0 : return false;
94 :
95 0 : Callbacks aDummy;
96 : return mpMap->lookup( aPropertyName,
97 0 : aDummy );
98 : }
99 :
100 0 : uno::Reference< beans::XPropertySetInfo > PropertySetHelper::getPropertySetInfo() const
101 : {
102 : // we're a stealth property set
103 0 : return uno::Reference< beans::XPropertySetInfo >();
104 : }
105 :
106 0 : void PropertySetHelper::setPropertyValue( const ::rtl::OUString& aPropertyName,
107 : const uno::Any& aValue )
108 : {
109 0 : Callbacks aCallbacks;
110 0 : if( !mpMap.get() ||
111 : !mpMap->lookup( aPropertyName,
112 0 : aCallbacks ) )
113 : {
114 0 : throwUnknown( aPropertyName );
115 : }
116 :
117 0 : if( aCallbacks.setter.empty() )
118 0 : throwVeto( aPropertyName );
119 :
120 0 : aCallbacks.setter(aValue);
121 0 : }
122 :
123 0 : uno::Any PropertySetHelper::getPropertyValue( const ::rtl::OUString& aPropertyName ) const
124 : {
125 0 : Callbacks aCallbacks;
126 0 : if( !mpMap.get() ||
127 : !mpMap->lookup( aPropertyName,
128 0 : aCallbacks ) )
129 : {
130 0 : throwUnknown( aPropertyName );
131 : }
132 :
133 0 : if( !aCallbacks.getter.empty() )
134 0 : return aCallbacks.getter();
135 :
136 : // TODO(Q1): subtlety, empty getter method silently returns
137 : // the empty any
138 0 : return uno::Any();
139 : }
140 :
141 0 : void PropertySetHelper::addPropertyChangeListener( const ::rtl::OUString& aPropertyName,
142 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
143 : {
144 : // check validity of property, but otherwise ignore the
145 : // request
146 0 : if( !isPropertyName( aPropertyName ) )
147 0 : throwUnknown( aPropertyName );
148 0 : }
149 :
150 0 : void PropertySetHelper::removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/,
151 : const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
152 : {
153 : // ignore request, no listener added in the first place
154 0 : }
155 :
156 0 : void PropertySetHelper::addVetoableChangeListener( const ::rtl::OUString& aPropertyName,
157 : const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
158 : {
159 : // check validity of property, but otherwise ignore the
160 : // request
161 0 : if( !isPropertyName( aPropertyName ) )
162 0 : throwUnknown( aPropertyName );
163 0 : }
164 :
165 0 : void PropertySetHelper::removeVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/,
166 : const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
167 : {
168 : // ignore request, no listener added in the first place
169 0 : }
170 : }
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|