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 "defaultgridcolumnmodel.hxx"
22 : #include "gridcolumn.hxx"
23 :
24 : #include <com/sun/star/awt/XVclWindowPeer.hpp>
25 :
26 : #include <comphelper/sequence.hxx>
27 : #include <comphelper/componentguard.hxx>
28 : #include <toolkit/helper/servicenames.hxx>
29 : #include <rtl/ustrbuf.hxx>
30 : #include <tools/diagnose_ex.h>
31 :
32 : //......................................................................................................................
33 : namespace toolkit
34 : //......................................................................................................................
35 : {
36 : /** === begin UNO using === **/
37 : using ::com::sun::star::uno::Reference;
38 : using ::com::sun::star::lang::XMultiServiceFactory;
39 : using ::com::sun::star::uno::RuntimeException;
40 : using ::com::sun::star::uno::Sequence;
41 : using ::com::sun::star::uno::UNO_QUERY_THROW;
42 : using ::com::sun::star::uno::UNO_QUERY;
43 : using ::com::sun::star::awt::grid::XGridColumn;
44 : using ::com::sun::star::uno::XInterface;
45 : using ::com::sun::star::lang::XMultiServiceFactory;
46 : using ::com::sun::star::lang::XComponent;
47 : using ::com::sun::star::lang::EventObject;
48 : using ::com::sun::star::container::XContainerListener;
49 : using ::com::sun::star::container::ContainerEvent;
50 : using ::com::sun::star::uno::Exception;
51 : using ::com::sun::star::lang::IndexOutOfBoundsException;
52 : using ::com::sun::star::util::XCloneable;
53 : using ::com::sun::star::lang::IllegalArgumentException;
54 : /** === end UNO using === **/
55 :
56 : //==================================================================================================================
57 : //= DefaultGridColumnModel
58 : //==================================================================================================================
59 : //------------------------------------------------------------------------------------------------------------------
60 0 : DefaultGridColumnModel::DefaultGridColumnModel( const Reference< XMultiServiceFactory >& i_factory )
61 : :DefaultGridColumnModel_Base( m_aMutex )
62 : ,m_aContext( i_factory )
63 : ,m_aContainerListeners( m_aMutex )
64 0 : ,m_aColumns()
65 : {
66 0 : }
67 :
68 : //------------------------------------------------------------------------------------------------------------------
69 0 : DefaultGridColumnModel::DefaultGridColumnModel( DefaultGridColumnModel const & i_copySource )
70 : :cppu::BaseMutex()
71 : ,DefaultGridColumnModel_Base( m_aMutex )
72 : ,m_aContext( i_copySource.m_aContext )
73 : ,m_aContainerListeners( m_aMutex )
74 0 : ,m_aColumns()
75 : {
76 0 : Columns aColumns;
77 0 : aColumns.reserve( i_copySource.m_aColumns.size() );
78 : try
79 : {
80 0 : for ( Columns::const_iterator col = i_copySource.m_aColumns.begin();
81 0 : col != i_copySource.m_aColumns.end();
82 : ++col
83 : )
84 : {
85 0 : Reference< XCloneable > const xCloneable( *col, UNO_QUERY_THROW );
86 0 : Reference< XGridColumn > const xClone( xCloneable->createClone(), UNO_QUERY_THROW );
87 :
88 0 : GridColumn* const pGridColumn = GridColumn::getImplementation( xClone );
89 0 : if ( pGridColumn == NULL )
90 0 : throw RuntimeException( "invalid clone source implementation", *this );
91 : // that's indeed a RuntimeException, not an IllegalArgumentException or some such:
92 : // a DefaultGridColumnModel implementation whose columns are not GridColumn implementations
93 : // is borked.
94 0 : pGridColumn->setIndex( col - i_copySource.m_aColumns.begin() );
95 :
96 0 : aColumns.push_back( xClone );
97 0 : }
98 : }
99 0 : catch( const Exception& )
100 : {
101 : DBG_UNHANDLED_EXCEPTION();
102 : }
103 0 : if ( aColumns.size() == i_copySource.m_aColumns.size() )
104 0 : m_aColumns.swap( aColumns );
105 0 : }
106 :
107 : //------------------------------------------------------------------------------------------------------------------
108 0 : DefaultGridColumnModel::~DefaultGridColumnModel()
109 : {
110 0 : }
111 :
112 : //------------------------------------------------------------------------------------------------------------------
113 0 : ::sal_Int32 SAL_CALL DefaultGridColumnModel::getColumnCount() throw (RuntimeException)
114 : {
115 0 : return m_aColumns.size();
116 : }
117 :
118 : //------------------------------------------------------------------------------------------------------------------
119 0 : Reference< XGridColumn > SAL_CALL DefaultGridColumnModel::createColumn( ) throw (RuntimeException)
120 : {
121 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
122 0 : return new GridColumn();
123 : }
124 :
125 : //------------------------------------------------------------------------------------------------------------------
126 0 : ::sal_Int32 SAL_CALL DefaultGridColumnModel::addColumn( const Reference< XGridColumn > & i_column ) throw (RuntimeException, IllegalArgumentException)
127 : {
128 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
129 :
130 0 : GridColumn* const pGridColumn = GridColumn::getImplementation( i_column );
131 0 : if ( pGridColumn == NULL )
132 0 : throw IllegalArgumentException( "invalid column implementation", *this, 1 );
133 :
134 0 : m_aColumns.push_back( i_column );
135 0 : sal_Int32 index = m_aColumns.size() - 1;
136 0 : pGridColumn->setIndex( index );
137 :
138 : // fire insertion notifications
139 0 : ContainerEvent aEvent;
140 0 : aEvent.Source = *this;
141 0 : aEvent.Accessor <<= index;
142 0 : aEvent.Element <<= i_column;
143 :
144 0 : aGuard.clear();
145 0 : m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
146 :
147 0 : return index;
148 : }
149 :
150 : //------------------------------------------------------------------------------------------------------------------
151 0 : void SAL_CALL DefaultGridColumnModel::removeColumn( ::sal_Int32 i_columnIndex ) throw (RuntimeException, IndexOutOfBoundsException)
152 : {
153 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
154 :
155 0 : if ( ( i_columnIndex < 0 ) || ( size_t( i_columnIndex ) >= m_aColumns.size() ) )
156 0 : throw IndexOutOfBoundsException( ::rtl::OUString(), *this );
157 :
158 0 : Columns::iterator const pos = m_aColumns.begin() + i_columnIndex;
159 0 : Reference< XGridColumn > const xColumn( *pos );
160 0 : m_aColumns.erase( pos );
161 :
162 : // update indexes of all subsequent columns
163 0 : sal_Int32 columnIndex( i_columnIndex );
164 0 : for ( Columns::iterator updatePos = m_aColumns.begin() + columnIndex;
165 0 : updatePos != m_aColumns.end();
166 : ++updatePos, ++columnIndex
167 : )
168 : {
169 0 : GridColumn* pColumnImpl = GridColumn::getImplementation( *updatePos );
170 0 : if ( !pColumnImpl )
171 : {
172 : SAL_WARN( "toolkit.controls", "DefaultGridColumnModel::removeColumn: invalid column implementation!" );
173 0 : continue;
174 : }
175 :
176 0 : pColumnImpl->setIndex( columnIndex );
177 : }
178 :
179 : // fire removal notifications
180 0 : ContainerEvent aEvent;
181 0 : aEvent.Source = *this;
182 0 : aEvent.Accessor <<= i_columnIndex;
183 0 : aEvent.Element <<= xColumn;
184 :
185 0 : aGuard.clear();
186 0 : m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvent );
187 :
188 : // dispose the removed column
189 : try
190 : {
191 0 : Reference< XComponent > const xColComp( xColumn, UNO_QUERY_THROW );
192 0 : xColComp->dispose();
193 : }
194 0 : catch( const Exception& )
195 : {
196 : DBG_UNHANDLED_EXCEPTION();
197 0 : }
198 0 : }
199 :
200 : //------------------------------------------------------------------------------------------------------------------
201 0 : Sequence< Reference< XGridColumn > > SAL_CALL DefaultGridColumnModel::getColumns() throw (RuntimeException)
202 : {
203 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
204 0 : return ::comphelper::containerToSequence( m_aColumns );
205 : }
206 :
207 : //------------------------------------------------------------------------------------------------------------------
208 0 : Reference< XGridColumn > SAL_CALL DefaultGridColumnModel::getColumn(::sal_Int32 index) throw (IndexOutOfBoundsException, RuntimeException)
209 : {
210 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
211 :
212 0 : if ( index >=0 && index < ((sal_Int32)m_aColumns.size()))
213 0 : return m_aColumns[index];
214 :
215 0 : throw IndexOutOfBoundsException();
216 : }
217 :
218 : //------------------------------------------------------------------------------------------------------------------
219 0 : void SAL_CALL DefaultGridColumnModel::setDefaultColumns(sal_Int32 rowElements) throw (RuntimeException)
220 : {
221 0 : ::std::vector< ContainerEvent > aRemovedColumns;
222 0 : ::std::vector< ContainerEvent > aInsertedColumns;
223 :
224 : {
225 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
226 :
227 : // remove existing columns
228 0 : while ( !m_aColumns.empty() )
229 : {
230 0 : const size_t lastColIndex = m_aColumns.size() - 1;
231 :
232 0 : ContainerEvent aEvent;
233 0 : aEvent.Source = *this;
234 0 : aEvent.Accessor <<= sal_Int32( lastColIndex );
235 0 : aEvent.Element <<= m_aColumns[ lastColIndex ];
236 0 : aRemovedColumns.push_back( aEvent );
237 :
238 0 : m_aColumns.erase( m_aColumns.begin() + lastColIndex );
239 0 : }
240 :
241 : // add new columns
242 0 : for ( sal_Int32 i=0; i<rowElements; ++i )
243 : {
244 0 : ::rtl::Reference< GridColumn > const pGridColumn = new GridColumn();
245 0 : Reference< XGridColumn > const xColumn( pGridColumn.get() );
246 0 : ::rtl::OUStringBuffer colTitle;
247 0 : colTitle.appendAscii( "Column " );
248 0 : colTitle.append( i + 1 );
249 0 : pGridColumn->setTitle( colTitle.makeStringAndClear() );
250 0 : pGridColumn->setColumnWidth( 80 /* APPFONT */ );
251 0 : pGridColumn->setFlexibility( 1 );
252 0 : pGridColumn->setResizeable( sal_True );
253 0 : pGridColumn->setDataColumnIndex( i );
254 :
255 0 : ContainerEvent aEvent;
256 0 : aEvent.Source = *this;
257 0 : aEvent.Accessor <<= i;
258 0 : aEvent.Element <<= xColumn;
259 0 : aInsertedColumns.push_back( aEvent );
260 :
261 0 : m_aColumns.push_back( xColumn );
262 0 : pGridColumn->setIndex( i );
263 0 : }
264 : }
265 :
266 : // fire removal notifications
267 0 : for ( ::std::vector< ContainerEvent >::const_iterator event = aRemovedColumns.begin();
268 0 : event != aRemovedColumns.end();
269 : ++event
270 : )
271 : {
272 0 : m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, *event );
273 : }
274 :
275 : // fire insertion notifications
276 0 : for ( ::std::vector< ContainerEvent >::const_iterator event = aInsertedColumns.begin();
277 0 : event != aInsertedColumns.end();
278 : ++event
279 : )
280 : {
281 0 : m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, *event );
282 : }
283 :
284 : // dispose removed columns
285 0 : for ( ::std::vector< ContainerEvent >::const_iterator event = aRemovedColumns.begin();
286 0 : event != aRemovedColumns.end();
287 : ++event
288 : )
289 : {
290 : try
291 : {
292 0 : const Reference< XComponent > xColComp( event->Element, UNO_QUERY_THROW );
293 0 : xColComp->dispose();
294 : }
295 0 : catch( const Exception& )
296 : {
297 : DBG_UNHANDLED_EXCEPTION();
298 : }
299 0 : }
300 0 : }
301 :
302 : //------------------------------------------------------------------------------------------------------------------
303 0 : ::rtl::OUString SAL_CALL DefaultGridColumnModel::getImplementationName( ) throw (RuntimeException)
304 : {
305 0 : return ::rtl::OUString( "org.openoffice.comp.toolkit.DefaultGridColumnModel" );
306 : }
307 :
308 : //------------------------------------------------------------------------------------------------------------------
309 0 : sal_Bool SAL_CALL DefaultGridColumnModel::supportsService( const ::rtl::OUString& i_serviceName ) throw (RuntimeException)
310 : {
311 0 : const Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() );
312 0 : for ( sal_Int32 i=0; i<aServiceNames.getLength(); ++i )
313 0 : if ( aServiceNames[i] == i_serviceName )
314 0 : return sal_True;
315 0 : return sal_False;
316 : }
317 :
318 : //------------------------------------------------------------------------------------------------------------------
319 0 : Sequence< ::rtl::OUString > SAL_CALL DefaultGridColumnModel::getSupportedServiceNames( ) throw (RuntimeException)
320 : {
321 0 : const ::rtl::OUString aServiceName( ::rtl::OUString::createFromAscii( szServiceName_DefaultGridColumnModel ) );
322 0 : const Sequence< ::rtl::OUString > aSeq( &aServiceName, 1 );
323 0 : return aSeq;
324 : }
325 :
326 : //------------------------------------------------------------------------------------------------------------------
327 0 : void SAL_CALL DefaultGridColumnModel::addContainerListener( const Reference< XContainerListener >& i_listener ) throw (RuntimeException)
328 : {
329 0 : if ( i_listener.is() )
330 0 : m_aContainerListeners.addInterface( i_listener );
331 0 : }
332 :
333 : //------------------------------------------------------------------------------------------------------------------
334 0 : void SAL_CALL DefaultGridColumnModel::removeContainerListener( const Reference< XContainerListener >& i_listener ) throw (RuntimeException)
335 : {
336 0 : if ( i_listener.is() )
337 0 : m_aContainerListeners.removeInterface( i_listener );
338 0 : }
339 :
340 : //------------------------------------------------------------------------------------------------------------------
341 0 : void SAL_CALL DefaultGridColumnModel::disposing()
342 : {
343 0 : DefaultGridColumnModel_Base::disposing();
344 :
345 0 : EventObject aEvent( *this );
346 0 : m_aContainerListeners.disposeAndClear( aEvent );
347 :
348 0 : ::osl::MutexGuard aGuard( m_aMutex );
349 :
350 : // remove, dispose and clear columns
351 0 : while ( !m_aColumns.empty() )
352 : {
353 : try
354 : {
355 0 : const Reference< XComponent > xColComponent( m_aColumns[ 0 ], UNO_QUERY_THROW );
356 0 : xColComponent->dispose();
357 : }
358 0 : catch( const Exception& )
359 : {
360 : DBG_UNHANDLED_EXCEPTION();
361 : }
362 :
363 0 : m_aColumns.erase( m_aColumns.begin() );
364 : }
365 :
366 0 : Columns aEmpty;
367 0 : m_aColumns.swap( aEmpty );
368 0 : }
369 :
370 : //------------------------------------------------------------------------------------------------------------------
371 0 : Reference< XCloneable > SAL_CALL DefaultGridColumnModel::createClone( ) throw (RuntimeException)
372 : {
373 0 : ::comphelper::ComponentGuard aGuard( *this, rBHelper );
374 0 : return new DefaultGridColumnModel( *this );
375 : }
376 :
377 : //......................................................................................................................
378 : } // namespace toolkit
379 : //......................................................................................................................
380 :
381 : //----------------------------------------------------------------------------------------------------------------------
382 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL DefaultGridColumnModel_CreateInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rFactory)
383 : {
384 0 : return ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >( ( ::cppu::OWeakObject* ) new ::toolkit::DefaultGridColumnModel( _rFactory ) );
385 : }
386 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|