bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghandler.cxx
blobe994186ca79d2e797e6e65a250a734cc72ed5871
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/table/CellAddress.hpp>
28 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
29 #include <tools/debug.hxx>
32 extern "C" void SAL_CALL createRegistryInfo_CellBindingPropertyHandler()
34 ::pcr::CellBindingPropertyHandler::registerImplementation();
38 namespace pcr
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::table;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::script;
47 using namespace ::com::sun::star::frame;
48 using namespace ::com::sun::star::inspection;
49 using namespace ::com::sun::star::form::binding;
50 using namespace ::comphelper;
52 CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
53 :CellBindingPropertyHandler_Base( _rxContext )
54 ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<sal_Int16>::get(), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
59 OUString SAL_CALL CellBindingPropertyHandler::getImplementationName_static( ) throw (RuntimeException)
61 return OUString( "com.sun.star.comp.extensions.CellBindingPropertyHandler" );
65 Sequence< OUString > SAL_CALL CellBindingPropertyHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
67 Sequence< OUString > aSupported( 1 );
68 aSupported[0] = "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( ) throw (RuntimeException, std::exception)
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 ) throw (NullPointerException, RuntimeException, std::exception)
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() )
165 setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( Sequence< OUString >() ) );
167 catch( const Exception& )
169 OSL_FAIL( "OPropertyBrowserController::actuatingPropertyChanged( ListCellRange ): caught an exception while resetting the string items!" );
173 break; // case PROPERTY_ID_LIST_CELL_RANGE
175 // ----- DataField -----
176 case PROPERTY_ID_CONTROLSOURCE:
178 OUString sControlSource;
179 _rNewValue >>= sControlSource;
180 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
181 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
183 break; // case PROPERTY_ID_CONTROLSOURCE
185 default:
186 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
189 for ( ::std::vector< PropertyId >::const_iterator loopAffected = aDependentProperties.begin();
190 loopAffected != aDependentProperties.end();
191 ++loopAffected
194 impl_updateDependentProperty_nothrow( *loopAffected, _rxInspectorUI );
199 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
203 switch ( _nPropId )
205 // ----- BoundColumn -----
206 case PROPERTY_ID_BOUNDCOLUMN:
208 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
209 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
210 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
212 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
213 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
215 break; // case PROPERTY_ID_BOUNDCOLUMN
217 } // switch
220 catch( const Exception& )
222 OSL_FAIL( "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow: caught an exception!" );
227 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
229 ::osl::MutexGuard aGuard( m_aMutex );
230 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
232 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::getPropertyValue: inconsistency!" );
233 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
235 Any aReturn;
236 switch ( nPropId )
238 case PROPERTY_ID_BOUND_CELL:
240 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
241 if ( !CellBindingHelper::isCellBinding( xBinding ) )
242 xBinding.clear();
244 aReturn <<= xBinding;
246 break;
248 case PROPERTY_ID_LIST_CELL_RANGE:
250 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
251 if ( !CellBindingHelper::isCellRangeListSource( xSource ) )
252 xSource.clear();
254 aReturn <<= xSource;
256 break;
258 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
260 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
261 aReturn <<= (sal_Int16)( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
263 break;
265 default:
266 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
267 break;
269 return aReturn;
273 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
275 ::osl::MutexGuard aGuard( m_aMutex );
276 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
278 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::setPropertyValue: inconsistency!" );
279 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
283 Any aOldValue = getPropertyValue( _rPropertyName );
285 switch ( nPropId )
287 case PROPERTY_ID_BOUND_CELL:
289 Reference< XValueBinding > xBinding;
290 _rValue >>= xBinding;
291 m_pHelper->setBinding( xBinding );
293 break;
295 case PROPERTY_ID_LIST_CELL_RANGE:
297 Reference< XListEntrySource > xSource;
298 _rValue >>= xSource;
299 m_pHelper->setListSource( xSource );
301 break;
303 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
305 sal_Int16 nExchangeType = 0;
306 OSL_VERIFY( _rValue >>= nExchangeType );
308 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
309 if ( xBinding.is() )
311 bool bNeedIntegerBinding = ( nExchangeType == 1 );
312 if ( (bool)bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
314 CellAddress aAddress;
315 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
317 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
318 m_pHelper->setBinding( xBinding );
323 break;
325 default:
326 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
327 break;
330 impl_setContextDocumentModified_nothrow();
332 Any aNewValue( getPropertyValue( _rPropertyName ) );
333 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
334 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
335 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
336 // and handle the listener notification itself
338 catch( const Exception& )
340 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: caught an exception!" );
345 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
347 ::osl::MutexGuard aGuard( m_aMutex );
348 Any aPropertyValue;
350 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!" );
351 if ( !m_pHelper.get() )
352 return aPropertyValue;
354 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
356 OUString sControlValue;
357 OSL_VERIFY( _rControlValue >>= sControlValue );
358 switch( nPropId )
360 case PROPERTY_ID_LIST_CELL_RANGE:
361 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
362 break;
364 case PROPERTY_ID_BOUND_CELL:
366 // if we have the possibility of an integer binding, then we must preserve
367 // this property's value (e.g. if the current binding is an integer binding, then
368 // the newly created one must be, too)
369 bool bIntegerBinding = false;
370 if ( m_pHelper->isCellIntegerBindingAllowed() )
372 sal_Int16 nCurrentBindingType = 0;
373 getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
374 bIntegerBinding = ( nCurrentBindingType != 0 );
376 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
378 break;
380 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
381 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
382 break;
384 default:
385 OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
386 break;
389 return aPropertyValue;
393 Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const OUString& _rPropertyName,
394 const Any& _rPropertyValue, const Type& /*_rControlValueType*/ ) throw (UnknownPropertyException, RuntimeException, std::exception)
396 ::osl::MutexGuard aGuard( m_aMutex );
397 Any aControlValue;
399 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!" );
400 if ( !m_pHelper.get() )
401 return aControlValue;
403 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
405 switch ( nPropId )
407 case PROPERTY_ID_BOUND_CELL:
409 Reference< XValueBinding > xBinding;
410 #if OSL_DEBUG_LEVEL > 0
411 bool bSuccess =
412 #endif
413 _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 #if OSL_DEBUG_LEVEL > 0
425 bool bSuccess =
426 #endif
427 _rPropertyValue >>= xSource;
428 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
430 // the only value binding we support so far is linking to spreadsheet cells
431 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
433 break;
435 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
436 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
437 break;
439 default:
440 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
441 break;
444 return aControlValue;
448 Sequence< Property > SAL_CALL CellBindingPropertyHandler::doDescribeSupportedProperties() const
450 ::std::vector< Property > aProperties;
452 bool bAllowCellLinking = m_pHelper.get() && m_pHelper->isCellBindingAllowed();
453 bool bAllowCellIntLinking = m_pHelper.get() && m_pHelper->isCellIntegerBindingAllowed();
454 bool bAllowListCellRange = m_pHelper.get() && m_pHelper->isListCellRangeAllowed();
455 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
457 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
458 + ( bAllowListCellRange ? 1 : 0 )
459 + ( bAllowCellIntLinking ? 1 : 0 );
460 aProperties.resize( nPos );
462 if ( bAllowCellLinking )
464 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
465 ::cppu::UnoType<OUString>::get(), 0 );
467 if ( bAllowCellIntLinking )
469 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
470 ::cppu::UnoType<sal_Int16>::get(), 0 );
472 if ( bAllowListCellRange )
474 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
475 ::cppu::UnoType<OUString>::get(), 0 );
479 if ( aProperties.empty() )
480 return Sequence< Property >();
481 return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
485 } // namespace pcr
488 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */