Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "cellbindinghandler.hxx"
21 : #include "formstrings.hxx"
22 : #include "formmetadata.hxx"
23 : #include "cellbindinghelper.hxx"
24 : #include "pcrservices.hxx"
25 :
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>
30 :
31 :
32 2 : extern "C" void SAL_CALL createRegistryInfo_CellBindingPropertyHandler()
33 : {
34 2 : ::pcr::CellBindingPropertyHandler::registerImplementation();
35 2 : }
36 :
37 :
38 : namespace pcr
39 : {
40 :
41 :
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;
51 :
52 1 : CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
53 : :CellBindingPropertyHandler_Base( _rxContext )
54 1 : ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<sal_Int16>::get(), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
55 : {
56 1 : }
57 :
58 :
59 3 : OUString SAL_CALL CellBindingPropertyHandler::getImplementationName_static( ) throw (RuntimeException)
60 : {
61 3 : return OUString( "com.sun.star.comp.extensions.CellBindingPropertyHandler" );
62 : }
63 :
64 :
65 3 : Sequence< OUString > SAL_CALL CellBindingPropertyHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
66 : {
67 3 : Sequence< OUString > aSupported( 1 );
68 3 : aSupported[0] = "com.sun.star.form.inspection.CellBindingPropertyHandler";
69 3 : return aSupported;
70 : }
71 :
72 :
73 0 : void CellBindingPropertyHandler::onNewComponent()
74 : {
75 0 : PropertyHandlerComponent::onNewComponent();
76 :
77 0 : Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
78 : DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
79 0 : if ( CellBindingHelper::isSpreadsheetDocument( xDocument ) )
80 0 : m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
81 0 : }
82 :
83 :
84 2 : CellBindingPropertyHandler::~CellBindingPropertyHandler( )
85 : {
86 2 : }
87 :
88 :
89 0 : Sequence< OUString > SAL_CALL CellBindingPropertyHandler::getActuatingProperties( ) throw (RuntimeException, std::exception)
90 : {
91 0 : Sequence< OUString > aInterestingProperties( 3 );
92 0 : aInterestingProperties[0] = PROPERTY_LIST_CELL_RANGE;
93 0 : aInterestingProperties[1] = PROPERTY_BOUND_CELL;
94 0 : aInterestingProperties[2] = PROPERTY_CONTROLSOURCE;
95 0 : return aInterestingProperties;
96 : }
97 :
98 :
99 0 : 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)
100 : {
101 0 : ::osl::MutexGuard aGuard( m_aMutex );
102 0 : 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
105 :
106 : OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
107 0 : if ( !_rxInspectorUI.is() )
108 0 : throw NullPointerException();
109 :
110 0 : ::std::vector< PropertyId > aDependentProperties;
111 :
112 0 : switch ( nActuatingPropId )
113 : {
114 : // ----- BoundCell -----
115 : case PROPERTY_ID_BOUND_CELL:
116 : {
117 : // the SQL-data-binding related properties need to be enabled if and only if
118 : // there is *no* valid cell binding
119 0 : Reference< XValueBinding > xBinding;
120 0 : _rNewValue >>= xBinding;
121 :
122 0 : if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
123 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
124 0 : if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
125 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
126 :
127 0 : if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
128 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
129 0 : if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
130 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
131 :
132 0 : aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
133 :
134 0 : if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
135 : {
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 0 : setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, makeAny( (sal_Int16) 0 ) );
140 0 : }
141 : }
142 0 : break;
143 :
144 : // ----- CellRange -----
145 : case PROPERTY_ID_LIST_CELL_RANGE:
146 : {
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 0 : Reference< XListEntrySource > xSource;
150 0 : _rNewValue >>= xSource;
151 :
152 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
153 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
154 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
155 :
156 0 : aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
157 :
158 : // also reset the list entries if the cell range is reset
159 : // #i28319#
160 0 : if ( !_bFirstTimeInit )
161 : {
162 : try
163 : {
164 0 : if ( !xSource.is() )
165 0 : setPropertyValue( PROPERTY_STRINGITEMLIST, makeAny( Sequence< OUString >() ) );
166 : }
167 0 : catch( const Exception& )
168 : {
169 : OSL_FAIL( "OPropertyBrowserController::actuatingPropertyChanged( ListCellRange ): caught an exception while resetting the string items!" );
170 : }
171 0 : }
172 : }
173 0 : break; // case PROPERTY_ID_LIST_CELL_RANGE
174 :
175 : // ----- DataField -----
176 : case PROPERTY_ID_CONTROLSOURCE:
177 : {
178 0 : OUString sControlSource;
179 0 : _rNewValue >>= sControlSource;
180 0 : if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
181 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
182 : }
183 0 : break; // case PROPERTY_ID_CONTROLSOURCE
184 :
185 : default:
186 : OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
187 : }
188 :
189 0 : for ( ::std::vector< PropertyId >::const_iterator loopAffected = aDependentProperties.begin();
190 0 : loopAffected != aDependentProperties.end();
191 : ++loopAffected
192 : )
193 : {
194 0 : impl_updateDependentProperty_nothrow( *loopAffected, _rxInspectorUI );
195 0 : }
196 0 : }
197 :
198 :
199 0 : void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
200 : {
201 : try
202 : {
203 0 : switch ( _nPropId )
204 : {
205 : // ----- BoundColumn -----
206 : case PROPERTY_ID_BOUNDCOLUMN:
207 : {
208 0 : CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
209 0 : Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
210 0 : Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
211 :
212 0 : if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
213 0 : _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
214 : }
215 0 : break; // case PROPERTY_ID_BOUNDCOLUMN
216 :
217 : } // switch
218 :
219 : }
220 0 : catch( const Exception& )
221 : {
222 : OSL_FAIL( "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow: caught an exception!" );
223 : }
224 0 : }
225 :
226 :
227 0 : Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
228 : {
229 0 : ::osl::MutexGuard aGuard( m_aMutex );
230 0 : PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
231 :
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
234 :
235 0 : Any aReturn;
236 0 : switch ( nPropId )
237 : {
238 : case PROPERTY_ID_BOUND_CELL:
239 : {
240 0 : Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
241 0 : if ( !CellBindingHelper::isCellBinding( xBinding ) )
242 0 : xBinding.clear();
243 :
244 0 : aReturn <<= xBinding;
245 : }
246 0 : break;
247 :
248 : case PROPERTY_ID_LIST_CELL_RANGE:
249 : {
250 0 : Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
251 0 : if ( !CellBindingHelper::isCellRangeListSource( xSource ) )
252 0 : xSource.clear();
253 :
254 0 : aReturn <<= xSource;
255 : }
256 0 : break;
257 :
258 : case PROPERTY_ID_CELL_EXCHANGE_TYPE:
259 : {
260 0 : Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
261 0 : aReturn <<= (sal_Int16)( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
262 : }
263 0 : break;
264 :
265 : default:
266 : OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
267 0 : break;
268 : }
269 0 : return aReturn;
270 : }
271 :
272 :
273 0 : void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
274 : {
275 0 : ::osl::MutexGuard aGuard( m_aMutex );
276 0 : PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
277 :
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
280 :
281 : try
282 : {
283 0 : Any aOldValue = getPropertyValue( _rPropertyName );
284 :
285 0 : switch ( nPropId )
286 : {
287 : case PROPERTY_ID_BOUND_CELL:
288 : {
289 0 : Reference< XValueBinding > xBinding;
290 0 : _rValue >>= xBinding;
291 0 : m_pHelper->setBinding( xBinding );
292 : }
293 0 : break;
294 :
295 : case PROPERTY_ID_LIST_CELL_RANGE:
296 : {
297 0 : Reference< XListEntrySource > xSource;
298 0 : _rValue >>= xSource;
299 0 : m_pHelper->setListSource( xSource );
300 : }
301 0 : break;
302 :
303 : case PROPERTY_ID_CELL_EXCHANGE_TYPE:
304 : {
305 0 : sal_Int16 nExchangeType = 0;
306 0 : OSL_VERIFY( _rValue >>= nExchangeType );
307 :
308 0 : Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
309 0 : if ( xBinding.is() )
310 : {
311 0 : bool bNeedIntegerBinding = ( nExchangeType == 1 );
312 0 : if ( (bool)bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
313 : {
314 0 : CellAddress aAddress;
315 0 : if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
316 : {
317 0 : xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
318 0 : m_pHelper->setBinding( xBinding );
319 : }
320 : }
321 0 : }
322 : }
323 0 : break;
324 :
325 : default:
326 : OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
327 0 : break;
328 : }
329 :
330 0 : impl_setContextDocumentModified_nothrow();
331 :
332 0 : Any aNewValue( getPropertyValue( _rPropertyName ) );
333 0 : 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
337 : }
338 0 : catch( const Exception& )
339 : {
340 : OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: caught an exception!" );
341 0 : }
342 0 : }
343 :
344 :
345 0 : Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
346 : {
347 0 : ::osl::MutexGuard aGuard( m_aMutex );
348 0 : Any aPropertyValue;
349 :
350 : OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!" );
351 0 : if ( !m_pHelper.get() )
352 0 : return aPropertyValue;
353 :
354 0 : PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
355 :
356 0 : OUString sControlValue;
357 0 : OSL_VERIFY( _rControlValue >>= sControlValue );
358 0 : switch( nPropId )
359 : {
360 : case PROPERTY_ID_LIST_CELL_RANGE:
361 0 : aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
362 0 : break;
363 :
364 : case PROPERTY_ID_BOUND_CELL:
365 : {
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 0 : bool bIntegerBinding = false;
370 0 : if ( m_pHelper->isCellIntegerBindingAllowed() )
371 : {
372 0 : sal_Int16 nCurrentBindingType = 0;
373 0 : getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
374 0 : bIntegerBinding = ( nCurrentBindingType != 0 );
375 : }
376 0 : aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
377 : }
378 0 : break;
379 :
380 : case PROPERTY_ID_CELL_EXCHANGE_TYPE:
381 0 : m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
382 0 : break;
383 :
384 : default:
385 : OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
386 0 : break;
387 : }
388 :
389 0 : return aPropertyValue;
390 : }
391 :
392 :
393 0 : Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const OUString& _rPropertyName,
394 : const Any& _rPropertyValue, const Type& /*_rControlValueType*/ ) throw (UnknownPropertyException, RuntimeException, std::exception)
395 : {
396 0 : ::osl::MutexGuard aGuard( m_aMutex );
397 0 : Any aControlValue;
398 :
399 : OSL_ENSURE( m_pHelper.get(), "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!" );
400 0 : if ( !m_pHelper.get() )
401 0 : return aControlValue;
402 :
403 0 : PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
404 :
405 0 : switch ( nPropId )
406 : {
407 : case PROPERTY_ID_BOUND_CELL:
408 : {
409 0 : Reference< XValueBinding > xBinding;
410 : #if OSL_DEBUG_LEVEL > 0
411 : bool bSuccess =
412 : #endif
413 0 : _rPropertyValue >>= xBinding;
414 : OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
415 :
416 : // the only value binding we support so far is linking to spreadsheet cells
417 0 : aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
418 : }
419 0 : break;
420 :
421 : case PROPERTY_ID_LIST_CELL_RANGE:
422 : {
423 0 : Reference< XListEntrySource > xSource;
424 : #if OSL_DEBUG_LEVEL > 0
425 : bool bSuccess =
426 : #endif
427 0 : _rPropertyValue >>= xSource;
428 : OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
429 :
430 : // the only value binding we support so far is linking to spreadsheet cells
431 0 : aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
432 : }
433 0 : break;
434 :
435 : case PROPERTY_ID_CELL_EXCHANGE_TYPE:
436 0 : aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
437 0 : break;
438 :
439 : default:
440 : OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
441 0 : break;
442 : }
443 :
444 0 : return aControlValue;
445 : }
446 :
447 :
448 0 : Sequence< Property > SAL_CALL CellBindingPropertyHandler::doDescribeSupportedProperties() const
449 : {
450 0 : ::std::vector< Property > aProperties;
451 :
452 0 : bool bAllowCellLinking = m_pHelper.get() && m_pHelper->isCellBindingAllowed();
453 0 : bool bAllowCellIntLinking = m_pHelper.get() && m_pHelper->isCellIntegerBindingAllowed();
454 0 : bool bAllowListCellRange = m_pHelper.get() && m_pHelper->isListCellRangeAllowed();
455 0 : if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
456 : {
457 : sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
458 0 : + ( bAllowListCellRange ? 1 : 0 )
459 0 : + ( bAllowCellIntLinking ? 1 : 0 );
460 0 : aProperties.resize( nPos );
461 :
462 0 : if ( bAllowCellLinking )
463 : {
464 0 : aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
465 0 : ::cppu::UnoType<OUString>::get(), 0 );
466 : }
467 0 : if ( bAllowCellIntLinking )
468 : {
469 0 : aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
470 0 : ::cppu::UnoType<sal_Int16>::get(), 0 );
471 : }
472 0 : if ( bAllowListCellRange )
473 : {
474 0 : aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
475 0 : ::cppu::UnoType<OUString>::get(), 0 );
476 : }
477 : }
478 :
479 0 : if ( aProperties.empty() )
480 0 : return Sequence< Property >();
481 0 : return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
482 : }
483 :
484 :
485 : } // namespace pcr
486 :
487 :
488 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|