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 <property.hxx>
22 #include <services.hxx>
23 #include <tools/debug.hxx>
24 #include <comphelper/diagnose_ex.hxx>
25 #include <connectivity/dbconversion.hxx>
26 #include <com/sun/star/sdbc/DataType.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/form/FormComponentType.hpp>
30 using namespace dbtools
;
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::sdb
;
39 using namespace ::com::sun::star::sdbc
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::util
;
42 using namespace ::com::sun::star::container
;
43 using namespace ::com::sun::star::form
;
44 using namespace ::com::sun::star::awt
;
45 using namespace ::com::sun::star::io
;
46 using namespace ::com::sun::star::lang
;
49 ODateControl::ODateControl(const Reference
<XComponentContext
>& _rxFactory
)
50 :OBoundControl(_rxFactory
, VCL_CONTROL_DATEFIELD
)
55 Sequence
<Type
> ODateControl::_getTypes()
57 return OBoundControl::_getTypes();
60 css::uno::Sequence
<OUString
> SAL_CALL
ODateControl::getSupportedServiceNames()
62 const css::uno::Sequence
<OUString
> vals
{ FRM_SUN_CONTROL_DATEFIELD
, STARDIV_ONE_FORM_CONTROL_DATEFIELD
};
63 return comphelper::concatSequences(OBoundControl::getSupportedServiceNames(), vals
);
67 Sequence
<Type
> ODateModel::_getTypes()
69 return OEditBaseModel::_getTypes();
73 ODateModel::ODateModel(const Reference
<XComponentContext
>& _rxFactory
)
74 : OEditBaseModel(_rxFactory
, VCL_CONTROLMODEL_DATEFIELD
,
75 FRM_SUN_CONTROL_DATEFIELD
, true, true)
76 // use the old control name for compytibility reasons
77 , OLimitedFormats(_rxFactory
, FormComponentType::DATEFIELD
)
78 , m_bDateTimeField(false)
80 m_nClassId
= FormComponentType::DATEFIELD
;
81 initValueProperty( PROPERTY_DATE
, PROPERTY_ID_DATE
);
83 setAggregateSet(m_xAggregateFastSet
, getOriginalHandle(PROPERTY_ID_DATEFORMAT
));
85 osl_atomic_increment( &m_refCount
);
88 if ( m_xAggregateSet
.is() )
89 m_xAggregateSet
->setPropertyValue( PROPERTY_DATEMIN
, Any(util::Date(1, 1, 1800)) );
91 catch( const Exception
& )
93 TOOLS_WARN_EXCEPTION( "forms.component", "ODateModel::ODateModel" );
95 osl_atomic_decrement( &m_refCount
);
99 ODateModel::ODateModel( const ODateModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
100 : OEditBaseModel(_pOriginal
, _rxFactory
)
101 , OLimitedFormats(_rxFactory
, FormComponentType::DATEFIELD
)
102 , m_bDateTimeField(false)
104 setAggregateSet( m_xAggregateFastSet
, getOriginalHandle( PROPERTY_ID_DATEFORMAT
) );
108 ODateModel::~ODateModel( )
110 setAggregateSet(Reference
< XFastPropertySet
>(), -1);
115 css::uno::Reference
< css::util::XCloneable
> SAL_CALL
ODateModel::createClone()
117 rtl::Reference
<ODateModel
> pClone
= new ODateModel(this, getContext());
118 pClone
->clonedFrom(this);
124 css::uno::Sequence
<OUString
> SAL_CALL
ODateModel::getSupportedServiceNames()
126 const css::uno::Sequence
<OUString
> vals
{
127 BINDABLE_CONTROL_MODEL
,
128 DATA_AWARE_CONTROL_MODEL
,
129 VALIDATABLE_CONTROL_MODEL
,
130 BINDABLE_DATA_AWARE_CONTROL_MODEL
,
131 VALIDATABLE_BINDABLE_CONTROL_MODEL
,
132 FRM_SUN_COMPONENT_DATEFIELD
,
133 FRM_SUN_COMPONENT_DATABASE_DATEFIELD
,
134 BINDABLE_DATABASE_DATE_FIELD
,
135 FRM_COMPONENT_DATEFIELD
138 return comphelper::concatSequences(OBoundControlModel::getSupportedServiceNames(), vals
);
142 OUString SAL_CALL
ODateModel::getServiceName()
144 return FRM_COMPONENT_DATEFIELD
; // old (non-sun) name for compatibility !
149 void ODateModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
151 OEditBaseModel::describeFixedProperties( _rProps
);
152 sal_Int32 nOldCount
= _rProps
.getLength();
153 _rProps
.realloc( nOldCount
+ 4);
154 css::beans::Property
* pProperties
= _rProps
.getArray() + nOldCount
;
155 *pProperties
++ = css::beans::Property(PROPERTY_DEFAULT_DATE
, PROPERTY_ID_DEFAULT_DATE
, cppu::UnoType
<util::Date
>::get(), css::beans::PropertyAttribute::BOUND
| css::beans::PropertyAttribute::MAYBEDEFAULT
| css::beans::PropertyAttribute::MAYBEVOID
);
156 *pProperties
++ = css::beans::Property(PROPERTY_TABINDEX
, PROPERTY_ID_TABINDEX
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
);
157 *pProperties
++ = css::beans::Property(PROPERTY_FORMATKEY
, PROPERTY_ID_FORMATKEY
, cppu::UnoType
<sal_Int32
>::get(), css::beans::PropertyAttribute::TRANSIENT
);
158 *pProperties
++ = css::beans::Property(PROPERTY_FORMATSSUPPLIER
, PROPERTY_ID_FORMATSSUPPLIER
, cppu::UnoType
<XNumberFormatsSupplier
>::get(),
159 css::beans::PropertyAttribute::READONLY
| css::beans::PropertyAttribute::TRANSIENT
);
160 DBG_ASSERT( pProperties
== _rProps
.getArray() + _rProps
.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
164 void SAL_CALL
ODateModel::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
168 case PROPERTY_ID_FORMATKEY
:
169 getFormatKeyPropertyValue(_rValue
);
171 case PROPERTY_ID_FORMATSSUPPLIER
:
172 _rValue
<<= getFormatsSupplier();
175 OEditBaseModel::getFastPropertyValue(_rValue
, _nHandle
);
181 sal_Bool SAL_CALL
ODateModel::convertFastPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
,
182 sal_Int32 _nHandle
, const Any
& _rValue
)
184 if (PROPERTY_ID_FORMATKEY
== _nHandle
)
185 return convertFormatKeyPropertyValue(_rConvertedValue
, _rOldValue
, _rValue
);
187 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue
, _rOldValue
, _nHandle
, _rValue
);
191 void SAL_CALL
ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle
, const Any
& _rValue
)
193 if (PROPERTY_ID_FORMATKEY
== _nHandle
)
194 setFormatKeyPropertyValue(_rValue
);
196 OEditBaseModel::setFastPropertyValue_NoBroadcast(_nHandle
, _rValue
);
201 void ODateModel::onConnectedDbColumn( const Reference
< XInterface
>& _rxForm
)
203 OBoundControlModel::onConnectedDbColumn( _rxForm
);
204 Reference
<XPropertySet
> xField
= getField();
208 m_bDateTimeField
= false;
211 sal_Int32 nFieldType
= 0;
212 xField
->getPropertyValue(PROPERTY_FIELDTYPE
) >>= nFieldType
;
213 m_bDateTimeField
= (nFieldType
== DataType::TIMESTAMP
);
215 catch(const Exception
&)
221 bool ODateModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
223 Any
aControlValue( m_xAggregateFastSet
->getFastPropertyValue( getValuePropertyAggHandle() ) );
224 if ( aControlValue
== m_aSaveValue
)
227 if ( !aControlValue
.hasValue() )
228 m_xColumnUpdate
->updateNull();
234 if ( !( aControlValue
>>= aDate
) )
237 aControlValue
>>= nAsInt
;
238 aDate
= DBTypeConversion::toDate(nAsInt
);
241 if ( !m_bDateTimeField
)
242 m_xColumnUpdate
->updateDate( aDate
);
245 util::DateTime aDateTime
= m_xColumn
->getTimestamp();
246 aDateTime
.Day
= aDate
.Day
;
247 aDateTime
.Month
= aDate
.Month
;
248 aDateTime
.Year
= aDate
.Year
;
249 m_xColumnUpdate
->updateTimestamp( aDateTime
);
252 catch(const Exception
&)
257 m_aSaveValue
= aControlValue
;
262 Any
ODateModel::translateControlValueToExternalValue( ) const
264 return getControlValue();
268 Any
ODateModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
270 return _rExternalValue
;
274 Any
ODateModel::translateControlValueToValidatableValue( ) const
276 return getControlValue();
280 Any
ODateModel::translateDbColumnToControlValue()
282 util::Date aDate
= m_xColumn
->getDate();
283 if (m_xColumn
->wasNull())
284 m_aSaveValue
.clear();
286 m_aSaveValue
<<= aDate
;
292 Any
ODateModel::getDefaultForReset() const
298 void ODateModel::resetNoBroadcast()
300 OEditBaseModel::resetNoBroadcast();
301 m_aSaveValue
.clear();
305 Sequence
< Type
> ODateModel::getSupportedBindingTypes()
307 return Sequence
< Type
>( & cppu::UnoType
<util::Date
>::get(), 1 );
312 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
313 com_sun_star_form_ODateModel_get_implementation(css::uno::XComponentContext
* component
,
314 css::uno::Sequence
<css::uno::Any
> const &)
316 return cppu::acquire(new frm::ODateModel(component
));
319 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
320 com_sun_star_form_ODateControl_get_implementation(css::uno::XComponentContext
* component
,
321 css::uno::Sequence
<css::uno::Any
> const &)
323 return cppu::acquire(new frm::ODateControl(component
));
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */