1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_extensions.hxx"
31 #include "handlerhelper.hxx"
32 #include "propresid.hrc"
33 #include "formresid.hrc"
34 #include <comphelper/extract.hxx>
35 #include "modulepcr.hxx"
36 #include "enumrepresentation.hxx"
37 #include "formmetadata.hxx"
38 #include "pcrcomponentcontext.hxx"
40 /** === begin UNO includes === **/
41 #include "com/sun/star/inspection/StringRepresentation.hpp"
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
43 #include <com/sun/star/uno/XComponentContext.hpp>
44 #include <com/sun/star/util/XModifiable.hpp>
45 #include <com/sun/star/awt/XWindow.hpp>
46 #include <com/sun/star/inspection/LineDescriptor.hpp>
47 #include <com/sun/star/inspection/PropertyControlType.hpp>
48 #include <com/sun/star/inspection/XStringListControl.hpp>
49 #include <com/sun/star/inspection/XNumericControl.hpp>
50 /** === end UNO includes === **/
51 #include <tools/debug.hxx>
52 #include <tools/diagnose_ex.h>
53 #include <tools/StringListResource.hxx>
54 #include <toolkit/helper/vclunohelper.hxx>
58 //........................................................................
61 //........................................................................
63 using namespace ::com::sun::star::uno
;
64 using namespace ::com::sun::star::lang
;
65 using namespace ::com::sun::star::awt
;
66 using namespace ::com::sun::star::util
;
67 using namespace ::com::sun::star::beans
;
68 using namespace ::com::sun::star::script
;
69 using namespace ::com::sun::star::inspection
;
71 //====================================================================
72 //= PropertyHandlerHelper
73 //====================================================================
74 //--------------------------------------------------------------------
75 void PropertyHandlerHelper::describePropertyLine( const Property
& _rProperty
,
76 LineDescriptor
& /* [out] */ _out_rDescriptor
, const Reference
< XPropertyControlFactory
>& _rxControlFactory
)
78 // display the pure property name - no L10N
79 _out_rDescriptor
.DisplayName
= _rProperty
.Name
;
81 OSL_PRECOND( _rxControlFactory
.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" );
82 if ( !_rxControlFactory
.is() )
85 sal_Bool bReadOnlyControl
= requiresReadOnlyControl( _rProperty
.Attributes
);
87 // special handling for booleans (this will become a list)
88 if ( _rProperty
.Type
.getTypeClass() == TypeClass_BOOLEAN
)
90 ::std::vector
< ::rtl::OUString
> aListEntries
;
91 tools::StringListResource
aRes(PcrRes(RID_RSC_ENUM_YESNO
),aListEntries
);
92 _out_rDescriptor
.Control
= createListBoxControl( _rxControlFactory
, aListEntries
, bReadOnlyControl
, sal_False
);
96 sal_Int16 nControlType
= PropertyControlType::TextField
;
97 switch ( _rProperty
.Type
.getTypeClass() )
100 case TypeClass_SHORT
:
101 case TypeClass_UNSIGNED_SHORT
:
103 case TypeClass_UNSIGNED_LONG
:
104 case TypeClass_HYPER
:
105 case TypeClass_UNSIGNED_HYPER
:
106 case TypeClass_FLOAT
:
107 case TypeClass_DOUBLE
:
108 nControlType
= PropertyControlType::NumericField
;
111 case TypeClass_SEQUENCE
:
112 nControlType
= PropertyControlType::StringListField
;
116 DBG_ERROR( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
119 case TypeClass_STRING
:
120 nControlType
= PropertyControlType::TextField
;
125 _out_rDescriptor
.Control
= _rxControlFactory
->createPropertyControl( nControlType
, bReadOnlyControl
);
128 //--------------------------------------------------------------------
131 Reference
< XPropertyControl
> lcl_implCreateListLikeControl(
132 const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
133 const ::std::vector
< ::rtl::OUString
>& _rInitialListEntries
,
134 sal_Bool _bReadOnlyControl
,
136 sal_Bool _bTrueIfListBoxFalseIfComboBox
139 Reference
< XStringListControl
> xListControl(
140 _rxControlFactory
->createPropertyControl(
141 _bTrueIfListBoxFalseIfComboBox
? PropertyControlType::ListBox
: PropertyControlType::ComboBox
, _bReadOnlyControl
146 ::std::vector
< ::rtl::OUString
> aInitialEntries( _rInitialListEntries
);
148 ::std::sort( aInitialEntries
.begin(), aInitialEntries
.end() );
150 for ( ::std::vector
< ::rtl::OUString
>::const_iterator loop
= aInitialEntries
.begin();
151 loop
!= aInitialEntries
.end();
154 xListControl
->appendListEntry( *loop
);
155 return xListControl
.get();
159 //--------------------------------------------------------------------
160 Reference
< XPropertyControl
> PropertyHandlerHelper::createListBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
161 const ::std::vector
< ::rtl::OUString
>& _rInitialListEntries
, sal_Bool _bReadOnlyControl
, sal_Bool _bSorted
)
163 return lcl_implCreateListLikeControl( _rxControlFactory
, _rInitialListEntries
, _bReadOnlyControl
, _bSorted
, sal_True
);
166 //--------------------------------------------------------------------
167 Reference
< XPropertyControl
> PropertyHandlerHelper::createComboBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
168 const ::std::vector
< ::rtl::OUString
>& _rInitialListEntries
, sal_Bool _bReadOnlyControl
, sal_Bool _bSorted
)
170 return lcl_implCreateListLikeControl( _rxControlFactory
, _rInitialListEntries
, _bReadOnlyControl
, _bSorted
, sal_False
);
173 //--------------------------------------------------------------------
174 Reference
< XPropertyControl
> PropertyHandlerHelper::createNumericControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
175 sal_Int16 _nDigits
, const Optional
< double >& _rMinValue
, const Optional
< double >& _rMaxValue
, sal_Bool _bReadOnlyControl
)
177 Reference
< XNumericControl
> xNumericControl(
178 _rxControlFactory
->createPropertyControl( PropertyControlType::NumericField
, _bReadOnlyControl
),
182 xNumericControl
->setDecimalDigits( _nDigits
);
183 xNumericControl
->setMinValue( _rMinValue
);
184 xNumericControl
->setMaxValue( _rMaxValue
);
186 return xNumericControl
.get();
189 //--------------------------------------------------------------------
190 Any
PropertyHandlerHelper::convertToPropertyValue( const Reference
< XComponentContext
>& _rxContext
,const Reference
< XTypeConverter
>& _rxTypeConverter
,
191 const Property
& _rProperty
, const Any
& _rControlValue
)
193 Any
aPropertyValue( _rControlValue
);
194 if ( !aPropertyValue
.hasValue() )
195 // NULL is converted to NULL
196 return aPropertyValue
;
198 if ( aPropertyValue
.getValueType().equals( _rProperty
.Type
) )
199 // nothing to do, type is already as desired
200 return aPropertyValue
;
202 if ( _rControlValue
.getValueType().getTypeClass() == TypeClass_STRING
)
204 ::rtl::OUString sControlValue
;
205 _rControlValue
>>= sControlValue
;
207 Reference
< XStringRepresentation
> xConversionHelper
= StringRepresentation::create( _rxContext
,_rxTypeConverter
);
208 aPropertyValue
= xConversionHelper
->convertToPropertyValue( sControlValue
, _rProperty
.Type
);
214 if ( _rxTypeConverter
.is() )
215 aPropertyValue
= _rxTypeConverter
->convertTo( _rControlValue
, _rProperty
.Type
);
217 catch( const Exception
& )
219 OSL_ENSURE( sal_False
, "PropertyHandlerHelper::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
223 return aPropertyValue
;
226 //--------------------------------------------------------------------
227 Any
PropertyHandlerHelper::convertToControlValue( const Reference
< XComponentContext
>& _rxContext
,const Reference
< XTypeConverter
>& _rxTypeConverter
,
228 const Any
& _rPropertyValue
, const Type
& _rControlValueType
)
230 Any
aControlValue( _rPropertyValue
);
231 if ( !aControlValue
.hasValue() )
232 // NULL is converted to NULL
233 return aControlValue
;
235 if ( _rControlValueType
.getTypeClass() == TypeClass_STRING
)
237 Reference
< XStringRepresentation
> xConversionHelper
= StringRepresentation::create( _rxContext
,_rxTypeConverter
);
238 aControlValue
<<= xConversionHelper
->convertToControlValue( _rPropertyValue
);
244 if ( _rxTypeConverter
.is() )
245 aControlValue
= _rxTypeConverter
->convertTo( _rPropertyValue
, _rControlValueType
);
247 catch( const Exception
& )
249 OSL_ENSURE( sal_False
, "PropertyHandlerHelper::convertToControlValue: caught an exception while converting via TypeConverter!" );
253 return aControlValue
;
256 //--------------------------------------------------------------------
257 void PropertyHandlerHelper::setContextDocumentModified( const ComponentContext
& _rContext
)
261 Reference
< XModifiable
> xDocumentModifiable( _rContext
.getContextValueByAsciiName( "ContextDocument" ), UNO_QUERY_THROW
);
262 xDocumentModifiable
->setModified( sal_True
);
264 catch( const Exception
& )
266 DBG_UNHANDLED_EXCEPTION();
270 //--------------------------------------------------------------------
271 Window
* PropertyHandlerHelper::getDialogParentWindow( const ComponentContext
& _rContext
)
273 Window
* pInspectorWindow
= NULL
;
276 Reference
< XWindow
> xInspectorWindow( _rContext
.getContextValueByAsciiName( "DialogParentWindow" ), UNO_QUERY_THROW
);
277 pInspectorWindow
= VCLUnoHelper::GetWindow( xInspectorWindow
);
279 catch( const Exception
& )
281 DBG_UNHANDLED_EXCEPTION();
283 return pInspectorWindow
;
286 //........................................................................
288 //........................................................................
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */