1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "refvaluecomponent.hxx"
21 #include <property.hxx>
23 #include <comphelper/property.hxx>
24 #include <tools/debug.hxx>
25 #include <comphelper/diagnose_ex.hxx>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::form::binding
;
45 OReferenceValueComponent::OReferenceValueComponent( const Reference
< XComponentContext
>& _rxFactory
, const OUString
& _rUnoControlModelTypeName
, const OUString
& _rDefault
)
46 :OBoundControlModel( _rxFactory
, _rUnoControlModelTypeName
, _rDefault
, false, true, true )
47 ,m_eDefaultChecked( TRISTATE_FALSE
)
52 OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent
* _pOriginal
, const Reference
< XComponentContext
>& _rxFactory
)
53 :OBoundControlModel( _pOriginal
, _rxFactory
)
55 m_sReferenceValue
= _pOriginal
->m_sReferenceValue
;
56 m_sNoCheckReferenceValue
= _pOriginal
->m_sNoCheckReferenceValue
;
57 m_eDefaultChecked
= _pOriginal
->m_eDefaultChecked
;
59 calculateExternalValueType();
63 OReferenceValueComponent::~OReferenceValueComponent()
68 void OReferenceValueComponent::setReferenceValue( const OUString
& _rRefValue
)
70 m_sReferenceValue
= _rRefValue
;
71 calculateExternalValueType();
75 void SAL_CALL
OReferenceValueComponent::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
79 case PROPERTY_ID_REFVALUE
: _rValue
<<= m_sReferenceValue
; break;
80 case PROPERTY_ID_DEFAULT_STATE
: _rValue
<<= static_cast<sal_Int16
>(m_eDefaultChecked
); break;
82 case PROPERTY_ID_UNCHECKED_REFVALUE
:
83 _rValue
<<= m_sNoCheckReferenceValue
;
87 OBoundControlModel::getFastPropertyValue( _rValue
, _nHandle
);
92 void SAL_CALL
OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle
, const Any
& _rValue
)
96 case PROPERTY_ID_REFVALUE
:
97 OSL_VERIFY( _rValue
>>= m_sReferenceValue
);
98 calculateExternalValueType();
101 case PROPERTY_ID_UNCHECKED_REFVALUE
:
102 OSL_VERIFY( _rValue
>>= m_sNoCheckReferenceValue
);
105 case PROPERTY_ID_DEFAULT_STATE
:
107 sal_Int16 nDefaultChecked
;
108 if (!(_rValue
>>= nDefaultChecked
) || nDefaultChecked
< 0
109 || nDefaultChecked
> 2)
111 throw css::lang::IllegalArgumentException(
112 ("DefaultState property value must be a SHORT in the range"
114 css::uno::Reference
<css::uno::XInterface
>(), -1);
116 m_eDefaultChecked
= static_cast<ToggleState
>(nDefaultChecked
);
122 OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle
, _rValue
);
127 sal_Bool SAL_CALL
OReferenceValueComponent::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
)
129 bool bModified
= false;
132 case PROPERTY_ID_REFVALUE
:
133 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_sReferenceValue
);
136 case PROPERTY_ID_UNCHECKED_REFVALUE
:
137 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_sNoCheckReferenceValue
);
140 case PROPERTY_ID_DEFAULT_STATE
:
141 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, static_cast<sal_Int16
>(m_eDefaultChecked
) );
145 bModified
= OBoundControlModel::convertFastPropertyValue( _rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
152 Any
OReferenceValueComponent::getDefaultForReset() const
154 return Any( static_cast<sal_Int16
>(m_eDefaultChecked
) );
158 void OReferenceValueComponent::describeFixedProperties( Sequence
< Property
>& _rProps
) const
160 OBoundControlModel::describeFixedProperties( _rProps
);
161 sal_Int32 nOldCount
= _rProps
.getLength();
162 _rProps
.realloc( nOldCount
+ 3);
163 css::beans::Property
* pProperties
= _rProps
.getArray() + nOldCount
;
164 *pProperties
++ = css::beans::Property(PROPERTY_REFVALUE
, PROPERTY_ID_REFVALUE
, cppu::UnoType
<OUString
>::get(), css::beans::PropertyAttribute::BOUND
);
165 *pProperties
++ = css::beans::Property(PROPERTY_DEFAULT_STATE
, PROPERTY_ID_DEFAULT_STATE
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
);
166 *pProperties
++ = css::beans::Property(PROPERTY_UNCHECKED_REFVALUE
, PROPERTY_ID_UNCHECKED_REFVALUE
, cppu::UnoType
<OUString
>::get(), css::beans::PropertyAttribute::BOUND
);
167 DBG_ASSERT( pProperties
== _rProps
.getArray() + _rProps
.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
171 Sequence
< Type
> OReferenceValueComponent::getSupportedBindingTypes()
173 ::std::vector
< Type
> aTypes
;
175 if ( !m_sReferenceValue
.isEmpty() )
176 aTypes
.push_back( cppu::UnoType
<OUString
>::get() );
178 aTypes
.push_back( cppu::UnoType
<sal_Bool
>::get() );
180 return comphelper::containerToSequence(aTypes
);
184 Any
OReferenceValueComponent::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
186 sal_Int16 nState
= TRISTATE_INDET
;
188 bool bExternalState
= false;
189 OUString sExternalValue
;
190 if ( _rExternalValue
>>= bExternalState
)
192 nState
= ::sal::static_int_cast
< sal_Int16
>( bExternalState
? TRISTATE_TRUE
: TRISTATE_FALSE
);
194 else if ( _rExternalValue
>>= sExternalValue
)
196 if ( sExternalValue
== m_sReferenceValue
)
197 nState
= TRISTATE_TRUE
;
200 if ( sExternalValue
== m_sNoCheckReferenceValue
)
201 nState
= TRISTATE_FALSE
;
203 nState
= TRISTATE_INDET
;
206 else if ( !_rExternalValue
.hasValue() )
208 nState
= TRISTATE_INDET
;
212 OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
215 return Any( nState
);
219 Any
OReferenceValueComponent::translateControlValueToExternalValue( ) const
225 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
226 sal_Int16 nControlValue
= TRISTATE_INDET
;
227 aControlValue
>>= nControlValue
;
229 bool bBooleanExchange
= getExternalValueType().getTypeClass() == TypeClass_BOOLEAN
;
230 bool bStringExchange
= getExternalValueType().getTypeClass() == TypeClass_STRING
;
231 OSL_ENSURE( bBooleanExchange
|| bStringExchange
,
232 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
234 switch( nControlValue
)
237 if ( bBooleanExchange
)
239 aExternalValue
<<= true;
241 else if ( bStringExchange
)
243 aExternalValue
<<= m_sReferenceValue
;
248 if ( bBooleanExchange
)
250 aExternalValue
<<= false;
252 else if ( bStringExchange
)
254 aExternalValue
<<= m_sNoCheckReferenceValue
;
259 catch( const Exception
& )
261 TOOLS_WARN_EXCEPTION( "forms.component", "OReferenceValueComponent::translateControlValueToExternalValue" );
264 return aExternalValue
;
268 Any
OReferenceValueComponent::translateControlValueToValidatableValue( ) const
270 if ( !m_xAggregateSet
.is() )
273 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
274 sal_Int16 nControlValue
= TRISTATE_INDET
;
275 aControlValue
>>= nControlValue
;
277 Any aValidatableValue
;
278 switch ( nControlValue
)
281 aValidatableValue
<<= true;
284 aValidatableValue
<<= false;
287 return aValidatableValue
;
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */