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