Update ooo320-m1
[ooovba.git] / extensions / source / propctrlr / propertycomposer.cxx
blob0c04cd6b52c41bcb62d556d46b97bfc18a7e6eaa
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertycomposer.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "propertycomposer.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/lang/NullPointerException.hpp>
37 #include <com/sun/star/lang/IllegalArgumentException.hpp>
38 /** === end UNO includes === **/
39 #include <osl/diagnose.h>
40 #include <tools/diagnose_ex.h>
42 #include <functional>
43 #include <algorithm>
44 #include <map>
46 //........................................................................
47 namespace pcr
49 //........................................................................
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::inspection;
56 //====================================================================
57 //= helper
58 //====================================================================
59 namespace
61 //----------------------------------------------------------------
62 struct SetPropertyValue : public ::std::unary_function< Reference< XPropertyHandler >, void >
64 ::rtl::OUString sPropertyName;
65 const Any& rValue;
66 SetPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) : sPropertyName( _rPropertyName ), rValue( _rValue ) { }
67 void operator()( const Reference< XPropertyHandler >& _rHandler )
69 _rHandler->setPropertyValue( sPropertyName, rValue );
73 //----------------------------------------------------------------
74 template < class BagType >
75 void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
77 ::std::copy( _rArray.getConstArray(), _rArray.getConstArray() + _rArray.getLength(),
78 ::std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
81 //----------------------------------------------------------------
82 template < class BagType >
83 void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
85 _rArray.realloc( _rBag.size() );
86 ::std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
90 //====================================================================
91 //= PropertyComposer
92 //====================================================================
94 // TODO: there are various places where we determine the first handler in our array which
95 // supports a given property id. This is, at the moment, done with searching all handlers,
96 // which is O( n * k ) at worst (n being the number of handlers, k being the maximum number
97 // of supported properties per handler). Shouldn't we cache this? So that it is O( log k )?
99 //--------------------------------------------------------------------
100 PropertyComposer::PropertyComposer( const ::std::vector< Reference< XPropertyHandler > >& _rSlaveHandlers )
101 :PropertyComposer_Base ( m_aMutex )
102 ,m_aSlaveHandlers ( _rSlaveHandlers )
103 ,m_aPropertyListeners ( m_aMutex )
104 ,m_bSupportedPropertiesAreKnown ( false )
106 if ( m_aSlaveHandlers.empty() )
107 throw IllegalArgumentException();
109 osl_incrementInterlockedCount( &m_refCount );
111 Reference< XPropertyChangeListener > xMeMyselfAndI( this );
112 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
113 loop != m_aSlaveHandlers.end();
114 ++loop
117 if ( !loop->is() )
118 throw NullPointerException();
119 (*loop)->addPropertyChangeListener( xMeMyselfAndI );
122 osl_decrementInterlockedCount( &m_refCount );
125 //--------------------------------------------------------------------
126 void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException)
128 MethodGuard aGuard( *this );
130 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
131 loop != m_aSlaveHandlers.end();
132 ++loop
135 (*loop)->inspect( _rxIntrospectee );
139 //--------------------------------------------------------------------
140 Any SAL_CALL PropertyComposer::getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
142 MethodGuard aGuard( *this );
143 return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
146 //--------------------------------------------------------------------
147 void SAL_CALL PropertyComposer::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException)
149 MethodGuard aGuard( *this );
150 ::std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
153 //--------------------------------------------------------------------
154 Any SAL_CALL PropertyComposer::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
156 MethodGuard aGuard( *this );
157 return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
160 //--------------------------------------------------------------------
161 Any SAL_CALL PropertyComposer::convertToControlValue( const ::rtl::OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException)
163 MethodGuard aGuard( *this );
164 return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
167 //--------------------------------------------------------------------
168 PropertyState SAL_CALL PropertyComposer::getPropertyState( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
170 MethodGuard aGuard( *this );
172 // assume DIRECT for the moment. This will stay this way if *all* slaves
173 // tell the property has DIRECT state, and if *all* values equal
174 PropertyState eState = PropertyState_DIRECT_VALUE;
176 // check the master state
177 Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
178 Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
179 eState = xPrimary->getPropertyState( _rPropertyName );
181 // loop through the secondary sets
182 PropertyState eSecondaryState = PropertyState_DIRECT_VALUE;
183 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
184 loop != m_aSlaveHandlers.end();
185 ++loop
188 // the secondary state
189 eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
191 // the secondary value
192 Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
194 if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
195 || ( aPrimaryValue != aSecondaryValue ) // unequal values
198 eState = PropertyState_AMBIGUOUS_VALUE;
199 break;
203 return eState;
206 //--------------------------------------------------------------------
207 void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
209 MethodGuard aGuard( *this );
210 m_aPropertyListeners.addListener( _rxListener );
213 //--------------------------------------------------------------------
214 void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
216 MethodGuard aGuard( *this );
217 m_aPropertyListeners.removeListener( _rxListener );
220 //--------------------------------------------------------------------
221 Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties() throw (RuntimeException)
223 MethodGuard aGuard( *this );
225 if ( !m_bSupportedPropertiesAreKnown )
227 // we support a property if and only if all of our slaves support it
229 // initially, use all the properties of an arbitrary handler (we take the first one)
230 putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
232 // now intersect with the properties of *all* other handlers
233 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
234 loop != m_aSlaveHandlers.end();
235 ++loop
238 // the properties supported by the current handler
239 PropertyBag aThisRound;
240 putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
242 // the intersection of those properties with all we already have
243 PropertyBag aIntersection;
244 ::std::set_intersection( aThisRound.begin(), aThisRound.end(), m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
245 ::std::insert_iterator< PropertyBag >( aIntersection, aIntersection.begin() ), PropertyLessByName() );
247 m_aSupportedProperties.swap( aIntersection );
248 if ( m_aSupportedProperties.empty() )
249 break;
252 // remove those properties which are not composable
253 for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
254 check != m_aSupportedProperties.end();
257 sal_Bool bIsComposable = isComposable( check->Name );
258 if ( !bIsComposable )
260 PropertyBag::iterator next = check; ++next;
261 m_aSupportedProperties.erase( check );
262 check = next;
264 else
265 ++check;
268 m_bSupportedPropertiesAreKnown = true;
271 Sequence< Property > aSurvived;
272 copyBagToArray( m_aSupportedProperties, aSurvived );
273 return aSurvived;
276 //--------------------------------------------------------------------
277 void uniteStringArrays( const PropertyComposer::HandlerArray& _rHandlers, Sequence< ::rtl::OUString > (SAL_CALL XPropertyHandler::*pGetter)( void ),
278 Sequence< ::rtl::OUString >& /* [out] */ _rUnion )
280 ::std::set< ::rtl::OUString > aUnitedBag;
282 Sequence< ::rtl::OUString > aThisRound;
283 for ( PropertyComposer::HandlerArray::const_iterator loop = _rHandlers.begin();
284 loop != _rHandlers.end();
285 ++loop
288 aThisRound = (loop->get()->*pGetter)();
289 putIntoBag( aThisRound, aUnitedBag );
292 copyBagToArray( aUnitedBag, _rUnion );
295 //--------------------------------------------------------------------
296 Sequence< ::rtl::OUString > SAL_CALL PropertyComposer::getSupersededProperties( ) throw (RuntimeException)
298 MethodGuard aGuard( *this );
300 // we supersede those properties which are superseded by at least one of our slaves
301 Sequence< ::rtl::OUString > aSuperseded;
302 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getSupersededProperties, aSuperseded );
303 return aSuperseded;
306 //--------------------------------------------------------------------
307 Sequence< ::rtl::OUString > SAL_CALL PropertyComposer::getActuatingProperties( ) throw (RuntimeException)
309 MethodGuard aGuard( *this );
311 // we're interested in those properties which at least one handler wants to have
312 Sequence< ::rtl::OUString > aActuating;
313 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getActuatingProperties, aActuating );
314 return aActuating;
317 //--------------------------------------------------------------------
318 LineDescriptor SAL_CALL PropertyComposer::describePropertyLine( const ::rtl::OUString& _rPropertyName,
319 const Reference< XPropertyControlFactory >& _rxControlFactory )
320 throw (UnknownPropertyException, NullPointerException, RuntimeException)
322 MethodGuard aGuard( *this );
323 return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
326 //--------------------------------------------------------------------
327 ::sal_Bool SAL_CALL PropertyComposer::isComposable( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
329 MethodGuard aGuard( *this );
330 return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
333 //--------------------------------------------------------------------
334 InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const ::rtl::OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI ) throw (UnknownPropertyException, NullPointerException, RuntimeException)
336 if ( !_rxInspectorUI.is() )
337 throw NullPointerException();
339 MethodGuard aGuard( *this );
341 impl_ensureUIRequestComposer( _rxInspectorUI );
342 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
344 // ask the first of the handlers
345 InteractiveSelectionResult eResult = (*m_aSlaveHandlers.begin())->onInteractivePropertySelection(
346 _rPropertyName,
347 _bPrimary,
348 _rData,
349 m_pUIRequestComposer->getUIForPropertyHandler( *m_aSlaveHandlers.begin() )
352 switch ( eResult )
354 case InteractiveSelectionResult_Cancelled:
355 // fine
356 break;
358 case InteractiveSelectionResult_Success:
359 case InteractiveSelectionResult_Pending:
360 OSL_ENSURE( false, "PropertyComposer::onInteractivePropertySelection: no chance to forward the new value to the other handlers!" );
361 // This means that we cannot know the new property value, which either has already been set
362 // at the first component ("Success"), or will be set later on once the asynchronous input
363 // is finished ("Pending"). So, we also cannot forward this new property value to the other
364 // handlers.
365 // We would need to be a listener at the property at the first component, but even this wouldn't
366 // be sufficient, since the property handler is free to change *any* property during a dedicated
367 // property UI.
368 eResult = InteractiveSelectionResult_Cancelled;
369 break;
371 case InteractiveSelectionResult_ObtainedValue:
372 // OK. Our own caller will pass this as setPropertyValue, and we will then pass it to
373 // all slave handlers
374 break;
376 default:
377 OSL_ENSURE( false, "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
378 break;
381 return eResult;
384 //--------------------------------------------------------------------
385 void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
387 OSL_ENSURE( !m_pUIRequestComposer.get() || m_pUIRequestComposer->getDelegatorUI().get() == _rxInspectorUI.get(),
388 "PropertyComposer::impl_ensureUIRequestComposer: somebody's changing the horse in the mid of the race!" );
390 if ( !m_pUIRequestComposer.get() )
391 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
394 //--------------------------------------------------------------------
395 void SAL_CALL PropertyComposer::actuatingPropertyChanged( const ::rtl::OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& _rOldValue, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit ) throw (NullPointerException, RuntimeException)
397 if ( !_rxInspectorUI.is() )
398 throw NullPointerException();
400 MethodGuard aGuard( *this );
402 impl_ensureUIRequestComposer( _rxInspectorUI );
403 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
405 // ask all handlers which expressed interest in this particular property, and "compose" their
406 // commands for the UIUpdater
407 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
408 loop != m_aSlaveHandlers.end();
409 ++loop
412 // TODO: make this cheaper (cache it?)
413 const StlSyntaxSequence< ::rtl::OUString > aThisHandlersActuatingProps = (*loop)->getActuatingProperties();
414 for ( StlSyntaxSequence< ::rtl::OUString >::const_iterator loopProps = aThisHandlersActuatingProps.begin();
415 loopProps != aThisHandlersActuatingProps.end();
416 ++loopProps
419 if ( *loopProps == _rActuatingPropertyName )
421 (*loop)->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
422 m_pUIRequestComposer->getUIForPropertyHandler( *loop ),
423 _bFirstTimeInit );
424 break;
430 //--------------------------------------------------------------------
431 IMPLEMENT_FORWARD_XCOMPONENT( PropertyComposer, PropertyComposer_Base )
433 //--------------------------------------------------------------------
434 void SAL_CALL PropertyComposer::disposing()
436 MethodGuard aGuard( *this );
438 // dispose our slave handlers
439 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
440 loop != m_aSlaveHandlers.end();
441 ++loop
444 (*loop)->removePropertyChangeListener( this );
445 (*loop)->dispose();
448 clearContainer( m_aSlaveHandlers );
450 if ( m_pUIRequestComposer.get() )
451 m_pUIRequestComposer->dispose();
452 m_pUIRequestComposer.reset( NULL );
455 //--------------------------------------------------------------------
456 void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException)
458 if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
459 // A slave handler might fire events for more properties than we support. Ignore those.
460 return;
462 PropertyChangeEvent aTranslatedEvent( evt );
465 aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
467 catch( const Exception& )
469 DBG_UNHANDLED_EXCEPTION();
471 m_aPropertyListeners.notify( aTranslatedEvent, &XPropertyChangeListener::propertyChange );
474 //--------------------------------------------------------------------
475 void SAL_CALL PropertyComposer::disposing( const EventObject& Source ) throw (RuntimeException)
477 MethodGuard aGuard( *this );
478 m_aPropertyListeners.disposing( Source );
481 //--------------------------------------------------------------------
482 sal_Bool SAL_CALL PropertyComposer::suspend( sal_Bool _bSuspend ) throw (RuntimeException)
484 MethodGuard aGuard( *this );
485 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
486 loop != m_aSlaveHandlers.end();
487 ++loop
490 if ( !(*loop)->suspend( _bSuspend ) )
492 if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
494 // if we tried to suspend, but one of the slave handlers vetoed,
495 // re-activate the handlers which actually did *not* veto
496 // the suspension
499 --loop;
500 (*loop)->suspend( sal_False );
502 while ( loop != m_aSlaveHandlers.begin() );
504 return false;
507 return true;
510 //--------------------------------------------------------------------
511 sal_Bool SAL_CALL PropertyComposer::hasPropertyByName( const ::rtl::OUString& _rName ) throw (RuntimeException)
513 return impl_isSupportedProperty_nothrow( _rName );
516 //........................................................................
517 } // namespace pcr
518 //........................................................................