1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/debug.hxx>
22 #include <tools/date.hxx>
23 #include <connectivity/dbconversion.hxx>
24 #include <com/sun/star/sdbc/DataType.hpp>
25 #include <comphelper/processfactory.hxx>
27 using namespace dbtools
;
29 //.........................................................................
32 //.........................................................................
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::sdb
;
37 using namespace ::com::sun::star::sdbc
;
38 using namespace ::com::sun::star::sdbcx
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::util
;
41 using namespace ::com::sun::star::container
;
42 using namespace ::com::sun::star::form
;
43 using namespace ::com::sun::star::awt
;
44 using namespace ::com::sun::star::io
;
45 using namespace ::com::sun::star::lang
;
47 //------------------------------------------------------------------
48 ODateControl::ODateControl(const Reference
<XMultiServiceFactory
>& _rxFactory
)
49 :OBoundControl(_rxFactory
, VCL_CONTROL_DATEFIELD
)
53 //------------------------------------------------------------------
54 InterfaceRef SAL_CALL
ODateControl_CreateInstance(const Reference
<XMultiServiceFactory
>& _rxFactory
)
56 return *(new ODateControl(_rxFactory
));
59 //------------------------------------------------------------------------------
60 Sequence
<Type
> ODateControl::_getTypes()
62 return OBoundControl::_getTypes();
65 //------------------------------------------------------------------------------
66 StringSequence SAL_CALL
ODateControl::getSupportedServiceNames() throw()
68 StringSequence aSupported
= OBoundControl::getSupportedServiceNames();
69 aSupported
.realloc(aSupported
.getLength() + 1);
71 OUString
*pArray
= aSupported
.getArray();
72 pArray
[aSupported
.getLength()-1] = FRM_SUN_CONTROL_DATEFIELD
;
76 /*************************************************************************/
77 //------------------------------------------------------------------
78 InterfaceRef SAL_CALL
ODateModel_CreateInstance(const Reference
<XMultiServiceFactory
>& _rxFactory
)
80 return *(new ODateModel(_rxFactory
));
83 //------------------------------------------------------------------------------
84 Sequence
<Type
> ODateModel::_getTypes()
86 return OEditBaseModel::_getTypes();
89 //------------------------------------------------------------------
90 DBG_NAME( ODateModel
)
91 //------------------------------------------------------------------
92 ODateModel::ODateModel(const Reference
<XMultiServiceFactory
>& _rxFactory
)
93 :OEditBaseModel( _rxFactory
, VCL_CONTROLMODEL_DATEFIELD
, FRM_SUN_CONTROL_DATEFIELD
, sal_True
, sal_True
)
94 // use the old control name for compytibility reasons
95 ,OLimitedFormats( comphelper::getComponentContext(_rxFactory
), FormComponentType::DATEFIELD
)
97 DBG_CTOR( ODateModel
, NULL
);
99 m_nClassId
= FormComponentType::DATEFIELD
;
100 initValueProperty( PROPERTY_DATE
, PROPERTY_ID_DATE
);
102 setAggregateSet(m_xAggregateFastSet
, getOriginalHandle(PROPERTY_ID_DATEFORMAT
));
104 osl_atomic_increment( &m_refCount
);
107 if ( m_xAggregateSet
.is() )
108 m_xAggregateSet
->setPropertyValue( PROPERTY_DATEMIN
, makeAny(util::Date(1, 1, 1800)) );
110 catch( const Exception
& )
112 OSL_FAIL( "ODateModel::ODateModel: caught an exception!" );
114 osl_atomic_decrement( &m_refCount
);
117 //------------------------------------------------------------------------------
118 ODateModel::ODateModel( const ODateModel
* _pOriginal
, const Reference
<XMultiServiceFactory
>& _rxFactory
)
119 :OEditBaseModel( _pOriginal
, _rxFactory
)
120 ,OLimitedFormats( comphelper::getComponentContext(_rxFactory
), FormComponentType::DATEFIELD
)
122 DBG_CTOR( ODateModel
, NULL
);
124 setAggregateSet( m_xAggregateFastSet
, getOriginalHandle( PROPERTY_ID_DATEFORMAT
) );
127 //------------------------------------------------------------------------------
128 ODateModel::~ODateModel( )
130 setAggregateSet(Reference
< XFastPropertySet
>(), -1);
131 DBG_DTOR( ODateModel
, NULL
);
135 //------------------------------------------------------------------------------
136 IMPLEMENT_DEFAULT_CLONING( ODateModel
)
139 //------------------------------------------------------------------------------
140 StringSequence SAL_CALL
ODateModel::getSupportedServiceNames() throw()
142 StringSequence aSupported
= OBoundControlModel::getSupportedServiceNames();
144 sal_Int32 nOldLen
= aSupported
.getLength();
145 aSupported
.realloc( nOldLen
+ 8 );
146 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
148 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
149 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
150 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
152 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
153 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
155 *pStoreTo
++ = FRM_SUN_COMPONENT_DATEFIELD
;
156 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD
;
157 *pStoreTo
++ = BINDABLE_DATABASE_DATE_FIELD
;
162 //------------------------------------------------------------------------------
163 OUString SAL_CALL
ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException
)
165 return OUString(FRM_COMPONENT_DATEFIELD
); // old (non-sun) name for compatibility !
169 //------------------------------------------------------------------------------
170 void ODateModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
172 BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel
)
173 DECL_PROP3(DEFAULT_DATE
, util::Date
, BOUND
, MAYBEDEFAULT
, MAYBEVOID
);
174 DECL_PROP1(TABINDEX
, sal_Int16
, BOUND
);
175 DECL_PROP1(FORMATKEY
, sal_Int32
, TRANSIENT
);
176 DECL_IFACE_PROP2(FORMATSSUPPLIER
, XNumberFormatsSupplier
, READONLY
, TRANSIENT
);
177 END_DESCRIBE_PROPERTIES();
180 //------------------------------------------------------------------------------
181 void SAL_CALL
ODateModel::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
185 case PROPERTY_ID_FORMATKEY
:
186 getFormatKeyPropertyValue(_rValue
);
188 case PROPERTY_ID_FORMATSSUPPLIER
:
189 _rValue
<<= getFormatsSupplier();
192 OEditBaseModel::getFastPropertyValue(_rValue
, _nHandle
);
197 //------------------------------------------------------------------------------
198 sal_Bool SAL_CALL
ODateModel::convertFastPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
,
199 sal_Int32 _nHandle
, const Any
& _rValue
) throw(IllegalArgumentException
)
201 if (PROPERTY_ID_FORMATKEY
== _nHandle
)
202 return convertFormatKeyPropertyValue(_rConvertedValue
, _rOldValue
, _rValue
);
204 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
207 //------------------------------------------------------------------------------
208 void SAL_CALL
ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle
, const Any
& _rValue
) throw ( ::com::sun::star::uno::Exception
)
210 if (PROPERTY_ID_FORMATKEY
== _nHandle
)
211 setFormatKeyPropertyValue(_rValue
);
213 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle
, _rValue
);
217 //------------------------------------------------------------------------------
218 void ODateModel::onConnectedDbColumn( const Reference
< XInterface
>& _rxForm
)
220 OBoundControlModel::onConnectedDbColumn( _rxForm
);
221 Reference
<XPropertySet
> xField
= getField();
224 m_bDateTimeField
= sal_False
;
227 sal_Int32 nFieldType
= 0;
228 xField
->getPropertyValue(PROPERTY_FIELDTYPE
) >>= nFieldType
;
229 m_bDateTimeField
= (nFieldType
== DataType::TIMESTAMP
);
231 catch(const Exception
&)
237 //------------------------------------------------------------------------------
238 sal_Bool
ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
240 Any
aControlValue( m_xAggregateFastSet
->getFastPropertyValue( getValuePropertyAggHandle() ) );
241 if ( !compare( aControlValue
, m_aSaveValue
) )
243 if ( !aControlValue
.hasValue() )
244 m_xColumnUpdate
->updateNull();
250 if ( !( aControlValue
>>= aDate
) )
253 aControlValue
>>= nAsInt
;
254 aDate
= DBTypeConversion::toDate(nAsInt
);
257 if ( !m_bDateTimeField
)
258 m_xColumnUpdate
->updateDate( aDate
);
261 util::DateTime aDateTime
= m_xColumn
->getTimestamp();
262 aDateTime
.Day
= aDate
.Day
;
263 aDateTime
.Month
= aDate
.Month
;
264 aDateTime
.Year
= aDate
.Year
;
265 m_xColumnUpdate
->updateTimestamp( aDateTime
);
268 catch(const Exception
&)
273 m_aSaveValue
= aControlValue
;
278 //------------------------------------------------------------------------------
279 Any
ODateModel::translateControlValueToExternalValue( ) const
281 return getControlValue();
284 //------------------------------------------------------------------------------
285 Any
ODateModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
287 return _rExternalValue
;
290 //------------------------------------------------------------------------------
291 Any
ODateModel::translateControlValueToValidatableValue( ) const
293 return getControlValue();
296 //------------------------------------------------------------------------------
297 Any
ODateModel::translateDbColumnToControlValue()
299 util::Date aDate
= m_xColumn
->getDate();
300 if (m_xColumn
->wasNull())
301 m_aSaveValue
.clear();
303 m_aSaveValue
<<= aDate
;
308 //------------------------------------------------------------------------------
309 Any
ODateModel::getDefaultForReset() const
314 //------------------------------------------------------------------------------
315 void ODateModel::resetNoBroadcast()
317 OEditBaseModel::resetNoBroadcast();
318 m_aSaveValue
.clear();
321 //------------------------------------------------------------------------------
322 Sequence
< Type
> ODateModel::getSupportedBindingTypes()
324 return Sequence
< Type
>( &::getCppuType( static_cast< util::Date
* >( NULL
) ), 1 );
327 //.........................................................................
329 //.........................................................................
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */