Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghandler.cxx
blobb45657864ab581cf2a84ea2b33678e506aa87f19
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 SAL_CALL 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 SAL_CALL CellBindingPropertyHandler::getImplementationName_static( )
62 return OUString( "com.sun.star.comp.extensions.CellBindingPropertyHandler" );
66 Sequence< OUString > SAL_CALL 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.get(), "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistentcy!" );
104 // if we survived impl_getPropertyId_throwRuntime, we should have a helper, since no helper implies no properties
106 OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
107 if ( !_rxInspectorUI.is() )
108 throw NullPointerException();
110 std::vector< PropertyId > aDependentProperties;
112 switch ( nActuatingPropId )
114 // ----- BoundCell -----
115 case PROPERTY_ID_BOUND_CELL:
117 // the SQL-data-binding related properties need to be enabled if and only if
118 // there is *no* valid cell binding
119 Reference< XValueBinding > xBinding;
120 _rNewValue >>= xBinding;
122 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
123 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
124 if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
125 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
127 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
128 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
129 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
130 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
132 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
134 if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
136 // ensure that the "transfer selection as" property is reset. Since we can't remember
137 // it at the object itself, but derive it from the binding only, we have to normalize
138 // it now that there *is* no binding anymore.
139 setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, makeAny( (sal_Int16) 0 ) );
142 break;
144 // ----- CellRange -----
145 case PROPERTY_ID_LIST_CELL_RANGE:
147 // the list source related properties need to be enabled if and only if
148 // there is *no* valid external list source for the control
149 Reference< XListEntrySource > xSource;
150 _rNewValue >>= xSource;
152 _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
153 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
154 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
156 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
158 // also reset the list entries if the cell range is reset
159 // #i28319#
160 if ( !_bFirstTimeInit )
164 if ( !xSource.is() )
166 setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( Sequence< OUString >() ) );
167 setPropertyValue( PROPERTY_TYPEDITEMLIST, makeAny( Sequence< Any >() ) );
170 catch( const Exception& )
172 OSL_FAIL( "OPropertyBrowserController::actuatingPropertyChanged( ListCellRange ): caught an exception while resetting the string items!" );
176 break; // case PROPERTY_ID_LIST_CELL_RANGE
178 // ----- DataField -----
179 case PROPERTY_ID_CONTROLSOURCE:
181 OUString sControlSource;
182 _rNewValue >>= sControlSource;
183 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
184 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
186 break; // case PROPERTY_ID_CONTROLSOURCE
188 default:
189 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
192 for ( std::vector< PropertyId >::const_iterator loopAffected = aDependentProperties.begin();
193 loopAffected != aDependentProperties.end();
194 ++loopAffected
197 impl_updateDependentProperty_nothrow( *loopAffected, _rxInspectorUI );
202 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
206 switch ( _nPropId )
208 // ----- BoundColumn -----
209 case PROPERTY_ID_BOUNDCOLUMN:
211 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
212 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
213 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
215 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
216 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
218 break; // case PROPERTY_ID_BOUNDCOLUMN
220 } // switch
223 catch( const Exception& )
225 OSL_FAIL( "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow: caught an exception!" );
230 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
232 ::osl::MutexGuard aGuard( m_aMutex );
233 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
235 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::getPropertyValue: inconsistency!" );
236 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
238 Any aReturn;
239 switch ( nPropId )
241 case PROPERTY_ID_BOUND_CELL:
243 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
244 if ( !CellBindingHelper::isCellBinding( xBinding ) )
245 xBinding.clear();
247 aReturn <<= xBinding;
249 break;
251 case PROPERTY_ID_LIST_CELL_RANGE:
253 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
254 if ( !CellBindingHelper::isCellRangeListSource( xSource ) )
255 xSource.clear();
257 aReturn <<= xSource;
259 break;
261 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
263 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
264 aReturn <<= (sal_Int16)( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
266 break;
268 default:
269 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
270 break;
272 return aReturn;
276 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
278 ::osl::MutexGuard aGuard( m_aMutex );
279 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
281 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::setPropertyValue: inconsistency!" );
282 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
286 Any aOldValue = getPropertyValue( _rPropertyName );
288 switch ( nPropId )
290 case PROPERTY_ID_BOUND_CELL:
292 Reference< XValueBinding > xBinding;
293 _rValue >>= xBinding;
294 m_pHelper->setBinding( xBinding );
296 break;
298 case PROPERTY_ID_LIST_CELL_RANGE:
300 Reference< XListEntrySource > xSource;
301 _rValue >>= xSource;
302 m_pHelper->setListSource( xSource );
304 break;
306 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
308 sal_Int16 nExchangeType = 0;
309 OSL_VERIFY( _rValue >>= nExchangeType );
311 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
312 if ( xBinding.is() )
314 bool bNeedIntegerBinding = ( nExchangeType == 1 );
315 if ( bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
317 CellAddress aAddress;
318 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
320 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
321 m_pHelper->setBinding( xBinding );
326 break;
328 default:
329 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
330 break;
333 impl_setContextDocumentModified_nothrow();
335 Any aNewValue( getPropertyValue( _rPropertyName ) );
336 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
337 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
338 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
339 // and handle the listener notification itself
341 catch( const Exception& )
343 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: caught an exception!" );
348 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
350 ::osl::MutexGuard aGuard( m_aMutex );
351 Any aPropertyValue;
353 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!" );
354 if ( !m_pHelper.get() )
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( m_pHelper.get(), "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!" );
403 if ( !m_pHelper.get() )
404 return aControlValue;
406 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
408 switch ( nPropId )
410 case PROPERTY_ID_BOUND_CELL:
412 Reference< XValueBinding > xBinding;
413 bool bSuccess = _rPropertyValue >>= xBinding;
414 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
416 // the only value binding we support so far is linking to spreadsheet cells
417 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
419 break;
421 case PROPERTY_ID_LIST_CELL_RANGE:
423 Reference< XListEntrySource > xSource;
424 bool bSuccess = _rPropertyValue >>= xSource;
425 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
427 // the only value binding we support so far is linking to spreadsheet cells
428 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
430 break;
432 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
433 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
434 break;
436 default:
437 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
438 break;
441 return aControlValue;
445 Sequence< Property > SAL_CALL CellBindingPropertyHandler::doDescribeSupportedProperties() const
447 std::vector< Property > aProperties;
449 bool bAllowCellLinking = m_pHelper.get() && m_pHelper->isCellBindingAllowed();
450 bool bAllowCellIntLinking = m_pHelper.get() && m_pHelper->isCellIntegerBindingAllowed();
451 bool bAllowListCellRange = m_pHelper.get() && m_pHelper->isListCellRangeAllowed();
452 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
454 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
455 + ( bAllowListCellRange ? 1 : 0 )
456 + ( bAllowCellIntLinking ? 1 : 0 );
457 aProperties.resize( nPos );
459 if ( bAllowCellLinking )
461 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
462 ::cppu::UnoType<OUString>::get(), 0 );
464 if ( bAllowCellIntLinking )
466 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
467 ::cppu::UnoType<sal_Int16>::get(), 0 );
469 if ( bAllowListCellRange )
471 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
472 ::cppu::UnoType<OUString>::get(), 0 );
476 if ( aProperties.empty() )
477 return Sequence< Property >();
478 return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
482 } // namespace pcr
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */