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 :
28 : #include "com/sun/star/inspection/StringRepresentation.hpp"
29 : #include <com/sun/star/beans/PropertyAttribute.hpp>
30 : #include <com/sun/star/uno/XComponentContext.hpp>
31 : #include <com/sun/star/util/XModifiable.hpp>
32 : #include <com/sun/star/awt/XWindow.hpp>
33 : #include <com/sun/star/inspection/LineDescriptor.hpp>
34 : #include <com/sun/star/inspection/PropertyControlType.hpp>
35 : #include <com/sun/star/inspection/XStringListControl.hpp>
36 : #include <com/sun/star/inspection/XNumericControl.hpp>
37 : #include <tools/debug.hxx>
38 : #include <tools/diagnose_ex.h>
39 : #include <tools/StringListResource.hxx>
40 : #include <toolkit/helper/vclunohelper.hxx>
41 :
42 : #include <algorithm>
43 :
44 :
45 : namespace pcr
46 : {
47 :
48 :
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::lang;
51 : using namespace ::com::sun::star::awt;
52 : using namespace ::com::sun::star::util;
53 : using namespace ::com::sun::star::beans;
54 : using namespace ::com::sun::star::script;
55 : using namespace ::com::sun::star::inspection;
56 :
57 :
58 : //= PropertyHandlerHelper
59 :
60 :
61 0 : void PropertyHandlerHelper::describePropertyLine( const Property& _rProperty,
62 : LineDescriptor& /* [out] */ _out_rDescriptor, const Reference< XPropertyControlFactory >& _rxControlFactory )
63 : {
64 : // display the pure property name - no L10N
65 0 : _out_rDescriptor.DisplayName = _rProperty.Name;
66 :
67 : OSL_PRECOND( _rxControlFactory.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" );
68 0 : if ( !_rxControlFactory.is() )
69 0 : return;
70 :
71 0 : sal_Bool bReadOnlyControl = requiresReadOnlyControl( _rProperty.Attributes );
72 :
73 : // special handling for booleans (this will become a list)
74 0 : if ( _rProperty.Type.getTypeClass() == TypeClass_BOOLEAN )
75 : {
76 0 : ::std::vector< OUString > aListEntries;
77 0 : tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries);
78 0 : _out_rDescriptor.Control = createListBoxControl( _rxControlFactory, aListEntries, bReadOnlyControl, sal_False );
79 0 : return;
80 : }
81 :
82 0 : sal_Int16 nControlType = PropertyControlType::TextField;
83 0 : switch ( _rProperty.Type.getTypeClass() )
84 : {
85 : case TypeClass_BYTE:
86 : case TypeClass_SHORT:
87 : case TypeClass_UNSIGNED_SHORT:
88 : case TypeClass_LONG:
89 : case TypeClass_UNSIGNED_LONG:
90 : case TypeClass_HYPER:
91 : case TypeClass_UNSIGNED_HYPER:
92 : case TypeClass_FLOAT:
93 : case TypeClass_DOUBLE:
94 0 : nControlType = PropertyControlType::NumericField;
95 0 : break;
96 :
97 : case TypeClass_SEQUENCE:
98 0 : nControlType = PropertyControlType::StringListField;
99 0 : break;
100 :
101 : default:
102 : OSL_FAIL( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
103 : // NO break!
104 :
105 : case TypeClass_STRING:
106 0 : nControlType = PropertyControlType::TextField;
107 0 : break;
108 : }
109 :
110 : // create a control
111 0 : _out_rDescriptor.Control = _rxControlFactory->createPropertyControl( nControlType, bReadOnlyControl );
112 : }
113 :
114 :
115 : namespace
116 : {
117 0 : Reference< XPropertyControl > lcl_implCreateListLikeControl(
118 : const Reference< XPropertyControlFactory >& _rxControlFactory,
119 : const ::std::vector< OUString >& _rInitialListEntries,
120 : sal_Bool _bReadOnlyControl,
121 : sal_Bool _bSorted,
122 : sal_Bool _bTrueIfListBoxFalseIfComboBox
123 : )
124 : {
125 : Reference< XStringListControl > xListControl(
126 0 : _rxControlFactory->createPropertyControl(
127 : _bTrueIfListBoxFalseIfComboBox ? PropertyControlType::ListBox : PropertyControlType::ComboBox, _bReadOnlyControl
128 0 : ),
129 : UNO_QUERY_THROW
130 0 : );
131 :
132 0 : ::std::vector< OUString > aInitialEntries( _rInitialListEntries );
133 0 : if ( _bSorted )
134 0 : ::std::sort( aInitialEntries.begin(), aInitialEntries.end() );
135 :
136 0 : for ( ::std::vector< OUString >::const_iterator loop = aInitialEntries.begin();
137 0 : loop != aInitialEntries.end();
138 : ++loop
139 : )
140 0 : xListControl->appendListEntry( *loop );
141 0 : return xListControl.get();
142 : }
143 : }
144 :
145 :
146 0 : Reference< XPropertyControl > PropertyHandlerHelper::createListBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
147 : const ::std::vector< OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
148 : {
149 0 : return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_True );
150 : }
151 :
152 :
153 0 : Reference< XPropertyControl > PropertyHandlerHelper::createComboBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
154 : const ::std::vector< OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
155 : {
156 0 : return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_False );
157 : }
158 :
159 :
160 0 : Reference< XPropertyControl > PropertyHandlerHelper::createNumericControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
161 : sal_Int16 _nDigits, const Optional< double >& _rMinValue, const Optional< double >& _rMaxValue, sal_Bool _bReadOnlyControl )
162 : {
163 : Reference< XNumericControl > xNumericControl(
164 0 : _rxControlFactory->createPropertyControl( PropertyControlType::NumericField, _bReadOnlyControl ),
165 : UNO_QUERY_THROW
166 0 : );
167 :
168 0 : xNumericControl->setDecimalDigits( _nDigits );
169 0 : xNumericControl->setMinValue( _rMinValue );
170 0 : xNumericControl->setMaxValue( _rMaxValue );
171 :
172 0 : return xNumericControl.get();
173 : }
174 :
175 :
176 0 : Any PropertyHandlerHelper::convertToPropertyValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
177 : const Property& _rProperty, const Any& _rControlValue )
178 : {
179 0 : Any aPropertyValue( _rControlValue );
180 0 : if ( !aPropertyValue.hasValue() )
181 : // NULL is converted to NULL
182 0 : return aPropertyValue;
183 :
184 0 : if ( aPropertyValue.getValueType().equals( _rProperty.Type ) )
185 : // nothing to do, type is already as desired
186 0 : return aPropertyValue;
187 :
188 0 : if ( _rControlValue.getValueType().getTypeClass() == TypeClass_STRING )
189 : {
190 0 : OUString sControlValue;
191 0 : _rControlValue >>= sControlValue;
192 :
193 0 : Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
194 0 : aPropertyValue = xConversionHelper->convertToPropertyValue( sControlValue, _rProperty.Type );
195 : }
196 : else
197 : {
198 : try
199 : {
200 0 : if ( _rxTypeConverter.is() )
201 0 : aPropertyValue = _rxTypeConverter->convertTo( _rControlValue, _rProperty.Type );
202 : }
203 0 : catch( const Exception& )
204 : {
205 : OSL_FAIL( "PropertyHandlerHelper::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
206 : }
207 : }
208 :
209 0 : return aPropertyValue;
210 : }
211 :
212 :
213 0 : Any PropertyHandlerHelper::convertToControlValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
214 : const Any& _rPropertyValue, const Type& _rControlValueType )
215 : {
216 0 : Any aControlValue( _rPropertyValue );
217 0 : if ( !aControlValue.hasValue() )
218 : // NULL is converted to NULL
219 0 : return aControlValue;
220 :
221 0 : if ( _rControlValueType.getTypeClass() == TypeClass_STRING )
222 : {
223 0 : Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
224 0 : aControlValue <<= xConversionHelper->convertToControlValue( _rPropertyValue );
225 : }
226 : else
227 : {
228 : try
229 : {
230 0 : if ( _rxTypeConverter.is() )
231 0 : aControlValue = _rxTypeConverter->convertTo( _rPropertyValue, _rControlValueType );
232 : }
233 0 : catch( const Exception& )
234 : {
235 : OSL_FAIL( "PropertyHandlerHelper::convertToControlValue: caught an exception while converting via TypeConverter!" );
236 : }
237 : }
238 :
239 0 : return aControlValue;
240 : }
241 :
242 :
243 0 : void PropertyHandlerHelper::setContextDocumentModified( const Reference<XComponentContext> & _rContext )
244 : {
245 : try
246 : {
247 0 : Reference< XModifiable > xDocumentModifiable( getContextDocument_throw(_rContext), UNO_QUERY_THROW );
248 0 : xDocumentModifiable->setModified( sal_True );
249 : }
250 0 : catch( const Exception& )
251 : {
252 : DBG_UNHANDLED_EXCEPTION();
253 : }
254 0 : }
255 :
256 0 : Reference< XInterface > PropertyHandlerHelper::getContextDocument( const Reference<XComponentContext> & _rContext )
257 : {
258 0 : Reference< XInterface > xI;
259 : try
260 : {
261 0 : xI = getContextDocument_throw( _rContext );
262 : }
263 0 : catch( const Exception& )
264 : {
265 : OSL_FAIL( "PropertyHandler::getContextValueByName: caught an exception!" );
266 : }
267 0 : return xI;
268 : }
269 :
270 0 : Reference< XInterface > PropertyHandlerHelper::getContextDocument_throw( const Reference<XComponentContext> & _rContext ) throw (RuntimeException)
271 : {
272 0 : Reference< XInterface > xI;
273 0 : Any aReturn = _rContext->getValueByName( "ContextDocument" );
274 0 : aReturn >>= xI;
275 0 : return xI;
276 : }
277 :
278 :
279 0 : Window* PropertyHandlerHelper::getDialogParentWindow( const Reference<XComponentContext>& _rContext )
280 : {
281 0 : Window* pInspectorWindow = NULL;
282 : try
283 : {
284 0 : Reference< XWindow > xInspectorWindow( _rContext->getValueByName( "DialogParentWindow" ), UNO_QUERY_THROW );
285 0 : pInspectorWindow = VCLUnoHelper::GetWindow( xInspectorWindow );
286 : }
287 0 : catch( const Exception& )
288 : {
289 : DBG_UNHANDLED_EXCEPTION();
290 : }
291 0 : return pInspectorWindow;
292 : }
293 :
294 :
295 : } // namespace pcr
296 :
297 :
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|