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 : #include "refvaluecomponent.hxx"
21 :
22 : #include <tools/diagnose_ex.h>
23 :
24 : #include <list>
25 :
26 :
27 : namespace frm
28 : {
29 :
30 :
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star::lang;
33 : using namespace ::com::sun::star::beans;
34 : using namespace ::com::sun::star::form::binding;
35 :
36 :
37 : //=
38 :
39 :
40 84 : OReferenceValueComponent::OReferenceValueComponent( const Reference< XComponentContext >& _rxFactory, const OUString& _rUnoControlModelTypeName, const OUString& _rDefault, bool _bSupportNoCheckRefValue )
41 : :OBoundControlModel( _rxFactory, _rUnoControlModelTypeName, _rDefault, false, true, true )
42 : ,m_eDefaultChecked( TRISTATE_FALSE )
43 84 : ,m_bSupportSecondRefValue( _bSupportNoCheckRefValue )
44 : {
45 84 : }
46 :
47 :
48 4 : OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent* _pOriginal, const Reference< XComponentContext>& _rxFactory )
49 4 : :OBoundControlModel( _pOriginal, _rxFactory )
50 : {
51 4 : m_sReferenceValue = _pOriginal->m_sReferenceValue;
52 4 : m_sNoCheckReferenceValue = _pOriginal->m_sNoCheckReferenceValue;
53 4 : m_eDefaultChecked = _pOriginal->m_eDefaultChecked;
54 4 : m_bSupportSecondRefValue = _pOriginal->m_bSupportSecondRefValue;
55 :
56 4 : calculateExternalValueType();
57 4 : }
58 :
59 :
60 82 : OReferenceValueComponent::~OReferenceValueComponent()
61 : {
62 82 : }
63 :
64 :
65 4 : void OReferenceValueComponent::setReferenceValue( const OUString& _rRefValue )
66 : {
67 4 : m_sReferenceValue = _rRefValue;
68 4 : calculateExternalValueType();
69 4 : }
70 :
71 :
72 17508 : void SAL_CALL OReferenceValueComponent::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
73 : {
74 17508 : switch ( _nHandle )
75 : {
76 366 : case PROPERTY_ID_REFVALUE: _rValue <<= m_sReferenceValue; break;
77 366 : case PROPERTY_ID_DEFAULT_STATE: _rValue <<= (sal_Int16)m_eDefaultChecked; break;
78 :
79 : case PROPERTY_ID_UNCHECKED_REFVALUE:
80 : OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::getFastPropertyValue: not supported!" );
81 358 : _rValue <<= m_sNoCheckReferenceValue;
82 358 : break;
83 :
84 : default:
85 16418 : OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
86 : }
87 17508 : }
88 :
89 :
90 630 : void SAL_CALL OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception, std::exception)
91 : {
92 630 : switch ( _nHandle )
93 : {
94 : case PROPERTY_ID_REFVALUE :
95 44 : OSL_VERIFY( _rValue >>= m_sReferenceValue );
96 44 : calculateExternalValueType();
97 44 : break;
98 :
99 : case PROPERTY_ID_UNCHECKED_REFVALUE:
100 : OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::setFastPropertyValue_NoBroadcast: not supported!" );
101 44 : OSL_VERIFY( _rValue >>= m_sNoCheckReferenceValue );
102 44 : break;
103 :
104 : case PROPERTY_ID_DEFAULT_STATE:
105 : {
106 52 : sal_Int16 nDefaultChecked( (sal_Int16)TRISTATE_FALSE );
107 52 : OSL_VERIFY( _rValue >>= nDefaultChecked );
108 52 : m_eDefaultChecked = (ToggleState)nDefaultChecked;
109 52 : resetNoBroadcast();
110 : }
111 52 : break;
112 :
113 : default:
114 490 : OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
115 : }
116 630 : }
117 :
118 :
119 2644 : sal_Bool SAL_CALL OReferenceValueComponent::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
120 : {
121 2644 : bool bModified = false;
122 2644 : switch ( _nHandle )
123 : {
124 : case PROPERTY_ID_REFVALUE:
125 224 : bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sReferenceValue );
126 224 : break;
127 :
128 : case PROPERTY_ID_UNCHECKED_REFVALUE:
129 : OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::convertFastPropertyValue: not supported!" );
130 224 : bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sNoCheckReferenceValue );
131 224 : break;
132 :
133 : case PROPERTY_ID_DEFAULT_STATE:
134 246 : bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_eDefaultChecked );
135 246 : break;
136 :
137 : default:
138 1950 : bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
139 1950 : break;
140 : }
141 2644 : return bModified;
142 : }
143 :
144 :
145 62 : Any OReferenceValueComponent::getDefaultForReset() const
146 : {
147 62 : return makeAny( (sal_Int16)m_eDefaultChecked );
148 : }
149 :
150 :
151 92 : void OReferenceValueComponent::describeFixedProperties( Sequence< Property >& _rProps ) const
152 : {
153 92 : BEGIN_DESCRIBE_PROPERTIES( m_bSupportSecondRefValue ? 3 : 2, OBoundControlModel )
154 92 : DECL_PROP1( REFVALUE, OUString, BOUND );
155 92 : DECL_PROP1( DEFAULT_STATE, sal_Int16, BOUND );
156 92 : if ( m_bSupportSecondRefValue )
157 : {
158 92 : DECL_PROP1( UNCHECKED_REFVALUE, OUString, BOUND );
159 : }
160 : END_DESCRIBE_PROPERTIES();
161 92 : }
162 :
163 :
164 20 : Sequence< Type > OReferenceValueComponent::getSupportedBindingTypes()
165 : {
166 20 : ::std::list< Type > aTypes;
167 20 : aTypes.push_back( cppu::UnoType<sal_Bool>::get() );
168 :
169 20 : if ( !m_sReferenceValue.isEmpty() )
170 0 : aTypes.push_front( cppu::UnoType<OUString>::get() );
171 : // push_front, because this is the preferred type
172 :
173 20 : Sequence< Type > aTypesRet( aTypes.size() );
174 20 : ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
175 20 : return aTypesRet;
176 : }
177 :
178 :
179 10 : Any OReferenceValueComponent::translateExternalValueToControlValue( const Any& _rExternalValue ) const
180 : {
181 10 : sal_Int16 nState = TRISTATE_INDET;
182 :
183 10 : bool bExternalState = false;
184 10 : OUString sExternalValue;
185 10 : if ( _rExternalValue >>= bExternalState )
186 : {
187 2 : nState = ::sal::static_int_cast< sal_Int16 >( bExternalState ? TRISTATE_TRUE : TRISTATE_FALSE );
188 : }
189 8 : else if ( _rExternalValue >>= sExternalValue )
190 : {
191 8 : if ( sExternalValue == m_sReferenceValue )
192 0 : nState = TRISTATE_TRUE;
193 : else
194 : {
195 8 : if ( !m_bSupportSecondRefValue || ( sExternalValue == m_sNoCheckReferenceValue ) )
196 0 : nState = TRISTATE_FALSE;
197 : else
198 8 : nState = TRISTATE_INDET;
199 : }
200 : }
201 0 : else if ( !_rExternalValue.hasValue() )
202 : {
203 0 : nState = TRISTATE_INDET;
204 : }
205 : else
206 : {
207 : OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
208 : }
209 :
210 10 : return makeAny( nState );
211 : }
212 :
213 :
214 0 : Any OReferenceValueComponent::translateControlValueToExternalValue( ) const
215 : {
216 0 : Any aExternalValue;
217 :
218 : try
219 : {
220 0 : Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
221 0 : sal_Int16 nControlValue = TRISTATE_INDET;
222 0 : aControlValue >>= nControlValue;
223 :
224 0 : bool bBooleanExchange = getExternalValueType().getTypeClass() == TypeClass_BOOLEAN;
225 0 : bool bStringExchange = getExternalValueType().getTypeClass() == TypeClass_STRING;
226 : OSL_ENSURE( bBooleanExchange || bStringExchange,
227 : "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
228 :
229 0 : switch( nControlValue )
230 : {
231 : case TRISTATE_TRUE:
232 0 : if ( bBooleanExchange )
233 : {
234 0 : aExternalValue <<= true;
235 : }
236 0 : else if ( bStringExchange )
237 : {
238 0 : aExternalValue <<= m_sReferenceValue;
239 : }
240 0 : break;
241 :
242 : case TRISTATE_FALSE:
243 0 : if ( bBooleanExchange )
244 : {
245 0 : aExternalValue <<= false;
246 : }
247 0 : else if ( bStringExchange )
248 : {
249 0 : aExternalValue <<= (m_bSupportSecondRefValue ? m_sNoCheckReferenceValue : OUString());
250 : }
251 0 : break;
252 0 : }
253 : }
254 0 : catch( const Exception& )
255 : {
256 : OSL_FAIL( "OReferenceValueComponent::translateControlValueToExternalValue: caught an exception!" );
257 : }
258 :
259 0 : return aExternalValue;
260 : }
261 :
262 :
263 46 : Any OReferenceValueComponent::translateControlValueToValidatableValue( ) const
264 : {
265 46 : if ( !m_xAggregateSet.is() )
266 0 : return Any();
267 :
268 46 : Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
269 46 : sal_Int16 nControlValue = TRISTATE_INDET;
270 46 : aControlValue >>= nControlValue;
271 :
272 92 : Any aValidatableValue;
273 46 : switch ( nControlValue )
274 : {
275 : case TRISTATE_TRUE:
276 8 : aValidatableValue <<= true;
277 8 : break;
278 : case TRISTATE_FALSE:
279 16 : aValidatableValue <<= false;
280 16 : break;
281 : }
282 92 : return aValidatableValue;
283 : }
284 :
285 :
286 : } // namespace frm
287 :
288 :
289 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|