merge the formfield patch from ooo-build
[ooovba.git] / forms / source / component / entrylisthelper.cxx
blobd56ef982be49cc77c3bd4f4b5d17ea921909a882
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: entrylisthelper.cxx,v $
10 * $Revision: 1.9 $
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_forms.hxx"
33 #include "entrylisthelper.hxx"
34 #include "FormComponent.hxx"
36 #include <osl/diagnose.h>
37 #include <comphelper/sequence.hxx>
38 #include <comphelper/property.hxx>
39 #include <algorithm>
41 //.........................................................................
42 namespace frm
44 //.........................................................................
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::util;
49 using namespace ::com::sun::star::form::binding;
51 //=====================================================================
52 //= OEntryListHelper
53 //=====================================================================
54 //---------------------------------------------------------------------
55 OEntryListHelper::OEntryListHelper( OControlModel& _rControlModel )
56 :m_rControlModel( _rControlModel )
57 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
61 //---------------------------------------------------------------------
62 OEntryListHelper::OEntryListHelper( const OEntryListHelper& _rSource, OControlModel& _rControlModel )
63 :m_rControlModel( _rControlModel )
64 ,m_xListSource ( _rSource.m_xListSource )
65 ,m_aStringItems( _rSource.m_aStringItems )
66 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
70 //---------------------------------------------------------------------
71 OEntryListHelper::~OEntryListHelper( )
75 //---------------------------------------------------------------------
76 void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource ) throw (RuntimeException)
78 ControlModelLock aLock( m_rControlModel );
80 // disconnect from the current external list source
81 disconnectExternalListSource();
83 // and connect to the new one
84 if ( _rxSource.is() )
85 connectExternalListSource( _rxSource, aLock );
88 //---------------------------------------------------------------------
89 Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource( ) throw (RuntimeException)
91 return m_xListSource;
95 //---------------------------------------------------------------------
96 void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent ) throw (RuntimeException)
98 ControlModelLock aLock( m_rControlModel );
100 OSL_ENSURE( _rEvent.Source == m_xListSource,
101 "OEntryListHelper::entryChanged: where did this come from?" );
102 OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ),
103 "OEntryListHelper::entryChanged: invalid index!" );
104 OSL_ENSURE( _rEvent.Entries.getLength() == 1,
105 "OEntryListHelper::entryChanged: invalid string list!" );
107 if ( ( _rEvent.Position >= 0 )
108 && ( _rEvent.Position < m_aStringItems.getLength() )
109 && ( _rEvent.Entries.getLength() > 0 )
112 m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
113 stringItemListChanged( aLock );
117 //---------------------------------------------------------------------
118 void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent ) throw (RuntimeException)
120 ControlModelLock aLock( m_rControlModel );
122 OSL_ENSURE( _rEvent.Source == m_xListSource,
123 "OEntryListHelper::entryRangeInserted: where did this come from?" );
124 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ) && ( _rEvent.Entries.getLength() > 0 ),
125 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
127 if ( ( _rEvent.Position > 0 )
128 && ( _rEvent.Position < m_aStringItems.getLength() )
129 && ( _rEvent.Entries.getLength() > 0 )
132 // the entries *before* the insertion pos
133 Sequence< ::rtl::OUString > aKeepEntries(
134 m_aStringItems.getConstArray(),
135 _rEvent.Position
137 // the entries *behind* the insertion pos
138 Sequence< ::rtl::OUString > aMovedEntries(
139 m_aStringItems.getConstArray() + _rEvent.Position,
140 m_aStringItems.getLength() - _rEvent.Position
143 // concat all three parts
144 m_aStringItems = ::comphelper::concatSequences(
145 aKeepEntries,
146 _rEvent.Entries,
147 aMovedEntries
150 stringItemListChanged( aLock );
154 //---------------------------------------------------------------------
155 void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent ) throw (RuntimeException)
157 ControlModelLock aLock( m_rControlModel );
159 OSL_ENSURE( _rEvent.Source == m_xListSource,
160 "OEntryListHelper::entryRangeRemoved: where did this come from?" );
161 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() ),
162 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
164 if ( ( _rEvent.Position > 0 )
165 && ( _rEvent.Count > 0 )
166 && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() )
169 // copy all items after the removed ones
170 ::std::copy(
171 m_aStringItems.getConstArray() + _rEvent.Position + _rEvent.Count,
172 m_aStringItems.getConstArray() + m_aStringItems.getLength(),
173 m_aStringItems.getArray( ) + _rEvent.Position
175 // shrink the array
176 m_aStringItems.realloc( m_aStringItems.getLength() - _rEvent.Count );
178 stringItemListChanged( aLock );
182 //---------------------------------------------------------------------
183 void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent ) throw (RuntimeException)
185 ControlModelLock aLock( m_rControlModel );
187 OSL_ENSURE( _rEvent.Source == m_xListSource,
188 "OEntryListHelper::allEntriesChanged: where did this come from?" );
190 Reference< XListEntrySource > xSource( _rEvent.Source, UNO_QUERY );
191 if ( _rEvent.Source == m_xListSource )
193 impl_lock_refreshList( aLock );
197 // XRefreshable
198 //------------------------------------------------------------------------------
199 void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
201 if ( _rxListener.is() )
202 m_aRefreshListeners.addInterface( _rxListener );
205 //------------------------------------------------------------------------------
206 void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
208 if ( _rxListener.is() )
209 m_aRefreshListeners.removeInterface( _rxListener );
212 //------------------------------------------------------------------------------
213 void SAL_CALL OEntryListHelper::refresh() throw(RuntimeException)
216 ControlModelLock aLock( m_rControlModel );
217 impl_lock_refreshList( aLock );
220 EventObject aEvt( static_cast< XRefreshable* >( this ) );
221 m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
224 //---------------------------------------------------------------------
225 void OEntryListHelper::impl_lock_refreshList( ControlModelLock& _rInstanceLock )
227 if ( hasExternalListSource() )
229 m_aStringItems = m_xListSource->getAllListEntries( );
230 stringItemListChanged( _rInstanceLock );
232 else
233 refreshInternalEntryList();
236 //---------------------------------------------------------------------
237 bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
239 if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
241 disconnectExternalListSource( );
242 return true;
244 return false;
247 //---------------------------------------------------------------------
248 void OEntryListHelper::disposing( )
250 EventObject aEvt( static_cast< XRefreshable* >( this ) );
251 m_aRefreshListeners.disposeAndClear(aEvt);
253 if ( hasExternalListSource( ) )
254 disconnectExternalListSource( );
257 //---------------------------------------------------------------------
258 void OEntryListHelper::disconnectExternalListSource( )
260 if ( m_xListSource.is() )
261 m_xListSource->removeListEntryListener( this );
263 m_xListSource.clear();
265 disconnectedExternalListSource();
268 //---------------------------------------------------------------------
269 void OEntryListHelper::connectedExternalListSource( )
271 // nothing to do here
274 //---------------------------------------------------------------------
275 void OEntryListHelper::disconnectedExternalListSource( )
277 // nothing to do here
280 //---------------------------------------------------------------------
281 void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
283 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
284 OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
286 // remember it
287 m_xListSource = _rxSource;
289 // initially fill our item list
290 if ( m_xListSource.is() )
292 // be notified when the list changes ...
293 m_xListSource->addListEntryListener( this );
295 m_aStringItems = m_xListSource->getAllListEntries( );
296 stringItemListChanged( _rInstanceLock );
298 // let derivees react on the new list source
299 connectedExternalListSource();
303 //---------------------------------------------------------------------
304 sal_Bool OEntryListHelper::convertNewListSourceProperty( Any& _rConvertedValue,
305 Any& _rOldValue, const Any& _rValue ) SAL_THROW( ( IllegalArgumentException ) )
307 if ( hasExternalListSource() )
308 throw IllegalArgumentException( );
309 // TODO: error message
311 return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aStringItems );
314 //---------------------------------------------------------------------
315 void OEntryListHelper::setNewStringItemList( const ::com::sun::star::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
317 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
318 OSL_VERIFY( _rValue >>= m_aStringItems );
319 stringItemListChanged( _rInstanceLock );
322 //.........................................................................
323 } // namespace frm
324 //.........................................................................