1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "cellbindinghandler.hxx"
31 #include "formstrings.hxx"
32 #include "formmetadata.hxx"
33 #include "cellbindinghelper.hxx"
34 
35 /** === begin UNO includes === **/
36 #include <com/sun/star/form/binding/XValueBinding.hpp>
37 #include <com/sun/star/table/CellAddress.hpp>
38 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
39 /** === end UNO includes === **/
40 #include <tools/debug.hxx>
41 
42 //------------------------------------------------------------------------
43 extern "C" void SAL_CALL createRegistryInfo_CellBindingPropertyHandler()
44 {
45     ::pcr::CellBindingPropertyHandler::registerImplementation();
46 }
47 
48 //........................................................................
49 namespace pcr
50 {
51 //........................................................................
52 
53     using namespace ::com::sun::star::uno;
54     using namespace ::com::sun::star::table;
55     using namespace ::com::sun::star::lang;
56     using namespace ::com::sun::star::beans;
57     using namespace ::com::sun::star::script;
58     using namespace ::com::sun::star::frame;
59     using namespace ::com::sun::star::inspection;
60     using namespace ::com::sun::star::form::binding;
61     using namespace ::comphelper;
62 
63 	//====================================================================
64 	//= CellBindingPropertyHandler
65 	//====================================================================
66     DBG_NAME( CellBindingPropertyHandler )
67 	//--------------------------------------------------------------------
68     CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
69         :CellBindingPropertyHandler_Base( _rxContext )
70         ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::getCppuType( static_cast< sal_Int16* >( NULL ) ), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
71     {
72         DBG_CTOR( CellBindingPropertyHandler, NULL );
73     }
74 
75     //--------------------------------------------------------------------
76     ::rtl::OUString SAL_CALL CellBindingPropertyHandler::getImplementationName_static(  ) throw (RuntimeException)
77     {
78         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.CellBindingPropertyHandler" ) );
79     }
80 
81     //--------------------------------------------------------------------
82     Sequence< ::rtl::OUString > SAL_CALL CellBindingPropertyHandler::getSupportedServiceNames_static(  ) throw (RuntimeException)
83     {
84         Sequence< ::rtl::OUString > aSupported( 1 );
85         aSupported[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.inspection.CellBindingPropertyHandler" ) );
86         return aSupported;
87     }
88 
89     //--------------------------------------------------------------------
90     void CellBindingPropertyHandler::onNewComponent()
91     {
92         PropertyHandlerComponent::onNewComponent();
93 
94         Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
95         DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
96         if ( CellBindingHelper::isSpreadsheetDocument( xDocument ) )
97             m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
98     }
99 
100 	//--------------------------------------------------------------------
101     CellBindingPropertyHandler::~CellBindingPropertyHandler( )
102     {
103         DBG_DTOR( CellBindingPropertyHandler, NULL );
104     }
105 
106     //--------------------------------------------------------------------
107     Sequence< ::rtl::OUString > SAL_CALL CellBindingPropertyHandler::getActuatingProperties( ) throw (RuntimeException)
108     {
109         Sequence< ::rtl::OUString > aInterestingProperties( 3 );
110         aInterestingProperties[0] = PROPERTY_LIST_CELL_RANGE;
111         aInterestingProperties[1] = PROPERTY_BOUND_CELL;
112         aInterestingProperties[2] = PROPERTY_CONTROLSOURCE;
113         return aInterestingProperties;
114     }
115 
116 	//--------------------------------------------------------------------
117     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     {
119         ::osl::MutexGuard aGuard( m_aMutex );
120         PropertyId nActuatingPropId( impl_getPropertyId_throw( _rActuatingPropertyName ) );
121         OSL_PRECOND( m_pHelper.get(), "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistentcy!" );
122             // if we survived impl_getPropertyId_throw, we should have a helper, since no helper implies no properties
123 
124         OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
125         if ( !_rxInspectorUI.is() )
126             throw NullPointerException();
127 
128         ::std::vector< PropertyId > aDependentProperties;
129 
130         switch ( nActuatingPropId )
131         {
132         // ----- BoundCell -----
133         case PROPERTY_ID_BOUND_CELL:
134         {
135             // the SQL-data-binding related properties need to be enabled if and only if
136             // there is *no* valid cell binding
137             Reference< XValueBinding > xBinding;
138             _rNewValue >>= xBinding;
139 
140             if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
141                 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
142             if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
143                 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
144 
145             if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
146                 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
147             if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
148                 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
149 
150             aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
151 
152             if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
153             {
154                 // ensure that the "transfer selection as" property is reset. Since we can't remember
155                 // it at the object itself, but derive it from the binding only, we have to normalize
156                 // it now that there *is* no binding anymore.
157                 setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, makeAny( (sal_Int16) 0 ) );
158             }
159         }
160         break;
161 
162         // ----- CellRange -----
163         case PROPERTY_ID_LIST_CELL_RANGE:
164         {
165             // the list source related properties need to be enabled if and only if
166             // there is *no* valid external list source for the control
167             Reference< XListEntrySource > xSource;
168             _rNewValue >>= xSource;
169 
170             _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
171             _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
172             _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
173 
174             aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
175 
176             // also reset the list entries if the cell range is reset
177             // #i28319# - 2004-04-27 - fs@openoffice.org
178             if ( !_bFirstTimeInit )
179             {
180                 try
181                 {
182                     if ( !xSource.is() )
183                         setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( Sequence< ::rtl::OUString >() ) );
184                 }
185                 catch( const Exception& )
186                 {
187             	    OSL_ENSURE( sal_False, "OPropertyBrowserController::actuatingPropertyChanged( ListCellRange ): caught an exception while resetting the string items!" );
188                 }
189             }
190         }
191         break;  // case PROPERTY_ID_LIST_CELL_RANGE
192 
193         // ----- DataField -----
194         case PROPERTY_ID_CONTROLSOURCE:
195         {
196             ::rtl::OUString sControlSource;
197             _rNewValue >>= sControlSource;
198             if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
199                 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.getLength() == 0 );
200         }
201         break;  // case PROPERTY_ID_CONTROLSOURCE
202 
203         default:
204             DBG_ERROR( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
205         }
206 
207         for ( ::std::vector< PropertyId >::const_iterator loopAffected = aDependentProperties.begin();
208               loopAffected != aDependentProperties.end();
209               ++loopAffected
210             )
211         {
212             impl_updateDependentProperty_nothrow( *loopAffected, _rxInspectorUI );
213         }
214     }
215 
216 	//--------------------------------------------------------------------
217     void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
218     {
219         try
220         {
221             switch ( _nPropId )
222             {
223             // ----- BoundColumn -----
224             case PROPERTY_ID_BOUNDCOLUMN:
225             {
226                 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
227                 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
228                 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
229 
230                 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
231                     _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
232             }
233             break;  // case PROPERTY_ID_BOUNDCOLUMN
234 
235             }   // switch
236 
237         }
238         catch( const Exception& )
239         {
240         	OSL_ENSURE( sal_False, "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow: caught an exception!" );
241         }
242     }
243 
244 	//--------------------------------------------------------------------
245     Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
246     {
247         ::osl::MutexGuard aGuard( m_aMutex );
248         PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
249 
250         OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::getPropertyValue: inconsistency!" );
251             // if we survived impl_getPropertyId_throw, we should have a helper, since no helper implies no properties
252 
253         Any aReturn;
254         switch ( nPropId )
255         {
256         case PROPERTY_ID_BOUND_CELL:
257         {
258             Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
259             if ( !m_pHelper->isCellBinding( xBinding ) )
260                 xBinding.clear();
261 
262             aReturn <<= xBinding;
263         }
264         break;
265 
266         case PROPERTY_ID_LIST_CELL_RANGE:
267         {
268             Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
269             if ( !m_pHelper->isCellRangeListSource( xSource ) )
270                 xSource.clear();
271 
272             aReturn <<= xSource;
273         }
274         break;
275 
276         case PROPERTY_ID_CELL_EXCHANGE_TYPE:
277         {
278             Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
279             aReturn <<= (sal_Int16)( m_pHelper->isCellIntegerBinding( xBinding ) ? 1 : 0 );
280         }
281         break;
282 
283         default:
284             DBG_ERROR( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
285             break;
286         }
287         return aReturn;
288     }
289 
290     //--------------------------------------------------------------------
291     void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException)
292     {
293         ::osl::MutexGuard aGuard( m_aMutex );
294         PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
295 
296         OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::setPropertyValue: inconsistency!" );
297             // if we survived impl_getPropertyId_throw, we should have a helper, since no helper implies no properties
298 
299         try
300         {
301             Any aOldValue = getPropertyValue( _rPropertyName );
302 
303             switch ( nPropId )
304             {
305             case PROPERTY_ID_BOUND_CELL:
306             {
307                 Reference< XValueBinding > xBinding;
308                 _rValue >>= xBinding;
309                 m_pHelper->setBinding( xBinding );
310             }
311             break;
312 
313             case PROPERTY_ID_LIST_CELL_RANGE:
314             {
315                 Reference< XListEntrySource > xSource;
316                 _rValue >>= xSource;
317                 m_pHelper->setListSource( xSource );
318             }
319             break;
320 
321             case PROPERTY_ID_CELL_EXCHANGE_TYPE:
322             {
323                 sal_Int16 nExchangeType = 0;
324                 OSL_VERIFY( _rValue >>= nExchangeType );
325 
326                 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
327                 if ( xBinding.is() )
328                 {
329                     sal_Bool bNeedIntegerBinding = ( nExchangeType == 1 );
330                     if ( (bool)bNeedIntegerBinding != m_pHelper->isCellIntegerBinding( xBinding ) )
331                     {
332                         CellAddress aAddress;
333                         if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
334                         {
335                             xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
336                             m_pHelper->setBinding( xBinding );
337                         }
338                     }
339                 }
340             }
341             break;
342 
343             default:
344                 DBG_ERROR( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
345                 break;
346             }
347 
348             impl_setContextDocumentModified_nothrow();
349 
350             Any aNewValue( getPropertyValue( _rPropertyName ) );
351             firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
352             // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
353             // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
354             // and handle the listener notification itself
355         }
356         catch( const Exception& )
357         {
358         	OSL_ENSURE( sal_False, "CellBindingPropertyHandler::setPropertyValue: caught an exception!" );
359         }
360     }
361 
362     //--------------------------------------------------------------------
363     Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
364     {
365         ::osl::MutexGuard aGuard( m_aMutex );
366         Any aPropertyValue;
367 
368         OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!" );
369         if ( !m_pHelper.get() )
370             return aPropertyValue;
371 
372         PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
373 
374         ::rtl::OUString sControlValue;
375         OSL_VERIFY( _rControlValue >>= sControlValue );
376 		switch( nPropId )
377 		{
378             case PROPERTY_ID_LIST_CELL_RANGE:
379                 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
380                 break;
381 
382 			case PROPERTY_ID_BOUND_CELL:
383             {
384                 // if we have the possibility of an integer binding, then we must preserve
385                 // this property's value (e.g. if the current binding is an integer binding, then
386                 // the newly created one must be, too)
387                 bool bIntegerBinding = false;
388                 if ( m_pHelper->isCellIntegerBindingAllowed() )
389                 {
390                     sal_Int16 nCurrentBindingType = 0;
391                     getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
392                     bIntegerBinding = ( nCurrentBindingType != 0 );
393                 }
394                 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
395             }
396             break;
397 
398             case PROPERTY_ID_CELL_EXCHANGE_TYPE:
399                 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
400                 break;
401 
402             default:
403                 DBG_ERROR( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
404                 break;
405         }
406 
407         return aPropertyValue;
408     }
409 
410     //--------------------------------------------------------------------
411     Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const ::rtl::OUString& _rPropertyName,
412         const Any& _rPropertyValue, const Type& /*_rControlValueType*/ ) throw (UnknownPropertyException, RuntimeException)
413     {
414         ::osl::MutexGuard aGuard( m_aMutex );
415         Any aControlValue;
416 
417         OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!" );
418         if ( !m_pHelper.get() )
419             return aControlValue;
420 
421         PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
422 
423 		switch ( nPropId )
424 		{
425             case PROPERTY_ID_BOUND_CELL:
426             {
427                 Reference< XValueBinding > xBinding;
428 #if OSL_DEBUG_LEVEL > 0
429                 sal_Bool bSuccess =
430 #endif
431                 _rPropertyValue >>= xBinding;
432                 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
433 
434                 // the only value binding we support so far is linking to spreadsheet cells
435                 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
436             }
437             break;
438 
439             case PROPERTY_ID_LIST_CELL_RANGE:
440             {
441                 Reference< XListEntrySource > xSource;
442 #if OSL_DEBUG_LEVEL > 0
443                 sal_Bool bSuccess =
444 #endif
445                 _rPropertyValue >>= xSource;
446                 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
447 
448                 // the only value binding we support so far is linking to spreadsheet cells
449                 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
450             }
451             break;
452 
453             case PROPERTY_ID_CELL_EXCHANGE_TYPE:
454                 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
455                 break;
456 
457             default:
458                 DBG_ERROR( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
459                 break;
460 		}
461 
462         return aControlValue;
463     }
464 
465     //--------------------------------------------------------------------
466     Sequence< Property > SAL_CALL CellBindingPropertyHandler::doDescribeSupportedProperties() const
467     {
468         ::std::vector< Property > aProperties;
469 
470         bool bAllowCellLinking      = m_pHelper.get() && m_pHelper->isCellBindingAllowed();
471         bool bAllowCellIntLinking   = m_pHelper.get() && m_pHelper->isCellIntegerBindingAllowed();
472         bool bAllowListCellRange    = m_pHelper.get() && m_pHelper->isListCellRangeAllowed();
473         if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
474         {
475             sal_Int32 nPos =  ( bAllowCellLinking    ? 1 : 0 )
476                             + ( bAllowListCellRange  ? 1 : 0 )
477                             + ( bAllowCellIntLinking ? 1 : 0 );
478             aProperties.resize( nPos );
479 
480             if ( bAllowCellLinking )
481             {
482                 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
483                     ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ), 0 );
484             }
485             if ( bAllowCellIntLinking )
486             {
487                 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
488                     ::getCppuType( static_cast< sal_Int16* >( NULL ) ), 0 );
489             }
490             if ( bAllowListCellRange )
491             {
492                 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
493                     ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ), 0 );
494             }
495         }
496 
497         if ( aProperties.empty() )
498             return Sequence< Property >();
499         return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
500     }
501 
502 //........................................................................
503 }   // namespace pcr
504 //........................................................................
505