update dev300-m58
[ooovba.git] / forms / source / component / Edit.cxx
blob4b483f3ef589cf2972fe541fd84ef5d297f2e54d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Edit.cxx,v $
10 * $Revision: 1.41 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
34 #include "Edit.hxx"
36 #include <com/sun/star/uno/Type.hxx>
37 #include <com/sun/star/awt/XWindow.hpp>
38 #include <com/sun/star/container/XIndexAccess.hpp>
39 #include <com/sun/star/form/XSubmit.hpp>
40 #include <com/sun/star/util/NumberFormat.hpp>
41 #include <com/sun/star/sdbc/DataType.hpp>
42 #include <com/sun/star/awt/XVclWindowPeer.hpp>
44 #include <vcl/svapp.hxx>
45 #include <vcl/wintypes.hxx>
47 #include <connectivity/dbtools.hxx>
48 #include <connectivity/formattedcolumnvalue.hxx>
49 #include <connectivity/dbconversion.hxx>
51 #include <tools/diagnose_ex.h>
52 #include <tools/debug.hxx>
54 #include <comphelper/container.hxx>
55 #include <comphelper/numbers.hxx>
57 using namespace dbtools;
59 //.........................................................................
60 namespace frm
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::sdb;
64 using namespace ::com::sun::star::sdbc;
65 using namespace ::com::sun::star::sdbcx;
66 using namespace ::com::sun::star::beans;
67 using namespace ::com::sun::star::container;
68 using namespace ::com::sun::star::form;
69 using namespace ::com::sun::star::awt;
70 using namespace ::com::sun::star::io;
71 using namespace ::com::sun::star::lang;
72 using namespace ::com::sun::star::util;
73 using namespace ::com::sun::star::form::binding;
75 //------------------------------------------------------------------
76 InterfaceRef SAL_CALL OEditControl_CreateInstance(const Reference< XMultiServiceFactory > & _rxFactory)
78 return *(new OEditControl(_rxFactory));
81 //------------------------------------------------------------------------------
82 Sequence<Type> OEditControl::_getTypes()
84 static Sequence<Type> aTypes;
85 if (!aTypes.getLength())
87 // my two base classes
88 aTypes = concatSequences(OBoundControl::_getTypes(), OEditControl_BASE::getTypes());
90 return aTypes;
93 //------------------------------------------------------------------------------
94 Any SAL_CALL OEditControl::queryAggregation(const Type& _rType) throw (RuntimeException)
96 Any aReturn = OBoundControl::queryAggregation(_rType);
97 if (!aReturn.hasValue())
98 aReturn = OEditControl_BASE::queryInterface(_rType);
100 return aReturn;
103 DBG_NAME(OEditControl);
104 //------------------------------------------------------------------------------
105 OEditControl::OEditControl(const Reference<XMultiServiceFactory>& _rxFactory)
106 :OBoundControl( _rxFactory, FRM_SUN_CONTROL_RICHTEXTCONTROL )
107 ,m_aChangeListeners(m_aMutex)
108 ,m_nKeyEvent( 0 )
110 DBG_CTOR(OEditControl,NULL);
112 increment(m_refCount);
114 Reference<XWindow> xComp;
115 if (query_aggregation(m_xAggregate, xComp))
117 xComp->addFocusListener(this);
118 xComp->addKeyListener(this);
121 decrement(m_refCount);
124 //------------------------------------------------------------------------------
125 OEditControl::~OEditControl()
127 if( m_nKeyEvent )
128 Application::RemoveUserEvent( m_nKeyEvent );
130 if (!OComponentHelper::rBHelper.bDisposed)
132 acquire();
133 dispose();
136 DBG_DTOR(OEditControl,NULL);
139 // XChangeBroadcaster
140 //------------------------------------------------------------------------------
141 void OEditControl::addChangeListener(const Reference<XChangeListener>& l) throw ( ::com::sun::star::uno::RuntimeException)
143 m_aChangeListeners.addInterface( l );
146 //------------------------------------------------------------------------------
147 void OEditControl::removeChangeListener(const Reference<XChangeListener>& l) throw ( ::com::sun::star::uno::RuntimeException)
149 m_aChangeListeners.removeInterface( l );
152 // OComponentHelper
153 //------------------------------------------------------------------------------
154 void OEditControl::disposing()
156 OBoundControl::disposing();
158 EventObject aEvt(static_cast<XWeak*>(this));
159 m_aChangeListeners.disposeAndClear(aEvt);
162 // XServiceInfo
163 //------------------------------------------------------------------------------
164 StringSequence OEditControl::getSupportedServiceNames() throw()
166 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
167 aSupported.realloc(aSupported.getLength() + 1);
169 ::rtl::OUString*pArray = aSupported.getArray();
170 pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_TEXTFIELD;
171 return aSupported;
174 // XEventListener
175 //------------------------------------------------------------------------------
176 void OEditControl::disposing(const EventObject& Source) throw( RuntimeException )
178 OBoundControl::disposing(Source);
181 // XFocusListener
182 //------------------------------------------------------------------------------
183 void OEditControl::focusGained( const FocusEvent& /*e*/ ) throw ( ::com::sun::star::uno::RuntimeException)
185 Reference<XPropertySet> xSet(getModel(), UNO_QUERY);
186 if (xSet.is())
187 xSet->getPropertyValue( PROPERTY_TEXT ) >>= m_aHtmlChangeValue;
190 //------------------------------------------------------------------------------
191 void OEditControl::focusLost( const FocusEvent& /*e*/ ) throw ( ::com::sun::star::uno::RuntimeException)
193 Reference<XPropertySet> xSet(getModel(), UNO_QUERY);
194 if (xSet.is())
196 ::rtl::OUString sNewHtmlChangeValue;
197 xSet->getPropertyValue( PROPERTY_TEXT ) >>= sNewHtmlChangeValue;
198 if( sNewHtmlChangeValue != m_aHtmlChangeValue )
200 EventObject aEvt( *this );
201 m_aChangeListeners.notifyEach( &XChangeListener::changed, aEvt );
206 // XKeyListener
207 //------------------------------------------------------------------------------
208 void OEditControl::keyPressed(const ::com::sun::star::awt::KeyEvent& e) throw ( ::com::sun::star::uno::RuntimeException)
210 if( e.KeyCode != KEY_RETURN || e.Modifiers != 0 )
211 return;
213 // Steht das Control in einem Formular mit einer Submit-URL?
214 Reference<XPropertySet> xSet(getModel(), UNO_QUERY);
215 if( !xSet.is() )
216 return;
218 // nicht fuer multiline edits
219 Any aTmp( xSet->getPropertyValue(PROPERTY_MULTILINE));
220 if ((aTmp.getValueType().equals(::getBooleanCppuType())) && getBOOL(aTmp))
221 return;
223 Reference<XFormComponent> xFComp(xSet, UNO_QUERY);
224 InterfaceRef xParent = xFComp->getParent();
225 if( !xParent.is() )
226 return;
228 Reference<XPropertySet> xFormSet(xParent, UNO_QUERY);
229 if( !xFormSet.is() )
230 return;
232 aTmp = xFormSet->getPropertyValue( PROPERTY_TARGET_URL );
233 if (!aTmp.getValueType().equals(::getCppuType((const ::rtl::OUString*)NULL)) ||
234 !getString(aTmp).getLength() )
235 return;
237 Reference<XIndexAccess> xElements(xParent, UNO_QUERY);
238 sal_Int32 nCount = xElements->getCount();
239 if( nCount > 1 )
241 Reference<XPropertySet> xFCSet;
242 for( sal_Int32 nIndex=0; nIndex < nCount; nIndex++ )
244 // Any aElement(xElements->getByIndex(nIndex));
245 xElements->getByIndex(nIndex) >>= xFCSet;
246 OSL_ENSURE(xFCSet.is(),"OEditControl::keyPressed: No XPropertySet!");
248 if (hasProperty(PROPERTY_CLASSID, xFCSet) &&
249 getINT16(xFCSet->getPropertyValue(PROPERTY_CLASSID)) == FormComponentType::TEXTFIELD)
251 // Noch ein weiteres Edit gefunden ==> dann nicht submitten
252 if (xFCSet != xSet)
253 return;
258 // Da wir noch im Haender stehen, submit asynchron ausloesen
259 if( m_nKeyEvent )
260 Application::RemoveUserEvent( m_nKeyEvent );
261 m_nKeyEvent = Application::PostUserEvent( LINK(this, OEditControl,OnKeyPressed) );
264 //------------------------------------------------------------------------------
265 void OEditControl::keyReleased(const ::com::sun::star::awt::KeyEvent& /*e*/) throw ( ::com::sun::star::uno::RuntimeException)
269 //------------------------------------------------------------------------------
270 IMPL_LINK(OEditControl, OnKeyPressed, void*, /*EMPTYARG*/)
272 m_nKeyEvent = 0;
274 Reference<XFormComponent> xFComp(getModel(), UNO_QUERY);
275 InterfaceRef xParent = xFComp->getParent();
276 Reference<XSubmit> xSubmit(xParent, UNO_QUERY);
277 if (xSubmit.is())
278 xSubmit->submit( Reference<XControl>(), ::com::sun::star::awt::MouseEvent() );
279 return 0L;
282 //------------------------------------------------------------------
283 void SAL_CALL OEditControl::createPeer( const Reference< XToolkit>& _rxToolkit, const Reference< XWindowPeer>& _rxParent ) throw ( RuntimeException )
285 OBoundControl::createPeer(_rxToolkit, _rxParent);
288 /*************************************************************************/
289 //------------------------------------------------------------------
290 InterfaceRef SAL_CALL OEditModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
292 return *(new OEditModel(_rxFactory));
295 //------------------------------------------------------------------------------
296 Sequence<Type> OEditModel::_getTypes()
298 return OEditBaseModel::_getTypes();
302 DBG_NAME(OEditModel);
303 //------------------------------------------------------------------
304 OEditModel::OEditModel(const Reference<XMultiServiceFactory>& _rxFactory)
305 :OEditBaseModel( _rxFactory, FRM_SUN_COMPONENT_RICHTEXTCONTROL, FRM_SUN_CONTROL_TEXTFIELD, sal_True, sal_True )
306 ,m_bMaxTextLenModified(sal_False)
307 ,m_bWritingFormattedFake(sal_False)
309 DBG_CTOR(OEditModel,NULL);
311 m_nClassId = FormComponentType::TEXTFIELD;
312 initValueProperty( PROPERTY_TEXT, PROPERTY_ID_TEXT );
315 //------------------------------------------------------------------
316 OEditModel::OEditModel( const OEditModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
317 :OEditBaseModel( _pOriginal, _rxFactory )
318 ,m_bMaxTextLenModified(sal_False)
319 ,m_bWritingFormattedFake(sal_False)
321 DBG_CTOR( OEditModel, NULL );
323 // Note that most of the properties are not clone from the original object:
324 // Things as the format key, it's type, and such, depend on the field being part of a loaded form
325 // (they're initialized in onConnectedDbColumn). Even if the original object _is_ part of such a form, we ourself
326 // certainly aren't, so these members are defaulted. If we're inserted into a form which is already loaded,
327 // they will be set to new values, anyway ....
330 //------------------------------------------------------------------
331 OEditModel::~OEditModel()
333 if (!OComponentHelper::rBHelper.bDisposed)
335 acquire();
336 dispose();
339 DBG_DTOR(OEditModel,NULL);
342 //------------------------------------------------------------------------------
343 IMPLEMENT_DEFAULT_CLONING( OEditModel )
345 //------------------------------------------------------------------------------
346 void OEditModel::disposing()
348 OEditBaseModel::disposing();
349 m_pValueFormatter.reset();
352 // XPersistObject
353 //------------------------------------------------------------------------------
354 ::rtl::OUString SAL_CALL OEditModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
356 return FRM_COMPONENT_EDIT; // old (non-sun) name for compatibility !
359 // XServiceInfo
360 //------------------------------------------------------------------------------
361 StringSequence SAL_CALL OEditModel::getSupportedServiceNames() throw()
363 StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
365 sal_Int32 nOldLen = aSupported.getLength();
366 aSupported.realloc( nOldLen + 8 );
367 ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
369 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
370 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
371 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
373 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
374 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
376 *pStoreTo++ = FRM_SUN_COMPONENT_TEXTFIELD;
377 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_TEXTFIELD;
378 *pStoreTo++ = BINDABLE_DATABASE_TEXT_FIELD;
380 return aSupported;
383 // XPropertySet
384 void SAL_CALL OEditModel::getFastPropertyValue(Any& rValue, sal_Int32 nHandle ) const
386 if ( PROPERTY_ID_PERSISTENCE_MAXTEXTLENGTH == nHandle )
388 if ( m_bMaxTextLenModified )
389 rValue <<= sal_Int16(0);
390 else if ( m_xAggregateSet.is() )
391 rValue = m_xAggregateSet->getPropertyValue(PROPERTY_MAXTEXTLEN);
393 else
395 OEditBaseModel::getFastPropertyValue(rValue, nHandle );
399 //------------------------------------------------------------------------------
400 void OEditModel::describeFixedProperties( Sequence< Property >& _rProps ) const
402 BEGIN_DESCRIBE_PROPERTIES( 5, OEditBaseModel )
403 DECL_PROP2(PERSISTENCE_MAXTEXTLENGTH,sal_Int16, READONLY, TRANSIENT);
404 DECL_PROP2(DEFAULT_TEXT, ::rtl::OUString, BOUND, MAYBEDEFAULT);
405 DECL_BOOL_PROP1(EMPTY_IS_NULL, BOUND);
406 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
407 DECL_BOOL_PROP2(FILTERPROPOSAL, BOUND, MAYBEDEFAULT);
408 END_DESCRIBE_PROPERTIES();
411 //------------------------------------------------------------------------------
412 void OEditModel::describeAggregateProperties( Sequence< Property >& _rAggregateProps ) const
414 OEditBaseModel::describeAggregateProperties( _rAggregateProps );
416 // our aggregate is a rich text model, which also derives from OControlModel, as
417 // do we, so we need to remove some duplicate properties
418 RemoveProperty( _rAggregateProps, PROPERTY_TABINDEX );
419 RemoveProperty( _rAggregateProps, PROPERTY_CLASSID );
420 RemoveProperty( _rAggregateProps, PROPERTY_NAME );
421 RemoveProperty( _rAggregateProps, PROPERTY_TAG );
422 RemoveProperty( _rAggregateProps, PROPERTY_NATIVE_LOOK );
426 //------------------------------------------------------------------------------
427 bool OEditModel::implActsAsRichText( ) const
429 sal_Bool bActAsRichText = sal_False;
430 if ( m_xAggregateSet.is() )
432 OSL_VERIFY( m_xAggregateSet->getPropertyValue( PROPERTY_RICH_TEXT ) >>= bActAsRichText );
434 return bActAsRichText;
437 //------------------------------------------------------------------------------
438 void SAL_CALL OEditModel::reset( ) throw(RuntimeException)
440 // no reset if we currently act as rich text control
441 if ( implActsAsRichText() )
442 return;
444 OEditBaseModel::reset();
447 //------------------------------------------------------------------------------
448 namespace
450 void lcl_transferProperties( const Reference< XPropertySet >& _rxSource, const Reference< XPropertySet >& _rxDest )
454 Reference< XPropertySetInfo > xSourceInfo;
455 if ( _rxSource.is() )
456 xSourceInfo = _rxSource->getPropertySetInfo();
458 Reference< XPropertySetInfo > xDestInfo;
459 if ( _rxDest.is() )
460 xDestInfo = _rxDest->getPropertySetInfo();
462 if ( !xSourceInfo.is() || !xDestInfo.is() )
464 OSL_ENSURE( sal_False, "lcl_transferProperties: invalid property set(s)!" );
465 return;
468 Sequence< Property > aSourceProps( xSourceInfo->getProperties() );
469 const Property* pSourceProps = aSourceProps.getConstArray();
470 const Property* pSourcePropsEnd = aSourceProps.getConstArray() + aSourceProps.getLength();
471 while ( pSourceProps != pSourcePropsEnd )
473 if ( !xDestInfo->hasPropertyByName( pSourceProps->Name ) )
475 ++pSourceProps;
476 continue;
479 Property aDestProp( xDestInfo->getPropertyByName( pSourceProps->Name ) );
480 if ( 0 != ( aDestProp.Attributes & PropertyAttribute::READONLY ) )
482 ++pSourceProps;
483 continue;
488 _rxDest->setPropertyValue( pSourceProps->Name, _rxSource->getPropertyValue( pSourceProps->Name ) );
490 catch( IllegalArgumentException e )
492 #if OSL_DEBUG_LEVEL > 0
493 ::rtl::OString sMessage( "could not transfer the property named '" );
494 sMessage += ::rtl::OString( pSourceProps->Name.getStr(), pSourceProps->Name.getLength(), RTL_TEXTENCODING_ASCII_US );
495 sMessage += ::rtl::OString( "'." );
496 if ( e.Message.getLength() )
498 sMessage += ::rtl::OString( "\n\nMessage:\n" );
499 sMessage += ::rtl::OString( e.Message.getStr(), e.Message.getLength(), RTL_TEXTENCODING_ASCII_US );
501 OSL_ENSURE( sal_False, sMessage.getStr() );
502 #endif
505 ++pSourceProps;
508 catch( const Exception& )
510 DBG_UNHANDLED_EXCEPTION();
515 //------------------------------------------------------------------------------
516 void OEditModel::writeAggregate( const Reference< XObjectOutputStream >& _rxOutStream ) const
518 // we need to fake the writing of our aggregate. Since #i24387#, we have another aggregate,
519 // but for compatibility, we need to use an "old" aggregate for writing and reading
521 Reference< XPropertySet > xFakedAggregate(
522 getContext().createComponent( (rtl::OUString)VCL_CONTROLMODEL_EDIT ),
523 UNO_QUERY
525 OSL_ENSURE( xFakedAggregate.is(), "OEditModel::writeAggregate: could not create an old EditControlModel!" );
526 if ( !xFakedAggregate.is() )
527 return;
529 lcl_transferProperties( m_xAggregateSet, xFakedAggregate );
531 Reference< XPersistObject > xFakedPersist( xFakedAggregate, UNO_QUERY );
532 OSL_ENSURE( xFakedPersist.is(), "OEditModel::writeAggregate: no XPersistObject!" );
533 if ( xFakedPersist.is() )
534 xFakedPersist->write( _rxOutStream );
537 //------------------------------------------------------------------------------
538 void OEditModel::readAggregate( const Reference< XObjectInputStream >& _rxInStream )
540 // we need to fake the reading of our aggregate. Since #i24387#, we have another aggregate,
541 // but for compatibility, we need to use an "old" aggregate for writing and reading
543 Reference< XPropertySet > xFakedAggregate(
544 getContext().createComponent( (rtl::OUString)VCL_CONTROLMODEL_EDIT ),
545 UNO_QUERY
547 Reference< XPersistObject > xFakedPersist( xFakedAggregate, UNO_QUERY );
548 OSL_ENSURE( xFakedPersist.is(), "OEditModel::readAggregate: no XPersistObject, or no faked aggregate at all!" );
549 if ( xFakedPersist.is() )
551 xFakedPersist->read( _rxInStream );
552 lcl_transferProperties( xFakedAggregate, m_xAggregateSet );
556 //------------------------------------------------------------------------------
557 void OEditModel::write(const Reference<XObjectOutputStream>& _rxOutStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
559 Any aCurrentText;
560 sal_Int16 nOldTextLen = 0;
561 // bin ich gerade loaded und habe dazu zeitweilig die MaxTextLen umgesetzt ?
562 if ( m_bMaxTextLenModified )
563 { // -> fuer die Dauer des Speicherns meinem aggregierten Model die alte TextLen einreden
565 // before doing this we have to save the current text value of the aggregate, as this may be affected by resetting the text len
566 // FS - 08.12.99 - 70606
567 aCurrentText = m_xAggregateSet->getPropertyValue(PROPERTY_TEXT);
569 m_xAggregateSet->getPropertyValue(PROPERTY_MAXTEXTLEN) >>= nOldTextLen;
570 m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, makeAny((sal_Int16)0));
573 OEditBaseModel::write(_rxOutStream);
575 if ( m_bMaxTextLenModified )
576 { // wieder zuruecksetzen
577 m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, makeAny(nOldTextLen));
578 // and reset the text
579 // First we set it to an empty string : Without this the second setPropertyValue would not do anything as it thinks
580 // we aren't changing the prop (it didn't notify the - implicite - change of the text prop while setting the max text len)
581 // This seems to be a bug with in toolkit's EditControl-implementation.
582 m_xAggregateSet->setPropertyValue(PROPERTY_TEXT, makeAny(::rtl::OUString()));
583 m_xAggregateSet->setPropertyValue(PROPERTY_TEXT, aCurrentText);
587 //------------------------------------------------------------------------------
588 void OEditModel::read(const Reference<XObjectInputStream>& _rxInStream) throw ( ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
590 OEditBaseModel::read(_rxInStream);
592 // Some versions (5.1 'til about 552) wrote a wrong DefaultControl-property value which is unknown
593 // to older versions (5.0).
594 // correct this ...
595 if (m_xAggregateSet.is())
597 Any aDefaultControl = m_xAggregateSet->getPropertyValue(PROPERTY_DEFAULTCONTROL);
598 if ( (aDefaultControl.getValueType().getTypeClass() == TypeClass_STRING)
599 && (getString(aDefaultControl).compareTo(STARDIV_ONE_FORM_CONTROL_TEXTFIELD) == COMPARE_EQUAL)
602 m_xAggregateSet->setPropertyValue( PROPERTY_DEFAULTCONTROL, makeAny( (::rtl::OUString)STARDIV_ONE_FORM_CONTROL_EDIT ) );
603 // Older as well as current versions should understand this : the former knew only the STARDIV_ONE_FORM_CONTROL_EDIT,
604 // the latter are registered for both STARDIV_ONE_FORM_CONTROL_EDIT and STARDIV_ONE_FORM_CONTROL_TEXTFIELD.
609 //------------------------------------------------------------------------------
610 sal_uInt16 OEditModel::getPersistenceFlags() const
612 sal_uInt16 nFlags = OEditBaseModel::getPersistenceFlags();
614 if (m_bWritingFormattedFake)
615 nFlags |= PF_FAKE_FORMATTED_FIELD;
617 return nFlags;
620 //------------------------------------------------------------------------------
621 void OEditModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
623 Reference< XPropertySet > xField = getField();
624 if ( xField.is() )
626 m_pValueFormatter.reset( new ::dbtools::FormattedColumnValue( getContext(), Reference< XRowSet >( _rxForm, UNO_QUERY ), xField ) );
628 if ( m_pValueFormatter->getKeyType() != NumberFormat::SCIENTIFIC )
630 m_bMaxTextLenModified = getINT16(m_xAggregateSet->getPropertyValue(PROPERTY_MAXTEXTLEN)) != 0;
631 if ( !m_bMaxTextLenModified )
633 sal_Int32 nFieldLen = 0;
634 xField->getPropertyValue(::rtl::OUString::createFromAscii("Precision")) >>= nFieldLen;
636 if (nFieldLen && nFieldLen <= USHRT_MAX)
638 Any aVal;
639 aVal <<= (sal_Int16)nFieldLen;
640 m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, aVal);
642 m_bMaxTextLenModified = sal_True;
645 else
646 m_bMaxTextLenModified = sal_False; // to get sure that the text len won't be set in unloaded
651 //------------------------------------------------------------------------------
652 void OEditModel::onDisconnectedDbColumn()
654 OEditBaseModel::onDisconnectedDbColumn();
656 m_pValueFormatter.reset();
658 if ( hasField() && m_bMaxTextLenModified )
660 Any aVal;
661 aVal <<= (sal_Int16)0; // nur wenn es 0 war, habe ich es in onConnectedDbColumn umgesetzt
662 m_xAggregateSet->setPropertyValue(PROPERTY_MAXTEXTLEN, aVal);
663 m_bMaxTextLenModified = sal_False;
667 //------------------------------------------------------------------------------
668 sal_Bool OEditModel::approveDbColumnType( sal_Int32 _nColumnType )
670 // if we act as rich text curently, we do not allow binding to a database column
671 if ( implActsAsRichText() )
672 return sal_False;
674 return OEditBaseModel::approveDbColumnType( _nColumnType );
677 //------------------------------------------------------------------------------
678 sal_Bool OEditModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
680 Any aNewValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
682 if ( aNewValue != m_aLastKnownValue )
684 ::rtl::OUString sNewValue;
685 aNewValue >>= sNewValue;
687 if ( !aNewValue.hasValue()
688 || ( !sNewValue.getLength() // an empty string
689 && m_bEmptyIsNull // which should be interpreted as NULL
693 m_xColumnUpdate->updateNull();
695 else
697 OSL_PRECOND( m_pValueFormatter.get(), "OEditModel::commitControlValueToDbColumn: no value formatter!" );
700 if ( m_pValueFormatter.get() )
702 if ( !m_pValueFormatter->setFormattedValue( sNewValue ) )
703 return sal_False;
705 else
706 m_xColumnUpdate->updateString( sNewValue );
708 catch ( const Exception& )
710 return sal_False;
713 m_aLastKnownValue = aNewValue;
716 return sal_True;
719 //------------------------------------------------------------------------------
720 Any OEditModel::translateDbColumnToControlValue()
722 OSL_PRECOND( m_pValueFormatter.get(), "OEditModel::translateDbColumnToControlValue: no value formatter!" );
723 if ( m_pValueFormatter.get() )
725 ::rtl::OUString sValue( m_pValueFormatter->getFormattedValue() );
726 if ( !sValue.getLength()
727 && m_pValueFormatter->getColumn().is()
728 && m_pValueFormatter->getColumn()->wasNull()
731 m_aLastKnownValue.clear();
733 else
735 // #i2817# OJ
736 sal_uInt16 nMaxTextLen = getINT16( m_xAggregateSet->getPropertyValue( PROPERTY_MAXTEXTLEN ) );
737 if ( nMaxTextLen && sValue.getLength() > nMaxTextLen )
739 sal_Int32 nDiff = sValue.getLength() - nMaxTextLen;
740 sValue = sValue.replaceAt( nMaxTextLen, nDiff, ::rtl::OUString() );
743 m_aLastKnownValue <<= sValue;
746 else
747 m_aLastKnownValue.clear();
749 return m_aLastKnownValue.hasValue() ? m_aLastKnownValue : makeAny( ::rtl::OUString() );
750 // (m_aLastKnownValue is alllowed to be VOID, the control value isn't)
753 //------------------------------------------------------------------------------
754 Any OEditModel::getDefaultForReset() const
756 return makeAny( m_aDefaultText );
759 //.........................................................................
761 //.........................................................................