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 5 : OReferenceValueComponent::OReferenceValueComponent( const Reference< XMultiServiceFactory>& _rxFactory, const ::rtl::OUString& _rUnoControlModelTypeName, const ::rtl::OUString& _rDefault, sal_Bool _bSupportNoCheckRefValue )
41 : :OBoundControlModel( _rxFactory, _rUnoControlModelTypeName, _rDefault, sal_False, sal_True, sal_True )
42 : ,m_eDefaultChecked( STATE_NOCHECK )
43 5 : ,m_bSupportSecondRefValue( _bSupportNoCheckRefValue )
44 : {
45 5 : }
46 :
47 : //--------------------------------------------------------------------
48 0 : OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent* _pOriginal, const Reference< XMultiServiceFactory>& _rxFactory )
49 0 : :OBoundControlModel( _pOriginal, _rxFactory )
50 : {
51 0 : m_sReferenceValue = _pOriginal->m_sReferenceValue;
52 0 : m_sNoCheckReferenceValue = _pOriginal->m_sNoCheckReferenceValue;
53 0 : m_eDefaultChecked = _pOriginal->m_eDefaultChecked;
54 0 : m_bSupportSecondRefValue = _pOriginal->m_bSupportSecondRefValue;
55 :
56 0 : calculateExternalValueType();
57 0 : }
58 :
59 : //--------------------------------------------------------------------
60 1 : OReferenceValueComponent::~OReferenceValueComponent()
61 : {
62 1 : }
63 :
64 : //--------------------------------------------------------------------
65 0 : void OReferenceValueComponent::setReferenceValue( const ::rtl::OUString& _rRefValue )
66 : {
67 0 : m_sReferenceValue = _rRefValue;
68 0 : calculateExternalValueType();
69 0 : }
70 :
71 : //--------------------------------------------------------------------
72 88 : void SAL_CALL OReferenceValueComponent::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
73 : {
74 88 : switch ( _nHandle )
75 : {
76 0 : case PROPERTY_ID_REFVALUE: _rValue <<= m_sReferenceValue; break;
77 0 : 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 0 : _rValue <<= m_sNoCheckReferenceValue;
82 0 : break;
83 :
84 : default:
85 88 : OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
86 : }
87 88 : }
88 :
89 : //--------------------------------------------------------------------
90 12 : void SAL_CALL OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception)
91 : {
92 12 : switch ( _nHandle )
93 : {
94 : case PROPERTY_ID_REFVALUE :
95 4 : OSL_VERIFY( _rValue >>= m_sReferenceValue );
96 4 : calculateExternalValueType();
97 4 : break;
98 :
99 : case PROPERTY_ID_UNCHECKED_REFVALUE:
100 : OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::setFastPropertyValue_NoBroadcast: not supported!" );
101 0 : OSL_VERIFY( _rValue >>= m_sNoCheckReferenceValue );
102 0 : break;
103 :
104 : case PROPERTY_ID_DEFAULT_STATE:
105 : {
106 2 : sal_Int16 nDefaultChecked( (sal_Int16)STATE_NOCHECK );
107 2 : OSL_VERIFY( _rValue >>= nDefaultChecked );
108 2 : m_eDefaultChecked = (ToggleState)nDefaultChecked;
109 2 : resetNoBroadcast();
110 : }
111 2 : break;
112 :
113 : default:
114 6 : OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
115 : }
116 12 : }
117 :
118 : //--------------------------------------------------------------------
119 18 : sal_Bool SAL_CALL OReferenceValueComponent::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
120 : {
121 18 : sal_Bool bModified = sal_False;
122 18 : switch ( _nHandle )
123 : {
124 : case PROPERTY_ID_REFVALUE:
125 4 : bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sReferenceValue );
126 4 : break;
127 :
128 : case PROPERTY_ID_UNCHECKED_REFVALUE:
129 : OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::convertFastPropertyValue: not supported!" );
130 0 : bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sNoCheckReferenceValue );
131 0 : break;
132 :
133 : case PROPERTY_ID_DEFAULT_STATE:
134 5 : bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_eDefaultChecked );
135 5 : break;
136 :
137 : default:
138 9 : bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
139 9 : break;
140 : }
141 18 : return bModified;
142 : }
143 :
144 : //------------------------------------------------------------------------------
145 2 : Any OReferenceValueComponent::getDefaultForReset() const
146 : {
147 2 : return makeAny( (sal_Int16)m_eDefaultChecked );
148 : }
149 :
150 : //--------------------------------------------------------------------
151 5 : void OReferenceValueComponent::describeFixedProperties( Sequence< Property >& _rProps ) const
152 : {
153 5 : BEGIN_DESCRIBE_PROPERTIES( m_bSupportSecondRefValue ? 3 : 2, OBoundControlModel )
154 5 : DECL_PROP1( REFVALUE, ::rtl::OUString, BOUND );
155 5 : DECL_PROP1( DEFAULT_STATE, sal_Int16, BOUND );
156 5 : if ( m_bSupportSecondRefValue )
157 : {
158 5 : DECL_PROP1( UNCHECKED_REFVALUE, ::rtl::OUString, BOUND );
159 : }
160 : END_DESCRIBE_PROPERTIES();
161 5 : }
162 :
163 : //-----------------------------------------------------------------------------
164 9 : Sequence< Type > OReferenceValueComponent::getSupportedBindingTypes()
165 : {
166 9 : ::std::list< Type > aTypes;
167 9 : aTypes.push_back( ::getCppuType( static_cast< sal_Bool* >( NULL ) ) );
168 :
169 9 : if ( !m_sReferenceValue.isEmpty() )
170 7 : aTypes.push_front( ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ) );
171 : // push_front, because this is the preferred type
172 :
173 9 : Sequence< Type > aTypesRet( aTypes.size() );
174 9 : ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
175 9 : return aTypesRet;
176 : }
177 :
178 : //-----------------------------------------------------------------------------
179 4 : Any OReferenceValueComponent::translateExternalValueToControlValue( const Any& _rExternalValue ) const
180 : {
181 4 : sal_Int16 nState = STATE_DONTKNOW;
182 :
183 4 : sal_Bool bExternalState = sal_False;
184 4 : ::rtl::OUString sExternalValue;
185 4 : if ( _rExternalValue >>= bExternalState )
186 : {
187 1 : nState = ::sal::static_int_cast< sal_Int16 >( bExternalState ? STATE_CHECK : STATE_NOCHECK );
188 : }
189 3 : else if ( _rExternalValue >>= sExternalValue )
190 : {
191 3 : if ( sExternalValue == m_sReferenceValue )
192 0 : nState = STATE_CHECK;
193 : else
194 : {
195 3 : if ( !m_bSupportSecondRefValue || ( sExternalValue == m_sNoCheckReferenceValue ) )
196 0 : nState = STATE_NOCHECK;
197 : else
198 3 : nState = STATE_DONTKNOW;
199 : }
200 : }
201 0 : else if ( !_rExternalValue.hasValue() )
202 : {
203 0 : nState = STATE_DONTKNOW;
204 : }
205 : else
206 : {
207 : OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
208 : }
209 :
210 4 : 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 = STATE_DONTKNOW;
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 STATE_CHECK:
232 0 : if ( bBooleanExchange )
233 : {
234 0 : aExternalValue <<= (sal_Bool)sal_True;
235 : }
236 0 : else if ( bStringExchange )
237 : {
238 0 : aExternalValue <<= m_sReferenceValue;
239 : }
240 0 : break;
241 :
242 : case STATE_NOCHECK:
243 0 : if ( bBooleanExchange )
244 : {
245 0 : aExternalValue <<= (sal_Bool)sal_False;
246 : }
247 0 : else if ( bStringExchange )
248 : {
249 0 : aExternalValue <<= (m_bSupportSecondRefValue ? m_sNoCheckReferenceValue : ::rtl::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 0 : Any OReferenceValueComponent::translateControlValueToValidatableValue( ) const
264 : {
265 0 : if ( !m_xAggregateSet.is() )
266 0 : return Any();
267 :
268 0 : Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
269 0 : sal_Int16 nControlValue = STATE_DONTKNOW;
270 0 : aControlValue >>= nControlValue;
271 :
272 0 : Any aValidatableValue;
273 0 : switch ( nControlValue )
274 : {
275 : case STATE_CHECK:
276 0 : aValidatableValue <<= (sal_Bool)sal_True;
277 0 : break;
278 : case STATE_NOCHECK:
279 0 : aValidatableValue <<= (sal_Bool)sal_False;
280 0 : break;
281 : }
282 0 : return aValidatableValue;
283 : }
284 :
285 : //........................................................................
286 : } // namespace frm
287 : //........................................................................
288 :
289 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|