1 /*************************************************************************
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * Copyright 2000, 2010 Oracle and/or its affiliates.
5  *
6  * OpenOffice.org - a multi-platform office productivity suite
7  *
8  * This file is part of OpenOffice.org.
9  *
10  * OpenOffice.org is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 3
12  * only, as published by the Free Software Foundation.
13  *
14  * OpenOffice.org is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License version 3 for more details
18  * (a copy is included in the LICENSE file that accompanied this code).
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with OpenOffice.org.  If not, see
22  * <http://www.openoffice.org/license.html>
23  * for a copy of the LGPLv3 License.
24  *
25  ************************************************************************/
26 
27 #ifndef TOOLKIT_SORTABLEGRIDDATAMODEL_HXX
28 #define TOOLKIT_SORTABLEGRIDDATAMODEL_HXX
29 
30 #include "initguard.hxx"
31 
32 /** === begin UNO includes === **/
33 #include <com/sun/star/awt/grid/XSortableMutableGridDataModel.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XInitialization.hpp>
36 #include <com/sun/star/i18n/XCollator.hpp>
37 #include <com/sun/star/awt/grid/XGridDataListener.hpp>
38 /** === end UNO includes === **/
39 
40 #include <comphelper/componentcontext.hxx>
41 #include <cppuhelper/basemutex.hxx>
42 #include <cppuhelper/compbase3.hxx>
43 #include <cppuhelper/implbase1.hxx>
44 
45 //......................................................................................................................
46 namespace toolkit
47 {
48 //......................................................................................................................
49 
50     class SortableGridDataModel;
51     typedef InitGuard< SortableGridDataModel >  MethodGuard;
52 
53 	//==================================================================================================================
54 	//= SortableGridDataModel
55 	//==================================================================================================================
56     typedef ::cppu::WeakComponentImplHelper3    <   ::com::sun::star::awt::grid::XSortableMutableGridDataModel
57                                                 ,   ::com::sun::star::lang::XServiceInfo
58                                                 ,   ::com::sun::star::lang::XInitialization
59                                                 >   SortableGridDataModel_Base;
60     typedef ::cppu::ImplHelper1 <   ::com::sun::star::awt::grid::XGridDataListener
61                                 >   SortableGridDataModel_PrivateBase;
62     class SortableGridDataModel :public ::cppu::BaseMutex
63                                 ,public SortableGridDataModel_Base
64                                 ,public SortableGridDataModel_PrivateBase
65 	{
66     public:
67         SortableGridDataModel( ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > const & i_factory );
68         SortableGridDataModel( SortableGridDataModel const & i_copySource );
69 
70         bool    isInitialized() const { return m_isInitialized; }
71 
72 #ifdef DBG_UTIL
73         const char* checkInvariants() const;
74 #endif
75 
76     protected:
77         ~SortableGridDataModel();
78 
79     public:
80         // XSortableGridData
81         virtual void SAL_CALL sortByColumn( ::sal_Int32 ColumnIndex, ::sal_Bool SortAscending ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
82         virtual void SAL_CALL removeColumnSort(  ) throw (::com::sun::star::uno::RuntimeException);
83         virtual ::com::sun::star::beans::Pair< ::sal_Int32, ::sal_Bool > SAL_CALL getCurrentSortOrder(  ) throw (::com::sun::star::uno::RuntimeException);
84 
85         // XMutableGridDataModel
86         virtual void SAL_CALL addRow( const ::com::sun::star::uno::Any& Heading, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& Data ) throw (::com::sun::star::uno::RuntimeException);
87         virtual void SAL_CALL addRows( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& Headings, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > >& Data ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
88         virtual void SAL_CALL insertRow( ::sal_Int32 i_index, const ::com::sun::star::uno::Any& i_heading, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& Data ) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::IndexOutOfBoundsException);
89         virtual void SAL_CALL insertRows( ::sal_Int32 i_index, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any>& Headings, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > >& Data ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
90         virtual void SAL_CALL removeRow( ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
91         virtual void SAL_CALL removeAllRows(  ) throw (::com::sun::star::uno::RuntimeException);
92         virtual void SAL_CALL updateCellData( ::sal_Int32 ColumnIndex, ::sal_Int32 RowIndex, const ::com::sun::star::uno::Any& Value ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
93         virtual void SAL_CALL updateRowData( const ::com::sun::star::uno::Sequence< ::sal_Int32 >& ColumnIndexes, ::sal_Int32 RowIndex, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& Values ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
94         virtual void SAL_CALL updateRowHeading( ::sal_Int32 RowIndex, const ::com::sun::star::uno::Any& Heading ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
95         virtual void SAL_CALL updateCellToolTip( ::sal_Int32 ColumnIndex, ::sal_Int32 RowIndex, const ::com::sun::star::uno::Any& Value ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
96         virtual void SAL_CALL updateRowToolTip( ::sal_Int32 RowIndex, const ::com::sun::star::uno::Any& Value ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
97         virtual void SAL_CALL addGridDataListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XGridDataListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
98         virtual void SAL_CALL removeGridDataListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XGridDataListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
99 
100         // XGridDataModel
101         virtual ::sal_Int32 SAL_CALL getRowCount() throw (::com::sun::star::uno::RuntimeException);
102         virtual ::sal_Int32 SAL_CALL getColumnCount() throw (::com::sun::star::uno::RuntimeException);
103         virtual ::com::sun::star::uno::Any SAL_CALL getCellData( ::sal_Int32 Column, ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
104         virtual ::com::sun::star::uno::Any SAL_CALL getCellToolTip( ::sal_Int32 Column, ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
105         virtual ::com::sun::star::uno::Any SAL_CALL getRowHeading( ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
106         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > SAL_CALL getRowData( ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
107 
108         // OComponentHelper
109         virtual void SAL_CALL disposing();
110 
111         // XCloneable
112         virtual ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > SAL_CALL createClone(  ) throw (::com::sun::star::uno::RuntimeException);
113 
114 	    // XServiceInfo
115         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
116 	    virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
117 	    virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
118 
119         // XInitialization
120         virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
121 
122         // XGridDataListener
123         virtual void SAL_CALL rowsInserted( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
124         virtual void SAL_CALL rowsRemoved( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
125         virtual void SAL_CALL dataChanged( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
126         virtual void SAL_CALL rowHeadingChanged( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
127 
128         // XEventListener
129         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& i_event ) throw (::com::sun::star::uno::RuntimeException);
130 
131         // XInterface
132         virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& aType ) throw (::com::sun::star::uno::RuntimeException);
133         virtual void SAL_CALL acquire(  ) throw ();
134         virtual void SAL_CALL release(  ) throw ();
135 
136         // XTypeProvider
137         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw (::com::sun::star::uno::RuntimeException);
138         virtual ::com::sun::star::uno::Sequence< ::sal_Int8 > SAL_CALL getImplementationId(  ) throw (::com::sun::star::uno::RuntimeException);
139 
140     private:
141         /** translates the given public index into one to be passed to our delegator
142             @throws ::com::sun::star::lang::IndexOutOfBoundsException
143                 if the given index does not denote a valid row
144         */
145         ::sal_Int32 impl_getPrivateRowIndex_throw( ::sal_Int32 const i_publicRowIndex ) const;
146 
147         /** translates the given private row index to a public one
148         */
149         ::sal_Int32 impl_getPublicRowIndex_nothrow( ::sal_Int32 const i_privateRowIndex ) const;
150 
151         inline bool impl_isSorted_nothrow() const
152         {
153             return m_currentSortColumn >= 0;
154         }
155 
156         /** rebuilds the index translation structure.
157 
158             Neither <member>m_currentSortColumn</member> nor <member>m_sortAscending</member> are touched by this method.
159             Also, the given column index is not checked, this is the responsibility of the caller.
160         */
161         bool    impl_reIndex_nothrow( ::sal_Int32 const i_columnIndex, sal_Bool const i_sortAscending );
162 
163         /** translates the given event, obtained from our delegator, to a version which can be broadcasted to our own
164             clients.
165         */
166         ::com::sun::star::awt::grid::GridDataEvent
167                 impl_createPublicEvent( ::com::sun::star::awt::grid::GridDataEvent const & i_originalEvent ) const;
168 
169         /** broadcasts the given event to our registered XGridDataListeners
170         */
171         void    impl_broadcast(
172                     void ( SAL_CALL ::com::sun::star::awt::grid::XGridDataListener::*i_listenerMethod )( const ::com::sun::star::awt::grid::GridDataEvent & ),
173                     ::com::sun::star::awt::grid::GridDataEvent const & i_publicEvent,
174                     MethodGuard& i_instanceLock
175                 );
176 
177         /** rebuilds our indexes, notifying row removal and row addition events
178 
179             First, a rowsRemoved event is notified to our registered listeners. Then, the index translation tables are
180             rebuilt, and a rowsInserted event is notified.
181 
182             Only to be called when we're sorted.
183         */
184         void    impl_rebuildIndexesAndNotify( MethodGuard& i_instanceLock );
185 
186         /** removes the current sorting, and notifies a change of all data
187         */
188         void    impl_removeColumnSort( MethodGuard& i_instanceLock );
189 
190         /** removes the current sorting, without any broadcast
191         */
192         void    impl_removeColumnSort_noBroadcast();
193 
194     private:
195         ::comphelper::ComponentContext                                                          m_context;
196         bool                                                                                    m_isInitialized;
197         ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XMutableGridDataModel >  m_delegator;
198         ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator >                   m_collator;
199         ::sal_Int32                                                                             m_currentSortColumn;
200         ::sal_Bool                                                                              m_sortAscending;
201         ::std::vector< ::sal_Int32 >                                                            m_publicToPrivateRowIndex;
202         ::std::vector< ::sal_Int32 >                                                            m_privateToPublicRowIndex;
203 	};
204 
205 //......................................................................................................................
206 } // namespace toolkit
207 //......................................................................................................................
208 
209 #endif // TOOLKIT_SORTABLEGRIDDATAMODEL_HXX
210