1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*24acc546SAndrew Rist  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*24acc546SAndrew Rist  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19*24acc546SAndrew Rist  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir #include "refvaluecomponent.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** === begin UNO includes === **/
29cdf0e10cSrcweir /** === end UNO includes === **/
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <tools/diagnose_ex.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <list>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //........................................................................
36cdf0e10cSrcweir namespace frm
37cdf0e10cSrcweir {
38cdf0e10cSrcweir //........................................................................
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
41cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
42cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
43cdf0e10cSrcweir     using namespace ::com::sun::star::form::binding;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     //====================================================================
46cdf0e10cSrcweir 	//=
47cdf0e10cSrcweir 	//====================================================================
48cdf0e10cSrcweir 	//--------------------------------------------------------------------
OReferenceValueComponent(const Reference<XMultiServiceFactory> & _rxFactory,const::rtl::OUString & _rUnoControlModelTypeName,const::rtl::OUString & _rDefault,sal_Bool _bSupportNoCheckRefValue)49cdf0e10cSrcweir 	OReferenceValueComponent::OReferenceValueComponent( const Reference< XMultiServiceFactory>& _rxFactory, const ::rtl::OUString& _rUnoControlModelTypeName, const ::rtl::OUString& _rDefault, sal_Bool _bSupportNoCheckRefValue )
50cdf0e10cSrcweir         :OBoundControlModel( _rxFactory, _rUnoControlModelTypeName, _rDefault, sal_False, sal_True, sal_True )
51cdf0e10cSrcweir 	    ,m_eDefaultChecked( STATE_NOCHECK )
52cdf0e10cSrcweir         ,m_bSupportSecondRefValue( _bSupportNoCheckRefValue )
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	//--------------------------------------------------------------------
OReferenceValueComponent(const OReferenceValueComponent * _pOriginal,const Reference<XMultiServiceFactory> & _rxFactory)57cdf0e10cSrcweir 	OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent* _pOriginal, const	Reference< XMultiServiceFactory>& _rxFactory )
58cdf0e10cSrcweir         :OBoundControlModel( _pOriginal, _rxFactory )
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         m_sReferenceValue           = _pOriginal->m_sReferenceValue;
61cdf0e10cSrcweir         m_sNoCheckReferenceValue    = _pOriginal->m_sNoCheckReferenceValue;
62cdf0e10cSrcweir         m_eDefaultChecked           = _pOriginal->m_eDefaultChecked;
63cdf0e10cSrcweir         m_bSupportSecondRefValue    = _pOriginal->m_bSupportSecondRefValue;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         calculateExternalValueType();
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	//--------------------------------------------------------------------
~OReferenceValueComponent()69cdf0e10cSrcweir     OReferenceValueComponent::~OReferenceValueComponent()
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     //--------------------------------------------------------------------
setReferenceValue(const::rtl::OUString & _rRefValue)74cdf0e10cSrcweir     void OReferenceValueComponent::setReferenceValue( const ::rtl::OUString& _rRefValue )
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         m_sReferenceValue = _rRefValue;
77cdf0e10cSrcweir         calculateExternalValueType();
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     //--------------------------------------------------------------------
getFastPropertyValue(Any & _rValue,sal_Int32 _nHandle) const81cdf0e10cSrcweir     void SAL_CALL OReferenceValueComponent::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir 	    switch ( _nHandle )
84cdf0e10cSrcweir 	    {
85cdf0e10cSrcweir         case PROPERTY_ID_REFVALUE:          _rValue <<= m_sReferenceValue; break;
86cdf0e10cSrcweir 		case PROPERTY_ID_DEFAULT_STATE:    _rValue <<= (sal_Int16)m_eDefaultChecked; break;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         case PROPERTY_ID_UNCHECKED_REFVALUE:
89cdf0e10cSrcweir             OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::getFastPropertyValue: not supported!" );
90cdf0e10cSrcweir             _rValue <<= m_sNoCheckReferenceValue;
91cdf0e10cSrcweir             break;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         default:
94cdf0e10cSrcweir             OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     //--------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle,const Any & _rValue)99cdf0e10cSrcweir     void SAL_CALL OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception)
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         switch ( _nHandle )
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir         case PROPERTY_ID_REFVALUE :
104cdf0e10cSrcweir             OSL_VERIFY( _rValue >>= m_sReferenceValue );
105cdf0e10cSrcweir             calculateExternalValueType();
106cdf0e10cSrcweir             break;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         case PROPERTY_ID_UNCHECKED_REFVALUE:
109cdf0e10cSrcweir             OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::setFastPropertyValue_NoBroadcast: not supported!" );
110cdf0e10cSrcweir             OSL_VERIFY( _rValue >>= m_sNoCheckReferenceValue );
111cdf0e10cSrcweir             break;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         case PROPERTY_ID_DEFAULT_STATE:
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             sal_Int16 nDefaultChecked( (sal_Int16)STATE_NOCHECK );
116cdf0e10cSrcweir 			OSL_VERIFY( _rValue >>= nDefaultChecked );
117cdf0e10cSrcweir             m_eDefaultChecked = (ToggleState)nDefaultChecked;
118cdf0e10cSrcweir 			resetNoBroadcast();
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir 		break;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         default:
123cdf0e10cSrcweir             OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     //--------------------------------------------------------------------
convertFastPropertyValue(Any & _rConvertedValue,Any & _rOldValue,sal_Int32 _nHandle,const Any & _rValue)128cdf0e10cSrcweir     sal_Bool SAL_CALL OReferenceValueComponent::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         sal_Bool bModified = sal_False;
131cdf0e10cSrcweir 	    switch ( _nHandle )
132cdf0e10cSrcweir 	    {
133cdf0e10cSrcweir 		case PROPERTY_ID_REFVALUE:
134cdf0e10cSrcweir 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sReferenceValue );
135cdf0e10cSrcweir 			break;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         case PROPERTY_ID_UNCHECKED_REFVALUE:
138cdf0e10cSrcweir             OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::convertFastPropertyValue: not supported!" );
139cdf0e10cSrcweir 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sNoCheckReferenceValue );
140cdf0e10cSrcweir             break;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         case PROPERTY_ID_DEFAULT_STATE:
143cdf0e10cSrcweir             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_eDefaultChecked );
144cdf0e10cSrcweir             break;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         default:
147cdf0e10cSrcweir 			bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
148cdf0e10cSrcweir 			break;
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         return bModified;
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     //------------------------------------------------------------------------------
getDefaultForReset() const154cdf0e10cSrcweir     Any OReferenceValueComponent::getDefaultForReset() const
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         return makeAny( (sal_Int16)m_eDefaultChecked );
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     //--------------------------------------------------------------------
describeFixedProperties(Sequence<Property> & _rProps) const160cdf0e10cSrcweir     void OReferenceValueComponent::describeFixedProperties( Sequence< Property >& _rProps ) const
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         BEGIN_DESCRIBE_PROPERTIES( m_bSupportSecondRefValue ? 3 : 2, OBoundControlModel )
163cdf0e10cSrcweir             DECL_PROP1( REFVALUE,       ::rtl::OUString,    BOUND );
164cdf0e10cSrcweir             DECL_PROP1( DEFAULT_STATE, sal_Int16,          BOUND );
165cdf0e10cSrcweir             if ( m_bSupportSecondRefValue )
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 DECL_PROP1( UNCHECKED_REFVALUE, ::rtl::OUString,    BOUND );
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir         END_DESCRIBE_PROPERTIES();
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     //-----------------------------------------------------------------------------
getSupportedBindingTypes()173cdf0e10cSrcweir     Sequence< Type > OReferenceValueComponent::getSupportedBindingTypes()
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         ::std::list< Type > aTypes;
176cdf0e10cSrcweir         aTypes.push_back( ::getCppuType( static_cast< sal_Bool* >( NULL ) ) );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         if ( m_sReferenceValue.getLength() )
179cdf0e10cSrcweir             aTypes.push_front( ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ) );
180cdf0e10cSrcweir             // push_front, because this is the preferred type
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         Sequence< Type > aTypesRet( aTypes.size() );
183cdf0e10cSrcweir         ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
184cdf0e10cSrcweir         return aTypesRet;
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     //-----------------------------------------------------------------------------
translateExternalValueToControlValue(const Any & _rExternalValue) const188cdf0e10cSrcweir     Any OReferenceValueComponent::translateExternalValueToControlValue( const Any& _rExternalValue ) const
189cdf0e10cSrcweir     {
190cdf0e10cSrcweir         sal_Int16 nState = STATE_DONTKNOW;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         sal_Bool bExternalState = sal_False;
193cdf0e10cSrcweir         ::rtl::OUString sExternalValue;
194cdf0e10cSrcweir         if ( _rExternalValue >>= bExternalState )
195cdf0e10cSrcweir         {
196cdf0e10cSrcweir             nState = ::sal::static_int_cast< sal_Int16 >( bExternalState ? STATE_CHECK : STATE_NOCHECK );
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir         else if ( _rExternalValue >>= sExternalValue )
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             if ( sExternalValue == m_sReferenceValue )
201cdf0e10cSrcweir                 nState = STATE_CHECK;
202cdf0e10cSrcweir             else
203cdf0e10cSrcweir             {
204cdf0e10cSrcweir                 if ( !m_bSupportSecondRefValue || ( sExternalValue == m_sNoCheckReferenceValue ) )
205cdf0e10cSrcweir                     nState = STATE_NOCHECK;
206cdf0e10cSrcweir                 else
207cdf0e10cSrcweir                     nState = STATE_DONTKNOW;
208cdf0e10cSrcweir             }
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir         else if ( !_rExternalValue.hasValue() )
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             nState = STATE_DONTKNOW;
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir         else
215cdf0e10cSrcweir         {
216cdf0e10cSrcweir             OSL_ENSURE( false, "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir         return makeAny( nState );
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     //-----------------------------------------------------------------------------
translateControlValueToExternalValue() const223cdf0e10cSrcweir     Any OReferenceValueComponent::translateControlValueToExternalValue( ) const
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         Any aExternalValue;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         try
228cdf0e10cSrcweir         {
229cdf0e10cSrcweir             Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
230cdf0e10cSrcweir             sal_Int16 nControlValue = STATE_DONTKNOW;
231cdf0e10cSrcweir 	        aControlValue >>= nControlValue;
232cdf0e10cSrcweir 
233cdf0e10cSrcweir             bool bBooleanExchange = getExternalValueType().getTypeClass() == TypeClass_BOOLEAN;
234cdf0e10cSrcweir             bool bStringExchange = getExternalValueType().getTypeClass() == TypeClass_STRING;
235cdf0e10cSrcweir             OSL_ENSURE( bBooleanExchange || bStringExchange,
236cdf0e10cSrcweir                 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
237cdf0e10cSrcweir 
238cdf0e10cSrcweir             switch( nControlValue )
239cdf0e10cSrcweir 	        {
240cdf0e10cSrcweir 		    case STATE_CHECK:
241cdf0e10cSrcweir                 if ( bBooleanExchange )
242cdf0e10cSrcweir                 {
243cdf0e10cSrcweir 			        aExternalValue <<= (sal_Bool)sal_True;
244cdf0e10cSrcweir                 }
245cdf0e10cSrcweir                 else if ( bStringExchange )
246cdf0e10cSrcweir                 {
247cdf0e10cSrcweir                     aExternalValue <<= m_sReferenceValue;
248cdf0e10cSrcweir                 }
249cdf0e10cSrcweir 			    break;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir             case STATE_NOCHECK:
252cdf0e10cSrcweir                 if ( bBooleanExchange )
253cdf0e10cSrcweir                 {
254cdf0e10cSrcweir 			        aExternalValue <<= (sal_Bool)sal_False;
255cdf0e10cSrcweir                 }
256cdf0e10cSrcweir                 else if ( bStringExchange )
257cdf0e10cSrcweir                 {
258cdf0e10cSrcweir                     aExternalValue <<= m_bSupportSecondRefValue ? m_sNoCheckReferenceValue : ::rtl::OUString();
259cdf0e10cSrcweir                 }
260cdf0e10cSrcweir 			    break;
261cdf0e10cSrcweir 	        }
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         catch( const Exception& )
264cdf0e10cSrcweir         {
265cdf0e10cSrcweir         	OSL_ENSURE( sal_False, "OReferenceValueComponent::translateControlValueToExternalValue: caught an exception!" );
266cdf0e10cSrcweir         }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir         return aExternalValue;
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     //-----------------------------------------------------------------------------
translateControlValueToValidatableValue() const272cdf0e10cSrcweir     Any OReferenceValueComponent::translateControlValueToValidatableValue( ) const
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         if ( !m_xAggregateSet.is() )
275cdf0e10cSrcweir             return Any();
276cdf0e10cSrcweir 
277cdf0e10cSrcweir         Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
278cdf0e10cSrcweir         sal_Int16 nControlValue = STATE_DONTKNOW;
279cdf0e10cSrcweir 	    aControlValue >>= nControlValue;
280cdf0e10cSrcweir 
281cdf0e10cSrcweir         Any aValidatableValue;
282cdf0e10cSrcweir         switch ( nControlValue )
283cdf0e10cSrcweir         {
284cdf0e10cSrcweir         case STATE_CHECK:
285cdf0e10cSrcweir             aValidatableValue <<= (sal_Bool)sal_True;
286cdf0e10cSrcweir             break;
287cdf0e10cSrcweir         case STATE_NOCHECK:
288cdf0e10cSrcweir             aValidatableValue <<= (sal_Bool)sal_False;
289cdf0e10cSrcweir             break;
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir         return aValidatableValue;
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir //........................................................................
295cdf0e10cSrcweir } // namespace frm
296cdf0e10cSrcweir //........................................................................
297cdf0e10cSrcweir 
298