1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir #include "entrylisthelper.hxx"
31*cdf0e10cSrcweir #include "FormComponent.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <osl/diagnose.h>
34*cdf0e10cSrcweir #include <comphelper/sequence.hxx>
35*cdf0e10cSrcweir #include <comphelper/property.hxx>
36*cdf0e10cSrcweir #include <algorithm>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir //.........................................................................
39*cdf0e10cSrcweir namespace frm
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir //.........................................................................
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
44*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
45*cdf0e10cSrcweir     using namespace ::com::sun::star::util;
46*cdf0e10cSrcweir     using namespace ::com::sun::star::form::binding;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir     //=====================================================================
49*cdf0e10cSrcweir     //= OEntryListHelper
50*cdf0e10cSrcweir     //=====================================================================
51*cdf0e10cSrcweir     //---------------------------------------------------------------------
52*cdf0e10cSrcweir     OEntryListHelper::OEntryListHelper( OControlModel& _rControlModel )
53*cdf0e10cSrcweir         :m_rControlModel( _rControlModel )
54*cdf0e10cSrcweir         ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir     }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     //---------------------------------------------------------------------
59*cdf0e10cSrcweir     OEntryListHelper::OEntryListHelper( const OEntryListHelper& _rSource, OControlModel& _rControlModel )
60*cdf0e10cSrcweir         :m_rControlModel( _rControlModel )
61*cdf0e10cSrcweir         ,m_xListSource ( _rSource.m_xListSource  )
62*cdf0e10cSrcweir         ,m_aStringItems( _rSource.m_aStringItems )
63*cdf0e10cSrcweir         ,m_aRefreshListeners( _rControlModel.getInstanceMutex() )
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     //---------------------------------------------------------------------
68*cdf0e10cSrcweir     OEntryListHelper::~OEntryListHelper( )
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     //---------------------------------------------------------------------
73*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::setListEntrySource( const Reference< XListEntrySource >& _rxSource ) throw (RuntimeException)
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         ControlModelLock aLock( m_rControlModel );
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         // disconnect from the current external list source
78*cdf0e10cSrcweir         disconnectExternalListSource();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         // and connect to the new one
81*cdf0e10cSrcweir         if ( _rxSource.is() )
82*cdf0e10cSrcweir             connectExternalListSource( _rxSource, aLock );
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     //---------------------------------------------------------------------
86*cdf0e10cSrcweir     Reference< XListEntrySource > SAL_CALL OEntryListHelper::getListEntrySource(  ) throw (RuntimeException)
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         return m_xListSource;
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     //---------------------------------------------------------------------
93*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::entryChanged( const ListEntryEvent& _rEvent ) throw (RuntimeException)
94*cdf0e10cSrcweir     {
95*cdf0e10cSrcweir         ControlModelLock aLock( m_rControlModel );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         OSL_ENSURE( _rEvent.Source == m_xListSource,
98*cdf0e10cSrcweir             "OEntryListHelper::entryChanged: where did this come from?" );
99*cdf0e10cSrcweir         OSL_ENSURE( ( _rEvent.Position >= 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ),
100*cdf0e10cSrcweir             "OEntryListHelper::entryChanged: invalid index!" );
101*cdf0e10cSrcweir         OSL_ENSURE( _rEvent.Entries.getLength() == 1,
102*cdf0e10cSrcweir             "OEntryListHelper::entryChanged: invalid string list!" );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         if  (   ( _rEvent.Position >= 0 )
105*cdf0e10cSrcweir             &&  ( _rEvent.Position < m_aStringItems.getLength() )
106*cdf0e10cSrcweir             &&  ( _rEvent.Entries.getLength() > 0 )
107*cdf0e10cSrcweir             )
108*cdf0e10cSrcweir         {
109*cdf0e10cSrcweir             m_aStringItems[ _rEvent.Position ] = _rEvent.Entries[ 0 ];
110*cdf0e10cSrcweir             stringItemListChanged( aLock );
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     //---------------------------------------------------------------------
115*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::entryRangeInserted( const ListEntryEvent& _rEvent ) throw (RuntimeException)
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         ControlModelLock aLock( m_rControlModel );
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         OSL_ENSURE( _rEvent.Source == m_xListSource,
120*cdf0e10cSrcweir             "OEntryListHelper::entryRangeInserted: where did this come from?" );
121*cdf0e10cSrcweir         OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Position < m_aStringItems.getLength() ) && ( _rEvent.Entries.getLength() > 0 ),
122*cdf0e10cSrcweir             "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir         if  (   ( _rEvent.Position > 0 )
125*cdf0e10cSrcweir             &&  ( _rEvent.Position < m_aStringItems.getLength() )
126*cdf0e10cSrcweir             &&  ( _rEvent.Entries.getLength() > 0 )
127*cdf0e10cSrcweir             )
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir             // the entries *before* the insertion pos
130*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aKeepEntries(
131*cdf0e10cSrcweir                 m_aStringItems.getConstArray(),
132*cdf0e10cSrcweir                 _rEvent.Position
133*cdf0e10cSrcweir             );
134*cdf0e10cSrcweir             // the entries *behind* the insertion pos
135*cdf0e10cSrcweir             Sequence< ::rtl::OUString > aMovedEntries(
136*cdf0e10cSrcweir                 m_aStringItems.getConstArray() + _rEvent.Position,
137*cdf0e10cSrcweir                 m_aStringItems.getLength() - _rEvent.Position
138*cdf0e10cSrcweir             );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir             // concat all three parts
141*cdf0e10cSrcweir             m_aStringItems = ::comphelper::concatSequences(
142*cdf0e10cSrcweir                 aKeepEntries,
143*cdf0e10cSrcweir                 _rEvent.Entries,
144*cdf0e10cSrcweir                 aMovedEntries
145*cdf0e10cSrcweir             );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir             stringItemListChanged( aLock );
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     //---------------------------------------------------------------------
152*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::entryRangeRemoved( const ListEntryEvent& _rEvent ) throw (RuntimeException)
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         ControlModelLock aLock( m_rControlModel );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         OSL_ENSURE( _rEvent.Source == m_xListSource,
157*cdf0e10cSrcweir             "OEntryListHelper::entryRangeRemoved: where did this come from?" );
158*cdf0e10cSrcweir         OSL_ENSURE( ( _rEvent.Position > 0 ) && ( _rEvent.Count > 0 ) && ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() ),
159*cdf0e10cSrcweir             "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         if  (   ( _rEvent.Position > 0 )
162*cdf0e10cSrcweir             &&  ( _rEvent.Count > 0 )
163*cdf0e10cSrcweir             &&  ( _rEvent.Position + _rEvent.Count <= m_aStringItems.getLength() )
164*cdf0e10cSrcweir             )
165*cdf0e10cSrcweir         {
166*cdf0e10cSrcweir             // copy all items after the removed ones
167*cdf0e10cSrcweir             ::std::copy(
168*cdf0e10cSrcweir                 m_aStringItems.getConstArray() + _rEvent.Position + _rEvent.Count,
169*cdf0e10cSrcweir                 m_aStringItems.getConstArray() + m_aStringItems.getLength(),
170*cdf0e10cSrcweir                 m_aStringItems.getArray( ) + _rEvent.Position
171*cdf0e10cSrcweir             );
172*cdf0e10cSrcweir             // shrink the array
173*cdf0e10cSrcweir             m_aStringItems.realloc( m_aStringItems.getLength() - _rEvent.Count );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir             stringItemListChanged( aLock );
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     //---------------------------------------------------------------------
180*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::allEntriesChanged( const EventObject& _rEvent ) throw (RuntimeException)
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         ControlModelLock aLock( m_rControlModel );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         OSL_ENSURE( _rEvent.Source == m_xListSource,
185*cdf0e10cSrcweir             "OEntryListHelper::allEntriesChanged: where did this come from?" );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         Reference< XListEntrySource > xSource( _rEvent.Source, UNO_QUERY );
188*cdf0e10cSrcweir         if ( _rEvent.Source == m_xListSource )
189*cdf0e10cSrcweir         {
190*cdf0e10cSrcweir             impl_lock_refreshList( aLock );
191*cdf0e10cSrcweir         }
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     // XRefreshable
195*cdf0e10cSrcweir     //------------------------------------------------------------------------------
196*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::addRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         if ( _rxListener.is() )
199*cdf0e10cSrcweir             m_aRefreshListeners.addInterface( _rxListener );
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     //------------------------------------------------------------------------------
203*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::removeRefreshListener(const Reference<XRefreshListener>& _rxListener) throw(RuntimeException)
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         if ( _rxListener.is() )
206*cdf0e10cSrcweir             m_aRefreshListeners.removeInterface( _rxListener );
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     //------------------------------------------------------------------------------
210*cdf0e10cSrcweir     void SAL_CALL OEntryListHelper::refresh() throw(RuntimeException)
211*cdf0e10cSrcweir     {
212*cdf0e10cSrcweir         {
213*cdf0e10cSrcweir             ControlModelLock aLock( m_rControlModel );
214*cdf0e10cSrcweir             impl_lock_refreshList( aLock );
215*cdf0e10cSrcweir         }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         EventObject aEvt( static_cast< XRefreshable* >( this ) );
218*cdf0e10cSrcweir         m_aRefreshListeners.notifyEach( &XRefreshListener::refreshed, aEvt );
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     //---------------------------------------------------------------------
222*cdf0e10cSrcweir     void OEntryListHelper::impl_lock_refreshList( ControlModelLock& _rInstanceLock )
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir         if ( hasExternalListSource() )
225*cdf0e10cSrcweir         {
226*cdf0e10cSrcweir             m_aStringItems = m_xListSource->getAllListEntries( );
227*cdf0e10cSrcweir             stringItemListChanged( _rInstanceLock );
228*cdf0e10cSrcweir         }
229*cdf0e10cSrcweir         else
230*cdf0e10cSrcweir             refreshInternalEntryList();
231*cdf0e10cSrcweir     }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     //---------------------------------------------------------------------
234*cdf0e10cSrcweir     bool OEntryListHelper::handleDisposing( const EventObject& _rEvent )
235*cdf0e10cSrcweir     {
236*cdf0e10cSrcweir         if ( m_xListSource .is() && ( _rEvent.Source == m_xListSource ) )
237*cdf0e10cSrcweir         {
238*cdf0e10cSrcweir             disconnectExternalListSource( );
239*cdf0e10cSrcweir             return true;
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir         return false;
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     //---------------------------------------------------------------------
245*cdf0e10cSrcweir     void OEntryListHelper::disposing( )
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir         EventObject aEvt( static_cast< XRefreshable* >( this ) );
248*cdf0e10cSrcweir         m_aRefreshListeners.disposeAndClear(aEvt);
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir         if ( hasExternalListSource( ) )
251*cdf0e10cSrcweir             disconnectExternalListSource( );
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     //---------------------------------------------------------------------
255*cdf0e10cSrcweir     void OEntryListHelper::disconnectExternalListSource( )
256*cdf0e10cSrcweir     {
257*cdf0e10cSrcweir         if ( m_xListSource.is() )
258*cdf0e10cSrcweir             m_xListSource->removeListEntryListener( this );
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         m_xListSource.clear();
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         disconnectedExternalListSource();
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     //---------------------------------------------------------------------
266*cdf0e10cSrcweir     void OEntryListHelper::connectedExternalListSource( )
267*cdf0e10cSrcweir     {
268*cdf0e10cSrcweir         // nothing to do here
269*cdf0e10cSrcweir     }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     //---------------------------------------------------------------------
272*cdf0e10cSrcweir     void OEntryListHelper::disconnectedExternalListSource( )
273*cdf0e10cSrcweir     {
274*cdf0e10cSrcweir         // nothing to do here
275*cdf0e10cSrcweir     }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     //---------------------------------------------------------------------
278*cdf0e10cSrcweir     void OEntryListHelper::connectExternalListSource( const Reference< XListEntrySource >& _rxSource, ControlModelLock& _rInstanceLock )
279*cdf0e10cSrcweir     {
280*cdf0e10cSrcweir         OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
281*cdf0e10cSrcweir         OSL_ENSURE( _rxSource.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir         // remember it
284*cdf0e10cSrcweir         m_xListSource = _rxSource;
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir         // initially fill our item list
287*cdf0e10cSrcweir         if ( m_xListSource.is() )
288*cdf0e10cSrcweir         {
289*cdf0e10cSrcweir             // be notified when the list changes ...
290*cdf0e10cSrcweir             m_xListSource->addListEntryListener( this );
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir             m_aStringItems = m_xListSource->getAllListEntries( );
293*cdf0e10cSrcweir             stringItemListChanged( _rInstanceLock );
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir             // let derivees react on the new list source
296*cdf0e10cSrcweir             connectedExternalListSource();
297*cdf0e10cSrcweir         }
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     //---------------------------------------------------------------------
301*cdf0e10cSrcweir     sal_Bool OEntryListHelper::convertNewListSourceProperty( Any& _rConvertedValue,
302*cdf0e10cSrcweir         Any& _rOldValue, const Any& _rValue ) SAL_THROW( ( IllegalArgumentException ) )
303*cdf0e10cSrcweir     {
304*cdf0e10cSrcweir         if ( hasExternalListSource() )
305*cdf0e10cSrcweir             throw IllegalArgumentException( );
306*cdf0e10cSrcweir             // TODO: error message
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         return ::comphelper::tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_aStringItems );
309*cdf0e10cSrcweir     }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir     //---------------------------------------------------------------------
312*cdf0e10cSrcweir     void OEntryListHelper::setNewStringItemList( const ::com::sun::star::uno::Any& _rValue, ControlModelLock& _rInstanceLock )
313*cdf0e10cSrcweir     {
314*cdf0e10cSrcweir         OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
315*cdf0e10cSrcweir         OSL_VERIFY( _rValue >>= m_aStringItems );
316*cdf0e10cSrcweir         stringItemListChanged( _rInstanceLock );
317*cdf0e10cSrcweir     }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir //.........................................................................
320*cdf0e10cSrcweir }   // namespace frm
321*cdf0e10cSrcweir //.........................................................................
322