use insert function instead of for loop
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghandler.cxx
blobb1acab9ccbceab68c947edb7d09498b2d5393502
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 .
20 #include "cellbindinghandler.hxx"
21 #include "formstrings.hxx"
22 #include "formmetadata.hxx"
23 #include "cellbindinghelper.hxx"
25 #include <com/sun/star/form/binding/XValueBinding.hpp>
26 #include <com/sun/star/lang/NullPointerException.hpp>
27 #include <com/sun/star/table/CellAddress.hpp>
28 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
29 #include <tools/debug.hxx>
30 #include <comphelper/diagnose_ex.hxx>
33 namespace pcr
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::table;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::frame;
42 using namespace ::com::sun::star::inspection;
43 using namespace ::com::sun::star::form::binding;
44 using namespace ::comphelper;
46 CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
47 :PropertyHandlerComponent( _rxContext )
48 ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<sal_Int16>::get(), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
53 OUString CellBindingPropertyHandler::getImplementationName( )
55 return u"com.sun.star.comp.extensions.CellBindingPropertyHandler"_ustr;
59 Sequence< OUString > CellBindingPropertyHandler::getSupportedServiceNames( )
61 return { u"com.sun.star.form.inspection.CellBindingPropertyHandler"_ustr };
65 void CellBindingPropertyHandler::onNewComponent()
67 PropertyHandlerComponent::onNewComponent();
69 Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
70 DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
71 if ( CellBindingHelper::isSpreadsheetDocument( xDocument ) )
72 m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
76 CellBindingPropertyHandler::~CellBindingPropertyHandler( )
81 Sequence< OUString > SAL_CALL CellBindingPropertyHandler::getActuatingProperties( )
83 Sequence< OUString > aInterestingProperties{ PROPERTY_LIST_CELL_RANGE,
84 PROPERTY_BOUND_CELL,
85 PROPERTY_CONTROLSOURCE };
86 return aInterestingProperties;
90 void SAL_CALL CellBindingPropertyHandler::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
92 ::osl::MutexGuard aGuard( m_aMutex );
93 PropertyId nActuatingPropId( impl_getPropertyId_throwRuntime( _rActuatingPropertyName ) );
94 OSL_PRECOND(m_pHelper,
95 "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistency!");
96 // if we survived impl_getPropertyId_throwRuntime, we should have a helper, since no helper implies no properties
98 OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
99 if ( !_rxInspectorUI.is() )
100 throw NullPointerException();
102 std::vector< PropertyId > aDependentProperties;
104 switch ( nActuatingPropId )
106 // ----- BoundCell -----
107 case PROPERTY_ID_BOUND_CELL:
109 // the SQL-data-binding related properties need to be enabled if and only if
110 // there is *no* valid cell binding
111 Reference< XValueBinding > xBinding;
112 _rNewValue >>= xBinding;
114 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
115 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
116 if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
117 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
119 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
120 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
121 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
122 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
124 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
126 if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
128 // ensure that the "transfer selection as" property is reset. Since we can't remember
129 // it at the object itself, but derive it from the binding only, we have to normalize
130 // it now that there *is* no binding anymore.
131 setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, Any( sal_Int16(0) ) );
134 break;
136 // ----- CellRange -----
137 case PROPERTY_ID_LIST_CELL_RANGE:
139 // the list source related properties need to be enabled if and only if
140 // there is *no* valid external list source for the control
141 Reference< XListEntrySource > xSource;
142 _rNewValue >>= xSource;
144 _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
145 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
146 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
148 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
150 // also reset the list entries if the cell range is reset
151 // #i28319#
152 if ( !_bFirstTimeInit )
156 if ( !xSource.is() )
158 setPropertyValue( PROPERTY_STRINGITEMLIST, Any( Sequence< OUString >() ) );
159 setPropertyValue( PROPERTY_TYPEDITEMLIST, Any( Sequence< Any >() ) );
162 catch( const Exception& )
164 TOOLS_WARN_EXCEPTION(
165 "extensions.propctrlr",
166 "ListCellRange: caught an exception while resetting the string items!");
170 break; // case PROPERTY_ID_LIST_CELL_RANGE
172 // ----- DataField -----
173 case PROPERTY_ID_CONTROLSOURCE:
175 OUString sControlSource;
176 _rNewValue >>= sControlSource;
177 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
178 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
180 break; // case PROPERTY_ID_CONTROLSOURCE
182 default:
183 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
186 for (auto const& dependentProperty : aDependentProperties)
188 impl_updateDependentProperty_nothrow( dependentProperty, _rxInspectorUI );
193 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
197 switch ( _nPropId )
199 // ----- BoundColumn -----
200 case PROPERTY_ID_BOUNDCOLUMN:
202 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
203 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
204 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
206 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
207 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
209 break; // case PROPERTY_ID_BOUNDCOLUMN
211 } // switch
214 catch( const Exception& )
216 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow" );
221 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
223 ::osl::MutexGuard aGuard( m_aMutex );
224 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
226 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::getPropertyValue: inconsistency!");
227 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
229 Any aReturn;
230 switch ( nPropId )
232 case PROPERTY_ID_BOUND_CELL:
234 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
235 if ( !CellBindingHelper::isCellBinding( xBinding ) )
236 xBinding.clear();
238 aReturn <<= xBinding;
240 break;
242 case PROPERTY_ID_LIST_CELL_RANGE:
244 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
245 if ( !CellBindingHelper::isCellRangeListSource( xSource ) )
246 xSource.clear();
248 aReturn <<= xSource;
250 break;
252 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
254 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
255 aReturn <<= static_cast<sal_Int16>( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
257 break;
259 default:
260 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
261 break;
263 return aReturn;
267 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
269 ::osl::MutexGuard aGuard( m_aMutex );
270 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
272 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::setPropertyValue: inconsistency!");
273 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
277 Any aOldValue = getPropertyValue( _rPropertyName );
279 switch ( nPropId )
281 case PROPERTY_ID_BOUND_CELL:
283 Reference< XValueBinding > xBinding;
284 _rValue >>= xBinding;
285 m_pHelper->setBinding( xBinding );
287 break;
289 case PROPERTY_ID_LIST_CELL_RANGE:
291 Reference< XListEntrySource > xSource;
292 _rValue >>= xSource;
293 m_pHelper->setListSource( xSource );
295 break;
297 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
299 sal_Int16 nExchangeType = 0;
300 OSL_VERIFY( _rValue >>= nExchangeType );
302 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
303 if ( xBinding.is() )
305 bool bNeedIntegerBinding = ( nExchangeType == 1 );
306 if ( bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
308 CellAddress aAddress;
309 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
311 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
312 m_pHelper->setBinding( xBinding );
317 break;
319 default:
320 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
321 break;
324 impl_setContextDocumentModified_nothrow();
326 Any aNewValue( getPropertyValue( _rPropertyName ) );
327 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
328 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
329 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
330 // and handle the listener notification itself
332 catch( const Exception& )
334 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingPropertyHandler::setPropertyValue" );
339 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
341 ::osl::MutexGuard aGuard( m_aMutex );
342 Any aPropertyValue;
344 OSL_ENSURE(
345 m_pHelper,
346 "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!");
347 if (!m_pHelper)
348 return aPropertyValue;
350 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
352 OUString sControlValue;
353 OSL_VERIFY( _rControlValue >>= sControlValue );
354 switch( nPropId )
356 case PROPERTY_ID_LIST_CELL_RANGE:
357 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
358 break;
360 case PROPERTY_ID_BOUND_CELL:
362 // if we have the possibility of an integer binding, then we must preserve
363 // this property's value (e.g. if the current binding is an integer binding, then
364 // the newly created one must be, too)
365 bool bIntegerBinding = false;
366 if ( m_pHelper->isCellIntegerBindingAllowed() )
368 sal_Int16 nCurrentBindingType = 0;
369 getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
370 bIntegerBinding = ( nCurrentBindingType != 0 );
372 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
374 break;
376 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
377 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
378 break;
380 default:
381 OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
382 break;
385 return aPropertyValue;
389 Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const OUString& _rPropertyName,
390 const Any& _rPropertyValue, const Type& /*_rControlValueType*/ )
392 ::osl::MutexGuard aGuard( m_aMutex );
393 Any aControlValue;
395 OSL_ENSURE(
396 m_pHelper,
397 "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!");
398 if (!m_pHelper)
399 return aControlValue;
401 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
403 switch ( nPropId )
405 case PROPERTY_ID_BOUND_CELL:
407 Reference< XValueBinding > xBinding;
408 bool bSuccess = _rPropertyValue >>= xBinding;
409 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
411 // the only value binding we support so far is linking to spreadsheet cells
412 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
414 break;
416 case PROPERTY_ID_LIST_CELL_RANGE:
418 Reference< XListEntrySource > xSource;
419 bool bSuccess = _rPropertyValue >>= xSource;
420 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
422 // the only value binding we support so far is linking to spreadsheet cells
423 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
425 break;
427 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
428 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
429 break;
431 default:
432 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
433 break;
436 return aControlValue;
440 Sequence< Property > CellBindingPropertyHandler::doDescribeSupportedProperties() const
442 std::vector< Property > aProperties;
444 bool bAllowCellLinking = m_pHelper && m_pHelper->isCellBindingAllowed();
445 bool bAllowCellIntLinking = m_pHelper && m_pHelper->isCellIntegerBindingAllowed();
446 bool bAllowListCellRange = m_pHelper && m_pHelper->isListCellRangeAllowed();
447 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
449 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
450 + ( bAllowListCellRange ? 1 : 0 )
451 + ( bAllowCellIntLinking ? 1 : 0 );
452 aProperties.resize( nPos );
454 if ( bAllowCellLinking )
456 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
457 ::cppu::UnoType<OUString>::get(), 0 );
459 if ( bAllowCellIntLinking )
461 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
462 ::cppu::UnoType<sal_Int16>::get(), 0 );
464 if ( bAllowListCellRange )
466 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
467 ::cppu::UnoType<OUString>::get(), 0 );
471 if ( aProperties.empty() )
472 return Sequence< Property >();
473 return comphelper::containerToSequence(aProperties);
477 } // namespace pcr
479 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
480 extensions_propctrlr_CellBindingPropertyHandler_get_implementation(
481 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
483 return cppu::acquire(new pcr::CellBindingPropertyHandler(context));
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */