1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_extensions.hxx" 26 #include "handlerhelper.hxx" 27 #ifndef EXTENSIONS_PROPRESID_HRC 28 #include "propresid.hrc" 29 #endif 30 #include "formresid.hrc" 31 #include <comphelper/extract.hxx> 32 #ifndef _EXTENSIONS_PROPCTRLR_MODULEPRC_HXX_ 33 #include "modulepcr.hxx" 34 #endif 35 #include "enumrepresentation.hxx" 36 #include "formmetadata.hxx" 37 #include "pcrcomponentcontext.hxx" 38 39 /** === begin UNO includes === **/ 40 #include "com/sun/star/inspection/StringRepresentation.hpp" 41 #include <com/sun/star/beans/PropertyAttribute.hpp> 42 #include <com/sun/star/uno/XComponentContext.hpp> 43 #include <com/sun/star/util/XModifiable.hpp> 44 #include <com/sun/star/awt/XWindow.hpp> 45 #include <com/sun/star/inspection/LineDescriptor.hpp> 46 #include <com/sun/star/inspection/PropertyControlType.hpp> 47 #include <com/sun/star/inspection/XStringListControl.hpp> 48 #include <com/sun/star/inspection/XNumericControl.hpp> 49 /** === end UNO includes === **/ 50 #include <tools/debug.hxx> 51 #include <tools/diagnose_ex.h> 52 #include <tools/StringListResource.hxx> 53 #include <toolkit/helper/vclunohelper.hxx> 54 55 #include <algorithm> 56 57 //........................................................................ 58 namespace pcr 59 { 60 //........................................................................ 61 62 using namespace ::com::sun::star::uno; 63 using namespace ::com::sun::star::lang; 64 using namespace ::com::sun::star::awt; 65 using namespace ::com::sun::star::util; 66 using namespace ::com::sun::star::beans; 67 using namespace ::com::sun::star::script; 68 using namespace ::com::sun::star::inspection; 69 70 //==================================================================== 71 //= PropertyHandlerHelper 72 //==================================================================== 73 //-------------------------------------------------------------------- describePropertyLine(const Property & _rProperty,LineDescriptor & _out_rDescriptor,const Reference<XPropertyControlFactory> & _rxControlFactory)74 void PropertyHandlerHelper::describePropertyLine( const Property& _rProperty, 75 LineDescriptor& /* [out] */ _out_rDescriptor, const Reference< XPropertyControlFactory >& _rxControlFactory ) 76 { 77 // display the pure property name - no L10N 78 _out_rDescriptor.DisplayName = _rProperty.Name; 79 80 OSL_PRECOND( _rxControlFactory.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" ); 81 if ( !_rxControlFactory.is() ) 82 return; 83 84 sal_Bool bReadOnlyControl = requiresReadOnlyControl( _rProperty.Attributes ); 85 86 // special handling for booleans (this will become a list) 87 if ( _rProperty.Type.getTypeClass() == TypeClass_BOOLEAN ) 88 { 89 ::std::vector< ::rtl::OUString > aListEntries; 90 tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries); 91 _out_rDescriptor.Control = createListBoxControl( _rxControlFactory, aListEntries, bReadOnlyControl, sal_False ); 92 return; 93 } 94 95 sal_Int16 nControlType = PropertyControlType::TextField; 96 switch ( _rProperty.Type.getTypeClass() ) 97 { 98 case TypeClass_BYTE: 99 case TypeClass_SHORT: 100 case TypeClass_UNSIGNED_SHORT: 101 case TypeClass_LONG: 102 case TypeClass_UNSIGNED_LONG: 103 case TypeClass_HYPER: 104 case TypeClass_UNSIGNED_HYPER: 105 case TypeClass_FLOAT: 106 case TypeClass_DOUBLE: 107 nControlType = PropertyControlType::NumericField; 108 break; 109 110 case TypeClass_SEQUENCE: 111 nControlType = PropertyControlType::StringListField; 112 break; 113 114 default: 115 DBG_ERROR( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" ); 116 // NO break! 117 118 case TypeClass_STRING: 119 nControlType = PropertyControlType::TextField; 120 break; 121 } 122 123 // create a control 124 _out_rDescriptor.Control = _rxControlFactory->createPropertyControl( nControlType, bReadOnlyControl ); 125 } 126 127 //-------------------------------------------------------------------- 128 namespace 129 { lcl_implCreateListLikeControl(const Reference<XPropertyControlFactory> & _rxControlFactory,const::std::vector<::rtl::OUString> & _rInitialListEntries,sal_Bool _bReadOnlyControl,sal_Bool _bSorted,sal_Bool _bTrueIfListBoxFalseIfComboBox)130 Reference< XPropertyControl > lcl_implCreateListLikeControl( 131 const Reference< XPropertyControlFactory >& _rxControlFactory, 132 const ::std::vector< ::rtl::OUString >& _rInitialListEntries, 133 sal_Bool _bReadOnlyControl, 134 sal_Bool _bSorted, 135 sal_Bool _bTrueIfListBoxFalseIfComboBox 136 ) 137 { 138 Reference< XStringListControl > xListControl( 139 _rxControlFactory->createPropertyControl( 140 _bTrueIfListBoxFalseIfComboBox ? PropertyControlType::ListBox : PropertyControlType::ComboBox, _bReadOnlyControl 141 ), 142 UNO_QUERY_THROW 143 ); 144 145 ::std::vector< ::rtl::OUString > aInitialEntries( _rInitialListEntries ); 146 if ( _bSorted ) 147 ::std::sort( aInitialEntries.begin(), aInitialEntries.end() ); 148 149 for ( ::std::vector< ::rtl::OUString >::const_iterator loop = aInitialEntries.begin(); 150 loop != aInitialEntries.end(); 151 ++loop 152 ) 153 xListControl->appendListEntry( *loop ); 154 return xListControl.get(); 155 } 156 } 157 158 //-------------------------------------------------------------------- createListBoxControl(const Reference<XPropertyControlFactory> & _rxControlFactory,const::std::vector<::rtl::OUString> & _rInitialListEntries,sal_Bool _bReadOnlyControl,sal_Bool _bSorted)159 Reference< XPropertyControl > PropertyHandlerHelper::createListBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory, 160 const ::std::vector< ::rtl::OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted ) 161 { 162 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_True ); 163 } 164 165 //-------------------------------------------------------------------- createComboBoxControl(const Reference<XPropertyControlFactory> & _rxControlFactory,const::std::vector<::rtl::OUString> & _rInitialListEntries,sal_Bool _bReadOnlyControl,sal_Bool _bSorted)166 Reference< XPropertyControl > PropertyHandlerHelper::createComboBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory, 167 const ::std::vector< ::rtl::OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted ) 168 { 169 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_False ); 170 } 171 172 //-------------------------------------------------------------------- createNumericControl(const Reference<XPropertyControlFactory> & _rxControlFactory,sal_Int16 _nDigits,const Optional<double> & _rMinValue,const Optional<double> & _rMaxValue,sal_Bool _bReadOnlyControl)173 Reference< XPropertyControl > PropertyHandlerHelper::createNumericControl( const Reference< XPropertyControlFactory >& _rxControlFactory, 174 sal_Int16 _nDigits, const Optional< double >& _rMinValue, const Optional< double >& _rMaxValue, sal_Bool _bReadOnlyControl ) 175 { 176 Reference< XNumericControl > xNumericControl( 177 _rxControlFactory->createPropertyControl( PropertyControlType::NumericField, _bReadOnlyControl ), 178 UNO_QUERY_THROW 179 ); 180 181 xNumericControl->setDecimalDigits( _nDigits ); 182 xNumericControl->setMinValue( _rMinValue ); 183 xNumericControl->setMaxValue( _rMaxValue ); 184 185 return xNumericControl.get(); 186 } 187 188 //-------------------------------------------------------------------- convertToPropertyValue(const Reference<XComponentContext> & _rxContext,const Reference<XTypeConverter> & _rxTypeConverter,const Property & _rProperty,const Any & _rControlValue)189 Any PropertyHandlerHelper::convertToPropertyValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter, 190 const Property& _rProperty, const Any& _rControlValue ) 191 { 192 Any aPropertyValue( _rControlValue ); 193 if ( !aPropertyValue.hasValue() ) 194 // NULL is converted to NULL 195 return aPropertyValue; 196 197 if ( aPropertyValue.getValueType().equals( _rProperty.Type ) ) 198 // nothing to do, type is already as desired 199 return aPropertyValue; 200 201 if ( _rControlValue.getValueType().getTypeClass() == TypeClass_STRING ) 202 { 203 ::rtl::OUString sControlValue; 204 _rControlValue >>= sControlValue; 205 206 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter ); 207 aPropertyValue = xConversionHelper->convertToPropertyValue( sControlValue, _rProperty.Type ); 208 } 209 else 210 { 211 try 212 { 213 if ( _rxTypeConverter.is() ) 214 aPropertyValue = _rxTypeConverter->convertTo( _rControlValue, _rProperty.Type ); 215 } 216 catch( const Exception& ) 217 { 218 OSL_ENSURE( sal_False, "PropertyHandlerHelper::convertToPropertyValue: caught an exception while converting via TypeConverter!" ); 219 } 220 } 221 222 return aPropertyValue; 223 } 224 225 //-------------------------------------------------------------------- convertToControlValue(const Reference<XComponentContext> & _rxContext,const Reference<XTypeConverter> & _rxTypeConverter,const Any & _rPropertyValue,const Type & _rControlValueType)226 Any PropertyHandlerHelper::convertToControlValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter, 227 const Any& _rPropertyValue, const Type& _rControlValueType ) 228 { 229 Any aControlValue( _rPropertyValue ); 230 if ( !aControlValue.hasValue() ) 231 // NULL is converted to NULL 232 return aControlValue; 233 234 if ( _rControlValueType.getTypeClass() == TypeClass_STRING ) 235 { 236 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter ); 237 aControlValue <<= xConversionHelper->convertToControlValue( _rPropertyValue ); 238 } 239 else 240 { 241 try 242 { 243 if ( _rxTypeConverter.is() ) 244 aControlValue = _rxTypeConverter->convertTo( _rPropertyValue, _rControlValueType ); 245 } 246 catch( const Exception& ) 247 { 248 OSL_ENSURE( sal_False, "PropertyHandlerHelper::convertToControlValue: caught an exception while converting via TypeConverter!" ); 249 } 250 } 251 252 return aControlValue; 253 } 254 255 //-------------------------------------------------------------------- setContextDocumentModified(const ComponentContext & _rContext)256 void PropertyHandlerHelper::setContextDocumentModified( const ComponentContext& _rContext ) 257 { 258 try 259 { 260 Reference< XModifiable > xDocumentModifiable( _rContext.getContextValueByAsciiName( "ContextDocument" ), UNO_QUERY_THROW ); 261 xDocumentModifiable->setModified( sal_True ); 262 } 263 catch( const Exception& ) 264 { 265 DBG_UNHANDLED_EXCEPTION(); 266 } 267 } 268 269 //-------------------------------------------------------------------- getDialogParentWindow(const ComponentContext & _rContext)270 Window* PropertyHandlerHelper::getDialogParentWindow( const ComponentContext& _rContext ) 271 { 272 Window* pInspectorWindow = NULL; 273 try 274 { 275 Reference< XWindow > xInspectorWindow( _rContext.getContextValueByAsciiName( "DialogParentWindow" ), UNO_QUERY_THROW ); 276 pInspectorWindow = VCLUnoHelper::GetWindow( xInspectorWindow ); 277 } 278 catch( const Exception& ) 279 { 280 DBG_UNHANDLED_EXCEPTION(); 281 } 282 return pInspectorWindow; 283 } 284 285 //........................................................................ 286 } // namespace pcr 287 //........................................................................ 288