Update ooo320-m1
[ooovba.git] / extensions / source / propctrlr / propertyhandler.hxx
blob34bfe8d388f610456f1b4e4e5660b15aced18981
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propertyhandler.hxx,v $
10 * $Revision: 1.9 $
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 #ifndef EXTENSIONS_SOURCE_PROPCTRLR_PROPERTYHANDLER_HXX
32 #define EXTENSIONS_SOURCE_PROPCTRLR_PROPERTYHANDLER_HXX
34 #include "pcrcomponentcontext.hxx"
35 #include "pcrcommon.hxx"
36 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPCR_HXX_
37 #include "modulepcr.hxx"
38 #endif
40 /** === begin UNO includes === **/
41 #include <com/sun/star/uno/XComponentContext.hpp>
42 #include <com/sun/star/beans/PropertyState.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <com/sun/star/beans/Property.hpp>
45 #include <com/sun/star/script/XTypeConverter.hpp>
46 #include <com/sun/star/frame/XModel.hpp>
47 #include <com/sun/star/uno/Sequence.hxx>
48 #include <com/sun/star/uno/Any.hxx>
49 #include <com/sun/star/util/Date.hpp>
50 #include <com/sun/star/util/Time.hpp>
51 #include <com/sun/star/util/DateTime.hpp>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/inspection/XPropertyHandler.hpp>
54 #include <com/sun/star/lang/XServiceInfo.hpp>
55 /** === end UNO includes === **/
56 #include <osl/interlck.h>
57 #include <cppuhelper/compbase1.hxx>
58 #include <cppuhelper/implbase1.hxx>
59 #include <comphelper/uno3.hxx>
61 #include <memory>
62 #include <vector>
64 namespace com { namespace sun { namespace star {
65 namespace inspection {
66 struct LineDescriptor;
67 class XPropertyControlFactory;
69 } } }
71 class Window;
72 //........................................................................
73 namespace pcr
75 //........................................................................
77 typedef sal_Int32 PropertyId;
79 //====================================================================
80 //= PropertyHandler
81 //====================================================================
82 class OPropertyInfoService;
83 typedef ::cppu::WeakComponentImplHelper1 < ::com::sun::star::inspection::XPropertyHandler
84 > PropertyHandler_Base;
85 /** the base class for property handlers
87 class PropertyHandler : public PropertyHandler_Base
89 private:
90 /// cache for getSupportedProperties
91 mutable StlSyntaxSequence< ::com::sun::star::beans::Property >
92 m_aSupportedProperties;
93 mutable bool m_bSupportedPropertiesAreKnown;
95 /// helper which ensures that we can access resources as long as the instance lives
96 PcrClient m_aEnsureResAccess;
98 private:
99 /// the property listener which has been registered
100 PropertyChangeListeners m_aPropertyListeners;
102 protected:
103 mutable ::osl::Mutex m_aMutex;
104 /// the context in which the instance was created
105 ComponentContext m_aContext;
106 /// the component we're inspecting
107 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > m_xComponent;
108 /// info about our component's properties
109 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > m_xComponentPropertyInfo;
110 /// type converter, needed on various occasions
111 ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter > m_xTypeConverter;
112 /// access to property meta data
113 ::std::auto_ptr< OPropertyInfoService > m_pInfoService;
115 protected:
116 PropertyHandler(
117 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext
119 ~PropertyHandler();
121 // default implementations for XPropertyHandler
122 virtual void SAL_CALL inspect( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxIntrospectee ) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::NullPointerException);
123 virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > SAL_CALL getSupportedProperties() throw (::com::sun::star::uno::RuntimeException);
124 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupersededProperties( ) throw (::com::sun::star::uno::RuntimeException);
125 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getActuatingProperties( ) throw (::com::sun::star::uno::RuntimeException);
126 virtual ::com::sun::star::uno::Any SAL_CALL convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Any& _rControlValue ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
127 virtual ::com::sun::star::uno::Any SAL_CALL convertToControlValue( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Any& _rPropertyValue, const ::com::sun::star::uno::Type& _rControlValueType ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
128 virtual ::com::sun::star::beans::PropertyState SAL_CALL getPropertyState( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
129 virtual ::com::sun::star::inspection::LineDescriptor SAL_CALL describePropertyLine( const ::rtl::OUString& _rPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlFactory >& _rxControlFactory ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
130 virtual ::sal_Bool SAL_CALL isComposable( const ::rtl::OUString& _rPropertyName ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException);
131 virtual ::com::sun::star::inspection::InteractiveSelectionResult SAL_CALL onInteractivePropertySelection( const ::rtl::OUString& _rPropertyName, sal_Bool _bPrimary, ::com::sun::star::uno::Any& _rData, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI >& _rxInspectorUI ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
132 virtual void SAL_CALL actuatingPropertyChanged( const ::rtl::OUString& _rActuatingPropertyName, const ::com::sun::star::uno::Any& _rNewValue, const ::com::sun::star::uno::Any& _rOldValue, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit ) throw (::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
133 virtual void SAL_CALL addPropertyChangeListener( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& _rxListener ) throw (::com::sun::star::uno::RuntimeException);
134 virtual void SAL_CALL removePropertyChangeListener( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& _rxListener ) throw (::com::sun::star::uno::RuntimeException);
135 virtual sal_Bool SAL_CALL suspend( sal_Bool _bSuspend ) throw (::com::sun::star::uno::RuntimeException);
137 // XComponent
138 DECLARE_XCOMPONENT()
139 virtual void SAL_CALL disposing();
141 // own overridables
142 virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >
143 SAL_CALL doDescribeSupportedProperties() const = 0;
145 /// called when XPropertyHandler::inspect has been called, and we thus have a new component to inspect
146 virtual void onNewComponent();
148 protected:
149 /** fires the change in a property value to our listener (if any)
150 @see addPropertyChangeListener
152 void firePropertyChange( const ::rtl::OUString& _rPropName, PropertyId _nPropId,
153 const ::com::sun::star::uno::Any& _rOldValue, const ::com::sun::star::uno::Any& _rNewValue ) SAL_THROW(());
155 /** retrieves a window which can be used as parent for dialogs
157 Window* impl_getDefaultDialogParent_nothrow() const;
159 /** retrieves the property id for a given property name
160 @throw com::sun::star::beans::UnknownPropertyException
161 if the property name is not known to our ->m_pInfoService
163 PropertyId impl_getPropertyId_throw( const ::rtl::OUString& _rPropertyName ) const;
165 //-------------------------------------------------------------------------------
166 // helper for implementing doDescribeSupportedProperties
167 /** adds a description for the given string property to the given property vector
168 Most probably to be called from within getSupportedProperties
170 inline void addStringPropertyDescription(
171 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
172 const ::rtl::OUString& _rPropertyName,
173 sal_Int16 _nAttribs = 0
174 ) const;
176 /** adds a description for the given int32 property to the given property vector
178 inline void addInt32PropertyDescription(
179 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
180 const ::rtl::OUString& _rPropertyName,
181 sal_Int16 _nAttribs = 0
182 ) const;
184 /** adds a description for the given int16 property to the given property vector
186 inline void addInt16PropertyDescription(
187 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
188 const ::rtl::OUString& _rPropertyName,
189 sal_Int16 _nAttribs = 0
190 ) const;
192 /** adds a description for the given double property to the given property vector
194 inline void addDoublePropertyDescription(
195 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
196 const ::rtl::OUString& _rPropertyName,
197 sal_Int16 _nAttribs = 0
198 ) const;
200 /** adds a description for the given date property to the given property vector
202 inline void addDatePropertyDescription(
203 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
204 const ::rtl::OUString& _rPropertyName,
205 sal_Int16 _nAttribs = 0
206 ) const;
208 /** adds a description for the given time property to the given property vector
210 inline void addTimePropertyDescription(
211 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
212 const ::rtl::OUString& _rPropertyName,
213 sal_Int16 _nAttribs = 0
214 ) const;
216 /** adds a description for the given DateTime property to the given property vector
218 inline void addDateTimePropertyDescription(
219 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
220 const ::rtl::OUString& _rPropertyName,
221 sal_Int16 _nAttribs = 0
222 ) const;
224 /// adds a Property, given by name only, to a given vector of Properties
225 void implAddPropertyDescription(
226 ::std::vector< ::com::sun::star::beans::Property >& _rProperties,
227 const ::rtl::OUString& _rPropertyName,
228 const ::com::sun::star::uno::Type& _rType,
229 sal_Int16 _nAttribs = 0
230 ) const;
232 //-------------------------------------------------------------------------------
233 // helper for accessing and maintaining meta data about our supported properties
235 /** retrieves a property given by handle
237 @return
238 a pointer to the descriptor for the given properties, if it is one of our
239 supported properties, <NULL/> else.
241 @see doDescribeSupportedProperties
242 @see impl_getPropertyFromId_throw
244 const ::com::sun::star::beans::Property*
245 impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const;
247 /** retrieves a property given by handle
249 @throws UnknownPropertyException
250 if the handler does not support a property with the given handle
252 @seealso doDescribeSupportedProperties
253 @see impl_getPropertyFromId_nothrow
255 const ::com::sun::star::beans::Property&
256 impl_getPropertyFromId_throw( PropertyId _nPropId ) const;
258 /** determines whether a given property id is part of our supported properties
259 @see getSupportedProperties
260 @see doDescribeSupportedProperties
262 inline bool impl_isSupportedProperty_nothrow( PropertyId _nPropId ) const
264 return impl_getPropertyFromId_nothrow( _nPropId ) != NULL;
267 /** retrieves a property given by name
269 @throws UnknownPropertyException
270 if the handler does not support a property with the given name
272 @seealso doDescribeSupportedProperties
274 const ::com::sun::star::beans::Property&
275 impl_getPropertyFromName_throw( const ::rtl::OUString& _rPropertyName ) const;
277 /** get the name of a property given by handle
279 inline ::rtl::OUString
280 impl_getPropertyNameFromId_nothrow( PropertyId _nPropId ) const;
282 /** returns the value of the ContextDocument property in the ComponentContext which was used to create
283 this handler.
285 inline ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >
286 impl_getContextDocument_nothrow() const
288 return ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >(
289 m_aContext.getContextValueByAsciiName( "ContextDocument" ), ::com::sun::star::uno::UNO_QUERY );
292 /** marks the context document as modified
294 @see impl_getContextDocument_nothrow
296 void impl_setContextDocumentModified_nothrow() const;
298 /// determines whether our component has a given property
299 bool impl_componentHasProperty_throw( const ::rtl::OUString& _rPropName ) const;
301 /** determines the default measure unit for the document in which our component lives
303 sal_Int16 impl_getDocumentMeasurementUnit_throw() const;
305 private:
306 PropertyHandler(); // never implemented
307 PropertyHandler( const PropertyHandler& ); // never implemented
308 PropertyHandler& operator=( const PropertyHandler& ); // never implemented
311 //--------------------------------------------------------------------
312 inline void PropertyHandler::addStringPropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
314 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) ), _nAttribs );
317 inline void PropertyHandler::addInt32PropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
319 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< sal_Int32* >( NULL ) ), _nAttribs );
322 inline void PropertyHandler::addInt16PropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
324 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< sal_Int16* >( NULL ) ), _nAttribs );
327 inline void PropertyHandler::addDoublePropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
329 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< double* >( NULL ) ), _nAttribs );
332 inline void PropertyHandler::addDatePropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
334 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< com::sun::star::util::Date* >( NULL ) ), _nAttribs );
337 inline void PropertyHandler::addTimePropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
339 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< com::sun::star::util::Time* >( NULL ) ), _nAttribs );
342 inline void PropertyHandler::addDateTimePropertyDescription( ::std::vector< ::com::sun::star::beans::Property >& _rProperties, const ::rtl::OUString& _rPropertyName, sal_Int16 _nAttribs ) const
344 implAddPropertyDescription( _rProperties, _rPropertyName, ::getCppuType( static_cast< com::sun::star::util::DateTime* >( NULL ) ), _nAttribs );
347 inline ::rtl::OUString PropertyHandler::impl_getPropertyNameFromId_nothrow( PropertyId _nPropId ) const
349 const ::com::sun::star::beans::Property* pProp = impl_getPropertyFromId_nothrow( _nPropId );
350 return pProp ? pProp->Name : ::rtl::OUString();
353 //====================================================================
354 //= PropertyHandlerComponent
355 //====================================================================
356 typedef ::cppu::ImplHelper1 < ::com::sun::star::lang::XServiceInfo
357 > PropertyHandlerComponent_Base;
358 /** PropertyHandler implementation which additionally supports XServiceInfo
360 class PropertyHandlerComponent :public PropertyHandler
361 ,public PropertyHandlerComponent_Base
363 protected:
364 PropertyHandlerComponent(
365 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext
368 DECLARE_XINTERFACE()
369 DECLARE_XTYPEPROVIDER()
371 // XServiceInfo
372 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (::com::sun::star::uno::RuntimeException) = 0;
373 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
374 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException) = 0;
377 //====================================================================
378 //= HandlerComponentBase
379 //====================================================================
380 /** a PropertyHandlerComponent implementation which routes XServiceInfo::getImplementationName and
381 XServiceInfo::getSupportedServiceNames to static versions of those methods, which are part of
382 the derived class.
384 Additionally, a method <member>Create</member> is provided which takes a component context, and returns a new
385 instance of the derived class. This <member>Create</member> is used to register the implementation
386 of the derived class at the <type>PcrModule</type>.
388 Well, every time we're talking about derived class, we in fact mean the template argument of
389 <type>HandlerComponentBase</type>. But usually this equals your derived class:
390 <pre>
391 class MyHandler;
392 typedef HandlerComponentBase< MyHandler > MyHandler_Base;
393 class MyHandler : MyHandler_Base
396 public:
397 static ::rtl::OUString SAL_CALL getImplementationName_static( ) throw (::com::sun::star::uno::RuntimeException);
398 static ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static( ) throw (::com::sun::star::uno::RuntimeException);
400 </pre>
402 template < class HANDLER >
403 class HandlerComponentBase : public PropertyHandlerComponent
405 protected:
406 HandlerComponentBase( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext )
407 :PropertyHandlerComponent( _rxContext )
411 protected:
412 // XServiceInfo
413 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (::com::sun::star::uno::RuntimeException);
414 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException);
415 static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL Create( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext );
417 public:
418 /** registers the implementation of HANDLER at the <type>PcrModule</type>
420 static void registerImplementation();
423 //--------------------------------------------------------------------
424 template < class HANDLER >
425 ::rtl::OUString SAL_CALL HandlerComponentBase< HANDLER >::getImplementationName( ) throw (::com::sun::star::uno::RuntimeException)
427 return HANDLER::getImplementationName_static();
430 //--------------------------------------------------------------------
431 template < class HANDLER >
432 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL HandlerComponentBase< HANDLER >::getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException)
434 return HANDLER::getSupportedServiceNames_static();
437 //--------------------------------------------------------------------
438 template < class HANDLER >
439 void HandlerComponentBase< HANDLER >::registerImplementation()
441 PcrModule::getInstance().registerImplementation(
442 HANDLER::getImplementationName_static(),
443 HANDLER::getSupportedServiceNames_static(),
444 HANDLER::Create
448 //--------------------------------------------------------------------
449 template < class HANDLER >
450 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL HandlerComponentBase< HANDLER >::Create( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext )
452 return *( new HANDLER( _rxContext ) );
455 //........................................................................
456 } // namespace pcr
457 //........................................................................
459 #endif // EXTENSIONS_SOURCE_PROPCTRLR_PROPERTYHANDLER_HXX