Bump for 3.6-28
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghandler.cxx
blobd58c8ae5fa3ed4e0ed2d5ef846b56c23fac4bdec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "cellbindinghandler.hxx"
30 #include "formstrings.hxx"
31 #include "formmetadata.hxx"
32 #include "cellbindinghelper.hxx"
34 /** === begin UNO includes === **/
35 #include <com/sun/star/form/binding/XValueBinding.hpp>
36 #include <com/sun/star/table/CellAddress.hpp>
37 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
38 /** === end UNO includes === **/
39 #include <tools/debug.hxx>
41 //------------------------------------------------------------------------
42 extern "C" void SAL_CALL createRegistryInfo_CellBindingPropertyHandler()
44 ::pcr::CellBindingPropertyHandler::registerImplementation();
47 //........................................................................
48 namespace pcr
50 //........................................................................
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::table;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::script;
57 using namespace ::com::sun::star::frame;
58 using namespace ::com::sun::star::inspection;
59 using namespace ::com::sun::star::form::binding;
60 using namespace ::comphelper;
62 //====================================================================
63 //= CellBindingPropertyHandler
64 //====================================================================
65 DBG_NAME( CellBindingPropertyHandler )
66 //--------------------------------------------------------------------
67 CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
68 :CellBindingPropertyHandler_Base( _rxContext )
69 ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::getCppuType( static_cast< sal_Int16* >( NULL ) ), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
71 DBG_CTOR( CellBindingPropertyHandler, NULL );
74 //--------------------------------------------------------------------
75 ::rtl::OUString SAL_CALL CellBindingPropertyHandler::getImplementationName_static( ) throw (RuntimeException)
77 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.CellBindingPropertyHandler" ) );
80 //--------------------------------------------------------------------
81 Sequence< ::rtl::OUString > SAL_CALL CellBindingPropertyHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
83 Sequence< ::rtl::OUString > aSupported( 1 );
84 aSupported[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.inspection.CellBindingPropertyHandler" ) );
85 return aSupported;
88 //--------------------------------------------------------------------
89 void CellBindingPropertyHandler::onNewComponent()
91 PropertyHandlerComponent::onNewComponent();
93 Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
94 DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
95 if ( CellBindingHelper::isSpreadsheetDocument( xDocument ) )
96 m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
99 //--------------------------------------------------------------------
100 CellBindingPropertyHandler::~CellBindingPropertyHandler( )
102 DBG_DTOR( CellBindingPropertyHandler, NULL );
105 //--------------------------------------------------------------------
106 Sequence< ::rtl::OUString > SAL_CALL CellBindingPropertyHandler::getActuatingProperties( ) throw (RuntimeException)
108 Sequence< ::rtl::OUString > aInterestingProperties( 3 );
109 aInterestingProperties[0] = PROPERTY_LIST_CELL_RANGE;
110 aInterestingProperties[1] = PROPERTY_BOUND_CELL;
111 aInterestingProperties[2] = PROPERTY_CONTROLSOURCE;
112 return aInterestingProperties;
115 //--------------------------------------------------------------------
116 void SAL_CALL CellBindingPropertyHandler::actuatingPropertyChanged( const ::rtl::OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit ) throw (NullPointerException, RuntimeException)
118 ::osl::MutexGuard aGuard( m_aMutex );
119 PropertyId nActuatingPropId( impl_getPropertyId_throw( _rActuatingPropertyName ) );
120 OSL_PRECOND( m_pHelper.get(), "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistentcy!" );
121 // if we survived impl_getPropertyId_throw, we should have a helper, since no helper implies no properties
123 OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
124 if ( !_rxInspectorUI.is() )
125 throw NullPointerException();
127 ::std::vector< PropertyId > aDependentProperties;
129 switch ( nActuatingPropId )
131 // ----- BoundCell -----
132 case PROPERTY_ID_BOUND_CELL:
134 // the SQL-data-binding related properties need to be enabled if and only if
135 // there is *no* valid cell binding
136 Reference< XValueBinding > xBinding;
137 _rNewValue >>= xBinding;
139 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
140 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
141 if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
142 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
144 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
145 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
146 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
147 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
149 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
151 if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
153 // ensure that the "transfer selection as" property is reset. Since we can't remember
154 // it at the object itself, but derive it from the binding only, we have to normalize
155 // it now that there *is* no binding anymore.
156 setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, makeAny( (sal_Int16) 0 ) );
159 break;
161 // ----- CellRange -----
162 case PROPERTY_ID_LIST_CELL_RANGE:
164 // the list source related properties need to be enabled if and only if
165 // there is *no* valid external list source for the control
166 Reference< XListEntrySource > xSource;
167 _rNewValue >>= xSource;
169 _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
170 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
171 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
173 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
175 // also reset the list entries if the cell range is reset
176 // #i28319#
177 if ( !_bFirstTimeInit )
181 if ( !xSource.is() )
182 setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( Sequence< ::rtl::OUString >() ) );
184 catch( const Exception& )
186 OSL_FAIL( "OPropertyBrowserController::actuatingPropertyChanged( ListCellRange ): caught an exception while resetting the string items!" );
190 break; // case PROPERTY_ID_LIST_CELL_RANGE
192 // ----- DataField -----
193 case PROPERTY_ID_CONTROLSOURCE:
195 ::rtl::OUString sControlSource;
196 _rNewValue >>= sControlSource;
197 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
198 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
200 break; // case PROPERTY_ID_CONTROLSOURCE
202 default:
203 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
206 for ( ::std::vector< PropertyId >::const_iterator loopAffected = aDependentProperties.begin();
207 loopAffected != aDependentProperties.end();
208 ++loopAffected
211 impl_updateDependentProperty_nothrow( *loopAffected, _rxInspectorUI );
215 //--------------------------------------------------------------------
216 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
220 switch ( _nPropId )
222 // ----- BoundColumn -----
223 case PROPERTY_ID_BOUNDCOLUMN:
225 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
226 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
227 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
229 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
230 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
232 break; // case PROPERTY_ID_BOUNDCOLUMN
234 } // switch
237 catch( const Exception& )
239 OSL_FAIL( "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow: caught an exception!" );
243 //--------------------------------------------------------------------
244 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
246 ::osl::MutexGuard aGuard( m_aMutex );
247 PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
249 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::getPropertyValue: inconsistency!" );
250 // if we survived impl_getPropertyId_throw, we should have a helper, since no helper implies no properties
252 Any aReturn;
253 switch ( nPropId )
255 case PROPERTY_ID_BOUND_CELL:
257 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
258 if ( !m_pHelper->isCellBinding( xBinding ) )
259 xBinding.clear();
261 aReturn <<= xBinding;
263 break;
265 case PROPERTY_ID_LIST_CELL_RANGE:
267 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
268 if ( !m_pHelper->isCellRangeListSource( xSource ) )
269 xSource.clear();
271 aReturn <<= xSource;
273 break;
275 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
277 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
278 aReturn <<= (sal_Int16)( m_pHelper->isCellIntegerBinding( xBinding ) ? 1 : 0 );
280 break;
282 default:
283 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
284 break;
286 return aReturn;
289 //--------------------------------------------------------------------
290 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException)
292 ::osl::MutexGuard aGuard( m_aMutex );
293 PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
295 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::setPropertyValue: inconsistency!" );
296 // if we survived impl_getPropertyId_throw, we should have a helper, since no helper implies no properties
300 Any aOldValue = getPropertyValue( _rPropertyName );
302 switch ( nPropId )
304 case PROPERTY_ID_BOUND_CELL:
306 Reference< XValueBinding > xBinding;
307 _rValue >>= xBinding;
308 m_pHelper->setBinding( xBinding );
310 break;
312 case PROPERTY_ID_LIST_CELL_RANGE:
314 Reference< XListEntrySource > xSource;
315 _rValue >>= xSource;
316 m_pHelper->setListSource( xSource );
318 break;
320 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
322 sal_Int16 nExchangeType = 0;
323 OSL_VERIFY( _rValue >>= nExchangeType );
325 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
326 if ( xBinding.is() )
328 sal_Bool bNeedIntegerBinding = ( nExchangeType == 1 );
329 if ( (bool)bNeedIntegerBinding != m_pHelper->isCellIntegerBinding( xBinding ) )
331 CellAddress aAddress;
332 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
334 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
335 m_pHelper->setBinding( xBinding );
340 break;
342 default:
343 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
344 break;
347 impl_setContextDocumentModified_nothrow();
349 Any aNewValue( getPropertyValue( _rPropertyName ) );
350 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
351 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
352 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
353 // and handle the listener notification itself
355 catch( const Exception& )
357 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: caught an exception!" );
361 //--------------------------------------------------------------------
362 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
364 ::osl::MutexGuard aGuard( m_aMutex );
365 Any aPropertyValue;
367 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!" );
368 if ( !m_pHelper.get() )
369 return aPropertyValue;
371 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
373 ::rtl::OUString sControlValue;
374 OSL_VERIFY( _rControlValue >>= sControlValue );
375 switch( nPropId )
377 case PROPERTY_ID_LIST_CELL_RANGE:
378 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
379 break;
381 case PROPERTY_ID_BOUND_CELL:
383 // if we have the possibility of an integer binding, then we must preserve
384 // this property's value (e.g. if the current binding is an integer binding, then
385 // the newly created one must be, too)
386 bool bIntegerBinding = false;
387 if ( m_pHelper->isCellIntegerBindingAllowed() )
389 sal_Int16 nCurrentBindingType = 0;
390 getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
391 bIntegerBinding = ( nCurrentBindingType != 0 );
393 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
395 break;
397 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
398 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
399 break;
401 default:
402 OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
403 break;
406 return aPropertyValue;
409 //--------------------------------------------------------------------
410 Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const ::rtl::OUString& _rPropertyName,
411 const Any& _rPropertyValue, const Type& /*_rControlValueType*/ ) throw (UnknownPropertyException, RuntimeException)
413 ::osl::MutexGuard aGuard( m_aMutex );
414 Any aControlValue;
416 OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!" );
417 if ( !m_pHelper.get() )
418 return aControlValue;
420 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
422 switch ( nPropId )
424 case PROPERTY_ID_BOUND_CELL:
426 Reference< XValueBinding > xBinding;
427 #if OSL_DEBUG_LEVEL > 0
428 sal_Bool bSuccess =
429 #endif
430 _rPropertyValue >>= xBinding;
431 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
433 // the only value binding we support so far is linking to spreadsheet cells
434 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
436 break;
438 case PROPERTY_ID_LIST_CELL_RANGE:
440 Reference< XListEntrySource > xSource;
441 #if OSL_DEBUG_LEVEL > 0
442 sal_Bool bSuccess =
443 #endif
444 _rPropertyValue >>= xSource;
445 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
447 // the only value binding we support so far is linking to spreadsheet cells
448 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
450 break;
452 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
453 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
454 break;
456 default:
457 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
458 break;
461 return aControlValue;
464 //--------------------------------------------------------------------
465 Sequence< Property > SAL_CALL CellBindingPropertyHandler::doDescribeSupportedProperties() const
467 ::std::vector< Property > aProperties;
469 bool bAllowCellLinking = m_pHelper.get() && m_pHelper->isCellBindingAllowed();
470 bool bAllowCellIntLinking = m_pHelper.get() && m_pHelper->isCellIntegerBindingAllowed();
471 bool bAllowListCellRange = m_pHelper.get() && m_pHelper->isListCellRangeAllowed();
472 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
474 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
475 + ( bAllowListCellRange ? 1 : 0 )
476 + ( bAllowCellIntLinking ? 1 : 0 );
477 aProperties.resize( nPos );
479 if ( bAllowCellLinking )
481 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
482 ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ), 0 );
484 if ( bAllowCellIntLinking )
486 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
487 ::getCppuType( static_cast< sal_Int16* >( NULL ) ), 0 );
489 if ( bAllowListCellRange )
491 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
492 ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ), 0 );
496 if ( aProperties.empty() )
497 return Sequence< Property >();
498 return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
501 //........................................................................
502 } // namespace pcr
503 //........................................................................
505 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */