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 "celllistsource.hxx"
21 #include <tools/debug.hxx>
22 #include <com/sun/star/text/XTextRange.hpp>
23 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
24 #include <com/sun/star/util/XModifyBroadcaster.hpp>
25 #include <com/sun/star/container/XIndexAccess.hpp>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/beans/NamedValue.hpp>
28 #include <cppuhelper/supportsservice.hxx>
35 #define PROP_HANDLE_RANGE_ADDRESS 1
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::table
;
40 using namespace ::com::sun::star::text
;
41 using namespace ::com::sun::star::sheet
;
42 using namespace ::com::sun::star::container
;
43 using namespace ::com::sun::star::beans
;
44 using namespace ::com::sun::star::util
;
45 using namespace ::com::sun::star::form::binding
;
48 const char* OCellListSource::checkConsistency_static( const void* _pThis
)
50 return static_cast< const OCellListSource
* >( _pThis
)->checkConsistency( );
53 const char* OCellListSource::checkConsistency( ) const
55 const char* pAssertion
= NULL
;
57 // TODO: place any checks here to ensure consistency of this instance
64 OCellListSource::OCellListSource( const Reference
< XSpreadsheetDocument
>& _rxDocument
)
65 :OCellListSource_Base( m_aMutex
)
66 ,OCellListSource_PBase( OCellListSource_Base::rBHelper
)
67 ,m_xDocument( _rxDocument
)
68 ,m_aListEntryListeners( m_aMutex
)
69 ,m_bInitialized( false )
71 OSL_PRECOND( m_xDocument
.is(), "OCellListSource::OCellListSource: invalid document!" );
73 // register our property at the base class
74 CellRangeAddress aInitialPropValue
;
75 registerPropertyNoMember(
76 OUString( "CellRange" ),
77 PROP_HANDLE_RANGE_ADDRESS
,
78 PropertyAttribute::BOUND
| PropertyAttribute::READONLY
,
79 ::getCppuType( &aInitialPropValue
),
85 OCellListSource::~OCellListSource( )
87 if ( !OCellListSource_Base::rBHelper
.bDisposed
)
89 acquire(); // prevent duplicate dtor
95 IMPLEMENT_FORWARD_XINTERFACE2( OCellListSource
, OCellListSource_Base
, OCellListSource_PBase
)
98 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OCellListSource
, OCellListSource_Base
, OCellListSource_PBase
)
101 void SAL_CALL
OCellListSource::disposing()
103 ::osl::MutexGuard
aGuard( m_aMutex
);
105 Reference
<XModifyBroadcaster
> xBroadcaster( m_xRange
, UNO_QUERY
);
106 if ( xBroadcaster
.is() )
108 xBroadcaster
->removeModifyListener( this );
111 EventObject
aDisposeEvent( *this );
112 m_aListEntryListeners
.disposeAndClear( aDisposeEvent
);
114 WeakAggComponentImplHelperBase::disposing();
116 // TODO: clean up here whatever you need to clean up (e.g. revoking listeners etc.)
120 Reference
< XPropertySetInfo
> SAL_CALL
OCellListSource::getPropertySetInfo( ) throw(RuntimeException
, std::exception
)
122 return createPropertySetInfo( getInfoHelper() ) ;
126 ::cppu::IPropertyArrayHelper
& SAL_CALL
OCellListSource::getInfoHelper()
128 return *OCellListSource_PABase::getArrayHelper();
132 ::cppu::IPropertyArrayHelper
* OCellListSource::createArrayHelper( ) const
134 Sequence
< Property
> aProps
;
135 describeProperties( aProps
);
136 return new ::cppu::OPropertyArrayHelper(aProps
);
140 void SAL_CALL
OCellListSource::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
142 OSL_ENSURE( _nHandle
== PROP_HANDLE_RANGE_ADDRESS
, "OCellListSource::getFastPropertyValue: invalid handle!" );
143 // we only have this one property ....
144 (void)_nHandle
; // avoid warning in product version
146 _rValue
<<= getRangeAddress( );
150 void OCellListSource::checkDisposed( ) const SAL_THROW( ( DisposedException
) )
152 if ( OCellListSource_Base::rBHelper
.bInDispose
|| OCellListSource_Base::rBHelper
.bDisposed
)
153 throw DisposedException();
154 // TODO: is it worth having an error message here?
157 void OCellListSource::checkInitialized() SAL_THROW( ( RuntimeException
) )
159 if ( !m_bInitialized
)
160 throw RuntimeException();
161 // TODO: error message
164 OUString SAL_CALL
OCellListSource::getImplementationName( ) throw (RuntimeException
, std::exception
)
166 return OUString( "com.sun.star.comp.sheet.OCellListSource" );
169 sal_Bool SAL_CALL
OCellListSource::supportsService( const OUString
& _rServiceName
) throw (RuntimeException
, std::exception
)
171 return cppu::supportsService(this, _rServiceName
);
174 Sequence
< OUString
> SAL_CALL
OCellListSource::getSupportedServiceNames( ) throw (RuntimeException
, std::exception
)
176 Sequence
< OUString
> aServices( 2 );
177 aServices
[ 0 ] = "com.sun.star.table.CellRangeListSource";
178 aServices
[ 1 ] = "com.sun.star.form.binding.ListEntrySource";
182 CellRangeAddress
OCellListSource::getRangeAddress( ) const
184 OSL_PRECOND( m_xRange
.is(), "OCellListSource::getRangeAddress: invalid range!" );
186 CellRangeAddress aAddress
;
187 Reference
< XCellRangeAddressable
> xRangeAddress( m_xRange
, UNO_QUERY
);
188 if ( xRangeAddress
.is() )
189 aAddress
= xRangeAddress
->getRangeAddress( );
194 OUString
OCellListSource::getCellTextContent_noCheck( sal_Int32 _nRangeRelativeColumn
, sal_Int32 _nRangeRelativeRow
)
196 OSL_PRECOND( m_xRange
.is(), "OCellListSource::getRangeAddress: invalid range!" );
197 Reference
< XTextRange
> xCellText
;
199 xCellText
.set(xCellText
.query( m_xRange
->getCellByPosition( _nRangeRelativeColumn
, _nRangeRelativeRow
) ));
202 if ( xCellText
.is() )
203 sText
= xCellText
->getString();
208 sal_Int32 SAL_CALL
OCellListSource::getListEntryCount( ) throw (RuntimeException
, std::exception
)
210 ::osl::MutexGuard
aGuard( m_aMutex
);
214 CellRangeAddress
aAddress( getRangeAddress( ) );
215 return aAddress
.EndRow
- aAddress
.StartRow
+ 1;
219 OUString SAL_CALL
OCellListSource::getListEntry( sal_Int32 _nPosition
) throw (IndexOutOfBoundsException
, RuntimeException
, std::exception
)
221 ::osl::MutexGuard
aGuard( m_aMutex
);
225 if ( _nPosition
>= getListEntryCount() )
226 throw IndexOutOfBoundsException();
228 return getCellTextContent_noCheck( 0, _nPosition
);
232 Sequence
< OUString
> SAL_CALL
OCellListSource::getAllListEntries( ) throw (RuntimeException
, std::exception
)
234 ::osl::MutexGuard
aGuard( m_aMutex
);
238 Sequence
< OUString
> aAllEntries( getListEntryCount() );
239 OUString
* pAllEntries
= aAllEntries
.getArray();
240 for ( sal_Int32 i
= 0; i
< aAllEntries
.getLength(); ++i
)
242 *pAllEntries
++ = getCellTextContent_noCheck( 0, i
);
249 void SAL_CALL
OCellListSource::addListEntryListener( const Reference
< XListEntryListener
>& _rxListener
) throw (NullPointerException
, RuntimeException
, std::exception
)
251 ::osl::MutexGuard
aGuard( m_aMutex
);
255 if ( !_rxListener
.is() )
256 throw NullPointerException();
258 m_aListEntryListeners
.addInterface( _rxListener
);
262 void SAL_CALL
OCellListSource::removeListEntryListener( const Reference
< XListEntryListener
>& _rxListener
) throw (NullPointerException
, RuntimeException
, std::exception
)
264 ::osl::MutexGuard
aGuard( m_aMutex
);
268 if ( !_rxListener
.is() )
269 throw NullPointerException();
271 m_aListEntryListeners
.removeInterface( _rxListener
);
275 void SAL_CALL
OCellListSource::modified( const EventObject
& /* aEvent */ ) throw (RuntimeException
, std::exception
)
281 void OCellListSource::notifyModified()
284 aEvent
.Source
.set(*this);
286 ::cppu::OInterfaceIteratorHelper
aIter( m_aListEntryListeners
);
287 while ( aIter
.hasMoreElements() )
291 static_cast< XListEntryListener
* >( aIter
.next() )->allEntriesChanged( aEvent
);
293 catch( const RuntimeException
& )
297 catch( const Exception
& )
299 OSL_FAIL( "OCellListSource::notifyModified: caught a (non-runtime) exception!" );
306 void SAL_CALL
OCellListSource::disposing( const EventObject
& aEvent
) throw (RuntimeException
, std::exception
)
308 Reference
<XInterface
> xRangeInt( m_xRange
, UNO_QUERY
);
309 if ( xRangeInt
== aEvent
.Source
)
311 // release references to range object
317 void SAL_CALL
OCellListSource::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
, std::exception
)
319 if ( m_bInitialized
)
321 // TODO: error message
323 // get the cell address
324 CellRangeAddress aRangeAddress
;
325 bool bFoundAddress
= false;
327 const Any
* pLoop
= _rArguments
.getConstArray();
328 const Any
* pLoopEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
329 for ( ; ( pLoop
!= pLoopEnd
) && !bFoundAddress
; ++pLoop
)
332 if ( *pLoop
>>= aValue
)
334 if ( aValue
.Name
== "CellRange" )
336 if ( aValue
.Value
>>= aRangeAddress
)
337 bFoundAddress
= true;
342 if ( !bFoundAddress
)
343 // TODO: error message
346 // determine the range we're bound to
349 if ( m_xDocument
.is() )
351 // first the sheets collection
352 Reference
< XIndexAccess
> xSheets(m_xDocument
->getSheets( ), UNO_QUERY
);
353 OSL_ENSURE( xSheets
.is(), "OCellListSource::initialize: could not retrieve the sheets!" );
357 // the concrete sheet
358 Reference
< XCellRange
> xSheet(xSheets
->getByIndex( aRangeAddress
.Sheet
), UNO_QUERY
);
359 OSL_ENSURE( xSheet
.is(), "OCellListSource::initialize: NULL sheet, but no exception!" );
364 m_xRange
.set(xSheet
->getCellRangeByPosition(
365 aRangeAddress
.StartColumn
, aRangeAddress
.StartRow
,
366 aRangeAddress
.EndColumn
, aRangeAddress
.EndRow
));
367 OSL_ENSURE( Reference
< XCellRangeAddressable
>( m_xRange
, UNO_QUERY
).is(), "OCellListSource::initialize: either NULL range, or cell without address access!" );
372 catch( const Exception
& )
374 OSL_FAIL( "OCellListSource::initialize: caught an exception while retrieving the cell object!" );
378 if ( !m_xRange
.is() )
380 // TODO error message
382 Reference
<XModifyBroadcaster
> xBroadcaster( m_xRange
, UNO_QUERY
);
383 if ( xBroadcaster
.is() )
385 xBroadcaster
->addModifyListener( this );
388 // TODO: add as XEventListener to the cell range, so we get notified when it dies,
389 // and can dispose ourself then
391 // TODO: somehow add as listener so we get notified when the address of the cell range changes
392 // We need to forward this as change in our CellRange property to our property change listeners
394 // TODO: somehow add as listener to the cells in the range, so that we get notified
395 // when their content changes. We need to forward this to our list entry listeners then
397 // TODO: somehow add as listener so that we get notified of insertions and removals of rows in our
398 // range. In this case, we need to fire a change in our CellRange property, and additionally
399 // notify our XListEntryListeners
401 m_bInitialized
= true;
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */