Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghandler.cxx
blob5db199db94ef371d937d36291e17c3bc3ce683e6
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"
24 #include "pcrservices.hxx"
26 #include <com/sun/star/form/binding/XValueBinding.hpp>
27 #include <com/sun/star/lang/NullPointerException.hpp>
28 #include <com/sun/star/table/CellAddress.hpp>
29 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
30 #include <tools/debug.hxx>
33 extern "C" void createRegistryInfo_CellBindingPropertyHandler()
35 ::pcr::CellBindingPropertyHandler::registerImplementation();
39 namespace pcr
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::table;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::script;
48 using namespace ::com::sun::star::frame;
49 using namespace ::com::sun::star::inspection;
50 using namespace ::com::sun::star::form::binding;
51 using namespace ::comphelper;
53 CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
54 :CellBindingPropertyHandler_Base( _rxContext )
55 ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<sal_Int16>::get(), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
60 OUString CellBindingPropertyHandler::getImplementationName_static( )
62 return "com.sun.star.comp.extensions.CellBindingPropertyHandler";
66 Sequence< OUString > CellBindingPropertyHandler::getSupportedServiceNames_static( )
68 Sequence<OUString> aSupported { "com.sun.star.form.inspection.CellBindingPropertyHandler" };
69 return aSupported;
73 void CellBindingPropertyHandler::onNewComponent()
75 PropertyHandlerComponent::onNewComponent();
77 Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
78 DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
79 if ( CellBindingHelper::isSpreadsheetDocument( xDocument ) )
80 m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
84 CellBindingPropertyHandler::~CellBindingPropertyHandler( )
89 Sequence< OUString > SAL_CALL CellBindingPropertyHandler::getActuatingProperties( )
91 Sequence< OUString > aInterestingProperties( 3 );
92 aInterestingProperties[0] = PROPERTY_LIST_CELL_RANGE;
93 aInterestingProperties[1] = PROPERTY_BOUND_CELL;
94 aInterestingProperties[2] = PROPERTY_CONTROLSOURCE;
95 return aInterestingProperties;
99 void SAL_CALL CellBindingPropertyHandler::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
101 ::osl::MutexGuard aGuard( m_aMutex );
102 PropertyId nActuatingPropId( impl_getPropertyId_throwRuntime( _rActuatingPropertyName ) );
103 OSL_PRECOND(m_pHelper,
104 "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistency!");
105 // if we survived impl_getPropertyId_throwRuntime, we should have a helper, since no helper implies no properties
107 OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
108 if ( !_rxInspectorUI.is() )
109 throw NullPointerException();
111 std::vector< PropertyId > aDependentProperties;
113 switch ( nActuatingPropId )
115 // ----- BoundCell -----
116 case PROPERTY_ID_BOUND_CELL:
118 // the SQL-data-binding related properties need to be enabled if and only if
119 // there is *no* valid cell binding
120 Reference< XValueBinding > xBinding;
121 _rNewValue >>= xBinding;
123 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
124 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
125 if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
126 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
128 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
129 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
130 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
131 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
133 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
135 if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
137 // ensure that the "transfer selection as" property is reset. Since we can't remember
138 // it at the object itself, but derive it from the binding only, we have to normalize
139 // it now that there *is* no binding anymore.
140 setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, makeAny( sal_Int16(0) ) );
143 break;
145 // ----- CellRange -----
146 case PROPERTY_ID_LIST_CELL_RANGE:
148 // the list source related properties need to be enabled if and only if
149 // there is *no* valid external list source for the control
150 Reference< XListEntrySource > xSource;
151 _rNewValue >>= xSource;
153 _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
154 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
155 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
157 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
159 // also reset the list entries if the cell range is reset
160 // #i28319#
161 if ( !_bFirstTimeInit )
165 if ( !xSource.is() )
167 setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( Sequence< OUString >() ) );
168 setPropertyValue( PROPERTY_TYPEDITEMLIST, makeAny( Sequence< Any >() ) );
171 catch( const Exception& )
173 OSL_FAIL( "OPropertyBrowserController::actuatingPropertyChanged( ListCellRange ): caught an exception while resetting the string items!" );
177 break; // case PROPERTY_ID_LIST_CELL_RANGE
179 // ----- DataField -----
180 case PROPERTY_ID_CONTROLSOURCE:
182 OUString sControlSource;
183 _rNewValue >>= sControlSource;
184 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
185 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
187 break; // case PROPERTY_ID_CONTROLSOURCE
189 default:
190 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
193 for (auto const& dependentProperty : aDependentProperties)
195 impl_updateDependentProperty_nothrow( dependentProperty, _rxInspectorUI );
200 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
204 switch ( _nPropId )
206 // ----- BoundColumn -----
207 case PROPERTY_ID_BOUNDCOLUMN:
209 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
210 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
211 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
213 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
214 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
216 break; // case PROPERTY_ID_BOUNDCOLUMN
218 } // switch
221 catch( const Exception& )
223 OSL_FAIL( "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow: caught an exception!" );
228 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
230 ::osl::MutexGuard aGuard( m_aMutex );
231 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
233 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::getPropertyValue: inconsistency!");
234 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
236 Any aReturn;
237 switch ( nPropId )
239 case PROPERTY_ID_BOUND_CELL:
241 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
242 if ( !CellBindingHelper::isCellBinding( xBinding ) )
243 xBinding.clear();
245 aReturn <<= xBinding;
247 break;
249 case PROPERTY_ID_LIST_CELL_RANGE:
251 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
252 if ( !CellBindingHelper::isCellRangeListSource( xSource ) )
253 xSource.clear();
255 aReturn <<= xSource;
257 break;
259 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
261 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
262 aReturn <<= static_cast<sal_Int16>( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
264 break;
266 default:
267 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
268 break;
270 return aReturn;
274 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
276 ::osl::MutexGuard aGuard( m_aMutex );
277 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
279 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::setPropertyValue: inconsistency!");
280 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
284 Any aOldValue = getPropertyValue( _rPropertyName );
286 switch ( nPropId )
288 case PROPERTY_ID_BOUND_CELL:
290 Reference< XValueBinding > xBinding;
291 _rValue >>= xBinding;
292 m_pHelper->setBinding( xBinding );
294 break;
296 case PROPERTY_ID_LIST_CELL_RANGE:
298 Reference< XListEntrySource > xSource;
299 _rValue >>= xSource;
300 m_pHelper->setListSource( xSource );
302 break;
304 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
306 sal_Int16 nExchangeType = 0;
307 OSL_VERIFY( _rValue >>= nExchangeType );
309 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
310 if ( xBinding.is() )
312 bool bNeedIntegerBinding = ( nExchangeType == 1 );
313 if ( bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
315 CellAddress aAddress;
316 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
318 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
319 m_pHelper->setBinding( xBinding );
324 break;
326 default:
327 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
328 break;
331 impl_setContextDocumentModified_nothrow();
333 Any aNewValue( getPropertyValue( _rPropertyName ) );
334 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
335 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
336 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
337 // and handle the listener notification itself
339 catch( const Exception& )
341 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: caught an exception!" );
346 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
348 ::osl::MutexGuard aGuard( m_aMutex );
349 Any aPropertyValue;
351 OSL_ENSURE(
352 m_pHelper,
353 "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!");
354 if (!m_pHelper)
355 return aPropertyValue;
357 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
359 OUString sControlValue;
360 OSL_VERIFY( _rControlValue >>= sControlValue );
361 switch( nPropId )
363 case PROPERTY_ID_LIST_CELL_RANGE:
364 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
365 break;
367 case PROPERTY_ID_BOUND_CELL:
369 // if we have the possibility of an integer binding, then we must preserve
370 // this property's value (e.g. if the current binding is an integer binding, then
371 // the newly created one must be, too)
372 bool bIntegerBinding = false;
373 if ( m_pHelper->isCellIntegerBindingAllowed() )
375 sal_Int16 nCurrentBindingType = 0;
376 getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
377 bIntegerBinding = ( nCurrentBindingType != 0 );
379 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
381 break;
383 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
384 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
385 break;
387 default:
388 OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
389 break;
392 return aPropertyValue;
396 Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const OUString& _rPropertyName,
397 const Any& _rPropertyValue, const Type& /*_rControlValueType*/ )
399 ::osl::MutexGuard aGuard( m_aMutex );
400 Any aControlValue;
402 OSL_ENSURE(
403 m_pHelper,
404 "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!");
405 if (!m_pHelper)
406 return aControlValue;
408 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
410 switch ( nPropId )
412 case PROPERTY_ID_BOUND_CELL:
414 Reference< XValueBinding > xBinding;
415 bool bSuccess = _rPropertyValue >>= xBinding;
416 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
418 // the only value binding we support so far is linking to spreadsheet cells
419 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
421 break;
423 case PROPERTY_ID_LIST_CELL_RANGE:
425 Reference< XListEntrySource > xSource;
426 bool bSuccess = _rPropertyValue >>= xSource;
427 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
429 // the only value binding we support so far is linking to spreadsheet cells
430 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
432 break;
434 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
435 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
436 break;
438 default:
439 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
440 break;
443 return aControlValue;
447 Sequence< Property > CellBindingPropertyHandler::doDescribeSupportedProperties() const
449 std::vector< Property > aProperties;
451 bool bAllowCellLinking = m_pHelper.get() && m_pHelper->isCellBindingAllowed();
452 bool bAllowCellIntLinking = m_pHelper.get() && m_pHelper->isCellIntegerBindingAllowed();
453 bool bAllowListCellRange = m_pHelper.get() && m_pHelper->isListCellRangeAllowed();
454 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
456 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
457 + ( bAllowListCellRange ? 1 : 0 )
458 + ( bAllowCellIntLinking ? 1 : 0 );
459 aProperties.resize( nPos );
461 if ( bAllowCellLinking )
463 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
464 ::cppu::UnoType<OUString>::get(), 0 );
466 if ( bAllowCellIntLinking )
468 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
469 ::cppu::UnoType<sal_Int16>::get(), 0 );
471 if ( bAllowListCellRange )
473 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
474 ::cppu::UnoType<OUString>::get(), 0 );
478 if ( aProperties.empty() )
479 return Sequence< Property >();
480 return comphelper::containerToSequence(aProperties);
484 } // namespace pcr
487 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */