nss: upgrade to release 3.73
[LibreOffice.git] / forms / source / component / ComboBox.cxx
blob475ca63bec01c41520b5581b9b2f8fbb548feb02
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "ComboBox.hxx"
22 #include <property.hxx>
23 #include <services.hxx>
25 #include <frm_resource.hxx>
26 #include <strings.hrc>
27 #include "BaseListBox.hxx"
29 #include <com/sun/star/form/FormComponentType.hpp>
30 #include <com/sun/star/sdbc/XRowSet.hpp>
31 #include <com/sun/star/container/XIndexAccess.hpp>
32 #include <com/sun/star/sdbc/XConnection.hpp>
34 #include <comphelper/basicio.hxx>
35 #include <comphelper/property.hxx>
36 #include <connectivity/dbtools.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <tools/debug.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <unotools/sharedunocomponent.hxx>
42 #include <limits.h>
44 using namespace dbtools;
47 namespace frm
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::sdb;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::sdbcx;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::container;
55 using namespace ::com::sun::star::form;
56 using namespace ::com::sun::star::awt;
57 using namespace ::com::sun::star::io;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::util;
60 using namespace ::com::sun::star::form::binding;
63 Sequence<Type> OComboBoxModel::_getTypes()
65 return ::comphelper::concatSequences(
66 OBoundControlModel::_getTypes(),
67 OEntryListHelper::getTypes(),
68 OErrorBroadcaster::getTypes()
72 // XServiceInfo
74 css::uno::Sequence<OUString> SAL_CALL OComboBoxModel::getSupportedServiceNames()
76 css::uno::Sequence<OUString> aSupported = OBoundControlModel::getSupportedServiceNames();
78 sal_Int32 nOldLen = aSupported.getLength();
79 aSupported.realloc( nOldLen + 9 );
80 OUString* pStoreTo = aSupported.getArray() + nOldLen;
82 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
83 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
84 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
86 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
87 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
89 *pStoreTo++ = FRM_SUN_COMPONENT_COMBOBOX;
90 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_COMBOBOX;
91 *pStoreTo++ = BINDABLE_DATABASE_COMBO_BOX;
93 *pStoreTo++ = FRM_COMPONENT_COMBOBOX;
95 return aSupported;
99 Any SAL_CALL OComboBoxModel::queryAggregation(const Type& _rType)
101 Any aReturn = OBoundControlModel::queryAggregation( _rType );
102 if ( !aReturn.hasValue() )
103 aReturn = OEntryListHelper::queryInterface( _rType );
104 if ( !aReturn.hasValue() )
105 aReturn = OErrorBroadcaster::queryInterface( _rType );
106 return aReturn;
110 OComboBoxModel::OComboBoxModel(const Reference<XComponentContext>& _rxFactory)
111 :OBoundControlModel( _rxFactory, VCL_CONTROLMODEL_COMBOBOX, FRM_SUN_CONTROL_COMBOBOX, true, true, true )
112 // use the old control name for compatibility reasons
113 ,OEntryListHelper( static_cast<OControlModel&>(*this) )
114 ,OErrorBroadcaster( OComponentHelper::rBHelper )
115 ,m_aListRowSet()
116 ,m_eListSourceType(ListSourceType_TABLE)
117 ,m_bEmptyIsNull(true)
119 m_nClassId = FormComponentType::COMBOBOX;
120 initValueProperty( PROPERTY_TEXT, PROPERTY_ID_TEXT );
124 OComboBoxModel::OComboBoxModel( const OComboBoxModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
125 :OBoundControlModel( _pOriginal, _rxFactory )
126 ,OEntryListHelper( *_pOriginal, static_cast<OControlModel&>(*this) )
127 ,OErrorBroadcaster( OComponentHelper::rBHelper )
128 ,m_aListRowSet()
129 ,m_aListSource( _pOriginal->m_aListSource )
130 ,m_aDefaultText( _pOriginal->m_aDefaultText )
131 ,m_eListSourceType( _pOriginal->m_eListSourceType )
132 ,m_bEmptyIsNull( _pOriginal->m_bEmptyIsNull )
137 OComboBoxModel::~OComboBoxModel()
139 if (!OComponentHelper::rBHelper.bDisposed)
141 acquire();
142 dispose();
147 // XCloneable
149 IMPLEMENT_DEFAULT_CLONING( OComboBoxModel )
152 void OComboBoxModel::disposing()
154 OBoundControlModel::disposing();
155 OEntryListHelper::disposing();
156 OErrorBroadcaster::disposing();
160 void OComboBoxModel::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
162 switch (_nHandle)
164 case PROPERTY_ID_LISTSOURCETYPE:
165 _rValue <<= m_eListSourceType;
166 break;
168 case PROPERTY_ID_LISTSOURCE:
169 _rValue <<= m_aListSource;
170 break;
172 case PROPERTY_ID_EMPTY_IS_NULL:
173 _rValue <<= m_bEmptyIsNull;
174 break;
176 case PROPERTY_ID_DEFAULT_TEXT:
177 _rValue <<= m_aDefaultText;
178 break;
180 case PROPERTY_ID_STRINGITEMLIST:
181 _rValue <<= comphelper::containerToSequence(getStringItemList());
182 break;
184 case PROPERTY_ID_TYPEDITEMLIST:
185 _rValue <<= getTypedItemList();
186 break;
188 default:
189 OBoundControlModel::getFastPropertyValue(_rValue, _nHandle);
194 void OComboBoxModel::setFastPropertyValue_NoBroadcast(sal_Int32 _nHandle, const Any& _rValue)
196 switch (_nHandle)
198 case PROPERTY_ID_LISTSOURCETYPE :
199 DBG_ASSERT(_rValue.getValueType().equals(::cppu::UnoType<ListSourceType>::get()),
200 "OComboBoxModel::setFastPropertyValue_NoBroadcast : invalid type !" );
201 _rValue >>= m_eListSourceType;
202 break;
204 case PROPERTY_ID_LISTSOURCE :
205 DBG_ASSERT(_rValue.getValueType().getTypeClass() == TypeClass_STRING,
206 "OComboBoxModel::setFastPropertyValue_NoBroadcast : invalid type !" );
207 _rValue >>= m_aListSource;
208 // The ListSource has changed -> reload
209 if (ListSourceType_VALUELIST != m_eListSourceType)
211 if ( m_xCursor.is() && !hasField() && !hasExternalListSource() )
212 // combo box is already connected to a database, and no external list source
213 // data source changed -> refresh
214 loadData( false );
216 break;
218 case PROPERTY_ID_EMPTY_IS_NULL :
219 DBG_ASSERT(_rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN,
220 "OComboBoxModel::setFastPropertyValue_NoBroadcast : invalid type !" );
221 _rValue >>= m_bEmptyIsNull;
222 break;
224 case PROPERTY_ID_DEFAULT_TEXT :
225 DBG_ASSERT(_rValue.getValueType().getTypeClass() == TypeClass_STRING,
226 "OComboBoxModel::setFastPropertyValue_NoBroadcast : invalid type !" );
227 _rValue >>= m_aDefaultText;
228 resetNoBroadcast();
229 break;
231 case PROPERTY_ID_STRINGITEMLIST:
233 ControlModelLock aLock( *this );
234 setNewStringItemList( _rValue, aLock );
235 // FIXME: this is bogus. setNewStringItemList expects a guard which has the *only*
236 // lock to the mutex, but setFastPropertyValue_NoBroadcast is already called with
237 // a lock - so we effectively has two locks here, of which setNewStringItemList can
238 // only control one.
240 break;
242 case PROPERTY_ID_TYPEDITEMLIST:
244 ControlModelLock aLock( *this );
245 setNewTypedItemList( _rValue, aLock );
246 // Same FIXME as above.
248 break;
250 default:
251 OBoundControlModel::setFastPropertyValue_NoBroadcast(_nHandle, _rValue);
255 sal_Bool OComboBoxModel::convertFastPropertyValue(
256 Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue)
258 bool bModified(false);
259 switch (_nHandle)
261 case PROPERTY_ID_LISTSOURCETYPE :
262 bModified = tryPropertyValueEnum(_rConvertedValue, _rOldValue, _rValue, m_eListSourceType);
263 break;
265 case PROPERTY_ID_LISTSOURCE :
266 bModified = tryPropertyValue(_rConvertedValue, _rOldValue, _rValue, m_aListSource);
267 break;
269 case PROPERTY_ID_EMPTY_IS_NULL :
270 bModified = tryPropertyValue(_rConvertedValue, _rOldValue, _rValue, m_bEmptyIsNull);
271 break;
273 case PROPERTY_ID_DEFAULT_TEXT :
274 bModified = tryPropertyValue(_rConvertedValue, _rOldValue, _rValue, m_aDefaultText);
275 break;
277 case PROPERTY_ID_STRINGITEMLIST:
278 bModified = convertNewListSourceProperty( _rConvertedValue, _rOldValue, _rValue );
279 break;
281 case PROPERTY_ID_TYPEDITEMLIST :
282 if (hasExternalListSource())
283 throw IllegalArgumentException();
284 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, getTypedItemList());
285 break;
287 default:
288 bModified = OBoundControlModel::convertFastPropertyValue(_rConvertedValue, _rOldValue, _nHandle, _rValue);
289 break;
291 return bModified;
294 void OComboBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
296 BEGIN_DESCRIBE_PROPERTIES( 7, OBoundControlModel )
297 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
298 DECL_PROP1(LISTSOURCETYPE, ListSourceType, BOUND);
299 DECL_PROP1(LISTSOURCE, OUString, BOUND);
300 DECL_BOOL_PROP1(EMPTY_IS_NULL, BOUND);
301 DECL_PROP1(DEFAULT_TEXT, OUString, BOUND);
302 DECL_PROP1(STRINGITEMLIST, Sequence< OUString >,BOUND);
303 DECL_PROP1(TYPEDITEMLIST, Sequence< Any >, OPTIONAL);
304 END_DESCRIBE_PROPERTIES();
308 void OComboBoxModel::describeAggregateProperties( Sequence< Property >& _rAggregateProps ) const
310 OBoundControlModel::describeAggregateProperties( _rAggregateProps );
312 // superseded properties:
313 RemoveProperty( _rAggregateProps, PROPERTY_STRINGITEMLIST );
314 RemoveProperty( _rAggregateProps, PROPERTY_TYPEDITEMLIST );
318 OUString SAL_CALL OComboBoxModel::getServiceName()
320 return FRM_COMPONENT_COMBOBOX; // old (non-sun) name for compatibility !
324 void SAL_CALL OComboBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
326 OBoundControlModel::write(_rxOutStream);
328 // Version
329 // Version 0x0002: EmptyIsNull
330 // Version 0x0003: ListSource->Seq
331 // Version 0x0004: DefaultText
332 // Version 0x0005: HelpText
333 _rxOutStream->writeShort(0x0006);
335 // Mask for Any
336 sal_uInt16 nAnyMask = 0;
337 if (m_aBoundColumn.getValueType().getTypeClass() == TypeClass_SHORT)
338 nAnyMask |= BOUNDCOLUMN;
339 _rxOutStream << nAnyMask;
341 css::uno::Sequence<OUString> aListSourceSeq(&m_aListSource, 1);
342 _rxOutStream << aListSourceSeq;
343 _rxOutStream << static_cast<sal_Int16>(m_eListSourceType);
345 if ((nAnyMask & BOUNDCOLUMN) == BOUNDCOLUMN)
347 sal_Int16 nBoundColumn = 0;
348 m_aBoundColumn >>= nBoundColumn;
349 _rxOutStream << nBoundColumn;
352 _rxOutStream << m_bEmptyIsNull;
353 _rxOutStream << m_aDefaultText;
354 writeHelpTextCompatibly(_rxOutStream);
356 // from version 0x0006 : common properties
357 writeCommonProperties(_rxOutStream);
361 void SAL_CALL OComboBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream)
363 OBoundControlModel::read(_rxInStream);
364 ControlModelLock aLock( *this );
366 // since we are "overwriting" the StringItemList of our aggregate (means we have
367 // an own place to store the value, instead of relying on our aggregate storing it),
368 // we need to respect what the aggregate just read for the StringItemList property.
371 if ( m_xAggregateSet.is() )
372 setNewStringItemList( m_xAggregateSet->getPropertyValue( PROPERTY_STRINGITEMLIST ), aLock );
374 catch( const Exception& )
376 OSL_FAIL( "OComboBoxModel::read: caught an exception while examining the aggregate's string item list!" );
379 // Version
380 sal_uInt16 nVersion = _rxInStream->readShort();
381 DBG_ASSERT(nVersion > 0, "OComboBoxModel::read : version 0 ? this should never have been written !");
383 if (nVersion > 0x0006)
385 OSL_FAIL("OComboBoxModel::read : invalid (means unknown) version !");
386 m_aListSource.clear();
387 m_aBoundColumn <<= sal_Int16(0);
388 m_aDefaultText.clear();
389 m_eListSourceType = ListSourceType_TABLE;
390 m_bEmptyIsNull = true;
391 defaultCommonProperties();
392 return;
395 // Mask for Any
396 sal_uInt16 nAnyMask;
397 _rxInStream >> nAnyMask;
399 // ListSource
400 if (nVersion < 0x0003)
402 _rxInStream >> m_aListSource;
404 else // nVersion == 4
406 m_aListSource.clear();
407 css::uno::Sequence<OUString> aListSource;
408 _rxInStream >> aListSource;
409 for (const OUString& rToken : std::as_const(aListSource))
410 m_aListSource += rToken;
413 sal_Int16 nListSourceType;
414 _rxInStream >> nListSourceType;
415 m_eListSourceType = static_cast<ListSourceType>(nListSourceType);
417 if ((nAnyMask & BOUNDCOLUMN) == BOUNDCOLUMN)
419 sal_Int16 nValue;
420 _rxInStream >> nValue;
421 m_aBoundColumn <<= nValue;
424 if (nVersion > 0x0001)
426 bool bNull;
427 _rxInStream >> bNull;
428 m_bEmptyIsNull = bNull;
431 if (nVersion > 0x0003) // nVersion == 4
432 _rxInStream >> m_aDefaultText;
434 // StringList must be emptied if a ListSource is set.
435 // This can be the case if we save in alive mode.
436 if ( !m_aListSource.isEmpty()
437 && !hasExternalListSource()
440 setFastPropertyValue( PROPERTY_ID_STRINGITEMLIST, makeAny( css::uno::Sequence<OUString>() ) );
441 setFastPropertyValue( PROPERTY_ID_TYPEDITEMLIST, makeAny( css::uno::Sequence<css::uno::Any>() ) );
444 if (nVersion > 0x0004)
445 readHelpTextCompatibly(_rxInStream);
447 if (nVersion > 0x0005)
448 readCommonProperties(_rxInStream);
450 // After reading in, display the default values
451 if ( !getControlSource().isEmpty() )
453 // (not if we don't have a control source - the "State" property acts like it is persistent, then
454 resetNoBroadcast();
459 void OComboBoxModel::loadData( bool _bForce )
461 DBG_ASSERT(m_eListSourceType != ListSourceType_VALUELIST, "OComboBoxModel::loadData : do not call for a value list !");
462 DBG_ASSERT( !hasExternalListSource(), "OComboBoxModel::loadData: cannot load from DB when I have an external list source!" );
464 if ( hasExternalListSource() )
465 return;
467 // Get Connection
468 if (!m_xCursor.is())
469 return;
470 Reference<XConnection> xConnection = getConnection(m_xCursor);
471 if (!xConnection.is())
472 return;
474 Reference<XServiceInfo> xServiceInfo(xConnection, UNO_QUERY);
475 if (!xServiceInfo.is() || !xServiceInfo->supportsService(SRV_SDB_CONNECTION))
477 OSL_FAIL("OComboBoxModel::loadData : invalid connection !");
478 return;
481 if (m_aListSource.isEmpty() || m_eListSourceType == ListSourceType_VALUELIST)
482 return;
484 ::utl::SharedUNOComponent< XResultSet > xListCursor;
487 m_aListRowSet.setConnection( xConnection );
489 bool bExecuteRowSet( false );
490 switch (m_eListSourceType)
492 case ListSourceType_TABLEFIELDS:
493 // don't work with a statement here, the fields will be collected below
494 break;
495 case ListSourceType_TABLE:
497 // does the bound field belong to the table ?
498 // if we use an alias for the bound field, we won't find it
499 // in that case we use the first field of the table
501 Reference<XNameAccess> xFieldsByName = getTableFields(xConnection, m_aListSource);
503 OUString aFieldName;
504 if ( xFieldsByName.is() && xFieldsByName->hasByName( getControlSource() ) )
506 aFieldName = getControlSource();
508 else
510 // otherwise look for the alias
511 Reference<XPropertySet> xFormProp(m_xCursor,UNO_QUERY);
512 Reference< XColumnsSupplier > xSupplyFields;
513 xFormProp->getPropertyValue("SingleSelectQueryComposer") >>= xSupplyFields;
515 // search the field
516 DBG_ASSERT(xSupplyFields.is(), "OComboBoxModel::loadData : invalid query composer !");
518 Reference< XNameAccess > xFieldNames = xSupplyFields->getColumns();
519 if ( xFieldNames->hasByName( getControlSource() ) )
521 Reference< XPropertySet > xComposerFieldAsSet;
522 xFieldNames->getByName( getControlSource() ) >>= xComposerFieldAsSet;
523 if (hasProperty(PROPERTY_FIELDSOURCE, xComposerFieldAsSet))
524 xComposerFieldAsSet->getPropertyValue(PROPERTY_FIELDSOURCE) >>= aFieldName;
528 if (aFieldName.isEmpty())
529 break;
531 Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
532 OSL_ENSURE(xMeta.is(),"No database meta data!");
533 if ( xMeta.is() )
535 OUString aQuote = xMeta->getIdentifierQuoteString();
537 OUString sCatalog, sSchema, sTable;
538 qualifiedNameComponents( xMeta, m_aListSource, sCatalog, sSchema, sTable, EComposeRule::InDataManipulation );
540 OUStringBuffer aStatement;
541 aStatement.append( "SELECT DISTINCT " );
542 aStatement.append ( quoteName( aQuote, aFieldName ) );
543 aStatement.append( " FROM " );
544 aStatement.append ( composeTableNameForSelect( xConnection, sCatalog, sSchema, sTable ) );
546 m_aListRowSet.setEscapeProcessing( false );
547 m_aListRowSet.setCommand( aStatement.makeStringAndClear() );
548 bExecuteRowSet = true;
550 } break;
551 case ListSourceType_QUERY:
553 m_aListRowSet.setCommandFromQuery( m_aListSource );
554 bExecuteRowSet = true;
556 break;
558 default:
560 m_aListRowSet.setEscapeProcessing( ListSourceType_SQLPASSTHROUGH != m_eListSourceType );
561 m_aListRowSet.setCommand( m_aListSource );
562 bExecuteRowSet = true;
566 if ( bExecuteRowSet )
568 if ( !_bForce && !m_aListRowSet.isDirty() )
570 // if none of the settings of the row set changed, compared to the last
571 // invocation of loadData, then don't re-fill the list. Instead, assume
572 // the list entries are the same.
573 return;
575 xListCursor.reset( m_aListRowSet.execute() );
578 catch(const SQLException& eSQL)
580 onError(eSQL, FRM_RES_STRING(RID_BASELISTBOX_ERROR_FILLLIST));
581 return;
583 catch( const Exception& )
585 DBG_UNHANDLED_EXCEPTION("forms.component");
586 return;
589 ::std::vector< OUString > aStringList;
590 aStringList.reserve(16);
593 OSL_ENSURE( xListCursor.is() || ( ListSourceType_TABLEFIELDS == m_eListSourceType ),
594 "OComboBoxModel::loadData: logic error!" );
595 if ( !xListCursor.is() && ( ListSourceType_TABLEFIELDS != m_eListSourceType ) )
596 return;
598 switch (m_eListSourceType)
600 case ListSourceType_SQL:
601 case ListSourceType_SQLPASSTHROUGH:
602 case ListSourceType_TABLE:
603 case ListSourceType_QUERY:
605 // The XDatabaseVariant of the first column
606 Reference<XColumnsSupplier> xSupplyCols(xListCursor, UNO_QUERY);
607 DBG_ASSERT(xSupplyCols.is(), "OComboBoxModel::loadData : cursor supports the row set service but is no column supplier?!");
608 Reference<XIndexAccess> xColumns;
609 if (xSupplyCols.is())
611 xColumns.set(xSupplyCols->getColumns(), UNO_QUERY);
612 DBG_ASSERT(xColumns.is(), "OComboBoxModel::loadData : no columns supplied by the row set !");
614 Reference< XPropertySet > xDataField;
615 if ( xColumns.is() )
616 xColumns->getByIndex(0) >>= xDataField;
617 if ( !xDataField.is() )
618 return;
620 ::dbtools::FormattedColumnValue aValueFormatter( getContext(), m_xCursor, xDataField );
622 // Fill Lists
623 sal_Int16 i = 0;
624 // At the moment by definition the list cursor is positioned _before_ the first row
625 while (xListCursor->next() && (i++<SHRT_MAX)) // Set max. count
627 aStringList.push_back( aValueFormatter.getFormattedValue() );
630 break;
631 case ListSourceType_TABLEFIELDS:
633 Reference<XNameAccess> xFieldNames = getTableFields(xConnection, m_aListSource);
634 if (xFieldNames.is())
636 const Sequence<OUString> aFieldNames = xFieldNames->getElementNames();
637 for (const OUString& rustrNames : aFieldNames)
638 aStringList.push_back(rustrNames);
641 break;
642 default:
643 OSL_FAIL( "OComboBoxModel::loadData: unreachable!" );
644 break;
647 catch(const SQLException& eSQL)
649 onError(eSQL, FRM_RES_STRING(RID_BASELISTBOX_ERROR_FILLLIST));
650 return;
652 catch( const Exception& )
654 DBG_UNHANDLED_EXCEPTION("forms.component");
655 return;
658 // Set String-Sequence at ListBox
659 setFastPropertyValue( PROPERTY_ID_STRINGITEMLIST, makeAny( comphelper::containerToSequence(aStringList) ) );
660 // Reset TypedItemList, no matching data.
661 setFastPropertyValue( PROPERTY_ID_TYPEDITEMLIST, makeAny( css::uno::Sequence<css::uno::Any>() ) );
665 void OComboBoxModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
667 Reference<XPropertySet> xField = getField();
668 if ( xField.is() )
669 m_pValueFormatter.reset( new ::dbtools::FormattedColumnValue( getContext(), Reference< XRowSet >( _rxForm, UNO_QUERY ), xField ) );
670 getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= m_aDesignModeStringItems;
672 // Only load data if a ListSource was supplied
673 if ( !m_aListSource.isEmpty() && m_xCursor.is() && !hasExternalListSource() )
674 loadData( false );
678 void OComboBoxModel::onDisconnectedDbColumn()
680 m_pValueFormatter.reset();
682 // reset the string item list
683 if ( !hasExternalListSource() )
684 setFastPropertyValue( PROPERTY_ID_STRINGITEMLIST, makeAny( m_aDesignModeStringItems ) );
686 m_aListRowSet.dispose();
690 void SAL_CALL OComboBoxModel::reloaded( const EventObject& aEvent )
692 OBoundControlModel::reloaded(aEvent);
694 // reload data if we have a list source
695 if ( !m_aListSource.isEmpty() && m_xCursor.is() && !hasExternalListSource() )
696 loadData( false );
700 void OComboBoxModel::resetNoBroadcast()
702 OBoundControlModel::resetNoBroadcast();
703 m_aLastKnownValue.clear();
707 bool OComboBoxModel::commitControlValueToDbColumn( bool _bPostReset )
709 Any aNewValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
711 OUString sNewValue;
712 aNewValue >>= sNewValue;
714 bool bModified = ( aNewValue != m_aLastKnownValue );
715 if ( bModified )
717 if ( !aNewValue.hasValue()
718 || ( sNewValue.isEmpty() // an empty string
719 && m_bEmptyIsNull // which should be interpreted as NULL
723 m_xColumnUpdate->updateNull();
725 else
729 OSL_PRECOND(m_pValueFormatter,
730 "OComboBoxModel::commitControlValueToDbColumn: no value formatter!");
731 if (m_pValueFormatter)
733 if ( !m_pValueFormatter->setFormattedValue( sNewValue ) )
734 return false;
736 else
737 m_xColumnUpdate->updateString( sNewValue );
739 catch ( const Exception& )
741 return false;
745 m_aLastKnownValue = aNewValue;
748 // add the new value to the list
749 bool bAddToList = bModified && !_bPostReset;
750 // (only if this is not the "commit" triggered by a "reset")
752 if ( bAddToList )
754 css::uno::Sequence<OUString> aStringItemList;
755 if ( getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aStringItemList )
757 bool bFound = false;
758 for (const OUString& rStringItem : std::as_const(aStringItemList))
760 if ( (bFound = rStringItem == sNewValue) )
761 break;
764 // not found -> add
765 if (!bFound)
767 sal_Int32 nOldLen = aStringItemList.getLength();
768 aStringItemList.realloc( nOldLen + 1 );
769 aStringItemList[ nOldLen ] = sNewValue;
771 setFastPropertyValue( PROPERTY_ID_STRINGITEMLIST, makeAny( aStringItemList ) );
772 setFastPropertyValue( PROPERTY_ID_TYPEDITEMLIST, makeAny( css::uno::Sequence<css::uno::Any>() ) );
777 return true;
780 // XPropertiesChangeListener
782 Any OComboBoxModel::translateDbColumnToControlValue()
784 OSL_PRECOND(m_pValueFormatter,
785 "OComboBoxModel::translateDbColumnToControlValue: no value formatter!");
786 if (m_pValueFormatter)
788 OUString sValue( m_pValueFormatter->getFormattedValue() );
789 if ( sValue.isEmpty()
790 && m_pValueFormatter->getColumn().is()
791 && m_pValueFormatter->getColumn()->wasNull()
794 m_aLastKnownValue.clear();
796 else
799 m_aLastKnownValue <<= sValue;
802 else
803 m_aLastKnownValue.clear();
805 return m_aLastKnownValue.hasValue() ? m_aLastKnownValue : makeAny( OUString() );
806 // (m_aLastKnownValue is allowed to be VOID, the control value isn't)
810 Any OComboBoxModel::getDefaultForReset() const
812 return makeAny( m_aDefaultText );
816 void OComboBoxModel::stringItemListChanged( ControlModelLock& /*_rInstanceLock*/ )
818 if ( m_xAggregateSet.is() )
820 m_xAggregateSet->setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( comphelper::containerToSequence(getStringItemList()) ) );
821 m_xAggregateSet->setPropertyValue( PROPERTY_TYPEDITEMLIST, makeAny( getTypedItemList()) ) ;
826 void OComboBoxModel::refreshInternalEntryList()
828 DBG_ASSERT( !hasExternalListSource(), "OComboBoxModel::refreshInternalEntryList: invalid call!" );
830 if ( !hasExternalListSource( )
831 && ( m_eListSourceType != ListSourceType_VALUELIST )
832 && ( m_xCursor.is() )
835 loadData( true );
840 void SAL_CALL OComboBoxModel::disposing( const EventObject& _rSource )
842 if ( !OEntryListHelper::handleDisposing( _rSource ) )
843 OBoundControlModel::disposing( _rSource );
847 //= OComboBoxControl
849 OComboBoxControl::OComboBoxControl(const Reference<XComponentContext>& _rxContext)
850 :OBoundControl(_rxContext, VCL_CONTROL_COMBOBOX)
855 css::uno::Sequence<OUString> SAL_CALL OComboBoxControl::getSupportedServiceNames()
857 css::uno::Sequence<OUString> aSupported = OBoundControl::getSupportedServiceNames();
858 aSupported.realloc(aSupported.getLength() + 2);
860 OUString* pArray = aSupported.getArray();
861 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_COMBOBOX;
862 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_COMBOBOX;
863 return aSupported;
868 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
869 com_sun_star_form_OComboBoxModel_get_implementation(css::uno::XComponentContext* component,
870 css::uno::Sequence<css::uno::Any> const &)
872 return cppu::acquire(new frm::OComboBoxModel(component));
875 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
876 com_sun_star_form_OComboBoxControl_get_implementation(css::uno::XComponentContext* component,
877 css::uno::Sequence<css::uno::Any> const &)
879 return cppu::acquire(new frm::OComboBoxControl(component));
882 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */