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