1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <DataProviderHandler.hxx>
20 #include <com/sun/star/lang/XInitialization.hpp>
21 #include <comphelper/namedvaluecollection.hxx>
22 #include <comphelper/property.hxx>
23 #include <comphelper/sequence.hxx>
24 #include <comphelper/types.hxx>
25 #include <comphelper/propertysequence.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <strings.hxx>
28 #include <unotools/syslocale.hxx>
29 #include <com/sun/star/form/inspection/FormComponentPropertyHandler.hpp>
30 #include <com/sun/star/inspection/PropertyControlType.hpp>
31 #include <com/sun/star/inspection/PropertyLineElement.hpp>
32 #include <com/sun/star/lang/NullPointerException.hpp>
33 #include <com/sun/star/chart/ChartDataRowSource.hpp>
34 #include <com/sun/star/chart2/FormattedString.hpp>
35 #include <com/sun/star/chart2/XDiagram.hpp>
36 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
37 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
38 #include <com/sun/star/chart2/XChartType.hpp>
39 #include <com/sun/star/chart2/XTitled.hpp>
40 #include <com/sun/star/chart2/XTitle.hpp>
41 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
42 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
43 #include <com/sun/star/report/XReportDefinition.hpp>
44 #include <com/sun/star/script/Converter.hpp>
45 #include <com/sun/star/container/XNameContainer.hpp>
46 #include <com/sun/star/util/MeasureUnit.hpp>
47 #include <tools/fldunit.hxx>
48 #include <metadata.hxx>
49 #include <vcl/svapp.hxx>
50 #include <osl/mutex.hxx>
51 #include <core_resource.hxx>
53 #include <strings.hrc>
54 #include <PropertyForward.hxx>
59 using namespace ::com::sun::star
;
61 DataProviderHandler::DataProviderHandler(uno::Reference
< uno::XComponentContext
> const & context
)
62 :DataProviderHandler_Base(m_aMutex
)
67 m_xFormComponentHandler
= form::inspection::FormComponentPropertyHandler::create(m_xContext
);
68 m_xTypeConverter
= script::Converter::create(m_xContext
);
70 }catch(const uno::Exception
&)
75 OUString SAL_CALL
DataProviderHandler::getImplementationName( )
77 return getImplementationName_Static();
80 sal_Bool SAL_CALL
DataProviderHandler::supportsService( const OUString
& ServiceName
)
82 return cppu::supportsService(this, ServiceName
);
85 uno::Sequence
< OUString
> SAL_CALL
DataProviderHandler::getSupportedServiceNames( )
87 return getSupportedServiceNames_static();
90 OUString
DataProviderHandler::getImplementationName_Static( )
92 return OUString("com.sun.star.comp.report.DataProviderHandler");
95 uno::Sequence
< OUString
> DataProviderHandler::getSupportedServiceNames_static( )
97 uno::Sequence
< OUString
> aSupported
{ "com.sun.star.report.inspection.DataProviderHandler" };
101 uno::Reference
< uno::XInterface
> DataProviderHandler::create( const uno::Reference
< uno::XComponentContext
>& _rxContext
)
103 return *(new DataProviderHandler( _rxContext
));
105 // override WeakComponentImplHelperBase::disposing()
106 // This function is called upon disposing the component,
107 // if your component needs special work when it becomes
108 // disposed, do it here.
109 void SAL_CALL
DataProviderHandler::disposing()
111 ::comphelper::disposeComponent(m_xFormComponentHandler
);
112 ::comphelper::disposeComponent( m_xMasterDetails
);
113 ::comphelper::disposeComponent(m_xTypeConverter
);
115 void SAL_CALL
DataProviderHandler::addEventListener(const uno::Reference
< lang::XEventListener
> & xListener
)
117 m_xFormComponentHandler
->addEventListener(xListener
);
120 void SAL_CALL
DataProviderHandler::removeEventListener(const uno::Reference
< lang::XEventListener
> & aListener
)
122 m_xFormComponentHandler
->removeEventListener(aListener
);
125 // inspection::XPropertyHandler:
127 /********************************************************************************/
128 void SAL_CALL
DataProviderHandler::inspect(const uno::Reference
< uno::XInterface
> & Component
)
132 uno::Reference
< container::XNameContainer
> xNameCont(Component
,uno::UNO_QUERY
);
133 const OUString
sFormComponent("FormComponent");
134 if ( xNameCont
->hasByName(sFormComponent
) )
136 uno::Reference
<beans::XPropertySet
> xProp(xNameCont
->getByName(sFormComponent
),uno::UNO_QUERY
);
137 const OUString
sModel("Model");
138 if ( xProp
.is() && xProp
->getPropertySetInfo()->hasPropertyByName(sModel
) )
140 m_xChartModel
.set(xProp
->getPropertyValue(sModel
),uno::UNO_QUERY
);
141 if ( m_xChartModel
.is() )
142 m_xFormComponent
= m_xChartModel
->getDataProvider();
145 m_xDataProvider
.set(m_xFormComponent
,uno::UNO_QUERY
);
146 m_xReportComponent
.set( xNameCont
->getByName("ReportComponent"), uno::UNO_QUERY
);
147 if ( m_xDataProvider
.is() )
149 std::shared_ptr
<AnyConverter
> aNoConverter(new AnyConverter
);
150 TPropertyNamePair aPropertyMediation
;
151 aPropertyMediation
.emplace( PROPERTY_MASTERFIELDS
, TPropertyConverter(PROPERTY_MASTERFIELDS
,aNoConverter
) );
152 aPropertyMediation
.emplace( PROPERTY_DETAILFIELDS
, TPropertyConverter(PROPERTY_DETAILFIELDS
,aNoConverter
) );
154 m_xMasterDetails
= new OPropertyMediator( m_xDataProvider
.get(), m_xReportComponent
.get(), aPropertyMediation
,true );
157 catch(const uno::Exception
&)
159 throw lang::NullPointerException();
161 if ( m_xFormComponent
.is() )
163 m_xFormComponentHandler
->inspect(m_xFormComponent
);
167 uno::Any SAL_CALL
DataProviderHandler::getPropertyValue(const OUString
& PropertyName
)
169 ::osl::MutexGuard
aGuard( m_aMutex
);
170 uno::Any aPropertyValue
;
171 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(PropertyName
);
174 case PROPERTY_ID_CHARTTYPE
:
175 // TODO: We need a possibility to get the UI of the selected chart type
176 // LEM: this business of ignoring ChartType seems very fishy!
177 //if( m_xChartModel.is() )
179 // uno::Reference< chart2::XDiagram > xDiagram( m_xChartModel->getFirstDiagram() );
180 // if( xDiagram.is() )
182 // OUString sChartTypes;
183 // uno::Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY_THROW );
184 // const uno::Sequence< uno::Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems());
185 // const uno::Reference< chart2::XCoordinateSystem >* pIter = aCooSysSeq.getConstArray();
186 // const uno::Reference< chart2::XCoordinateSystem >* pEnd = pIter + aCooSysSeq.getLength();
187 // for(;pIter != pEnd;++pIter)
189 // const uno::Reference< chart2::XChartTypeContainer > xCTCnt( *pIter, uno::UNO_QUERY_THROW );
190 // const uno::Sequence< uno::Reference< chart2::XChartType > > aCTSeq( xCTCnt->getChartTypes());
191 // const uno::Reference< chart2::XChartType >* pChartTypeIter = aCTSeq.getConstArray();
192 // const uno::Reference< chart2::XChartType >* pChartTypeEnd = pChartTypeIter + aCTSeq.getLength();
193 // for(;pChartTypeIter != pChartTypeEnd;++pChartTypeIter)
195 // sChartTypes += (*pChartTypeIter)->getChartType();
196 // sChartTypes += ";";
199 // aPropertyValue;// <<= sChartTypes;
203 case PROPERTY_ID_PREVIEW_COUNT
:
204 aPropertyValue
<<= m_xDataProvider
->getRowLimit();
207 aPropertyValue
= m_xFormComponentHandler
->getPropertyValue( PropertyName
);
210 return aPropertyValue
;
213 void SAL_CALL
DataProviderHandler::setPropertyValue(const OUString
& PropertyName
, const uno::Any
& Value
)
215 ::osl::MutexGuard
aGuard( m_aMutex
);
216 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(PropertyName
);
219 case PROPERTY_ID_CHARTTYPE
:
221 case PROPERTY_ID_PREVIEW_COUNT
:
222 m_xDataProvider
->setPropertyValue(PropertyName
,Value
);
225 m_xFormComponentHandler
->setPropertyValue(PropertyName
, Value
);
230 void DataProviderHandler::impl_updateChartTitle_throw(const uno::Any
& _aValue
)
232 uno::Reference
<chart2::XTitled
> xTitled(m_xChartModel
,uno::UNO_QUERY
);
235 uno::Reference
<chart2::XTitle
> xTitle
= xTitled
->getTitleObject();
238 xTitle
.set(m_xContext
->getServiceManager()->createInstanceWithContext("com.sun.star.chart2.Title",m_xContext
),uno::UNO_QUERY
);
239 xTitled
->setTitleObject(xTitle
);
243 uno::Reference
< chart2::XFormattedString2
> xFormatted
= chart2::FormattedString::create(m_xContext
);
246 xFormatted
->setString(sStr
);
247 uno::Sequence
< uno::Reference
< chart2::XFormattedString
> > aArgs(1);
248 aArgs
[0] = xFormatted
;
249 xTitle
->setText(aArgs
);
254 beans::PropertyState SAL_CALL
DataProviderHandler::getPropertyState(const OUString
& PropertyName
)
256 return m_xFormComponentHandler
->getPropertyState(PropertyName
);
259 inspection::LineDescriptor SAL_CALL
DataProviderHandler::describePropertyLine(const OUString
& PropertyName
, const uno::Reference
< inspection::XPropertyControlFactory
> & _xControlFactory
)
261 inspection::LineDescriptor aOut
;
262 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(PropertyName
);
265 case PROPERTY_ID_CHARTTYPE
:
266 aOut
.PrimaryButtonId
= UID_RPT_PROP_CHARTTYPE_DLG
;
267 aOut
.Control
= _xControlFactory
->createPropertyControl(inspection::PropertyControlType::TextField
, true);
268 aOut
.HasPrimaryButton
= true;
270 case PROPERTY_ID_PREVIEW_COUNT
:
271 aOut
.Control
= _xControlFactory
->createPropertyControl(inspection::PropertyControlType::NumericField
, false);
273 case PROPERTY_ID_MASTERFIELDS
:
274 case PROPERTY_ID_DETAILFIELDS
:
275 aOut
.Control
= _xControlFactory
->createPropertyControl(inspection::PropertyControlType::StringListField
, false);
276 aOut
.PrimaryButtonId
= UID_RPT_PROP_DLG_LINKFIELDS
;
277 aOut
.HasPrimaryButton
= true;
280 aOut
= m_xFormComponentHandler
->describePropertyLine(PropertyName
, _xControlFactory
);
284 aOut
.Category
= (OPropertyInfoService::getPropertyUIFlags(nId
) & PropUIFlags::DataProperty
) ?
288 aOut
.HelpURL
= HelpIdUrl::getHelpURL( OPropertyInfoService::getPropertyHelpId( nId
) );
289 aOut
.DisplayName
= OPropertyInfoService::getPropertyTranslation(nId
);
294 uno::Any SAL_CALL
DataProviderHandler::convertToPropertyValue(const OUString
& _rPropertyValue
, const uno::Any
& _rControlValue
)
296 ::osl::MutexGuard
aGuard( m_aMutex
);
297 uno::Any
aPropertyValue( _rControlValue
);
298 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(_rPropertyValue
);
301 case PROPERTY_ID_CHARTTYPE
:
303 case PROPERTY_ID_PREVIEW_COUNT
:
306 aPropertyValue
= m_xTypeConverter
->convertTo( _rControlValue
, ::cppu::UnoType
<sal_Int32
>::get());
308 catch( const uno::Exception
& )
310 OSL_FAIL( "DataProviderHandler::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
313 case PROPERTY_ID_MASTERFIELDS
:
314 case PROPERTY_ID_DETAILFIELDS
:
317 aPropertyValue
= m_xFormComponentHandler
->convertToPropertyValue(_rPropertyValue
, _rControlValue
);
319 return aPropertyValue
;
322 uno::Any SAL_CALL
DataProviderHandler::convertToControlValue(const OUString
& _rPropertyName
, const uno::Any
& _rPropertyValue
, const uno::Type
& ControlValueType
)
324 uno::Any
aControlValue( _rPropertyValue
);
325 if ( !aControlValue
.hasValue() )
326 // NULL is converted to NULL
327 return aControlValue
;
329 ::osl::MutexGuard
aGuard( m_aMutex
);
330 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(_rPropertyName
);
333 case PROPERTY_ID_CHARTTYPE
:
335 case PROPERTY_ID_MASTERFIELDS
:
336 case PROPERTY_ID_DETAILFIELDS
:
337 case PROPERTY_ID_PREVIEW_COUNT
:
340 aControlValue
= m_xTypeConverter
->convertTo( _rPropertyValue
, ControlValueType
);
342 catch( const uno::Exception
& )
344 OSL_FAIL( "GeometryHandler::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
348 aControlValue
= m_xFormComponentHandler
->convertToControlValue(_rPropertyName
, _rPropertyValue
, ControlValueType
);
350 return aControlValue
;
353 void SAL_CALL
DataProviderHandler::addPropertyChangeListener(const uno::Reference
< beans::XPropertyChangeListener
> & Listener
)
355 m_xFormComponentHandler
->addPropertyChangeListener(Listener
);
358 void SAL_CALL
DataProviderHandler::removePropertyChangeListener(const uno::Reference
< beans::XPropertyChangeListener
> & _rxListener
)
360 m_xFormComponentHandler
->removePropertyChangeListener(_rxListener
);
363 uno::Sequence
< beans::Property
> SAL_CALL
DataProviderHandler::getSupportedProperties()
365 ::std::vector
< beans::Property
> aNewProps
;
366 if( m_xChartModel
.is() )
368 rptui::OPropertyInfoService::getExcludeProperties( aNewProps
, m_xFormComponentHandler
);
369 beans::Property aValue
;
370 static const OUStringLiteral s_pProperties
[] =
373 ,PROPERTY_MASTERFIELDS
374 ,PROPERTY_DETAILFIELDS
375 ,PROPERTY_PREVIEW_COUNT
378 for (const auto & rName
: s_pProperties
)
381 aNewProps
.push_back(aValue
);
384 return uno::Sequence
< beans::Property
>(aNewProps
.data(), aNewProps
.size());
387 uno::Sequence
< OUString
> SAL_CALL
DataProviderHandler::getSupersededProperties()
389 uno::Sequence
< OUString
> aRet
{ PROPERTY_TITLE
}; // have a look at OPropertyInfoService::getExcludeProperties
393 uno::Sequence
< OUString
> SAL_CALL
DataProviderHandler::getActuatingProperties()
395 ::osl::MutexGuard
aGuard( m_aMutex
);
397 uno::Sequence
< OUString
> aSeq
{ PROPERTY_TITLE
};
398 return ::comphelper::concatSequences(m_xFormComponentHandler
->getActuatingProperties(),aSeq
);
401 sal_Bool SAL_CALL
DataProviderHandler::isComposable( const OUString
& _rPropertyName
)
403 return OPropertyInfoService::isComposable( _rPropertyName
, m_xFormComponentHandler
);
406 inspection::InteractiveSelectionResult SAL_CALL
DataProviderHandler::onInteractivePropertySelection(const OUString
& PropertyName
, sal_Bool Primary
, uno::Any
& out_Data
, const uno::Reference
< inspection::XObjectInspectorUI
> & _rxInspectorUI
)
408 if ( !_rxInspectorUI
.is() )
409 throw lang::NullPointerException();
411 inspection::InteractiveSelectionResult eResult
= inspection::InteractiveSelectionResult_Cancelled
;
412 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
414 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(PropertyName
);
417 case PROPERTY_ID_CHARTTYPE
:
418 if ( impl_dialogChartType_nothrow(aGuard
) )
419 eResult
= inspection::InteractiveSelectionResult_ObtainedValue
;
421 case PROPERTY_ID_MASTERFIELDS
:
422 case PROPERTY_ID_DETAILFIELDS
:
423 if ( impl_dialogLinkedFields_nothrow( aGuard
) )
424 eResult
= inspection::InteractiveSelectionResult_Success
;
427 eResult
= m_xFormComponentHandler
->onInteractivePropertySelection(PropertyName
, Primary
, out_Data
, _rxInspectorUI
);
433 void SAL_CALL
DataProviderHandler::actuatingPropertyChanged(const OUString
& ActuatingPropertyName
, const uno::Any
& NewValue
, const uno::Any
& OldValue
, const uno::Reference
< inspection::XObjectInspectorUI
> & InspectorUI
, sal_Bool FirstTimeInit
)
435 osl::MutexGuard
aGuard( m_aMutex
);
437 if ( ActuatingPropertyName
== PROPERTY_COMMAND
)
439 if ( NewValue
!= OldValue
)
441 uno::Reference
< report::XReportDefinition
> xReport
= m_xReportComponent
->getSection()->getReportDefinition();
442 bool bDoEnableMasterDetailFields
= xReport
.is() && !xReport
->getCommand().isEmpty() && !m_xDataProvider
->getCommand().isEmpty();
443 InspectorUI
->enablePropertyUIElements( PROPERTY_DETAILFIELDS
, inspection::PropertyLineElement::PrimaryButton
, bDoEnableMasterDetailFields
);
444 InspectorUI
->enablePropertyUIElements( PROPERTY_MASTERFIELDS
, inspection::PropertyLineElement::PrimaryButton
, bDoEnableMasterDetailFields
);
446 bool bModified
= xReport
->isModified();
447 // this fills the chart again
448 ::comphelper::NamedValueCollection aArgs
;
449 aArgs
.put( "CellRangeRepresentation", uno::makeAny( OUString( "all" ) ) );
450 aArgs
.put( "HasCategories", uno::makeAny( true ) );
451 aArgs
.put( "FirstCellAsLabel", uno::makeAny( true ) );
452 aArgs
.put( "DataRowSource", uno::makeAny( chart::ChartDataRowSource_COLUMNS
) );
453 uno::Reference
< chart2::data::XDataReceiver
> xReceiver(m_xChartModel
,uno::UNO_QUERY_THROW
);
454 xReceiver
->setArguments( aArgs
.getPropertyValues() );
456 xReport
->setModified(false);
458 m_xFormComponentHandler
->actuatingPropertyChanged(ActuatingPropertyName
, NewValue
, OldValue
, InspectorUI
, FirstTimeInit
);
460 else if ( ActuatingPropertyName
== PROPERTY_TITLE
)
462 if ( NewValue
!= OldValue
)
463 impl_updateChartTitle_throw(NewValue
);
467 const sal_Int32 nId
= OPropertyInfoService::getPropertyId(ActuatingPropertyName
);
471 case PROPERTY_ID_MASTERFIELDS
:
473 case PROPERTY_ID_DETAILFIELDS
:
476 m_xFormComponentHandler
->actuatingPropertyChanged(ActuatingPropertyName
, NewValue
, OldValue
, InspectorUI
, FirstTimeInit
);
481 sal_Bool SAL_CALL
DataProviderHandler::suspend(sal_Bool Suspend
)
483 return m_xFormComponentHandler
->suspend(Suspend
);
485 bool DataProviderHandler::impl_dialogLinkedFields_nothrow( ::osl::ClearableMutexGuard
& _rClearBeforeDialog
) const
487 uno::Sequence
<uno::Any
> aSeq(comphelper::InitAnyPropertySequence(
489 {"ParentWindow", m_xContext
->getValueByName("DialogParentWindow")},
490 {"Detail", uno::Any(m_xDataProvider
)},
491 {"Master", uno::Any(m_xReportComponent
->getSection()->getReportDefinition())},
492 {"Explanation", uno::Any(RptResId(RID_STR_EXPLANATION
))},
493 {"DetailLabel", uno::Any(RptResId(RID_STR_DETAILLABEL
))},
494 {"MasterLabel", uno::Any(RptResId(RID_STR_MASTERLABEL
))},
497 uno::Reference
< ui::dialogs::XExecutableDialog
> xDialog(
498 m_xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
499 "org.openoffice.comp.form.ui.MasterDetailLinkDialog", aSeq
, m_xContext
),
502 _rClearBeforeDialog
.clear();
503 return ( xDialog
->execute() != 0 );
506 bool DataProviderHandler::impl_dialogChartType_nothrow( ::osl::ClearableMutexGuard
& _rClearBeforeDialog
) const
508 uno::Sequence
<uno::Any
> aSeq(comphelper::InitAnyPropertySequence(
510 {"ParentWindow", m_xContext
->getValueByName("DialogParentWindow")},
511 {"ChartModel", uno::Any(m_xChartModel
)}
514 uno::Reference
< ui::dialogs::XExecutableDialog
> xDialog(
515 m_xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
516 "com.sun.star.comp.chart2.ChartTypeDialog", aSeq
, m_xContext
),
519 _rClearBeforeDialog
.clear();
520 return ( xDialog
->execute() != 0 );
526 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */