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>
36 #include <rtl/logfile.hxx>
41 //............................................................................
44 //............................................................................
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::frame
;
49 using namespace ::com::sun::star::sheet
;
50 using namespace ::com::sun::star::container
;
51 using namespace ::com::sun::star::drawing
;
52 using namespace ::com::sun::star::table
;
53 using namespace ::com::sun::star::form
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::com::sun::star::form::binding
;
59 using ::com::sun::star::uno::Reference
;
60 using ::com::sun::star::uno::XInterface
;
61 using ::com::sun::star::container::XChild
;
62 using ::com::sun::star::frame::XModel
;
63 using ::com::sun::star::uno::UNO_QUERY
;
65 //....................................................................
66 template< class TYPE
>
67 Reference
< TYPE
> getTypedModelNode( const Reference
< XInterface
>& _rxModelNode
)
69 Reference
< TYPE
> xTypedNode( _rxModelNode
, UNO_QUERY
);
70 if ( xTypedNode
.is() )
74 Reference
< XChild
> xChild( _rxModelNode
, UNO_QUERY
);
76 return getTypedModelNode
< TYPE
>( xChild
->getParent() );
82 //....................................................................
83 Reference
< XModel
> getDocument( const Reference
< XInterface
>& _rxModelNode
)
85 return getTypedModelNode
< XModel
>( _rxModelNode
);
88 //....................................................................
89 struct StringCompare
: public ::std::unary_function
< OUString
, bool >
92 const OUString m_sReference
;
95 StringCompare( const OUString
& _rReference
) : m_sReference( _rReference
) { }
97 inline bool operator()( const OUString
& _rCompare
)
99 return ( _rCompare
== m_sReference
);
104 //========================================================================
105 //= FormCellBindingHelper
106 //========================================================================
107 //------------------------------------------------------------------------
108 FormCellBindingHelper::FormCellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxDocument
)
109 :m_xControlModel( _rxControlModel
)
110 ,m_xDocument( _rxDocument
, UNO_QUERY
)
112 OSL_ENSURE( m_xControlModel
.is(), "FormCellBindingHelper::FormCellBindingHelper: invalid control model!" );
114 if ( !m_xDocument
.is() )
115 m_xDocument
= m_xDocument
.query( getDocument( m_xControlModel
) );
116 OSL_ENSURE( m_xDocument
.is(), "FormCellBindingHelper::FormCellBindingHelper: Did not find the spreadsheet document!" );
119 //------------------------------------------------------------------------
120 sal_Bool
FormCellBindingHelper::livesInSpreadsheetDocument( const Reference
< XPropertySet
>& _rxControlModel
)
122 Reference
< XSpreadsheetDocument
> xDocument( getDocument( _rxControlModel
), UNO_QUERY
);
123 return xDocument
.is();
126 //------------------------------------------------------------------------
127 bool FormCellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
, sal_Int16
/*_nAssumeSheet*/ ) const
130 return doConvertAddressRepresentations(
131 PROPERTY_FILE_REPRESENTATION
,
132 makeAny( _rAddressDescription
),
137 && ( aAddress
>>= _rAddress
);
140 //------------------------------------------------------------------------
141 bool FormCellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
142 CellRangeAddress
& /* [out] */ _rAddress
) const
145 return doConvertAddressRepresentations(
146 PROPERTY_FILE_REPRESENTATION
,
147 makeAny( _rAddressDescription
),
152 && ( aAddress
>>= _rAddress
);
155 //------------------------------------------------------------------------
156 Reference
< XValueBinding
> FormCellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bUseIntegerBinding
) const
158 Reference
< XValueBinding
> xBinding
;
159 if ( !m_xDocument
.is() )
163 // get the UNO representation of the address
164 CellAddress aAddress
;
165 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
168 xBinding
= xBinding
.query( createDocumentDependentInstance(
169 _bUseIntegerBinding
? OUString(SERVICE_LISTINDEXCELLBINDING
) : OUString(SERVICE_CELLVALUEBINDING
),
177 //------------------------------------------------------------------------
178 Reference
< XListEntrySource
> FormCellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
180 Reference
< XListEntrySource
> xSource
;
182 CellRangeAddress aRangeAddress
;
183 if ( !convertStringAddress( _rAddress
, aRangeAddress
) )
186 // create a range object for this address
187 xSource
= xSource
.query( createDocumentDependentInstance(
188 SERVICE_CELLRANGELISTSOURCE
,
189 PROPERTY_LIST_CELL_RANGE
,
190 makeAny( aRangeAddress
)
196 //------------------------------------------------------------------------
197 OUString
FormCellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
199 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "FormCellBindingHelper::getStringAddressFromCellBinding: this is no cell binding!" );
204 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
205 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "FormCellBindingHelper::getStringAddressFromCellBinding: no property set for the binding!" );
206 if ( xBindingProps
.is() )
208 CellAddress aAddress
;
209 xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= aAddress
;
212 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aAddress
),
213 PROPERTY_FILE_REPRESENTATION
, aStringAddress
, false );
215 aStringAddress
>>= sAddress
;
218 catch( const Exception
& )
220 OSL_FAIL( "FormCellBindingHelper::getStringAddressFromCellBinding: caught an exception!" );
226 //------------------------------------------------------------------------
227 OUString
FormCellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
229 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "FormCellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
234 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
235 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "FormCellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
236 if ( xSourceProps
.is() )
238 CellRangeAddress aRangeAddress
;
239 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
242 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aRangeAddress
),
243 PROPERTY_FILE_REPRESENTATION
, aStringAddress
, true );
244 aStringAddress
>>= sAddress
;
247 catch( const Exception
& )
249 OSL_FAIL( "FormCellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
255 //------------------------------------------------------------------------
256 bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const Reference
< XSpreadsheetDocument
>& _rxDocument
, const OUString
& _rService
) SAL_THROW(())
258 bool bYesItIs
= false;
262 Reference
< XServiceInfo
> xSI( _rxDocument
, UNO_QUERY
);
263 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
265 Reference
< XMultiServiceFactory
> xDocumentFactory( _rxDocument
, UNO_QUERY
);
266 OSL_ENSURE( xDocumentFactory
.is(), "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
268 Sequence
< OUString
> aAvailableServices
;
269 if ( xDocumentFactory
.is() )
270 aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
272 const OUString
* pFound
= ::std::find_if(
273 aAvailableServices
.getConstArray(),
274 aAvailableServices
.getConstArray() + aAvailableServices
.getLength(),
275 StringCompare( _rService
)
277 if ( pFound
- aAvailableServices
.getConstArray() < aAvailableServices
.getLength() )
283 catch( const Exception
& )
285 OSL_FAIL( "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: caught an exception!" );
291 //------------------------------------------------------------------------
292 bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const SAL_THROW(())
294 return isSpreadsheetDocumentWhichSupplies( m_xDocument
, _rService
);
297 //------------------------------------------------------------------------
298 bool FormCellBindingHelper::isListCellRangeAllowed( const Reference
< XModel
>& _rxDocument
)
300 return isSpreadsheetDocumentWhichSupplies(
301 Reference
< XSpreadsheetDocument
>( _rxDocument
, UNO_QUERY
),
302 SERVICE_CELLRANGELISTSOURCE
306 //------------------------------------------------------------------------
307 bool FormCellBindingHelper::isListCellRangeAllowed( ) const
309 bool bAllow( false );
311 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
314 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_CELLRANGELISTSOURCE
);
320 //------------------------------------------------------------------------
321 bool FormCellBindingHelper::isCellBindingAllowed( ) const
323 bool bAllow( false );
325 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
326 if ( xBindable
.is() )
328 // the control can potentially be bound to an external value
329 // Does it live within a Calc document, and is able to supply CellBindings?
330 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_CELLVALUEBINDING
);
336 //------------------------------------------------------------------------
337 bool FormCellBindingHelper::isCellBindingAllowed( const Reference
< XModel
>& _rxDocument
)
339 return isSpreadsheetDocumentWhichSupplies(
340 Reference
< XSpreadsheetDocument
>( _rxDocument
, UNO_QUERY
),
341 SERVICE_CELLVALUEBINDING
345 //------------------------------------------------------------------------
346 bool FormCellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
348 return doesComponentSupport( _rxBinding
.get(), SERVICE_CELLVALUEBINDING
);
351 //------------------------------------------------------------------------
352 bool FormCellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
) const
354 return doesComponentSupport( _rxBinding
.get(), SERVICE_LISTINDEXCELLBINDING
);
357 //------------------------------------------------------------------------
358 bool FormCellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
) const
360 return doesComponentSupport( _rxSource
.get(), SERVICE_CELLRANGELISTSOURCE
);
363 //------------------------------------------------------------------------
364 bool FormCellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
) const
366 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
367 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
371 //------------------------------------------------------------------------
372 Reference
< XValueBinding
> FormCellBindingHelper::getCurrentBinding( ) const
374 Reference
< XValueBinding
> xBinding
;
375 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
376 if ( xBindable
.is() )
377 xBinding
= xBindable
->getValueBinding();
381 //------------------------------------------------------------------------
382 Reference
< XListEntrySource
> FormCellBindingHelper::getCurrentListSource( ) const
384 Reference
< XListEntrySource
> xSource
;
385 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
387 xSource
= xSink
->getListEntrySource();
391 //------------------------------------------------------------------------
392 void FormCellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
394 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
395 OSL_PRECOND( xBindable
.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
396 if ( xBindable
.is() )
397 xBindable
->setValueBinding( _rxBinding
);
400 //------------------------------------------------------------------------
401 void FormCellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
403 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
404 OSL_PRECOND( xSink
.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
406 xSink
->setListEntrySource( _rxSource
);
409 //------------------------------------------------------------------------
410 Reference
< XInterface
> FormCellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
411 const Any
& _rArgumentValue
) const
413 Reference
< XInterface
> xReturn
;
415 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
416 OSL_ENSURE( xDocumentFactory
.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
417 if ( xDocumentFactory
.is() )
421 if ( !_rArgumentName
.isEmpty() )
424 aArg
.Name
= _rArgumentName
;
425 aArg
.Value
= _rArgumentValue
;
427 Sequence
< Any
> aArgs( 1 );
430 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
434 xReturn
= xDocumentFactory
->createInstance( _rService
);
437 catch ( const Exception
& )
439 OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
445 //------------------------------------------------------------------------
446 bool FormCellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
447 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const SAL_THROW(())
449 bool bSuccess
= false;
451 Reference
< XPropertySet
> xConverter(
452 createDocumentDependentInstance(
453 _bIsRange
? OUString(SERVICE_RANGEADDRESS_CONVERSION
) : OUString(SERVICE_ADDRESS_CONVERSION
),
459 OSL_ENSURE( xConverter
.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
460 if ( xConverter
.is() )
464 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
465 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
468 catch( const Exception
& )
470 OSL_FAIL( "FormCellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
477 //............................................................................
478 } // namespace xmloff
479 //............................................................................
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */