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