Update ooo320-m1
[ooovba.git] / toolkit / source / controls / grid / defaultgriddatamodel.cxx
blob3251769fecf001b570c705bb26340ee179c3b6b2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: treedatamodel.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
33 #include "defaultgriddatamodel.hxx"
34 #include <comphelper/sequence.hxx>
35 #include <toolkit/helper/servicenames.hxx>
36 #include <rtl/ref.hxx>
38 using ::rtl::OUString;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::awt;
42 using namespace ::com::sun::star::awt::grid;
43 using namespace ::com::sun::star::lang;
45 #define ROWHEIGHT ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "RowHeight" ))
46 #define ROWHEADERS ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "RowHeaders" ))
48 namespace toolkit
51 ///////////////////////////////////////////////////////////////////////
52 // class DefaultGridDataModel
53 ///////////////////////////////////////////////////////////////////////
55 DefaultGridDataModel::DefaultGridDataModel()
56 : rowHeight(0),
57 rowHeaders(std::vector< ::rtl::OUString >())
61 //---------------------------------------------------------------------
63 DefaultGridDataModel::~DefaultGridDataModel()
67 void DefaultGridDataModel::broadcast( broadcast_type eType, const GridDataEvent& aEvent )
69 ::cppu::OInterfaceContainerHelper* pIter = BrdcstHelper.getContainer( XGridDataListener::static_type() );
70 if( pIter )
72 ::cppu::OInterfaceIteratorHelper aListIter(*pIter);
73 while(aListIter.hasMoreElements())
75 XGridDataListener* pListener = static_cast<XGridDataListener*>(aListIter.next());
76 switch( eType )
78 case row_added: pListener->rowAdded(aEvent); break;
79 case row_removed: pListener->rowRemoved(aEvent); break;
80 case data_changed: pListener->dataChanged(aEvent); break;
86 //---------------------------------------------------------------------
88 void DefaultGridDataModel::broadcast_changed( ::rtl::OUString name, Any oldValue, Any newValue )
90 Reference< XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
91 GridDataEvent aEvent( xSource, name, oldValue, newValue, 0, ::rtl::OUString(), Sequence< ::rtl::OUString>() );
92 broadcast( data_changed, aEvent);
95 //---------------------------------------------------------------------
97 void DefaultGridDataModel::broadcast_add( sal_Int32 index, const ::rtl::OUString & headerName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rowData )
99 Reference< XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
100 GridDataEvent aEvent( xSource, ::rtl::OUString(), Any(), Any(), index, headerName, rowData );
101 broadcast( row_added, aEvent);
104 //---------------------------------------------------------------------
106 void DefaultGridDataModel::broadcast_remove( sal_Int32 index, const ::rtl::OUString & headerName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rowData )
108 Reference< XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
109 GridDataEvent aEvent( xSource, ::rtl::OUString(), Any(), Any(), index, headerName, rowData );
110 broadcast( row_removed, aEvent);
113 //---------------------------------------------------------------------
115 //---------------------------------------------------------------------
116 // XDefaultGridDataModel
117 //---------------------------------------------------------------------
118 ::sal_Int32 SAL_CALL DefaultGridDataModel::getRowHeight() throw (::com::sun::star::uno::RuntimeException)
120 return rowHeight;
123 //---------------------------------------------------------------------
125 void SAL_CALL DefaultGridDataModel::setRowHeight(::sal_Int32 value) throw (::com::sun::star::uno::RuntimeException)
127 sal_Int32 oldValue = rowHeight;
128 rowHeight = value;
130 broadcast_changed( ROWHEIGHT, Any(oldValue), Any(value) );
133 //---------------------------------------------------------------------
135 ::sal_Int32 SAL_CALL DefaultGridDataModel::getRowCount() throw (::com::sun::star::uno::RuntimeException)
137 return data.size();
140 //---------------------------------------------------------------------
142 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL DefaultGridDataModel::getRowHeaders() throw (::com::sun::star::uno::RuntimeException)
144 return comphelper::containerToSequence(rowHeaders);
147 //---------------------------------------------------------------------
149 void SAL_CALL DefaultGridDataModel::setRowHeaders(const ::com::sun::star::uno::Sequence< ::rtl::OUString > & value) throw (::com::sun::star::uno::RuntimeException)
151 ::com::sun::star::uno::Sequence< ::rtl::OUString > oldValue( comphelper::containerToSequence(rowHeaders) );
153 std::vector< rtl::OUString>::iterator iterator;
154 int i = 0;
155 int sequenceSize = value.getLength();
157 for(iterator = rowHeaders.begin(); iterator != rowHeaders.end(); iterator++)
159 if ( sequenceSize > i )
160 *iterator = value[i];
161 else
162 *iterator = ::rtl::OUString();
163 i++;
166 broadcast_changed( ROWHEADERS, Any(oldValue), Any(comphelper::containerToSequence(rowHeaders)) );
169 //---------------------------------------------------------------------
171 void SAL_CALL DefaultGridDataModel::addRow(const ::rtl::OUString & headername, const ::com::sun::star::uno::Sequence< ::rtl::OUString > & rRowdata) throw (::com::sun::star::uno::RuntimeException)
173 // store header name
174 rowHeaders.push_back(headername);
177 // store row data
178 std::vector< rtl::OUString > newRow(
179 comphelper::sequenceToContainer< std::vector<rtl::OUString > >(rRowdata));
181 data.push_back( newRow );
183 broadcast_add( data.size()-1, headername, rRowdata);
187 //---------------------------------------------------------------------
189 void SAL_CALL DefaultGridDataModel::removeRow(::sal_Int32 index) throw (::com::sun::star::uno::RuntimeException)
191 if ( index >= 0 && index <= getRowCount()-1)
193 /* if(Reference< XGridControl >( getPeer(), UNO_QUERY_THROW )->isSelectedIndex( index ))
195 ::com::sun::star::uno::Sequence<::sal_Int32> selectedRows = Reference< XGridControl >( getPeer(), UNO_QUERY_THROW )->getSelection();
196 selectedRow.erase(selectedRows.begin()+index);
199 ::rtl::OUString headerName( (::rtl::OUString) rowHeaders[index] );
200 rowHeaders.erase(rowHeaders.begin() + index);
202 Sequence< ::rtl::OUString >& rowData ( (Sequence< ::rtl::OUString >&)data[index] );
203 data.erase(data.begin() + index);
204 broadcast_remove( index, headerName, rowData);
206 else
207 return;
209 //---------------------------------------------------------------------
210 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > > SAL_CALL DefaultGridDataModel::getData() throw (::com::sun::star::uno::RuntimeException)
213 std::vector< std::vector< ::rtl::OUString > >::iterator iterator;
214 std::vector< Sequence< ::rtl::OUString > > dummyContainer(0);
217 for(iterator = data.begin(); iterator != data.end(); iterator++)
219 Sequence< ::rtl::OUString > cols(comphelper::containerToSequence(*iterator));
220 dummyContainer.push_back( cols );
222 Sequence< Sequence< ::rtl::OUString > > dataSequence(comphelper::containerToSequence(dummyContainer));
224 return dataSequence;
227 //---------------------------------------------------------------------
229 void SAL_CALL DefaultGridDataModel::addDataListener( const Reference< XGridDataListener >& xListener ) throw (RuntimeException)
231 BrdcstHelper.addListener( XGridDataListener::static_type(), xListener );
234 //---------------------------------------------------------------------
236 void SAL_CALL DefaultGridDataModel::removeDataListener( const Reference< XGridDataListener >& xListener ) throw (RuntimeException)
238 BrdcstHelper.removeListener( XGridDataListener::static_type(), xListener );
241 void SAL_CALL DefaultGridDataModel::removeAll() throw (RuntimeException)
243 rowHeaders.clear();
244 data.clear();
245 broadcast_remove( -1, ::rtl::OUString(), 0);
248 //---------------------------------------------------------------------
249 // XComponent
250 //---------------------------------------------------------------------
252 void SAL_CALL DefaultGridDataModel::dispose() throw (RuntimeException)
254 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
256 ::com::sun::star::lang::EventObject aEvent;
257 aEvent.Source.set( static_cast< ::cppu::OWeakObject* >( this ) );
258 BrdcstHelper.aLC.disposeAndClear( aEvent );
262 //---------------------------------------------------------------------
264 void SAL_CALL DefaultGridDataModel::addEventListener( const Reference< XEventListener >& xListener ) throw (RuntimeException)
266 BrdcstHelper.addListener( XEventListener::static_type(), xListener );
269 //---------------------------------------------------------------------
271 void SAL_CALL DefaultGridDataModel::removeEventListener( const Reference< XEventListener >& xListener ) throw (RuntimeException)
273 BrdcstHelper.removeListener( XEventListener::static_type(), xListener );
275 //---------------------------------------------------------------------
276 // XServiceInfo
277 //---------------------------------------------------------------------
279 ::rtl::OUString SAL_CALL DefaultGridDataModel::getImplementationName( ) throw (RuntimeException)
281 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
282 static const OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "toolkit.DefaultGridDataModel" ) );
283 return aImplName;
286 //---------------------------------------------------------------------
288 sal_Bool SAL_CALL DefaultGridDataModel::supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException)
290 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
291 return ServiceName.equalsAscii( szServiceName_DefaultGridDataModel );
294 //---------------------------------------------------------------------
296 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL DefaultGridDataModel::getSupportedServiceNames( ) throw (RuntimeException)
298 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
299 static const OUString aServiceName( OUString::createFromAscii( szServiceName_DefaultGridDataModel ) );
300 static const Sequence< OUString > aSeq( &aServiceName, 1 );
301 return aSeq;
306 Reference< XInterface > SAL_CALL DefaultGridDataModel_CreateInstance( const Reference< XMultiServiceFactory >& )
308 return Reference < XInterface >( ( ::cppu::OWeakObject* ) new ::toolkit::DefaultGridDataModel );