Update ooo320-m1
[ooovba.git] / extensions / source / propctrlr / handlerhelper.cxx
blobbff9d5e80c87a6e96ad04c8ad5e0ff9dae70d266
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: handlerhelper.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "handlerhelper.hxx"
34 #ifndef EXTENSIONS_PROPRESID_HRC
35 #include "propresid.hrc"
36 #endif
37 #include "formresid.hrc"
38 #include <comphelper/extract.hxx>
39 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_
40 #include "modulepcr.hxx"
41 #endif
42 #include "enumrepresentation.hxx"
43 #include "formmetadata.hxx"
44 #include "pcrcomponentcontext.hxx"
46 /** === begin UNO includes === **/
47 #include "com/sun/star/inspection/StringRepresentation.hpp"
48 #include <com/sun/star/beans/PropertyAttribute.hpp>
49 #include <com/sun/star/uno/XComponentContext.hpp>
50 #include <com/sun/star/util/XModifiable.hpp>
51 #include <com/sun/star/awt/XWindow.hpp>
52 #include <com/sun/star/inspection/LineDescriptor.hpp>
53 #include <com/sun/star/inspection/PropertyControlType.hpp>
54 #include <com/sun/star/inspection/XStringListControl.hpp>
55 #include <com/sun/star/inspection/XNumericControl.hpp>
56 /** === end UNO includes === **/
57 #include <tools/debug.hxx>
58 #include <tools/diagnose_ex.h>
59 #include <tools/StringListResource.hxx>
60 #include <toolkit/helper/vclunohelper.hxx>
62 #include <algorithm>
64 //........................................................................
65 namespace pcr
67 //........................................................................
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::lang;
71 using namespace ::com::sun::star::awt;
72 using namespace ::com::sun::star::util;
73 using namespace ::com::sun::star::beans;
74 using namespace ::com::sun::star::script;
75 using namespace ::com::sun::star::inspection;
77 //====================================================================
78 //= PropertyHandlerHelper
79 //====================================================================
80 //--------------------------------------------------------------------
81 void PropertyHandlerHelper::describePropertyLine( const Property& _rProperty,
82 LineDescriptor& /* [out] */ _out_rDescriptor, const Reference< XPropertyControlFactory >& _rxControlFactory )
84 // display the pure property name - no L10N
85 _out_rDescriptor.DisplayName = _rProperty.Name;
87 OSL_PRECOND( _rxControlFactory.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" );
88 if ( !_rxControlFactory.is() )
89 return;
91 sal_Bool bReadOnlyControl = requiresReadOnlyControl( _rProperty.Attributes );
93 // special handling for booleans (this will become a list)
94 if ( _rProperty.Type.getTypeClass() == TypeClass_BOOLEAN )
96 ::std::vector< ::rtl::OUString > aListEntries;
97 tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries);
98 _out_rDescriptor.Control = createListBoxControl( _rxControlFactory, aListEntries, bReadOnlyControl, sal_False );
99 return;
102 sal_Int16 nControlType = PropertyControlType::TextField;
103 switch ( _rProperty.Type.getTypeClass() )
105 case TypeClass_BYTE:
106 case TypeClass_SHORT:
107 case TypeClass_UNSIGNED_SHORT:
108 case TypeClass_LONG:
109 case TypeClass_UNSIGNED_LONG:
110 case TypeClass_HYPER:
111 case TypeClass_UNSIGNED_HYPER:
112 case TypeClass_FLOAT:
113 case TypeClass_DOUBLE:
114 nControlType = PropertyControlType::NumericField;
115 break;
117 case TypeClass_SEQUENCE:
118 nControlType = PropertyControlType::StringListField;
119 break;
121 default:
122 DBG_ERROR( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
123 // NO break!
125 case TypeClass_STRING:
126 nControlType = PropertyControlType::TextField;
127 break;
130 // create a control
131 _out_rDescriptor.Control = _rxControlFactory->createPropertyControl( nControlType, bReadOnlyControl );
134 //--------------------------------------------------------------------
135 namespace
137 Reference< XPropertyControl > lcl_implCreateListLikeControl(
138 const Reference< XPropertyControlFactory >& _rxControlFactory,
139 const ::std::vector< ::rtl::OUString >& _rInitialListEntries,
140 sal_Bool _bReadOnlyControl,
141 sal_Bool _bSorted,
142 sal_Bool _bTrueIfListBoxFalseIfComboBox
145 Reference< XStringListControl > xListControl(
146 _rxControlFactory->createPropertyControl(
147 _bTrueIfListBoxFalseIfComboBox ? PropertyControlType::ListBox : PropertyControlType::ComboBox, _bReadOnlyControl
149 UNO_QUERY_THROW
152 ::std::vector< ::rtl::OUString > aInitialEntries( _rInitialListEntries );
153 if ( _bSorted )
154 ::std::sort( aInitialEntries.begin(), aInitialEntries.end() );
156 for ( ::std::vector< ::rtl::OUString >::const_iterator loop = aInitialEntries.begin();
157 loop != aInitialEntries.end();
158 ++loop
160 xListControl->appendListEntry( *loop );
161 return xListControl.get();
165 //--------------------------------------------------------------------
166 Reference< XPropertyControl > PropertyHandlerHelper::createListBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
167 const Sequence< ::rtl::OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
169 ::std::vector< ::rtl::OUString > aAsVector( _rInitialListEntries.getConstArray(), _rInitialListEntries.getConstArray() + _rInitialListEntries.getLength() );
170 return lcl_implCreateListLikeControl( _rxControlFactory, aAsVector, _bReadOnlyControl, _bSorted, sal_True );
173 //--------------------------------------------------------------------
174 Reference< XPropertyControl > PropertyHandlerHelper::createListBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
175 const ::std::vector< ::rtl::OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
177 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_True );
180 //--------------------------------------------------------------------
181 Reference< XPropertyControl > PropertyHandlerHelper::createComboBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
182 const ::std::vector< ::rtl::OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
184 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_False );
187 //--------------------------------------------------------------------
188 Reference< XPropertyControl > PropertyHandlerHelper::createNumericControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
189 sal_Int16 _nDigits, const Optional< double >& _rMinValue, const Optional< double >& _rMaxValue, sal_Bool _bReadOnlyControl )
191 Reference< XNumericControl > xNumericControl(
192 _rxControlFactory->createPropertyControl( PropertyControlType::NumericField, _bReadOnlyControl ),
193 UNO_QUERY_THROW
196 xNumericControl->setDecimalDigits( _nDigits );
197 xNumericControl->setMinValue( _rMinValue );
198 xNumericControl->setMaxValue( _rMaxValue );
200 return xNumericControl.get();
203 //--------------------------------------------------------------------
204 Any PropertyHandlerHelper::convertToPropertyValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
205 const Property& _rProperty, const Any& _rControlValue )
207 Any aPropertyValue( _rControlValue );
208 if ( !aPropertyValue.hasValue() )
209 // NULL is converted to NULL
210 return aPropertyValue;
212 if ( aPropertyValue.getValueType().equals( _rProperty.Type ) )
213 // nothing to do, type is already as desired
214 return aPropertyValue;
216 if ( _rControlValue.getValueType().getTypeClass() == TypeClass_STRING )
218 ::rtl::OUString sControlValue;
219 _rControlValue >>= sControlValue;
221 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
222 aPropertyValue = xConversionHelper->convertToPropertyValue( sControlValue, _rProperty.Type );
224 else
228 if ( _rxTypeConverter.is() )
229 aPropertyValue = _rxTypeConverter->convertTo( _rControlValue, _rProperty.Type );
231 catch( const Exception& )
233 OSL_ENSURE( sal_False, "PropertyHandlerHelper::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
237 return aPropertyValue;
240 //--------------------------------------------------------------------
241 Any PropertyHandlerHelper::convertToControlValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
242 const Any& _rPropertyValue, const Type& _rControlValueType )
244 Any aControlValue( _rPropertyValue );
245 if ( !aControlValue.hasValue() )
246 // NULL is converted to NULL
247 return aControlValue;
249 if ( _rControlValueType.getTypeClass() == TypeClass_STRING )
251 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
252 aControlValue <<= xConversionHelper->convertToControlValue( _rPropertyValue );
254 else
258 if ( _rxTypeConverter.is() )
259 aControlValue = _rxTypeConverter->convertTo( _rPropertyValue, _rControlValueType );
261 catch( const Exception& )
263 OSL_ENSURE( sal_False, "PropertyHandlerHelper::convertToControlValue: caught an exception while converting via TypeConverter!" );
267 return aControlValue;
270 //--------------------------------------------------------------------
271 void PropertyHandlerHelper::setContextDocumentModified( const ComponentContext& _rContext )
275 Reference< XModifiable > xDocumentModifiable( _rContext.getContextValueByAsciiName( "ContextDocument" ), UNO_QUERY_THROW );
276 xDocumentModifiable->setModified( sal_True );
278 catch( const Exception& )
280 DBG_UNHANDLED_EXCEPTION();
284 //--------------------------------------------------------------------
285 Window* PropertyHandlerHelper::getDialogParentWindow( const ComponentContext& _rContext )
287 Window* pInspectorWindow = NULL;
290 Reference< XWindow > xInspectorWindow( _rContext.getContextValueByAsciiName( "DialogParentWindow" ), UNO_QUERY_THROW );
291 pInspectorWindow = VCLUnoHelper::GetWindow( xInspectorWindow );
293 catch( const Exception& )
295 DBG_UNHANDLED_EXCEPTION();
297 return pInspectorWindow;
300 //........................................................................
301 } // namespace pcr
302 //........................................................................