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