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