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 "services.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
;
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::sdb
;
36 using namespace ::com::sun::star::sdbc
;
37 using namespace ::com::sun::star::sdbcx
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::util
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::form
;
42 using namespace ::com::sun::star::awt
;
43 using namespace ::com::sun::star::io
;
44 using namespace ::com::sun::star::lang
;
47 ODateControl::ODateControl(const Reference
<XComponentContext
>& _rxFactory
)
48 :OBoundControl(_rxFactory
, VCL_CONTROL_DATEFIELD
)
53 Sequence
<Type
> ODateControl::_getTypes()
55 return OBoundControl::_getTypes();
59 StringSequence SAL_CALL
ODateControl::getSupportedServiceNames() throw(std::exception
)
61 StringSequence aSupported
= OBoundControl::getSupportedServiceNames();
62 aSupported
.realloc(aSupported
.getLength() + 2);
64 OUString
*pArray
= aSupported
.getArray();
65 pArray
[aSupported
.getLength()-1] = FRM_SUN_CONTROL_DATEFIELD
;
66 pArray
[aSupported
.getLength()-2] = STARDIV_ONE_FORM_CONTROL_DATEFIELD
;
71 Sequence
<Type
> ODateModel::_getTypes()
73 return OEditBaseModel::_getTypes();
77 ODateModel::ODateModel(const Reference
<XComponentContext
>& _rxFactory
)
78 : OEditBaseModel(_rxFactory
, VCL_CONTROLMODEL_DATEFIELD
,
79 FRM_SUN_CONTROL_DATEFIELD
, true, true)
80 // use the old control name for compytibility reasons
81 , OLimitedFormats(_rxFactory
, FormComponentType::DATEFIELD
)
82 , m_bDateTimeField(false)
84 m_nClassId
= FormComponentType::DATEFIELD
;
85 initValueProperty( PROPERTY_DATE
, PROPERTY_ID_DATE
);
87 setAggregateSet(m_xAggregateFastSet
, getOriginalHandle(PROPERTY_ID_DATEFORMAT
));
89 osl_atomic_increment( &m_refCount
);
92 if ( m_xAggregateSet
.is() )
93 m_xAggregateSet
->setPropertyValue( PROPERTY_DATEMIN
, makeAny(util::Date(1, 1, 1800)) );
95 catch( const Exception
& )
97 OSL_FAIL( "ODateModel::ODateModel: caught an exception!" );
99 osl_atomic_decrement( &m_refCount
);
103 ODateModel::ODateModel( const ODateModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
104 : OEditBaseModel(_pOriginal
, _rxFactory
)
105 , OLimitedFormats(_rxFactory
, FormComponentType::DATEFIELD
)
106 , m_bDateTimeField(false)
108 setAggregateSet( m_xAggregateFastSet
, getOriginalHandle( PROPERTY_ID_DATEFORMAT
) );
112 ODateModel::~ODateModel( )
114 setAggregateSet(Reference
< XFastPropertySet
>(), -1);
119 IMPLEMENT_DEFAULT_CLONING( ODateModel
)
123 StringSequence SAL_CALL
ODateModel::getSupportedServiceNames() throw(std::exception
)
125 StringSequence aSupported
= OBoundControlModel::getSupportedServiceNames();
127 sal_Int32 nOldLen
= aSupported
.getLength();
128 aSupported
.realloc( nOldLen
+ 9 );
129 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
131 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
132 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
133 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
135 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
136 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
138 *pStoreTo
++ = FRM_SUN_COMPONENT_DATEFIELD
;
139 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_DATEFIELD
;
140 *pStoreTo
++ = BINDABLE_DATABASE_DATE_FIELD
;
142 *pStoreTo
++ = FRM_COMPONENT_DATEFIELD
;
148 OUString SAL_CALL
ODateModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException
, std::exception
)
150 return OUString(FRM_COMPONENT_DATEFIELD
); // old (non-sun) name for compatibility !
155 void ODateModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
157 BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel
)
158 DECL_PROP3(DEFAULT_DATE
, util::Date
, BOUND
, MAYBEDEFAULT
, MAYBEVOID
);
159 DECL_PROP1(TABINDEX
, sal_Int16
, BOUND
);
160 DECL_PROP1(FORMATKEY
, sal_Int32
, TRANSIENT
);
161 DECL_IFACE_PROP2(FORMATSSUPPLIER
, XNumberFormatsSupplier
, READONLY
, TRANSIENT
);
162 END_DESCRIBE_PROPERTIES();
166 void SAL_CALL
ODateModel::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
170 case PROPERTY_ID_FORMATKEY
:
171 getFormatKeyPropertyValue(_rValue
);
173 case PROPERTY_ID_FORMATSSUPPLIER
:
174 _rValue
<<= getFormatsSupplier();
177 OEditBaseModel::getFastPropertyValue(_rValue
, _nHandle
);
183 sal_Bool SAL_CALL
ODateModel::convertFastPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
,
184 sal_Int32 _nHandle
, const Any
& _rValue
) throw(IllegalArgumentException
)
186 if (PROPERTY_ID_FORMATKEY
== _nHandle
)
187 return convertFormatKeyPropertyValue(_rConvertedValue
, _rOldValue
, _rValue
);
189 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
193 void SAL_CALL
ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle
, const Any
& _rValue
) throw ( ::com::sun::star::uno::Exception
, std::exception
)
195 if (PROPERTY_ID_FORMATKEY
== _nHandle
)
196 setFormatKeyPropertyValue(_rValue
);
198 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle
, _rValue
);
203 void ODateModel::onConnectedDbColumn( const Reference
< XInterface
>& _rxForm
)
205 OBoundControlModel::onConnectedDbColumn( _rxForm
);
206 Reference
<XPropertySet
> xField
= getField();
209 m_bDateTimeField
= false;
212 sal_Int32 nFieldType
= 0;
213 xField
->getPropertyValue(PROPERTY_FIELDTYPE
) >>= nFieldType
;
214 m_bDateTimeField
= (nFieldType
== DataType::TIMESTAMP
);
216 catch(const Exception
&)
223 bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
225 Any
aControlValue( m_xAggregateFastSet
->getFastPropertyValue( getValuePropertyAggHandle() ) );
226 if ( !compare( aControlValue
, m_aSaveValue
) )
228 if ( !aControlValue
.hasValue() )
229 m_xColumnUpdate
->updateNull();
235 if ( !( aControlValue
>>= aDate
) )
238 aControlValue
>>= nAsInt
;
239 aDate
= DBTypeConversion::toDate(nAsInt
);
242 if ( !m_bDateTimeField
)
243 m_xColumnUpdate
->updateDate( aDate
);
246 util::DateTime aDateTime
= m_xColumn
->getTimestamp();
247 aDateTime
.Day
= aDate
.Day
;
248 aDateTime
.Month
= aDate
.Month
;
249 aDateTime
.Year
= aDate
.Year
;
250 m_xColumnUpdate
->updateTimestamp( aDateTime
);
253 catch(const Exception
&)
258 m_aSaveValue
= aControlValue
;
264 Any
ODateModel::translateControlValueToExternalValue( ) const
266 return getControlValue();
270 Any
ODateModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
272 return _rExternalValue
;
276 Any
ODateModel::translateControlValueToValidatableValue( ) const
278 return getControlValue();
282 Any
ODateModel::translateDbColumnToControlValue()
284 util::Date aDate
= m_xColumn
->getDate();
285 if (m_xColumn
->wasNull())
286 m_aSaveValue
.clear();
288 m_aSaveValue
<<= aDate
;
294 Any
ODateModel::getDefaultForReset() const
300 void ODateModel::resetNoBroadcast()
302 OEditBaseModel::resetNoBroadcast();
303 m_aSaveValue
.clear();
307 Sequence
< Type
> ODateModel::getSupportedBindingTypes()
309 return Sequence
< Type
>( & cppu::UnoType
<util::Date
>::get(), 1 );
314 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
315 com_sun_star_form_ODateModel_get_implementation(::com::sun::star::uno::XComponentContext
* component
,
316 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
318 return cppu::acquire(new frm::ODateModel(component
));
321 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
322 com_sun_star_form_ODateControl_get_implementation(::com::sun::star::uno::XComponentContext
* component
,
323 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
325 return cppu::acquire(new frm::ODateControl(component
));
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */