update dev300-m58
[ooovba.git] / forms / source / component / Time.cxx
blob0d857acef28c5876b0ed124236d66aa84bdbe40c
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: Time.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 "Time.hxx"
34 #include <tools/debug.hxx>
35 #include <tools/time.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::container;
53 using namespace ::com::sun::star::form;
54 using namespace ::com::sun::star::util;
55 using namespace ::com::sun::star::awt;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::lang;
59 //==================================================================
60 //=
61 //==================================================================
63 //==================================================================
64 //= OTimeControl
65 //==================================================================
66 //------------------------------------------------------------------
67 OTimeControl::OTimeControl(const Reference<XMultiServiceFactory>& _rxFactory)
68 :OBoundControl(_rxFactory, VCL_CONTROL_TIMEFIELD)
72 //------------------------------------------------------------------
73 InterfaceRef SAL_CALL OTimeControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
75 return *(new OTimeControl(_rxFactory));
78 //------------------------------------------------------------------------------
79 Sequence<Type> OTimeControl::_getTypes()
81 return OBoundControl::_getTypes();
84 //------------------------------------------------------------------------------
85 StringSequence SAL_CALL OTimeControl::getSupportedServiceNames() throw()
87 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
88 aSupported.realloc(aSupported.getLength() + 1);
90 ::rtl::OUString*pArray = aSupported.getArray();
91 pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_TIMEFIELD;
92 return aSupported;
95 //==================================================================
96 //= OTimeModel
97 //==================================================================
98 //------------------------------------------------------------------
99 InterfaceRef SAL_CALL OTimeModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
101 return *(new OTimeModel(_rxFactory));
104 // XServiceInfo
105 //------------------------------------------------------------------------------
106 StringSequence SAL_CALL OTimeModel::getSupportedServiceNames() throw()
108 StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
110 sal_Int32 nOldLen = aSupported.getLength();
111 aSupported.realloc( nOldLen + 8 );
112 ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
114 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
115 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
116 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
118 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
119 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
121 *pStoreTo++ = FRM_SUN_COMPONENT_TIMEFIELD;
122 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_TIMEFIELD;
123 *pStoreTo++ = BINDABLE_DATABASE_TIME_FIELD;
125 return aSupported;
128 //------------------------------------------------------------------------------
129 Sequence<Type> OTimeModel::_getTypes()
131 return OBoundControlModel::_getTypes();
134 //------------------------------------------------------------------
135 DBG_NAME( OTimeModel )
136 //------------------------------------------------------------------
137 OTimeModel::OTimeModel(const Reference<XMultiServiceFactory>& _rxFactory)
138 :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_TIMEFIELD, FRM_SUN_CONTROL_TIMEFIELD, sal_True, sal_True )
139 // use the old control name for compytibility reasons
140 ,OLimitedFormats(_rxFactory, FormComponentType::TIMEFIELD)
142 DBG_CTOR( OTimeModel, NULL );
144 m_nClassId = FormComponentType::TIMEFIELD;
145 initValueProperty( PROPERTY_TIME, PROPERTY_ID_TIME );
147 setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_TIMEFORMAT));
150 //------------------------------------------------------------------------------
151 OTimeModel::OTimeModel( const OTimeModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
152 :OEditBaseModel( _pOriginal, _rxFactory )
153 ,OLimitedFormats( _rxFactory, FormComponentType::TIMEFIELD )
155 DBG_CTOR( OTimeModel, NULL );
157 setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_TIMEFORMAT ) );
160 //------------------------------------------------------------------------------
161 OTimeModel::~OTimeModel( )
163 setAggregateSet(Reference< XFastPropertySet >(), -1);
164 DBG_DTOR( OTimeModel, NULL );
167 // XCloneable
168 //------------------------------------------------------------------------------
169 IMPLEMENT_DEFAULT_CLONING( OTimeModel )
171 //------------------------------------------------------------------------------
172 ::rtl::OUString SAL_CALL OTimeModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
174 return FRM_COMPONENT_TIMEFIELD; // old (non-sun) name for compatibility !
177 // XPropertySet
178 //------------------------------------------------------------------------------
179 void OTimeModel::describeFixedProperties( Sequence< Property >& _rProps ) const
181 BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
182 DECL_PROP3(DEFAULT_TIME, sal_Int32, BOUND, MAYBEDEFAULT, MAYBEVOID);
183 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
184 DECL_PROP1(FORMATKEY, sal_Int32, TRANSIENT);
185 DECL_IFACE_PROP2(FORMATSSUPPLIER, XNumberFormatsSupplier, READONLY, TRANSIENT);
186 END_DESCRIBE_PROPERTIES();
189 //------------------------------------------------------------------------------
190 void SAL_CALL OTimeModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
192 switch (_nHandle)
194 case PROPERTY_ID_FORMATKEY:
195 getFormatKeyPropertyValue(_rValue);
196 break;
197 case PROPERTY_ID_FORMATSSUPPLIER:
198 _rValue <<= getFormatsSupplier();
199 break;
200 default:
201 OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
202 break;
206 //------------------------------------------------------------------------------
207 sal_Bool SAL_CALL OTimeModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
208 sal_Int32 _nHandle, const Any& _rValue ) throw(IllegalArgumentException)
210 if (PROPERTY_ID_FORMATKEY == _nHandle)
211 return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
212 else
213 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
216 //------------------------------------------------------------------------------
217 void SAL_CALL OTimeModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue) throw ( ::com::sun::star::uno::Exception)
219 if (PROPERTY_ID_FORMATKEY == _nHandle)
220 setFormatKeyPropertyValue(_rValue);
221 else
222 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
225 // XLoadListener
226 //------------------------------------------------------------------------------
227 void OTimeModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
229 OBoundControlModel::onConnectedDbColumn( _rxForm );
230 Reference<XPropertySet> xField = getField();
231 if (xField.is())
233 m_bDateTimeField = sal_False;
236 sal_Int32 nFieldType = 0;
237 xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
238 m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
240 catch(Exception&)
246 //------------------------------------------------------------------------------
247 sal_Bool OTimeModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
249 Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
250 if ( !compare( aControlValue, m_aSaveValue ) )
252 if ( !aControlValue.hasValue() )
253 m_xColumnUpdate->updateNull();
254 else
258 util::Time aTime;
259 if ( !( aControlValue >>= aTime ) )
261 sal_Int32 nAsInt(0);
262 aControlValue >>= nAsInt;
263 aTime = DBTypeConversion::toTime(nAsInt);
266 if (!m_bDateTimeField)
267 m_xColumnUpdate->updateTime(aTime);
268 else
270 util::DateTime aDateTime = m_xColumn->getTimestamp();
271 aDateTime.HundredthSeconds = aTime.HundredthSeconds;
272 aDateTime.Seconds = aTime.Seconds;
273 aDateTime.Minutes = aTime.Minutes;
274 aDateTime.Hours = aTime.Hours;
275 m_xColumnUpdate->updateTimestamp(aDateTime);
278 catch(Exception&)
280 return sal_False;
283 m_aSaveValue = aControlValue;
285 return sal_True;
288 //------------------------------------------------------------------------------
289 void OTimeModel::impl_translateControlValueToUNOTime( Any& _rUNOValue ) const
291 _rUNOValue = getControlValue();
292 if ( _rUNOValue.hasValue() )
294 sal_Int32 nTime = 0;
295 OSL_VERIFY( _rUNOValue >>= nTime );
296 if ( nTime == ::Time( 99, 99, 99 ).GetTime() )
297 // "invalid time" in VCL is different from "invalid time" in UNO
298 _rUNOValue.clear();
299 else
300 _rUNOValue <<= DBTypeConversion::toTime( nTime );
304 //------------------------------------------------------------------------------
305 Any OTimeModel::translateControlValueToExternalValue( ) const
307 Any aExternalValue;
308 impl_translateControlValueToUNOTime( aExternalValue );
309 return aExternalValue;
312 //------------------------------------------------------------------------------
313 Any OTimeModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
315 Any aControlValue;
316 if ( _rExternalValue.hasValue() )
318 util::Time aTime;
319 OSL_VERIFY( _rExternalValue >>= aTime );
320 aControlValue <<= DBTypeConversion::toINT32( aTime );
322 return aControlValue;
325 //------------------------------------------------------------------------------
326 Any OTimeModel::translateControlValueToValidatableValue( ) const
328 Any aValidatableValue;
329 impl_translateControlValueToUNOTime( aValidatableValue );
330 return aValidatableValue;
333 //------------------------------------------------------------------------------
334 Any OTimeModel::translateDbColumnToControlValue()
336 util::Time aTime = m_xColumn->getTime();
337 if ( m_xColumn->wasNull() )
338 m_aSaveValue.clear();
339 else
340 // the aggregated set expects an Int32 as value ...
341 m_aSaveValue <<= DBTypeConversion::toINT32( aTime );
343 return m_aSaveValue;
346 //------------------------------------------------------------------------------
347 Any OTimeModel::getDefaultForReset() const
349 return m_aDefault;
352 //------------------------------------------------------------------------------
353 Sequence< Type > OTimeModel::getSupportedBindingTypes()
355 return Sequence< Type >( &::getCppuType( static_cast< util::Time* >( NULL ) ), 1 );
358 //.........................................................................
359 } // namespace frm
360 //.........................................................................