bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / component / entrylisthelper.cxx
blob07df31f2e87a72ce2a1b2ed7b5f20b246b91c945
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>
28 //.........................................................................
29 namespace frm
31 //.........................................................................
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 //=====================================================================
39 //= OEntryListHelper
40 //=====================================================================
41 //---------------------------------------------------------------------
42 OEntryListHelper::OEntryListHelper( OControlModel& _rControlModel )
43 :m_rControlModel( _rControlModel )
44 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
48 //---------------------------------------------------------------------
49 OEntryListHelper::OEntryListHelper( const OEntryListHelper& _rSource, OControlModel& _rControlModel )
50 :m_rControlModel( _rControlModel )
51 ,m_xListSource ( _rSource.m_xListSource )
52 ,m_aStringItems( _rSource.m_aStringItems )
53 ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
57 //---------------------------------------------------------------------
58 OEntryListHelper::~OEntryListHelper( )
62 //---------------------------------------------------------------------
63 void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource ) throw (RuntimeException)
65 ControlModelLock aLock( m_rControlModel );
67 // disconnect from the current external list source
68 disconnectExternalListSource();
70 // and connect to the new one
71 if ( _rxSource.is() )
72 connectExternalListSource( _rxSource, aLock );
75 //---------------------------------------------------------------------
76 Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource( ) throw (RuntimeException)
78 return m_xListSource;
82 //---------------------------------------------------------------------
83 void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent ) throw (RuntimeException)
85 ControlModelLock aLock( m_rControlModel );
87 OSL_ENSURE( _rEvent.Source == m_xListSource,
88 "OEntryListHelper::entryChanged: where did this come from?" );
89 OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ),
90 "OEntryListHelper::entryChanged: invalid index!" );
91 OSL_ENSURE( _rEvent.Entries.getLength() == 1,
92 "OEntryListHelper::entryChanged: invalid string list!" );
94 if ( ( _rEvent.Position >= 0 )
95 && ( _rEvent.Position < m_aStringItems.getLength() )
96 && ( _rEvent.Entries.getLength() > 0 )
99 m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
100 stringItemListChanged( aLock );
104 //---------------------------------------------------------------------
105 void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent ) throw (RuntimeException)
107 ControlModelLock aLock( m_rControlModel );
109 OSL_ENSURE( _rEvent.Source == m_xListSource,
110 "OEntryListHelper::entryRangeInserted: where did this come from?" );
111 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ) && ( _rEvent.Entries.getLength() > 0 ),
112 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
114 if ( ( _rEvent.Position > 0 )
115 && ( _rEvent.Position < m_aStringItems.getLength() )
116 && ( _rEvent.Entries.getLength() > 0 )
119 // the entries *before* the insertion pos
120 Sequence< OUString > aKeepEntries(
121 m_aStringItems.getConstArray(),
122 _rEvent.Position
124 // the entries *behind* the insertion pos
125 Sequence< OUString > aMovedEntries(
126 m_aStringItems.getConstArray() + _rEvent.Position,
127 m_aStringItems.getLength() - _rEvent.Position
130 // concat all three parts
131 m_aStringItems = ::comphelper::concatSequences(
132 aKeepEntries,
133 _rEvent.Entries,
134 aMovedEntries
137 stringItemListChanged( aLock );
141 //---------------------------------------------------------------------
142 void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent ) throw (RuntimeException)
144 ControlModelLock aLock( m_rControlModel );
146 OSL_ENSURE( _rEvent.Source == m_xListSource,
147 "OEntryListHelper::entryRangeRemoved: where did this come from?" );
148 OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() ),
149 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
151 if ( ( _rEvent.Position > 0 )
152 && ( _rEvent.Count > 0 )
153 && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() )
156 // copy all items after the removed ones
157 ::std::copy(
158 m_aStringItems.getConstArray() + _rEvent.Position + _rEvent.Count,
159 m_aStringItems.getConstArray() + m_aStringItems.getLength(),
160 m_aStringItems.getArray( ) + _rEvent.Position
162 // shrink the array
163 m_aStringItems.realloc( m_aStringItems.getLength() - _rEvent.Count );
165 stringItemListChanged( aLock );
169 //---------------------------------------------------------------------
170 void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent ) throw (RuntimeException)
172 ControlModelLock aLock( m_rControlModel );
174 OSL_ENSURE( _rEvent.Source == m_xListSource,
175 "OEntryListHelper::allEntriesChanged: where did this come from?" );
177 Reference< XListEntrySource > xSource( _rEvent.Source, UNO_QUERY );
178 if ( _rEvent.Source == m_xListSource )
180 impl_lock_refreshList( aLock );
184 // XRefreshable
185 //------------------------------------------------------------------------------
186 void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
188 if ( _rxListener.is() )
189 m_aRefreshListeners.addInterface( _rxListener );
192 //------------------------------------------------------------------------------
193 void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
195 if ( _rxListener.is() )
196 m_aRefreshListeners.removeInterface( _rxListener );
199 //------------------------------------------------------------------------------
200 void SAL_CALL OEntryListHelper::refresh() throw(RuntimeException)
203 ControlModelLock aLock( m_rControlModel );
204 impl_lock_refreshList( aLock );
207 EventObject aEvt( static_cast< XRefreshable* >( this ) );
208 m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
211 //---------------------------------------------------------------------
212 void OEntryListHelper::impl_lock_refreshList( ControlModelLock& _rInstanceLock )
214 if ( hasExternalListSource() )
216 m_aStringItems = m_xListSource->getAllListEntries( );
217 stringItemListChanged( _rInstanceLock );
219 else
220 refreshInternalEntryList();
223 //---------------------------------------------------------------------
224 bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
226 if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
228 disconnectExternalListSource( );
229 return true;
231 return false;
234 //---------------------------------------------------------------------
235 void OEntryListHelper::disposing( )
237 EventObject aEvt( static_cast< XRefreshable* >( this ) );
238 m_aRefreshListeners.disposeAndClear(aEvt);
240 if ( hasExternalListSource( ) )
241 disconnectExternalListSource( );
244 //---------------------------------------------------------------------
245 void OEntryListHelper::disconnectExternalListSource( )
247 if ( m_xListSource.is() )
248 m_xListSource->removeListEntryListener( this );
250 m_xListSource.clear();
252 disconnectedExternalListSource();
255 //---------------------------------------------------------------------
256 void OEntryListHelper::connectedExternalListSource( )
258 // nothing to do here
261 //---------------------------------------------------------------------
262 void OEntryListHelper::disconnectedExternalListSource( )
264 // nothing to do here
267 //---------------------------------------------------------------------
268 void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
270 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
271 OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
273 // remember it
274 m_xListSource = _rxSource;
276 // initially fill our item list
277 if ( m_xListSource.is() )
279 // be notified when the list changes ...
280 m_xListSource->addListEntryListener( this );
282 m_aStringItems = m_xListSource->getAllListEntries( );
283 stringItemListChanged( _rInstanceLock );
285 // let derivees react on the new list source
286 connectedExternalListSource();
290 //---------------------------------------------------------------------
291 sal_Bool OEntryListHelper::convertNewListSourceProperty( Any& _rConvertedValue,
292 Any& _rOldValue, const Any& _rValue ) SAL_THROW( ( IllegalArgumentException ) )
294 if ( hasExternalListSource() )
295 throw IllegalArgumentException( );
296 // TODO: error message
298 return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aStringItems );
301 //---------------------------------------------------------------------
302 void OEntryListHelper::setNewStringItemList( const ::com::sun::star::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
304 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
305 OSL_VERIFY( _rValue >>= m_aStringItems );
306 stringItemListChanged( _rInstanceLock );
309 //.........................................................................
310 } // namespace frm
311 //.........................................................................
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */