1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uiconfigelementwrapperbase.cxx,v $
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_framework.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include <helper/uiconfigelementwrapperbase.hxx>
39 #include <properties.h>
40 #include <threadhelp/resetableguard.hxx>
41 #include <uielement/constitemcontainer.hxx>
42 #include <uielement/rootitemcontainer.hxx>
44 //_________________________________________________________________________________________________________________
46 //_________________________________________________________________________________________________________________
47 #include <com/sun/star/beans/PropertyAttribute.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/beans/XPropertySet.hpp>
50 #include <com/sun/star/ui/XUIConfiguration.hpp>
51 #include <com/sun/star/lang/DisposedException.hpp>
53 //_________________________________________________________________________________________________________________
54 // includes of other projects
55 //_________________________________________________________________________________________________________________
56 #include <vcl/svapp.hxx>
57 #include <rtl/logfile.hxx>
59 const int UIELEMENT_PROPHANDLE_CONFIGSOURCE
= 1;
60 const int UIELEMENT_PROPHANDLE_FRAME
= 2;
61 const int UIELEMENT_PROPHANDLE_PERSISTENT
= 3;
62 const int UIELEMENT_PROPHANDLE_RESOURCEURL
= 4;
63 const int UIELEMENT_PROPHANDLE_TYPE
= 5;
64 const int UIELEMENT_PROPHANDLE_XMENUBAR
= 6;
65 const int UIELEMENT_PROPHANDLE_CONFIGLISTENER
= 7;
66 const int UIELEMENT_PROPHANDLE_NOCLOSE
= 8;
67 const int UIELEMENT_PROPCOUNT
= 8;
68 const rtl::OUString
UIELEMENT_PROPNAME_CONFIGLISTENER( RTL_CONSTASCII_USTRINGPARAM( "ConfigListener" ));
69 const rtl::OUString
UIELEMENT_PROPNAME_CONFIGSOURCE( RTL_CONSTASCII_USTRINGPARAM( "ConfigurationSource" ));
70 const rtl::OUString
UIELEMENT_PROPNAME_FRAME( RTL_CONSTASCII_USTRINGPARAM( "Frame" ));
71 const rtl::OUString
UIELEMENT_PROPNAME_PERSISTENT( RTL_CONSTASCII_USTRINGPARAM( "Persistent" ));
72 const rtl::OUString
UIELEMENT_PROPNAME_RESOURCEURL( RTL_CONSTASCII_USTRINGPARAM( "ResourceURL" ));
73 const rtl::OUString
UIELEMENT_PROPNAME_TYPE( RTL_CONSTASCII_USTRINGPARAM( "Type" ));
74 const rtl::OUString
UIELEMENT_PROPNAME_XMENUBAR( RTL_CONSTASCII_USTRINGPARAM( "XMenuBar" ));
75 const rtl::OUString
UIELEMENT_PROPNAME_NOCLOSE( RTL_CONSTASCII_USTRINGPARAM( "NoClose" ));
78 using namespace com::sun::star::beans
;
79 using namespace com::sun::star::uno
;
80 using namespace com::sun::star::frame
;
81 using namespace com::sun::star::lang
;
82 using namespace com::sun::star::container
;
83 using namespace ::com::sun::star::ui
;
88 //*****************************************************************************************************************
89 // XInterface, XTypeProvider
90 //*****************************************************************************************************************
91 DEFINE_XINTERFACE_10 ( UIConfigElementWrapperBase
,
93 DIRECT_INTERFACE( ::com::sun::star::lang::XTypeProvider
),
94 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElement
),
95 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElementSettings
),
96 DIRECT_INTERFACE( ::com::sun::star::beans::XMultiPropertySet
),
97 DIRECT_INTERFACE( ::com::sun::star::beans::XFastPropertySet
),
98 DIRECT_INTERFACE( ::com::sun::star::beans::XPropertySet
),
99 DIRECT_INTERFACE( ::com::sun::star::lang::XInitialization
),
100 DIRECT_INTERFACE( ::com::sun::star::lang::XComponent
),
101 DIRECT_INTERFACE( ::com::sun::star::util::XUpdatable
),
102 DIRECT_INTERFACE( ::com::sun::star::ui::XUIConfigurationListener
)
105 DEFINE_XTYPEPROVIDER_10 ( UIConfigElementWrapperBase
,
106 ::com::sun::star::lang::XTypeProvider
,
107 ::com::sun::star::ui::XUIElement
,
108 ::com::sun::star::ui::XUIElementSettings
,
109 ::com::sun::star::beans::XMultiPropertySet
,
110 ::com::sun::star::beans::XFastPropertySet
,
111 ::com::sun::star::beans::XPropertySet
,
112 ::com::sun::star::lang::XInitialization
,
113 ::com::sun::star::lang::XComponent
,
114 ::com::sun::star::util::XUpdatable
,
115 ::com::sun::star::ui::XUIConfigurationListener
118 UIConfigElementWrapperBase::UIConfigElementWrapperBase( sal_Int16 nType
,const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _xServiceFactory
)
119 : ThreadHelpBase ( &Application::GetSolarMutex() )
120 , ::cppu::OBroadcastHelperVar
< ::cppu::OMultiTypeInterfaceContainerHelper
, ::cppu::OMultiTypeInterfaceContainerHelper::keyType
>( m_aLock
.getShareableOslMutex() )
121 , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper
* >(this)) )
122 , ::cppu::OWeakObject ( )
124 , m_bPersistent ( sal_True
)
125 , m_bInitialized ( sal_False
)
126 , m_bConfigListener ( sal_False
)
127 , m_bConfigListening ( sal_False
)
128 , m_bDisposed ( sal_False
)
129 , m_bNoClose ( sal_False
)
130 , m_xServiceFactory ( _xServiceFactory
)
131 , m_aListenerContainer ( m_aLock
.getShareableOslMutex() )
135 UIConfigElementWrapperBase::~UIConfigElementWrapperBase()
140 void SAL_CALL
UIConfigElementWrapperBase::dispose() throw (::com::sun::star::uno::RuntimeException
)
142 // must be implemented by derived class
143 ResetableGuard
aLock( m_aLock
);
144 m_bDisposed
= sal_True
;
147 void SAL_CALL
UIConfigElementWrapperBase::addEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
)
149 m_aListenerContainer
.addInterface( ::getCppuType( ( const css::uno::Reference
< css::lang::XEventListener
>* ) NULL
), xListener
);
152 void SAL_CALL
UIConfigElementWrapperBase::removeEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& aListener
) throw (::com::sun::star::uno::RuntimeException
)
154 m_aListenerContainer
.removeInterface( ::getCppuType( ( const css::uno::Reference
< css::lang::XEventListener
>* ) NULL
), aListener
);
158 void SAL_CALL
UIConfigElementWrapperBase::disposing( const EventObject
& )
159 throw( RuntimeException
)
161 ResetableGuard
aLock( m_aLock
);
162 m_xConfigSource
.clear();
165 void SAL_CALL
UIConfigElementWrapperBase::initialize( const Sequence
< Any
>& aArguments
)
166 throw ( Exception
, RuntimeException
)
168 ResetableGuard
aLock( m_aLock
);
170 if ( !m_bInitialized
)
172 for ( sal_Int32 n
= 0; n
< aArguments
.getLength(); n
++ )
174 PropertyValue aPropValue
;
175 if ( aArguments
[n
] >>= aPropValue
)
177 if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_CONFIGSOURCE
))
178 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGSOURCE
, aPropValue
.Value
);
179 else if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_FRAME
))
180 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_FRAME
, aPropValue
.Value
);
181 else if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_PERSISTENT
))
182 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_PERSISTENT
, aPropValue
.Value
);
183 else if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_RESOURCEURL
))
184 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_RESOURCEURL
, aPropValue
.Value
);
185 else if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_TYPE
))
186 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_TYPE
, aPropValue
.Value
);
187 else if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_CONFIGLISTENER
))
188 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGLISTENER
, aPropValue
.Value
);
189 else if ( aPropValue
.Name
.equals( UIELEMENT_PROPNAME_NOCLOSE
))
190 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_NOCLOSE
, aPropValue
.Value
);
194 m_bInitialized
= sal_True
;
199 void SAL_CALL
UIConfigElementWrapperBase::update() throw (::com::sun::star::uno::RuntimeException
)
201 // can be implemented by derived class
204 void SAL_CALL
UIConfigElementWrapperBase::elementInserted( const ::com::sun::star::ui::ConfigurationEvent
& ) throw (::com::sun::star::uno::RuntimeException
)
206 // can be implemented by derived class
209 void SAL_CALL
UIConfigElementWrapperBase::elementRemoved( const ::com::sun::star::ui::ConfigurationEvent
& ) throw (::com::sun::star::uno::RuntimeException
)
211 // can be implemented by derived class
214 void SAL_CALL
UIConfigElementWrapperBase::elementReplaced( const ::com::sun::star::ui::ConfigurationEvent
& ) throw (::com::sun::star::uno::RuntimeException
)
216 // can be implemented by derived class
219 // XPropertySet helper
220 sal_Bool SAL_CALL
UIConfigElementWrapperBase::convertFastPropertyValue( Any
& aConvertedValue
,
223 const Any
& aValue
) throw( com::sun::star::lang::IllegalArgumentException
)
225 // Initialize state with FALSE !!!
226 // (Handle can be invalid)
227 sal_Bool bReturn
= sal_False
;
231 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
232 bReturn
= PropHelper::willPropertyBeChanged(
233 com::sun::star::uno::makeAny(m_bConfigListener
),
239 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
240 bReturn
= PropHelper::willPropertyBeChanged(
241 com::sun::star::uno::makeAny(m_xConfigSource
),
247 case UIELEMENT_PROPHANDLE_FRAME
:
249 Reference
< XFrame
> xFrame( m_xWeakFrame
);
250 bReturn
= PropHelper::willPropertyBeChanged(
251 com::sun::star::uno::makeAny(xFrame
),
258 case UIELEMENT_PROPHANDLE_PERSISTENT
:
259 bReturn
= PropHelper::willPropertyBeChanged(
260 com::sun::star::uno::makeAny(m_bPersistent
),
266 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
267 bReturn
= PropHelper::willPropertyBeChanged(
268 com::sun::star::uno::makeAny(m_aResourceURL
),
274 case UIELEMENT_PROPHANDLE_TYPE
:
275 bReturn
= PropHelper::willPropertyBeChanged(
276 com::sun::star::uno::makeAny(m_nType
),
282 case UIELEMENT_PROPHANDLE_XMENUBAR
:
283 bReturn
= PropHelper::willPropertyBeChanged(
284 com::sun::star::uno::makeAny(m_xMenuBar
),
290 case UIELEMENT_PROPHANDLE_NOCLOSE
:
291 bReturn
= PropHelper::willPropertyBeChanged(
292 com::sun::star::uno::makeAny(m_bNoClose
),
299 // Return state of operation.
303 void SAL_CALL
UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
,
304 const com::sun::star::uno::Any
& aValue
) throw( com::sun::star::uno::Exception
)
308 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
310 bool bBool( m_bConfigListener
);
312 if ( m_bConfigListener
!= bBool
)
314 if ( m_bConfigListening
)
316 if ( m_xConfigSource
.is() && !bBool
)
320 Reference
< XUIConfiguration
> xUIConfig( m_xConfigSource
, UNO_QUERY
);
321 if ( xUIConfig
.is() )
323 xUIConfig
->removeConfigurationListener( Reference
< XUIConfigurationListener
>( static_cast< OWeakObject
* >( this ), UNO_QUERY
));
324 m_bConfigListening
= sal_False
;
334 if ( m_xConfigSource
.is() && bBool
)
338 Reference
< XUIConfiguration
> xUIConfig( m_xConfigSource
, UNO_QUERY
);
339 if ( xUIConfig
.is() )
341 xUIConfig
->addConfigurationListener( Reference
< XUIConfigurationListener
>( static_cast< OWeakObject
* >( this ), UNO_QUERY
));
342 m_bConfigListening
= sal_True
;
351 m_bConfigListener
= bBool
;
355 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
356 aValue
>>= m_xConfigSource
;
358 case UIELEMENT_PROPHANDLE_FRAME
:
360 Reference
< XFrame
> xFrame
;
363 m_xWeakFrame
= xFrame
;
366 case UIELEMENT_PROPHANDLE_PERSISTENT
:
368 sal_Bool
bBool( m_bPersistent
);
370 m_bPersistent
= bBool
;
373 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
374 aValue
>>= m_aResourceURL
;
376 case UIELEMENT_PROPHANDLE_TYPE
:
379 case UIELEMENT_PROPHANDLE_XMENUBAR
:
380 aValue
>>= m_xMenuBar
;
382 case UIELEMENT_PROPHANDLE_NOCLOSE
:
384 sal_Bool
bBool( m_bNoClose
);
392 void SAL_CALL
UIConfigElementWrapperBase::getFastPropertyValue( com::sun::star::uno::Any
& aValue
,
393 sal_Int32 nHandle
) const
397 case UIELEMENT_PROPHANDLE_CONFIGLISTENER
:
398 aValue
<<= m_bConfigListener
;
400 case UIELEMENT_PROPHANDLE_CONFIGSOURCE
:
401 aValue
<<= m_xConfigSource
;
403 case UIELEMENT_PROPHANDLE_FRAME
:
405 Reference
< XFrame
> xFrame( m_xWeakFrame
);
409 case UIELEMENT_PROPHANDLE_PERSISTENT
:
410 aValue
<<= m_bPersistent
;
412 case UIELEMENT_PROPHANDLE_RESOURCEURL
:
413 aValue
<<= m_aResourceURL
;
415 case UIELEMENT_PROPHANDLE_TYPE
:
418 case UIELEMENT_PROPHANDLE_XMENUBAR
:
419 aValue
<<= m_xMenuBar
;
421 case UIELEMENT_PROPHANDLE_NOCLOSE
:
422 aValue
<<= m_bNoClose
;
427 ::cppu::IPropertyArrayHelper
& SAL_CALL
UIConfigElementWrapperBase::getInfoHelper()
429 // Optimize this method !
430 // We initialize a static variable only one time. And we don't must use a mutex at every call!
431 // For the first call; pInfoHelper is NULL - for the second call pInfoHelper is different from NULL!
432 static ::cppu::OPropertyArrayHelper
* pInfoHelper
= NULL
;
434 if( pInfoHelper
== NULL
)
436 // Ready for multithreading
437 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() ) ;
439 // Control this pointer again, another instance can be faster then these!
440 if( pInfoHelper
== NULL
)
442 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
443 // "impl_getStaticPropertyDescriptor" is a non exported and static funtion, who will define a static propertytable.
444 // "sal_True" say: Table is sorted by name.
445 static ::cppu::OPropertyArrayHelper
aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True
);
446 pInfoHelper
= &aInfoHelper
;
450 return(*pInfoHelper
);
453 com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySetInfo
> SAL_CALL
UIConfigElementWrapperBase::getPropertySetInfo() throw (::com::sun::star::uno::RuntimeException
)
455 // Optimize this method !
456 // We initialize a static variable only one time. And we don't must use a mutex at every call!
457 // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
458 static com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySetInfo
>* pInfo
= NULL
;
462 // Ready for multithreading
463 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() ) ;
464 // Control this pointer again, another instance can be faster then these!
467 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
468 // (Use method "getInfoHelper()".)
469 static com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
477 const com::sun::star::uno::Sequence
< com::sun::star::beans::Property
> UIConfigElementWrapperBase::impl_getStaticPropertyDescriptor()
479 // Create a new static property array to initialize sequence!
480 // Table of all predefined properties of this class. Its used from OPropertySetHelper-class!
481 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
482 // It's necessary for methods of OPropertySetHelper.
484 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
486 static const com::sun::star::beans::Property pProperties
[] =
488 com::sun::star::beans::Property( UIELEMENT_PROPNAME_CONFIGLISTENER
, UIELEMENT_PROPHANDLE_CONFIGLISTENER
, ::getCppuType((const sal_Bool
*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
489 com::sun::star::beans::Property( UIELEMENT_PROPNAME_CONFIGSOURCE
, UIELEMENT_PROPHANDLE_CONFIGSOURCE
, ::getCppuType((const Reference
< ::com::sun::star::ui::XUIConfigurationManager
>*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
490 com::sun::star::beans::Property( UIELEMENT_PROPNAME_FRAME
, UIELEMENT_PROPHANDLE_FRAME
, ::getCppuType((const Reference
< com::sun::star::frame::XFrame
>*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
),
491 com::sun::star::beans::Property( UIELEMENT_PROPNAME_NOCLOSE
, UIELEMENT_PROPHANDLE_NOCLOSE
, ::getCppuType((const sal_Bool
*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
492 com::sun::star::beans::Property( UIELEMENT_PROPNAME_PERSISTENT
, UIELEMENT_PROPHANDLE_PERSISTENT
, ::getCppuType((const sal_Bool
*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
),
493 com::sun::star::beans::Property( UIELEMENT_PROPNAME_RESOURCEURL
, UIELEMENT_PROPHANDLE_RESOURCEURL
, ::getCppuType((const ::rtl::OUString
*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
),
494 com::sun::star::beans::Property( UIELEMENT_PROPNAME_TYPE
, UIELEMENT_PROPHANDLE_TYPE
, ::getCppuType((const ::rtl::OUString
*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
),
495 com::sun::star::beans::Property( UIELEMENT_PROPNAME_XMENUBAR
, UIELEMENT_PROPHANDLE_XMENUBAR
, ::getCppuType((const Reference
< com::sun::star::awt::XMenuBar
>*)NULL
), com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
)
497 // Use it to initialize sequence!
498 static const com::sun::star::uno::Sequence
< com::sun::star::beans::Property
> lPropertyDescriptor( pProperties
, UIELEMENT_PROPCOUNT
);
499 // Return static "PropertyDescriptor"
500 return lPropertyDescriptor
;
502 void SAL_CALL
UIConfigElementWrapperBase::setSettings( const Reference
< XIndexAccess
>& xSettings
) throw ( RuntimeException
)
504 ResetableGuard
aLock( m_aLock
);
507 // throw DisposedException();
509 if ( xSettings
.is() )
511 // Create a copy of the data if the container is not const
512 Reference
< XIndexReplace
> xReplace( xSettings
, UNO_QUERY
);
514 m_xConfigData
= Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( new ConstItemContainer( xSettings
) ), UNO_QUERY
);
516 m_xConfigData
= xSettings
;
518 if ( m_xConfigSource
.is() && m_bPersistent
)
520 ::rtl::OUString
aResourceURL( m_aResourceURL
);
521 Reference
< XUIConfigurationManager
> xUICfgMgr( m_xConfigSource
);
527 xUICfgMgr
->replaceSettings( aResourceURL
, m_xConfigData
);
529 catch( NoSuchElementException
& )
533 else if ( !m_bPersistent
)
535 // Transient menubar => Fill menubar with new data
540 void UIConfigElementWrapperBase::impl_fillNewData()
543 Reference
< XIndexAccess
> SAL_CALL
UIConfigElementWrapperBase::getSettings( sal_Bool bWriteable
) throw ( RuntimeException
)
545 ResetableGuard
aLock( m_aLock
);
548 // throw DisposedException();
551 return Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( new RootItemContainer( m_xConfigData
) ), UNO_QUERY
);
553 return m_xConfigData
;