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>
33 #define PROP_HANDLE_RANGE_ADDRESS 1
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::table
;
38 using namespace ::com::sun::star::text
;
39 using namespace ::com::sun::star::sheet
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::util
;
43 using namespace ::com::sun::star::form::binding
;
45 OCellListSource::OCellListSource( const Reference
< XSpreadsheetDocument
>& _rxDocument
)
46 :OCellListSource_Base( m_aMutex
)
47 ,OCellListSource_PBase( OCellListSource_Base::rBHelper
)
48 ,m_xDocument( _rxDocument
)
49 ,m_aListEntryListeners( m_aMutex
)
50 ,m_bInitialized( false )
52 OSL_PRECOND( m_xDocument
.is(), "OCellListSource::OCellListSource: invalid document!" );
54 // register our property at the base class
55 CellRangeAddress aInitialPropValue
;
56 registerPropertyNoMember(
57 OUString( "CellRange" ),
58 PROP_HANDLE_RANGE_ADDRESS
,
59 PropertyAttribute::BOUND
| PropertyAttribute::READONLY
,
60 cppu::UnoType
<decltype(aInitialPropValue
)>::get(),
65 OCellListSource::~OCellListSource( )
67 if ( !OCellListSource_Base::rBHelper
.bDisposed
)
69 acquire(); // prevent duplicate dtor
74 IMPLEMENT_FORWARD_XINTERFACE2( OCellListSource
, OCellListSource_Base
, OCellListSource_PBase
)
76 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OCellListSource
, OCellListSource_Base
, OCellListSource_PBase
)
78 void SAL_CALL
OCellListSource::disposing()
80 ::osl::MutexGuard
aGuard( m_aMutex
);
82 Reference
<XModifyBroadcaster
> xBroadcaster( m_xRange
, UNO_QUERY
);
83 if ( xBroadcaster
.is() )
85 xBroadcaster
->removeModifyListener( this );
88 EventObject
aDisposeEvent( *this );
89 m_aListEntryListeners
.disposeAndClear( aDisposeEvent
);
91 WeakAggComponentImplHelperBase::disposing();
93 // TODO: clean up here whatever you need to clean up (e.g. revoking listeners etc.)
96 Reference
< XPropertySetInfo
> SAL_CALL
OCellListSource::getPropertySetInfo( ) throw(RuntimeException
, std::exception
)
98 return createPropertySetInfo( getInfoHelper() ) ;
101 ::cppu::IPropertyArrayHelper
& SAL_CALL
OCellListSource::getInfoHelper()
103 return *OCellListSource_PABase::getArrayHelper();
106 ::cppu::IPropertyArrayHelper
* OCellListSource::createArrayHelper( ) const
108 Sequence
< Property
> aProps
;
109 describeProperties( aProps
);
110 return new ::cppu::OPropertyArrayHelper(aProps
);
113 void SAL_CALL
OCellListSource::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
115 OSL_ENSURE( _nHandle
== PROP_HANDLE_RANGE_ADDRESS
, "OCellListSource::getFastPropertyValue: invalid handle!" );
116 // we only have this one property ....
117 (void)_nHandle
; // avoid warning in product version
119 _rValue
<<= getRangeAddress( );
122 void OCellListSource::checkDisposed( ) const
124 if ( OCellListSource_Base::rBHelper
.bInDispose
|| OCellListSource_Base::rBHelper
.bDisposed
)
125 throw DisposedException();
126 // TODO: is it worth having an error message here?
129 void OCellListSource::checkInitialized()
131 if ( !m_bInitialized
)
132 throw RuntimeException();
133 // TODO: error message
136 OUString SAL_CALL
OCellListSource::getImplementationName( ) throw (RuntimeException
, std::exception
)
138 return OUString( "com.sun.star.comp.sheet.OCellListSource" );
141 sal_Bool SAL_CALL
OCellListSource::supportsService( const OUString
& _rServiceName
) throw (RuntimeException
, std::exception
)
143 return cppu::supportsService(this, _rServiceName
);
146 Sequence
< OUString
> SAL_CALL
OCellListSource::getSupportedServiceNames( ) throw (RuntimeException
, std::exception
)
148 Sequence
< OUString
> aServices( 2 );
149 aServices
[ 0 ] = "com.sun.star.table.CellRangeListSource";
150 aServices
[ 1 ] = "com.sun.star.form.binding.ListEntrySource";
154 CellRangeAddress
OCellListSource::getRangeAddress( ) const
156 OSL_PRECOND( m_xRange
.is(), "OCellListSource::getRangeAddress: invalid range!" );
158 CellRangeAddress aAddress
;
159 Reference
< XCellRangeAddressable
> xRangeAddress( m_xRange
, UNO_QUERY
);
160 if ( xRangeAddress
.is() )
161 aAddress
= xRangeAddress
->getRangeAddress( );
165 OUString
OCellListSource::getCellTextContent_noCheck( sal_Int32 _nRangeRelativeColumn
, sal_Int32 _nRangeRelativeRow
)
167 OSL_PRECOND( m_xRange
.is(), "OCellListSource::getRangeAddress: invalid range!" );
168 Reference
< XTextRange
> xCellText
;
170 xCellText
.set(m_xRange
->getCellByPosition( _nRangeRelativeColumn
, _nRangeRelativeRow
), css::uno::UNO_QUERY
);
173 if ( xCellText
.is() )
174 sText
= xCellText
->getString();
178 sal_Int32 SAL_CALL
OCellListSource::getListEntryCount( ) throw (RuntimeException
, std::exception
)
180 ::osl::MutexGuard
aGuard( m_aMutex
);
184 CellRangeAddress
aAddress( getRangeAddress( ) );
185 return aAddress
.EndRow
- aAddress
.StartRow
+ 1;
188 OUString SAL_CALL
OCellListSource::getListEntry( sal_Int32 _nPosition
) throw (IndexOutOfBoundsException
, RuntimeException
, std::exception
)
190 ::osl::MutexGuard
aGuard( m_aMutex
);
194 if ( _nPosition
>= getListEntryCount() )
195 throw IndexOutOfBoundsException();
197 return getCellTextContent_noCheck( 0, _nPosition
);
200 Sequence
< OUString
> SAL_CALL
OCellListSource::getAllListEntries( ) throw (RuntimeException
, std::exception
)
202 ::osl::MutexGuard
aGuard( m_aMutex
);
206 Sequence
< OUString
> aAllEntries( getListEntryCount() );
207 OUString
* pAllEntries
= aAllEntries
.getArray();
208 for ( sal_Int32 i
= 0; i
< aAllEntries
.getLength(); ++i
)
210 *pAllEntries
++ = getCellTextContent_noCheck( 0, i
);
216 void SAL_CALL
OCellListSource::addListEntryListener( const Reference
< XListEntryListener
>& _rxListener
) throw (NullPointerException
, RuntimeException
, std::exception
)
218 ::osl::MutexGuard
aGuard( m_aMutex
);
222 if ( !_rxListener
.is() )
223 throw NullPointerException();
225 m_aListEntryListeners
.addInterface( _rxListener
);
228 void SAL_CALL
OCellListSource::removeListEntryListener( const Reference
< XListEntryListener
>& _rxListener
) throw (NullPointerException
, RuntimeException
, std::exception
)
230 ::osl::MutexGuard
aGuard( m_aMutex
);
234 if ( !_rxListener
.is() )
235 throw NullPointerException();
237 m_aListEntryListeners
.removeInterface( _rxListener
);
240 void SAL_CALL
OCellListSource::modified( const EventObject
& /* aEvent */ ) throw (RuntimeException
, std::exception
)
245 void OCellListSource::notifyModified()
248 aEvent
.Source
.set(*this);
250 ::cppu::OInterfaceIteratorHelper
aIter( m_aListEntryListeners
);
251 while ( aIter
.hasMoreElements() )
255 static_cast< XListEntryListener
* >( aIter
.next() )->allEntriesChanged( aEvent
);
257 catch( const RuntimeException
& )
261 catch( const Exception
& )
263 OSL_FAIL( "OCellListSource::notifyModified: caught a (non-runtime) exception!" );
269 void SAL_CALL
OCellListSource::disposing( const EventObject
& aEvent
) throw (RuntimeException
, std::exception
)
271 Reference
<XInterface
> xRangeInt( m_xRange
, UNO_QUERY
);
272 if ( xRangeInt
== aEvent
.Source
)
274 // release references to range object
279 void SAL_CALL
OCellListSource::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
, std::exception
)
281 if ( m_bInitialized
)
283 // TODO: error message
285 // get the cell address
286 CellRangeAddress aRangeAddress
;
287 bool bFoundAddress
= false;
289 const Any
* pLoop
= _rArguments
.getConstArray();
290 const Any
* pLoopEnd
= _rArguments
.getConstArray() + _rArguments
.getLength();
291 for ( ; ( pLoop
!= pLoopEnd
) && !bFoundAddress
; ++pLoop
)
294 if ( *pLoop
>>= aValue
)
296 if ( aValue
.Name
== "CellRange" )
298 if ( aValue
.Value
>>= aRangeAddress
)
299 bFoundAddress
= true;
304 if ( !bFoundAddress
)
305 // TODO: error message
308 // determine the range we're bound to
311 if ( m_xDocument
.is() )
313 // first the sheets collection
314 Reference
< XIndexAccess
> xSheets(m_xDocument
->getSheets( ), UNO_QUERY
);
315 OSL_ENSURE( xSheets
.is(), "OCellListSource::initialize: could not retrieve the sheets!" );
319 // the concrete sheet
320 Reference
< XCellRange
> xSheet(xSheets
->getByIndex( aRangeAddress
.Sheet
), UNO_QUERY
);
321 OSL_ENSURE( xSheet
.is(), "OCellListSource::initialize: NULL sheet, but no exception!" );
326 m_xRange
.set(xSheet
->getCellRangeByPosition(
327 aRangeAddress
.StartColumn
, aRangeAddress
.StartRow
,
328 aRangeAddress
.EndColumn
, aRangeAddress
.EndRow
));
329 OSL_ENSURE( Reference
< XCellRangeAddressable
>( m_xRange
, UNO_QUERY
).is(), "OCellListSource::initialize: either NULL range, or cell without address access!" );
334 catch( const Exception
& )
336 OSL_FAIL( "OCellListSource::initialize: caught an exception while retrieving the cell object!" );
339 if ( !m_xRange
.is() )
341 // TODO error message
343 Reference
<XModifyBroadcaster
> xBroadcaster( m_xRange
, UNO_QUERY
);
344 if ( xBroadcaster
.is() )
346 xBroadcaster
->addModifyListener( this );
349 // TODO: add as XEventListener to the cell range, so we get notified when it dies,
350 // and can dispose ourself then
352 // TODO: somehow add as listener so we get notified when the address of the cell range changes
353 // We need to forward this as change in our CellRange property to our property change listeners
355 // TODO: somehow add as listener to the cells in the range, so that we get notified
356 // when their content changes. We need to forward this to our list entry listeners then
358 // TODO: somehow add as listener so that we get notified of insertions and removals of rows in our
359 // range. In this case, we need to fire a change in our CellRange property, and additionally
360 // notify our XListEntryListeners
362 m_bInitialized
= true;
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */