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