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