Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 :
29 : //.........................................................................
30 : namespace calc
31 : {
32 : //.........................................................................
33 :
34 : #define PROP_HANDLE_RANGE_ADDRESS 1
35 :
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::lang;
38 : using namespace ::com::sun::star::table;
39 : using namespace ::com::sun::star::text;
40 : using namespace ::com::sun::star::sheet;
41 : using namespace ::com::sun::star::container;
42 : using namespace ::com::sun::star::beans;
43 : using namespace ::com::sun::star::util;
44 : using namespace ::com::sun::star::form::binding;
45 :
46 : //=====================================================================
47 : //= OCellListSource
48 : //=====================================================================
49 : DBG_NAME( OCellListSource )
50 : //---------------------------------------------------------------------
51 : #ifdef DBG_UTIL
52 : const char* OCellListSource::checkConsistency_static( const void* _pThis )
53 : {
54 : return static_cast< const OCellListSource* >( _pThis )->checkConsistency( );
55 : }
56 :
57 : const char* OCellListSource::checkConsistency( ) const
58 : {
59 : const char* pAssertion = NULL;
60 :
61 : // TODO: place any checks here to ensure consistency of this instance
62 :
63 : return pAssertion;
64 : }
65 : #endif
66 :
67 : //---------------------------------------------------------------------
68 0 : OCellListSource::OCellListSource( const Reference< XSpreadsheetDocument >& _rxDocument )
69 : :OCellListSource_Base( m_aMutex )
70 : ,OCellListSource_PBase( OCellListSource_Base::rBHelper )
71 : ,m_xDocument( _rxDocument )
72 : ,m_aListEntryListeners( m_aMutex )
73 0 : ,m_bInitialized( false )
74 : {
75 : DBG_CTOR( OCellListSource, checkConsistency_static );
76 :
77 : OSL_PRECOND( m_xDocument.is(), "OCellListSource::OCellListSource: invalid document!" );
78 :
79 : // register our property at the base class
80 0 : CellRangeAddress aInitialPropValue;
81 : registerPropertyNoMember(
82 : ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "CellRange" )),
83 : PROP_HANDLE_RANGE_ADDRESS,
84 : PropertyAttribute::BOUND | PropertyAttribute::READONLY,
85 0 : ::getCppuType( &aInitialPropValue ),
86 : &aInitialPropValue
87 0 : );
88 0 : }
89 :
90 : //---------------------------------------------------------------------
91 0 : OCellListSource::~OCellListSource( )
92 : {
93 0 : if ( !OCellListSource_Base::rBHelper.bDisposed )
94 : {
95 0 : acquire(); // prevent duplicate dtor
96 0 : dispose();
97 : }
98 :
99 : DBG_DTOR( OCellListSource, checkConsistency_static );
100 0 : }
101 :
102 : //--------------------------------------------------------------------
103 0 : IMPLEMENT_FORWARD_XINTERFACE2( OCellListSource, OCellListSource_Base, OCellListSource_PBase )
104 :
105 : //--------------------------------------------------------------------
106 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OCellListSource, OCellListSource_Base, OCellListSource_PBase )
107 :
108 : //--------------------------------------------------------------------
109 0 : void SAL_CALL OCellListSource::disposing()
110 : {
111 0 : ::osl::MutexGuard aGuard( m_aMutex );
112 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
113 :
114 0 : Reference<XModifyBroadcaster> xBroadcaster( m_xRange, UNO_QUERY );
115 0 : if ( xBroadcaster.is() )
116 : {
117 0 : xBroadcaster->removeModifyListener( this );
118 : }
119 :
120 0 : EventObject aDisposeEvent( *this );
121 0 : m_aListEntryListeners.disposeAndClear( aDisposeEvent );
122 :
123 0 : WeakAggComponentImplHelperBase::disposing();
124 :
125 : // TODO: clean up here whatever you need to clean up (e.g. revoking listeners etc.)
126 0 : }
127 :
128 : //--------------------------------------------------------------------
129 0 : Reference< XPropertySetInfo > SAL_CALL OCellListSource::getPropertySetInfo( ) throw(RuntimeException)
130 : {
131 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
132 0 : return createPropertySetInfo( getInfoHelper() ) ;
133 : }
134 :
135 : //--------------------------------------------------------------------
136 0 : ::cppu::IPropertyArrayHelper& SAL_CALL OCellListSource::getInfoHelper()
137 : {
138 0 : return *OCellListSource_PABase::getArrayHelper();
139 : }
140 :
141 : //--------------------------------------------------------------------
142 0 : ::cppu::IPropertyArrayHelper* OCellListSource::createArrayHelper( ) const
143 : {
144 0 : Sequence< Property > aProps;
145 0 : describeProperties( aProps );
146 0 : return new ::cppu::OPropertyArrayHelper(aProps);
147 : }
148 :
149 : //--------------------------------------------------------------------
150 0 : void SAL_CALL OCellListSource::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
151 : {
152 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
153 : OSL_ENSURE( _nHandle == PROP_HANDLE_RANGE_ADDRESS, "OCellListSource::getFastPropertyValue: invalid handle!" );
154 : // we only have this one property ....
155 : (void)_nHandle; // avoid warning in product version
156 :
157 0 : _rValue <<= getRangeAddress( );
158 0 : }
159 :
160 : //--------------------------------------------------------------------
161 0 : void OCellListSource::checkDisposed( ) const SAL_THROW( ( DisposedException ) )
162 : {
163 0 : if ( OCellListSource_Base::rBHelper.bInDispose || OCellListSource_Base::rBHelper.bDisposed )
164 0 : throw DisposedException();
165 : // TODO: is it worth having an error message here?
166 0 : }
167 :
168 : //--------------------------------------------------------------------
169 0 : void OCellListSource::checkInitialized() SAL_THROW( ( RuntimeException ) )
170 : {
171 0 : if ( !m_bInitialized )
172 0 : throw RuntimeException();
173 : // TODO: error message
174 0 : }
175 :
176 : //--------------------------------------------------------------------
177 0 : ::rtl::OUString SAL_CALL OCellListSource::getImplementationName( ) throw (RuntimeException)
178 : {
179 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.sheet.OCellListSource" ) );
180 : }
181 :
182 : //--------------------------------------------------------------------
183 0 : sal_Bool SAL_CALL OCellListSource::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
184 : {
185 0 : Sequence< ::rtl::OUString > aSupportedServices( getSupportedServiceNames() );
186 0 : const ::rtl::OUString* pLookup = aSupportedServices.getConstArray();
187 0 : const ::rtl::OUString* pLookupEnd = aSupportedServices.getConstArray() + aSupportedServices.getLength();
188 0 : while ( pLookup != pLookupEnd )
189 0 : if ( *pLookup++ == _rServiceName )
190 0 : return sal_True;
191 :
192 0 : return false;
193 : }
194 :
195 : //--------------------------------------------------------------------
196 0 : Sequence< ::rtl::OUString > SAL_CALL OCellListSource::getSupportedServiceNames( ) throw (RuntimeException)
197 : {
198 0 : Sequence< ::rtl::OUString > aServices( 2 );
199 0 : aServices[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.table.CellRangeListSource" ) );
200 0 : aServices[ 1 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.binding.ListEntrySource" ) );
201 0 : return aServices;
202 : }
203 :
204 : //--------------------------------------------------------------------
205 0 : CellRangeAddress OCellListSource::getRangeAddress( ) const
206 : {
207 : OSL_PRECOND( m_xRange.is(), "OCellListSource::getRangeAddress: invalid range!" );
208 :
209 0 : CellRangeAddress aAddress;
210 0 : Reference< XCellRangeAddressable > xRangeAddress( m_xRange, UNO_QUERY );
211 0 : if ( xRangeAddress.is() )
212 0 : aAddress = xRangeAddress->getRangeAddress( );
213 0 : return aAddress;
214 : }
215 :
216 : //--------------------------------------------------------------------
217 0 : ::rtl::OUString OCellListSource::getCellTextContent_noCheck( sal_Int32 _nRangeRelativeColumn, sal_Int32 _nRangeRelativeRow )
218 : {
219 : OSL_PRECOND( m_xRange.is(), "OCellListSource::getRangeAddress: invalid range!" );
220 0 : Reference< XTextRange > xCellText;
221 0 : if ( m_xRange.is() )
222 0 : xCellText.set(xCellText.query( m_xRange->getCellByPosition( _nRangeRelativeColumn, _nRangeRelativeRow ) ));
223 :
224 0 : ::rtl::OUString sText;
225 0 : if ( xCellText.is() )
226 0 : sText = xCellText->getString();
227 0 : return sText;
228 : }
229 :
230 : //--------------------------------------------------------------------
231 0 : sal_Int32 SAL_CALL OCellListSource::getListEntryCount( ) throw (RuntimeException)
232 : {
233 0 : ::osl::MutexGuard aGuard( m_aMutex );
234 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
235 0 : checkDisposed();
236 0 : checkInitialized();
237 :
238 0 : CellRangeAddress aAddress( getRangeAddress( ) );
239 0 : return aAddress.EndRow - aAddress.StartRow + 1;
240 : }
241 :
242 : //--------------------------------------------------------------------
243 0 : ::rtl::OUString SAL_CALL OCellListSource::getListEntry( sal_Int32 _nPosition ) throw (IndexOutOfBoundsException, RuntimeException)
244 : {
245 0 : ::osl::MutexGuard aGuard( m_aMutex );
246 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
247 0 : checkDisposed();
248 0 : checkInitialized();
249 :
250 0 : if ( _nPosition >= getListEntryCount() )
251 0 : throw IndexOutOfBoundsException();
252 :
253 0 : return getCellTextContent_noCheck( 0, _nPosition );
254 : }
255 :
256 : //--------------------------------------------------------------------
257 0 : Sequence< ::rtl::OUString > SAL_CALL OCellListSource::getAllListEntries( ) throw (RuntimeException)
258 : {
259 0 : ::osl::MutexGuard aGuard( m_aMutex );
260 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
261 0 : checkDisposed();
262 0 : checkInitialized();
263 :
264 0 : Sequence< ::rtl::OUString > aAllEntries( getListEntryCount() );
265 0 : ::rtl::OUString* pAllEntries = aAllEntries.getArray();
266 0 : for ( sal_Int32 i = 0; i < aAllEntries.getLength(); ++i )
267 : {
268 0 : *pAllEntries++ = getCellTextContent_noCheck( 0, i );
269 : }
270 :
271 0 : return aAllEntries;
272 : }
273 :
274 : //--------------------------------------------------------------------
275 0 : void SAL_CALL OCellListSource::addListEntryListener( const Reference< XListEntryListener >& _rxListener ) throw (NullPointerException, RuntimeException)
276 : {
277 0 : ::osl::MutexGuard aGuard( m_aMutex );
278 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
279 0 : checkDisposed();
280 0 : checkInitialized();
281 :
282 0 : if ( !_rxListener.is() )
283 0 : throw NullPointerException();
284 :
285 0 : m_aListEntryListeners.addInterface( _rxListener );
286 0 : }
287 :
288 : //--------------------------------------------------------------------
289 0 : void SAL_CALL OCellListSource::removeListEntryListener( const Reference< XListEntryListener >& _rxListener ) throw (NullPointerException, RuntimeException)
290 : {
291 0 : ::osl::MutexGuard aGuard( m_aMutex );
292 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
293 0 : checkDisposed();
294 0 : checkInitialized();
295 :
296 0 : if ( !_rxListener.is() )
297 0 : throw NullPointerException();
298 :
299 0 : m_aListEntryListeners.removeInterface( _rxListener );
300 0 : }
301 :
302 : //--------------------------------------------------------------------
303 0 : void SAL_CALL OCellListSource::modified( const EventObject& /* aEvent */ ) throw (RuntimeException)
304 : {
305 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
306 :
307 0 : notifyModified();
308 0 : }
309 :
310 : //--------------------------------------------------------------------
311 0 : void OCellListSource::notifyModified()
312 : {
313 0 : EventObject aEvent;
314 0 : aEvent.Source.set(*this);
315 :
316 0 : ::cppu::OInterfaceIteratorHelper aIter( m_aListEntryListeners );
317 0 : while ( aIter.hasMoreElements() )
318 : {
319 : try
320 : {
321 0 : static_cast< XListEntryListener* >( aIter.next() )->allEntriesChanged( aEvent );
322 : }
323 0 : catch( const RuntimeException& )
324 : {
325 : // silent this
326 : }
327 0 : catch( const Exception& )
328 : {
329 : OSL_FAIL( "OCellListSource::notifyModified: caught a (non-runtime) exception!" );
330 : }
331 0 : }
332 :
333 0 : }
334 :
335 : //--------------------------------------------------------------------
336 0 : void SAL_CALL OCellListSource::disposing( const EventObject& aEvent ) throw (RuntimeException)
337 : {
338 : DBG_CHKTHIS( OCellListSource, checkConsistency_static );
339 :
340 0 : Reference<XInterface> xRangeInt( m_xRange, UNO_QUERY );
341 0 : if ( xRangeInt == aEvent.Source )
342 : {
343 : // release references to range object
344 0 : m_xRange.clear();
345 0 : }
346 0 : }
347 :
348 : //--------------------------------------------------------------------
349 0 : void SAL_CALL OCellListSource::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
350 : {
351 0 : if ( m_bInitialized )
352 0 : throw Exception();
353 : // TODO: error message
354 :
355 : // get the cell address
356 0 : CellRangeAddress aRangeAddress;
357 0 : sal_Bool bFoundAddress = false;
358 :
359 0 : const Any* pLoop = _rArguments.getConstArray();
360 0 : const Any* pLoopEnd = _rArguments.getConstArray() + _rArguments.getLength();
361 0 : for ( ; ( pLoop != pLoopEnd ) && !bFoundAddress; ++pLoop )
362 : {
363 0 : NamedValue aValue;
364 0 : if ( *pLoop >>= aValue )
365 : {
366 0 : if ( aValue.Name == "CellRange" )
367 : {
368 0 : if ( aValue.Value >>= aRangeAddress )
369 0 : bFoundAddress = sal_True;
370 : }
371 : }
372 0 : }
373 :
374 0 : if ( !bFoundAddress )
375 : // TODO: error message
376 0 : throw Exception();
377 :
378 : // determine the range we're bound to
379 : try
380 : {
381 0 : if ( m_xDocument.is() )
382 : {
383 : // first the sheets collection
384 0 : Reference< XIndexAccess > xSheets(m_xDocument->getSheets( ), UNO_QUERY);
385 : OSL_ENSURE( xSheets.is(), "OCellListSource::initialize: could not retrieve the sheets!" );
386 :
387 0 : if ( xSheets.is() )
388 : {
389 : // the concrete sheet
390 0 : Reference< XCellRange > xSheet(xSheets->getByIndex( aRangeAddress.Sheet ), UNO_QUERY);
391 : OSL_ENSURE( xSheet.is(), "OCellListSource::initialize: NULL sheet, but no exception!" );
392 :
393 : // the concrete cell
394 0 : if ( xSheet.is() )
395 : {
396 0 : m_xRange.set(xSheet->getCellRangeByPosition(
397 : aRangeAddress.StartColumn, aRangeAddress.StartRow,
398 0 : aRangeAddress.EndColumn, aRangeAddress.EndRow));
399 : OSL_ENSURE( Reference< XCellRangeAddressable >( m_xRange, UNO_QUERY ).is(), "OCellListSource::initialize: either NULL range, or cell without address access!" );
400 0 : }
401 0 : }
402 : }
403 : }
404 0 : catch( const Exception& )
405 : {
406 : OSL_FAIL( "OCellListSource::initialize: caught an exception while retrieving the cell object!" );
407 : }
408 :
409 :
410 0 : if ( !m_xRange.is() )
411 0 : throw Exception();
412 : // TODO error message
413 :
414 0 : Reference<XModifyBroadcaster> xBroadcaster( m_xRange, UNO_QUERY );
415 0 : if ( xBroadcaster.is() )
416 : {
417 0 : xBroadcaster->addModifyListener( this );
418 : }
419 :
420 : // TODO: add as XEventListener to the cell range, so we get notified when it dies,
421 : // and can dispose ourself then
422 :
423 : // TODO: somehow add as listener so we get notified when the address of the cell range changes
424 : // We need to forward this as change in our CellRange property to our property change listeners
425 :
426 : // TODO: somehow add as listener to the cells in the range, so that we get notified
427 : // when their content changes. We need to forward this to our list entry listeners then
428 :
429 : // TODO: somehow add as listener so that we get notified of insertions and removals of rows in our
430 : // range. In this case, we need to fire a change in our CellRange property, and additionally
431 : // notify our XListEntryListeners
432 :
433 0 : m_bInitialized = sal_True;
434 0 : }
435 :
436 : //.........................................................................
437 : } // namespace calc
438 : //.........................................................................
439 :
440 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|