Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "gridcolumn.hxx"
22 :
23 : #include <com/sun/star/awt/XVclWindowPeer.hpp>
24 : #include <com/sun/star/awt/grid/XGridColumnModel.hpp>
25 : #include <com/sun/star/awt/grid/XGridColumn.hpp>
26 : #include <com/sun/star/lang/XServiceInfo.hpp>
27 : #include <com/sun/star/uno/XComponentContext.hpp>
28 :
29 : #include <comphelper/sequence.hxx>
30 : #include <comphelper/componentguard.hxx>
31 : #include <cppuhelper/basemutex.hxx>
32 : #include <cppuhelper/compbase2.hxx>
33 : #include <cppuhelper/supportsservice.hxx>
34 : #include <rtl/ustrbuf.hxx>
35 : #include <tools/diagnose_ex.h>
36 :
37 : #include <vector>
38 :
39 : using namespace css::awt;
40 : using namespace css::awt::grid;
41 : using namespace css::container;
42 : using namespace css::lang;
43 : using namespace css::uno;
44 : using namespace toolkit;
45 :
46 : namespace {
47 :
48 : typedef ::cppu::WeakComponentImplHelper2 < css::awt::grid::XGridColumnModel
49 : , css::lang::XServiceInfo
50 : > DefaultGridColumnModel_Base;
51 :
52 : class DefaultGridColumnModel :public ::cppu::BaseMutex
53 : ,public DefaultGridColumnModel_Base
54 : {
55 : public:
56 : DefaultGridColumnModel();
57 : DefaultGridColumnModel( DefaultGridColumnModel const & i_copySource );
58 : virtual ~DefaultGridColumnModel();
59 :
60 : // XGridColumnModel
61 : virtual ::sal_Int32 SAL_CALL getColumnCount() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
62 : virtual css::uno::Reference< css::awt::grid::XGridColumn > SAL_CALL createColumn( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
63 : virtual ::sal_Int32 SAL_CALL addColumn(const css::uno::Reference< css::awt::grid::XGridColumn > & column) throw (css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception) SAL_OVERRIDE;
64 : virtual void SAL_CALL removeColumn( ::sal_Int32 i_columnIndex ) throw (css::uno::RuntimeException, css::lang::IndexOutOfBoundsException, std::exception) SAL_OVERRIDE;
65 : virtual css::uno::Sequence< css::uno::Reference< css::awt::grid::XGridColumn > > SAL_CALL getColumns() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
66 : virtual css::uno::Reference< css::awt::grid::XGridColumn > SAL_CALL getColumn(::sal_Int32 index) throw (css::lang::IndexOutOfBoundsException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
67 : virtual void SAL_CALL setDefaultColumns(sal_Int32 rowElements) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
68 :
69 : // XServiceInfo
70 : virtual OUString SAL_CALL getImplementationName( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
72 : virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 :
74 : // XContainer
75 : virtual void SAL_CALL addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
76 : virtual void SAL_CALL removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
77 :
78 : // XCloneable
79 : virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone( ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
80 :
81 : // OComponentHelper
82 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
83 :
84 : private:
85 : typedef ::std::vector< css::uno::Reference< css::awt::grid::XGridColumn > > Columns;
86 :
87 : ::cppu::OInterfaceContainerHelper m_aContainerListeners;
88 : Columns m_aColumns;
89 : };
90 :
91 20 : DefaultGridColumnModel::DefaultGridColumnModel()
92 : :DefaultGridColumnModel_Base( m_aMutex )
93 : ,m_aContainerListeners( m_aMutex )
94 20 : ,m_aColumns()
95 : {
96 20 : }
97 :
98 2 : DefaultGridColumnModel::DefaultGridColumnModel( DefaultGridColumnModel const & i_copySource )
99 : :cppu::BaseMutex()
100 : ,DefaultGridColumnModel_Base( m_aMutex )
101 : ,m_aContainerListeners( m_aMutex )
102 2 : ,m_aColumns()
103 : {
104 2 : Columns aColumns;
105 2 : aColumns.reserve( i_copySource.m_aColumns.size() );
106 : try
107 : {
108 66 : for ( Columns::const_iterator col = i_copySource.m_aColumns.begin();
109 44 : col != i_copySource.m_aColumns.end();
110 : ++col
111 : )
112 : {
113 20 : Reference< css::util::XCloneable > const xCloneable( *col, UNO_QUERY_THROW );
114 40 : Reference< XGridColumn > const xClone( xCloneable->createClone(), UNO_QUERY_THROW );
115 :
116 20 : GridColumn* const pGridColumn = GridColumn::getImplementation( xClone );
117 20 : if ( pGridColumn == NULL )
118 0 : throw RuntimeException( "invalid clone source implementation", *this );
119 : // that's indeed a RuntimeException, not an IllegalArgumentException or some such:
120 : // a DefaultGridColumnModel implementation whose columns are not GridColumn implementations
121 : // is borked.
122 20 : pGridColumn->setIndex( col - i_copySource.m_aColumns.begin() );
123 :
124 20 : aColumns.push_back( xClone );
125 20 : }
126 : }
127 0 : catch( const Exception& )
128 : {
129 : DBG_UNHANDLED_EXCEPTION();
130 : }
131 2 : if ( aColumns.size() == i_copySource.m_aColumns.size() )
132 2 : m_aColumns.swap( aColumns );
133 2 : }
134 :
135 :
136 44 : DefaultGridColumnModel::~DefaultGridColumnModel()
137 : {
138 44 : }
139 :
140 :
141 112 : ::sal_Int32 SAL_CALL DefaultGridColumnModel::getColumnCount() throw (RuntimeException, std::exception)
142 : {
143 112 : return m_aColumns.size();
144 : }
145 :
146 :
147 4 : Reference< XGridColumn > SAL_CALL DefaultGridColumnModel::createColumn( ) throw (RuntimeException, std::exception)
148 : {
149 4 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
150 4 : return new GridColumn();
151 : }
152 :
153 :
154 6 : ::sal_Int32 SAL_CALL DefaultGridColumnModel::addColumn( const Reference< XGridColumn > & i_column )
155 : throw (RuntimeException, css::lang::IllegalArgumentException, std::exception)
156 : {
157 6 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
158 :
159 6 : GridColumn* const pGridColumn = GridColumn::getImplementation( i_column );
160 6 : if ( pGridColumn == NULL )
161 2 : throw css::lang::IllegalArgumentException( "invalid column implementation", *this, 1 );
162 :
163 4 : m_aColumns.push_back( i_column );
164 4 : sal_Int32 index = m_aColumns.size() - 1;
165 4 : pGridColumn->setIndex( index );
166 :
167 : // fire insertion notifications
168 8 : ContainerEvent aEvent;
169 4 : aEvent.Source = *this;
170 4 : aEvent.Accessor <<= index;
171 4 : aEvent.Element <<= i_column;
172 :
173 4 : aGuard.clear();
174 4 : m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
175 :
176 10 : return index;
177 : }
178 :
179 :
180 14 : void SAL_CALL DefaultGridColumnModel::removeColumn( ::sal_Int32 i_columnIndex )
181 : throw (RuntimeException, css::lang::IndexOutOfBoundsException, std::exception)
182 : {
183 14 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
184 :
185 14 : if ( ( i_columnIndex < 0 ) || ( size_t( i_columnIndex ) >= m_aColumns.size() ) )
186 0 : throw css::lang::IndexOutOfBoundsException( OUString(), *this );
187 :
188 14 : Columns::iterator const pos = m_aColumns.begin() + i_columnIndex;
189 28 : Reference< XGridColumn > const xColumn( *pos );
190 14 : m_aColumns.erase( pos );
191 :
192 : // update indexes of all subsequent columns
193 14 : sal_Int32 columnIndex( i_columnIndex );
194 87 : for ( Columns::iterator updatePos = m_aColumns.begin() + columnIndex;
195 58 : updatePos != m_aColumns.end();
196 : ++updatePos, ++columnIndex
197 : )
198 : {
199 15 : GridColumn* pColumnImpl = GridColumn::getImplementation( *updatePos );
200 15 : if ( !pColumnImpl )
201 : {
202 : SAL_WARN( "toolkit.controls", "DefaultGridColumnModel::removeColumn: invalid column implementation!" );
203 0 : continue;
204 : }
205 :
206 15 : pColumnImpl->setIndex( columnIndex );
207 : }
208 :
209 : // fire removal notifications
210 28 : ContainerEvent aEvent;
211 14 : aEvent.Source = *this;
212 14 : aEvent.Accessor <<= i_columnIndex;
213 14 : aEvent.Element <<= xColumn;
214 :
215 14 : aGuard.clear();
216 14 : m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
217 :
218 : // dispose the removed column
219 : try
220 : {
221 14 : xColumn->dispose();
222 : }
223 0 : catch( const Exception& )
224 : {
225 : DBG_UNHANDLED_EXCEPTION();
226 14 : }
227 14 : }
228 :
229 :
230 10 : Sequence< Reference< XGridColumn > > SAL_CALL DefaultGridColumnModel::getColumns() throw (RuntimeException, std::exception)
231 : {
232 10 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
233 10 : return ::comphelper::containerToSequence( m_aColumns );
234 : }
235 :
236 :
237 84 : Reference< XGridColumn > SAL_CALL DefaultGridColumnModel::getColumn(::sal_Int32 index)
238 : throw (css::lang::IndexOutOfBoundsException, RuntimeException, std::exception)
239 : {
240 84 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
241 :
242 84 : if ( index >=0 && index < ((sal_Int32)m_aColumns.size()))
243 168 : return m_aColumns[index];
244 :
245 0 : throw css::lang::IndexOutOfBoundsException();
246 : }
247 :
248 :
249 10 : void SAL_CALL DefaultGridColumnModel::setDefaultColumns(sal_Int32 rowElements) throw (RuntimeException, std::exception)
250 : {
251 10 : ::std::vector< ContainerEvent > aRemovedColumns;
252 20 : ::std::vector< ContainerEvent > aInsertedColumns;
253 :
254 : {
255 10 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
256 :
257 : // remove existing columns
258 26 : while ( !m_aColumns.empty() )
259 : {
260 6 : const size_t lastColIndex = m_aColumns.size() - 1;
261 :
262 6 : ContainerEvent aEvent;
263 6 : aEvent.Source = *this;
264 6 : aEvent.Accessor <<= sal_Int32( lastColIndex );
265 6 : aEvent.Element <<= m_aColumns[ lastColIndex ];
266 6 : aRemovedColumns.push_back( aEvent );
267 :
268 6 : m_aColumns.erase( m_aColumns.begin() + lastColIndex );
269 6 : }
270 :
271 : // add new columns
272 58 : for ( sal_Int32 i=0; i<rowElements; ++i )
273 : {
274 48 : ::rtl::Reference< GridColumn > const pGridColumn = new GridColumn();
275 96 : Reference< XGridColumn > const xColumn( pGridColumn.get() );
276 96 : OUStringBuffer colTitle;
277 48 : colTitle.appendAscii( "Column " );
278 48 : colTitle.append( i + 1 );
279 48 : pGridColumn->setTitle( colTitle.makeStringAndClear() );
280 48 : pGridColumn->setColumnWidth( 80 /* APPFONT */ );
281 48 : pGridColumn->setFlexibility( 1 );
282 48 : pGridColumn->setResizeable( sal_True );
283 48 : pGridColumn->setDataColumnIndex( i );
284 :
285 96 : ContainerEvent aEvent;
286 48 : aEvent.Source = *this;
287 48 : aEvent.Accessor <<= i;
288 48 : aEvent.Element <<= xColumn;
289 48 : aInsertedColumns.push_back( aEvent );
290 :
291 48 : m_aColumns.push_back( xColumn );
292 48 : pGridColumn->setIndex( i );
293 58 : }
294 : }
295 :
296 : // fire removal notifications
297 48 : for ( ::std::vector< ContainerEvent >::const_iterator event = aRemovedColumns.begin();
298 32 : event != aRemovedColumns.end();
299 : ++event
300 : )
301 : {
302 6 : m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, *event );
303 : }
304 :
305 : // fire insertion notifications
306 174 : for ( ::std::vector< ContainerEvent >::const_iterator event = aInsertedColumns.begin();
307 116 : event != aInsertedColumns.end();
308 : ++event
309 : )
310 : {
311 48 : m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, *event );
312 : }
313 :
314 : // dispose removed columns
315 48 : for ( ::std::vector< ContainerEvent >::const_iterator event = aRemovedColumns.begin();
316 32 : event != aRemovedColumns.end();
317 : ++event
318 : )
319 : {
320 : try
321 : {
322 6 : const Reference< XComponent > xColComp( event->Element, UNO_QUERY_THROW );
323 6 : xColComp->dispose();
324 : }
325 0 : catch( const Exception& )
326 : {
327 : DBG_UNHANDLED_EXCEPTION();
328 : }
329 10 : }
330 10 : }
331 :
332 :
333 0 : OUString SAL_CALL DefaultGridColumnModel::getImplementationName( ) throw (RuntimeException, std::exception)
334 : {
335 0 : return OUString("stardiv.Toolkit.DefaultGridColumnModel");
336 : }
337 :
338 0 : sal_Bool SAL_CALL DefaultGridColumnModel::supportsService( const OUString& i_serviceName ) throw (RuntimeException, std::exception)
339 : {
340 0 : return cppu::supportsService(this, i_serviceName);
341 : }
342 :
343 0 : Sequence< OUString > SAL_CALL DefaultGridColumnModel::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
344 : {
345 0 : const OUString aServiceName("com.sun.star.awt.grid.DefaultGridColumnModel");
346 0 : const Sequence< OUString > aSeq( &aServiceName, 1 );
347 0 : return aSeq;
348 : }
349 :
350 :
351 4 : void SAL_CALL DefaultGridColumnModel::addContainerListener( const Reference< XContainerListener >& i_listener ) throw (RuntimeException, std::exception)
352 : {
353 4 : if ( i_listener.is() )
354 4 : m_aContainerListeners.addInterface( i_listener );
355 4 : }
356 :
357 :
358 2 : void SAL_CALL DefaultGridColumnModel::removeContainerListener( const Reference< XContainerListener >& i_listener ) throw (RuntimeException, std::exception)
359 : {
360 2 : if ( i_listener.is() )
361 2 : m_aContainerListeners.removeInterface( i_listener );
362 2 : }
363 :
364 :
365 22 : void SAL_CALL DefaultGridColumnModel::disposing()
366 : {
367 22 : DefaultGridColumnModel_Base::disposing();
368 :
369 22 : EventObject aEvent( *this );
370 22 : m_aContainerListeners.disposeAndClear( aEvent );
371 :
372 44 : ::osl::MutexGuard aGuard( m_aMutex );
373 :
374 : // remove, dispose and clear columns
375 96 : while ( !m_aColumns.empty() )
376 : {
377 : try
378 : {
379 52 : const Reference< XComponent > xColComponent( m_aColumns[ 0 ], UNO_QUERY_THROW );
380 52 : xColComponent->dispose();
381 : }
382 0 : catch( const Exception& )
383 : {
384 : DBG_UNHANDLED_EXCEPTION();
385 : }
386 :
387 52 : m_aColumns.erase( m_aColumns.begin() );
388 : }
389 :
390 44 : Columns aEmpty;
391 44 : m_aColumns.swap( aEmpty );
392 22 : }
393 :
394 :
395 2 : Reference< css::util::XCloneable > SAL_CALL DefaultGridColumnModel::createClone( ) throw (RuntimeException, std::exception)
396 : {
397 2 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
398 2 : return new DefaultGridColumnModel( *this );
399 : }
400 :
401 : }
402 :
403 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
404 20 : stardiv_Toolkit_DefaultGridColumnModel_get_implementation(
405 : css::uno::XComponentContext *,
406 : css::uno::Sequence<css::uno::Any> const &)
407 : {
408 20 : return cppu::acquire(new DefaultGridColumnModel());
409 : }
410 :
411 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|