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 "initguard.hxx"
21 :
22 : #include <com/sun/star/i18n/Collator.hpp>
23 : #include <com/sun/star/i18n/XCollator.hpp>
24 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 : #include <com/sun/star/lang/XInitialization.hpp>
26 : #include <com/sun/star/lang/XServiceInfo.hpp>
27 : #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
28 : #include <com/sun/star/awt/grid/XGridDataListener.hpp>
29 : #include <com/sun/star/awt/grid/XSortableMutableGridDataModel.hpp>
30 :
31 : #include <cppuhelper/basemutex.hxx>
32 : #include <cppuhelper/compbase3.hxx>
33 : #include <cppuhelper/implbase1.hxx>
34 : #include <comphelper/anycompare.hxx>
35 : #include <cppuhelper/supportsservice.hxx>
36 : #include <cppuhelper/typeprovider.hxx>
37 : #include <tools/diagnose_ex.h>
38 : #include <tools/debug.hxx>
39 : #include <vcl/svapp.hxx>
40 : #include <vcl/settings.hxx>
41 :
42 : using namespace css::awt;
43 : using namespace css::awt::grid;
44 : using namespace css::i18n;
45 : using namespace css::lang;
46 : using namespace css::ucb;
47 : using namespace css::uno;
48 : using namespace toolkit;
49 :
50 : namespace {
51 :
52 : class SortableGridDataModel;
53 : typedef InitGuard< SortableGridDataModel > MethodGuard;
54 :
55 : typedef ::cppu::WeakComponentImplHelper3 < css::awt::grid::XSortableMutableGridDataModel
56 : , css::lang::XServiceInfo
57 : , css::lang::XInitialization
58 : > SortableGridDataModel_Base;
59 : typedef ::cppu::ImplHelper1 < css::awt::grid::XGridDataListener
60 : > SortableGridDataModel_PrivateBase;
61 : class SortableGridDataModel :public ::cppu::BaseMutex
62 : ,public SortableGridDataModel_Base
63 : ,public SortableGridDataModel_PrivateBase
64 : {
65 : public:
66 : SortableGridDataModel( const css::uno::Reference< css::uno::XComponentContext > & rxContext );
67 : SortableGridDataModel( SortableGridDataModel const & i_copySource );
68 :
69 0 : bool isInitialized() const { return m_isInitialized; }
70 :
71 : protected:
72 : virtual ~SortableGridDataModel();
73 :
74 : public:
75 : // XSortableGridData
76 : virtual void SAL_CALL sortByColumn( ::sal_Int32 ColumnIndex, sal_Bool SortAscending ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
77 : virtual void SAL_CALL removeColumnSort( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
78 : virtual css::beans::Pair< ::sal_Int32, sal_Bool > SAL_CALL getCurrentSortOrder( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
79 :
80 : // XMutableGridDataModel
81 : virtual void SAL_CALL addRow( const css::uno::Any& Heading, const css::uno::Sequence< css::uno::Any >& Data ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
82 : virtual void SAL_CALL addRows( const css::uno::Sequence< css::uno::Any >& Headings, const css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& Data ) throw (css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
83 : virtual void SAL_CALL insertRow( ::sal_Int32 i_index, const css::uno::Any& i_heading, const css::uno::Sequence< css::uno::Any >& Data ) throw (css::uno::RuntimeException, css::lang::IndexOutOfBoundsException, std::exception) SAL_OVERRIDE;
84 : virtual void SAL_CALL insertRows( ::sal_Int32 i_index, const css::uno::Sequence< css::uno::Any>& Headings, const css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& Data ) throw (css::lang::IllegalArgumentException, css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
85 : virtual void SAL_CALL removeRow( ::sal_Int32 RowIndex ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
86 : virtual void SAL_CALL removeAllRows( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual void SAL_CALL updateCellData( ::sal_Int32 ColumnIndex, ::sal_Int32 RowIndex, const css::uno::Any& Value ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
88 : virtual void SAL_CALL updateRowData( const css::uno::Sequence< ::sal_Int32 >& ColumnIndexes, ::sal_Int32 RowIndex, const css::uno::Sequence< css::uno::Any >& Values ) throw (css::lang::IndexOutOfBoundsException, css::lang::IllegalArgumentException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
89 : virtual void SAL_CALL updateRowHeading( ::sal_Int32 RowIndex, const css::uno::Any& Heading ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
90 : virtual void SAL_CALL updateCellToolTip( ::sal_Int32 ColumnIndex, ::sal_Int32 RowIndex, const css::uno::Any& Value ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
91 : virtual void SAL_CALL updateRowToolTip( ::sal_Int32 RowIndex, const css::uno::Any& Value ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
92 : virtual void SAL_CALL addGridDataListener( const css::uno::Reference< css::awt::grid::XGridDataListener >& Listener ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
93 : virtual void SAL_CALL removeGridDataListener( const css::uno::Reference< css::awt::grid::XGridDataListener >& Listener ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
94 :
95 : // XGridDataModel
96 : virtual ::sal_Int32 SAL_CALL getRowCount() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
97 : virtual ::sal_Int32 SAL_CALL getColumnCount() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
98 : virtual css::uno::Any SAL_CALL getCellData( ::sal_Int32 Column, ::sal_Int32 RowIndex ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
99 : virtual css::uno::Any SAL_CALL getCellToolTip( ::sal_Int32 Column, ::sal_Int32 RowIndex ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
100 : virtual css::uno::Any SAL_CALL getRowHeading( ::sal_Int32 RowIndex ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
101 : virtual css::uno::Sequence< css::uno::Any > SAL_CALL getRowData( ::sal_Int32 RowIndex ) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
102 :
103 : // OComponentHelper
104 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
105 :
106 : // XCloneable
107 : virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
108 :
109 : // XServiceInfo
110 : virtual OUString SAL_CALL getImplementationName( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
111 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
112 : virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
113 :
114 : // XInitialization
115 : virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) throw (css::uno::Exception, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
116 :
117 : // XGridDataListener
118 : virtual void SAL_CALL rowsInserted( const css::awt::grid::GridDataEvent& Event ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
119 : virtual void SAL_CALL rowsRemoved( const css::awt::grid::GridDataEvent& Event ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
120 : virtual void SAL_CALL dataChanged( const css::awt::grid::GridDataEvent& Event ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
121 : virtual void SAL_CALL rowHeadingChanged( const css::awt::grid::GridDataEvent& Event ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
122 :
123 : // XEventListener
124 : virtual void SAL_CALL disposing( const css::lang::EventObject& i_event ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
125 :
126 : // XInterface
127 : virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type& aType ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
128 : virtual void SAL_CALL acquire( ) throw () SAL_OVERRIDE;
129 : virtual void SAL_CALL release( ) throw () SAL_OVERRIDE;
130 :
131 : // XTypeProvider
132 : virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
133 : virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL getImplementationId( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
134 :
135 : private:
136 : /** translates the given public index into one to be passed to our delegator
137 : @throws css::lang::IndexOutOfBoundsException
138 : if the given index does not denote a valid row
139 : */
140 : ::sal_Int32 impl_getPrivateRowIndex_throw( ::sal_Int32 const i_publicRowIndex ) const;
141 :
142 : /** translates the given private row index to a public one
143 : */
144 : ::sal_Int32 impl_getPublicRowIndex_nothrow( ::sal_Int32 const i_privateRowIndex ) const;
145 :
146 0 : inline bool impl_isSorted_nothrow() const
147 : {
148 0 : return m_currentSortColumn >= 0;
149 : }
150 :
151 : /** rebuilds the index translation structure.
152 :
153 : Neither <member>m_currentSortColumn</member> nor <member>m_sortAscending</member> are touched by this method.
154 : Also, the given column index is not checked, this is the responsibility of the caller.
155 : */
156 : bool impl_reIndex_nothrow( ::sal_Int32 const i_columnIndex, bool const i_sortAscending );
157 :
158 : /** translates the given event, obtained from our delegator, to a version which can be broadcasted to our own
159 : clients.
160 : */
161 : css::awt::grid::GridDataEvent
162 : impl_createPublicEvent( css::awt::grid::GridDataEvent const & i_originalEvent ) const;
163 :
164 : /** broadcasts the given event to our registered XGridDataListeners
165 : */
166 : void impl_broadcast(
167 : void ( SAL_CALL css::awt::grid::XGridDataListener::*i_listenerMethod )( const css::awt::grid::GridDataEvent & ),
168 : css::awt::grid::GridDataEvent const & i_publicEvent,
169 : MethodGuard& i_instanceLock
170 : );
171 :
172 : /** rebuilds our indexes, notifying row removal and row addition events
173 :
174 : First, a rowsRemoved event is notified to our registered listeners. Then, the index translation tables are
175 : rebuilt, and a rowsInserted event is notified.
176 :
177 : Only to be called when we're sorted.
178 : */
179 : void impl_rebuildIndexesAndNotify( MethodGuard& i_instanceLock );
180 :
181 : /** removes the current sorting, and notifies a change of all data
182 : */
183 : void impl_removeColumnSort( MethodGuard& i_instanceLock );
184 :
185 : /** removes the current sorting, without any broadcast
186 : */
187 : void impl_removeColumnSort_noBroadcast();
188 :
189 : private:
190 : css::uno::Reference< css::uno::XComponentContext > m_xContext;
191 : bool m_isInitialized;
192 : css::uno::Reference< css::awt::grid::XMutableGridDataModel > m_delegator;
193 : css::uno::Reference< css::i18n::XCollator > m_collator;
194 : ::sal_Int32 m_currentSortColumn;
195 : bool m_sortAscending;
196 : ::std::vector< ::sal_Int32 > m_publicToPrivateRowIndex;
197 : ::std::vector< ::sal_Int32 > m_privateToPublicRowIndex;
198 : };
199 :
200 : namespace
201 : {
202 : template< class STLCONTAINER >
203 0 : static void lcl_clear( STLCONTAINER& i_container )
204 : {
205 0 : STLCONTAINER empty;
206 0 : empty.swap( i_container );
207 0 : }
208 : }
209 :
210 0 : SortableGridDataModel::SortableGridDataModel( Reference< XComponentContext > const & rxContext )
211 : :SortableGridDataModel_Base( m_aMutex )
212 : ,SortableGridDataModel_PrivateBase()
213 : ,m_xContext( rxContext )
214 : ,m_isInitialized( false )
215 : ,m_delegator()
216 : ,m_collator()
217 : ,m_currentSortColumn( -1 )
218 : ,m_sortAscending( true )
219 : ,m_publicToPrivateRowIndex()
220 0 : ,m_privateToPublicRowIndex()
221 : {
222 0 : }
223 :
224 :
225 0 : SortableGridDataModel::SortableGridDataModel( SortableGridDataModel const & i_copySource )
226 : :cppu::BaseMutex()
227 : ,SortableGridDataModel_Base( m_aMutex )
228 : ,SortableGridDataModel_PrivateBase()
229 : ,m_xContext( i_copySource.m_xContext )
230 : ,m_isInitialized( true )
231 : ,m_delegator()
232 : ,m_collator( i_copySource.m_collator )
233 : ,m_currentSortColumn( i_copySource.m_currentSortColumn )
234 : ,m_sortAscending( i_copySource.m_sortAscending )
235 : ,m_publicToPrivateRowIndex( i_copySource.m_publicToPrivateRowIndex )
236 0 : ,m_privateToPublicRowIndex( i_copySource.m_privateToPublicRowIndex )
237 : {
238 0 : ENSURE_OR_THROW( i_copySource.m_delegator.is(),
239 : "not expected to be called for a disposed copy source!" );
240 0 : m_delegator.set( i_copySource.m_delegator->createClone(), UNO_QUERY_THROW );
241 0 : }
242 :
243 :
244 0 : SortableGridDataModel::~SortableGridDataModel()
245 : {
246 0 : if ( !rBHelper.bDisposed )
247 : {
248 0 : acquire();
249 0 : dispose();
250 : }
251 0 : }
252 :
253 :
254 0 : Any SAL_CALL SortableGridDataModel::queryInterface( const Type& aType ) throw (RuntimeException, std::exception)
255 : {
256 0 : Any aReturn( SortableGridDataModel_Base::queryInterface( aType ) );
257 0 : if ( !aReturn.hasValue() )
258 0 : aReturn = SortableGridDataModel_PrivateBase::queryInterface( aType );
259 0 : return aReturn;
260 : }
261 :
262 :
263 0 : void SAL_CALL SortableGridDataModel::acquire( ) throw ()
264 : {
265 0 : SortableGridDataModel_Base::acquire();
266 0 : }
267 :
268 :
269 0 : void SAL_CALL SortableGridDataModel::release( ) throw ()
270 : {
271 0 : SortableGridDataModel_Base::release();
272 0 : }
273 :
274 :
275 0 : Sequence< Type > SAL_CALL SortableGridDataModel::getTypes( ) throw (RuntimeException, std::exception)
276 : {
277 0 : return SortableGridDataModel_Base::getTypes();
278 : // don't expose the types got via SortableGridDataModel_PrivateBase - they're private, after all
279 : }
280 :
281 :
282 0 : Sequence< ::sal_Int8 > SAL_CALL SortableGridDataModel::getImplementationId( ) throw (RuntimeException, std::exception)
283 : {
284 0 : return css::uno::Sequence<sal_Int8>();
285 : }
286 :
287 :
288 : namespace
289 : {
290 0 : Reference< XCollator > lcl_loadDefaultCollator_throw( const Reference<XComponentContext> & rxContext )
291 : {
292 0 : Reference< XCollator > const xCollator = Collator::create( rxContext );
293 0 : xCollator->loadDefaultCollator( Application::GetSettings().GetLanguageTag().getLocale(), 0 );
294 0 : return xCollator;
295 : }
296 : }
297 :
298 :
299 0 : void SAL_CALL SortableGridDataModel::initialize( const Sequence< Any >& i_arguments ) throw (Exception, RuntimeException, std::exception)
300 : {
301 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
302 :
303 0 : if ( m_delegator.is() )
304 0 : throw AlreadyInitializedException( OUString(), *this );
305 :
306 0 : Reference< XMutableGridDataModel > xDelegator;
307 0 : Reference< XCollator > xCollator;
308 0 : switch ( i_arguments.getLength() )
309 : {
310 : case 1: // SortableGridDataModel.create( XMutableGridDataModel )
311 0 : xDelegator.set( i_arguments[0], UNO_QUERY );
312 0 : xCollator = lcl_loadDefaultCollator_throw( m_xContext );
313 0 : break;
314 :
315 : case 2: // SortableGridDataModel.createWithCollator( XMutableGridDataModel, XCollator )
316 0 : xDelegator.set( i_arguments[0], UNO_QUERY );
317 0 : xCollator.set( i_arguments[1], UNO_QUERY );
318 0 : if ( !xCollator.is() )
319 0 : throw IllegalArgumentException( OUString(), *this, 2 );
320 0 : break;
321 : }
322 0 : if ( !xDelegator.is() )
323 0 : throw IllegalArgumentException( OUString(), *this, 1 );
324 :
325 0 : m_delegator = xDelegator;
326 0 : m_collator = xCollator;
327 :
328 0 : m_delegator->addGridDataListener( this );
329 :
330 0 : m_isInitialized = true;
331 0 : }
332 :
333 :
334 0 : GridDataEvent SortableGridDataModel::impl_createPublicEvent( GridDataEvent const & i_originalEvent ) const
335 : {
336 0 : GridDataEvent aEvent( i_originalEvent );
337 0 : aEvent.Source = *const_cast< SortableGridDataModel* >( this );
338 0 : aEvent.FirstRow = impl_getPublicRowIndex_nothrow( aEvent.FirstRow );
339 0 : aEvent.LastRow = impl_getPublicRowIndex_nothrow( aEvent.LastRow );
340 0 : return aEvent;
341 : }
342 :
343 :
344 0 : void SortableGridDataModel::impl_broadcast( void ( SAL_CALL XGridDataListener::*i_listenerMethod )( const GridDataEvent & ),
345 : GridDataEvent const & i_publicEvent, MethodGuard& i_instanceLock )
346 : {
347 0 : ::cppu::OInterfaceContainerHelper* pListeners = rBHelper.getContainer( cppu::UnoType<XGridDataListener>::get() );
348 0 : if ( pListeners == NULL )
349 0 : return;
350 :
351 0 : i_instanceLock.clear();
352 0 : pListeners->notifyEach( i_listenerMethod, i_publicEvent );
353 : }
354 :
355 :
356 0 : void SAL_CALL SortableGridDataModel::rowsInserted( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
357 : {
358 0 : MethodGuard aGuard( *this, rBHelper );
359 :
360 0 : if ( impl_isSorted_nothrow() )
361 : {
362 : // no infrastructure is in place currently to sort the new row to its proper location,
363 : // so we remove the sorting here.
364 0 : impl_removeColumnSort( aGuard );
365 0 : aGuard.reset();
366 : }
367 :
368 0 : GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
369 0 : impl_broadcast( &XGridDataListener::rowsInserted, aEvent, aGuard );
370 0 : }
371 :
372 :
373 : namespace
374 : {
375 0 : void lcl_decrementValuesGreaterThan( ::std::vector< ::sal_Int32 > & io_indexMap, sal_Int32 const i_threshold )
376 : {
377 0 : for ( ::std::vector< ::sal_Int32 >::iterator loop = io_indexMap.begin();
378 0 : loop != io_indexMap.end();
379 : ++loop
380 : )
381 : {
382 0 : if ( *loop >= i_threshold )
383 0 : --*loop;
384 : }
385 0 : }
386 : }
387 :
388 :
389 0 : void SortableGridDataModel::impl_rebuildIndexesAndNotify( MethodGuard& i_instanceLock )
390 : {
391 : OSL_PRECOND( impl_isSorted_nothrow(), "SortableGridDataModel::impl_rebuildIndexesAndNotify: illegal call!" );
392 :
393 : // clear the indexes
394 0 : lcl_clear( m_publicToPrivateRowIndex );
395 0 : lcl_clear( m_privateToPublicRowIndex );
396 :
397 : // rebuild the index
398 0 : if ( !impl_reIndex_nothrow( m_currentSortColumn, m_sortAscending ) )
399 : {
400 0 : impl_removeColumnSort( i_instanceLock );
401 0 : return;
402 : }
403 :
404 : // broadcast an artificial event, saying that all rows have been removed
405 0 : GridDataEvent const aRemovalEvent( *this, -1, -1, -1, -1 );
406 0 : impl_broadcast( &XGridDataListener::rowsRemoved, aRemovalEvent, i_instanceLock );
407 0 : i_instanceLock.reset();
408 :
409 : // broadcast an artificial event, saying that n rows have been added
410 0 : GridDataEvent const aAdditionEvent( *this, -1, -1, 0, m_delegator->getRowCount() - 1 );
411 0 : impl_broadcast( &XGridDataListener::rowsInserted, aAdditionEvent, i_instanceLock );
412 : }
413 :
414 :
415 0 : void SAL_CALL SortableGridDataModel::rowsRemoved( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
416 : {
417 0 : MethodGuard aGuard( *this, rBHelper );
418 :
419 : // if the data is not sorted, broadcast the event unchanged
420 0 : if ( !impl_isSorted_nothrow() )
421 : {
422 0 : GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
423 0 : impl_broadcast( &XGridDataListener::rowsRemoved, aEvent, aGuard );
424 0 : return;
425 : }
426 :
427 : // if all rows have been removed, also simply multiplex to own listeners
428 0 : if ( i_event.FirstRow < 0 )
429 : {
430 0 : lcl_clear( m_publicToPrivateRowIndex );
431 0 : lcl_clear( m_privateToPublicRowIndex );
432 0 : GridDataEvent aEvent( i_event );
433 0 : aEvent.Source = *this;
434 0 : impl_broadcast( &XGridDataListener::rowsRemoved, aEvent, aGuard );
435 0 : return;
436 : }
437 :
438 0 : bool needReIndex = false;
439 0 : if ( i_event.FirstRow != i_event.LastRow )
440 : {
441 : OSL_ENSURE( false, "SortableGridDataModel::rowsRemoved: missing implementation - removal of multiple rows!" );
442 0 : needReIndex = true;
443 : }
444 0 : else if ( size_t( i_event.FirstRow ) >= m_privateToPublicRowIndex.size() )
445 : {
446 : OSL_ENSURE( false, "SortableGridDataModel::rowsRemoved: inconsistent/wrong data!" );
447 0 : needReIndex = true;
448 : }
449 :
450 0 : if ( needReIndex )
451 : {
452 0 : impl_rebuildIndexesAndNotify( aGuard );
453 0 : return;
454 : }
455 :
456 : // build public event version
457 0 : GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
458 :
459 : // remove the entries from the index maps
460 0 : sal_Int32 const privateIndex = i_event.FirstRow;
461 0 : sal_Int32 const publicIndex = aEvent.FirstRow;
462 :
463 0 : m_publicToPrivateRowIndex.erase( m_publicToPrivateRowIndex.begin() + publicIndex );
464 0 : m_privateToPublicRowIndex.erase( m_privateToPublicRowIndex.begin() + privateIndex );
465 :
466 : // adjust remaining entries in the index maps
467 0 : lcl_decrementValuesGreaterThan( m_publicToPrivateRowIndex, privateIndex );
468 0 : lcl_decrementValuesGreaterThan( m_privateToPublicRowIndex, publicIndex );
469 :
470 : // broadcast the event
471 0 : impl_broadcast( &XGridDataListener::rowsRemoved, aEvent, aGuard );
472 : }
473 :
474 :
475 0 : void SAL_CALL SortableGridDataModel::dataChanged( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
476 : {
477 0 : MethodGuard aGuard( *this, rBHelper );
478 :
479 0 : GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
480 0 : impl_broadcast( &XGridDataListener::dataChanged, aEvent, aGuard );
481 0 : }
482 :
483 :
484 0 : void SAL_CALL SortableGridDataModel::rowHeadingChanged( const GridDataEvent& i_event ) throw (RuntimeException, std::exception)
485 : {
486 0 : MethodGuard aGuard( *this, rBHelper );
487 :
488 0 : GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
489 0 : impl_broadcast( &XGridDataListener::rowHeadingChanged, aEvent, aGuard );
490 0 : }
491 :
492 :
493 0 : void SAL_CALL SortableGridDataModel::disposing( const EventObject& i_event ) throw (RuntimeException, std::exception)
494 : {
495 : // not interested in
496 : OSL_UNUSED( i_event );
497 0 : }
498 :
499 :
500 : namespace
501 : {
502 : class CellDataLessComparison : public ::std::binary_function< sal_Int32, sal_Int32, bool >
503 : {
504 : public:
505 0 : CellDataLessComparison(
506 : ::std::vector< Any > const & i_data,
507 : ::comphelper::IKeyPredicateLess& i_predicate,
508 : bool const i_sortAscending
509 : )
510 : :m_data( i_data )
511 : ,m_predicate( i_predicate )
512 0 : ,m_sortAscending( i_sortAscending )
513 : {
514 0 : }
515 :
516 0 : bool operator()( sal_Int32 const i_lhs, sal_Int32 const i_rhs ) const
517 : {
518 0 : Any const & lhs = m_data[ i_lhs ];
519 0 : Any const & rhs = m_data[ i_rhs ];
520 : // <VOID/> is less than everything else
521 0 : if ( !lhs.hasValue() )
522 0 : return m_sortAscending;
523 0 : if ( !rhs.hasValue() )
524 0 : return !m_sortAscending;
525 :
526 : // actually compare
527 0 : if ( m_sortAscending )
528 0 : return m_predicate.isLess( lhs, rhs );
529 : else
530 0 : return m_predicate.isLess( rhs, lhs );
531 : }
532 :
533 : private:
534 : ::std::vector< Any > const & m_data;
535 : ::comphelper::IKeyPredicateLess const & m_predicate;
536 : bool const m_sortAscending;
537 : };
538 : }
539 :
540 :
541 0 : bool SortableGridDataModel::impl_reIndex_nothrow( ::sal_Int32 const i_columnIndex, bool const i_sortAscending )
542 : {
543 0 : ::sal_Int32 const rowCount( getRowCount() );
544 0 : ::std::vector< ::sal_Int32 > aPublicToPrivate( rowCount );
545 :
546 : try
547 : {
548 : // build an unsorted translation table, and retrieve the unsorted data
549 0 : ::std::vector< Any > aColumnData( rowCount );
550 0 : Type dataType;
551 0 : for ( ::sal_Int32 rowIndex = 0; rowIndex < rowCount; ++rowIndex )
552 : {
553 0 : aColumnData[ rowIndex ] = m_delegator->getCellData( i_columnIndex, rowIndex );
554 0 : aPublicToPrivate[ rowIndex ] = rowIndex;
555 :
556 : // determine the data types we assume for the complete column
557 0 : if ( ( dataType.getTypeClass() == TypeClass_VOID ) && aColumnData[ rowIndex ].hasValue() )
558 0 : dataType = aColumnData[ rowIndex ].getValueType();
559 : }
560 :
561 : // get predicate object
562 0 : ::std::auto_ptr< ::comphelper::IKeyPredicateLess > const pPredicate( ::comphelper::getStandardLessPredicate( dataType, m_collator ) );
563 0 : ENSURE_OR_RETURN_FALSE( pPredicate.get(), "SortableGridDataModel::impl_reIndex_nothrow: no sortable data found!" );
564 :
565 : // then sort
566 0 : CellDataLessComparison const aComparator( aColumnData, *pPredicate, i_sortAscending );
567 0 : ::std::sort( aPublicToPrivate.begin(), aPublicToPrivate.end(), aComparator );
568 : }
569 0 : catch( const Exception& )
570 : {
571 : DBG_UNHANDLED_EXCEPTION();
572 0 : return false;
573 : }
574 :
575 : // also build the "private to public" mapping
576 0 : ::std::vector< sal_Int32 > aPrivateToPublic( aPublicToPrivate.size() );
577 0 : for ( size_t i=0; i<aPublicToPrivate.size(); ++i )
578 0 : aPrivateToPublic[ aPublicToPrivate[i] ] = i;
579 :
580 0 : m_publicToPrivateRowIndex.swap( aPublicToPrivate );
581 0 : m_privateToPublicRowIndex.swap( aPrivateToPublic );
582 :
583 0 : return true;
584 : }
585 :
586 :
587 0 : void SAL_CALL SortableGridDataModel::sortByColumn( ::sal_Int32 i_columnIndex, sal_Bool i_sortAscending ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
588 : {
589 0 : MethodGuard aGuard( *this, rBHelper );
590 :
591 0 : if ( ( i_columnIndex < 0 ) || ( i_columnIndex >= getColumnCount() ) )
592 0 : throw IndexOutOfBoundsException( OUString(), *this );
593 :
594 0 : if ( !impl_reIndex_nothrow( i_columnIndex, i_sortAscending ) )
595 0 : return;
596 :
597 0 : m_currentSortColumn = i_columnIndex;
598 0 : m_sortAscending = i_sortAscending;
599 :
600 : impl_broadcast(
601 : &XGridDataListener::dataChanged,
602 : GridDataEvent( *this, -1, -1, -1, -1 ),
603 : aGuard
604 0 : );
605 : }
606 :
607 :
608 0 : void SortableGridDataModel::impl_removeColumnSort_noBroadcast()
609 : {
610 0 : lcl_clear( m_publicToPrivateRowIndex );
611 0 : lcl_clear( m_privateToPublicRowIndex );
612 :
613 0 : m_currentSortColumn = -1;
614 0 : m_sortAscending = true;
615 0 : }
616 :
617 :
618 0 : void SortableGridDataModel::impl_removeColumnSort( MethodGuard& i_instanceLock )
619 : {
620 0 : impl_removeColumnSort_noBroadcast();
621 : impl_broadcast(
622 : &XGridDataListener::dataChanged,
623 : GridDataEvent( *this, -1, -1, -1, -1 ),
624 : i_instanceLock
625 0 : );
626 0 : }
627 :
628 :
629 0 : void SAL_CALL SortableGridDataModel::removeColumnSort( ) throw (RuntimeException, std::exception)
630 : {
631 0 : MethodGuard aGuard( *this, rBHelper );
632 0 : impl_removeColumnSort( aGuard );
633 0 : }
634 :
635 :
636 0 : css::beans::Pair< ::sal_Int32, sal_Bool > SAL_CALL SortableGridDataModel::getCurrentSortOrder( ) throw (RuntimeException, std::exception)
637 : {
638 0 : MethodGuard aGuard( *this, rBHelper );
639 :
640 0 : return css::beans::Pair< ::sal_Int32, sal_Bool >( m_currentSortColumn, m_sortAscending );
641 : }
642 :
643 :
644 0 : void SAL_CALL SortableGridDataModel::addRow( const Any& i_heading, const Sequence< Any >& i_data ) throw (RuntimeException, std::exception)
645 : {
646 0 : MethodGuard aGuard( *this, rBHelper );
647 :
648 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
649 0 : aGuard.clear();
650 0 : delegator->addRow( i_heading, i_data );
651 0 : }
652 :
653 :
654 0 : void SAL_CALL SortableGridDataModel::addRows( const Sequence< Any >& i_headings, const Sequence< Sequence< Any > >& i_data ) throw (IllegalArgumentException, RuntimeException, std::exception)
655 : {
656 0 : MethodGuard aGuard( *this, rBHelper );
657 :
658 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
659 0 : aGuard.clear();
660 0 : delegator->addRows( i_headings, i_data );
661 0 : }
662 :
663 :
664 0 : void SAL_CALL SortableGridDataModel::insertRow( ::sal_Int32 i_index, const Any& i_heading, const Sequence< Any >& i_data ) throw (RuntimeException, IndexOutOfBoundsException, std::exception)
665 : {
666 0 : MethodGuard aGuard( *this, rBHelper );
667 :
668 0 : ::sal_Int32 const rowIndex = i_index == getRowCount() ? i_index : impl_getPrivateRowIndex_throw( i_index );
669 : // note that |RowCount| is a valid index in this method, but not for impl_getPrivateRowIndex_throw
670 :
671 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
672 0 : aGuard.clear();
673 0 : delegator->insertRow( rowIndex, i_heading, i_data );
674 0 : }
675 :
676 :
677 0 : void SAL_CALL SortableGridDataModel::insertRows( ::sal_Int32 i_index, const Sequence< Any>& i_headings, const Sequence< Sequence< Any > >& i_data ) throw (IllegalArgumentException, IndexOutOfBoundsException, RuntimeException, std::exception)
678 : {
679 0 : MethodGuard aGuard( *this, rBHelper );
680 :
681 0 : ::sal_Int32 const rowIndex = i_index == getRowCount() ? i_index : impl_getPrivateRowIndex_throw( i_index );
682 : // note that |RowCount| is a valid index in this method, but not for impl_getPrivateRowIndex_throw
683 :
684 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
685 0 : aGuard.clear();
686 0 : delegator->insertRows( rowIndex, i_headings, i_data );
687 0 : }
688 :
689 :
690 0 : void SAL_CALL SortableGridDataModel::removeRow( ::sal_Int32 i_rowIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
691 : {
692 0 : MethodGuard aGuard( *this, rBHelper );
693 :
694 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
695 :
696 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
697 0 : aGuard.clear();
698 0 : delegator->removeRow( rowIndex );
699 0 : }
700 :
701 :
702 0 : void SAL_CALL SortableGridDataModel::removeAllRows( ) throw (RuntimeException, std::exception)
703 : {
704 0 : MethodGuard aGuard( *this, rBHelper );
705 :
706 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
707 0 : aGuard.clear();
708 0 : delegator->removeAllRows();
709 0 : }
710 :
711 :
712 0 : void SAL_CALL SortableGridDataModel::updateCellData( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex, const Any& i_value ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
713 : {
714 0 : MethodGuard aGuard( *this, rBHelper );
715 :
716 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
717 :
718 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
719 0 : aGuard.clear();
720 0 : delegator->updateCellData( i_columnIndex, rowIndex, i_value );
721 0 : }
722 :
723 :
724 0 : void SAL_CALL SortableGridDataModel::updateRowData( const Sequence< ::sal_Int32 >& i_columnIndexes, ::sal_Int32 i_rowIndex, const Sequence< Any >& i_values ) throw (IndexOutOfBoundsException, IllegalArgumentException, RuntimeException, std::exception)
725 : {
726 0 : MethodGuard aGuard( *this, rBHelper );
727 :
728 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
729 :
730 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
731 0 : aGuard.clear();
732 0 : delegator->updateRowData( i_columnIndexes, rowIndex, i_values );
733 0 : }
734 :
735 :
736 0 : void SAL_CALL SortableGridDataModel::updateRowHeading( ::sal_Int32 i_rowIndex, const Any& i_heading ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
737 : {
738 0 : MethodGuard aGuard( *this, rBHelper );
739 :
740 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
741 :
742 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
743 0 : aGuard.clear();
744 0 : delegator->updateRowHeading( rowIndex, i_heading );
745 0 : }
746 :
747 :
748 0 : void SAL_CALL SortableGridDataModel::updateCellToolTip( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex, const Any& i_value ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
749 : {
750 0 : MethodGuard aGuard( *this, rBHelper );
751 :
752 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
753 :
754 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
755 0 : aGuard.clear();
756 0 : delegator->updateCellToolTip( i_columnIndex, rowIndex, i_value );
757 0 : }
758 :
759 :
760 0 : void SAL_CALL SortableGridDataModel::updateRowToolTip( ::sal_Int32 i_rowIndex, const Any& i_value ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
761 : {
762 0 : MethodGuard aGuard( *this, rBHelper );
763 :
764 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
765 :
766 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
767 0 : aGuard.clear();
768 0 : delegator->updateRowToolTip( rowIndex, i_value );
769 0 : }
770 :
771 :
772 0 : void SAL_CALL SortableGridDataModel::addGridDataListener( const Reference< XGridDataListener >& i_listener ) throw (RuntimeException, std::exception)
773 : {
774 0 : rBHelper.addListener( cppu::UnoType<XGridDataListener>::get(), i_listener );
775 0 : }
776 :
777 :
778 0 : void SAL_CALL SortableGridDataModel::removeGridDataListener( const Reference< XGridDataListener >& i_listener ) throw (RuntimeException, std::exception)
779 : {
780 0 : rBHelper.removeListener( cppu::UnoType<XGridDataListener>::get(), i_listener );
781 0 : }
782 :
783 :
784 0 : ::sal_Int32 SAL_CALL SortableGridDataModel::getRowCount() throw (RuntimeException, std::exception)
785 : {
786 0 : MethodGuard aGuard( *this, rBHelper );
787 :
788 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
789 0 : aGuard.clear();
790 0 : return delegator->getRowCount();
791 : }
792 :
793 :
794 0 : ::sal_Int32 SAL_CALL SortableGridDataModel::getColumnCount() throw (RuntimeException, std::exception)
795 : {
796 0 : MethodGuard aGuard( *this, rBHelper );
797 :
798 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
799 0 : aGuard.clear();
800 0 : return delegator->getColumnCount();
801 : }
802 :
803 :
804 0 : Any SAL_CALL SortableGridDataModel::getCellData( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
805 : {
806 0 : MethodGuard aGuard( *this, rBHelper );
807 :
808 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
809 :
810 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
811 0 : aGuard.clear();
812 0 : return delegator->getCellData( i_columnIndex, rowIndex );
813 : }
814 :
815 :
816 0 : Any SAL_CALL SortableGridDataModel::getCellToolTip( ::sal_Int32 i_columnIndex, ::sal_Int32 i_rowIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
817 : {
818 0 : MethodGuard aGuard( *this, rBHelper );
819 :
820 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
821 :
822 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
823 0 : aGuard.clear();
824 0 : return delegator->getCellToolTip( i_columnIndex, rowIndex );
825 : }
826 :
827 :
828 0 : Any SAL_CALL SortableGridDataModel::getRowHeading( ::sal_Int32 i_rowIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
829 : {
830 0 : MethodGuard aGuard( *this, rBHelper );
831 :
832 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
833 :
834 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
835 0 : aGuard.clear();
836 0 : return delegator->getRowHeading( rowIndex );
837 : }
838 :
839 :
840 0 : Sequence< Any > SAL_CALL SortableGridDataModel::getRowData( ::sal_Int32 i_rowIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
841 : {
842 0 : MethodGuard aGuard( *this, rBHelper );
843 :
844 0 : ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex );
845 :
846 0 : Reference< XMutableGridDataModel > const delegator( m_delegator );
847 0 : aGuard.clear();
848 0 : return delegator->getRowData( rowIndex );
849 : }
850 :
851 :
852 0 : void SAL_CALL SortableGridDataModel::disposing()
853 : {
854 0 : m_currentSortColumn = -1;
855 :
856 0 : Reference< XComponent > const delegatorComponent( m_delegator.get() );
857 0 : m_delegator->removeGridDataListener( this );
858 0 : m_delegator.clear();
859 0 : delegatorComponent->dispose();
860 :
861 0 : Reference< XComponent > const collatorComponent( m_collator, UNO_QUERY );
862 0 : m_collator.clear();
863 0 : if ( collatorComponent.is() )
864 0 : collatorComponent->dispose();
865 :
866 0 : lcl_clear( m_publicToPrivateRowIndex );
867 0 : lcl_clear( m_privateToPublicRowIndex );
868 0 : }
869 :
870 :
871 0 : Reference< css::util::XCloneable > SAL_CALL SortableGridDataModel::createClone( ) throw (RuntimeException, std::exception)
872 : {
873 0 : MethodGuard aGuard( *this, rBHelper );
874 :
875 0 : return new SortableGridDataModel( *this );
876 : }
877 :
878 :
879 0 : OUString SAL_CALL SortableGridDataModel::getImplementationName( ) throw (RuntimeException, std::exception)
880 : {
881 0 : return OUString( "org.openoffice.comp.toolkit.SortableGridDataModel" );
882 : }
883 :
884 0 : sal_Bool SAL_CALL SortableGridDataModel::supportsService( const OUString& i_serviceName ) throw (RuntimeException, std::exception)
885 : {
886 0 : return cppu::supportsService(this, i_serviceName);
887 : }
888 :
889 0 : Sequence< OUString > SAL_CALL SortableGridDataModel::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
890 : {
891 0 : Sequence< OUString > aServiceNames(1);
892 0 : aServiceNames[0] = OUString("com.sun.star.awt.grid.SortableGridDataModel");
893 0 : return aServiceNames;
894 : }
895 :
896 :
897 0 : ::sal_Int32 SortableGridDataModel::impl_getPrivateRowIndex_throw( ::sal_Int32 const i_publicRowIndex ) const
898 : {
899 0 : if ( ( i_publicRowIndex < 0 ) || ( i_publicRowIndex >= m_delegator->getRowCount() ) )
900 0 : throw IndexOutOfBoundsException( OUString(), *const_cast< SortableGridDataModel* >( this ) );
901 :
902 0 : if ( !impl_isSorted_nothrow() )
903 : // no need to translate anything
904 0 : return i_publicRowIndex;
905 :
906 0 : ENSURE_OR_RETURN( size_t( i_publicRowIndex ) < m_publicToPrivateRowIndex.size(),
907 : "SortableGridDataModel::impl_getPrivateRowIndex_throw: inconsistency!", i_publicRowIndex );
908 : // obviously the translation table contains too few elements - it should have exactly |getRowCount()|
909 : // elements
910 :
911 0 : return m_publicToPrivateRowIndex[ i_publicRowIndex ];
912 : }
913 :
914 :
915 0 : ::sal_Int32 SortableGridDataModel::impl_getPublicRowIndex_nothrow( ::sal_Int32 const i_privateRowIndex ) const
916 : {
917 0 : if ( !impl_isSorted_nothrow() )
918 : // no need to translate anything
919 0 : return i_privateRowIndex;
920 :
921 0 : if ( i_privateRowIndex < 0 )
922 0 : return i_privateRowIndex;
923 :
924 0 : ENSURE_OR_RETURN( size_t( i_privateRowIndex ) < m_privateToPublicRowIndex.size(),
925 : "SortableGridDataModel::impl_getPublicRowIndex_nothrow: invalid index!", i_privateRowIndex );
926 :
927 0 : return m_privateToPublicRowIndex[ i_privateRowIndex ];
928 : }
929 :
930 : }
931 :
932 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
933 0 : org_openoffice_comp_toolkit_SortableGridDataModel_get_implementation(
934 : css::uno::XComponentContext *context,
935 : css::uno::Sequence<css::uno::Any> const &)
936 : {
937 0 : return cppu::acquire(new SortableGridDataModel(context));
938 : }
939 :
940 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|