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/diagnose_ex.h>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::form::binding
;
42 OReferenceValueComponent::OReferenceValueComponent( const Reference
< XComponentContext
>& _rxFactory
, const OUString
& _rUnoControlModelTypeName
, const OUString
& _rDefault
)
43 :OBoundControlModel( _rxFactory
, _rUnoControlModelTypeName
, _rDefault
, false, true, true )
44 ,m_eDefaultChecked( TRISTATE_FALSE
)
49 OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent
* _pOriginal
, const Reference
< XComponentContext
>& _rxFactory
)
50 :OBoundControlModel( _pOriginal
, _rxFactory
)
52 m_sReferenceValue
= _pOriginal
->m_sReferenceValue
;
53 m_sNoCheckReferenceValue
= _pOriginal
->m_sNoCheckReferenceValue
;
54 m_eDefaultChecked
= _pOriginal
->m_eDefaultChecked
;
56 calculateExternalValueType();
60 OReferenceValueComponent::~OReferenceValueComponent()
65 void OReferenceValueComponent::setReferenceValue( const OUString
& _rRefValue
)
67 m_sReferenceValue
= _rRefValue
;
68 calculateExternalValueType();
72 void SAL_CALL
OReferenceValueComponent::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
76 case PROPERTY_ID_REFVALUE
: _rValue
<<= m_sReferenceValue
; break;
77 case PROPERTY_ID_DEFAULT_STATE
: _rValue
<<= static_cast<sal_Int16
>(m_eDefaultChecked
); break;
79 case PROPERTY_ID_UNCHECKED_REFVALUE
:
80 _rValue
<<= m_sNoCheckReferenceValue
;
84 OBoundControlModel::getFastPropertyValue( _rValue
, _nHandle
);
89 void SAL_CALL
OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle
, const Any
& _rValue
)
93 case PROPERTY_ID_REFVALUE
:
94 OSL_VERIFY( _rValue
>>= m_sReferenceValue
);
95 calculateExternalValueType();
98 case PROPERTY_ID_UNCHECKED_REFVALUE
:
99 OSL_VERIFY( _rValue
>>= m_sNoCheckReferenceValue
);
102 case PROPERTY_ID_DEFAULT_STATE
:
104 sal_Int16 nDefaultChecked
;
105 if (!(_rValue
>>= nDefaultChecked
) || nDefaultChecked
< 0
106 || nDefaultChecked
> 2)
108 throw css::lang::IllegalArgumentException(
109 ("DefaultState property value must be a SHORT in the range"
111 css::uno::Reference
<css::uno::XInterface
>(), -1);
113 m_eDefaultChecked
= static_cast<ToggleState
>(nDefaultChecked
);
119 OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle
, _rValue
);
124 sal_Bool SAL_CALL
OReferenceValueComponent::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
)
126 bool bModified
= false;
129 case PROPERTY_ID_REFVALUE
:
130 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_sReferenceValue
);
133 case PROPERTY_ID_UNCHECKED_REFVALUE
:
134 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, m_sNoCheckReferenceValue
);
137 case PROPERTY_ID_DEFAULT_STATE
:
138 bModified
= tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, static_cast<sal_Int16
>(m_eDefaultChecked
) );
142 bModified
= OBoundControlModel::convertFastPropertyValue( _rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
149 Any
OReferenceValueComponent::getDefaultForReset() const
151 return makeAny( static_cast<sal_Int16
>(m_eDefaultChecked
) );
155 void OReferenceValueComponent::describeFixedProperties( Sequence
< Property
>& _rProps
) const
157 BEGIN_DESCRIBE_PROPERTIES( 3, OBoundControlModel
)
158 DECL_PROP1( REFVALUE
, OUString
, BOUND
);
159 DECL_PROP1( DEFAULT_STATE
, sal_Int16
, BOUND
);
160 DECL_PROP1( UNCHECKED_REFVALUE
, OUString
, BOUND
);
161 END_DESCRIBE_PROPERTIES();
165 Sequence
< Type
> OReferenceValueComponent::getSupportedBindingTypes()
167 ::std::vector
< Type
> aTypes
;
169 if ( !m_sReferenceValue
.isEmpty() )
170 aTypes
.push_back( cppu::UnoType
<OUString
>::get() );
172 aTypes
.push_back( cppu::UnoType
<sal_Bool
>::get() );
174 return comphelper::containerToSequence(aTypes
);
178 Any
OReferenceValueComponent::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
180 sal_Int16 nState
= TRISTATE_INDET
;
182 bool bExternalState
= false;
183 OUString sExternalValue
;
184 if ( _rExternalValue
>>= bExternalState
)
186 nState
= ::sal::static_int_cast
< sal_Int16
>( bExternalState
? TRISTATE_TRUE
: TRISTATE_FALSE
);
188 else if ( _rExternalValue
>>= sExternalValue
)
190 if ( sExternalValue
== m_sReferenceValue
)
191 nState
= TRISTATE_TRUE
;
194 if ( sExternalValue
== m_sNoCheckReferenceValue
)
195 nState
= TRISTATE_FALSE
;
197 nState
= TRISTATE_INDET
;
200 else if ( !_rExternalValue
.hasValue() )
202 nState
= TRISTATE_INDET
;
206 OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
209 return makeAny( nState
);
213 Any
OReferenceValueComponent::translateControlValueToExternalValue( ) const
219 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
220 sal_Int16 nControlValue
= TRISTATE_INDET
;
221 aControlValue
>>= nControlValue
;
223 bool bBooleanExchange
= getExternalValueType().getTypeClass() == TypeClass_BOOLEAN
;
224 bool bStringExchange
= getExternalValueType().getTypeClass() == TypeClass_STRING
;
225 OSL_ENSURE( bBooleanExchange
|| bStringExchange
,
226 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
228 switch( nControlValue
)
231 if ( bBooleanExchange
)
233 aExternalValue
<<= true;
235 else if ( bStringExchange
)
237 aExternalValue
<<= m_sReferenceValue
;
242 if ( bBooleanExchange
)
244 aExternalValue
<<= false;
246 else if ( bStringExchange
)
248 aExternalValue
<<= m_sNoCheckReferenceValue
;
253 catch( const Exception
& )
255 TOOLS_WARN_EXCEPTION( "forms.component", "OReferenceValueComponent::translateControlValueToExternalValue" );
258 return aExternalValue
;
262 Any
OReferenceValueComponent::translateControlValueToValidatableValue( ) const
264 if ( !m_xAggregateSet
.is() )
267 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
268 sal_Int16 nControlValue
= TRISTATE_INDET
;
269 aControlValue
>>= nControlValue
;
271 Any aValidatableValue
;
272 switch ( nControlValue
)
275 aValidatableValue
<<= true;
278 aValidatableValue
<<= false;
281 return aValidatableValue
;
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */