LibreOffice Module forms (master) 1
Date.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "Date.hxx"
21#include <property.hxx>
22#include <services.hxx>
23#include <tools/debug.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>
29
30using namespace dbtools;
31
32namespace frm
33{
34
35
36using namespace ::com::sun::star;
37using namespace ::com::sun::star::uno;
38using namespace ::com::sun::star::sdb;
39using namespace ::com::sun::star::sdbc;
40using namespace ::com::sun::star::beans;
41using namespace ::com::sun::star::util;
42using namespace ::com::sun::star::container;
43using namespace ::com::sun::star::form;
44using namespace ::com::sun::star::awt;
45using namespace ::com::sun::star::io;
46using namespace ::com::sun::star::lang;
47
48
49ODateControl::ODateControl(const Reference<XComponentContext>& _rxFactory)
50 :OBoundControl(_rxFactory, VCL_CONTROL_DATEFIELD)
51{
52}
53
54
55Sequence<Type> ODateControl::_getTypes()
56{
57 return OBoundControl::_getTypes();
58}
59
60css::uno::Sequence<OUString> SAL_CALL ODateControl::getSupportedServiceNames()
61{
62 const css::uno::Sequence<OUString> vals { FRM_SUN_CONTROL_DATEFIELD, STARDIV_ONE_FORM_CONTROL_DATEFIELD };
63 return comphelper::concatSequences(OBoundControl::getSupportedServiceNames(), vals);
64}
65
66
67Sequence<Type> ODateModel::_getTypes()
68{
69 return OEditBaseModel::_getTypes();
70}
71
72
73ODateModel::ODateModel(const Reference<XComponentContext>& _rxFactory)
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)
79{
80 m_nClassId = FormComponentType::DATEFIELD;
82
83 setAggregateSet(m_xAggregateFastSet, getOriginalHandle(PROPERTY_ID_DATEFORMAT));
84
85 osl_atomic_increment( &m_refCount );
86 try
87 {
88 if ( m_xAggregateSet.is() )
89 m_xAggregateSet->setPropertyValue( PROPERTY_DATEMIN, Any(util::Date(1, 1, 1800)) );
90 }
91 catch( const Exception& )
92 {
93 TOOLS_WARN_EXCEPTION( "forms.component", "ODateModel::ODateModel" );
94 }
95 osl_atomic_decrement( &m_refCount );
96}
97
98
99ODateModel::ODateModel( const ODateModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
100 : OEditBaseModel(_pOriginal, _rxFactory)
101 , OLimitedFormats(_rxFactory, FormComponentType::DATEFIELD)
102 , m_bDateTimeField(false)
103{
104 setAggregateSet( m_xAggregateFastSet, getOriginalHandle( PROPERTY_ID_DATEFORMAT ) );
105}
106
107
109{
110 setAggregateSet(Reference< XFastPropertySet >(), -1);
111}
112
113// XCloneable
114
115css::uno::Reference< css::util::XCloneable > SAL_CALL ODateModel::createClone()
116{
118 pClone->clonedFrom(this);
119 return pClone;
120}
121
122// XServiceInfo
123
124css::uno::Sequence<OUString> SAL_CALL ODateModel::getSupportedServiceNames()
125{
126 const css::uno::Sequence<OUString> vals {
136 };
137
139}
140
141
142OUString SAL_CALL ODateModel::getServiceName()
143{
144 return FRM_COMPONENT_DATEFIELD; // old (non-sun) name for compatibility !
145}
146
147// XPropertySet
148
149void ODateModel::describeFixedProperties( Sequence< Property >& _rProps ) const
150{
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);
159 css::beans::PropertyAttribute::READONLY | css::beans::PropertyAttribute::TRANSIENT);
160 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
161}
162
163
164void SAL_CALL ODateModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle ) const
165{
166 switch (_nHandle)
167 {
170 break;
172 _rValue <<= getFormatsSupplier();
173 break;
174 default:
175 OEditBaseModel::getFastPropertyValue(_rValue, _nHandle);
176 break;
177 }
178}
179
180
181sal_Bool SAL_CALL ODateModel::convertFastPropertyValue(Any& _rConvertedValue, Any& _rOldValue,
182 sal_Int32 _nHandle, const Any& _rValue )
183{
184 if (PROPERTY_ID_FORMATKEY == _nHandle)
185 return convertFormatKeyPropertyValue(_rConvertedValue, _rOldValue, _rValue);
186 else
187 return OEditBaseModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue );
188}
189
190
191void SAL_CALL ODateModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue)
192{
193 if (PROPERTY_ID_FORMATKEY == _nHandle)
195 else
197}
198
199// XLoadListener
200
201void ODateModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
202{
204 Reference<XPropertySet> xField = getField();
205 if (!xField.is())
206 return;
207
208 m_bDateTimeField = false;
209 try
210 {
211 sal_Int32 nFieldType = 0;
212 xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nFieldType;
213 m_bDateTimeField = (nFieldType == DataType::TIMESTAMP);
214 }
215 catch(const Exception&)
216 {
217 }
218}
219
220
222{
223 Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
224 if ( aControlValue == m_aSaveValue )
225 return true;
226
227 if ( !aControlValue.hasValue() )
228 m_xColumnUpdate->updateNull();
229 else
230 {
231 try
232 {
233 util::Date aDate;
234 if ( !( aControlValue >>= aDate ) )
235 {
236 sal_Int32 nAsInt(0);
237 aControlValue >>= nAsInt;
238 aDate = DBTypeConversion::toDate(nAsInt);
239 }
240
241 if ( !m_bDateTimeField )
242 m_xColumnUpdate->updateDate( aDate );
243 else
244 {
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 );
250 }
251 }
252 catch(const Exception&)
253 {
254 return false;
255 }
256 }
257 m_aSaveValue = aControlValue;
258 return true;
259}
260
261
263{
264 return getControlValue();
265}
266
267
268Any ODateModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
269{
270 return _rExternalValue;
271}
272
273
275{
276 return getControlValue();
277}
278
279
281{
282 util::Date aDate = m_xColumn->getDate();
283 if (m_xColumn->wasNull())
284 m_aSaveValue.clear();
285 else
286 m_aSaveValue <<= aDate;
287
288 return m_aSaveValue;
289}
290
291
293{
294 return m_aDefault;
295}
296
297
299{
301 m_aSaveValue.clear();
302}
303
304
306{
307 return Sequence< Type >( & cppu::UnoType<util::Date>::get(), 1 );
308}
309
310} // namespace frm
311
312extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
313com_sun_star_form_ODateModel_get_implementation(css::uno::XComponentContext* component,
314 css::uno::Sequence<css::uno::Any> const &)
315{
316 return cppu::acquire(new frm::ODateModel(component));
317}
318
319extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
320com_sun_star_form_ODateControl_get_implementation(css::uno::XComponentContext* component,
321 css::uno::Sequence<css::uno::Any> const &)
322{
323 return cppu::acquire(new frm::ODateControl(component));
324}
325
326/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_ODateControl_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
Definition: Date.cxx:320
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_ODateModel_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
Definition: Date.cxx:313
oslInterlockedCount m_refCount
sal_Int32 getValuePropertyAggHandle() const
virtual void onConnectedDbColumn(const css::uno::Reference< css::uno::XInterface > &_rxForm)
called whenever a connection to a database column has been established
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
void initValueProperty(const OUString &_rValuePropertyName, sal_Int32 _nValuePropertyExternalHandle)
initializes the part of the class which is related to the control value.
css::uno::Reference< css::sdb::XColumn > m_xColumn
virtual void describeFixedProperties(css::uno::Sequence< css::beans::Property > &_rProps) const override
describes the properties provided by this class, or its respective derived class
virtual void resetNoBroadcast()
called to reset the control to some kind of default.
const css::uno::Reference< css::beans::XPropertySet > & getField() const
css::uno::Reference< css::sdb::XColumnUpdate > m_xColumnUpdate
virtual css::uno::Any getControlValue() const
retrieves the current value of the control
const css::uno::Reference< css::uno::XComponentContext > & getContext() const
ODateControl(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory)
virtual void onConnectedDbColumn(const css::uno::Reference< css::uno::XInterface > &_rxForm) override
called whenever a connection to a database column has been established
Definition: Date.cxx:201
virtual void describeFixedProperties(css::uno::Sequence< css::beans::Property > &_rProps) const override
describes the properties provided by this class, or its respective derived class
Definition: Date.cxx:149
virtual void resetNoBroadcast() override
called to reset the control to some kind of default.
Definition: Date.cxx:298
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
Definition: Date.cxx:115
virtual ~ODateModel() override
Definition: Date.cxx:108
ODateModel(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory)
virtual css::uno::Any translateDbColumnToControlValue() override
translates a db column value into a control value.
Definition: Date.cxx:280
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
Definition: Date.cxx:191
virtual css::uno::Any translateExternalValueToControlValue(const css::uno::Any &_rExternalValue) const override
translates the given value, which was obtained from the current external value binding,...
Definition: Date.cxx:268
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
Definition: Date.cxx:181
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
Definition: Date.cxx:164
virtual css::uno::Any getDefaultForReset() const override
returns the default which should be used when resetting the control
Definition: Date.cxx:292
css::uno::Any m_aSaveValue
Definition: Date.hxx:33
virtual css::uno::Sequence< css::uno::Type > getSupportedBindingTypes() override
returns the data types which the control could use to exchange data with an external value binding
Definition: Date.cxx:305
virtual bool commitControlValueToDbColumn(bool _bPostReset) override
commits the current control value to the database column we're bound to @precond we're properly bound...
Definition: Date.cxx:221
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: Date.cxx:124
virtual css::uno::Any translateControlValueToValidatableValue() const override
retrieves the current value of the control, in a shape which can be used with our external validator.
Definition: Date.cxx:274
virtual css::uno::Any translateControlValueToExternalValue() const override
commits the current control value to our external value binding
Definition: Date.cxx:262
bool m_bDateTimeField
Definition: Date.hxx:34
virtual OUString SAL_CALL getServiceName() override
Definition: Date.cxx:142
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
Definition: EditBase.cxx:291
css::uno::Any m_aDefault
Definition: EditBase.hxx:44
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
Definition: EditBase.cxx:268
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
Definition: EditBase.cxx:325
maintains translation tables format key <-> enum value
const css::uno::Reference< css::util::XNumberFormatsSupplier > & getFormatsSupplier() const
void setAggregateSet(const css::uno::Reference< css::beans::XFastPropertySet > &_rxAggregate, sal_Int32 _nOriginalPropertyHandle)
void getFormatKeyPropertyValue(css::uno::Any &_rValue) const
void setFormatKeyPropertyValue(const css::uno::Any &_rNewValue)
bool convertFormatKeyPropertyValue(css::uno::Any &_rConvertedValue, css::uno::Any &_rOldValue, const css::uno::Any &_rNewValue)
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
constexpr OUStringLiteral PROPERTY_TABINDEX
Definition: frm_strings.hxx:26
constexpr OUStringLiteral PROPERTY_DATE
Definition: frm_strings.hxx:56
constexpr OUStringLiteral PROPERTY_DEFAULT_DATE
Definition: frm_strings.hxx:86
constexpr OUStringLiteral PROPERTY_FIELDTYPE
Definition: frm_strings.hxx:90
constexpr OUStringLiteral PROPERTY_FORMATKEY
Definition: frm_strings.hxx:66
constexpr OUStringLiteral PROPERTY_DATEMIN
constexpr OUStringLiteral PROPERTY_FORMATSSUPPLIER
Definition: frm_strings.hxx:67
@ Exception
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &... rSn)
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
#define PROPERTY_ID_FORMATKEY
Definition: property.hxx:68
#define PROPERTY_ID_DATEFORMAT
Definition: property.hxx:133
#define PROPERTY_ID_DEFAULT_DATE
Definition: property.hxx:174
#define PROPERTY_ID_FORMATSSUPPLIER
Definition: property.hxx:190
#define PROPERTY_ID_TABINDEX
Definition: property.hxx:41
#define PROPERTY_ID_DATE
Definition: property.hxx:130
constexpr OUStringLiteral STARDIV_ONE_FORM_CONTROL_DATEFIELD
Definition: services.hxx:100
constexpr OUStringLiteral FRM_SUN_CONTROL_DATEFIELD
Definition: services.hxx:160
constexpr OUStringLiteral VALIDATABLE_BINDABLE_CONTROL_MODEL
Definition: services.hxx:184
constexpr OUStringLiteral BINDABLE_DATA_AWARE_CONTROL_MODEL
Definition: services.hxx:181
constexpr OUStringLiteral VCL_CONTROL_DATEFIELD
Definition: services.hxx:32
constexpr OUStringLiteral BINDABLE_DATABASE_DATE_FIELD
Definition: services.hxx:176
constexpr OUStringLiteral DATA_AWARE_CONTROL_MODEL
Definition: services.hxx:182
constexpr OUStringLiteral FRM_SUN_COMPONENT_DATABASE_DATEFIELD
Definition: services.hxx:144
constexpr OUStringLiteral VCL_CONTROLMODEL_DATEFIELD
Definition: services.hxx:50
constexpr OUStringLiteral VALIDATABLE_CONTROL_MODEL
Definition: services.hxx:183
constexpr OUStringLiteral BINDABLE_CONTROL_MODEL
Definition: services.hxx:179
constexpr OUStringLiteral FRM_COMPONENT_DATEFIELD
Definition: services.hxx:79
constexpr OUStringLiteral FRM_SUN_COMPONENT_DATEFIELD
Definition: services.hxx:125
unsigned char sal_Bool