Bump for 3.6-28
[LibreOffice.git] / extensions / source / propctrlr / propertycomposer.cxx
blobd694c0abc3526c0dd99b0dbec630d708abc94b17
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "propertycomposer.hxx"
31 /** === begin UNO includes === **/
32 #include <com/sun/star/lang/NullPointerException.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 /** === end UNO includes === **/
35 #include <osl/diagnose.h>
36 #include <tools/diagnose_ex.h>
38 #include <functional>
39 #include <algorithm>
40 #include <map>
42 //........................................................................
43 namespace pcr
45 //........................................................................
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::inspection;
52 //====================================================================
53 //= helper
54 //====================================================================
55 namespace
57 //----------------------------------------------------------------
58 struct SetPropertyValue : public ::std::unary_function< Reference< XPropertyHandler >, void >
60 ::rtl::OUString sPropertyName;
61 const Any& rValue;
62 SetPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) : sPropertyName( _rPropertyName ), rValue( _rValue ) { }
63 void operator()( const Reference< XPropertyHandler >& _rHandler )
65 _rHandler->setPropertyValue( sPropertyName, rValue );
69 //----------------------------------------------------------------
70 template < class BagType >
71 void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
73 ::std::copy( _rArray.getConstArray(), _rArray.getConstArray() + _rArray.getLength(),
74 ::std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
77 //----------------------------------------------------------------
78 template < class BagType >
79 void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
81 _rArray.realloc( _rBag.size() );
82 ::std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
86 //====================================================================
87 //= PropertyComposer
88 //====================================================================
90 // TODO: there are various places where we determine the first handler in our array which
91 // supports a given property id. This is, at the moment, done with searching all handlers,
92 // which is O( n * k ) at worst (n being the number of handlers, k being the maximum number
93 // of supported properties per handler). Shouldn't we cache this? So that it is O( log k )?
95 //--------------------------------------------------------------------
96 PropertyComposer::PropertyComposer( const ::std::vector< Reference< XPropertyHandler > >& _rSlaveHandlers )
97 :PropertyComposer_Base ( m_aMutex )
98 ,m_aSlaveHandlers ( _rSlaveHandlers )
99 ,m_aPropertyListeners ( m_aMutex )
100 ,m_bSupportedPropertiesAreKnown ( false )
102 if ( m_aSlaveHandlers.empty() )
103 throw IllegalArgumentException();
105 osl_incrementInterlockedCount( &m_refCount );
107 Reference< XPropertyChangeListener > xMeMyselfAndI( this );
108 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
109 loop != m_aSlaveHandlers.end();
110 ++loop
113 if ( !loop->is() )
114 throw NullPointerException();
115 (*loop)->addPropertyChangeListener( xMeMyselfAndI );
118 osl_decrementInterlockedCount( &m_refCount );
121 //--------------------------------------------------------------------
122 void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException)
124 MethodGuard aGuard( *this );
126 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
127 loop != m_aSlaveHandlers.end();
128 ++loop
131 (*loop)->inspect( _rxIntrospectee );
135 //--------------------------------------------------------------------
136 Any SAL_CALL PropertyComposer::getPropertyValue( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
138 MethodGuard aGuard( *this );
139 return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
142 //--------------------------------------------------------------------
143 void SAL_CALL PropertyComposer::setPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException)
145 MethodGuard aGuard( *this );
146 ::std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
149 //--------------------------------------------------------------------
150 Any SAL_CALL PropertyComposer::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
152 MethodGuard aGuard( *this );
153 return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
156 //--------------------------------------------------------------------
157 Any SAL_CALL PropertyComposer::convertToControlValue( const ::rtl::OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException)
159 MethodGuard aGuard( *this );
160 return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
163 //--------------------------------------------------------------------
164 PropertyState SAL_CALL PropertyComposer::getPropertyState( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
166 MethodGuard aGuard( *this );
168 // assume DIRECT for the moment. This will stay this way if *all* slaves
169 // tell the property has DIRECT state, and if *all* values equal
170 PropertyState eState = PropertyState_DIRECT_VALUE;
172 // check the master state
173 Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
174 Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
175 eState = xPrimary->getPropertyState( _rPropertyName );
177 // loop through the secondary sets
178 PropertyState eSecondaryState = PropertyState_DIRECT_VALUE;
179 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
180 loop != m_aSlaveHandlers.end();
181 ++loop
184 // the secondary state
185 eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
187 // the secondary value
188 Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
190 if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
191 || ( aPrimaryValue != aSecondaryValue ) // unequal values
194 eState = PropertyState_AMBIGUOUS_VALUE;
195 break;
199 return eState;
202 //--------------------------------------------------------------------
203 void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
205 MethodGuard aGuard( *this );
206 m_aPropertyListeners.addListener( _rxListener );
209 //--------------------------------------------------------------------
210 void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
212 MethodGuard aGuard( *this );
213 m_aPropertyListeners.removeListener( _rxListener );
216 //--------------------------------------------------------------------
217 Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties() throw (RuntimeException)
219 MethodGuard aGuard( *this );
221 if ( !m_bSupportedPropertiesAreKnown )
223 // we support a property if and only if all of our slaves support it
225 // initially, use all the properties of an arbitrary handler (we take the first one)
226 putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
228 // now intersect with the properties of *all* other handlers
229 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
230 loop != m_aSlaveHandlers.end();
231 ++loop
234 // the properties supported by the current handler
235 PropertyBag aThisRound;
236 putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
238 // the intersection of those properties with all we already have
239 PropertyBag aIntersection;
240 ::std::set_intersection( aThisRound.begin(), aThisRound.end(), m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
241 ::std::insert_iterator< PropertyBag >( aIntersection, aIntersection.begin() ), PropertyLessByName() );
243 m_aSupportedProperties.swap( aIntersection );
244 if ( m_aSupportedProperties.empty() )
245 break;
248 // remove those properties which are not composable
249 for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
250 check != m_aSupportedProperties.end();
253 sal_Bool bIsComposable = isComposable( check->Name );
254 if ( !bIsComposable )
256 PropertyBag::iterator next = check; ++next;
257 m_aSupportedProperties.erase( check );
258 check = next;
260 else
261 ++check;
264 m_bSupportedPropertiesAreKnown = true;
267 Sequence< Property > aSurvived;
268 copyBagToArray( m_aSupportedProperties, aSurvived );
269 return aSurvived;
272 //--------------------------------------------------------------------
273 void uniteStringArrays( const PropertyComposer::HandlerArray& _rHandlers, Sequence< ::rtl::OUString > (SAL_CALL XPropertyHandler::*pGetter)( void ),
274 Sequence< ::rtl::OUString >& /* [out] */ _rUnion )
276 ::std::set< ::rtl::OUString > aUnitedBag;
278 Sequence< ::rtl::OUString > aThisRound;
279 for ( PropertyComposer::HandlerArray::const_iterator loop = _rHandlers.begin();
280 loop != _rHandlers.end();
281 ++loop
284 aThisRound = (loop->get()->*pGetter)();
285 putIntoBag( aThisRound, aUnitedBag );
288 copyBagToArray( aUnitedBag, _rUnion );
291 //--------------------------------------------------------------------
292 Sequence< ::rtl::OUString > SAL_CALL PropertyComposer::getSupersededProperties( ) throw (RuntimeException)
294 MethodGuard aGuard( *this );
296 // we supersede those properties which are superseded by at least one of our slaves
297 Sequence< ::rtl::OUString > aSuperseded;
298 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getSupersededProperties, aSuperseded );
299 return aSuperseded;
302 //--------------------------------------------------------------------
303 Sequence< ::rtl::OUString > SAL_CALL PropertyComposer::getActuatingProperties( ) throw (RuntimeException)
305 MethodGuard aGuard( *this );
307 // we're interested in those properties which at least one handler wants to have
308 Sequence< ::rtl::OUString > aActuating;
309 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getActuatingProperties, aActuating );
310 return aActuating;
313 //--------------------------------------------------------------------
314 LineDescriptor SAL_CALL PropertyComposer::describePropertyLine( const ::rtl::OUString& _rPropertyName,
315 const Reference< XPropertyControlFactory >& _rxControlFactory )
316 throw (UnknownPropertyException, NullPointerException, RuntimeException)
318 MethodGuard aGuard( *this );
319 return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
322 //--------------------------------------------------------------------
323 ::sal_Bool SAL_CALL PropertyComposer::isComposable( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
325 MethodGuard aGuard( *this );
326 return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
329 //--------------------------------------------------------------------
330 InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const ::rtl::OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI ) throw (UnknownPropertyException, NullPointerException, RuntimeException)
332 if ( !_rxInspectorUI.is() )
333 throw NullPointerException();
335 MethodGuard aGuard( *this );
337 impl_ensureUIRequestComposer( _rxInspectorUI );
338 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
340 // ask the first of the handlers
341 InteractiveSelectionResult eResult = (*m_aSlaveHandlers.begin())->onInteractivePropertySelection(
342 _rPropertyName,
343 _bPrimary,
344 _rData,
345 m_pUIRequestComposer->getUIForPropertyHandler( *m_aSlaveHandlers.begin() )
348 switch ( eResult )
350 case InteractiveSelectionResult_Cancelled:
351 // fine
352 break;
354 case InteractiveSelectionResult_Success:
355 case InteractiveSelectionResult_Pending:
356 OSL_FAIL( "PropertyComposer::onInteractivePropertySelection: no chance to forward the new value to the other handlers!" );
357 // This means that we cannot know the new property value, which either has already been set
358 // at the first component ("Success"), or will be set later on once the asynchronous input
359 // is finished ("Pending"). So, we also cannot forward this new property value to the other
360 // handlers.
361 // We would need to be a listener at the property at the first component, but even this wouldn't
362 // be sufficient, since the property handler is free to change *any* property during a dedicated
363 // property UI.
364 eResult = InteractiveSelectionResult_Cancelled;
365 break;
367 case InteractiveSelectionResult_ObtainedValue:
368 // OK. Our own caller will pass this as setPropertyValue, and we will then pass it to
369 // all slave handlers
370 break;
372 default:
373 OSL_FAIL( "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
374 break;
377 return eResult;
380 //--------------------------------------------------------------------
381 void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
383 OSL_ENSURE( !m_pUIRequestComposer.get() || m_pUIRequestComposer->getDelegatorUI().get() == _rxInspectorUI.get(),
384 "PropertyComposer::impl_ensureUIRequestComposer: somebody's changing the horse in the mid of the race!" );
386 if ( !m_pUIRequestComposer.get() )
387 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
390 //--------------------------------------------------------------------
391 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)
393 if ( !_rxInspectorUI.is() )
394 throw NullPointerException();
396 MethodGuard aGuard( *this );
398 impl_ensureUIRequestComposer( _rxInspectorUI );
399 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
401 // ask all handlers which expressed interest in this particular property, and "compose" their
402 // commands for the UIUpdater
403 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
404 loop != m_aSlaveHandlers.end();
405 ++loop
408 // TODO: make this cheaper (cache it?)
409 const StlSyntaxSequence< ::rtl::OUString > aThisHandlersActuatingProps = (*loop)->getActuatingProperties();
410 for ( StlSyntaxSequence< ::rtl::OUString >::const_iterator loopProps = aThisHandlersActuatingProps.begin();
411 loopProps != aThisHandlersActuatingProps.end();
412 ++loopProps
415 if ( *loopProps == _rActuatingPropertyName )
417 (*loop)->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
418 m_pUIRequestComposer->getUIForPropertyHandler( *loop ),
419 _bFirstTimeInit );
420 break;
426 //--------------------------------------------------------------------
427 IMPLEMENT_FORWARD_XCOMPONENT( PropertyComposer, PropertyComposer_Base )
429 //--------------------------------------------------------------------
430 void SAL_CALL PropertyComposer::disposing()
432 MethodGuard aGuard( *this );
434 // dispose our slave handlers
435 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
436 loop != m_aSlaveHandlers.end();
437 ++loop
440 (*loop)->removePropertyChangeListener( this );
441 (*loop)->dispose();
444 clearContainer( m_aSlaveHandlers );
446 if ( m_pUIRequestComposer.get() )
447 m_pUIRequestComposer->dispose();
448 m_pUIRequestComposer.reset( NULL );
451 //--------------------------------------------------------------------
452 void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException)
454 if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
455 // A slave handler might fire events for more properties than we support. Ignore those.
456 return;
458 PropertyChangeEvent aTranslatedEvent( evt );
461 aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
463 catch( const Exception& )
465 DBG_UNHANDLED_EXCEPTION();
467 m_aPropertyListeners.notify( aTranslatedEvent, &XPropertyChangeListener::propertyChange );
470 //--------------------------------------------------------------------
471 void SAL_CALL PropertyComposer::disposing( const EventObject& Source ) throw (RuntimeException)
473 MethodGuard aGuard( *this );
474 m_aPropertyListeners.disposing( Source );
477 //--------------------------------------------------------------------
478 sal_Bool SAL_CALL PropertyComposer::suspend( sal_Bool _bSuspend ) throw (RuntimeException)
480 MethodGuard aGuard( *this );
481 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
482 loop != m_aSlaveHandlers.end();
483 ++loop
486 if ( !(*loop)->suspend( _bSuspend ) )
488 if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
490 // if we tried to suspend, but one of the slave handlers vetoed,
491 // re-activate the handlers which actually did *not* veto
492 // the suspension
495 --loop;
496 (*loop)->suspend( sal_False );
498 while ( loop != m_aSlaveHandlers.begin() );
500 return false;
503 return true;
506 //--------------------------------------------------------------------
507 sal_Bool SAL_CALL PropertyComposer::hasPropertyByName( const ::rtl::OUString& _rName ) throw (RuntimeException)
509 return impl_isSupportedProperty_nothrow( _rName );
512 //........................................................................
513 } // namespace pcr
514 //........................................................................
516 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */