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 #ifndef TOOLKIT_SORTABLEGRIDDATAMODEL_HXX
25 #define TOOLKIT_SORTABLEGRIDDATAMODEL_HXX
26 
27 #include "initguard.hxx"
28 
29 /** === begin UNO includes === **/
30 #include <com/sun/star/awt/grid/XSortableMutableGridDataModel.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/lang/XInitialization.hpp>
33 #include <com/sun/star/i18n/XCollator.hpp>
34 #include <com/sun/star/awt/grid/XGridDataListener.hpp>
35 /** === end UNO includes === **/
36 
37 #include <comphelper/componentcontext.hxx>
38 #include <cppuhelper/basemutex.hxx>
39 #include <cppuhelper/compbase3.hxx>
40 #include <cppuhelper/implbase1.hxx>
41 
42 //......................................................................................................................
43 namespace toolkit
44 {
45 //......................................................................................................................
46 
47     class SortableGridDataModel;
48     typedef InitGuard< SortableGridDataModel >  MethodGuard;
49 
50 	//==================================================================================================================
51 	//= SortableGridDataModel
52 	//==================================================================================================================
53     typedef ::cppu::WeakComponentImplHelper3    <   ::com::sun::star::awt::grid::XSortableMutableGridDataModel
54                                                 ,   ::com::sun::star::lang::XServiceInfo
55                                                 ,   ::com::sun::star::lang::XInitialization
56                                                 >   SortableGridDataModel_Base;
57     typedef ::cppu::ImplHelper1 <   ::com::sun::star::awt::grid::XGridDataListener
58                                 >   SortableGridDataModel_PrivateBase;
59     class SortableGridDataModel :public ::cppu::BaseMutex
60                                 ,public SortableGridDataModel_Base
61                                 ,public SortableGridDataModel_PrivateBase
62 	{
63     public:
64         SortableGridDataModel( ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > const & i_factory );
65         SortableGridDataModel( SortableGridDataModel const & i_copySource );
66 
isInitialized() const67         bool    isInitialized() const { return m_isInitialized; }
68 
69 #ifdef DBG_UTIL
70         const char* checkInvariants() const;
71 #endif
72 
73     protected:
74         ~SortableGridDataModel();
75 
76     public:
77         // XSortableGridData
78         virtual void SAL_CALL sortByColumn( ::sal_Int32 ColumnIndex, ::sal_Bool SortAscending ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
79         virtual void SAL_CALL removeColumnSort(  ) throw (::com::sun::star::uno::RuntimeException);
80         virtual ::com::sun::star::beans::Pair< ::sal_Int32, ::sal_Bool > SAL_CALL getCurrentSortOrder(  ) throw (::com::sun::star::uno::RuntimeException);
81 
82         // XMutableGridDataModel
83         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);
84         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);
85         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);
86         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);
87         virtual void SAL_CALL removeRow( ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
88         virtual void SAL_CALL removeAllRows(  ) throw (::com::sun::star::uno::RuntimeException);
89         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);
90         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);
91         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);
92         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);
93         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);
94         virtual void SAL_CALL addGridDataListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XGridDataListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
95         virtual void SAL_CALL removeGridDataListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XGridDataListener >& Listener ) throw (::com::sun::star::uno::RuntimeException);
96 
97         // XGridDataModel
98         virtual ::sal_Int32 SAL_CALL getRowCount() throw (::com::sun::star::uno::RuntimeException);
99         virtual ::sal_Int32 SAL_CALL getColumnCount() throw (::com::sun::star::uno::RuntimeException);
100         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);
101         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);
102         virtual ::com::sun::star::uno::Any SAL_CALL getRowHeading( ::sal_Int32 RowIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException);
103         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);
104 
105         // OComponentHelper
106         virtual void SAL_CALL disposing();
107 
108         // XCloneable
109         virtual ::com::sun::star::uno::Reference< ::com::sun::star::util::XCloneable > SAL_CALL createClone(  ) throw (::com::sun::star::uno::RuntimeException);
110 
111 	    // XServiceInfo
112         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
113 	    virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
114 	    virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
115 
116         // XInitialization
117         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);
118 
119         // XGridDataListener
120         virtual void SAL_CALL rowsInserted( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
121         virtual void SAL_CALL rowsRemoved( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
122         virtual void SAL_CALL dataChanged( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
123         virtual void SAL_CALL rowHeadingChanged( const ::com::sun::star::awt::grid::GridDataEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
124 
125         // XEventListener
126         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& i_event ) throw (::com::sun::star::uno::RuntimeException);
127 
128         // XInterface
129         virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& aType ) throw (::com::sun::star::uno::RuntimeException);
130         virtual void SAL_CALL acquire(  ) throw ();
131         virtual void SAL_CALL release(  ) throw ();
132 
133         // XTypeProvider
134         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw (::com::sun::star::uno::RuntimeException);
135         virtual ::com::sun::star::uno::Sequence< ::sal_Int8 > SAL_CALL getImplementationId(  ) throw (::com::sun::star::uno::RuntimeException);
136 
137     private:
138         /** translates the given public index into one to be passed to our delegator
139             @throws ::com::sun::star::lang::IndexOutOfBoundsException
140                 if the given index does not denote a valid row
141         */
142         ::sal_Int32 impl_getPrivateRowIndex_throw( ::sal_Int32 const i_publicRowIndex ) const;
143 
144         /** translates the given private row index to a public one
145         */
146         ::sal_Int32 impl_getPublicRowIndex_nothrow( ::sal_Int32 const i_privateRowIndex ) const;
147 
impl_isSorted_nothrow() const148         inline bool impl_isSorted_nothrow() const
149         {
150             return m_currentSortColumn >= 0;
151         }
152 
153         /** rebuilds the index translation structure.
154 
155             Neither <member>m_currentSortColumn</member> nor <member>m_sortAscending</member> are touched by this method.
156             Also, the given column index is not checked, this is the responsibility of the caller.
157         */
158         bool    impl_reIndex_nothrow( ::sal_Int32 const i_columnIndex, sal_Bool const i_sortAscending );
159 
160         /** translates the given event, obtained from our delegator, to a version which can be broadcasted to our own
161             clients.
162         */
163         ::com::sun::star::awt::grid::GridDataEvent
164                 impl_createPublicEvent( ::com::sun::star::awt::grid::GridDataEvent const & i_originalEvent ) const;
165 
166         /** broadcasts the given event to our registered XGridDataListeners
167         */
168         void    impl_broadcast(
169                     void ( SAL_CALL ::com::sun::star::awt::grid::XGridDataListener::*i_listenerMethod )( const ::com::sun::star::awt::grid::GridDataEvent & ),
170                     ::com::sun::star::awt::grid::GridDataEvent const & i_publicEvent,
171                     MethodGuard& i_instanceLock
172                 );
173 
174         /** rebuilds our indexes, notifying row removal and row addition events
175 
176             First, a rowsRemoved event is notified to our registered listeners. Then, the index translation tables are
177             rebuilt, and a rowsInserted event is notified.
178 
179             Only to be called when we're sorted.
180         */
181         void    impl_rebuildIndexesAndNotify( MethodGuard& i_instanceLock );
182 
183         /** removes the current sorting, and notifies a change of all data
184         */
185         void    impl_removeColumnSort( MethodGuard& i_instanceLock );
186 
187         /** removes the current sorting, without any broadcast
188         */
189         void    impl_removeColumnSort_noBroadcast();
190 
191     private:
192         ::comphelper::ComponentContext                                                          m_context;
193         bool                                                                                    m_isInitialized;
194         ::com::sun::star::uno::Reference< ::com::sun::star::awt::grid::XMutableGridDataModel >  m_delegator;
195         ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator >                   m_collator;
196         ::sal_Int32                                                                             m_currentSortColumn;
197         ::sal_Bool                                                                              m_sortAscending;
198         ::std::vector< ::sal_Int32 >                                                            m_publicToPrivateRowIndex;
199         ::std::vector< ::sal_Int32 >                                                            m_privateToPublicRowIndex;
200 	};
201 
202 //......................................................................................................................
203 } // namespace toolkit
204 //......................................................................................................................
205 
206 #endif // TOOLKIT_SORTABLEGRIDDATAMODEL_HXX
207