Bump for 3.6-28
[LibreOffice.git] / extensions / source / propctrlr / propcontroller.cxx
blob36bc66627533d990ca6e232c726a697a82bdeda2
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 "propcontroller.hxx"
30 #include "pcrstrings.hxx"
31 #include "standardcontrol.hxx"
32 #include "linedescriptor.hxx"
33 #include "propresid.hrc"
34 #include "formresid.hrc"
35 #include "propertyeditor.hxx"
36 #include "modulepcr.hxx"
37 #include "formstrings.hxx"
38 #include "formmetadata.hxx"
39 #include "formbrowsertools.hxx"
40 #include "propertycomposer.hxx"
42 /** === begin UNO includes === **/
43 #include <com/sun/star/awt/XWindow.hpp>
44 #include <com/sun/star/util/XCloseable.hpp>
45 #include <com/sun/star/inspection/PropertyControlType.hpp>
46 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
47 /** === end UNO includes === **/
48 #include <tools/debug.hxx>
49 #include <tools/diagnose_ex.h>
50 #include <comphelper/types.hxx>
51 #include <comphelper/extract.hxx>
52 #include <toolkit/awt/vclxwindow.hxx>
53 #include <toolkit/unohlp.hxx>
54 #include <comphelper/property.hxx>
55 #include <vcl/msgbox.hxx>
56 #include <vcl/svapp.hxx>
57 #include <osl/mutex.hxx>
58 #include <cppuhelper/component_context.hxx>
59 #include <cppuhelper/exc_hlp.hxx>
61 #include <algorithm>
62 #include <functional>
63 #include <sal/macros.h>
65 //------------------------------------------------------------------------
66 // !!! outside the namespace !!!
67 extern "C" void SAL_CALL createRegistryInfo_OPropertyBrowserController()
69 ::pcr::OAutoRegistration< ::pcr::OPropertyBrowserController > aAutoRegistration;
72 //............................................................................
73 namespace pcr
75 //............................................................................
77 using namespace ::com::sun::star;
78 using namespace ::com::sun::star::uno;
79 using namespace ::com::sun::star::awt;
80 using namespace ::com::sun::star::form;
81 using namespace ::com::sun::star::beans;
82 using namespace ::com::sun::star::script;
83 using namespace ::com::sun::star::lang;
84 using namespace ::com::sun::star::container;
85 using namespace ::com::sun::star::frame;
86 using namespace ::com::sun::star::util;
87 using namespace ::com::sun::star::inspection;
88 using namespace ::com::sun::star::ucb;
89 using namespace ::comphelper;
91 #define THISREF() static_cast< XController* >(this)
93 //========================================================================
94 //= OPropertyBrowserController
95 //========================================================================
96 DBG_NAME(OPropertyBrowserController)
97 //------------------------------------------------------------------------
98 OPropertyBrowserController::OPropertyBrowserController( const Reference< XComponentContext >& _rxContext )
99 :m_aContext(_rxContext)
100 ,m_aDisposeListeners( m_aMutex )
101 ,m_aControlObservers( m_aMutex )
102 ,m_pView(NULL)
103 ,m_bContainerFocusListening( false )
104 ,m_bSuspendingPropertyHandlers( false )
105 ,m_bConstructed( false )
106 ,m_bBindingIntrospectee( false )
108 DBG_CTOR(OPropertyBrowserController,NULL);
111 //------------------------------------------------------------------------
112 OPropertyBrowserController::~OPropertyBrowserController()
114 // stop listening for property changes
115 acquire();
116 stopInspection( true );
117 DBG_DTOR(OPropertyBrowserController,NULL);
120 //------------------------------------------------------------------------
121 IMPLEMENT_FORWARD_REFCOUNT( OPropertyBrowserController, OPropertyBrowserController_Base )
123 //------------------------------------------------------------------------
124 Any SAL_CALL OPropertyBrowserController::queryInterface( const Type& _rType ) throw (RuntimeException)
126 Any aReturn = OPropertyBrowserController_Base::queryInterface( _rType );
127 if ( !aReturn.hasValue() )
128 aReturn = ::cppu::queryInterface(
129 _rType,
130 static_cast< XObjectInspectorUI* >( this )
132 return aReturn;
135 //------------------------------------------------------------------------
136 void OPropertyBrowserController::startContainerWindowListening()
138 if (m_bContainerFocusListening)
139 return;
141 if (m_xFrame.is())
143 Reference< XWindow > xContainerWindow = m_xFrame->getContainerWindow();
144 if (xContainerWindow.is())
146 xContainerWindow->addFocusListener(this);
147 m_bContainerFocusListening = sal_True;
151 DBG_ASSERT(m_bContainerFocusListening, "OPropertyBrowserController::startContainerWindowListening: unable to start listening (inconsistence)!");
154 //------------------------------------------------------------------------
155 void OPropertyBrowserController::stopContainerWindowListening()
157 if (!m_bContainerFocusListening)
158 return;
160 if (m_xFrame.is())
162 Reference< XWindow > xContainerWindow = m_xFrame->getContainerWindow();
163 if (xContainerWindow.is())
165 xContainerWindow->removeFocusListener(this);
166 m_bContainerFocusListening = sal_False;
170 DBG_ASSERT(!m_bContainerFocusListening, "OPropertyBrowserController::stopContainerWindowListening: unable to stop listening (inconsistence)!");
173 //--------------------------------------------------------------------
174 Reference< XObjectInspectorModel > SAL_CALL OPropertyBrowserController::getInspectorModel() throw (RuntimeException)
176 return m_xModel;
179 //--------------------------------------------------------------------
180 void OPropertyBrowserController::impl_initializeView_nothrow()
182 OSL_PRECOND( haveView(), "OPropertyBrowserController::impl_initializeView_nothrow: not to be called when we have no view!" );
183 if ( !haveView() )
184 return;
186 if ( !m_xModel.is() )
187 // allowed
188 return;
192 getPropertyBox().EnableHelpSection( m_xModel->getHasHelpSection() );
193 getPropertyBox().SetHelpLineLimites( m_xModel->getMinHelpTextLines(), m_xModel->getMaxHelpTextLines() );
195 catch( const Exception& )
197 DBG_UNHANDLED_EXCEPTION();
201 //--------------------------------------------------------------------
202 void OPropertyBrowserController::impl_updateReadOnlyView_nothrow()
204 // this is a huge cudgel, admitted.
205 // The problem is that in case we were previously read-only, all our controls
206 // were created read-only, too. We cannot simply switch them to not-read-only.
207 // Even if they had an API for this, we do not know whether they were
208 // originally created read-only, or if they are read-only just because
209 // the model was.
210 impl_rebindToInspectee_nothrow( m_aInspectedObjects );
213 //--------------------------------------------------------------------
214 bool OPropertyBrowserController::impl_isReadOnlyModel_throw() const
216 if ( !m_xModel.is() )
217 return false;
219 return m_xModel->getIsReadOnly();
222 //--------------------------------------------------------------------
223 void OPropertyBrowserController::impl_startOrStopModelListening_nothrow( bool _bDoListen ) const
227 Reference< XPropertySet > xModelProperties( m_xModel, UNO_QUERY );
228 if ( !xModelProperties.is() )
229 // okay, so the model doesn't want to change its properties
230 // dynamically - fine with us
231 return;
233 void (SAL_CALL XPropertySet::*pListenerOperation)( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& )
234 = _bDoListen ? &XPropertySet::addPropertyChangeListener : &XPropertySet::removePropertyChangeListener;
236 (xModelProperties.get()->*pListenerOperation)(
237 ::rtl::OUString( "IsReadOnly" ),
238 const_cast< OPropertyBrowserController* >( this )
241 catch( const Exception& )
243 DBG_UNHANDLED_EXCEPTION();
247 //--------------------------------------------------------------------
248 void OPropertyBrowserController::impl_bindToNewModel_nothrow( const Reference< XObjectInspectorModel >& _rxInspectorModel )
250 impl_startOrStopModelListening_nothrow( false );
251 m_xModel = _rxInspectorModel;
252 impl_startOrStopModelListening_nothrow( true );
254 // initialize the view, if we already have one
255 if ( haveView() )
256 impl_initializeView_nothrow();
258 // inspect again, if we already have inspectees
259 if ( !m_aInspectedObjects.empty() )
260 impl_rebindToInspectee_nothrow( m_aInspectedObjects );
263 //--------------------------------------------------------------------
264 void SAL_CALL OPropertyBrowserController::setInspectorModel( const Reference< XObjectInspectorModel >& _inspectorModel ) throw (RuntimeException)
266 ::osl::MutexGuard aGuard( m_aMutex );
268 if ( m_xModel == _inspectorModel )
269 return;
271 impl_bindToNewModel_nothrow( _inspectorModel );
274 //--------------------------------------------------------------------
275 Reference< XObjectInspectorUI > SAL_CALL OPropertyBrowserController::getInspectorUI() throw (RuntimeException)
277 // we're derived from this interface, though we do not expose it in queryInterface and getTypes.
278 return this;
281 //--------------------------------------------------------------------
282 void SAL_CALL OPropertyBrowserController::inspect( const Sequence< Reference< XInterface > >& _rObjects ) throw (com::sun::star::util::VetoException, RuntimeException)
284 SolarMutexGuard aSolarGuard;
285 ::osl::MutexGuard aGuard( m_aMutex );
287 if ( m_bSuspendingPropertyHandlers || !suspendAll_nothrow() )
288 { // we already are trying to suspend the component (this is somewhere up the stack)
289 // OR one of our property handlers raised a veto against closing. Well, we *need* to close
290 // it in order to inspect another object.
291 throw VetoException();
293 if ( m_bBindingIntrospectee )
294 throw VetoException();
296 m_bBindingIntrospectee = true;
297 impl_rebindToInspectee_nothrow( InterfaceArray( _rObjects.getConstArray(), _rObjects.getConstArray() + _rObjects.getLength() ) );
298 m_bBindingIntrospectee = false;
302 //--------------------------------------------------------------------
303 Reference< XDispatch > SAL_CALL OPropertyBrowserController::queryDispatch( const URL& /*URL*/, const ::rtl::OUString& /*TargetFrameName*/, ::sal_Int32 /*SearchFlags*/ ) throw (RuntimeException)
305 // we don't have any dispatches at all, right now
306 return Reference< XDispatch >();
309 //--------------------------------------------------------------------
310 Sequence< Reference< XDispatch > > SAL_CALL OPropertyBrowserController::queryDispatches( const Sequence< DispatchDescriptor >& Requests ) throw (RuntimeException)
312 Sequence< Reference< XDispatch > > aReturn;
313 sal_Int32 nLen = Requests.getLength();
314 aReturn.realloc( nLen );
316 Reference< XDispatch >* pReturn = aReturn.getArray();
317 const Reference< XDispatch >* pReturnEnd = aReturn.getArray() + nLen;
318 const DispatchDescriptor* pDescripts = Requests.getConstArray();
320 for ( ; pReturn != pReturnEnd; ++ pReturn, ++pDescripts )
321 *pReturn = queryDispatch( pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags );
323 return aReturn;
326 //------------------------------------------------------------------------
327 void SAL_CALL OPropertyBrowserController::initialize( const Sequence< Any >& _arguments ) throw (Exception, RuntimeException)
329 if ( m_bConstructed )
330 throw AlreadyInitializedException();
332 StlSyntaxSequence< Any > arguments( _arguments );
333 if ( arguments.empty() )
334 { // constructor: "createDefault()"
335 createDefault();
336 return;
339 Reference< XObjectInspectorModel > xModel;
340 if ( arguments.size() == 1 )
341 { // constructor: "createWithModel( XObjectInspectorModel )"
342 if ( !( arguments[0] >>= xModel ) )
343 throw IllegalArgumentException( ::rtl::OUString(), *this, 0 );
344 createWithModel( xModel );
345 return;
348 throw IllegalArgumentException( ::rtl::OUString(), *this, 0 );
351 //------------------------------------------------------------------------
352 void OPropertyBrowserController::createDefault()
354 m_bConstructed = true;
357 //------------------------------------------------------------------------
358 void OPropertyBrowserController::createWithModel( const Reference< XObjectInspectorModel >& _rxModel )
360 osl_incrementInterlockedCount( &m_refCount );
362 setInspectorModel( _rxModel );
364 osl_decrementInterlockedCount( &m_refCount );
366 m_bConstructed = true;
369 //------------------------------------------------------------------------
370 void SAL_CALL OPropertyBrowserController::attachFrame( const Reference< XFrame >& _rxFrame ) throw(RuntimeException)
372 SolarMutexGuard aSolarGuard;
373 ::osl::MutexGuard aGuard( m_aMutex );
375 if (_rxFrame.is() && haveView())
376 throw RuntimeException(::rtl::OUString("Unable to attach to a second frame."),*this);
378 // revoke as focus listener from the old container window
379 stopContainerWindowListening();
381 m_xFrame = _rxFrame;
382 if (!m_xFrame.is())
383 return;
385 // TODO: this construction perhaps should be done outside. Don't know the exact meaning of attachFrame.
386 // Maybe it is intended to only announce the frame to the controller, and the instance doing this
387 // announcement is responsible for calling setComponent, too.
388 Reference< XWindow > xContainerWindow = m_xFrame->getContainerWindow();
389 VCLXWindow* pContainerWindow = VCLXWindow::GetImplementation(xContainerWindow);
390 Window* pParentWin = pContainerWindow ? pContainerWindow->GetWindow() : NULL;
391 if (!pParentWin)
392 throw RuntimeException(::rtl::OUString("The frame is invalid. Unable to extract the container window."),*this);
394 if ( Construct( pParentWin ) )
398 m_xFrame->setComponent( VCLUnoHelper::GetInterface( m_pView ), this );
400 catch( const Exception& )
402 OSL_FAIL( "OPropertyBrowserController::attachFrame: caught an exception!" );
406 startContainerWindowListening();
408 UpdateUI();
411 //------------------------------------------------------------------------
412 sal_Bool SAL_CALL OPropertyBrowserController::attachModel( const Reference< XModel >& _rxModel ) throw(RuntimeException)
414 Reference< XObjectInspectorModel > xModel( _rxModel, UNO_QUERY );
415 if ( !xModel.is() )
416 return false;
418 setInspectorModel( xModel );
419 return getInspectorModel() == _rxModel;
422 //------------------------------------------------------------------------
423 sal_Bool OPropertyBrowserController::suspendAll_nothrow()
425 // if there is a handle inside its "onInteractivePropertySelection" method,
426 // then veto
427 // Normally, we could expect every handler to do this itself, but being
428 // realistic, it's safer to handle this here in general.
429 if ( m_xInteractiveHandler.is() )
430 return sal_False;
432 m_bSuspendingPropertyHandlers = true;
433 sal_Bool bHandlerVeto = !suspendPropertyHandlers_nothrow( sal_True );
434 m_bSuspendingPropertyHandlers = false;
435 if ( bHandlerVeto )
436 return sal_False;
438 return sal_True;
441 //------------------------------------------------------------------------
442 sal_Bool OPropertyBrowserController::suspendPropertyHandlers_nothrow( sal_Bool _bSuspend )
444 PropertyHandlerArray aAllHandlers; // will contain every handler exactly once
445 for ( PropertyHandlerRepository::const_iterator handler = m_aPropertyHandlers.begin();
446 handler != m_aPropertyHandlers.end();
447 ++handler
450 if ( ::std::find( aAllHandlers.begin(), aAllHandlers.end(), handler->second ) != aAllHandlers.end() )
451 // already visited this particular handler (m_aPropertyHandlers usually contains
452 // the same handler more than once)
453 continue;
454 aAllHandlers.push_back( handler->second );
457 for ( PropertyHandlerArray::iterator loop = aAllHandlers.begin();
458 loop != aAllHandlers.end();
459 ++loop
464 if ( !(*loop)->suspend( _bSuspend ) )
465 if ( _bSuspend )
466 // if we're not suspending, but reactivating, ignore the error
467 return sal_False;
469 catch( const Exception& )
471 OSL_FAIL( "OPropertyBrowserController::suspendPropertyHandlers_nothrow: caught an exception!" );
474 return sal_True;
477 //------------------------------------------------------------------------
478 sal_Bool SAL_CALL OPropertyBrowserController::suspend( sal_Bool _bSuspend ) throw(RuntimeException)
480 ::osl::MutexGuard aGuard( m_aMutex );
481 OSL_ENSURE( haveView(), "OPropertyBrowserController::suspend: don't have a view anymore!" );
483 if ( !_bSuspend )
484 { // this means a "suspend" is to be "revoked"
485 suspendPropertyHandlers_nothrow( sal_False );
486 // we ourself cannot revoke our suspend
487 return sal_False;
490 if ( !suspendAll_nothrow() )
491 return sal_False;
493 // commit the editor's content
494 if ( haveView() )
495 getPropertyBox().CommitModified();
497 // stop listening
498 stopContainerWindowListening();
500 // outtahere
501 return sal_True;
504 //------------------------------------------------------------------------
505 Any SAL_CALL OPropertyBrowserController::getViewData( ) throw(RuntimeException)
507 return makeAny( m_sPageSelection );
510 //------------------------------------------------------------------------
511 void SAL_CALL OPropertyBrowserController::restoreViewData( const Any& Data ) throw(RuntimeException)
513 ::rtl::OUString sPageSelection;
514 if ( ( Data >>= sPageSelection ) && !sPageSelection.isEmpty() )
516 m_sPageSelection = sPageSelection;
517 selectPageFromViewData();
521 //------------------------------------------------------------------------
522 Reference< XModel > SAL_CALL OPropertyBrowserController::getModel( ) throw(RuntimeException)
524 // have no model
525 return Reference< XModel >();
528 //------------------------------------------------------------------------
529 Reference< XFrame > SAL_CALL OPropertyBrowserController::getFrame( ) throw(RuntimeException)
531 return m_xFrame;
534 //------------------------------------------------------------------------
535 void SAL_CALL OPropertyBrowserController::dispose( ) throw(RuntimeException)
537 SolarMutexGuard aSolarGuard;
539 // stop inspecting the current object
540 stopInspection( false );
542 // say our dispose listeners goodbye
543 ::com::sun::star::lang::EventObject aEvt;
544 aEvt.Source = static_cast< ::cppu::OWeakObject* >(this);
545 m_aDisposeListeners.disposeAndClear(aEvt);
546 m_aControlObservers.disposeAndClear(aEvt);
548 // don't delete explicitly (this is done by the frame we reside in)
549 m_pView = NULL;
551 Reference< XComponent > xViewAsComp( m_xView, UNO_QUERY );
552 if ( xViewAsComp.is() )
553 xViewAsComp->removeEventListener( static_cast< XPropertyChangeListener* >( this ) );
554 m_xView.clear( );
556 m_aInspectedObjects.clear();
557 impl_bindToNewModel_nothrow( NULL );
560 //------------------------------------------------------------------------
561 void SAL_CALL OPropertyBrowserController::addEventListener( const Reference< XEventListener >& _rxListener ) throw(RuntimeException)
563 m_aDisposeListeners.addInterface(_rxListener);
566 //------------------------------------------------------------------------
567 void SAL_CALL OPropertyBrowserController::removeEventListener( const Reference< XEventListener >& _rxListener ) throw(RuntimeException)
569 m_aDisposeListeners.removeInterface(_rxListener);
572 //------------------------------------------------------------------------
573 ::rtl::OUString SAL_CALL OPropertyBrowserController::getImplementationName( ) throw(RuntimeException)
575 return getImplementationName_static();
578 //------------------------------------------------------------------------
579 sal_Bool SAL_CALL OPropertyBrowserController::supportsService( const ::rtl::OUString& ServiceName ) throw(RuntimeException)
581 Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
582 const ::rtl::OUString* pArray = aSupported.getConstArray();
583 for (sal_Int32 i = 0; i < aSupported.getLength(); ++i, ++pArray)
584 if (pArray->equals(ServiceName))
585 return sal_True;
586 return sal_False;
589 //------------------------------------------------------------------------
590 Sequence< ::rtl::OUString > SAL_CALL OPropertyBrowserController::getSupportedServiceNames( ) throw(RuntimeException)
592 return getSupportedServiceNames_static();
595 //------------------------------------------------------------------------
596 ::rtl::OUString OPropertyBrowserController::getImplementationName_static( ) throw(RuntimeException)
598 return ::rtl::OUString("org.openoffice.comp.extensions.ObjectInspector");
601 //------------------------------------------------------------------------
602 Sequence< ::rtl::OUString > OPropertyBrowserController::getSupportedServiceNames_static( ) throw(RuntimeException)
604 Sequence< ::rtl::OUString > aSupported(1);
605 aSupported[0] = ::rtl::OUString("com.sun.star.inspection.ObjectInspector");
606 return aSupported;
609 //------------------------------------------------------------------------
610 Reference< XInterface > SAL_CALL OPropertyBrowserController::Create(const Reference< XComponentContext >& _rxContext)
612 return *(new OPropertyBrowserController( _rxContext ) );
615 //------------------------------------------------------------------------
616 void SAL_CALL OPropertyBrowserController::focusGained( const FocusEvent& _rSource ) throw (RuntimeException)
618 Reference< XWindow > xSourceWindow(_rSource.Source, UNO_QUERY);
619 Reference< XWindow > xContainerWindow;
620 if (m_xFrame.is())
621 xContainerWindow = m_xFrame->getContainerWindow();
623 if ( xContainerWindow.get() == xSourceWindow.get() )
624 { // our container window got the focus
625 if ( haveView() )
626 getPropertyBox().GrabFocus();
630 //------------------------------------------------------------------------
631 void SAL_CALL OPropertyBrowserController::focusLost( const FocusEvent& /*_rSource*/ ) throw (RuntimeException)
633 // not interested in
636 //------------------------------------------------------------------------
637 void SAL_CALL OPropertyBrowserController::disposing( const EventObject& _rSource ) throw(RuntimeException)
639 if ( m_xView.is() && ( m_xView == _rSource.Source ) )
641 m_xView = NULL;
642 m_pView = NULL;
645 for ( InterfaceArray::iterator loop = m_aInspectedObjects.begin();
646 loop != m_aInspectedObjects.end();
647 ++loop
650 if ( *loop == _rSource.Source )
652 m_aInspectedObjects.erase( loop );
653 break;
658 //------------------------------------------------------------------------
659 IMPL_LINK_NOARG(OPropertyBrowserController, OnPageActivation)
661 updateViewDataFromActivePage();
662 return 0L;
665 //------------------------------------------------------------------------
666 void OPropertyBrowserController::updateViewDataFromActivePage()
668 if (!haveView())
669 return;
671 ::rtl::OUString sOldSelection = m_sPageSelection;
672 m_sPageSelection = ::rtl::OUString();
674 const sal_uInt16 nCurrentPage = m_pView->getActivaPage();
675 if ( (sal_uInt16)-1 != nCurrentPage )
677 for ( HashString2Int16::const_iterator pageId = m_aPageIds.begin();
678 pageId != m_aPageIds.end();
679 ++pageId
682 if ( nCurrentPage == pageId->second )
684 m_sPageSelection = pageId->first;
685 break;
690 if ( !m_sPageSelection.isEmpty() )
691 m_sLastValidPageSelection = m_sPageSelection;
692 else if ( !sOldSelection.isEmpty() )
693 m_sLastValidPageSelection = sOldSelection;
696 //------------------------------------------------------------------------
697 sal_uInt16 OPropertyBrowserController::impl_getPageIdForCategory_nothrow( const ::rtl::OUString& _rCategoryName ) const
699 sal_uInt16 nPageId = (sal_uInt16)-1;
700 HashString2Int16::const_iterator pagePos = m_aPageIds.find( _rCategoryName );
701 if ( pagePos != m_aPageIds.end() )
702 nPageId = pagePos->second;
703 return nPageId;
706 //------------------------------------------------------------------------
707 void OPropertyBrowserController::selectPageFromViewData()
709 sal_uInt16 nNewPage = impl_getPageIdForCategory_nothrow( m_sPageSelection );
711 if ( haveView() && ( nNewPage != (sal_uInt16)-1 ) )
712 m_pView->activatePage( nNewPage );
714 // just in case ...
715 updateViewDataFromActivePage();
718 //------------------------------------------------------------------------
719 sal_Bool OPropertyBrowserController::Construct(Window* _pParentWin)
721 DBG_ASSERT(!haveView(), "OPropertyBrowserController::Construct: already have a view!");
722 DBG_ASSERT(_pParentWin, "OPropertyBrowserController::Construct: invalid parent window!");
724 m_pView = new OPropertyBrowserView(m_aContext.getLegacyServiceFactory(), _pParentWin);
725 m_pView->setPageActivationHandler(LINK(this, OPropertyBrowserController, OnPageActivation));
727 // add as dispose listener for our view. The view is disposed by the frame we're plugged into,
728 // and this disposal _deletes_ the view, so it would be deadly if we use our m_pView member
729 // after that
730 m_xView = VCLUnoHelper::GetInterface(m_pView);
731 Reference< XComponent > xViewAsComp(m_xView, UNO_QUERY);
732 if (xViewAsComp.is())
733 xViewAsComp->addEventListener( static_cast< XPropertyChangeListener* >( this ) );
735 getPropertyBox().SetLineListener(this);
736 getPropertyBox().SetControlObserver(this);
737 impl_initializeView_nothrow();
739 m_pView->Show();
741 return sal_True;
744 //------------------------------------------------------------------------
745 void SAL_CALL OPropertyBrowserController::propertyChange( const PropertyChangeEvent& _rEvent ) throw (RuntimeException)
747 if ( _rEvent.Source == m_xModel )
749 if ( _rEvent.PropertyName == "IsReadOnly" )
750 impl_updateReadOnlyView_nothrow();
751 return;
754 if ( m_sCommittingProperty == _rEvent.PropertyName )
755 return;
757 if ( !haveView() )
758 return;
760 Any aNewValue( _rEvent.NewValue );
761 if ( impl_hasPropertyHandlerFor_nothrow( _rEvent.PropertyName ) )
763 // forward the new value to the property box, to reflect the change in the UI
764 aNewValue = impl_getPropertyValue_throw( _rEvent.PropertyName );
766 // check whether the state is ambiguous. This is interesting in case we display the properties
767 // for multiple objects at once: In this case, we'll get a notification from one of the objects,
768 // but need to care for the "composed" value, which can be "ambiguous".
769 PropertyHandlerRef xHandler( impl_getHandlerForProperty_throw( _rEvent.PropertyName ), UNO_SET_THROW );
770 PropertyState ePropertyState( xHandler->getPropertyState( _rEvent.PropertyName ) );
771 bool bAmbiguousValue = ( PropertyState_AMBIGUOUS_VALUE == ePropertyState );
773 getPropertyBox().SetPropertyValue( _rEvent.PropertyName, aNewValue, bAmbiguousValue );
776 // if it's a actuating property, then update the UI for any dependent
777 // properties
778 if ( impl_isActuatingProperty_nothrow( _rEvent.PropertyName ) )
779 impl_broadcastPropertyChange_nothrow( _rEvent.PropertyName, aNewValue, _rEvent.OldValue, false );
782 //------------------------------------------------------------------------
783 Reference< XPropertyControl > SAL_CALL OPropertyBrowserController::createPropertyControl( ::sal_Int16 ControlType, ::sal_Bool _CreateReadOnly ) throw (IllegalArgumentException, RuntimeException)
785 ::osl::MutexGuard aGuard( m_aMutex );
787 Reference< XPropertyControl > xControl;
789 // default winbits: a border only
790 WinBits nWinBits = WB_BORDER;
792 // read-only-ness
793 _CreateReadOnly |= (sal_Bool)impl_isReadOnlyModel_throw();
794 if ( _CreateReadOnly )
795 nWinBits |= WB_READONLY;
797 switch ( ControlType )
799 case PropertyControlType::StringListField:
800 xControl = new OMultilineEditControl( &getPropertyBox(), eStringList, nWinBits | WB_DROPDOWN | WB_TABSTOP );
801 break;
803 case PropertyControlType::MultiLineTextField:
804 xControl = new OMultilineEditControl( &getPropertyBox(), eMultiLineText, nWinBits | WB_DROPDOWN | WB_TABSTOP );
805 break;
807 case PropertyControlType::ListBox:
808 xControl = new OListboxControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_DROPDOWN);
809 break;
811 case PropertyControlType::ComboBox:
812 xControl = new OComboboxControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_DROPDOWN);
813 break;
815 case PropertyControlType::TextField:
816 xControl = new OEditControl( &getPropertyBox(), sal_False, nWinBits | WB_TABSTOP );
817 break;
819 case PropertyControlType::CharacterField:
820 xControl = new OEditControl( &getPropertyBox(), sal_True, nWinBits | WB_TABSTOP );
821 break;
823 case PropertyControlType::NumericField:
824 xControl = new ONumericControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_SPIN | WB_REPEAT );
825 break;
827 case PropertyControlType::DateTimeField:
828 xControl = new ODateTimeControl( &getPropertyBox(), nWinBits | WB_TABSTOP );
829 break;
831 case PropertyControlType::DateField:
832 xControl = new ODateControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_SPIN | WB_REPEAT );
833 break;
835 case PropertyControlType::TimeField:
836 xControl = new OTimeControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_SPIN | WB_REPEAT );
837 break;
839 case PropertyControlType::ColorListBox:
840 xControl = new OColorControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_DROPDOWN );
841 break;
843 case PropertyControlType::HyperlinkField:
844 xControl = new OHyperlinkControl( &getPropertyBox(), nWinBits | WB_TABSTOP | WB_DROPDOWN );
845 break;
847 default:
848 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
851 return xControl;
854 //------------------------------------------------------------------------
855 void OPropertyBrowserController::impl_toggleInspecteeListening_nothrow( bool _bOn )
857 for ( InterfaceArray::const_iterator loop = m_aInspectedObjects.begin();
858 loop != m_aInspectedObjects.end();
859 ++loop
864 Reference< XComponent > xComp( *loop, UNO_QUERY );
865 if ( xComp.is() )
867 if ( _bOn )
868 xComp->addEventListener( static_cast< XPropertyChangeListener* >( this ) );
869 else
870 xComp->removeEventListener( static_cast< XPropertyChangeListener* >( this ) );
873 catch( const Exception& )
875 DBG_UNHANDLED_EXCEPTION();
880 //------------------------------------------------------------------------
881 void OPropertyBrowserController::stopInspection( bool _bCommitModified )
883 if ( haveView() )
885 if ( _bCommitModified )
886 // commit the editor's content
887 getPropertyBox().CommitModified();
889 // hide the property box so that it does not flicker
890 getPropertyBox().Hide();
892 // clear the property box
893 getPropertyBox().ClearAll();
896 // destroy the view first
897 if ( haveView() )
899 // remove the pages
900 for ( HashString2Int16::const_iterator erase = m_aPageIds.begin();
901 erase != m_aPageIds.end();
902 ++erase
904 getPropertyBox().RemovePage( erase->second );
905 clearContainer( m_aPageIds );
908 clearContainer( m_aProperties );
910 // de-register as dispose-listener from our inspected objects
911 impl_toggleInspecteeListening_nothrow( false );
913 // handlers are obsolete, so is our "composer" for their UI requests
914 if ( m_pUIRequestComposer.get() )
915 m_pUIRequestComposer->dispose();
916 m_pUIRequestComposer.reset( NULL );
918 // clean up the property handlers
919 PropertyHandlerArray aAllHandlers; // will contain every handler exactly once
920 for ( PropertyHandlerRepository::const_iterator aHandler = m_aPropertyHandlers.begin();
921 aHandler != m_aPropertyHandlers.end();
922 ++aHandler
924 if ( ::std::find( aAllHandlers.begin(), aAllHandlers.end(), aHandler->second ) == aAllHandlers.end() )
925 aAllHandlers.push_back( aHandler->second );
927 for ( PropertyHandlerArray::iterator loop = aAllHandlers.begin();
928 loop != aAllHandlers.end();
929 ++loop
934 (*loop)->removePropertyChangeListener( this );
935 (*loop)->dispose();
937 catch( const DisposedException& )
940 catch( const Exception& )
942 DBG_UNHANDLED_EXCEPTION();
946 clearContainer( m_aPropertyHandlers );
947 clearContainer( m_aDependencyHandlers );
950 //------------------------------------------------------------------------
951 bool OPropertyBrowserController::impl_hasPropertyHandlerFor_nothrow( const ::rtl::OUString& _rPropertyName ) const
953 PropertyHandlerRepository::const_iterator handlerPos = m_aPropertyHandlers.find( _rPropertyName );
954 return ( handlerPos != m_aPropertyHandlers.end() );
957 //------------------------------------------------------------------------
958 OPropertyBrowserController::PropertyHandlerRef OPropertyBrowserController::impl_getHandlerForProperty_throw( const ::rtl::OUString& _rPropertyName ) const
960 PropertyHandlerRepository::const_iterator handlerPos = m_aPropertyHandlers.find( _rPropertyName );
961 if ( handlerPos == m_aPropertyHandlers.end() )
962 throw RuntimeException();
963 return handlerPos->second;
966 //------------------------------------------------------------------------
967 Any OPropertyBrowserController::impl_getPropertyValue_throw( const ::rtl::OUString& _rPropertyName )
969 PropertyHandlerRef handler = impl_getHandlerForProperty_throw( _rPropertyName );
970 return handler->getPropertyValue( _rPropertyName );
973 //------------------------------------------------------------------------
974 void OPropertyBrowserController::impl_rebindToInspectee_nothrow( const InterfaceArray& _rObjects )
978 // stop inspecting the old object(s)
979 stopInspection( true );
981 // inspect the new object(s)
982 m_aInspectedObjects = _rObjects;
983 doInspection();
985 // update the user interface
986 UpdateUI();
989 catch(const Exception&)
991 OSL_FAIL("OPropertyBrowserController::impl_rebindToInspectee_nothrow: caught an exception !");
995 //------------------------------------------------------------------------
996 void OPropertyBrowserController::doInspection()
1000 //////////////////////////////////////////////////////////////////////
1001 // obtain the properties of the object
1002 ::std::vector< Property > aProperties;
1004 PropertyHandlerArray aPropertyHandlers;
1005 getPropertyHandlers( m_aInspectedObjects, aPropertyHandlers );
1007 PropertyHandlerArray::iterator aHandler( aPropertyHandlers.begin() );
1008 while ( aHandler != aPropertyHandlers.end() )
1010 DBG_ASSERT( aHandler->get(), "OPropertyBrowserController::doInspection: invalid handler!" );
1012 StlSyntaxSequence< Property > aThisHandlersProperties = (*aHandler)->getSupportedProperties();
1014 if ( aThisHandlersProperties.empty() )
1016 // this handler doesn't know anything about the current inspectee -> ignore it
1017 (*aHandler)->dispose();
1018 aHandler = aPropertyHandlers.erase( aHandler );
1019 continue;
1022 // append these properties to our "all properties" array
1023 aProperties.reserve( aProperties.size() + aThisHandlersProperties.size() );
1024 for ( StlSyntaxSequence< Property >::const_iterator copyProperty = aThisHandlersProperties.begin();
1025 copyProperty != aThisHandlersProperties.end();
1026 ++copyProperty
1029 ::std::vector< Property >::const_iterator previous = ::std::find_if(
1030 aProperties.begin(),
1031 aProperties.end(),
1032 FindPropertyByName( copyProperty->Name )
1034 if ( previous == aProperties.end() )
1036 aProperties.push_back( *copyProperty );
1037 continue;
1040 // there already was another (previous) handler which supported this property.
1041 // Don't add it to aProperties, again.
1043 // Also, ensure that handlers which previously expressed interest in *changes*
1044 // of this property are not notified.
1045 // This is 'cause we have a new handler which is responsible for this property,
1046 // which means it can give it a completely different meaning than the previous
1047 // handler for this property is prepared for.
1048 ::std::pair< PropertyHandlerMultiRepository::iterator, PropertyHandlerMultiRepository::iterator >
1049 aDepHandlers = m_aDependencyHandlers.equal_range( copyProperty->Name );
1050 m_aDependencyHandlers.erase( aDepHandlers.first, aDepHandlers.second );
1053 // determine the superseded properties
1054 StlSyntaxSequence< ::rtl::OUString > aSupersededByThisHandler = (*aHandler)->getSupersededProperties();
1055 for ( StlSyntaxSequence< ::rtl::OUString >::const_iterator superseded = aSupersededByThisHandler.begin();
1056 superseded != aSupersededByThisHandler.end();
1057 ++superseded
1060 ::std::vector< Property >::iterator existent = ::std::find_if(
1061 aProperties.begin(),
1062 aProperties.end(),
1063 FindPropertyByName( *superseded )
1065 if ( existent != aProperties.end() )
1066 // one of the properties superseded by this handler was supported by a previous
1067 // one -> erase
1068 aProperties.erase( existent );
1071 // be notified of changes which this handler is responsible for
1072 (*aHandler)->addPropertyChangeListener( this );
1074 // remember this handler for every of the properties which it is responsible
1075 // for
1076 for ( StlSyntaxSequence< Property >::const_iterator remember = aThisHandlersProperties.begin();
1077 remember != aThisHandlersProperties.end();
1078 ++remember
1081 m_aPropertyHandlers[ remember->Name ] = *aHandler;
1082 // note that this implies that if two handlers support the same property,
1083 // the latter wins
1086 // see if the handler expresses interest in any actuating properties
1087 StlSyntaxSequence< ::rtl::OUString > aInterestingActuations = (*aHandler)->getActuatingProperties();
1088 for ( StlSyntaxSequence< ::rtl::OUString >::const_iterator aLoop = aInterestingActuations.begin();
1089 aLoop != aInterestingActuations.end();
1090 ++aLoop
1093 m_aDependencyHandlers.insert( PropertyHandlerMultiRepository::value_type(
1094 *aLoop, *aHandler ) );
1097 ++aHandler;
1100 // create a new composer for UI requests coming from the handlers
1101 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( getInspectorUI(), this ) );
1103 // sort the properties by relative position, as indicated by the model
1104 for ( ::std::vector< Property >::const_iterator sourceProps = aProperties.begin();
1105 sourceProps != aProperties.end();
1106 ++sourceProps
1109 sal_Int32 nRelativePropertyOrder = sourceProps - aProperties.begin();
1110 if ( m_xModel.is() )
1111 nRelativePropertyOrder = m_xModel->getPropertyOrderIndex( sourceProps->Name );
1112 while ( m_aProperties.find( nRelativePropertyOrder ) != m_aProperties.end() )
1113 ++nRelativePropertyOrder;
1114 m_aProperties[ nRelativePropertyOrder ] = *sourceProps;
1117 // be notified when one of our inspectees dies
1118 impl_toggleInspecteeListening_nothrow( true );
1120 catch(const Exception&)
1122 OSL_FAIL("OPropertyBrowserController::doInspection : caught an exception !");
1126 //------------------------------------------------------------------------
1127 ::com::sun::star::awt::Size SAL_CALL OPropertyBrowserController::getMinimumSize() throw (::com::sun::star::uno::RuntimeException)
1129 ::com::sun::star::awt::Size aSize;
1130 if( m_pView )
1131 return m_pView->getMinimumSize();
1132 else
1133 return aSize;
1136 //------------------------------------------------------------------------
1137 ::com::sun::star::awt::Size SAL_CALL OPropertyBrowserController::getPreferredSize() throw (::com::sun::star::uno::RuntimeException)
1139 return getMinimumSize();
1142 //------------------------------------------------------------------------
1143 ::com::sun::star::awt::Size SAL_CALL OPropertyBrowserController::calcAdjustedSize( const ::com::sun::star::awt::Size& _rNewSize ) throw (::com::sun::star::uno::RuntimeException)
1145 awt::Size aMinSize = getMinimumSize( );
1146 awt::Size aAdjustedSize( _rNewSize );
1147 if ( aAdjustedSize.Width < aMinSize.Width )
1148 aAdjustedSize.Width = aMinSize.Width;
1149 if ( aAdjustedSize.Height < aMinSize.Height )
1150 aAdjustedSize.Height = aMinSize.Height;
1151 return aAdjustedSize;
1154 //------------------------------------------------------------------------
1155 void OPropertyBrowserController::describePropertyLine( const Property& _rProperty, OLineDescriptor& _rDescriptor ) SAL_THROW((Exception))
1159 PropertyHandlerRepository::const_iterator handler = m_aPropertyHandlers.find( _rProperty.Name );
1160 if ( handler == m_aPropertyHandlers.end() )
1161 throw RuntimeException(); // caught below
1163 _rDescriptor.assignFrom( handler->second->describePropertyLine( _rProperty.Name, this ) );
1165 //////////////////////////////////////////////////////////////////////
1167 _rDescriptor.xPropertyHandler = handler->second;
1168 _rDescriptor.sName = _rProperty.Name;
1169 _rDescriptor.aValue = _rDescriptor.xPropertyHandler->getPropertyValue( _rProperty.Name );
1171 if ( _rDescriptor.DisplayName.isEmpty() )
1173 #ifdef DBG_UTIL
1174 ::rtl::OString sMessage( "OPropertyBrowserController::describePropertyLine: handler did not provide a display name for '" );
1175 sMessage += ::rtl::OString( _rProperty.Name.getStr(), _rProperty.Name.getLength(), RTL_TEXTENCODING_ASCII_US );
1176 sMessage += ::rtl::OString( "'!" );
1177 DBG_ASSERT( !_rDescriptor.DisplayName.isEmpty(), sMessage.getStr() );
1178 #endif
1179 _rDescriptor.DisplayName = _rProperty.Name;
1182 PropertyState ePropertyState( _rDescriptor.xPropertyHandler->getPropertyState( _rProperty.Name ) );
1183 if ( PropertyState_AMBIGUOUS_VALUE == ePropertyState )
1185 _rDescriptor.bUnknownValue = true;
1186 _rDescriptor.aValue.clear();
1189 _rDescriptor.bReadOnly = impl_isReadOnlyModel_throw();
1191 catch( const Exception& )
1193 OSL_FAIL( "OPropertyBrowserController::describePropertyLine: caught an exception!" );
1197 //------------------------------------------------------------------------
1198 void OPropertyBrowserController::impl_buildCategories_throw()
1200 OSL_PRECOND( m_aPageIds.empty(), "OPropertyBrowserController::impl_buildCategories_throw: duplicate call!" );
1202 StlSyntaxSequence< PropertyCategoryDescriptor > aCategories;
1203 if ( m_xModel.is() )
1204 aCategories = m_xModel->describeCategories();
1206 for ( StlSyntaxSequence< PropertyCategoryDescriptor >::const_iterator category = aCategories.begin();
1207 category != aCategories.end();
1208 ++category
1211 OSL_ENSURE( m_aPageIds.find( category->ProgrammaticName ) == m_aPageIds.end(),
1212 "OPropertyBrowserController::impl_buildCategories_throw: duplicate programmatic name!" );
1214 m_aPageIds[ category->ProgrammaticName ] =
1215 getPropertyBox().AppendPage( category->UIName, HelpIdUrl::getHelpId( category->HelpURL ) );
1219 //------------------------------------------------------------------------
1220 void OPropertyBrowserController::UpdateUI()
1224 if ( !haveView() )
1225 // too early, will return later
1226 return;
1228 getPropertyBox().DisableUpdate();
1230 sal_Bool bHaveFocus = getPropertyBox().HasChildPathFocus();
1232 // create our tab pages
1233 impl_buildCategories_throw();
1234 // (and allow for pages to be actually unused)
1235 ::std::set< sal_uInt16 > aUsedPages;
1237 // when building the UI below, remember which properties are actuating,
1238 // to allow for a initial actuatinPropertyChanged call
1239 ::std::vector< ::rtl::OUString > aActuatingProperties;
1240 ::std::vector< Any > aActuatingPropertyValues;
1242 // ask the handlers to describe the property UI, and insert the resulting
1243 // entries into our list boxes
1244 OrderedPropertyMap::const_iterator property( m_aProperties.begin() );
1245 for ( ; property != m_aProperties.end(); ++property )
1247 OLineDescriptor aDescriptor;
1248 describePropertyLine( property->second, aDescriptor );
1250 bool bIsActuatingProperty = impl_isActuatingProperty_nothrow( property->second.Name );
1252 #if OSL_DEBUG_LEVEL > 0
1253 if ( aDescriptor.Category.isEmpty() )
1255 ::rtl::OString sMessage( "OPropertyBrowserController::UpdateUI: empty category provided for property '" );
1256 sMessage += ::rtl::OString( property->second.Name.getStr(), property->second.Name.getLength(), osl_getThreadTextEncoding() );
1257 sMessage += "'!";
1258 OSL_FAIL( sMessage.getStr() );
1260 #endif
1261 // finally insert this property control
1262 sal_uInt16 nTargetPageId = impl_getPageIdForCategory_nothrow( aDescriptor.Category );
1263 if ( nTargetPageId == (sal_uInt16)-1 )
1265 // this category does not yet exist. This is allowed, as an inspector model might be lazy, and not provide
1266 // any category information of its own. In this case, we have a fallback ...
1267 m_aPageIds[ aDescriptor.Category ] =
1268 getPropertyBox().AppendPage( aDescriptor.Category, rtl::OString() );
1269 nTargetPageId = impl_getPageIdForCategory_nothrow( aDescriptor.Category );
1272 getPropertyBox().InsertEntry( aDescriptor, nTargetPageId );
1273 aUsedPages.insert( nTargetPageId );
1275 // if it's an actuating property, remember it
1276 if ( bIsActuatingProperty )
1278 aActuatingProperties.push_back( property->second.Name );
1279 aActuatingPropertyValues.push_back( impl_getPropertyValue_throw( property->second.Name ) );
1283 // update any dependencies for the actuating properties which we encountered
1285 ::std::vector< ::rtl::OUString >::const_iterator aProperty = aActuatingProperties.begin();
1286 ::std::vector< Any >::const_iterator aPropertyValue = aActuatingPropertyValues.begin();
1287 for ( ; aProperty != aActuatingProperties.end(); ++aProperty, ++aPropertyValue )
1288 impl_broadcastPropertyChange_nothrow( *aProperty, *aPropertyValue, *aPropertyValue, true );
1291 // remove any unused pages (which we did not encounter properties for)
1292 HashString2Int16 aSurvivingPageIds;
1293 for ( HashString2Int16::iterator pageId = m_aPageIds.begin();
1294 pageId != m_aPageIds.end();
1295 ++pageId
1298 if ( aUsedPages.find( pageId->second ) == aUsedPages.end() )
1299 getPropertyBox().RemovePage( pageId->second );
1300 else
1301 aSurvivingPageIds.insert( *pageId );
1303 m_aPageIds.swap( aSurvivingPageIds );
1306 getPropertyBox().Show();
1307 getPropertyBox().EnableUpdate();
1308 if ( bHaveFocus )
1309 getPropertyBox().GrabFocus();
1311 // activate the first page
1312 if ( !m_aPageIds.empty() )
1314 Sequence< PropertyCategoryDescriptor > aCategories( m_xModel->describeCategories() );
1315 if ( aCategories.getLength() )
1316 m_pView->activatePage( m_aPageIds[ aCategories[0].ProgrammaticName ] );
1317 else
1318 // allowed: if we default-created the pages ...
1319 m_pView->activatePage( m_aPageIds.begin()->second );
1322 // activate the previously active page (if possible)
1323 if ( !m_sLastValidPageSelection.isEmpty() )
1324 m_sPageSelection = m_sLastValidPageSelection;
1325 selectPageFromViewData();
1327 catch( const Exception& )
1329 DBG_UNHANDLED_EXCEPTION();
1333 //------------------------------------------------------------------------
1334 void OPropertyBrowserController::Clicked( const ::rtl::OUString& _rName, sal_Bool _bPrimary )
1338 // since the browse buttons do not get the focus when clicked with the mouse,
1339 // we need to commit the changes in the current property field
1340 getPropertyBox().CommitModified();
1342 PropertyHandlerRepository::const_iterator handler = m_aPropertyHandlers.find( _rName );
1343 DBG_ASSERT( handler != m_aPropertyHandlers.end(), "OPropertyBrowserController::Clicked: a property without handler? This will crash!" );
1345 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
1347 Any aData;
1348 m_xInteractiveHandler = handler->second;
1349 InteractiveSelectionResult eResult =
1350 handler->second->onInteractivePropertySelection( _rName, _bPrimary, aData,
1351 m_pUIRequestComposer->getUIForPropertyHandler( handler->second ) );
1353 switch ( eResult )
1355 case InteractiveSelectionResult_Cancelled:
1356 case InteractiveSelectionResult_Success:
1357 // okay, nothing to do
1358 break;
1359 case InteractiveSelectionResult_ObtainedValue:
1360 handler->second->setPropertyValue( _rName, aData );
1361 break;
1362 case InteractiveSelectionResult_Pending:
1363 // also okay, we expect that the handler has disabled the UI as necessary
1364 break;
1365 default:
1366 OSL_FAIL( "OPropertyBrowserController::Clicked: unknown result value!" );
1367 break;
1370 catch (const Exception&)
1372 DBG_UNHANDLED_EXCEPTION();
1374 m_xInteractiveHandler = NULL;
1377 //------------------------------------------------------------------------
1378 sal_Bool SAL_CALL OPropertyBrowserController::hasPropertyByName( const ::rtl::OUString& _rName ) throw (RuntimeException)
1380 for ( OrderedPropertyMap::const_iterator search = m_aProperties.begin();
1381 search != m_aProperties.end();
1382 ++search
1384 if ( search->second.Name == _rName )
1385 return true;
1386 return false;
1389 //------------------------------------------------------------------------
1390 void OPropertyBrowserController::Commit( const ::rtl::OUString& rName, const Any& _rValue )
1394 rtl::OUString sPlcHolder = String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) );
1395 bool bIsPlaceHolderValue = false;
1397 if ( rName.equals( PROPERTY_IMAGE_URL ) )
1399 // if the prop value is the PlaceHolder
1400 // can ignore it
1401 rtl::OUString sVal;
1402 _rValue >>= sVal;
1403 if ( sVal.equals( sPlcHolder ) )
1404 bIsPlaceHolderValue = true;
1406 m_sCommittingProperty = rName;
1408 bool bIsActuatingProperty = impl_isActuatingProperty_nothrow( rName );
1410 Any aOldValue;
1411 if ( bIsActuatingProperty )
1412 aOldValue = impl_getPropertyValue_throw( rName );
1414 // do we have a dedicated handler for this property, which we can delegate some tasks to?
1415 PropertyHandlerRef handler = impl_getHandlerForProperty_throw( rName );
1417 //////////////////////////////////////////////////////////////////////
1418 // set the value ( only if it's not a placeholder )
1419 if ( !bIsPlaceHolderValue )
1420 handler->setPropertyValue( rName, _rValue );
1422 //////////////////////////////////////////////////////////////////////
1423 // re-retrieve the value
1424 Any aNormalizedValue = handler->getPropertyValue( rName );
1426 // care for any inter-property dependencies
1427 if ( bIsActuatingProperty )
1428 impl_broadcastPropertyChange_nothrow( rName, aNormalizedValue, aOldValue, false );
1430 // and display it again. This ensures proper formatting
1431 getPropertyBox().SetPropertyValue( rName, aNormalizedValue, false );
1433 catch(const PropertyVetoException& eVetoException)
1435 InfoBox(m_pView, eVetoException.Message).Execute();
1436 PropertyHandlerRef handler = impl_getHandlerForProperty_throw( rName );
1437 Any aNormalizedValue = handler->getPropertyValue( rName );
1438 getPropertyBox().SetPropertyValue( rName, aNormalizedValue, false );
1440 catch(const Exception&)
1442 OSL_FAIL("OPropertyBrowserController::Commit : caught an exception !");
1445 m_sCommittingProperty = ::rtl::OUString();
1448 //--------------------------------------------------------------------
1449 namespace
1453 //--------------------------------------------------------------------
1454 void OPropertyBrowserController::focusGained( const Reference< XPropertyControl >& _Control )
1456 m_aControlObservers.notifyEach( &XPropertyControlObserver::focusGained, _Control );
1459 //--------------------------------------------------------------------
1460 void OPropertyBrowserController::valueChanged( const Reference< XPropertyControl >& _Control )
1462 m_aControlObservers.notifyEach( &XPropertyControlObserver::valueChanged, _Control );
1465 //------------------------------------------------------------------------
1466 namespace
1468 Reference< XPropertyHandler > lcl_createHandler( const ComponentContext& _rContext, const Any& _rFactoryDescriptor )
1470 Reference< XPropertyHandler > xHandler;
1472 ::rtl::OUString sServiceName;
1473 Reference< XSingleServiceFactory > xServiceFac;
1474 Reference< XSingleComponentFactory > xComponentFac;
1476 if ( _rFactoryDescriptor >>= sServiceName )
1477 _rContext.createComponent( sServiceName, xHandler );
1478 else if ( _rFactoryDescriptor >>= xServiceFac )
1479 xHandler = xHandler.query( xServiceFac->createInstance() );
1480 else if ( _rFactoryDescriptor >>= xComponentFac )
1481 xHandler = xHandler.query( xComponentFac->createInstanceWithContext( _rContext.getUNOContext() ) );
1482 OSL_ENSURE(xHandler.is(),"lcl_createHandler: Can not create handler");
1483 return xHandler;
1487 //------------------------------------------------------------------------
1488 void OPropertyBrowserController::getPropertyHandlers( const InterfaceArray& _rObjects, PropertyHandlerArray& _rHandlers )
1490 _rHandlers.resize( 0 );
1491 if ( _rObjects.empty() )
1492 return;
1494 // create a component context for the handlers, containing some information about where
1495 // they live
1496 Reference< XComponentContext > xHandlerContext( m_aContext.getUNOContext() );
1498 // if our own creator did not pass a dialog parent window, use our own view for this
1499 Reference< XWindow > xParentWindow( m_aContext.getContextValueByAsciiName( "DialogParentWindow" ), UNO_QUERY );
1500 if ( !xParentWindow.is() )
1502 ::cppu::ContextEntry_Init aHandlerContextInfo[] =
1504 ::cppu::ContextEntry_Init( ::rtl::OUString( "DialogParentWindow" ), makeAny( VCLUnoHelper::GetInterface( m_pView ) ) )
1506 xHandlerContext = ::cppu::createComponentContext(
1507 aHandlerContextInfo, SAL_N_ELEMENTS( aHandlerContextInfo ),
1508 m_aContext.getUNOContext() );
1511 Sequence< Any > aHandlerFactories;
1512 if ( m_xModel.is() )
1513 aHandlerFactories = m_xModel->getHandlerFactories();
1515 const Any* pHandlerFactory = aHandlerFactories.getConstArray();
1516 const Any* pHandlerFactoryEnd = aHandlerFactories.getConstArray() + aHandlerFactories.getLength();
1518 while ( pHandlerFactory != pHandlerFactoryEnd )
1520 if ( _rObjects.size() == 1 )
1521 { // we're inspecting only one object -> one handler
1522 Reference< XPropertyHandler > xHandler( lcl_createHandler( m_aContext, *pHandlerFactory ) );
1523 if ( xHandler.is() )
1525 xHandler->inspect( _rObjects[0] );
1526 _rHandlers.push_back( xHandler );
1529 else
1531 // create a single handler for every single object
1532 ::std::vector< Reference< XPropertyHandler > > aSingleHandlers( _rObjects.size() );
1533 ::std::vector< Reference< XPropertyHandler > >::iterator pHandler = aSingleHandlers.begin();
1535 InterfaceArray::const_iterator pObject = _rObjects.begin();
1536 InterfaceArray::const_iterator pObjectEnd = _rObjects.end();
1538 for ( ; pObject != pObjectEnd; ++pObject )
1540 *pHandler = lcl_createHandler( m_aContext, *pHandlerFactory );
1541 if ( pHandler->is() )
1543 (*pHandler)->inspect( *pObject );
1544 ++pHandler;
1547 aSingleHandlers.resize( pHandler - aSingleHandlers.begin() );
1549 // then create a handler which composes information out of those single handlers
1550 if ( !aSingleHandlers.empty() )
1551 _rHandlers.push_back( new PropertyComposer( aSingleHandlers ) );
1554 ++pHandlerFactory;
1557 // note that the handlers will not be used by our caller, if they indicate that there are no
1558 // properties they feel responsible for
1561 //------------------------------------------------------------------------
1562 bool OPropertyBrowserController::impl_findObjectProperty_nothrow( const ::rtl::OUString& _rName, OrderedPropertyMap::const_iterator* _pProperty )
1564 OrderedPropertyMap::const_iterator search = m_aProperties.begin();
1565 for ( ; search != m_aProperties.end(); ++search )
1566 if ( search->second.Name == _rName )
1567 break;
1568 if ( _pProperty )
1569 *_pProperty = search;
1570 return ( search != m_aProperties.end() );
1573 //------------------------------------------------------------------------
1574 void OPropertyBrowserController::rebuildPropertyUI( const ::rtl::OUString& _rPropertyName ) throw (RuntimeException)
1576 ::osl::MutexGuard aGuard( m_aMutex );
1577 if ( !haveView() )
1578 throw RuntimeException();
1580 OrderedPropertyMap::const_iterator propertyPos;
1581 if ( !impl_findObjectProperty_nothrow( _rPropertyName, &propertyPos ) )
1582 return;
1584 OLineDescriptor aDescriptor;
1587 describePropertyLine( propertyPos->second, aDescriptor );
1589 catch( const Exception& )
1591 OSL_FAIL( "OPropertyBrowserController::rebuildPropertyUI: caught an exception!" );
1594 getPropertyBox().ChangeEntry( aDescriptor );
1597 //------------------------------------------------------------------------
1598 void OPropertyBrowserController::enablePropertyUI( const ::rtl::OUString& _rPropertyName, sal_Bool _bEnable ) throw (RuntimeException)
1600 ::osl::MutexGuard aGuard( m_aMutex );
1601 if ( !haveView() )
1602 throw RuntimeException();
1604 if ( !impl_findObjectProperty_nothrow( _rPropertyName ) )
1605 return;
1607 getPropertyBox().EnablePropertyLine( _rPropertyName, _bEnable );
1610 //------------------------------------------------------------------------
1611 void OPropertyBrowserController::enablePropertyUIElements( const ::rtl::OUString& _rPropertyName, sal_Int16 _nElements, sal_Bool _bEnable ) throw (RuntimeException)
1613 ::osl::MutexGuard aGuard( m_aMutex );
1614 if ( !haveView() )
1615 throw RuntimeException();
1617 if ( !impl_findObjectProperty_nothrow( _rPropertyName ) )
1618 return;
1620 getPropertyBox().EnablePropertyControls( _rPropertyName, _nElements, _bEnable );
1623 //------------------------------------------------------------------------
1624 void OPropertyBrowserController::showPropertyUI( const ::rtl::OUString& _rPropertyName ) throw (RuntimeException)
1626 ::osl::MutexGuard aGuard( m_aMutex );
1627 if ( !haveView() )
1628 throw RuntimeException();
1630 // look up the property in our object properties
1631 OrderedPropertyMap::const_iterator propertyPos;
1632 if ( !impl_findObjectProperty_nothrow( _rPropertyName, &propertyPos ) )
1633 return;
1635 if ( getPropertyBox().GetPropertyPos( _rPropertyName ) != LISTBOX_ENTRY_NOTFOUND )
1637 rebuildPropertyUI( _rPropertyName );
1638 return;
1641 OLineDescriptor aDescriptor;
1642 describePropertyLine( propertyPos->second, aDescriptor );
1644 // look for the position to insert the property
1646 // side note: The methods GetPropertyPos and InsertEntry of the OPropertyEditor work
1647 // only on the current page. This implies that it's impossible to use this method here
1648 // to show property lines which are *not* on the current page.
1649 // This is sufficient for now, but should be changed in the future.
1651 // by definition, the properties in m_aProperties are in the order in which they appear in the UI
1652 // So all we need is a predecessor of pProperty in m_aProperties
1653 sal_uInt16 nUIPos = LISTBOX_ENTRY_NOTFOUND;
1656 if ( propertyPos != m_aProperties.begin() )
1657 --propertyPos;
1658 nUIPos = getPropertyBox().GetPropertyPos( propertyPos->second.Name );
1660 while ( ( nUIPos == LISTBOX_ENTRY_NOTFOUND ) && ( propertyPos != m_aProperties.begin() ) );
1662 if ( nUIPos == LISTBOX_ENTRY_NOTFOUND )
1663 // insert at the very top
1664 nUIPos = 0;
1665 else
1666 // insert right after the predecessor we found
1667 ++nUIPos;
1669 getPropertyBox().InsertEntry(
1670 aDescriptor, impl_getPageIdForCategory_nothrow( aDescriptor.Category ), nUIPos );
1673 //------------------------------------------------------------------------
1674 void OPropertyBrowserController::hidePropertyUI( const ::rtl::OUString& _rPropertyName ) throw (RuntimeException)
1676 ::osl::MutexGuard aGuard( m_aMutex );
1677 if ( !haveView() )
1678 throw RuntimeException();
1680 if ( !impl_findObjectProperty_nothrow( _rPropertyName ) )
1681 return;
1683 getPropertyBox().RemoveEntry( _rPropertyName );
1686 //------------------------------------------------------------------------
1687 void OPropertyBrowserController::showCategory( const ::rtl::OUString& _rCategory, sal_Bool _bShow ) throw (RuntimeException)
1689 ::osl::MutexGuard aGuard( m_aMutex );
1690 if ( !haveView() )
1691 throw RuntimeException();
1693 sal_uInt16 nPageId = impl_getPageIdForCategory_nothrow( _rCategory );
1694 OSL_ENSURE( nPageId != (sal_uInt16)-1, "OPropertyBrowserController::showCategory: invalid category!" );
1696 getPropertyBox().ShowPropertyPage( nPageId, _bShow );
1699 //------------------------------------------------------------------------
1700 Reference< XPropertyControl > SAL_CALL OPropertyBrowserController::getPropertyControl( const ::rtl::OUString& _rPropertyName ) throw (RuntimeException)
1702 ::osl::MutexGuard aGuard( m_aMutex );
1703 if ( !haveView() )
1704 throw RuntimeException();
1706 Reference< XPropertyControl > xControl( getPropertyBox().GetPropertyControl( _rPropertyName ) );
1707 return xControl;
1710 //--------------------------------------------------------------------
1711 void SAL_CALL OPropertyBrowserController::registerControlObserver( const Reference< XPropertyControlObserver >& _Observer ) throw (RuntimeException)
1713 m_aControlObservers.addInterface( _Observer );
1716 //--------------------------------------------------------------------
1717 void SAL_CALL OPropertyBrowserController::revokeControlObserver( const Reference< XPropertyControlObserver >& _Observer ) throw (RuntimeException)
1719 m_aControlObservers.removeInterface( _Observer );
1722 //------------------------------------------------------------------------
1723 void SAL_CALL OPropertyBrowserController::setHelpSectionText( const ::rtl::OUString& _rHelpText ) throw (NoSupportException, RuntimeException)
1725 SolarMutexGuard aSolarGuard;
1726 ::osl::MutexGuard aGuard( m_aMutex );
1728 if ( !haveView() )
1729 throw DisposedException();
1731 if ( !getPropertyBox().HasHelpSection() )
1732 throw NoSupportException();
1734 getPropertyBox().SetHelpText( _rHelpText );
1737 //------------------------------------------------------------------------
1738 void OPropertyBrowserController::impl_broadcastPropertyChange_nothrow( const ::rtl::OUString& _rPropertyName, const Any& _rNewValue, const Any& _rOldValue, bool _bFirstTimeInit ) const
1740 // are there one or more handlers which are interested in the actuation?
1741 ::std::pair< PropertyHandlerMultiRepository::const_iterator, PropertyHandlerMultiRepository::const_iterator > aInterestedHandlers =
1742 m_aDependencyHandlers.equal_range( _rPropertyName );
1743 if ( aInterestedHandlers.first == aInterestedHandlers.second )
1744 // none of our handlers is interested in this
1745 return;
1747 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
1750 // collect the responses from all interested handlers
1751 PropertyHandlerMultiRepository::const_iterator handler = aInterestedHandlers.first;
1752 while ( handler != aInterestedHandlers.second )
1754 handler->second->actuatingPropertyChanged( _rPropertyName, _rNewValue, _rOldValue,
1755 m_pUIRequestComposer->getUIForPropertyHandler( handler->second ),
1756 _bFirstTimeInit );
1757 ++handler;
1760 catch( const Exception& )
1762 DBG_UNHANDLED_EXCEPTION();
1766 //............................................................................
1767 } // namespace pcr
1768 //............................................................................
1771 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */