bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / entrylisthelper.cxx
blob2139d708bdb77a55313d5176a4e78caa7cdda939
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "entrylisthelper.hxx"
21 #include "FormComponent.hxx"
23 #include <osl/diagnose.h>
24 #include <comphelper/sequence.hxx>
25 #include <comphelper/property.hxx>
26 #include <algorithm>
29 namespace frm
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::util;
36 using namespace ::com::sun::star::form::binding;
38 OEntryListHelper::OEntryListHelper( OControlModel& _rControlModel )
39 :m_rControlModel( _rControlModel )
40 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
45 OEntryListHelper::OEntryListHelper( const OEntryListHelper& _rSource, OControlModel& _rControlModel )
46 :m_rControlModel( _rControlModel )
47 ,m_xListSource ( _rSource.m_xListSource )
48 ,m_aStringItems( _rSource.m_aStringItems )
49 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
54 OEntryListHelper::~OEntryListHelper( )
59 void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource ) throw (RuntimeException, std::exception)
61 ControlModelLock aLock( m_rControlModel );
63 // disconnect from the current external list source
64 disconnectExternalListSource();
66 // and connect to the new one
67 if ( _rxSource.is() )
68 connectExternalListSource( _rxSource, aLock );
72 Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource( ) throw (RuntimeException, std::exception)
74 return m_xListSource;
79 void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent ) throw (RuntimeException, std::exception)
81 ControlModelLock aLock( m_rControlModel );
83 OSL_ENSURE( _rEvent.Source == m_xListSource,
84 "OEntryListHelper::entryChanged: where did this come from?" );
85 OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ),
86 "OEntryListHelper::entryChanged: invalid index!" );
87 OSL_ENSURE( _rEvent.Entries.getLength() == 1,
88 "OEntryListHelper::entryChanged: invalid string list!" );
90 if ( ( _rEvent.Position >= 0 )
91 && ( _rEvent.Position < m_aStringItems.getLength() )
92 && ( _rEvent.Entries.getLength() > 0 )
95 m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
96 stringItemListChanged( aLock );
101 void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent ) throw (RuntimeException, std::exception)
103 ControlModelLock aLock( m_rControlModel );
105 OSL_ENSURE( _rEvent.Source == m_xListSource,
106 "OEntryListHelper::entryRangeInserted: where did this come from?" );
107 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ) && ( _rEvent.Entries.getLength() > 0 ),
108 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
110 if ( ( _rEvent.Position > 0 )
111 && ( _rEvent.Position < m_aStringItems.getLength() )
112 && ( _rEvent.Entries.getLength() > 0 )
115 // the entries *before* the insertion pos
116 Sequence< OUString > aKeepEntries(
117 m_aStringItems.getConstArray(),
118 _rEvent.Position
120 // the entries *behind* the insertion pos
121 Sequence< OUString > aMovedEntries(
122 m_aStringItems.getConstArray() + _rEvent.Position,
123 m_aStringItems.getLength() - _rEvent.Position
126 // concat all three parts
127 m_aStringItems = ::comphelper::concatSequences(
128 aKeepEntries,
129 _rEvent.Entries,
130 aMovedEntries
133 stringItemListChanged( aLock );
138 void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent ) throw (RuntimeException, std::exception)
140 ControlModelLock aLock( m_rControlModel );
142 OSL_ENSURE( _rEvent.Source == m_xListSource,
143 "OEntryListHelper::entryRangeRemoved: where did this come from?" );
144 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() ),
145 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
147 if ( ( _rEvent.Position > 0 )
148 && ( _rEvent.Count > 0 )
149 && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() )
152 // copy all items after the removed ones
153 ::std::copy(
154 m_aStringItems.getConstArray() + _rEvent.Position + _rEvent.Count,
155 m_aStringItems.getConstArray() + m_aStringItems.getLength(),
156 m_aStringItems.getArray( ) + _rEvent.Position
158 // shrink the array
159 m_aStringItems.realloc( m_aStringItems.getLength() - _rEvent.Count );
161 stringItemListChanged( aLock );
166 void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent ) throw (RuntimeException, std::exception)
168 ControlModelLock aLock( m_rControlModel );
170 OSL_ENSURE( _rEvent.Source == m_xListSource,
171 "OEntryListHelper::allEntriesChanged: where did this come from?" );
173 Reference< XListEntrySource > xSource( _rEvent.Source, UNO_QUERY );
174 if ( _rEvent.Source == m_xListSource )
176 impl_lock_refreshList( aLock );
180 // XRefreshable
182 void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException, std::exception)
184 if ( _rxListener.is() )
185 m_aRefreshListeners.addInterface( _rxListener );
189 void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException, std::exception)
191 if ( _rxListener.is() )
192 m_aRefreshListeners.removeInterface( _rxListener );
196 void SAL_CALL OEntryListHelper::refresh() throw(RuntimeException, std::exception)
199 ControlModelLock aLock( m_rControlModel );
200 impl_lock_refreshList( aLock );
203 EventObject aEvt( static_cast< XRefreshable* >( this ) );
204 m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
208 void OEntryListHelper::impl_lock_refreshList( ControlModelLock& _rInstanceLock )
210 if ( hasExternalListSource() )
212 m_aStringItems = m_xListSource->getAllListEntries( );
213 stringItemListChanged( _rInstanceLock );
215 else
216 refreshInternalEntryList();
220 bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
222 if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
224 disconnectExternalListSource( );
225 return true;
227 return false;
231 void OEntryListHelper::disposing( )
233 EventObject aEvt( static_cast< XRefreshable* >( this ) );
234 m_aRefreshListeners.disposeAndClear(aEvt);
236 if ( hasExternalListSource( ) )
237 disconnectExternalListSource( );
241 void OEntryListHelper::disconnectExternalListSource( )
243 if ( m_xListSource.is() )
244 m_xListSource->removeListEntryListener( this );
246 m_xListSource.clear();
248 disconnectedExternalListSource();
252 void OEntryListHelper::connectedExternalListSource( )
254 // nothing to do here
258 void OEntryListHelper::disconnectedExternalListSource( )
260 // nothing to do here
264 void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
266 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
267 OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
269 // remember it
270 m_xListSource = _rxSource;
272 // initially fill our item list
273 if ( m_xListSource.is() )
275 // be notified when the list changes ...
276 m_xListSource->addListEntryListener( this );
278 m_aStringItems = m_xListSource->getAllListEntries( );
279 stringItemListChanged( _rInstanceLock );
281 // let derivees react on the new list source
282 connectedExternalListSource();
287 bool OEntryListHelper::convertNewListSourceProperty( Any& _rConvertedValue,
288 Any& _rOldValue, const Any& _rValue )
290 if ( hasExternalListSource() )
291 throw IllegalArgumentException( );
292 // TODO: error message
294 return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aStringItems );
298 void OEntryListHelper::setNewStringItemList( const ::com::sun::star::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
300 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
301 OSL_VERIFY( _rValue >>= m_aStringItems );
302 stringItemListChanged( _rInstanceLock );
306 } // namespace frm
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */