1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "formcellbinding.hxx"
21 #include <com/sun/star/form/binding/XBindableValue.hpp>
22 #include <com/sun/star/form/binding/XListEntrySink.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/container/XChild.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/beans/NamedValue.hpp>
28 #include "strings.hxx"
29 #include <osl/diagnose.h>
30 #include <comphelper/diagnose_ex.hxx>
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::frame
;
40 using namespace ::com::sun::star::sheet
;
41 using namespace ::com::sun::star::container
;
42 using namespace ::com::sun::star::table
;
43 using namespace ::com::sun::star::form
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::form::binding
;
49 using ::com::sun::star::uno::Reference
;
50 using ::com::sun::star::uno::XInterface
;
51 using ::com::sun::star::container::XChild
;
52 using ::com::sun::star::frame::XModel
;
53 using ::com::sun::star::uno::UNO_QUERY
;
55 template< class TYPE
>
56 Reference
< TYPE
> getTypedModelNode( const Reference
< XInterface
>& _rxModelNode
)
58 Reference
< TYPE
> xTypedNode( _rxModelNode
, UNO_QUERY
);
59 if ( xTypedNode
.is() )
63 Reference
< XChild
> xChild( _rxModelNode
, UNO_QUERY
);
65 return getTypedModelNode
< TYPE
>( xChild
->getParent() );
71 Reference
< XModel
> getDocument( const Reference
< XInterface
>& _rxModelNode
)
73 return getTypedModelNode
< XModel
>( _rxModelNode
);
79 const OUString
& m_sReference
;
82 explicit StringCompare( const OUString
& _rReference
) : m_sReference( _rReference
) { }
84 bool operator()( std::u16string_view _rCompare
)
86 return ( _rCompare
== m_sReference
);
91 //= FormCellBindingHelper
92 FormCellBindingHelper::FormCellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxDocument
)
93 :m_xControlModel( _rxControlModel
)
94 ,m_xDocument( _rxDocument
, UNO_QUERY
)
96 OSL_ENSURE( m_xControlModel
.is(), "FormCellBindingHelper::FormCellBindingHelper: invalid control model!" );
98 if ( !m_xDocument
.is() )
99 m_xDocument
.set(getDocument( m_xControlModel
), css::uno::UNO_QUERY
);
100 OSL_ENSURE( m_xDocument
.is(), "FormCellBindingHelper::FormCellBindingHelper: Did not find the spreadsheet document!" );
103 bool FormCellBindingHelper::livesInSpreadsheetDocument( const Reference
< XPropertySet
>& _rxControlModel
)
105 Reference
< XSpreadsheetDocument
> xDocument( getDocument( _rxControlModel
), UNO_QUERY
);
106 return xDocument
.is();
109 bool FormCellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
112 return doConvertAddressRepresentations(
113 PROPERTY_FILE_REPRESENTATION
,
114 Any( _rAddressDescription
),
119 && ( aAddress
>>= _rAddress
);
122 bool FormCellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
123 CellRangeAddress
& /* [out] */ _rAddress
) const
126 return doConvertAddressRepresentations(
127 PROPERTY_FILE_REPRESENTATION
,
128 Any( _rAddressDescription
),
133 && ( aAddress
>>= _rAddress
);
136 Reference
< XValueBinding
> FormCellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bUseIntegerBinding
) const
138 Reference
< XValueBinding
> xBinding
;
139 if ( !m_xDocument
.is() )
143 // get the UNO representation of the address
144 CellAddress aAddress
;
145 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
148 xBinding
.set(createDocumentDependentInstance(
149 _bUseIntegerBinding
? SERVICE_LISTINDEXCELLBINDING
: SERVICE_CELLVALUEBINDING
,
152 ), css::uno::UNO_QUERY
);
157 Reference
< XListEntrySource
> FormCellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
159 Reference
< XListEntrySource
> xSource
;
161 CellRangeAddress aRangeAddress
;
162 if ( !convertStringAddress( _rAddress
, aRangeAddress
) )
165 // create a range object for this address
166 xSource
.set(createDocumentDependentInstance(
167 SERVICE_CELLRANGELISTSOURCE
,
168 PROPERTY_LIST_CELL_RANGE
,
170 ), css::uno::UNO_QUERY
);
175 OUString
FormCellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
177 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "FormCellBindingHelper::getStringAddressFromCellBinding: this is no cell binding!" );
182 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
183 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "FormCellBindingHelper::getStringAddressFromCellBinding: no property set for the binding!" );
184 if ( xBindingProps
.is() )
186 CellAddress aAddress
;
187 xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= aAddress
;
190 doConvertAddressRepresentations( PROPERTY_ADDRESS
, Any( aAddress
),
191 PROPERTY_FILE_REPRESENTATION
, aStringAddress
, false );
193 aStringAddress
>>= sAddress
;
196 catch( const Exception
& )
198 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::getStringAddressFromCellBinding" );
204 OUString
FormCellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
206 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "FormCellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
211 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
212 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "FormCellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
213 if ( xSourceProps
.is() )
215 CellRangeAddress aRangeAddress
;
216 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
219 doConvertAddressRepresentations( PROPERTY_ADDRESS
, Any( aRangeAddress
),
220 PROPERTY_FILE_REPRESENTATION
, aStringAddress
, true );
221 aStringAddress
>>= sAddress
;
224 catch( const Exception
& )
226 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::getStringAddressFromCellListSource" );
232 bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const Reference
< XSpreadsheetDocument
>& _rxDocument
, const OUString
& _rService
)
234 bool bYesItIs
= false;
238 Reference
< XServiceInfo
> xSI( _rxDocument
, UNO_QUERY
);
239 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
241 Reference
< XMultiServiceFactory
> xDocumentFactory( _rxDocument
, UNO_QUERY
);
242 OSL_ENSURE( xDocumentFactory
.is(), "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
244 if ( xDocumentFactory
.is() )
246 const Sequence
<OUString
> aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
248 bYesItIs
= std::any_of( aAvailableServices
.begin(), aAvailableServices
.end(), StringCompare( _rService
) );
252 catch( const Exception
& )
254 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies" );
260 bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const
262 return isSpreadsheetDocumentWhichSupplies( m_xDocument
, _rService
);
265 bool FormCellBindingHelper::isListCellRangeAllowed( const Reference
< XModel
>& _rxDocument
)
267 return isSpreadsheetDocumentWhichSupplies(
268 Reference
< XSpreadsheetDocument
>( _rxDocument
, UNO_QUERY
),
269 SERVICE_CELLRANGELISTSOURCE
273 bool FormCellBindingHelper::isListCellRangeAllowed( ) const
275 bool bAllow( false );
277 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
280 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_CELLRANGELISTSOURCE
);
286 bool FormCellBindingHelper::isCellBindingAllowed( ) const
288 bool bAllow( false );
290 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
291 if ( xBindable
.is() )
293 // the control can potentially be bound to an external value
294 // Does it live within a Calc document, and is able to supply CellBindings?
295 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_CELLVALUEBINDING
);
301 bool FormCellBindingHelper::isCellBindingAllowed( const Reference
< XModel
>& _rxDocument
)
303 return isSpreadsheetDocumentWhichSupplies(
304 Reference
< XSpreadsheetDocument
>( _rxDocument
, UNO_QUERY
),
305 SERVICE_CELLVALUEBINDING
309 bool FormCellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
)
311 return doesComponentSupport( _rxBinding
, SERVICE_CELLVALUEBINDING
);
314 bool FormCellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
)
316 return doesComponentSupport( _rxBinding
, SERVICE_LISTINDEXCELLBINDING
);
319 bool FormCellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
)
321 return doesComponentSupport( _rxSource
, SERVICE_CELLRANGELISTSOURCE
);
324 bool FormCellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
)
326 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
327 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
331 Reference
< XValueBinding
> FormCellBindingHelper::getCurrentBinding( ) const
333 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
334 if ( xBindable
.is() )
335 return xBindable
->getValueBinding();
336 return Reference
<XValueBinding
>();
339 Reference
< XListEntrySource
> FormCellBindingHelper::getCurrentListSource( ) const
341 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
343 return xSink
->getListEntrySource();
344 return Reference
<XListEntrySource
>();
347 void FormCellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
349 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
350 OSL_PRECOND( xBindable
.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
351 if ( xBindable
.is() )
352 xBindable
->setValueBinding( _rxBinding
);
355 void FormCellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
357 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
358 OSL_PRECOND( xSink
.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
360 xSink
->setListEntrySource( _rxSource
);
363 Reference
< XInterface
> FormCellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
364 const Any
& _rArgumentValue
) const
366 Reference
< XInterface
> xReturn
;
368 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
369 OSL_ENSURE( xDocumentFactory
.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
370 if ( xDocumentFactory
.is() )
374 if ( !_rArgumentName
.isEmpty() )
377 aArg
.Name
= _rArgumentName
;
378 aArg
.Value
= _rArgumentValue
;
380 Sequence
< Any
> aArgs
{ Any(aArg
) };
381 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
385 xReturn
= xDocumentFactory
->createInstance( _rService
);
388 catch ( const Exception
& )
390 OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
396 bool FormCellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
397 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const
399 bool bSuccess
= false;
401 Reference
< XPropertySet
> xConverter(
402 createDocumentDependentInstance(
403 _bIsRange
? SERVICE_RANGEADDRESS_CONVERSION
: SERVICE_ADDRESS_CONVERSION
,
409 OSL_ENSURE( xConverter
.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
410 if ( xConverter
.is() )
414 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
415 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
418 catch( const Exception
& )
420 TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::doConvertAddressRepresentations" );
427 } // namespace xmloff
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */