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