1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cellbindinghelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "cellbindinghelper.hxx"
34 #include <com/sun/star/form/binding/XBindableValue.hpp>
35 #include <com/sun/star/form/binding/XListEntrySink.hpp>
36 #include <com/sun/star/form/FormComponentType.hpp>
37 #include <com/sun/star/form/XGridColumnFactory.hpp>
38 #include <com/sun/star/container/XChild.hpp>
39 #include <com/sun/star/container/XNamed.hpp>
40 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
41 #include <com/sun/star/table/XCellRange.hpp>
42 #include <com/sun/star/form/XFormsSupplier.hpp>
43 #include <com/sun/star/form/XForm.hpp>
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/beans/NamedValue.hpp>
47 #include <com/sun/star/sheet/XSpreadsheet.hpp>
48 #include <unotools/transliterationwrapper.hxx>
49 #include <osl/diagnose.h>
50 #include <tools/diagnose_ex.h>
51 #include "formstrings.hxx"
56 //............................................................................
59 //............................................................................
61 using namespace ::com::sun::star::uno
;
62 using namespace ::com::sun::star::beans
;
63 using namespace ::com::sun::star::frame
;
64 using namespace ::com::sun::star::sheet
;
65 using namespace ::com::sun::star::container
;
66 using namespace ::com::sun::star::drawing
;
67 using namespace ::com::sun::star::table
;
68 using namespace ::com::sun::star::form
;
69 using namespace ::com::sun::star::lang
;
70 using namespace ::com::sun::star::i18n
;
71 using namespace ::com::sun::star::form::binding
;
75 //....................................................................
76 struct StringCompare
: public ::std::unary_function
< ::rtl::OUString
, bool >
79 ::rtl::OUString m_sReference
;
82 StringCompare( const ::rtl::OUString
& _rReference
) : m_sReference( _rReference
) { }
84 inline bool operator()( const ::rtl::OUString
& _rCompare
)
86 return ( _rCompare
== m_sReference
) ? true : false;
91 //========================================================================
93 //========================================================================
94 //------------------------------------------------------------------------
95 CellBindingHelper::CellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxContextDocument
)
96 :m_xControlModel( _rxControlModel
)
98 OSL_ENSURE( m_xControlModel
.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
100 m_xDocument
= m_xDocument
.query( _rxContextDocument
);
101 OSL_ENSURE( m_xDocument
.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
103 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION
),
104 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
107 //------------------------------------------------------------------------
108 sal_Bool
CellBindingHelper::isSpreadsheetDocument( const Reference
< XModel
>& _rxContextDocument
)
110 return Reference
< XSpreadsheetDocument
>::query( _rxContextDocument
).is();
113 //------------------------------------------------------------------------
114 sal_Int16
CellBindingHelper::getControlSheetIndex( Reference
< XSpreadsheet
>& _out_rxSheet
) const
116 sal_Int16 nSheetIndex
= -1;
117 // every sheet has a draw page, and every draw page has a forms collection.
118 // Our control, OTOH, belongs to a forms collection. Match these ...
121 // for determining the draw page, we need the forms collection which
122 // the object belongs to. This is the first object up the hierarchy which is
123 // *no* XForm (and, well, no XGridColumnFactory)
124 Reference
< XChild
> xCheck( m_xControlModel
, UNO_QUERY
);
125 Reference
< XForm
> xParentAsForm
; if ( xCheck
.is() ) xParentAsForm
= xParentAsForm
.query( xCheck
->getParent() );
126 Reference
< XGridColumnFactory
> xParentAsGrid
; if ( xCheck
.is() ) xParentAsGrid
= xParentAsGrid
.query( xCheck
->getParent() );
128 while ( ( xParentAsForm
.is() || xParentAsGrid
.is() ) && xCheck
.is() )
130 xCheck
= xCheck
.query( xCheck
->getParent() );
131 xParentAsForm
= xParentAsForm
.query( xCheck
.is() ? xCheck
->getParent() : (Reference
< XInterface
>) Reference
< XForm
>() );
132 xParentAsGrid
= xParentAsGrid
.query( xCheck
.is() ? xCheck
->getParent() : (Reference
< XInterface
>) Reference
< XGridColumnFactory
>() );
134 Reference
< XInterface
> xFormsCollection( xCheck
.is() ? xCheck
->getParent() : Reference
< XInterface
>() );
136 // now iterate through the sheets
137 Reference
< XIndexAccess
> xSheets( m_xDocument
->getSheets(), UNO_QUERY
);
138 if ( xSheets
.is() && xFormsCollection
.is() )
140 for ( sal_Int32 i
= 0; i
< xSheets
->getCount(); ++i
)
142 Reference
< XDrawPageSupplier
> xSuppPage( xSheets
->getByIndex( i
), UNO_QUERY_THROW
);
143 Reference
< XFormsSupplier
> xSuppForms( xSuppPage
->getDrawPage(), UNO_QUERY_THROW
);
145 if ( xSuppForms
->getForms() == xFormsCollection
)
147 nSheetIndex
= (sal_Int16
)i
;
148 _out_rxSheet
.set( xSuppPage
, UNO_QUERY_THROW
);
154 catch( const Exception
& )
156 DBG_UNHANDLED_EXCEPTION();
162 //------------------------------------------------------------------------
163 bool CellBindingHelper::convertStringAddress( const ::rtl::OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
166 return doConvertAddressRepresentations(
167 PROPERTY_UI_REPRESENTATION
,
168 makeAny( _rAddressDescription
),
173 && ( aAddress
>>= _rAddress
);
176 //------------------------------------------------------------------------
177 bool CellBindingHelper::doConvertAddressRepresentations( const ::rtl::OUString
& _rInputProperty
, const Any
& _rInputValue
,
178 const ::rtl::OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const SAL_THROW(())
180 bool bSuccess
= false;
182 Reference
< XPropertySet
> xConverter(
183 createDocumentDependentInstance(
184 _bIsRange
? SERVICE_RANGEADDRESS_CONVERSION
: SERVICE_ADDRESS_CONVERSION
,
190 OSL_ENSURE( xConverter
.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
191 if ( xConverter
.is() )
195 Reference
< XSpreadsheet
> xSheet
;
196 xConverter
->setPropertyValue( PROPERTY_REFERENCE_SHEET
, makeAny( (sal_Int32
)getControlSheetIndex( xSheet
) ) );
197 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
198 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
201 catch( const Exception
& )
203 OSL_ENSURE( sal_False
, "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
210 //------------------------------------------------------------------------
211 bool CellBindingHelper::convertStringAddress( const ::rtl::OUString
& _rAddressDescription
,
212 CellRangeAddress
& /* [out] */ _rAddress
) const
215 return doConvertAddressRepresentations(
216 PROPERTY_UI_REPRESENTATION
,
217 makeAny( _rAddressDescription
),
222 && ( aAddress
>>= _rAddress
);
225 //------------------------------------------------------------------------
226 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromAddress( const CellAddress
& _rAddress
, bool _bSupportIntegerExchange
) const
228 Reference
< XValueBinding
> xBinding( createDocumentDependentInstance(
229 _bSupportIntegerExchange
? SERVICE_SHEET_CELL_INT_BINDING
: SERVICE_SHEET_CELL_BINDING
,
237 //------------------------------------------------------------------------
238 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromStringAddress( const ::rtl::OUString
& _rAddress
, bool _bSupportIntegerExchange
) const
240 Reference
< XValueBinding
> xBinding
;
241 if ( !m_xDocument
.is() )
245 // get the UNO representation of the address
246 CellAddress aAddress
;
247 if ( !_rAddress
.getLength() || !convertStringAddress( _rAddress
, aAddress
) )
250 return createCellBindingFromAddress( aAddress
, _bSupportIntegerExchange
);
253 //------------------------------------------------------------------------
254 Reference
< XListEntrySource
> CellBindingHelper::createCellListSourceFromStringAddress( const ::rtl::OUString
& _rAddress
) const
256 Reference
< XListEntrySource
> xSource
;
258 CellRangeAddress aRangeAddress
;
259 if ( !_rAddress
.getLength() || !convertStringAddress( _rAddress
, aRangeAddress
) )
262 // create a range object for this address
263 xSource
= xSource
.query( createDocumentDependentInstance(
264 SERVICE_SHEET_CELLRANGE_LISTSOURCE
,
265 PROPERTY_LIST_CELL_RANGE
,
266 makeAny( aRangeAddress
)
272 //------------------------------------------------------------------------
273 Reference
< XInterface
> CellBindingHelper::createDocumentDependentInstance( const ::rtl::OUString
& _rService
, const ::rtl::OUString
& _rArgumentName
,
274 const Any
& _rArgumentValue
) const
276 Reference
< XInterface
> xReturn
;
278 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
279 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
280 if ( xDocumentFactory
.is() )
284 if ( _rArgumentName
.getLength() )
287 aArg
.Name
= _rArgumentName
;
288 aArg
.Value
= _rArgumentValue
;
290 Sequence
< Any
> aArgs( 1 );
293 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
297 xReturn
= xDocumentFactory
->createInstance( _rService
);
300 catch ( const Exception
& )
302 OSL_ENSURE( sal_False
, "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
308 //------------------------------------------------------------------------
309 bool CellBindingHelper::getAddressFromCellBinding(
310 const Reference
< XValueBinding
>& _rxBinding
, CellAddress
& _rAddress
) const
312 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
314 bool bReturn
= false;
315 if ( !m_xDocument
.is() )
321 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
322 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
323 if ( xBindingProps
.is() )
325 CellAddress aAddress
;
326 bReturn
= (bool)( xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= _rAddress
);
329 catch( const Exception
& )
331 OSL_ENSURE( sal_False
, "CellBindingHelper::getAddressFromCellBinding: caught an exception!" );
337 //------------------------------------------------------------------------
338 ::rtl::OUString
CellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
340 CellAddress aAddress
;
341 ::rtl::OUString sAddress
;
342 if ( getAddressFromCellBinding( _rxBinding
, aAddress
) )
345 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aAddress
),
346 PROPERTY_UI_REPRESENTATION
, aStringAddress
, false );
348 aStringAddress
>>= sAddress
;
354 //------------------------------------------------------------------------
355 ::rtl::OUString
CellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
357 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
359 ::rtl::OUString sAddress
;
360 if ( !m_xDocument
.is() )
366 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
367 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
368 if ( xSourceProps
.is() )
370 CellRangeAddress aRangeAddress
;
371 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
374 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aRangeAddress
),
375 PROPERTY_UI_REPRESENTATION
, aStringAddress
, true );
376 aStringAddress
>>= sAddress
;
379 catch( const Exception
& )
381 OSL_ENSURE( sal_False
, "CellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
387 //------------------------------------------------------------------------
388 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const ::rtl::OUString
& _rService
) const
390 bool bYesItIs
= false;
392 Reference
< XServiceInfo
> xSI( m_xDocument
, UNO_QUERY
);
393 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
395 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
396 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
398 Sequence
< ::rtl::OUString
> aAvailableServices
;
399 if ( xDocumentFactory
.is() )
400 aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
402 const ::rtl::OUString
* pFound
= ::std::find_if(
403 aAvailableServices
.getConstArray(),
404 aAvailableServices
.getConstArray() + aAvailableServices
.getLength(),
405 StringCompare( _rService
)
407 if ( pFound
- aAvailableServices
.getConstArray() < aAvailableServices
.getLength() )
416 //------------------------------------------------------------------------
417 bool CellBindingHelper::isListCellRangeAllowed( ) const
419 bool bAllow( false );
421 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
424 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
430 //------------------------------------------------------------------------
431 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
435 // first, we only offer this for controls which allow bindings in general
436 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
437 if ( !xBindable
.is() )
440 // then, we must live in a spreadsheet document which can provide the special
441 // service needed for exchanging integer values
443 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING
);
445 // then, we only offer this for list boxes
450 sal_Int16 nClassId
= FormComponentType::CONTROL
;
451 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
452 if ( FormComponentType::LISTBOX
!= nClassId
)
455 catch( const Exception
& )
457 OSL_ENSURE( sal_False
, "CellBindingHelper::isCellIntegerBindingAllowed: caught an exception!" );
458 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
467 //------------------------------------------------------------------------
468 bool CellBindingHelper::isCellBindingAllowed( ) const
470 bool bAllow( false );
472 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
473 if ( xBindable
.is() )
475 // the control can potentially be bound to an external value
476 // Does it live within a Calc document, and is able to supply CellBindings?
477 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING
);
480 // disallow for some types
481 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distingusih
482 // using this list? The current behavior below is somewhat hackish ...
487 sal_Int16 nClassId
= FormComponentType::CONTROL
;
488 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
489 if ( ( FormComponentType::DATEFIELD
== nClassId
) || ( FormComponentType::TIMEFIELD
== nClassId
) )
492 catch( const Exception
& )
494 OSL_ENSURE( sal_False
, "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
501 //------------------------------------------------------------------------
502 bool CellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
504 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_BINDING
);
507 //------------------------------------------------------------------------
508 bool CellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
) const
510 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_INT_BINDING
);
513 //------------------------------------------------------------------------
514 bool CellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
) const
516 return doesComponentSupport( _rxSource
.get(), SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
519 //------------------------------------------------------------------------
520 bool CellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const ::rtl::OUString
& _rService
) const
523 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
524 bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
528 //------------------------------------------------------------------------
529 Reference
< XValueBinding
> CellBindingHelper::getCurrentBinding( ) const
531 Reference
< XValueBinding
> xBinding
;
532 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
533 if ( xBindable
.is() )
534 xBinding
= xBindable
->getValueBinding();
538 //------------------------------------------------------------------------
539 Reference
< XListEntrySource
> CellBindingHelper::getCurrentListSource( ) const
541 Reference
< XListEntrySource
> xSource
;
542 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
544 xSource
= xSink
->getListEntrySource();
548 //------------------------------------------------------------------------
549 void CellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
551 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
552 OSL_PRECOND( xBindable
.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
553 if ( xBindable
.is() )
554 xBindable
->setValueBinding( _rxBinding
);
557 //------------------------------------------------------------------------
558 void CellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
560 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
561 OSL_PRECOND( xSink
.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
563 xSink
->setListEntrySource( _rxSource
);
566 //............................................................................
568 //............................................................................