1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/form/binding/XListEntryTypedSource.hpp>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::lang
;
36 using namespace ::com::sun::star::util
;
37 using namespace ::com::sun::star::form::binding
;
39 OEntryListHelper::OEntryListHelper( OControlModel
& _rControlModel
)
40 :m_rControlModel( _rControlModel
)
41 ,m_aRefreshListeners( _rControlModel
.getInstanceMutex() )
46 OEntryListHelper::OEntryListHelper( const OEntryListHelper
& _rSource
, OControlModel
& _rControlModel
)
47 :m_rControlModel( _rControlModel
)
48 ,m_xListSource ( _rSource
.m_xListSource
)
49 ,m_aStringItems( _rSource
.m_aStringItems
)
50 ,m_aRefreshListeners( _rControlModel
.getInstanceMutex() )
55 OEntryListHelper::~OEntryListHelper( )
60 void SAL_CALL
OEntryListHelper::setListEntrySource( const Reference
< XListEntrySource
>& _rxSource
)
62 ControlModelLock
aLock( m_rControlModel
);
64 // disconnect from the current external list source
65 disconnectExternalListSource();
67 // and connect to the new one
69 connectExternalListSource( _rxSource
, aLock
);
73 Reference
< XListEntrySource
> SAL_CALL
OEntryListHelper::getListEntrySource( )
79 void SAL_CALL
OEntryListHelper::entryChanged( const ListEntryEvent
& _rEvent
)
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
< static_cast<sal_Int32
>(m_aStringItems
.size()) ),
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
< static_cast<sal_Int32
>(m_aStringItems
.size()) )
92 && _rEvent
.Entries
.hasElements()
95 m_aStringItems
[ _rEvent
.Position
] = _rEvent
.Entries
[ 0 ];
96 if (m_aTypedItems
.hasElements())
97 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
98 stringItemListChanged( aLock
);
103 void SAL_CALL
OEntryListHelper::entryRangeInserted( const ListEntryEvent
& _rEvent
)
105 ControlModelLock
aLock( m_rControlModel
);
107 OSL_ENSURE( _rEvent
.Source
== m_xListSource
,
108 "OEntryListHelper::entryRangeInserted: where did this come from?" );
109 OSL_ENSURE( ( _rEvent
.Position
> 0 ) && ( _rEvent
.Position
< static_cast<sal_Int32
>(m_aStringItems
.size()) ) && _rEvent
.Entries
.hasElements(),
110 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
112 if ( ( _rEvent
.Position
> 0 )
113 && ( _rEvent
.Position
< static_cast<sal_Int32
>(m_aStringItems
.size()) )
114 && _rEvent
.Entries
.hasElements()
117 m_aStringItems
.insert(m_aStringItems
.begin() + _rEvent
.Position
, _rEvent
.Entries
.begin(), _rEvent
.Entries
.end());
118 if (m_aTypedItems
.hasElements())
119 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
120 stringItemListChanged( aLock
);
125 void SAL_CALL
OEntryListHelper::entryRangeRemoved( const ListEntryEvent
& _rEvent
)
127 ControlModelLock
aLock( m_rControlModel
);
129 OSL_ENSURE( _rEvent
.Source
== m_xListSource
,
130 "OEntryListHelper::entryRangeRemoved: where did this come from?" );
131 OSL_ENSURE( ( _rEvent
.Position
> 0 ) && ( _rEvent
.Count
> 0 ) && ( _rEvent
.Position
+ _rEvent
.Count
<= static_cast<sal_Int32
>(m_aStringItems
.size()) ),
132 "OEntryListHelper::entryRangeRemoved: invalid count and/or position!" );
134 if ( !(( _rEvent
.Position
> 0 )
135 && ( _rEvent
.Count
> 0 )
136 && ( _rEvent
.Position
+ _rEvent
.Count
<= static_cast<sal_Int32
>(m_aStringItems
.size()) ))
140 m_aStringItems
.erase(m_aStringItems
.begin() + _rEvent
.Position
,
141 m_aStringItems
.begin() + _rEvent
.Position
+ _rEvent
.Count
);
142 if (_rEvent
.Position
+ _rEvent
.Count
<= m_aTypedItems
.getLength())
144 Sequence
<Any
> aTmp( m_aTypedItems
.getLength() - _rEvent
.Count
);
145 sal_Int32 nStop
= _rEvent
.Position
;
147 for ( ; i
< nStop
; ++i
)
149 aTmp
[i
] = m_aTypedItems
[i
];
151 nStop
= aTmp
.getLength();
152 for (sal_Int32 j
= _rEvent
.Position
+ _rEvent
.Count
; i
< nStop
; ++i
, ++j
)
154 aTmp
[i
] = m_aTypedItems
[j
];
156 m_aTypedItems
= aTmp
;
158 else if (m_aTypedItems
.hasElements())
160 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
162 stringItemListChanged( aLock
);
166 void SAL_CALL
OEntryListHelper::allEntriesChanged( const EventObject
& _rEvent
)
168 ControlModelLock
aLock( m_rControlModel
);
170 OSL_ENSURE( _rEvent
.Source
== m_xListSource
,
171 "OEntryListHelper::allEntriesChanged: where did this come from?" );
173 if ( _rEvent
.Source
== m_xListSource
)
175 impl_lock_refreshList( aLock
);
181 void SAL_CALL
OEntryListHelper::addRefreshListener(const Reference
<XRefreshListener
>& _rxListener
)
183 if ( _rxListener
.is() )
184 m_aRefreshListeners
.addInterface( _rxListener
);
188 void SAL_CALL
OEntryListHelper::removeRefreshListener(const Reference
<XRefreshListener
>& _rxListener
)
190 if ( _rxListener
.is() )
191 m_aRefreshListeners
.removeInterface( _rxListener
);
195 void SAL_CALL
OEntryListHelper::refresh()
198 ControlModelLock
aLock( m_rControlModel
);
199 impl_lock_refreshList( aLock
);
202 EventObject
aEvt( static_cast< XRefreshable
* >( this ) );
203 m_aRefreshListeners
.notifyEach( &XRefreshListener::refreshed
, aEvt
);
207 void OEntryListHelper::impl_lock_refreshList( ControlModelLock
& _rInstanceLock
)
209 if ( hasExternalListSource() )
210 obtainListSourceEntries( _rInstanceLock
);
212 refreshInternalEntryList();
216 bool OEntryListHelper::handleDisposing( const EventObject
& _rEvent
)
218 if ( m_xListSource
.is() && ( _rEvent
.Source
== m_xListSource
) )
220 disconnectExternalListSource( );
227 void OEntryListHelper::disposing( )
229 EventObject
aEvt( static_cast< XRefreshable
* >( this ) );
230 m_aRefreshListeners
.disposeAndClear(aEvt
);
232 if ( hasExternalListSource( ) )
233 disconnectExternalListSource( );
237 void OEntryListHelper::disconnectExternalListSource( )
239 if ( m_xListSource
.is() )
240 m_xListSource
->removeListEntryListener( this );
242 m_xListSource
.clear();
246 void OEntryListHelper::connectExternalListSource( const Reference
< XListEntrySource
>& _rxSource
, ControlModelLock
& _rInstanceLock
)
248 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
249 OSL_ENSURE( _rxSource
.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
252 m_xListSource
= _rxSource
;
254 // initially fill our item list
255 if ( m_xListSource
.is() )
257 // be notified when the list changes ...
258 m_xListSource
->addListEntryListener( this );
260 obtainListSourceEntries( _rInstanceLock
);
265 void OEntryListHelper::obtainListSourceEntries( ControlModelLock
& _rInstanceLock
)
267 Reference
< XListEntryTypedSource
> xTyped
;
268 xTyped
.set( m_xListSource
, UNO_QUERY
);
271 comphelper::sequenceToContainer( m_aStringItems
, xTyped
->getAllListEntriesTyped( m_aTypedItems
));
275 comphelper::sequenceToContainer( m_aStringItems
, m_xListSource
->getAllListEntries());
276 if (m_aTypedItems
.hasElements())
277 m_aTypedItems
= Sequence
<Any
>();
279 stringItemListChanged( _rInstanceLock
);
283 bool OEntryListHelper::convertNewListSourceProperty( Any
& _rConvertedValue
,
284 Any
& _rOldValue
, const Any
& _rValue
)
286 if ( hasExternalListSource() )
287 throw IllegalArgumentException( );
288 // TODO: error message
290 return ::comphelper::tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, comphelper::containerToSequence(m_aStringItems
) );
294 void OEntryListHelper::setNewStringItemList( const css::uno::Any
& _rValue
, ControlModelLock
& _rInstanceLock
)
296 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
297 css::uno::Sequence
<OUString
> aTmp
;
298 OSL_VERIFY( _rValue
>>= aTmp
);
299 comphelper::sequenceToContainer(m_aStringItems
, aTmp
);
300 if (m_aTypedItems
.hasElements())
301 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
302 stringItemListChanged( _rInstanceLock
);
306 void OEntryListHelper::setNewTypedItemList( const css::uno::Any
& _rValue
, ControlModelLock
& _rInstanceLock
)
308 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewTypedItemList: this should never have survived convertNewListSourceProperty!" );
309 if (!(_rValue
>>= m_aTypedItems
))
312 if (m_aTypedItems
.hasElements())
313 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
315 // Sets both properties, assuming that TypedItemList belongs to StringItemList.
316 stringItemListChanged( _rInstanceLock
);
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */