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 auto aTmpRange
= asNonConstRange(aTmp
);
146 sal_Int32 nStop
= _rEvent
.Position
;
148 for ( ; i
< nStop
; ++i
)
150 aTmpRange
[i
] = m_aTypedItems
[i
];
152 nStop
= aTmp
.getLength();
153 for (sal_Int32 j
= _rEvent
.Position
+ _rEvent
.Count
; i
< nStop
; ++i
, ++j
)
155 aTmpRange
[i
] = m_aTypedItems
[j
];
157 m_aTypedItems
= aTmp
;
159 else if (m_aTypedItems
.hasElements())
161 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
163 stringItemListChanged( aLock
);
167 void SAL_CALL
OEntryListHelper::allEntriesChanged( const EventObject
& _rEvent
)
169 ControlModelLock
aLock( m_rControlModel
);
171 OSL_ENSURE( _rEvent
.Source
== m_xListSource
,
172 "OEntryListHelper::allEntriesChanged: where did this come from?" );
174 if ( _rEvent
.Source
== m_xListSource
)
176 impl_lock_refreshList( aLock
);
182 void SAL_CALL
OEntryListHelper::addRefreshListener(const Reference
<XRefreshListener
>& _rxListener
)
184 if ( _rxListener
.is() )
185 m_aRefreshListeners
.addInterface( _rxListener
);
189 void SAL_CALL
OEntryListHelper::removeRefreshListener(const Reference
<XRefreshListener
>& _rxListener
)
191 if ( _rxListener
.is() )
192 m_aRefreshListeners
.removeInterface( _rxListener
);
196 void SAL_CALL
OEntryListHelper::refresh()
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() )
211 obtainListSourceEntries( _rInstanceLock
);
213 refreshInternalEntryList();
217 bool OEntryListHelper::handleDisposing( const EventObject
& _rEvent
)
219 if ( m_xListSource
.is() && ( _rEvent
.Source
== m_xListSource
) )
221 disconnectExternalListSource( );
228 void OEntryListHelper::disposing( )
230 EventObject
aEvt( static_cast< XRefreshable
* >( this ) );
231 m_aRefreshListeners
.disposeAndClear(aEvt
);
233 if ( hasExternalListSource( ) )
234 disconnectExternalListSource( );
238 void OEntryListHelper::disconnectExternalListSource( )
240 if ( m_xListSource
.is() )
241 m_xListSource
->removeListEntryListener( this );
243 m_xListSource
.clear();
247 void OEntryListHelper::connectExternalListSource( const Reference
< XListEntrySource
>& _rxSource
, ControlModelLock
& _rInstanceLock
)
249 OSL_ENSURE( !hasExternalListSource(), "OEntryListHelper::connectExternalListSource: only to be called if no external source is active!" );
250 OSL_ENSURE( _rxSource
.is(), "OEntryListHelper::connectExternalListSource: invalid list source!" );
253 m_xListSource
= _rxSource
;
255 // initially fill our item list
256 if ( m_xListSource
.is() )
258 // be notified when the list changes ...
259 m_xListSource
->addListEntryListener( this );
261 obtainListSourceEntries( _rInstanceLock
);
266 void OEntryListHelper::obtainListSourceEntries( ControlModelLock
& _rInstanceLock
)
268 Reference
< XListEntryTypedSource
> xTyped
;
269 xTyped
.set( m_xListSource
, UNO_QUERY
);
272 comphelper::sequenceToContainer( m_aStringItems
, xTyped
->getAllListEntriesTyped( m_aTypedItems
));
276 comphelper::sequenceToContainer( m_aStringItems
, m_xListSource
->getAllListEntries());
277 if (m_aTypedItems
.hasElements())
278 m_aTypedItems
= Sequence
<Any
>();
280 stringItemListChanged( _rInstanceLock
);
284 bool OEntryListHelper::convertNewListSourceProperty( Any
& _rConvertedValue
,
285 Any
& _rOldValue
, const Any
& _rValue
)
287 if ( hasExternalListSource() )
288 throw IllegalArgumentException( );
289 // TODO: error message
291 return ::comphelper::tryPropertyValue( _rConvertedValue
, _rOldValue
, _rValue
, comphelper::containerToSequence(m_aStringItems
) );
295 void OEntryListHelper::setNewStringItemList( const css::uno::Any
& _rValue
, ControlModelLock
& _rInstanceLock
)
297 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewStringItemList: this should never have survived convertNewListSourceProperty!" );
298 css::uno::Sequence
<OUString
> aTmp
;
299 OSL_VERIFY( _rValue
>>= aTmp
);
300 comphelper::sequenceToContainer(m_aStringItems
, aTmp
);
301 if (m_aTypedItems
.hasElements())
302 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
303 stringItemListChanged( _rInstanceLock
);
307 void OEntryListHelper::setNewTypedItemList( const css::uno::Any
& _rValue
, ControlModelLock
& _rInstanceLock
)
309 OSL_PRECOND( !hasExternalListSource(), "OEntryListHelper::setNewTypedItemList: this should never have survived convertNewListSourceProperty!" );
310 if (!(_rValue
>>= m_aTypedItems
))
313 if (m_aTypedItems
.hasElements())
314 m_aTypedItems
= Sequence
<Any
>(); // doesn't match anymore
316 // Sets both properties, assuming that TypedItemList belongs to StringItemList.
317 stringItemListChanged( _rInstanceLock
);
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */