xref: /trunk/main/forms/source/component/FormattedField.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "FormattedField.hxx"
32*cdf0e10cSrcweir #include "services.hxx"
33*cdf0e10cSrcweir #include "property.hrc"
34*cdf0e10cSrcweir #include "property.hxx"
35*cdf0e10cSrcweir #include "frm_resource.hxx"
36*cdf0e10cSrcweir #include "frm_resource.hrc"
37*cdf0e10cSrcweir #include "propertybaghelper.hxx"
38*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
39*cdf0e10cSrcweir #include <comphelper/numbers.hxx>
40*cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
41*cdf0e10cSrcweir #include <connectivity/dbconversion.hxx>
42*cdf0e10cSrcweir #include <svl/zforlist.hxx>
43*cdf0e10cSrcweir #include <svl/numuno.hxx>
44*cdf0e10cSrcweir #include <vcl/svapp.hxx>
45*cdf0e10cSrcweir #include <tools/debug.hxx>
46*cdf0e10cSrcweir #include <tools/wintypes.hxx>
47*cdf0e10cSrcweir #include <i18npool/mslangid.hxx>
48*cdf0e10cSrcweir #include <rtl/textenc.h>
49*cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/awt/MouseEvent.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/form/XSubmit.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/awt/XKeyListener.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/form/FormComponentType.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/form/XForm.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
62*cdf0e10cSrcweir #include <vos/mutex.hxx>
63*cdf0e10cSrcweir     // needed as long as we use the SolarMutex
64*cdf0e10cSrcweir #include <comphelper/streamsection.hxx>
65*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
66*cdf0e10cSrcweir #include <unotools/desktopterminationobserver.hxx>
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir #include <list>
69*cdf0e10cSrcweir #include <algorithm>
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir using namespace dbtools;
72*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
73*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
74*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
75*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
76*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
77*cdf0e10cSrcweir using namespace ::com::sun::star::container;
78*cdf0e10cSrcweir using namespace ::com::sun::star::form;
79*cdf0e10cSrcweir using namespace ::com::sun::star::awt;
80*cdf0e10cSrcweir using namespace ::com::sun::star::io;
81*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
82*cdf0e10cSrcweir using namespace ::com::sun::star::util;
83*cdf0e10cSrcweir using namespace ::com::sun::star::form::binding;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir namespace
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     typedef com::sun::star::util::Date UNODate;
88*cdf0e10cSrcweir     typedef com::sun::star::util::Time UNOTime;
89*cdf0e10cSrcweir     typedef com::sun::star::util::DateTime UNODateTime;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir //.........................................................................
93*cdf0e10cSrcweir namespace frm
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir /*************************************************************************/
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir class StandardFormatsSupplier : protected SvNumberFormatsSupplierObj, public ::utl::ITerminationListener
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir protected:
101*cdf0e10cSrcweir             SvNumberFormatter*                       m_pMyPrivateFormatter;
102*cdf0e10cSrcweir     static  WeakReference< XNumberFormatsSupplier >  s_xDefaultFormatsSupplier;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir public:
105*cdf0e10cSrcweir     static Reference< XNumberFormatsSupplier > get( const Reference< XMultiServiceFactory >& _rxORB );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     using SvNumberFormatsSupplierObj::operator new;
108*cdf0e10cSrcweir     using SvNumberFormatsSupplierObj::operator delete;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir protected:
111*cdf0e10cSrcweir     StandardFormatsSupplier(const Reference<XMultiServiceFactory>& _rxFactory,LanguageType _eSysLanguage);
112*cdf0e10cSrcweir     ~StandardFormatsSupplier();
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir protected:
115*cdf0e10cSrcweir     virtual bool    queryTermination() const;
116*cdf0e10cSrcweir     virtual void    notifyTermination();
117*cdf0e10cSrcweir };
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir //------------------------------------------------------------------
120*cdf0e10cSrcweir WeakReference< XNumberFormatsSupplier > StandardFormatsSupplier::s_xDefaultFormatsSupplier;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir //------------------------------------------------------------------
123*cdf0e10cSrcweir StandardFormatsSupplier::StandardFormatsSupplier(const Reference< XMultiServiceFactory > & _rxFactory,LanguageType _eSysLanguage)
124*cdf0e10cSrcweir     :SvNumberFormatsSupplierObj()
125*cdf0e10cSrcweir     ,m_pMyPrivateFormatter(new SvNumberFormatter(_rxFactory, _eSysLanguage))
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir     SetNumberFormatter(m_pMyPrivateFormatter);
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     // #i29147# - 2004-06-18 - fs@openoffice.org
130*cdf0e10cSrcweir     ::utl::DesktopTerminationObserver::registerTerminationListener( this );
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir //------------------------------------------------------------------
134*cdf0e10cSrcweir StandardFormatsSupplier::~StandardFormatsSupplier()
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     ::utl::DesktopTerminationObserver::revokeTerminationListener( this );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     DELETEZ( m_pMyPrivateFormatter );
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir //------------------------------------------------------------------
142*cdf0e10cSrcweir Reference< XNumberFormatsSupplier > StandardFormatsSupplier::get( const Reference< XMultiServiceFactory >& _rxORB )
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir     LanguageType eSysLanguage = LANGUAGE_SYSTEM;
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
147*cdf0e10cSrcweir         Reference< XNumberFormatsSupplier > xSupplier = s_xDefaultFormatsSupplier;
148*cdf0e10cSrcweir         if ( xSupplier.is() )
149*cdf0e10cSrcweir             return xSupplier;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         // get the Office's locale
152*cdf0e10cSrcweir         const Locale& rSysLocale = SvtSysLocale().GetLocaleData().getLocale();
153*cdf0e10cSrcweir         // translate
154*cdf0e10cSrcweir         eSysLanguage = MsLangId::convertLocaleToLanguage( rSysLocale );
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     StandardFormatsSupplier* pSupplier = new StandardFormatsSupplier( _rxORB, eSysLanguage );
158*cdf0e10cSrcweir     Reference< XNumberFormatsSupplier > xNewlyCreatedSupplier( pSupplier );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     {
161*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
162*cdf0e10cSrcweir         Reference< XNumberFormatsSupplier > xSupplier = s_xDefaultFormatsSupplier;
163*cdf0e10cSrcweir         if ( xSupplier.is() )
164*cdf0e10cSrcweir             // somebody used the small time frame where the mutex was not locked to create and set
165*cdf0e10cSrcweir             // the supplier
166*cdf0e10cSrcweir             return xSupplier;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         s_xDefaultFormatsSupplier = xNewlyCreatedSupplier;
169*cdf0e10cSrcweir     }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     return xNewlyCreatedSupplier;
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir //------------------------------------------------------------------
175*cdf0e10cSrcweir bool StandardFormatsSupplier::queryTermination() const
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir     return true;
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir //------------------------------------------------------------------
181*cdf0e10cSrcweir void StandardFormatsSupplier::notifyTermination()
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     Reference< XNumberFormatsSupplier > xKeepAlive = this;
184*cdf0e10cSrcweir     // when the application is terminating, release our static reference so that we are cleared/destructed
185*cdf0e10cSrcweir     // earlier than upon unloading the library
186*cdf0e10cSrcweir     // #i29147# - 2004-06-18 - fs@openoffice.org
187*cdf0e10cSrcweir     s_xDefaultFormatsSupplier = WeakReference< XNumberFormatsSupplier >( );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     SetNumberFormatter( NULL );
190*cdf0e10cSrcweir     DELETEZ( m_pMyPrivateFormatter );
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir /*************************************************************************/
194*cdf0e10cSrcweir //------------------------------------------------------------------
195*cdf0e10cSrcweir InterfaceRef SAL_CALL OFormattedControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     return *(new OFormattedControl(_rxFactory));
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir //------------------------------------------------------------------
201*cdf0e10cSrcweir Sequence<Type> OFormattedControl::_getTypes()
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     return ::comphelper::concatSequences(
204*cdf0e10cSrcweir         OFormattedControl_BASE::getTypes(),
205*cdf0e10cSrcweir         OBoundControl::_getTypes()
206*cdf0e10cSrcweir     );
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir //------------------------------------------------------------------
210*cdf0e10cSrcweir Any SAL_CALL OFormattedControl::queryAggregation(const Type& _rType) throw (RuntimeException)
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir     Any aReturn = OBoundControl::queryAggregation(_rType);
213*cdf0e10cSrcweir     if (!aReturn.hasValue())
214*cdf0e10cSrcweir         aReturn = OFormattedControl_BASE::queryInterface(_rType);
215*cdf0e10cSrcweir     return aReturn;
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir DBG_NAME(OFormattedControl);
220*cdf0e10cSrcweir //------------------------------------------------------------------------------
221*cdf0e10cSrcweir OFormattedControl::OFormattedControl(const Reference<XMultiServiceFactory>& _rxFactory)
222*cdf0e10cSrcweir                :OBoundControl(_rxFactory, VCL_CONTROL_FORMATTEDFIELD)
223*cdf0e10cSrcweir                ,m_nKeyEvent(0)
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     DBG_CTOR(OFormattedControl,NULL);
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     increment(m_refCount);
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         Reference<XWindow>  xComp;
230*cdf0e10cSrcweir         if (query_aggregation(m_xAggregate, xComp))
231*cdf0e10cSrcweir         {
232*cdf0e10cSrcweir             xComp->addKeyListener(this);
233*cdf0e10cSrcweir         }
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir     decrement(m_refCount);
236*cdf0e10cSrcweir }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir //------------------------------------------------------------------------------
239*cdf0e10cSrcweir OFormattedControl::~OFormattedControl()
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir     if( m_nKeyEvent )
242*cdf0e10cSrcweir         Application::RemoveUserEvent( m_nKeyEvent );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     if (!OComponentHelper::rBHelper.bDisposed)
245*cdf0e10cSrcweir     {
246*cdf0e10cSrcweir         acquire();
247*cdf0e10cSrcweir         dispose();
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     DBG_DTOR(OFormattedControl,NULL);
251*cdf0e10cSrcweir }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir // XKeyListener
254*cdf0e10cSrcweir //------------------------------------------------------------------------------
255*cdf0e10cSrcweir void OFormattedControl::disposing(const EventObject& _rSource) throw(RuntimeException)
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir     OBoundControl::disposing(_rSource);
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir //------------------------------------------------------------------------------
261*cdf0e10cSrcweir void OFormattedControl::keyPressed(const ::com::sun::star::awt::KeyEvent& e) throw ( ::com::sun::star::uno::RuntimeException)
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     if( e.KeyCode != KEY_RETURN || e.Modifiers != 0 )
264*cdf0e10cSrcweir         return;
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     // Steht das Control in einem Formular mit einer Submit-URL?
267*cdf0e10cSrcweir     Reference<com::sun::star::beans::XPropertySet>  xSet(getModel(), UNO_QUERY);
268*cdf0e10cSrcweir     if( !xSet.is() )
269*cdf0e10cSrcweir         return;
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     Reference<XFormComponent>  xFComp(xSet, UNO_QUERY);
272*cdf0e10cSrcweir     InterfaceRef  xParent = xFComp->getParent();
273*cdf0e10cSrcweir     if( !xParent.is() )
274*cdf0e10cSrcweir         return;
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     Reference<com::sun::star::beans::XPropertySet>  xFormSet(xParent, UNO_QUERY);
277*cdf0e10cSrcweir     if( !xFormSet.is() )
278*cdf0e10cSrcweir         return;
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     Any aTmp(xFormSet->getPropertyValue( PROPERTY_TARGET_URL ));
281*cdf0e10cSrcweir     if (!isA(aTmp, static_cast< ::rtl::OUString* >(NULL)) ||
282*cdf0e10cSrcweir         !getString(aTmp).getLength() )
283*cdf0e10cSrcweir         return;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     Reference<XIndexAccess>  xElements(xParent, UNO_QUERY);
286*cdf0e10cSrcweir     sal_Int32 nCount = xElements->getCount();
287*cdf0e10cSrcweir     if( nCount > 1 )
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir         Reference<com::sun::star::beans::XPropertySet>  xFCSet;
291*cdf0e10cSrcweir         for( sal_Int32 nIndex=0; nIndex < nCount; nIndex++ )
292*cdf0e10cSrcweir         {
293*cdf0e10cSrcweir             //  Any aElement(xElements->getByIndex(nIndex));
294*cdf0e10cSrcweir             xElements->getByIndex(nIndex) >>= xFCSet;
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir             if (hasProperty(PROPERTY_CLASSID, xFCSet) &&
297*cdf0e10cSrcweir                 getINT16(xFCSet->getPropertyValue(PROPERTY_CLASSID)) == FormComponentType::TEXTFIELD)
298*cdf0e10cSrcweir             {
299*cdf0e10cSrcweir                 // Noch ein weiteres Edit gefunden ==> dann nicht submitten
300*cdf0e10cSrcweir                 if (xFCSet != xSet)
301*cdf0e10cSrcweir                     return;
302*cdf0e10cSrcweir             }
303*cdf0e10cSrcweir         }
304*cdf0e10cSrcweir     }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir     // Da wir noch im Haender stehen, submit asynchron ausloesen
307*cdf0e10cSrcweir     if( m_nKeyEvent )
308*cdf0e10cSrcweir         Application::RemoveUserEvent( m_nKeyEvent );
309*cdf0e10cSrcweir     m_nKeyEvent = Application::PostUserEvent( LINK(this, OFormattedControl,
310*cdf0e10cSrcweir                                             OnKeyPressed) );
311*cdf0e10cSrcweir }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir //------------------------------------------------------------------------------
314*cdf0e10cSrcweir void OFormattedControl::keyReleased(const ::com::sun::star::awt::KeyEvent& /*e*/) throw ( ::com::sun::star::uno::RuntimeException)
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir //------------------------------------------------------------------------------
319*cdf0e10cSrcweir IMPL_LINK(OFormattedControl, OnKeyPressed, void*, /*EMPTYARG*/)
320*cdf0e10cSrcweir {
321*cdf0e10cSrcweir     m_nKeyEvent = 0;
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir     Reference<XFormComponent>  xFComp(getModel(), UNO_QUERY);
324*cdf0e10cSrcweir     InterfaceRef  xParent = xFComp->getParent();
325*cdf0e10cSrcweir     Reference<XSubmit>  xSubmit(xParent, UNO_QUERY);
326*cdf0e10cSrcweir     if (xSubmit.is())
327*cdf0e10cSrcweir         xSubmit->submit( Reference<XControl> (), ::com::sun::star::awt::MouseEvent() );
328*cdf0e10cSrcweir     return 0L;
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir //------------------------------------------------------------------------------
332*cdf0e10cSrcweir StringSequence  OFormattedControl::getSupportedServiceNames() throw()
333*cdf0e10cSrcweir {
334*cdf0e10cSrcweir     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
335*cdf0e10cSrcweir     aSupported.realloc(aSupported.getLength() + 1);
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir     ::rtl::OUString*pArray = aSupported.getArray();
338*cdf0e10cSrcweir     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_FORMATTEDFIELD;
339*cdf0e10cSrcweir     return aSupported;
340*cdf0e10cSrcweir }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir //------------------------------------------------------------------------------
343*cdf0e10cSrcweir void OFormattedControl::setDesignMode(sal_Bool bOn) throw ( ::com::sun::star::uno::RuntimeException)
344*cdf0e10cSrcweir {
345*cdf0e10cSrcweir     OBoundControl::setDesignMode(bOn);
346*cdf0e10cSrcweir }
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir /*************************************************************************/
349*cdf0e10cSrcweir DBG_NAME(OFormattedModel)
350*cdf0e10cSrcweir //------------------------------------------------------------------
351*cdf0e10cSrcweir void OFormattedModel::implConstruct()
352*cdf0e10cSrcweir {
353*cdf0e10cSrcweir     // members
354*cdf0e10cSrcweir     m_bOriginalNumeric = sal_False;
355*cdf0e10cSrcweir     m_bNumeric = sal_False;
356*cdf0e10cSrcweir     m_xOriginalFormatter = NULL;
357*cdf0e10cSrcweir     m_nKeyType = NumberFormat::UNDEFINED;
358*cdf0e10cSrcweir     m_aNullDate = DBTypeConversion::getStandardDate();
359*cdf0e10cSrcweir     m_nFieldType =  DataType::OTHER;
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     // default our formats supplier
362*cdf0e10cSrcweir     increment(m_refCount);
363*cdf0e10cSrcweir     setPropertyToDefaultByHandle(PROPERTY_ID_FORMATSSUPPLIER);
364*cdf0e10cSrcweir     decrement(m_refCount);
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     startAggregatePropertyListening( PROPERTY_FORMATKEY );
367*cdf0e10cSrcweir     startAggregatePropertyListening( PROPERTY_FORMATSSUPPLIER );
368*cdf0e10cSrcweir }
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir //------------------------------------------------------------------
371*cdf0e10cSrcweir OFormattedModel::OFormattedModel(const Reference<XMultiServiceFactory>& _rxFactory)
372*cdf0e10cSrcweir     :OEditBaseModel(_rxFactory, VCL_CONTROLMODEL_FORMATTEDFIELD, FRM_SUN_CONTROL_FORMATTEDFIELD, sal_True, sal_True )
373*cdf0e10cSrcweir                             // use the old control name for compytibility reasons
374*cdf0e10cSrcweir     ,OErrorBroadcaster( OComponentHelper::rBHelper )
375*cdf0e10cSrcweir {
376*cdf0e10cSrcweir     DBG_CTOR(OFormattedModel, NULL);
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir     implConstruct();
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir     m_nClassId = FormComponentType::TEXTFIELD;
381*cdf0e10cSrcweir     initValueProperty( PROPERTY_EFFECTIVE_VALUE, PROPERTY_ID_EFFECTIVE_VALUE );
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir //------------------------------------------------------------------
385*cdf0e10cSrcweir OFormattedModel::OFormattedModel( const OFormattedModel* _pOriginal, const Reference< XMultiServiceFactory >& _rxFactory )
386*cdf0e10cSrcweir     :OEditBaseModel( _pOriginal, _rxFactory )
387*cdf0e10cSrcweir     ,OErrorBroadcaster( OComponentHelper::rBHelper )
388*cdf0e10cSrcweir {
389*cdf0e10cSrcweir     DBG_CTOR(OFormattedModel, NULL);
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir     implConstruct();
392*cdf0e10cSrcweir }
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir //------------------------------------------------------------------------------
395*cdf0e10cSrcweir OFormattedModel::~OFormattedModel()
396*cdf0e10cSrcweir {
397*cdf0e10cSrcweir     DBG_DTOR(OFormattedModel, NULL);
398*cdf0e10cSrcweir }
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir // XCloneable
401*cdf0e10cSrcweir //------------------------------------------------------------------------------
402*cdf0e10cSrcweir IMPLEMENT_DEFAULT_CLONING( OFormattedModel )
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir //------------------------------------------------------------------------------
405*cdf0e10cSrcweir void SAL_CALL OFormattedModel::disposing()
406*cdf0e10cSrcweir {
407*cdf0e10cSrcweir     OErrorBroadcaster::disposing();
408*cdf0e10cSrcweir     OEditBaseModel::disposing();
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir // XServiceInfo
412*cdf0e10cSrcweir //------------------------------------------------------------------------------
413*cdf0e10cSrcweir StringSequence OFormattedModel::getSupportedServiceNames() throw()
414*cdf0e10cSrcweir {
415*cdf0e10cSrcweir     StringSequence aSupported = OEditBaseModel::getSupportedServiceNames();
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir     sal_Int32 nOldLen = aSupported.getLength();
418*cdf0e10cSrcweir     aSupported.realloc( nOldLen + 8 );
419*cdf0e10cSrcweir     ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
422*cdf0e10cSrcweir     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
423*cdf0e10cSrcweir     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
426*cdf0e10cSrcweir     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir     *pStoreTo++ = FRM_SUN_COMPONENT_FORMATTEDFIELD;
429*cdf0e10cSrcweir     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_FORMATTEDFIELD;
430*cdf0e10cSrcweir     *pStoreTo++ = BINDABLE_DATABASE_FORMATTED_FIELD;
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir     return aSupported;
433*cdf0e10cSrcweir }
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir // XAggregation
436*cdf0e10cSrcweir //------------------------------------------------------------------------------
437*cdf0e10cSrcweir Any SAL_CALL OFormattedModel::queryAggregation(const Type& _rType) throw(RuntimeException)
438*cdf0e10cSrcweir {
439*cdf0e10cSrcweir     Any aReturn = OEditBaseModel::queryAggregation( _rType );
440*cdf0e10cSrcweir     return aReturn.hasValue() ? aReturn : OErrorBroadcaster::queryInterface( _rType );
441*cdf0e10cSrcweir }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir // XTypeProvider
444*cdf0e10cSrcweir //------------------------------------------------------------------------------
445*cdf0e10cSrcweir Sequence< Type > OFormattedModel::_getTypes()
446*cdf0e10cSrcweir {
447*cdf0e10cSrcweir     return ::comphelper::concatSequences(
448*cdf0e10cSrcweir         OEditBaseModel::_getTypes(),
449*cdf0e10cSrcweir         OErrorBroadcaster::getTypes()
450*cdf0e10cSrcweir     );
451*cdf0e10cSrcweir }
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir // XPersistObject
454*cdf0e10cSrcweir //------------------------------------------------------------------------------
455*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OFormattedModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
456*cdf0e10cSrcweir {
457*cdf0e10cSrcweir     return ::rtl::OUString(FRM_COMPONENT_EDIT);
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir // XPropertySet
461*cdf0e10cSrcweir //------------------------------------------------------------------------------
462*cdf0e10cSrcweir void OFormattedModel::describeFixedProperties( Sequence< Property >& _rProps ) const
463*cdf0e10cSrcweir {
464*cdf0e10cSrcweir     BEGIN_DESCRIBE_PROPERTIES( 3, OEditBaseModel )
465*cdf0e10cSrcweir         DECL_BOOL_PROP1(EMPTY_IS_NULL,                          BOUND);
466*cdf0e10cSrcweir         DECL_PROP1(TABINDEX,            sal_Int16,              BOUND);
467*cdf0e10cSrcweir         DECL_BOOL_PROP2(FILTERPROPOSAL,                         BOUND, MAYBEDEFAULT);
468*cdf0e10cSrcweir     END_DESCRIBE_PROPERTIES();
469*cdf0e10cSrcweir }
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir //------------------------------------------------------------------------------
472*cdf0e10cSrcweir void OFormattedModel::describeAggregateProperties( Sequence< Property >& _rAggregateProps ) const
473*cdf0e10cSrcweir {
474*cdf0e10cSrcweir     OEditBaseModel::describeAggregateProperties( _rAggregateProps );
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir     // TreatAsNumeric nicht transient : wir wollen es an der UI anbinden (ist noetig, um dem EffectiveDefault
477*cdf0e10cSrcweir     // - der kann Text oder Zahl sein - einen Sinn zu geben)
478*cdf0e10cSrcweir     ModifyPropertyAttributes(_rAggregateProps, PROPERTY_TREATASNUMERIC, 0, PropertyAttribute::TRANSIENT);
479*cdf0e10cSrcweir     // same for FormatKey
480*cdf0e10cSrcweir     // (though the paragraph above for the TreatAsNumeric does not hold anymore - we do not have an UI for this.
481*cdf0e10cSrcweir     // But we have for the format key ...)
482*cdf0e10cSrcweir     // 25.06.2001 - 87862 - frank.schoenheit@sun.com
483*cdf0e10cSrcweir     ModifyPropertyAttributes(_rAggregateProps, PROPERTY_FORMATKEY, 0, PropertyAttribute::TRANSIENT);
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir     RemoveProperty(_rAggregateProps, PROPERTY_STRICTFORMAT);
486*cdf0e10cSrcweir         // no strict format property for formatted fields: it does not make sense, 'cause
487*cdf0e10cSrcweir         // there is no general way to decide which characters/sub strings are allowed during the input of an
488*cdf0e10cSrcweir         // arbitraryly formatted control
489*cdf0e10cSrcweir         // 81441 - 12/07/00 - FS
490*cdf0e10cSrcweir }
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir //------------------------------------------------------------------------------
493*cdf0e10cSrcweir void OFormattedModel::getFastPropertyValue(Any& rValue, sal_Int32 nHandle) const
494*cdf0e10cSrcweir {
495*cdf0e10cSrcweir     OEditBaseModel::getFastPropertyValue(rValue, nHandle);
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir //------------------------------------------------------------------------------
499*cdf0e10cSrcweir void OFormattedModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw ( ::com::sun::star::uno::Exception)
500*cdf0e10cSrcweir {
501*cdf0e10cSrcweir     OEditBaseModel::setFastPropertyValue_NoBroadcast(nHandle, rValue);
502*cdf0e10cSrcweir }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir //------------------------------------------------------------------------------
505*cdf0e10cSrcweir sal_Bool OFormattedModel::convertFastPropertyValue(Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue)
506*cdf0e10cSrcweir                                                         throw( IllegalArgumentException )
507*cdf0e10cSrcweir {
508*cdf0e10cSrcweir     return OEditBaseModel::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
509*cdf0e10cSrcweir }
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir //------------------------------------------------------------------------------
512*cdf0e10cSrcweir void OFormattedModel::setPropertyToDefaultByHandle(sal_Int32 nHandle)
513*cdf0e10cSrcweir {
514*cdf0e10cSrcweir     if (nHandle == PROPERTY_ID_FORMATSSUPPLIER)
515*cdf0e10cSrcweir     {
516*cdf0e10cSrcweir         Reference<XNumberFormatsSupplier>  xSupplier = calcDefaultFormatsSupplier();
517*cdf0e10cSrcweir         DBG_ASSERT(m_xAggregateSet.is(), "OFormattedModel::setPropertyToDefaultByHandle(FORMATSSUPPLIER) : have no aggregate !");
518*cdf0e10cSrcweir         if (m_xAggregateSet.is())
519*cdf0e10cSrcweir             m_xAggregateSet->setPropertyValue(PROPERTY_FORMATSSUPPLIER, makeAny(xSupplier));
520*cdf0e10cSrcweir     }
521*cdf0e10cSrcweir     else
522*cdf0e10cSrcweir         OEditBaseModel::setPropertyToDefaultByHandle(nHandle);
523*cdf0e10cSrcweir }
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir //------------------------------------------------------------------------------
526*cdf0e10cSrcweir void OFormattedModel::setPropertyToDefault(const ::rtl::OUString& aPropertyName) throw( com::sun::star::beans::UnknownPropertyException, RuntimeException )
527*cdf0e10cSrcweir {
528*cdf0e10cSrcweir     OPropertyArrayAggregationHelper& rPH = m_aPropertyBagHelper.getInfoHelper();
529*cdf0e10cSrcweir     sal_Int32 nHandle = rPH.getHandleByName( aPropertyName );
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir     if (nHandle == PROPERTY_ID_FORMATSSUPPLIER)
532*cdf0e10cSrcweir         setPropertyToDefaultByHandle(PROPERTY_ID_FORMATSSUPPLIER);
533*cdf0e10cSrcweir     else
534*cdf0e10cSrcweir         OEditBaseModel::setPropertyToDefault(aPropertyName);
535*cdf0e10cSrcweir }
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir //------------------------------------------------------------------------------
538*cdf0e10cSrcweir Any OFormattedModel::getPropertyDefaultByHandle( sal_Int32 nHandle ) const
539*cdf0e10cSrcweir {
540*cdf0e10cSrcweir     if (nHandle == PROPERTY_ID_FORMATSSUPPLIER)
541*cdf0e10cSrcweir     {
542*cdf0e10cSrcweir         Reference<XNumberFormatsSupplier>  xSupplier = calcDefaultFormatsSupplier();
543*cdf0e10cSrcweir         return makeAny(xSupplier);
544*cdf0e10cSrcweir     }
545*cdf0e10cSrcweir     else
546*cdf0e10cSrcweir         return OEditBaseModel::getPropertyDefaultByHandle(nHandle);
547*cdf0e10cSrcweir }
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir //------------------------------------------------------------------------------
550*cdf0e10cSrcweir Any SAL_CALL OFormattedModel::getPropertyDefault( const ::rtl::OUString& aPropertyName ) throw( com::sun::star::beans::UnknownPropertyException, RuntimeException )
551*cdf0e10cSrcweir {
552*cdf0e10cSrcweir     OPropertyArrayAggregationHelper& rPH = m_aPropertyBagHelper.getInfoHelper();
553*cdf0e10cSrcweir     sal_Int32 nHandle = rPH.getHandleByName( aPropertyName );
554*cdf0e10cSrcweir 
555*cdf0e10cSrcweir     if (nHandle == PROPERTY_ID_FORMATSSUPPLIER)
556*cdf0e10cSrcweir         return getPropertyDefaultByHandle(PROPERTY_ID_FORMATSSUPPLIER);
557*cdf0e10cSrcweir     else
558*cdf0e10cSrcweir         return OEditBaseModel::getPropertyDefault(aPropertyName);
559*cdf0e10cSrcweir }
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir //------------------------------------------------------------------------------
562*cdf0e10cSrcweir void OFormattedModel::_propertyChanged( const com::sun::star::beans::PropertyChangeEvent& evt ) throw(RuntimeException)
563*cdf0e10cSrcweir {
564*cdf0e10cSrcweir     // TODO: check how this works with external bindings
565*cdf0e10cSrcweir 
566*cdf0e10cSrcweir     OSL_ENSURE( evt.Source == m_xAggregateSet, "OFormattedModel::_propertyChanged: where did this come from?" );
567*cdf0e10cSrcweir     if ( evt.Source == m_xAggregateSet )
568*cdf0e10cSrcweir     {
569*cdf0e10cSrcweir         Reference< XPropertySet > xSourceSet( evt.Source, UNO_QUERY );
570*cdf0e10cSrcweir         if ( evt.PropertyName.equals( PROPERTY_FORMATKEY ) )
571*cdf0e10cSrcweir         {
572*cdf0e10cSrcweir             if ( evt.NewValue.getValueType().getTypeClass() == TypeClass_LONG )
573*cdf0e10cSrcweir             {
574*cdf0e10cSrcweir                 try
575*cdf0e10cSrcweir                 {
576*cdf0e10cSrcweir                     ::osl::MutexGuard aGuard( m_aMutex );
577*cdf0e10cSrcweir 
578*cdf0e10cSrcweir                     Reference<XNumberFormatsSupplier> xSupplier( calcFormatsSupplier() );
579*cdf0e10cSrcweir                     m_nKeyType  = getNumberFormatType(xSupplier->getNumberFormats(), getINT32( evt.NewValue ) );
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir                     // as m_aSaveValue (which is used by commitControlValueToDbColumn) is format dependent we have
582*cdf0e10cSrcweir                     // to recalc it, which is done by translateDbColumnToControlValue
583*cdf0e10cSrcweir                     if ( m_xColumn.is() && m_xAggregateFastSet.is()  && !m_xCursor->isBeforeFirst() && !m_xCursor->isAfterLast())
584*cdf0e10cSrcweir                     {
585*cdf0e10cSrcweir                         setControlValue( translateDbColumnToControlValue(), eOther );
586*cdf0e10cSrcweir                     }
587*cdf0e10cSrcweir 
588*cdf0e10cSrcweir                     // if we're connected to an external value binding, then re-calculate the type
589*cdf0e10cSrcweir                     // used to exchange the value - it depends on the format, too
590*cdf0e10cSrcweir                     if ( hasExternalValueBinding() )
591*cdf0e10cSrcweir                     {
592*cdf0e10cSrcweir                         calculateExternalValueType();
593*cdf0e10cSrcweir                     }
594*cdf0e10cSrcweir                 }
595*cdf0e10cSrcweir                 catch(Exception&)
596*cdf0e10cSrcweir                 {
597*cdf0e10cSrcweir                 }
598*cdf0e10cSrcweir             }
599*cdf0e10cSrcweir             return;
600*cdf0e10cSrcweir         }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir         if ( evt.PropertyName.equals( PROPERTY_FORMATSSUPPLIER ) )
603*cdf0e10cSrcweir         {
604*cdf0e10cSrcweir             updateFormatterNullDate();
605*cdf0e10cSrcweir             return;
606*cdf0e10cSrcweir         }
607*cdf0e10cSrcweir 
608*cdf0e10cSrcweir         OBoundControlModel::_propertyChanged( evt );
609*cdf0e10cSrcweir     }
610*cdf0e10cSrcweir }
611*cdf0e10cSrcweir 
612*cdf0e10cSrcweir //------------------------------------------------------------------------------
613*cdf0e10cSrcweir void OFormattedModel::updateFormatterNullDate()
614*cdf0e10cSrcweir {
615*cdf0e10cSrcweir     // calc the current NULL date
616*cdf0e10cSrcweir     Reference< XNumberFormatsSupplier > xSupplier( calcFormatsSupplier() );
617*cdf0e10cSrcweir     if ( xSupplier.is() )
618*cdf0e10cSrcweir         xSupplier->getNumberFormatSettings()->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NullDate" ) ) ) >>= m_aNullDate;
619*cdf0e10cSrcweir }
620*cdf0e10cSrcweir 
621*cdf0e10cSrcweir //------------------------------------------------------------------------------
622*cdf0e10cSrcweir Reference< XNumberFormatsSupplier > OFormattedModel::calcFormatsSupplier() const
623*cdf0e10cSrcweir {
624*cdf0e10cSrcweir     Reference<XNumberFormatsSupplier>  xSupplier;
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir     DBG_ASSERT(m_xAggregateSet.is(), "OFormattedModel::calcFormatsSupplier : have no aggregate !");
627*cdf0e10cSrcweir     // hat mein aggregiertes Model einen FormatSupplier ?
628*cdf0e10cSrcweir     if( m_xAggregateSet.is() )
629*cdf0e10cSrcweir         m_xAggregateSet->getPropertyValue(PROPERTY_FORMATSSUPPLIER) >>= xSupplier;
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir     if (!xSupplier.is())
632*cdf0e10cSrcweir         // check if my parent form has a supplier
633*cdf0e10cSrcweir         xSupplier = calcFormFormatsSupplier();
634*cdf0e10cSrcweir 
635*cdf0e10cSrcweir     if (!xSupplier.is())
636*cdf0e10cSrcweir         xSupplier = calcDefaultFormatsSupplier();
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir     DBG_ASSERT(xSupplier.is(), "OFormattedModel::calcFormatsSupplier : no supplier !");
639*cdf0e10cSrcweir         // jetzt sollte aber einer da sein
640*cdf0e10cSrcweir     return xSupplier;
641*cdf0e10cSrcweir }
642*cdf0e10cSrcweir 
643*cdf0e10cSrcweir //------------------------------------------------------------------------------
644*cdf0e10cSrcweir Reference<XNumberFormatsSupplier>  OFormattedModel::calcFormFormatsSupplier() const
645*cdf0e10cSrcweir {
646*cdf0e10cSrcweir     Reference<XChild>  xMe;
647*cdf0e10cSrcweir     query_interface(static_cast<XWeak*>(const_cast<OFormattedModel*>(this)), xMe);
648*cdf0e10cSrcweir     // damit stellen wir sicher, dass wir auch fuer den Fall der Aggregation das richtige
649*cdf0e10cSrcweir     // Objekt bekommen
650*cdf0e10cSrcweir     DBG_ASSERT(xMe.is(), "OFormattedModel::calcFormFormatsSupplier : I should have a content interface !");
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir     // jetzt durchhangeln nach oben, bis wir auf eine starform treffen (angefangen mit meinem eigenen Parent)
653*cdf0e10cSrcweir     Reference<XChild>  xParent(xMe->getParent(), UNO_QUERY);
654*cdf0e10cSrcweir     Reference<XForm>  xNextParentForm(xParent, UNO_QUERY);
655*cdf0e10cSrcweir     while (!xNextParentForm.is() && xParent.is())
656*cdf0e10cSrcweir     {
657*cdf0e10cSrcweir         xParent         = xParent.query( xParent->getParent() );
658*cdf0e10cSrcweir         xNextParentForm = xNextParentForm.query( xParent );
659*cdf0e10cSrcweir     }
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir     if (!xNextParentForm.is())
662*cdf0e10cSrcweir     {
663*cdf0e10cSrcweir         DBG_ERROR("OFormattedModel::calcFormFormatsSupplier : have no ancestor which is a form !");
664*cdf0e10cSrcweir         return NULL;
665*cdf0e10cSrcweir     }
666*cdf0e10cSrcweir 
667*cdf0e10cSrcweir     // den FormatSupplier von meinem Vorfahren (falls der einen hat)
668*cdf0e10cSrcweir     Reference< XRowSet > xRowSet( xNextParentForm, UNO_QUERY );
669*cdf0e10cSrcweir     Reference< XNumberFormatsSupplier > xSupplier;
670*cdf0e10cSrcweir     if (xRowSet.is())
671*cdf0e10cSrcweir         xSupplier = getNumberFormats( getConnection(xRowSet), sal_True, getContext().getLegacyServiceFactory() );
672*cdf0e10cSrcweir     return xSupplier;
673*cdf0e10cSrcweir }
674*cdf0e10cSrcweir 
675*cdf0e10cSrcweir //------------------------------------------------------------------------------
676*cdf0e10cSrcweir Reference< XNumberFormatsSupplier > OFormattedModel::calcDefaultFormatsSupplier() const
677*cdf0e10cSrcweir {
678*cdf0e10cSrcweir     return StandardFormatsSupplier::get( getContext().getLegacyServiceFactory() );
679*cdf0e10cSrcweir }
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir // XBoundComponent
682*cdf0e10cSrcweir //------------------------------------------------------------------------------
683*cdf0e10cSrcweir void OFormattedModel::loaded(const EventObject& rEvent) throw ( ::com::sun::star::uno::RuntimeException)
684*cdf0e10cSrcweir {
685*cdf0e10cSrcweir     // HACK : our onConnectedDbColumn accesses our NumberFormatter which locks the solar mutex (as it doesn't have
686*cdf0e10cSrcweir     // an own one). To prevent deadlocks with other threads which may request a property from us in an
687*cdf0e10cSrcweir     // UI-triggered action (e.g. an tooltip) we lock the solar mutex _here_ before our base class locks
688*cdf0e10cSrcweir     // it's own muext (which is used for property requests)
689*cdf0e10cSrcweir     // alternative a): we use two mutexes, one which is passed to the OPropertysetHelper and used for
690*cdf0e10cSrcweir     // property requests and one for our own code. This would need a lot of code rewriting
691*cdf0e10cSrcweir     // alternative b): The NumberFormatter has to be really threadsafe (with an own mutex), which is
692*cdf0e10cSrcweir     // the only "clean" solution for me.
693*cdf0e10cSrcweir     // FS - 69603 - 02.11.99
694*cdf0e10cSrcweir 
695*cdf0e10cSrcweir     ::vos::OGuard aGuard(Application::GetSolarMutex());
696*cdf0e10cSrcweir     OEditBaseModel::loaded(rEvent);
697*cdf0e10cSrcweir }
698*cdf0e10cSrcweir 
699*cdf0e10cSrcweir //------------------------------------------------------------------------------
700*cdf0e10cSrcweir void OFormattedModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
701*cdf0e10cSrcweir {
702*cdf0e10cSrcweir     m_xOriginalFormatter = NULL;
703*cdf0e10cSrcweir 
704*cdf0e10cSrcweir     // get some properties of the field
705*cdf0e10cSrcweir     m_nFieldType = DataType::OTHER;
706*cdf0e10cSrcweir     Reference<XPropertySet> xField = getField();
707*cdf0e10cSrcweir     if ( xField.is() )
708*cdf0e10cSrcweir         xField->getPropertyValue( PROPERTY_FIELDTYPE ) >>= m_nFieldType;
709*cdf0e10cSrcweir 
710*cdf0e10cSrcweir     sal_Int32 nFormatKey = 0;
711*cdf0e10cSrcweir 
712*cdf0e10cSrcweir     DBG_ASSERT(m_xAggregateSet.is(), "OFormattedModel::onConnectedDbColumn : have no aggregate !");
713*cdf0e10cSrcweir     if (m_xAggregateSet.is())
714*cdf0e10cSrcweir     {   // all the following doesn't make any sense if we have no aggregate ...
715*cdf0e10cSrcweir         Any aSupplier = m_xAggregateSet->getPropertyValue(PROPERTY_FORMATSSUPPLIER);
716*cdf0e10cSrcweir         DBG_ASSERT( aSupplier.hasValue(), "OFormattedModel::onConnectedDbColumn : invalid property value !" );
717*cdf0e10cSrcweir         // das sollte im Constructor oder im read auf was richtiges gesetzt worden sein
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir         Any aFmtKey = m_xAggregateSet->getPropertyValue(PROPERTY_FORMATKEY);
720*cdf0e10cSrcweir         if ( !(aFmtKey >>= nFormatKey ) )
721*cdf0e10cSrcweir         {   // nobody gave us a format to use. So we examine the field we're bound to for a
722*cdf0e10cSrcweir             // format key, and use it ourself, too
723*cdf0e10cSrcweir             sal_Int32 nType = DataType::VARCHAR;
724*cdf0e10cSrcweir             if (xField.is())
725*cdf0e10cSrcweir             {
726*cdf0e10cSrcweir                 aFmtKey = xField->getPropertyValue(PROPERTY_FORMATKEY);
727*cdf0e10cSrcweir                 xField->getPropertyValue(PROPERTY_FIELDTYPE) >>= nType ;
728*cdf0e10cSrcweir             }
729*cdf0e10cSrcweir 
730*cdf0e10cSrcweir             Reference<XNumberFormatsSupplier>  xSupplier = calcFormFormatsSupplier();
731*cdf0e10cSrcweir             DBG_ASSERT(xSupplier.is(), "OFormattedModel::onConnectedDbColumn : bound to a field but no parent with a formatter ? how this ?");
732*cdf0e10cSrcweir             if (xSupplier.is())
733*cdf0e10cSrcweir             {
734*cdf0e10cSrcweir                 m_bOriginalNumeric = getBOOL(getPropertyValue(PROPERTY_TREATASNUMERIC));
735*cdf0e10cSrcweir 
736*cdf0e10cSrcweir                 if (!aFmtKey.hasValue())
737*cdf0e10cSrcweir                 {   // we aren't bound to a field (or this field's format is invalid)
738*cdf0e10cSrcweir                     // -> determine the standard text (or numeric) format of the supplier
739*cdf0e10cSrcweir                     Reference<XNumberFormatTypes>  xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
740*cdf0e10cSrcweir                     if (xTypes.is())
741*cdf0e10cSrcweir                     {
742*cdf0e10cSrcweir                         Locale aApplicationLocale = Application::GetSettings().GetUILocale();
743*cdf0e10cSrcweir 
744*cdf0e10cSrcweir                         if (m_bOriginalNumeric)
745*cdf0e10cSrcweir                             aFmtKey <<= (sal_Int32)xTypes->getStandardFormat(NumberFormat::NUMBER, aApplicationLocale);
746*cdf0e10cSrcweir                         else
747*cdf0e10cSrcweir                             aFmtKey <<= (sal_Int32)xTypes->getStandardFormat(NumberFormat::TEXT, aApplicationLocale);
748*cdf0e10cSrcweir                     }
749*cdf0e10cSrcweir                 }
750*cdf0e10cSrcweir 
751*cdf0e10cSrcweir                 aSupplier >>= m_xOriginalFormatter;
752*cdf0e10cSrcweir                 m_xAggregateSet->setPropertyValue(PROPERTY_FORMATSSUPPLIER, makeAny(xSupplier));
753*cdf0e10cSrcweir                 m_xAggregateSet->setPropertyValue(PROPERTY_FORMATKEY, aFmtKey);
754*cdf0e10cSrcweir 
755*cdf0e10cSrcweir                 // das Numeric-Flag an mein gebundenes Feld anpassen
756*cdf0e10cSrcweir                 if (xField.is())
757*cdf0e10cSrcweir                 {
758*cdf0e10cSrcweir                     m_bNumeric = sal_False;
759*cdf0e10cSrcweir                     switch (nType)
760*cdf0e10cSrcweir                     {
761*cdf0e10cSrcweir                         case DataType::BIT:
762*cdf0e10cSrcweir                         case DataType::BOOLEAN:
763*cdf0e10cSrcweir                         case DataType::TINYINT:
764*cdf0e10cSrcweir                         case DataType::SMALLINT:
765*cdf0e10cSrcweir                         case DataType::INTEGER:
766*cdf0e10cSrcweir                         case DataType::BIGINT:
767*cdf0e10cSrcweir                         case DataType::FLOAT:
768*cdf0e10cSrcweir                         case DataType::REAL:
769*cdf0e10cSrcweir                         case DataType::DOUBLE:
770*cdf0e10cSrcweir                         case DataType::NUMERIC:
771*cdf0e10cSrcweir                         case DataType::DECIMAL:
772*cdf0e10cSrcweir                         case DataType::DATE:
773*cdf0e10cSrcweir                         case DataType::TIME:
774*cdf0e10cSrcweir                         case DataType::TIMESTAMP:
775*cdf0e10cSrcweir                             m_bNumeric = sal_True;
776*cdf0e10cSrcweir                             break;
777*cdf0e10cSrcweir                     }
778*cdf0e10cSrcweir                 }
779*cdf0e10cSrcweir                 else
780*cdf0e10cSrcweir                     m_bNumeric = m_bOriginalNumeric;
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir                 setPropertyValue(PROPERTY_TREATASNUMERIC, makeAny((sal_Bool)m_bNumeric));
783*cdf0e10cSrcweir 
784*cdf0e10cSrcweir                 OSL_VERIFY( aFmtKey >>= nFormatKey );
785*cdf0e10cSrcweir             }
786*cdf0e10cSrcweir         }
787*cdf0e10cSrcweir     }
788*cdf0e10cSrcweir 
789*cdf0e10cSrcweir     Reference<XNumberFormatsSupplier>  xSupplier = calcFormatsSupplier();
790*cdf0e10cSrcweir     m_bNumeric = getBOOL( getPropertyValue( PROPERTY_TREATASNUMERIC ) );
791*cdf0e10cSrcweir     m_nKeyType  = getNumberFormatType( xSupplier->getNumberFormats(), nFormatKey );
792*cdf0e10cSrcweir     xSupplier->getNumberFormatSettings()->getPropertyValue( ::rtl::OUString::createFromAscii("NullDate") ) >>= m_aNullDate;
793*cdf0e10cSrcweir 
794*cdf0e10cSrcweir     OEditBaseModel::onConnectedDbColumn( _rxForm );
795*cdf0e10cSrcweir }
796*cdf0e10cSrcweir 
797*cdf0e10cSrcweir //------------------------------------------------------------------------------
798*cdf0e10cSrcweir void OFormattedModel::onDisconnectedDbColumn()
799*cdf0e10cSrcweir {
800*cdf0e10cSrcweir     OEditBaseModel::onDisconnectedDbColumn();
801*cdf0e10cSrcweir     if (m_xOriginalFormatter.is())
802*cdf0e10cSrcweir     {   // unser aggregiertes Model hatte keinerlei Format-Informationen
803*cdf0e10cSrcweir         m_xAggregateSet->setPropertyValue(PROPERTY_FORMATSSUPPLIER, makeAny(m_xOriginalFormatter));
804*cdf0e10cSrcweir         m_xAggregateSet->setPropertyValue(PROPERTY_FORMATKEY, Any());
805*cdf0e10cSrcweir         setPropertyValue(PROPERTY_TREATASNUMERIC, makeAny((sal_Bool)m_bOriginalNumeric));
806*cdf0e10cSrcweir         m_xOriginalFormatter = NULL;
807*cdf0e10cSrcweir     }
808*cdf0e10cSrcweir 
809*cdf0e10cSrcweir     m_nFieldType = DataType::OTHER;
810*cdf0e10cSrcweir     m_nKeyType   = NumberFormat::UNDEFINED;
811*cdf0e10cSrcweir     m_aNullDate  = DBTypeConversion::getStandardDate();
812*cdf0e10cSrcweir }
813*cdf0e10cSrcweir 
814*cdf0e10cSrcweir //------------------------------------------------------------------------------
815*cdf0e10cSrcweir void OFormattedModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
816*cdf0e10cSrcweir {
817*cdf0e10cSrcweir     OEditBaseModel::write(_rxOutStream);
818*cdf0e10cSrcweir     _rxOutStream->writeShort(0x0003);
819*cdf0e10cSrcweir 
820*cdf0e10cSrcweir     DBG_ASSERT(m_xAggregateSet.is(), "OFormattedModel::write : have no aggregate !");
821*cdf0e10cSrcweir 
822*cdf0e10cSrcweir     // mein Format (evtl. void) in ein persistentes Format bringen (der Supplier zusammen mit dem Key ist es zwar auch,
823*cdf0e10cSrcweir     // aber deswegen muessen wir ja nicht gleich den ganzen Supplier speichern, das waere ein klein wenig Overhead ;)
824*cdf0e10cSrcweir 
825*cdf0e10cSrcweir         Reference<XNumberFormatsSupplier>  xSupplier;
826*cdf0e10cSrcweir         Any aFmtKey;
827*cdf0e10cSrcweir     sal_Bool bVoidKey = sal_True;
828*cdf0e10cSrcweir     if (m_xAggregateSet.is())
829*cdf0e10cSrcweir     {
830*cdf0e10cSrcweir         Any aSupplier = m_xAggregateSet->getPropertyValue(PROPERTY_FORMATSSUPPLIER);
831*cdf0e10cSrcweir         if (aSupplier.getValueType().getTypeClass() != TypeClass_VOID)
832*cdf0e10cSrcweir         {
833*cdf0e10cSrcweir             OSL_VERIFY( aSupplier >>= xSupplier );
834*cdf0e10cSrcweir         }
835*cdf0e10cSrcweir 
836*cdf0e10cSrcweir         aFmtKey = m_xAggregateSet->getPropertyValue(PROPERTY_FORMATKEY);
837*cdf0e10cSrcweir         bVoidKey = (!xSupplier.is() || !aFmtKey.hasValue()) || (isLoaded() && m_xOriginalFormatter.is());
838*cdf0e10cSrcweir             // (kein Fomatter und/oder Key) oder (loaded und faked Formatter)
839*cdf0e10cSrcweir     }
840*cdf0e10cSrcweir 
841*cdf0e10cSrcweir     _rxOutStream->writeBoolean(!bVoidKey);
842*cdf0e10cSrcweir     if (!bVoidKey)
843*cdf0e10cSrcweir     {
844*cdf0e10cSrcweir         // aus dem FormatKey und dem Formatter persistente Angaben basteln
845*cdf0e10cSrcweir 
846*cdf0e10cSrcweir         Any aKey = m_xAggregateSet->getPropertyValue(PROPERTY_FORMATKEY);
847*cdf0e10cSrcweir         sal_Int32 nKey = aKey.hasValue() ? getINT32(aKey) : 0;
848*cdf0e10cSrcweir 
849*cdf0e10cSrcweir         Reference<XNumberFormats>  xFormats = xSupplier->getNumberFormats();
850*cdf0e10cSrcweir 
851*cdf0e10cSrcweir         ::rtl::OUString         sFormatDescription;
852*cdf0e10cSrcweir         LanguageType    eFormatLanguage = LANGUAGE_DONTKNOW;
853*cdf0e10cSrcweir 
854*cdf0e10cSrcweir         static const ::rtl::OUString s_aLocaleProp = ::rtl::OUString::createFromAscii("Locale");
855*cdf0e10cSrcweir         Reference<com::sun::star::beans::XPropertySet>  xFormat = xFormats->getByKey(nKey);
856*cdf0e10cSrcweir         if (hasProperty(s_aLocaleProp, xFormat))
857*cdf0e10cSrcweir         {
858*cdf0e10cSrcweir             Any aLocale = xFormat->getPropertyValue(s_aLocaleProp);
859*cdf0e10cSrcweir             DBG_ASSERT(isA(aLocale, static_cast<Locale*>(NULL)), "OFormattedModel::write : invalid language property !");
860*cdf0e10cSrcweir             if (isA(aLocale, static_cast<Locale*>(NULL)))
861*cdf0e10cSrcweir             {
862*cdf0e10cSrcweir                 Locale* pLocale = (Locale*)aLocale.getValue();
863*cdf0e10cSrcweir                 eFormatLanguage = MsLangId::convertLocaleToLanguage( *pLocale );
864*cdf0e10cSrcweir             }
865*cdf0e10cSrcweir         }
866*cdf0e10cSrcweir 
867*cdf0e10cSrcweir         static const ::rtl::OUString s_aFormatStringProp = ::rtl::OUString::createFromAscii("FormatString");
868*cdf0e10cSrcweir         if (hasProperty(s_aFormatStringProp, xFormat))
869*cdf0e10cSrcweir             xFormat->getPropertyValue(s_aFormatStringProp) >>= sFormatDescription;
870*cdf0e10cSrcweir 
871*cdf0e10cSrcweir         _rxOutStream->writeUTF(sFormatDescription);
872*cdf0e10cSrcweir         _rxOutStream->writeLong((sal_Int32)eFormatLanguage);
873*cdf0e10cSrcweir     }
874*cdf0e10cSrcweir 
875*cdf0e10cSrcweir     // version 2 : write the properties common to all OEditBaseModels
876*cdf0e10cSrcweir     writeCommonEditProperties(_rxOutStream);
877*cdf0e10cSrcweir 
878*cdf0e10cSrcweir     // version 3 : write the effective value property of the aggregate
879*cdf0e10cSrcweir     // Due to a bug within the UnoControlFormattedFieldModel implementation (our default aggregate) this props value isn't correctly read
880*cdf0e10cSrcweir     // and this can't be corrected without being incompatible.
881*cdf0e10cSrcweir     // so we have our own handling.
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir     // and to be a little bit more compatible we make the following section skippable
884*cdf0e10cSrcweir     {
885*cdf0e10cSrcweir         Reference< XDataOutputStream > xOut(_rxOutStream, UNO_QUERY);
886*cdf0e10cSrcweir         OStreamSection aDownCompat(xOut);
887*cdf0e10cSrcweir 
888*cdf0e10cSrcweir         // a sub version within the skippable block
889*cdf0e10cSrcweir         _rxOutStream->writeShort(0x0000);
890*cdf0e10cSrcweir 
891*cdf0e10cSrcweir         // version 0: the effective value of the aggregate
892*cdf0e10cSrcweir                 Any aEffectiveValue;
893*cdf0e10cSrcweir         if (m_xAggregateSet.is())
894*cdf0e10cSrcweir         {
895*cdf0e10cSrcweir             try { aEffectiveValue = m_xAggregateSet->getPropertyValue(PROPERTY_EFFECTIVE_VALUE); } catch(Exception&) { }
896*cdf0e10cSrcweir         }
897*cdf0e10cSrcweir 
898*cdf0e10cSrcweir         {
899*cdf0e10cSrcweir             OStreamSection aDownCompat2(xOut);
900*cdf0e10cSrcweir             switch (aEffectiveValue.getValueType().getTypeClass())
901*cdf0e10cSrcweir             {
902*cdf0e10cSrcweir                 case TypeClass_STRING:
903*cdf0e10cSrcweir                     _rxOutStream->writeShort(0x0000);
904*cdf0e10cSrcweir                     _rxOutStream->writeUTF(::comphelper::getString(aEffectiveValue));
905*cdf0e10cSrcweir                     break;
906*cdf0e10cSrcweir                 case TypeClass_DOUBLE:
907*cdf0e10cSrcweir                     _rxOutStream->writeShort(0x0001);
908*cdf0e10cSrcweir                     _rxOutStream->writeDouble(::comphelper::getDouble(aEffectiveValue));
909*cdf0e10cSrcweir                     break;
910*cdf0e10cSrcweir                 default:    // void and all unknown states
911*cdf0e10cSrcweir                     DBG_ASSERT(!aEffectiveValue.hasValue(), "FmXFormattedModel::write : unknown property value type !");
912*cdf0e10cSrcweir                     _rxOutStream->writeShort(0x0002);
913*cdf0e10cSrcweir                     break;
914*cdf0e10cSrcweir             }
915*cdf0e10cSrcweir         }
916*cdf0e10cSrcweir     }
917*cdf0e10cSrcweir }
918*cdf0e10cSrcweir 
919*cdf0e10cSrcweir //------------------------------------------------------------------------------
920*cdf0e10cSrcweir void OFormattedModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
921*cdf0e10cSrcweir {
922*cdf0e10cSrcweir     OEditBaseModel::read(_rxInStream);
923*cdf0e10cSrcweir     sal_uInt16 nVersion = _rxInStream->readShort();
924*cdf0e10cSrcweir 
925*cdf0e10cSrcweir     Reference<XNumberFormatsSupplier>  xSupplier;
926*cdf0e10cSrcweir     sal_Int32 nKey = -1;
927*cdf0e10cSrcweir     switch (nVersion)
928*cdf0e10cSrcweir     {
929*cdf0e10cSrcweir         case 0x0001 :
930*cdf0e10cSrcweir         case 0x0002 :
931*cdf0e10cSrcweir         case 0x0003 :
932*cdf0e10cSrcweir         {
933*cdf0e10cSrcweir             sal_Bool bNonVoidKey = _rxInStream->readBoolean();
934*cdf0e10cSrcweir             if (bNonVoidKey)
935*cdf0e10cSrcweir             {
936*cdf0e10cSrcweir                 // den String und die Language lesen ....
937*cdf0e10cSrcweir                 ::rtl::OUString sFormatDescription = _rxInStream->readUTF();
938*cdf0e10cSrcweir                 LanguageType eDescriptionLanguage = (LanguageType)_rxInStream->readLong();
939*cdf0e10cSrcweir 
940*cdf0e10cSrcweir                 // und daraus von einem Formatter zu einem Key zusammenwuerfeln lassen ...
941*cdf0e10cSrcweir                 xSupplier = calcFormatsSupplier();
942*cdf0e10cSrcweir                     // calcFormatsSupplier nimmt erst den vom Model, dann einen von der starform, dann einen ganz neuen ....
943*cdf0e10cSrcweir                 Reference<XNumberFormats>  xFormats = xSupplier->getNumberFormats();
944*cdf0e10cSrcweir 
945*cdf0e10cSrcweir                 if (xFormats.is())
946*cdf0e10cSrcweir                 {
947*cdf0e10cSrcweir                     Locale aDescriptionLanguage( MsLangId::convertLanguageToLocale(eDescriptionLanguage));
948*cdf0e10cSrcweir 
949*cdf0e10cSrcweir                     nKey = xFormats->queryKey(sFormatDescription, aDescriptionLanguage, sal_False);
950*cdf0e10cSrcweir                     if (nKey == (sal_Int32)-1)
951*cdf0e10cSrcweir                     {   // noch nicht vorhanden in meinem Formatter ...
952*cdf0e10cSrcweir                         nKey = xFormats->addNew(sFormatDescription, aDescriptionLanguage);
953*cdf0e10cSrcweir                     }
954*cdf0e10cSrcweir                 }
955*cdf0e10cSrcweir             }
956*cdf0e10cSrcweir             if ((nVersion == 0x0002) || (nVersion == 0x0003))
957*cdf0e10cSrcweir                 readCommonEditProperties(_rxInStream);
958*cdf0e10cSrcweir 
959*cdf0e10cSrcweir             if (nVersion == 0x0003)
960*cdf0e10cSrcweir             {   // since version 3 there is a "skippable" block at this position
961*cdf0e10cSrcweir                 Reference< XDataInputStream > xIn(_rxInStream, UNO_QUERY);
962*cdf0e10cSrcweir                 OStreamSection aDownCompat(xIn);
963*cdf0e10cSrcweir 
964*cdf0e10cSrcweir                 sal_Int16 nSubVersion = _rxInStream->readShort();
965*cdf0e10cSrcweir                 (void)nSubVersion;
966*cdf0e10cSrcweir 
967*cdf0e10cSrcweir                 // version 0 and higher : the "effective value" property
968*cdf0e10cSrcweir                 Any aEffectiveValue;
969*cdf0e10cSrcweir                 {
970*cdf0e10cSrcweir                     OStreamSection aDownCompat2(xIn);
971*cdf0e10cSrcweir                     switch (_rxInStream->readShort())
972*cdf0e10cSrcweir                     {
973*cdf0e10cSrcweir                         case 0: // String
974*cdf0e10cSrcweir                             aEffectiveValue <<= _rxInStream->readUTF();
975*cdf0e10cSrcweir                             break;
976*cdf0e10cSrcweir                         case 1: // double
977*cdf0e10cSrcweir                             aEffectiveValue <<= (double)_rxInStream->readDouble();
978*cdf0e10cSrcweir                             break;
979*cdf0e10cSrcweir                         case 2:
980*cdf0e10cSrcweir                             break;
981*cdf0e10cSrcweir                         case 3:
982*cdf0e10cSrcweir                             DBG_ERROR("FmXFormattedModel::read : unknown effective value type !");
983*cdf0e10cSrcweir                     }
984*cdf0e10cSrcweir                 }
985*cdf0e10cSrcweir 
986*cdf0e10cSrcweir                 // this property is only to be set if we have no control source : in all other cases the base class did a
987*cdf0e10cSrcweir                 // reset after it's read and this set the effective value to a default value
988*cdf0e10cSrcweir                 if ( m_xAggregateSet.is() && ( getControlSource().getLength() == 0 ) )
989*cdf0e10cSrcweir                 {
990*cdf0e10cSrcweir                     try
991*cdf0e10cSrcweir                     {
992*cdf0e10cSrcweir                         m_xAggregateSet->setPropertyValue(PROPERTY_EFFECTIVE_VALUE, aEffectiveValue);
993*cdf0e10cSrcweir                     }
994*cdf0e10cSrcweir                     catch(Exception&)
995*cdf0e10cSrcweir                     {
996*cdf0e10cSrcweir                     }
997*cdf0e10cSrcweir                 }
998*cdf0e10cSrcweir             }
999*cdf0e10cSrcweir         }
1000*cdf0e10cSrcweir         break;
1001*cdf0e10cSrcweir         default :
1002*cdf0e10cSrcweir             DBG_ERROR("OFormattedModel::read : unknown version !");
1003*cdf0e10cSrcweir             // dann bleibt das Format des aggregierten Sets, wie es bei der Erzeugung ist : void
1004*cdf0e10cSrcweir             defaultCommonEditProperties();
1005*cdf0e10cSrcweir             break;
1006*cdf0e10cSrcweir     }
1007*cdf0e10cSrcweir 
1008*cdf0e10cSrcweir     if ((nKey != -1) && m_xAggregateSet.is())
1009*cdf0e10cSrcweir     {
1010*cdf0e10cSrcweir                 m_xAggregateSet->setPropertyValue(PROPERTY_FORMATSSUPPLIER, makeAny(xSupplier));
1011*cdf0e10cSrcweir                 m_xAggregateSet->setPropertyValue(PROPERTY_FORMATKEY, makeAny((sal_Int32)nKey));
1012*cdf0e10cSrcweir     }
1013*cdf0e10cSrcweir     else
1014*cdf0e10cSrcweir     {
1015*cdf0e10cSrcweir         setPropertyToDefault(PROPERTY_FORMATSSUPPLIER);
1016*cdf0e10cSrcweir         setPropertyToDefault(PROPERTY_FORMATKEY);
1017*cdf0e10cSrcweir     }
1018*cdf0e10cSrcweir }
1019*cdf0e10cSrcweir 
1020*cdf0e10cSrcweir //------------------------------------------------------------------------------
1021*cdf0e10cSrcweir sal_uInt16 OFormattedModel::getPersistenceFlags() const
1022*cdf0e10cSrcweir {
1023*cdf0e10cSrcweir     return (OEditBaseModel::getPersistenceFlags() & ~PF_HANDLE_COMMON_PROPS);
1024*cdf0e10cSrcweir     // a) we do our own call to writeCommonEditProperties
1025*cdf0e10cSrcweir }
1026*cdf0e10cSrcweir 
1027*cdf0e10cSrcweir //------------------------------------------------------------------------------
1028*cdf0e10cSrcweir sal_Bool OFormattedModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
1029*cdf0e10cSrcweir {
1030*cdf0e10cSrcweir     Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
1031*cdf0e10cSrcweir     if ( aControlValue != m_aSaveValue )
1032*cdf0e10cSrcweir     {
1033*cdf0e10cSrcweir         // Leerstring + EmptyIsNull = void
1034*cdf0e10cSrcweir         if  (   !aControlValue.hasValue()
1035*cdf0e10cSrcweir             ||  (   ( aControlValue.getValueType().getTypeClass() == TypeClass_STRING )
1036*cdf0e10cSrcweir                 &&  ( getString( aControlValue ).getLength() == 0 )
1037*cdf0e10cSrcweir                 &&  m_bEmptyIsNull
1038*cdf0e10cSrcweir                 )
1039*cdf0e10cSrcweir             )
1040*cdf0e10cSrcweir             m_xColumnUpdate->updateNull();
1041*cdf0e10cSrcweir         else
1042*cdf0e10cSrcweir         {
1043*cdf0e10cSrcweir             try
1044*cdf0e10cSrcweir             {
1045*cdf0e10cSrcweir                 double f = 0.0;
1046*cdf0e10cSrcweir                 if ( aControlValue.getValueType().getTypeClass() == TypeClass_DOUBLE || (aControlValue >>= f)) // #i110323
1047*cdf0e10cSrcweir                 {
1048*cdf0e10cSrcweir                     DBTypeConversion::setValue( m_xColumnUpdate, m_aNullDate, getDouble( aControlValue ), m_nKeyType );
1049*cdf0e10cSrcweir                 }
1050*cdf0e10cSrcweir                 else
1051*cdf0e10cSrcweir                 {
1052*cdf0e10cSrcweir                     DBG_ASSERT( aControlValue.getValueType().getTypeClass() == TypeClass_STRING, "OFormattedModel::commitControlValueToDbColumn: invalud value type !" );
1053*cdf0e10cSrcweir                     m_xColumnUpdate->updateString( getString( aControlValue ) );
1054*cdf0e10cSrcweir                 }
1055*cdf0e10cSrcweir             }
1056*cdf0e10cSrcweir             catch(Exception&)
1057*cdf0e10cSrcweir             {
1058*cdf0e10cSrcweir                 return sal_False;
1059*cdf0e10cSrcweir             }
1060*cdf0e10cSrcweir         }
1061*cdf0e10cSrcweir         m_aSaveValue = aControlValue;
1062*cdf0e10cSrcweir     }
1063*cdf0e10cSrcweir     return sal_True;
1064*cdf0e10cSrcweir }
1065*cdf0e10cSrcweir 
1066*cdf0e10cSrcweir //------------------------------------------------------------------------------
1067*cdf0e10cSrcweir void OFormattedModel::onConnectedExternalValue( )
1068*cdf0e10cSrcweir {
1069*cdf0e10cSrcweir     OEditBaseModel::onConnectedExternalValue();
1070*cdf0e10cSrcweir     updateFormatterNullDate();
1071*cdf0e10cSrcweir }
1072*cdf0e10cSrcweir 
1073*cdf0e10cSrcweir //------------------------------------------------------------------------------
1074*cdf0e10cSrcweir Any OFormattedModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
1075*cdf0e10cSrcweir {
1076*cdf0e10cSrcweir     Any aControlValue;
1077*cdf0e10cSrcweir     switch( _rExternalValue.getValueTypeClass() )
1078*cdf0e10cSrcweir     {
1079*cdf0e10cSrcweir     case TypeClass_VOID:
1080*cdf0e10cSrcweir         break;
1081*cdf0e10cSrcweir 
1082*cdf0e10cSrcweir     case TypeClass_STRING:
1083*cdf0e10cSrcweir         aControlValue = _rExternalValue;
1084*cdf0e10cSrcweir         break;
1085*cdf0e10cSrcweir 
1086*cdf0e10cSrcweir     case TypeClass_BOOLEAN:
1087*cdf0e10cSrcweir     {
1088*cdf0e10cSrcweir         sal_Bool bExternalValue = sal_False;
1089*cdf0e10cSrcweir         _rExternalValue >>= bExternalValue;
1090*cdf0e10cSrcweir         aControlValue <<= (double)( bExternalValue ? 1 : 0 );
1091*cdf0e10cSrcweir     }
1092*cdf0e10cSrcweir     break;
1093*cdf0e10cSrcweir 
1094*cdf0e10cSrcweir     default:
1095*cdf0e10cSrcweir     {
1096*cdf0e10cSrcweir         if ( _rExternalValue.getValueType().equals( ::getCppuType( static_cast< UNODate* >( NULL ) ) ) )
1097*cdf0e10cSrcweir         {
1098*cdf0e10cSrcweir             UNODate aDate;
1099*cdf0e10cSrcweir             _rExternalValue >>= aDate;
1100*cdf0e10cSrcweir             aControlValue <<= DBTypeConversion::toDouble( aDate, m_aNullDate );
1101*cdf0e10cSrcweir         }
1102*cdf0e10cSrcweir         else if ( _rExternalValue.getValueType().equals( ::getCppuType( static_cast< UNOTime* >( NULL ) ) ) )
1103*cdf0e10cSrcweir         {
1104*cdf0e10cSrcweir             UNOTime aTime;
1105*cdf0e10cSrcweir             _rExternalValue >>= aTime;
1106*cdf0e10cSrcweir             aControlValue <<= DBTypeConversion::toDouble( aTime );
1107*cdf0e10cSrcweir         }
1108*cdf0e10cSrcweir         else if ( _rExternalValue.getValueType().equals( ::getCppuType( static_cast< UNODateTime* >( NULL ) ) ) )
1109*cdf0e10cSrcweir         {
1110*cdf0e10cSrcweir             UNODateTime aDateTime;
1111*cdf0e10cSrcweir             _rExternalValue >>= aDateTime;
1112*cdf0e10cSrcweir             aControlValue <<= DBTypeConversion::toDouble( aDateTime, m_aNullDate );
1113*cdf0e10cSrcweir         }
1114*cdf0e10cSrcweir         else
1115*cdf0e10cSrcweir         {
1116*cdf0e10cSrcweir             OSL_ENSURE( _rExternalValue.getValueTypeClass() == TypeClass_DOUBLE,
1117*cdf0e10cSrcweir                 "OFormattedModel::translateExternalValueToControlValue: don't know how to translate this type!" );
1118*cdf0e10cSrcweir             double fValue = 0;
1119*cdf0e10cSrcweir             OSL_VERIFY( _rExternalValue >>= fValue );
1120*cdf0e10cSrcweir             aControlValue <<= fValue;
1121*cdf0e10cSrcweir         }
1122*cdf0e10cSrcweir     }
1123*cdf0e10cSrcweir     }
1124*cdf0e10cSrcweir 
1125*cdf0e10cSrcweir     return aControlValue;
1126*cdf0e10cSrcweir }
1127*cdf0e10cSrcweir 
1128*cdf0e10cSrcweir //------------------------------------------------------------------------------
1129*cdf0e10cSrcweir Any OFormattedModel::translateControlValueToExternalValue( ) const
1130*cdf0e10cSrcweir {
1131*cdf0e10cSrcweir     OSL_PRECOND( hasExternalValueBinding(),
1132*cdf0e10cSrcweir         "OFormattedModel::translateControlValueToExternalValue: precondition not met!" );
1133*cdf0e10cSrcweir 
1134*cdf0e10cSrcweir     Any aControlValue( getControlValue() );
1135*cdf0e10cSrcweir     if ( !aControlValue.hasValue() )
1136*cdf0e10cSrcweir         return aControlValue;
1137*cdf0e10cSrcweir 
1138*cdf0e10cSrcweir     Any aExternalValue;
1139*cdf0e10cSrcweir 
1140*cdf0e10cSrcweir     // translate into the the external value type
1141*cdf0e10cSrcweir     Type aExternalValueType( getExternalValueType() );
1142*cdf0e10cSrcweir     switch ( aExternalValueType.getTypeClass() )
1143*cdf0e10cSrcweir     {
1144*cdf0e10cSrcweir     case TypeClass_STRING:
1145*cdf0e10cSrcweir     {
1146*cdf0e10cSrcweir         ::rtl::OUString sString;
1147*cdf0e10cSrcweir         if ( aControlValue >>= sString )
1148*cdf0e10cSrcweir         {
1149*cdf0e10cSrcweir             aExternalValue <<= sString;
1150*cdf0e10cSrcweir             break;
1151*cdf0e10cSrcweir         }
1152*cdf0e10cSrcweir     }
1153*cdf0e10cSrcweir     // NO break here!
1154*cdf0e10cSrcweir 
1155*cdf0e10cSrcweir     case TypeClass_BOOLEAN:
1156*cdf0e10cSrcweir     {
1157*cdf0e10cSrcweir         double fValue = 0;
1158*cdf0e10cSrcweir         OSL_VERIFY( aControlValue >>= fValue );
1159*cdf0e10cSrcweir             // if this asserts ... well, the somebody set the TreatAsNumeric property to false,
1160*cdf0e10cSrcweir             // and the control value is a string. This implies some weird misconfiguration
1161*cdf0e10cSrcweir             // of the FormattedModel, so we won't care for it for the moment.
1162*cdf0e10cSrcweir         aExternalValue <<= (sal_Bool)( fValue ? sal_True : sal_False );
1163*cdf0e10cSrcweir     }
1164*cdf0e10cSrcweir     break;
1165*cdf0e10cSrcweir 
1166*cdf0e10cSrcweir     default:
1167*cdf0e10cSrcweir     {
1168*cdf0e10cSrcweir         double fValue = 0;
1169*cdf0e10cSrcweir         OSL_VERIFY( aControlValue >>= fValue );
1170*cdf0e10cSrcweir             // if this asserts ... well, the somebody set the TreatAsNumeric property to false,
1171*cdf0e10cSrcweir             // and the control value is a string. This implies some weird misconfiguration
1172*cdf0e10cSrcweir             // of the FormattedModel, so we won't care for it for the moment.
1173*cdf0e10cSrcweir 
1174*cdf0e10cSrcweir         if ( aExternalValueType.equals( ::getCppuType( static_cast< UNODate* >( NULL ) ) ) )
1175*cdf0e10cSrcweir         {
1176*cdf0e10cSrcweir             aExternalValue <<= DBTypeConversion::toDate( fValue, m_aNullDate );
1177*cdf0e10cSrcweir         }
1178*cdf0e10cSrcweir         else if ( aExternalValueType.equals( ::getCppuType( static_cast< UNOTime* >( NULL ) ) ) )
1179*cdf0e10cSrcweir         {
1180*cdf0e10cSrcweir             aExternalValue <<= DBTypeConversion::toTime( fValue );
1181*cdf0e10cSrcweir         }
1182*cdf0e10cSrcweir         else if ( aExternalValueType.equals( ::getCppuType( static_cast< UNODateTime* >( NULL ) ) ) )
1183*cdf0e10cSrcweir         {
1184*cdf0e10cSrcweir             aExternalValue <<= DBTypeConversion::toDateTime( fValue, m_aNullDate );
1185*cdf0e10cSrcweir         }
1186*cdf0e10cSrcweir         else
1187*cdf0e10cSrcweir         {
1188*cdf0e10cSrcweir             OSL_ENSURE( aExternalValueType.equals( ::getCppuType( static_cast< double* >( NULL ) ) ),
1189*cdf0e10cSrcweir                 "OFormattedModel::translateControlValueToExternalValue: don't know how to translate this type!" );
1190*cdf0e10cSrcweir             aExternalValue <<= fValue;
1191*cdf0e10cSrcweir         }
1192*cdf0e10cSrcweir     }
1193*cdf0e10cSrcweir     break;
1194*cdf0e10cSrcweir     }
1195*cdf0e10cSrcweir     return aExternalValue;
1196*cdf0e10cSrcweir }
1197*cdf0e10cSrcweir 
1198*cdf0e10cSrcweir //------------------------------------------------------------------------------
1199*cdf0e10cSrcweir Any OFormattedModel::translateDbColumnToControlValue()
1200*cdf0e10cSrcweir {
1201*cdf0e10cSrcweir     if ( m_bNumeric )
1202*cdf0e10cSrcweir         m_aSaveValue <<= DBTypeConversion::getValue( m_xColumn, m_aNullDate ); // #100056# OJ
1203*cdf0e10cSrcweir     else
1204*cdf0e10cSrcweir         m_aSaveValue <<= m_xColumn->getString();
1205*cdf0e10cSrcweir 
1206*cdf0e10cSrcweir     if ( m_xColumn->wasNull() )
1207*cdf0e10cSrcweir         m_aSaveValue.clear();
1208*cdf0e10cSrcweir 
1209*cdf0e10cSrcweir     return m_aSaveValue;
1210*cdf0e10cSrcweir }
1211*cdf0e10cSrcweir 
1212*cdf0e10cSrcweir // -----------------------------------------------------------------------------
1213*cdf0e10cSrcweir Sequence< Type > OFormattedModel::getSupportedBindingTypes()
1214*cdf0e10cSrcweir {
1215*cdf0e10cSrcweir     ::std::list< Type > aTypes;
1216*cdf0e10cSrcweir     aTypes.push_back( ::getCppuType( static_cast< double* >( NULL ) ) );
1217*cdf0e10cSrcweir 
1218*cdf0e10cSrcweir     switch ( m_nKeyType & ~NumberFormat::DEFINED )
1219*cdf0e10cSrcweir     {
1220*cdf0e10cSrcweir     case NumberFormat::DATE:
1221*cdf0e10cSrcweir         aTypes.push_front(::getCppuType( static_cast< UNODate* >( NULL ) ) );
1222*cdf0e10cSrcweir         break;
1223*cdf0e10cSrcweir     case NumberFormat::TIME:
1224*cdf0e10cSrcweir         aTypes.push_front(::getCppuType( static_cast< UNOTime* >( NULL ) ) );
1225*cdf0e10cSrcweir         break;
1226*cdf0e10cSrcweir     case NumberFormat::DATETIME:
1227*cdf0e10cSrcweir         aTypes.push_front(::getCppuType( static_cast< UNODateTime* >( NULL ) ) );
1228*cdf0e10cSrcweir         break;
1229*cdf0e10cSrcweir     case NumberFormat::TEXT:
1230*cdf0e10cSrcweir         aTypes.push_front(::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ) );
1231*cdf0e10cSrcweir         break;
1232*cdf0e10cSrcweir     case NumberFormat::LOGICAL:
1233*cdf0e10cSrcweir         aTypes.push_front(::getCppuType( static_cast< sal_Bool* >( NULL ) ) );
1234*cdf0e10cSrcweir         break;
1235*cdf0e10cSrcweir     }
1236*cdf0e10cSrcweir 
1237*cdf0e10cSrcweir     Sequence< Type > aTypesRet( aTypes.size() );
1238*cdf0e10cSrcweir     ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
1239*cdf0e10cSrcweir     return aTypesRet;
1240*cdf0e10cSrcweir }
1241*cdf0e10cSrcweir 
1242*cdf0e10cSrcweir //------------------------------------------------------------------------------
1243*cdf0e10cSrcweir Any OFormattedModel::getDefaultForReset() const
1244*cdf0e10cSrcweir {
1245*cdf0e10cSrcweir     return m_xAggregateSet->getPropertyValue( PROPERTY_EFFECTIVE_DEFAULT );
1246*cdf0e10cSrcweir }
1247*cdf0e10cSrcweir 
1248*cdf0e10cSrcweir //------------------------------------------------------------------------------
1249*cdf0e10cSrcweir void OFormattedModel::resetNoBroadcast()
1250*cdf0e10cSrcweir {
1251*cdf0e10cSrcweir     OEditBaseModel::resetNoBroadcast();
1252*cdf0e10cSrcweir     m_aSaveValue.clear();
1253*cdf0e10cSrcweir }
1254*cdf0e10cSrcweir 
1255*cdf0e10cSrcweir //.........................................................................
1256*cdf0e10cSrcweir }
1257*cdf0e10cSrcweir //.........................................................................
1258