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