LibreOffice Module extensions (master) 1
cellbindinghandler.cxx
Go to the documentation of this file.
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
21#include "formstrings.hxx"
22#include "formmetadata.hxx"
23#include "cellbindinghelper.hxx"
24
25#include <com/sun/star/form/binding/XValueBinding.hpp>
26#include <com/sun/star/lang/NullPointerException.hpp>
27#include <com/sun/star/table/CellAddress.hpp>
28#include <com/sun/star/inspection/XObjectInspectorUI.hpp>
29#include <tools/debug.hxx>
31
32
33namespace pcr
34{
35
36
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::table;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::script;
42 using namespace ::com::sun::star::frame;
43 using namespace ::com::sun::star::inspection;
44 using namespace ::com::sun::star::form::binding;
45 using namespace ::comphelper;
46
47 CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
48 :PropertyHandlerComponent( _rxContext )
49 ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<sal_Int16>::get(), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
50 {
51 }
52
53
55 {
56 return "com.sun.star.comp.extensions.CellBindingPropertyHandler";
57 }
58
59
61 {
62 return { "com.sun.star.form.inspection.CellBindingPropertyHandler" };
63 }
64
65
67 {
69
70 Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
71 DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
73 m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
74 }
75
76
78 {
79 }
80
81
83 {
84 Sequence< OUString > aInterestingProperties{ PROPERTY_LIST_CELL_RANGE,
87 return aInterestingProperties;
88 }
89
90
91 void SAL_CALL CellBindingPropertyHandler::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
92 {
93 ::osl::MutexGuard aGuard( m_aMutex );
94 PropertyId nActuatingPropId( impl_getPropertyId_throwRuntime( _rActuatingPropertyName ) );
95 OSL_PRECOND(m_pHelper,
96 "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistency!");
97 // if we survived impl_getPropertyId_throwRuntime, we should have a helper, since no helper implies no properties
98
99 OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
100 if ( !_rxInspectorUI.is() )
101 throw NullPointerException();
102
103 std::vector< PropertyId > aDependentProperties;
104
105 switch ( nActuatingPropId )
106 {
107 // ----- BoundCell -----
109 {
110 // the SQL-data-binding related properties need to be enabled if and only if
111 // there is *no* valid cell binding
112 Reference< XValueBinding > xBinding;
113 _rNewValue >>= xBinding;
114
116 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
118 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
119
121 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
123 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
124
125 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
126
127 if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
128 {
129 // ensure that the "transfer selection as" property is reset. Since we can't remember
130 // it at the object itself, but derive it from the binding only, we have to normalize
131 // it now that there *is* no binding anymore.
133 }
134 }
135 break;
136
137 // ----- CellRange -----
139 {
140 // the list source related properties need to be enabled if and only if
141 // there is *no* valid external list source for the control
142 Reference< XListEntrySource > xSource;
143 _rNewValue >>= xSource;
144
145 _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
146 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
147 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
148
149 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
150
151 // also reset the list entries if the cell range is reset
152 // #i28319#
153 if ( !_bFirstTimeInit )
154 {
155 try
156 {
157 if ( !xSource.is() )
158 {
159 setPropertyValue( PROPERTY_STRINGITEMLIST, Any( Sequence< OUString >() ) );
160 setPropertyValue( PROPERTY_TYPEDITEMLIST, Any( Sequence< Any >() ) );
161 }
162 }
163 catch( const Exception& )
164 {
166 "extensions.propctrlr",
167 "ListCellRange: caught an exception while resetting the string items!");
168 }
169 }
170 }
171 break; // case PROPERTY_ID_LIST_CELL_RANGE
172
173 // ----- DataField -----
175 {
176 OUString sControlSource;
177 _rNewValue >>= sControlSource;
179 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
180 }
181 break; // case PROPERTY_ID_CONTROLSOURCE
182
183 default:
184 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
185 }
186
187 for (auto const& dependentProperty : aDependentProperties)
188 {
189 impl_updateDependentProperty_nothrow( dependentProperty, _rxInspectorUI );
190 }
191 }
192
193
194 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
195 {
196 try
197 {
198 switch ( _nPropId )
199 {
200 // ----- BoundColumn -----
202 {
203 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
204 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
205 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
206
208 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
209 }
210 break; // case PROPERTY_ID_BOUNDCOLUMN
211
212 } // switch
213
214 }
215 catch( const Exception& )
216 {
217 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow" );
218 }
219 }
220
221
222 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
223 {
224 ::osl::MutexGuard aGuard( m_aMutex );
225 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
226
227 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::getPropertyValue: inconsistency!");
228 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
229
230 Any aReturn;
231 switch ( nPropId )
232 {
234 {
235 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
236 if ( !CellBindingHelper::isCellBinding( xBinding ) )
237 xBinding.clear();
238
239 aReturn <<= xBinding;
240 }
241 break;
242
244 {
245 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
247 xSource.clear();
248
249 aReturn <<= xSource;
250 }
251 break;
252
254 {
255 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
256 aReturn <<= static_cast<sal_Int16>( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
257 }
258 break;
259
260 default:
261 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
262 break;
263 }
264 return aReturn;
265 }
266
267
268 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
269 {
270 ::osl::MutexGuard aGuard( m_aMutex );
271 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
272
273 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::setPropertyValue: inconsistency!");
274 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
275
276 try
277 {
278 Any aOldValue = getPropertyValue( _rPropertyName );
279
280 switch ( nPropId )
281 {
283 {
284 Reference< XValueBinding > xBinding;
285 _rValue >>= xBinding;
286 m_pHelper->setBinding( xBinding );
287 }
288 break;
289
291 {
292 Reference< XListEntrySource > xSource;
293 _rValue >>= xSource;
294 m_pHelper->setListSource( xSource );
295 }
296 break;
297
299 {
300 sal_Int16 nExchangeType = 0;
301 OSL_VERIFY( _rValue >>= nExchangeType );
302
303 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
304 if ( xBinding.is() )
305 {
306 bool bNeedIntegerBinding = ( nExchangeType == 1 );
307 if ( bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
308 {
309 CellAddress aAddress;
310 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
311 {
312 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
313 m_pHelper->setBinding( xBinding );
314 }
315 }
316 }
317 }
318 break;
319
320 default:
321 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
322 break;
323 }
324
326
327 Any aNewValue( getPropertyValue( _rPropertyName ) );
328 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
329 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
330 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
331 // and handle the listener notification itself
332 }
333 catch( const Exception& )
334 {
335 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingPropertyHandler::setPropertyValue" );
336 }
337 }
338
339
340 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
341 {
342 ::osl::MutexGuard aGuard( m_aMutex );
343 Any aPropertyValue;
344
345 OSL_ENSURE(
346 m_pHelper,
347 "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!");
348 if (!m_pHelper)
349 return aPropertyValue;
350
351 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
352
353 OUString sControlValue;
354 OSL_VERIFY( _rControlValue >>= sControlValue );
355 switch( nPropId )
356 {
358 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
359 break;
360
362 {
363 // if we have the possibility of an integer binding, then we must preserve
364 // this property's value (e.g. if the current binding is an integer binding, then
365 // the newly created one must be, too)
366 bool bIntegerBinding = false;
367 if ( m_pHelper->isCellIntegerBindingAllowed() )
368 {
369 sal_Int16 nCurrentBindingType = 0;
370 getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
371 bIntegerBinding = ( nCurrentBindingType != 0 );
372 }
373 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
374 }
375 break;
376
378 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
379 break;
380
381 default:
382 OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
383 break;
384 }
385
386 return aPropertyValue;
387 }
388
389
390 Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const OUString& _rPropertyName,
391 const Any& _rPropertyValue, const Type& /*_rControlValueType*/ )
392 {
393 ::osl::MutexGuard aGuard( m_aMutex );
394 Any aControlValue;
395
396 OSL_ENSURE(
397 m_pHelper,
398 "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!");
399 if (!m_pHelper)
400 return aControlValue;
401
402 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
403
404 switch ( nPropId )
405 {
407 {
408 Reference< XValueBinding > xBinding;
409 bool bSuccess = _rPropertyValue >>= xBinding;
410 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
411
412 // the only value binding we support so far is linking to spreadsheet cells
413 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
414 }
415 break;
416
418 {
419 Reference< XListEntrySource > xSource;
420 bool bSuccess = _rPropertyValue >>= xSource;
421 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
422
423 // the only value binding we support so far is linking to spreadsheet cells
424 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
425 }
426 break;
427
429 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
430 break;
431
432 default:
433 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
434 break;
435 }
436
437 return aControlValue;
438 }
439
440
442 {
443 std::vector< Property > aProperties;
444
445 bool bAllowCellLinking = m_pHelper && m_pHelper->isCellBindingAllowed();
446 bool bAllowCellIntLinking = m_pHelper && m_pHelper->isCellIntegerBindingAllowed();
447 bool bAllowListCellRange = m_pHelper && m_pHelper->isListCellRangeAllowed();
448 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
449 {
450 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
451 + ( bAllowListCellRange ? 1 : 0 )
452 + ( bAllowCellIntLinking ? 1 : 0 );
453 aProperties.resize( nPos );
454
455 if ( bAllowCellLinking )
456 {
459 }
460 if ( bAllowCellIntLinking )
461 {
464 }
465 if ( bAllowListCellRange )
466 {
469 }
470 }
471
472 if ( aProperties.empty() )
473 return Sequence< Property >();
475 }
476
477
478} // namespace pcr
479
480extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
482 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
483{
484 return cppu::acquire(new pcr::CellBindingPropertyHandler(context));
485}
486
487/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * extensions_propctrlr_CellBindingPropertyHandler_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
mutable::osl::Mutex m_aMutex
encapsulates functionality related to binding a form control to a spreadsheet cell
static bool isCellIntegerBinding(const css::uno::Reference< css::form::binding::XValueBinding > &_rxBinding)
checks whether a given binding is a spreadsheet cell binding, exchanging integer values
static bool isCellRangeListSource(const css::uno::Reference< css::form::binding::XListEntrySource > &_rxSource)
checks whether a given list source is a spreadsheet cell list source
static bool isCellBinding(const css::uno::Reference< css::form::binding::XValueBinding > &_rxBinding)
checks whether a given binding is a spreadsheet cell binding
static bool isSpreadsheetDocument(const css::uno::Reference< css::frame::XModel > &_rxContextDocument)
determines whether the given model is a spreadsheet document model
virtual void SAL_CALL actuatingPropertyChanged(const OUString &_rActuatingPropertyName, const css::uno::Any &_rNewValue, const css::uno::Any &_rOldValue, const css::uno::Reference< css::inspection::XObjectInspectorUI > &_rxInspectorUI, sal_Bool _bFirstTimeInit) override
virtual css::uno::Any SAL_CALL convertToControlValue(const OUString &_rPropertyName, const css::uno::Any &_rPropertyValue, const css::uno::Type &_rControlValueType) override
void impl_updateDependentProperty_nothrow(PropertyId _nPropId, const css::uno::Reference< css::inspection::XObjectInspectorUI > &_rxInspectorUI) const
updates a property (UI) whose state depends on more than one other property
virtual ~CellBindingPropertyHandler() override
virtual OUString SAL_CALL getImplementationName() override
std::unique_ptr< CellBindingHelper > m_pHelper
virtual css::uno::Sequence< css::beans::Property > doDescribeSupportedProperties() const override
virtual void onNewComponent() override
called when XPropertyHandler::inspect has been called, and we thus have a new component to inspect
virtual css::uno::Sequence< OUString > SAL_CALL getActuatingProperties() override
virtual void SAL_CALL setPropertyValue(const OUString &_rPropertyName, const css::uno::Any &_rValue) override
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &_rPropertyName) override
virtual css::uno::Any SAL_CALL convertToPropertyValue(const OUString &_rPropertyName, const css::uno::Any &_rControlValue) override
::rtl::Reference< IPropertyEnumRepresentation > m_pCellExchangeConverter
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
CellBindingPropertyHandler(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
an implementation of the IPropertyEnumRepresentation
PropertyHandler implementation which additionally supports XServiceInfo.
std::unique_ptr< OPropertyInfoService > m_pInfoService
access to property meta data
virtual void onNewComponent()
called when XPropertyHandler::inspect has been called, and we thus have a new component to inspect
bool impl_isSupportedProperty_nothrow(PropertyId _nPropId) const
determines whether a given property id is part of our supported properties
void firePropertyChange(const OUString &_rPropName, PropertyId _nPropId, const css::uno::Any &_rOldValue, const css::uno::Any &_rNewValue)
fires the change in a property value to our listener (if any)
bool impl_componentHasProperty_throw(const OUString &_rPropName) const
determines whether our component has a given property
PropertyId impl_getPropertyId_throwUnknownProperty(const OUString &_rPropertyName) const
retrieves the property id for a given property name
css::uno::Reference< css::beans::XPropertySet > m_xComponent
the component we're inspecting
PropertyId impl_getPropertyId_throwRuntime(const OUString &_rPropertyName) const
retrieves the property id for a given property name
void impl_setContextDocumentModified_nothrow() const
marks the context document as modified
css::uno::Reference< css::frame::XModel > impl_getContextDocument_nothrow() const
returns the value of the ContextDocument property in the ComponentContext which was used to create th...
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define PROPERTY_ID_FILTERPROPOSAL
#define PROPERTY_ID_BOUNDCOLUMN
#define PROPERTY_ID_CELL_EXCHANGE_TYPE
#define PROPERTY_ID_BOUND_CELL
#define PROPERTY_ID_EMPTY_IS_NULL
#define PROPERTY_ID_LIST_CELL_RANGE
#define PROPERTY_ID_CONTROLSOURCE
constexpr OUStringLiteral PROPERTY_FILTERPROPOSAL
constexpr OUStringLiteral PROPERTY_CONTROLSOURCE
Definition: formstrings.hxx:42
constexpr OUStringLiteral PROPERTY_LIST_CELL_RANGE
constexpr OUStringLiteral PROPERTY_EMPTY_IS_NULL
Definition: formstrings.hxx:72
constexpr OUStringLiteral PROPERTY_LISTSOURCETYPE
Definition: formstrings.hxx:73
constexpr OUStringLiteral PROPERTY_TYPEDITEMLIST
Definition: formstrings.hxx:61
constexpr OUStringLiteral PROPERTY_BOUND_CELL
constexpr OUStringLiteral PROPERTY_BOUNDCOLUMN
constexpr OUStringLiteral PROPERTY_CELL_EXCHANGE_TYPE
constexpr OUStringLiteral PROPERTY_STRINGITEMLIST
Definition: formstrings.hxx:60
constexpr OUStringLiteral PROPERTY_LISTSOURCE
Definition: formstrings.hxx:74
sal_uInt16 nPos
@ Exception
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Type
a property handler for any virtual string properties
Definition: browserline.cxx:39
sal_Int32 PropertyId
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
unsigned char sal_Bool