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
;
44 OReferenceValueComponent::OReferenceValueComponent( const Reference
< XComponentContext
>& _rxFactory
, const OUString
& _rUnoControlModelTypeName
, const OUString
& _rDefault
)
45 :OBoundControlModel( _rxFactory
, _rUnoControlModelTypeName
, _rDefault
, false, true, true )
46 ,m_eDefaultChecked( TRISTATE_FALSE
)
51 OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent
* _pOriginal
, const Reference
< XComponentContext
>& _rxFactory
)
52 :OBoundControlModel( _pOriginal
, _rxFactory
)
54 m_sReferenceValue
= _pOriginal
->m_sReferenceValue
;
55 m_sNoCheckReferenceValue
= _pOriginal
->m_sNoCheckReferenceValue
;
56 m_eDefaultChecked
= _pOriginal
->m_eDefaultChecked
;
58 calculateExternalValueType();
62 OReferenceValueComponent::~OReferenceValueComponent()
67 void OReferenceValueComponent::setReferenceValue( const OUString
& _rRefValue
)
69 m_sReferenceValue
= _rRefValue
;
70 calculateExternalValueType();
74 void SAL_CALL
OReferenceValueComponent::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
78 case PROPERTY_ID_REFVALUE
: _rValue
<<= m_sReferenceValue
; break;
79 case PROPERTY_ID_DEFAULT_STATE
: _rValue
<<= static_cast<sal_Int16
>(m_eDefaultChecked
); break;
81 case PROPERTY_ID_UNCHECKED_REFVALUE
:
82 _rValue
<<= m_sNoCheckReferenceValue
;
86 OBoundControlModel::getFastPropertyValue( _rValue
, _nHandle
);
91 void SAL_CALL
OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle
, const Any
& _rValue
)
95 case PROPERTY_ID_REFVALUE
:
96 OSL_VERIFY( _rValue
>>= m_sReferenceValue
);
97 calculateExternalValueType();
100 case PROPERTY_ID_UNCHECKED_REFVALUE
:
101 OSL_VERIFY( _rValue
>>= m_sNoCheckReferenceValue
);
104 case PROPERTY_ID_DEFAULT_STATE
:
106 sal_Int16 nDefaultChecked
;
107 if (!(_rValue
>>= nDefaultChecked
) || nDefaultChecked
< 0
108 || nDefaultChecked
> 2)
110 throw css::lang::IllegalArgumentException(
111 (u
"DefaultState property value must be a SHORT in the range"
113 css::uno::Reference
<css::uno::XInterface
>(), -1);
115 m_eDefaultChecked
= static_cast<ToggleState
>(nDefaultChecked
);
121 OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle
, _rValue
);
126 sal_Bool SAL_CALL
OReferenceValueComponent::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
)
128 bool bModified
= false;
131 case PROPERTY_ID_REFVALUE
:
132 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_sReferenceValue
);
135 case PROPERTY_ID_UNCHECKED_REFVALUE
:
136 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_sNoCheckReferenceValue
);
139 case PROPERTY_ID_DEFAULT_STATE
:
140 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, static_cast<sal_Int16
>(m_eDefaultChecked
) );
144 bModified
= OBoundControlModel::convertFastPropertyValue( _rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
151 Any
OReferenceValueComponent::getDefaultForReset() const
153 return Any( static_cast<sal_Int16
>(m_eDefaultChecked
) );
157 void OReferenceValueComponent::describeFixedProperties( Sequence
< Property
>& _rProps
) const
159 OBoundControlModel::describeFixedProperties( _rProps
);
160 sal_Int32 nOldCount
= _rProps
.getLength();
161 _rProps
.realloc( nOldCount
+ 3);
162 css::beans::Property
* pProperties
= _rProps
.getArray() + nOldCount
;
163 *pProperties
++ = css::beans::Property(PROPERTY_REFVALUE
, PROPERTY_ID_REFVALUE
, cppu::UnoType
<OUString
>::get(), css::beans::PropertyAttribute::BOUND
);
164 *pProperties
++ = css::beans::Property(PROPERTY_DEFAULT_STATE
, PROPERTY_ID_DEFAULT_STATE
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
);
165 *pProperties
++ = css::beans::Property(PROPERTY_UNCHECKED_REFVALUE
, PROPERTY_ID_UNCHECKED_REFVALUE
, cppu::UnoType
<OUString
>::get(), css::beans::PropertyAttribute::BOUND
);
166 DBG_ASSERT( pProperties
== _rProps
.getArray() + _rProps
.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
170 Sequence
< Type
> OReferenceValueComponent::getSupportedBindingTypes()
172 ::std::vector
< Type
> aTypes
;
174 if ( !m_sReferenceValue
.isEmpty() )
175 aTypes
.push_back( cppu::UnoType
<OUString
>::get() );
177 aTypes
.push_back( cppu::UnoType
<sal_Bool
>::get() );
179 return comphelper::containerToSequence(aTypes
);
183 Any
OReferenceValueComponent::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
185 sal_Int16 nState
= TRISTATE_INDET
;
187 bool bExternalState
= false;
188 OUString sExternalValue
;
189 if ( _rExternalValue
>>= bExternalState
)
191 nState
= ::sal::static_int_cast
< sal_Int16
>( bExternalState
? TRISTATE_TRUE
: TRISTATE_FALSE
);
193 else if ( _rExternalValue
>>= sExternalValue
)
195 if ( sExternalValue
== m_sReferenceValue
)
196 nState
= TRISTATE_TRUE
;
199 if ( sExternalValue
== m_sNoCheckReferenceValue
)
200 nState
= TRISTATE_FALSE
;
202 nState
= TRISTATE_INDET
;
205 else if ( !_rExternalValue
.hasValue() )
207 nState
= TRISTATE_INDET
;
211 OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
214 return Any( nState
);
218 Any
OReferenceValueComponent::translateControlValueToExternalValue( ) const
224 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
225 sal_Int16 nControlValue
= TRISTATE_INDET
;
226 aControlValue
>>= nControlValue
;
228 bool bBooleanExchange
= getExternalValueType().getTypeClass() == TypeClass_BOOLEAN
;
229 bool bStringExchange
= getExternalValueType().getTypeClass() == TypeClass_STRING
;
230 OSL_ENSURE( bBooleanExchange
|| bStringExchange
,
231 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
233 switch( nControlValue
)
236 if ( bBooleanExchange
)
238 aExternalValue
<<= true;
240 else if ( bStringExchange
)
242 aExternalValue
<<= m_sReferenceValue
;
247 if ( bBooleanExchange
)
249 aExternalValue
<<= false;
251 else if ( bStringExchange
)
253 aExternalValue
<<= m_sNoCheckReferenceValue
;
258 catch( const Exception
& )
260 TOOLS_WARN_EXCEPTION( "forms.component", "OReferenceValueComponent::translateControlValueToExternalValue" );
263 return aExternalValue
;
267 Any
OReferenceValueComponent::translateControlValueToValidatableValue( ) const
269 if ( !m_xAggregateSet
.is() )
272 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
273 sal_Int16 nControlValue
= TRISTATE_INDET
;
274 aControlValue
>>= nControlValue
;
276 Any aValidatableValue
;
277 switch ( nControlValue
)
280 aValidatableValue
<<= true;
283 aValidatableValue
<<= false;
286 return aValidatableValue
;
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */