update dev300-m58
[ooovba.git] / forms / source / component / Date.cxx
blob72daa5f0b82cff378422ba536b0c9988486fef04
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Date.cxx,v $
10 * $Revision: 1.27 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
33 #include "Date.hxx"
34 #include <tools/debug.hxx>
35 #include <tools/date.hxx>
36 #include <connectivity/dbconversion.hxx>
37 #include <com/sun/star/sdbc/DataType.hpp>
39 using namespace dbtools;
41 //.........................................................................
42 namespace frm
44 //.........................................................................
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::sdb;
49 using namespace ::com::sun::star::sdbc;
50 using namespace ::com::sun::star::sdbcx;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::util;
53 using namespace ::com::sun::star::container;
54 using namespace ::com::sun::star::form;
55 using namespace ::com::sun::star::awt;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::lang;
59 //------------------------------------------------------------------
60 ODateControl::ODateControl(const Reference<XMultiServiceFactory>& _rxFactory)
61 :OBoundControl(_rxFactory, VCL_CONTROL_DATEFIELD)
65 //------------------------------------------------------------------
66 InterfaceRef SAL_CALL ODateControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
68 return *(new ODateControl(_rxFactory));
71 //------------------------------------------------------------------------------
72 Sequence<Type> ODateControl::_getTypes()
74 return OBoundControl::_getTypes();
77 //------------------------------------------------------------------------------
78 StringSequence SAL_CALL ODateControl::getSupportedServiceNames() throw()
80 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
81 aSupported.realloc(aSupported.getLength() + 1);
83 ::rtl::OUString*pArray = aSupported.getArray();
84 pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_DATEFIELD;
85 return aSupported;
88 /*************************************************************************/
89 //------------------------------------------------------------------
90 InterfaceRef SAL_CALL ODateModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
92 return *(new ODateModel(_rxFactory));
95 //------------------------------------------------------------------------------
96 Sequence<Type> ODateModel::_getTypes()
98 return OEditBaseModel::_getTypes();
101 //------------------------------------------------------------------
102 DBG_NAME( ODateModel )
103 //------------------------------------------------------------------
104 ODateModel::ODateModel(const Reference<XMultiServiceFactory>& _rxFactory)
105 :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_DATEFIELD, FRM_SUN_CONTROL_DATEFIELD, sal_True, sal_True )
106 // use the old control name for compytibility reasons
107 ,OLimitedFormats( _rxFactory, FormComponentType::DATEFIELD )
109 DBG_CTOR( ODateModel, NULL );
111 m_nClassId = FormComponentType::DATEFIELD;
112 initValueProperty( PROPERTY_DATE, PROPERTY_ID_DATE );
114 setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_DATEFORMAT));
116 osl_incrementInterlockedCount( &m_refCount );
119 if ( m_xAggregateSet.is() )
120 m_xAggregateSet->setPropertyValue( PROPERTY_DATEMIN, makeAny( (sal_Int32)( ::Date( 1, 1, 1800 ).GetDate() ) ) );
122 catch( const Exception& )
124 OSL_ENSURE( sal_False, "ODateModel::ODateModel: caught an exception!" );
126 osl_decrementInterlockedCount( &m_refCount );
129 //------------------------------------------------------------------------------
130 ODateModel::ODateModel( const ODateModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
131 :OEditBaseModel( _pOriginal, _rxFactory )
132 ,OLimitedFormats( _rxFactory, FormComponentType::DATEFIELD )
134 DBG_CTOR( ODateModel, NULL );
136 setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_DATEFORMAT ) );
139 //------------------------------------------------------------------------------
140 ODateModel::~ODateModel( )
142 setAggregateSet(Reference< XFastPropertySet >(), -1);
143 DBG_DTOR( ODateModel, NULL );
146 // XCloneable
147 //------------------------------------------------------------------------------
148 IMPLEMENT_DEFAULT_CLONING( ODateModel )
150 // XServiceInfo
151 //------------------------------------------------------------------------------
152 StringSequence SAL_CALL ODateModel::getSupportedServiceNames() throw()
154 StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
156 sal_Int32 nOldLen = aSupported.getLength();
157 aSupported.realloc( nOldLen + 8 );
158 ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
160 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
161 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
162 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
164 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
165 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
167 *pStoreTo++ = FRM_SUN_COMPONENT_DATEFIELD;
168 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD;
169 *pStoreTo++ = BINDABLE_DATABASE_DATE_FIELD;
171 return aSupported;
174 //------------------------------------------------------------------------------
175 ::rtl::OUString SAL_CALL ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
177 return FRM_COMPONENT_DATEFIELD; // old (non-sun) name for compatibility !
180 // XPropertySet
181 //------------------------------------------------------------------------------
182 void ODateModel::describeFixedProperties( Sequence< Property >& _rProps ) const
184 BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
185 DECL_PROP3(DEFAULT_DATE, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID);
186 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
187 DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT);
188 DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT);
189 END_DESCRIBE_PROPERTIES();
192 //------------------------------------------------------------------------------
193 void SAL_CALL ODateModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
195 switch (_nHandle)
197 case PROPERTY_ID_FORMATKEY:
198 getFormatKeyPropertyValue(_rValue);
199 break;
200 case PROPERTY_ID_FORMATSSUPPLIER:
201 _rValue <<= getFormatsSupplier();
202 break;
203 default:
204 OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
205 break;
209 //------------------------------------------------------------------------------
210 sal_Bool SAL_CALL ODateModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
211 sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
213 if (PROPERTY_ID_FORMATKEY == _nHandle)
214 return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
215 else
216 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
219 //------------------------------------------------------------------------------
220 void SAL_CALL ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception)
222 if (PROPERTY_ID_FORMATKEY == _nHandle)
223 setFormatKeyPropertyValue(_rValue);
224 else
225 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
228 // XLoadListener
229 //------------------------------------------------------------------------------
230 void ODateModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
232 OBoundControlModel::onConnectedDbColumn( _rxForm );
233 Reference<XPropertySet> xField = getField();
234 if (xField.is())
236 m_bDateTimeField = sal_False;
239 sal_Int32 nFieldType = 0;
240 xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
241 m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
243 catch(Exception&)
249 //------------------------------------------------------------------------------
250 sal_Bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
252 Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
253 if ( !compare( aControlValue, m_aSaveValue ) )
255 if ( !aControlValue.hasValue() )
256 m_xColumnUpdate->updateNull();
257 else
261 util::Date aDate;
262 if ( !( aControlValue >>= aDate ) )
264 sal_Int32 nAsInt(0);
265 aControlValue >>= nAsInt;
266 aDate = DBTypeConversion::toDate(nAsInt);
269 if ( !m_bDateTimeField )
270 m_xColumnUpdate->updateDate( aDate );
271 else
273 util::DateTime aDateTime = m_xColumn->getTimestamp();
274 aDateTime.Day = aDate.Day;
275 aDateTime.Month = aDate.Month;
276 aDateTime.Year = aDate.Year;
277 m_xColumnUpdate->updateTimestamp( aDateTime );
280 catch(Exception&)
282 return sal_False;
285 m_aSaveValue = aControlValue;
287 return sal_True;
290 //------------------------------------------------------------------------------
291 void ODateModel::impl_translateControlValueToUNODate( Any& _rUNOValue ) const
293 _rUNOValue = getControlValue();
294 if ( _rUNOValue.hasValue() )
296 sal_Int32 nDate = 0;
297 OSL_VERIFY( _rUNOValue >>= nDate );
298 _rUNOValue <<= DBTypeConversion::toDate( nDate );
302 //------------------------------------------------------------------------------
303 Any ODateModel::translateControlValueToExternalValue( ) const
305 Any aExternalValue;
306 impl_translateControlValueToUNODate( aExternalValue );
307 return aExternalValue;
310 //------------------------------------------------------------------------------
311 Any ODateModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
313 Any aControlValue;
314 if ( _rExternalValue.hasValue() )
316 util::Date aDate;
317 OSL_VERIFY( _rExternalValue >>= aDate );
318 aControlValue <<= DBTypeConversion::toINT32( aDate );
320 return aControlValue;
323 //------------------------------------------------------------------------------
324 Any ODateModel::translateControlValueToValidatableValue( ) const
326 Any aValidatableValue;
327 impl_translateControlValueToUNODate( aValidatableValue );
328 return aValidatableValue;
331 //------------------------------------------------------------------------------
332 Any ODateModel::translateDbColumnToControlValue()
334 util::Date aDate = m_xColumn->getDate();
335 if (m_xColumn->wasNull())
336 m_aSaveValue.clear();
337 else
338 // the aggregated set expects an Int32 as value ...
339 m_aSaveValue <<= DBTypeConversion::toINT32(aDate);
341 return m_aSaveValue;
344 //------------------------------------------------------------------------------
345 Any ODateModel::getDefaultForReset() const
347 return m_aDefault;
350 //------------------------------------------------------------------------------
351 Sequence< Type > ODateModel::getSupportedBindingTypes()
353 return Sequence< Type >( &::getCppuType( static_cast< util::Date* >( NULL ) ), 1 );
356 //.........................................................................
357 } // namespace frm
358 //.........................................................................