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/form/XGridColumnFactory.hpp>
24 #include <com/sun/star/frame/XModel.hpp>
25 #include <com/sun/star/container/XChild.hpp>
26 #include <com/sun/star/container/XNamed.hpp>
27 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
28 #include <com/sun/star/table/XCellRange.hpp>
29 #include <com/sun/star/form/XFormsSupplier.hpp>
30 #include <com/sun/star/form/XForm.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/beans/NamedValue.hpp>
34 #include "strings.hxx"
35 #include <osl/diagnose.h>
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::frame
;
46 using namespace ::com::sun::star::sheet
;
47 using namespace ::com::sun::star::container
;
48 using namespace ::com::sun::star::drawing
;
49 using namespace ::com::sun::star::table
;
50 using namespace ::com::sun::star::form
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::form::binding
;
56 using ::com::sun::star::uno::Reference
;
57 using ::com::sun::star::uno::XInterface
;
58 using ::com::sun::star::container::XChild
;
59 using ::com::sun::star::frame::XModel
;
60 using ::com::sun::star::uno::UNO_QUERY
;
62 template< class TYPE
>
63 Reference
< TYPE
> getTypedModelNode( const Reference
< XInterface
>& _rxModelNode
)
65 Reference
< TYPE
> xTypedNode( _rxModelNode
, UNO_QUERY
);
66 if ( xTypedNode
.is() )
70 Reference
< XChild
> xChild( _rxModelNode
, UNO_QUERY
);
72 return getTypedModelNode
< TYPE
>( xChild
->getParent() );
78 Reference
< XModel
> getDocument( const Reference
< XInterface
>& _rxModelNode
)
80 return getTypedModelNode
< XModel
>( _rxModelNode
);
83 struct StringCompare
: public ::std::unary_function
< OUString
, bool >
86 const OUString m_sReference
;
89 explicit StringCompare( const OUString
& _rReference
) : m_sReference( _rReference
) { }
91 inline bool operator()( const OUString
& _rCompare
)
93 return ( _rCompare
== m_sReference
);
98 //= FormCellBindingHelper
99 FormCellBindingHelper::FormCellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxDocument
)
100 :m_xControlModel( _rxControlModel
)
101 ,m_xDocument( _rxDocument
, UNO_QUERY
)
103 OSL_ENSURE( m_xControlModel
.is(), "FormCellBindingHelper::FormCellBindingHelper: invalid control model!" );
105 if ( !m_xDocument
.is() )
106 m_xDocument
.set(getDocument( m_xControlModel
), css::uno::UNO_QUERY
);
107 OSL_ENSURE( m_xDocument
.is(), "FormCellBindingHelper::FormCellBindingHelper: Did not find the spreadsheet document!" );
110 bool FormCellBindingHelper::livesInSpreadsheetDocument( const Reference
< XPropertySet
>& _rxControlModel
)
112 Reference
< XSpreadsheetDocument
> xDocument( getDocument( _rxControlModel
), UNO_QUERY
);
113 return xDocument
.is();
116 bool FormCellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
119 return doConvertAddressRepresentations(
120 PROPERTY_FILE_REPRESENTATION
,
121 makeAny( _rAddressDescription
),
126 && ( aAddress
>>= _rAddress
);
129 bool FormCellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
130 CellRangeAddress
& /* [out] */ _rAddress
) const
133 return doConvertAddressRepresentations(
134 PROPERTY_FILE_REPRESENTATION
,
135 makeAny( _rAddressDescription
),
140 && ( aAddress
>>= _rAddress
);
143 Reference
< XValueBinding
> FormCellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bUseIntegerBinding
) const
145 Reference
< XValueBinding
> xBinding
;
146 if ( !m_xDocument
.is() )
150 // get the UNO representation of the address
151 CellAddress aAddress
;
152 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
155 xBinding
.set(createDocumentDependentInstance(
156 _bUseIntegerBinding
? OUString(SERVICE_LISTINDEXCELLBINDING
) : OUString(SERVICE_CELLVALUEBINDING
),
159 ), css::uno::UNO_QUERY
);
164 Reference
< XListEntrySource
> FormCellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
166 Reference
< XListEntrySource
> xSource
;
168 CellRangeAddress aRangeAddress
;
169 if ( !convertStringAddress( _rAddress
, aRangeAddress
) )
172 // create a range object for this address
173 xSource
.set(createDocumentDependentInstance(
174 SERVICE_CELLRANGELISTSOURCE
,
175 PROPERTY_LIST_CELL_RANGE
,
176 makeAny( aRangeAddress
)
177 ), css::uno::UNO_QUERY
);
182 OUString
FormCellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
184 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "FormCellBindingHelper::getStringAddressFromCellBinding: this is no cell binding!" );
189 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
190 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "FormCellBindingHelper::getStringAddressFromCellBinding: no property set for the binding!" );
191 if ( xBindingProps
.is() )
193 CellAddress aAddress
;
194 xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= aAddress
;
197 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aAddress
),
198 PROPERTY_FILE_REPRESENTATION
, aStringAddress
, false );
200 aStringAddress
>>= sAddress
;
203 catch( const Exception
& )
205 OSL_FAIL( "FormCellBindingHelper::getStringAddressFromCellBinding: caught an exception!" );
211 OUString
FormCellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
213 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "FormCellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
218 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
219 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "FormCellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
220 if ( xSourceProps
.is() )
222 CellRangeAddress aRangeAddress
;
223 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
226 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aRangeAddress
),
227 PROPERTY_FILE_REPRESENTATION
, aStringAddress
, true );
228 aStringAddress
>>= sAddress
;
231 catch( const Exception
& )
233 OSL_FAIL( "FormCellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
239 bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const Reference
< XSpreadsheetDocument
>& _rxDocument
, const OUString
& _rService
)
241 bool bYesItIs
= false;
245 Reference
< XServiceInfo
> xSI( _rxDocument
, UNO_QUERY
);
246 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
248 Reference
< XMultiServiceFactory
> xDocumentFactory( _rxDocument
, UNO_QUERY
);
249 OSL_ENSURE( xDocumentFactory
.is(), "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
251 Sequence
< OUString
> aAvailableServices
;
252 if ( xDocumentFactory
.is() )
253 aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
255 const OUString
* pFound
= ::std::find_if(
256 aAvailableServices
.getConstArray(),
257 aAvailableServices
.getConstArray() + aAvailableServices
.getLength(),
258 StringCompare( _rService
)
260 if ( pFound
- aAvailableServices
.getConstArray() < aAvailableServices
.getLength() )
266 catch( const Exception
& )
268 OSL_FAIL( "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: caught an exception!" );
274 bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const
276 return isSpreadsheetDocumentWhichSupplies( m_xDocument
, _rService
);
279 bool FormCellBindingHelper::isListCellRangeAllowed( const Reference
< XModel
>& _rxDocument
)
281 return isSpreadsheetDocumentWhichSupplies(
282 Reference
< XSpreadsheetDocument
>( _rxDocument
, UNO_QUERY
),
283 SERVICE_CELLRANGELISTSOURCE
287 bool FormCellBindingHelper::isListCellRangeAllowed( ) const
289 bool bAllow( false );
291 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
294 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_CELLRANGELISTSOURCE
);
300 bool FormCellBindingHelper::isCellBindingAllowed( ) const
302 bool bAllow( false );
304 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
305 if ( xBindable
.is() )
307 // the control can potentially be bound to an external value
308 // Does it live within a Calc document, and is able to supply CellBindings?
309 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_CELLVALUEBINDING
);
315 bool FormCellBindingHelper::isCellBindingAllowed( const Reference
< XModel
>& _rxDocument
)
317 return isSpreadsheetDocumentWhichSupplies(
318 Reference
< XSpreadsheetDocument
>( _rxDocument
, UNO_QUERY
),
319 SERVICE_CELLVALUEBINDING
323 bool FormCellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
)
325 return doesComponentSupport( _rxBinding
.get(), SERVICE_CELLVALUEBINDING
);
328 bool FormCellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
)
330 return doesComponentSupport( _rxBinding
.get(), SERVICE_LISTINDEXCELLBINDING
);
333 bool FormCellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
)
335 return doesComponentSupport( _rxSource
.get(), SERVICE_CELLRANGELISTSOURCE
);
338 bool FormCellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
)
340 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
341 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
345 Reference
< XValueBinding
> FormCellBindingHelper::getCurrentBinding( ) const
347 Reference
< XValueBinding
> xBinding
;
348 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
349 if ( xBindable
.is() )
350 xBinding
= xBindable
->getValueBinding();
354 Reference
< XListEntrySource
> FormCellBindingHelper::getCurrentListSource( ) const
356 Reference
< XListEntrySource
> xSource
;
357 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
359 xSource
= xSink
->getListEntrySource();
363 void FormCellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
365 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
366 OSL_PRECOND( xBindable
.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
367 if ( xBindable
.is() )
368 xBindable
->setValueBinding( _rxBinding
);
371 void FormCellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
373 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
374 OSL_PRECOND( xSink
.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
376 xSink
->setListEntrySource( _rxSource
);
379 Reference
< XInterface
> FormCellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
380 const Any
& _rArgumentValue
) const
382 Reference
< XInterface
> xReturn
;
384 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
385 OSL_ENSURE( xDocumentFactory
.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
386 if ( xDocumentFactory
.is() )
390 if ( !_rArgumentName
.isEmpty() )
393 aArg
.Name
= _rArgumentName
;
394 aArg
.Value
= _rArgumentValue
;
396 Sequence
< Any
> aArgs( 1 );
399 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
403 xReturn
= xDocumentFactory
->createInstance( _rService
);
406 catch ( const Exception
& )
408 OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
414 bool FormCellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
415 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const
417 bool bSuccess
= false;
419 Reference
< XPropertySet
> xConverter(
420 createDocumentDependentInstance(
421 _bIsRange
? OUString(SERVICE_RANGEADDRESS_CONVERSION
) : OUString(SERVICE_ADDRESS_CONVERSION
),
427 OSL_ENSURE( xConverter
.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
428 if ( xConverter
.is() )
432 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
433 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
436 catch( const Exception
& )
438 OSL_FAIL( "FormCellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
445 } // namespace xmloff
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */