1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir #include "refvaluecomponent.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir /** === begin UNO includes === **/
33*cdf0e10cSrcweir /** === end UNO includes === **/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <list>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //........................................................................
40*cdf0e10cSrcweir namespace frm
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir //........................................................................
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
45*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
46*cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
47*cdf0e10cSrcweir     using namespace ::com::sun::star::form::binding;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     //====================================================================
50*cdf0e10cSrcweir 	//=
51*cdf0e10cSrcweir 	//====================================================================
52*cdf0e10cSrcweir 	//--------------------------------------------------------------------
53*cdf0e10cSrcweir 	OReferenceValueComponent::OReferenceValueComponent( const Reference< XMultiServiceFactory>& _rxFactory, const ::rtl::OUString& _rUnoControlModelTypeName, const ::rtl::OUString& _rDefault, sal_Bool _bSupportNoCheckRefValue )
54*cdf0e10cSrcweir         :OBoundControlModel( _rxFactory, _rUnoControlModelTypeName, _rDefault, sal_False, sal_True, sal_True )
55*cdf0e10cSrcweir 	    ,m_eDefaultChecked( STATE_NOCHECK )
56*cdf0e10cSrcweir         ,m_bSupportSecondRefValue( _bSupportNoCheckRefValue )
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	//--------------------------------------------------------------------
61*cdf0e10cSrcweir 	OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent* _pOriginal, const	Reference< XMultiServiceFactory>& _rxFactory )
62*cdf0e10cSrcweir         :OBoundControlModel( _pOriginal, _rxFactory )
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         m_sReferenceValue           = _pOriginal->m_sReferenceValue;
65*cdf0e10cSrcweir         m_sNoCheckReferenceValue    = _pOriginal->m_sNoCheckReferenceValue;
66*cdf0e10cSrcweir         m_eDefaultChecked           = _pOriginal->m_eDefaultChecked;
67*cdf0e10cSrcweir         m_bSupportSecondRefValue    = _pOriginal->m_bSupportSecondRefValue;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir         calculateExternalValueType();
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	//--------------------------------------------------------------------
73*cdf0e10cSrcweir     OReferenceValueComponent::~OReferenceValueComponent()
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     //--------------------------------------------------------------------
78*cdf0e10cSrcweir     void OReferenceValueComponent::setReferenceValue( const ::rtl::OUString& _rRefValue )
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         m_sReferenceValue = _rRefValue;
81*cdf0e10cSrcweir         calculateExternalValueType();
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     //--------------------------------------------------------------------
85*cdf0e10cSrcweir     void SAL_CALL OReferenceValueComponent::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir 	    switch ( _nHandle )
88*cdf0e10cSrcweir 	    {
89*cdf0e10cSrcweir         case PROPERTY_ID_REFVALUE:          _rValue <<= m_sReferenceValue; break;
90*cdf0e10cSrcweir 		case PROPERTY_ID_DEFAULT_STATE:    _rValue <<= (sal_Int16)m_eDefaultChecked; break;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         case PROPERTY_ID_UNCHECKED_REFVALUE:
93*cdf0e10cSrcweir             OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::getFastPropertyValue: not supported!" );
94*cdf0e10cSrcweir             _rValue <<= m_sNoCheckReferenceValue;
95*cdf0e10cSrcweir             break;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         default:
98*cdf0e10cSrcweir             OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     //--------------------------------------------------------------------
103*cdf0e10cSrcweir     void SAL_CALL OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception)
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         switch ( _nHandle )
106*cdf0e10cSrcweir         {
107*cdf0e10cSrcweir         case PROPERTY_ID_REFVALUE :
108*cdf0e10cSrcweir             OSL_VERIFY( _rValue >>= m_sReferenceValue );
109*cdf0e10cSrcweir             calculateExternalValueType();
110*cdf0e10cSrcweir             break;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         case PROPERTY_ID_UNCHECKED_REFVALUE:
113*cdf0e10cSrcweir             OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::setFastPropertyValue_NoBroadcast: not supported!" );
114*cdf0e10cSrcweir             OSL_VERIFY( _rValue >>= m_sNoCheckReferenceValue );
115*cdf0e10cSrcweir             break;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         case PROPERTY_ID_DEFAULT_STATE:
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             sal_Int16 nDefaultChecked( (sal_Int16)STATE_NOCHECK );
120*cdf0e10cSrcweir 			OSL_VERIFY( _rValue >>= nDefaultChecked );
121*cdf0e10cSrcweir             m_eDefaultChecked = (ToggleState)nDefaultChecked;
122*cdf0e10cSrcweir 			resetNoBroadcast();
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir 		break;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         default:
127*cdf0e10cSrcweir             OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     //--------------------------------------------------------------------
132*cdf0e10cSrcweir     sal_Bool SAL_CALL OReferenceValueComponent::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         sal_Bool bModified = sal_False;
135*cdf0e10cSrcweir 	    switch ( _nHandle )
136*cdf0e10cSrcweir 	    {
137*cdf0e10cSrcweir 		case PROPERTY_ID_REFVALUE:
138*cdf0e10cSrcweir 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sReferenceValue );
139*cdf0e10cSrcweir 			break;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         case PROPERTY_ID_UNCHECKED_REFVALUE:
142*cdf0e10cSrcweir             OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::convertFastPropertyValue: not supported!" );
143*cdf0e10cSrcweir 			bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sNoCheckReferenceValue );
144*cdf0e10cSrcweir             break;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         case PROPERTY_ID_DEFAULT_STATE:
147*cdf0e10cSrcweir             bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_eDefaultChecked );
148*cdf0e10cSrcweir             break;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         default:
151*cdf0e10cSrcweir 			bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
152*cdf0e10cSrcweir 			break;
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir         return bModified;
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     //------------------------------------------------------------------------------
158*cdf0e10cSrcweir     Any OReferenceValueComponent::getDefaultForReset() const
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         return makeAny( (sal_Int16)m_eDefaultChecked );
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     //--------------------------------------------------------------------
164*cdf0e10cSrcweir     void OReferenceValueComponent::describeFixedProperties( Sequence< Property >& _rProps ) const
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         BEGIN_DESCRIBE_PROPERTIES( m_bSupportSecondRefValue ? 3 : 2, OBoundControlModel )
167*cdf0e10cSrcweir             DECL_PROP1( REFVALUE,       ::rtl::OUString,    BOUND );
168*cdf0e10cSrcweir             DECL_PROP1( DEFAULT_STATE, sal_Int16,          BOUND );
169*cdf0e10cSrcweir             if ( m_bSupportSecondRefValue )
170*cdf0e10cSrcweir             {
171*cdf0e10cSrcweir                 DECL_PROP1( UNCHECKED_REFVALUE, ::rtl::OUString,    BOUND );
172*cdf0e10cSrcweir             }
173*cdf0e10cSrcweir         END_DESCRIBE_PROPERTIES();
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     //-----------------------------------------------------------------------------
177*cdf0e10cSrcweir     Sequence< Type > OReferenceValueComponent::getSupportedBindingTypes()
178*cdf0e10cSrcweir     {
179*cdf0e10cSrcweir         ::std::list< Type > aTypes;
180*cdf0e10cSrcweir         aTypes.push_back( ::getCppuType( static_cast< sal_Bool* >( NULL ) ) );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         if ( m_sReferenceValue.getLength() )
183*cdf0e10cSrcweir             aTypes.push_front( ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ) );
184*cdf0e10cSrcweir             // push_front, because this is the preferred type
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         Sequence< Type > aTypesRet( aTypes.size() );
187*cdf0e10cSrcweir         ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
188*cdf0e10cSrcweir         return aTypesRet;
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     //-----------------------------------------------------------------------------
192*cdf0e10cSrcweir     Any OReferenceValueComponent::translateExternalValueToControlValue( const Any& _rExternalValue ) const
193*cdf0e10cSrcweir     {
194*cdf0e10cSrcweir         sal_Int16 nState = STATE_DONTKNOW;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         sal_Bool bExternalState = sal_False;
197*cdf0e10cSrcweir         ::rtl::OUString sExternalValue;
198*cdf0e10cSrcweir         if ( _rExternalValue >>= bExternalState )
199*cdf0e10cSrcweir         {
200*cdf0e10cSrcweir             nState = ::sal::static_int_cast< sal_Int16 >( bExternalState ? STATE_CHECK : STATE_NOCHECK );
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir         else if ( _rExternalValue >>= sExternalValue )
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             if ( sExternalValue == m_sReferenceValue )
205*cdf0e10cSrcweir                 nState = STATE_CHECK;
206*cdf0e10cSrcweir             else
207*cdf0e10cSrcweir             {
208*cdf0e10cSrcweir                 if ( !m_bSupportSecondRefValue || ( sExternalValue == m_sNoCheckReferenceValue ) )
209*cdf0e10cSrcweir                     nState = STATE_NOCHECK;
210*cdf0e10cSrcweir                 else
211*cdf0e10cSrcweir                     nState = STATE_DONTKNOW;
212*cdf0e10cSrcweir             }
213*cdf0e10cSrcweir         }
214*cdf0e10cSrcweir         else if ( !_rExternalValue.hasValue() )
215*cdf0e10cSrcweir         {
216*cdf0e10cSrcweir             nState = STATE_DONTKNOW;
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir         else
219*cdf0e10cSrcweir         {
220*cdf0e10cSrcweir             OSL_ENSURE( false, "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         return makeAny( nState );
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     //-----------------------------------------------------------------------------
227*cdf0e10cSrcweir     Any OReferenceValueComponent::translateControlValueToExternalValue( ) const
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         Any aExternalValue;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir         try
232*cdf0e10cSrcweir         {
233*cdf0e10cSrcweir             Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
234*cdf0e10cSrcweir             sal_Int16 nControlValue = STATE_DONTKNOW;
235*cdf0e10cSrcweir 	        aControlValue >>= nControlValue;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir             bool bBooleanExchange = getExternalValueType().getTypeClass() == TypeClass_BOOLEAN;
238*cdf0e10cSrcweir             bool bStringExchange = getExternalValueType().getTypeClass() == TypeClass_STRING;
239*cdf0e10cSrcweir             OSL_ENSURE( bBooleanExchange || bStringExchange,
240*cdf0e10cSrcweir                 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir             switch( nControlValue )
243*cdf0e10cSrcweir 	        {
244*cdf0e10cSrcweir 		    case STATE_CHECK:
245*cdf0e10cSrcweir                 if ( bBooleanExchange )
246*cdf0e10cSrcweir                 {
247*cdf0e10cSrcweir 			        aExternalValue <<= (sal_Bool)sal_True;
248*cdf0e10cSrcweir                 }
249*cdf0e10cSrcweir                 else if ( bStringExchange )
250*cdf0e10cSrcweir                 {
251*cdf0e10cSrcweir                     aExternalValue <<= m_sReferenceValue;
252*cdf0e10cSrcweir                 }
253*cdf0e10cSrcweir 			    break;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir             case STATE_NOCHECK:
256*cdf0e10cSrcweir                 if ( bBooleanExchange )
257*cdf0e10cSrcweir                 {
258*cdf0e10cSrcweir 			        aExternalValue <<= (sal_Bool)sal_False;
259*cdf0e10cSrcweir                 }
260*cdf0e10cSrcweir                 else if ( bStringExchange )
261*cdf0e10cSrcweir                 {
262*cdf0e10cSrcweir                     aExternalValue <<= m_bSupportSecondRefValue ? m_sNoCheckReferenceValue : ::rtl::OUString();
263*cdf0e10cSrcweir                 }
264*cdf0e10cSrcweir 			    break;
265*cdf0e10cSrcweir 	        }
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir         catch( const Exception& )
268*cdf0e10cSrcweir         {
269*cdf0e10cSrcweir         	OSL_ENSURE( sal_False, "OReferenceValueComponent::translateControlValueToExternalValue: caught an exception!" );
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir         return aExternalValue;
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     //-----------------------------------------------------------------------------
276*cdf0e10cSrcweir     Any OReferenceValueComponent::translateControlValueToValidatableValue( ) const
277*cdf0e10cSrcweir     {
278*cdf0e10cSrcweir         if ( !m_xAggregateSet.is() )
279*cdf0e10cSrcweir             return Any();
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
282*cdf0e10cSrcweir         sal_Int16 nControlValue = STATE_DONTKNOW;
283*cdf0e10cSrcweir 	    aControlValue >>= nControlValue;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir         Any aValidatableValue;
286*cdf0e10cSrcweir         switch ( nControlValue )
287*cdf0e10cSrcweir         {
288*cdf0e10cSrcweir         case STATE_CHECK:
289*cdf0e10cSrcweir             aValidatableValue <<= (sal_Bool)sal_True;
290*cdf0e10cSrcweir             break;
291*cdf0e10cSrcweir         case STATE_NOCHECK:
292*cdf0e10cSrcweir             aValidatableValue <<= (sal_Bool)sal_False;
293*cdf0e10cSrcweir             break;
294*cdf0e10cSrcweir         }
295*cdf0e10cSrcweir         return aValidatableValue;
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir //........................................................................
299*cdf0e10cSrcweir } // namespace frm
300*cdf0e10cSrcweir //........................................................................
301*cdf0e10cSrcweir 
302