LCOV - code coverage report
Current view: top level - libreoffice/extensions/source/propctrlr - eformshelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 316 0.0 %
Date: 2012-12-27 Functions: 0 29 0.0 %
Legend: Lines: hit not hit

          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 "eformshelper.hxx"
      21             : #include "formstrings.hxx"
      22             : #include "formresid.hrc"
      23             : #include "modulepcr.hxx"
      24             : #include "propeventtranslation.hxx"
      25             : #include "formbrowsertools.hxx"
      26             : 
      27             : #include <com/sun/star/lang/XServiceInfo.hpp>
      28             : #include <com/sun/star/form/FormComponentType.hpp>
      29             : #include <com/sun/star/xforms/XFormsUIHelper1.hpp>
      30             : #include <com/sun/star/xsd/DataTypeClass.hpp>
      31             : #include <com/sun/star/form/binding/XListEntrySink.hpp>
      32             : #include <tools/diagnose_ex.h>
      33             : #include <rtl/ustrbuf.hxx>
      34             : 
      35             : #include <functional>
      36             : #include <algorithm>
      37             : #include <o3tl/compat_functional.hxx>
      38             : 
      39             : //........................................................................
      40             : namespace pcr
      41             : {
      42             : //........................................................................
      43             : 
      44             :     using namespace ::com::sun::star;
      45             :     using namespace ::com::sun::star::uno;
      46             :     using namespace ::com::sun::star::beans;
      47             :     using namespace ::com::sun::star::container;
      48             :     using namespace ::com::sun::star::form::binding;
      49             :     using namespace ::com::sun::star::xsd;
      50             :     using namespace ::com::sun::star::lang;
      51             :     using namespace ::com::sun::star::form;
      52             : 
      53             :     //====================================================================
      54             :     //= file-local helpers
      55             :     //====================================================================
      56             :     namespace
      57             :     {
      58             :         //--------------------------------------------------------------------
      59           0 :         ::rtl::OUString composeModelElementUIName( const ::rtl::OUString& _rModelName, const ::rtl::OUString& _rElementName )
      60             :         {
      61           0 :             ::rtl::OUStringBuffer aBuffer;
      62           0 :             aBuffer.appendAscii( "[" );
      63           0 :             aBuffer.append( _rModelName );
      64           0 :             aBuffer.appendAscii( "] " );
      65           0 :             aBuffer.append( _rElementName );
      66           0 :             return aBuffer.makeStringAndClear();
      67             :         }
      68             :     }
      69             : 
      70             :     //====================================================================
      71             :     //= EFormsHelper
      72             :     //====================================================================
      73             :     //--------------------------------------------------------------------
      74           0 :     EFormsHelper::EFormsHelper( ::osl::Mutex& _rMutex, const Reference< XPropertySet >& _rxControlModel, const Reference< frame::XModel >& _rxContextDocument )
      75             :         :m_xControlModel( _rxControlModel )
      76           0 :         ,m_aPropertyListeners( _rMutex )
      77             :     {
      78             :         OSL_ENSURE( _rxControlModel.is(), "EFormsHelper::EFormsHelper: invalid control model!" );
      79           0 :         m_xBindableControl = m_xBindableControl.query( _rxControlModel );
      80             : 
      81           0 :         m_xDocument = m_xDocument.query( _rxContextDocument );
      82             :         OSL_ENSURE( m_xDocument.is(), "EFormsHelper::EFormsHelper: invalid document!" );
      83             : 
      84           0 :     }
      85             : 
      86             :     //--------------------------------------------------------------------
      87           0 :     bool EFormsHelper::isEForm( const Reference< frame::XModel >& _rxContextDocument )
      88             :     {
      89             :         try
      90             :         {
      91           0 :             Reference< xforms::XFormsSupplier > xDocument( _rxContextDocument, UNO_QUERY );
      92           0 :             if ( !xDocument.is() )
      93           0 :                 return false;
      94             : 
      95           0 :             return xDocument->getXForms().is();
      96             :         }
      97           0 :         catch( const Exception& )
      98             :         {
      99             :             OSL_FAIL( "EFormsHelper::isEForm: caught an exception!" );
     100             :         }
     101           0 :         return false;
     102             :     }
     103             : 
     104             :     //--------------------------------------------------------------------
     105           0 :     bool EFormsHelper::canBindToDataType( sal_Int32 _nDataType ) const SAL_THROW(())
     106             :     {
     107           0 :         if ( !m_xBindableControl.is() )
     108             :             // cannot bind at all
     109           0 :             return false;
     110             : 
     111             :         // some types cannot be bound, independent from the control type
     112           0 :         if (  ( DataTypeClass::hexBinary == _nDataType )
     113             :            || ( DataTypeClass::base64Binary == _nDataType )
     114             :            || ( DataTypeClass::QName == _nDataType )
     115             :            || ( DataTypeClass::NOTATION == _nDataType )
     116             :            )
     117           0 :            return false;
     118             : 
     119           0 :         bool bCan = false;
     120             :         try
     121             :         {
     122             :             // classify the control model
     123           0 :             sal_Int16 nControlType = FormComponentType::CONTROL;
     124           0 :             OSL_VERIFY( m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nControlType );
     125             : 
     126             :             // some lists
     127           0 :             sal_Int16 nNumericCompatibleTypes[] = { DataTypeClass::DECIMAL, DataTypeClass::FLOAT, DataTypeClass::DOUBLE, 0 };
     128           0 :             sal_Int16 nDateCompatibleTypes[] = { DataTypeClass::DATE, 0 };
     129           0 :             sal_Int16 nTimeCompatibleTypes[] = { DataTypeClass::TIME, 0 };
     130           0 :             sal_Int16 nCheckboxCompatibleTypes[] = { DataTypeClass::BOOLEAN, DataTypeClass::STRING, DataTypeClass::anyURI, 0 };
     131           0 :             sal_Int16 nRadiobuttonCompatibleTypes[] = { DataTypeClass::STRING, DataTypeClass::anyURI, 0 };
     132           0 :             sal_Int16 nFormattedCompatibleTypes[] = { DataTypeClass::DECIMAL, DataTypeClass::FLOAT, DataTypeClass::DOUBLE, DataTypeClass::DATETIME, DataTypeClass::DATE, DataTypeClass::TIME, 0 };
     133             : 
     134           0 :             sal_Int16* pCompatibleTypes = NULL;
     135           0 :             switch ( nControlType )
     136             :             {
     137             :             case FormComponentType::SPINBUTTON:
     138             :             case FormComponentType::NUMERICFIELD:
     139           0 :                 pCompatibleTypes = nNumericCompatibleTypes;
     140           0 :                 break;
     141             :             case FormComponentType::DATEFIELD:
     142           0 :                 pCompatibleTypes = nDateCompatibleTypes;
     143           0 :                 break;
     144             :             case FormComponentType::TIMEFIELD:
     145           0 :                 pCompatibleTypes = nTimeCompatibleTypes;
     146           0 :                 break;
     147             :             case FormComponentType::CHECKBOX:
     148           0 :                 pCompatibleTypes = nCheckboxCompatibleTypes;
     149           0 :                 break;
     150             :             case FormComponentType::RADIOBUTTON:
     151           0 :                 pCompatibleTypes = nRadiobuttonCompatibleTypes;
     152           0 :                 break;
     153             : 
     154             :             case FormComponentType::TEXTFIELD:
     155             :             {
     156             :                 // both the normal text field, and the formatted field, claim to be a TEXTFIELD
     157             :                 // need to distinguish by service name
     158           0 :                 Reference< XServiceInfo > xSI( m_xControlModel, UNO_QUERY );
     159             :                 OSL_ENSURE( xSI.is(), "EFormsHelper::canBindToDataType: a control model which has no service info?" );
     160           0 :                 if ( xSI.is() )
     161             :                 {
     162           0 :                     if ( xSI->supportsService( SERVICE_COMPONENT_FORMATTEDFIELD ) )
     163             :                     {
     164           0 :                         pCompatibleTypes = nFormattedCompatibleTypes;
     165             :                         break;
     166             :                     }
     167           0 :                 }
     168             :                 // NO break here!
     169             :             }
     170             :             case FormComponentType::LISTBOX:
     171             :             case FormComponentType::COMBOBOX:
     172             :                 // edit fields and list/combo boxes can be bound to anything
     173           0 :                 bCan = true;
     174             :             }
     175             : 
     176           0 :             if ( !bCan && pCompatibleTypes )
     177             :             {
     178           0 :                 if ( _nDataType == -1 )
     179             :                 {
     180             :                     // the control can be bound to at least one type, and exactly this is being asked for
     181           0 :                     bCan = true;
     182             :                 }
     183             :                 else
     184             :                 {
     185           0 :                     while ( *pCompatibleTypes && !bCan )
     186           0 :                         bCan = ( *pCompatibleTypes++ == _nDataType );
     187             :                 }
     188             :             }
     189             :         }
     190           0 :         catch( const Exception& )
     191             :         {
     192             :             OSL_FAIL( "EFormsHelper::canBindToDataType: caught an exception!" );
     193             :         }
     194             : 
     195           0 :         return bCan;
     196             :     }
     197             : 
     198             :     //--------------------------------------------------------------------
     199           0 :     bool EFormsHelper::isListEntrySink() const SAL_THROW(())
     200             :     {
     201           0 :         bool bIs = false;
     202             :         try
     203             :         {
     204           0 :             Reference< XListEntrySink > xAsSink( m_xControlModel, UNO_QUERY );
     205           0 :             bIs = xAsSink.is();
     206             :         }
     207           0 :         catch( const Exception& )
     208             :         {
     209             :             OSL_FAIL( "EFormsHelper::isListEntrySink: caught an exception!" );
     210             :         }
     211           0 :         return bIs;
     212             :     }
     213             : 
     214             :     //--------------------------------------------------------------------
     215           0 :     void EFormsHelper::impl_switchBindingListening_throw( bool _bDoListening, const Reference< XPropertyChangeListener >& _rxListener )
     216             :     {
     217           0 :         Reference< XPropertySet > xBindingProps;
     218           0 :         if ( m_xBindableControl.is() )
     219           0 :             xBindingProps = xBindingProps.query( m_xBindableControl->getValueBinding() );
     220           0 :         if ( !xBindingProps.is() )
     221           0 :             return;
     222             : 
     223           0 :         if ( _bDoListening )
     224             :         {
     225           0 :             xBindingProps->addPropertyChangeListener( ::rtl::OUString(), _rxListener );
     226             :         }
     227             :         else
     228             :         {
     229           0 :             xBindingProps->removePropertyChangeListener( ::rtl::OUString(), _rxListener );
     230           0 :         }
     231             :     }
     232             : 
     233             :     //--------------------------------------------------------------------
     234           0 :     void EFormsHelper::registerBindingListener( const Reference< XPropertyChangeListener >& _rxBindingListener )
     235             :     {
     236           0 :         if ( !_rxBindingListener.is() )
     237           0 :             return;
     238           0 :         impl_toggleBindingPropertyListening_throw( true, _rxBindingListener );
     239             :     }
     240             : 
     241             :     //--------------------------------------------------------------------
     242           0 :     void EFormsHelper::impl_toggleBindingPropertyListening_throw( bool _bDoListen, const Reference< XPropertyChangeListener >& _rxConcreteListenerOrNull )
     243             :     {
     244           0 :         if ( !_bDoListen )
     245             :         {
     246             :             SAL_WNODEPRECATED_DECLARATIONS_PUSH
     247           0 :             ::std::auto_ptr< ::cppu::OInterfaceIteratorHelper > pListenerIterator = m_aPropertyListeners.createIterator();
     248             :             SAL_WNODEPRECATED_DECLARATIONS_POP
     249           0 :             while ( pListenerIterator->hasMoreElements() )
     250             :             {
     251           0 :                 PropertyEventTranslation* pTranslator = dynamic_cast< PropertyEventTranslation* >( pListenerIterator->next() );
     252             :                 OSL_ENSURE( pTranslator, "EFormsHelper::impl_toggleBindingPropertyListening_throw: invalid listener element in my container!" );
     253           0 :                 if ( !pTranslator )
     254           0 :                     continue;
     255             : 
     256           0 :                 Reference< XPropertyChangeListener > xEventSourceTranslator( pTranslator );
     257           0 :                 if ( _rxConcreteListenerOrNull.is() )
     258             :                 {
     259           0 :                     if ( pTranslator->getDelegator() == _rxConcreteListenerOrNull )
     260             :                     {
     261           0 :                         impl_switchBindingListening_throw( false, xEventSourceTranslator );
     262           0 :                         m_aPropertyListeners.removeListener( xEventSourceTranslator );
     263             :                         break;
     264             :                     }
     265             :                 }
     266             :                 else
     267             :                 {
     268           0 :                     impl_switchBindingListening_throw( false, xEventSourceTranslator );
     269             :                 }
     270           0 :             }
     271             :         }
     272             :         else
     273             :         {
     274           0 :             if ( _rxConcreteListenerOrNull.is() )
     275             :             {
     276           0 :                 Reference< XPropertyChangeListener > xEventSourceTranslator( new PropertyEventTranslation( _rxConcreteListenerOrNull, m_xBindableControl ) );
     277           0 :                 m_aPropertyListeners.addListener( xEventSourceTranslator );
     278           0 :                 impl_switchBindingListening_throw( true, xEventSourceTranslator );
     279             :             }
     280             :             else
     281             :             {
     282             :                 SAL_WNODEPRECATED_DECLARATIONS_PUSH
     283           0 :                 ::std::auto_ptr< ::cppu::OInterfaceIteratorHelper > pListenerIterator = m_aPropertyListeners.createIterator();
     284             :                 SAL_WNODEPRECATED_DECLARATIONS_POP
     285           0 :                 while ( pListenerIterator->hasMoreElements() )
     286             :                 {
     287           0 :                     Reference< XPropertyChangeListener > xListener( pListenerIterator->next(), UNO_QUERY );
     288           0 :                     impl_switchBindingListening_throw( true, xListener );
     289           0 :                 }
     290             :             }
     291             :         }
     292           0 :     }
     293             : 
     294             :     //--------------------------------------------------------------------
     295           0 :     void EFormsHelper::revokeBindingListener( const Reference< XPropertyChangeListener >& _rxBindingListener )
     296             :     {
     297           0 :         impl_toggleBindingPropertyListening_throw( false, _rxBindingListener );
     298           0 :     }
     299             : 
     300             :     //--------------------------------------------------------------------
     301           0 :     void EFormsHelper::getFormModelNames( ::std::vector< ::rtl::OUString >& /* [out] */ _rModelNames ) const SAL_THROW(())
     302             :     {
     303           0 :         if ( m_xDocument.is() )
     304             :         {
     305             :             try
     306             :             {
     307           0 :                 _rModelNames.resize( 0 );
     308             : 
     309           0 :                 Reference< XNameContainer > xForms( m_xDocument->getXForms() );
     310             :                 OSL_ENSURE( xForms.is(), "EFormsHelper::getFormModelNames: invalid forms container!" );
     311           0 :                 if ( xForms.is() )
     312             :                 {
     313           0 :                     Sequence< ::rtl::OUString > aModelNames = xForms->getElementNames();
     314           0 :                     _rModelNames.resize( aModelNames.getLength() );
     315           0 :                     ::std::copy( aModelNames.getConstArray(), aModelNames.getConstArray() + aModelNames.getLength(),
     316             :                         _rModelNames.begin()
     317           0 :                     );
     318           0 :                 }
     319             :             }
     320           0 :             catch( const Exception& )
     321             :             {
     322             :                 OSL_FAIL( "EFormsHelper::getFormModelNames: caught an exception!" );
     323             :             }
     324             :         }
     325           0 :     }
     326             : 
     327             :     //--------------------------------------------------------------------
     328           0 :     void EFormsHelper::getBindingNames( const ::rtl::OUString& _rModelName, ::std::vector< ::rtl::OUString >& /* [out] */ _rBindingNames ) const SAL_THROW(())
     329             :     {
     330           0 :         _rBindingNames.resize( 0 );
     331             :         try
     332             :         {
     333           0 :             Reference< xforms::XModel > xModel( getFormModelByName( _rModelName ) );
     334           0 :             if ( xModel.is() )
     335             :             {
     336           0 :                 Reference< XNameAccess > xBindings( xModel->getBindings(), UNO_QUERY );
     337             :                 OSL_ENSURE( xBindings.is(), "EFormsHelper::getBindingNames: invalid bindings container obtained from the model!" );
     338           0 :                 if ( xBindings.is() )
     339             :                 {
     340           0 :                     Sequence< ::rtl::OUString > aNames = xBindings->getElementNames();
     341           0 :                     _rBindingNames.resize( aNames.getLength() );
     342           0 :                     ::std::copy( aNames.getConstArray(), aNames.getConstArray() + aNames.getLength(), _rBindingNames.begin() );
     343           0 :                 }
     344           0 :             }
     345             :         }
     346           0 :         catch( const Exception& )
     347             :         {
     348             :             OSL_FAIL( "EFormsHelper::getBindingNames: caught an exception!" );
     349             :         }
     350           0 :     }
     351             : 
     352             :     //--------------------------------------------------------------------
     353           0 :     Reference< xforms::XModel > EFormsHelper::getFormModelByName( const ::rtl::OUString& _rModelName ) const SAL_THROW(())
     354             :     {
     355           0 :         Reference< xforms::XModel > xReturn;
     356             :         try
     357             :         {
     358           0 :             Reference< XNameContainer > xForms( m_xDocument->getXForms() );
     359             :             OSL_ENSURE( xForms.is(), "EFormsHelper::getFormModelByName: invalid forms container!" );
     360           0 :             if ( xForms.is() )
     361           0 :                 OSL_VERIFY( xForms->getByName( _rModelName ) >>= xReturn );
     362             :         }
     363           0 :         catch( const Exception& )
     364             :         {
     365             :             OSL_FAIL( "EFormsHelper::getFormModelByName: caught an exception!" );
     366             :         }
     367           0 :         return xReturn;
     368             :     }
     369             : 
     370             :     //--------------------------------------------------------------------
     371           0 :     Reference< xforms::XModel > EFormsHelper::getCurrentFormModel() const SAL_THROW(())
     372             :     {
     373           0 :         Reference< xforms::XModel > xModel;
     374             :         try
     375             :         {
     376           0 :             Reference< XPropertySet > xBinding( getCurrentBinding() );
     377           0 :             if ( xBinding.is() )
     378             :             {
     379           0 :                 OSL_VERIFY( xBinding->getPropertyValue( PROPERTY_MODEL ) >>= xModel );
     380           0 :             }
     381             :         }
     382           0 :         catch( const Exception& )
     383             :         {
     384             :             OSL_FAIL( "EFormsHelper::getCurrentFormModel: caught an exception!" );
     385             :         }
     386           0 :         return xModel;
     387             :     }
     388             : 
     389             :     //--------------------------------------------------------------------
     390           0 :     ::rtl::OUString EFormsHelper::getCurrentFormModelName() const SAL_THROW(())
     391             :     {
     392           0 :         ::rtl::OUString sModelName;
     393             :         try
     394             :         {
     395           0 :             Reference< xforms::XModel > xFormsModel( getCurrentFormModel() );
     396           0 :             if ( xFormsModel.is() )
     397           0 :                 sModelName = xFormsModel->getID();
     398             :         }
     399           0 :         catch( const Exception& )
     400             :         {
     401             :             OSL_FAIL( "EFormsHelper::getCurrentFormModel: caught an exception!" );
     402             :         }
     403           0 :         return sModelName;
     404             :     }
     405             : 
     406             :     //--------------------------------------------------------------------
     407           0 :     Reference< XPropertySet > EFormsHelper::getCurrentBinding() const SAL_THROW(())
     408             :     {
     409           0 :         Reference< XPropertySet > xBinding;
     410             : 
     411             :         try
     412             :         {
     413           0 :             if ( m_xBindableControl.is() )
     414           0 :                 xBinding = xBinding.query( m_xBindableControl->getValueBinding() );
     415             :         }
     416           0 :         catch( const Exception& )
     417             :         {
     418             :             OSL_FAIL( "EFormsHelper::getCurrentBinding: caught an exception!" );
     419             :         }
     420             : 
     421           0 :         return xBinding;
     422             :     }
     423             : 
     424             :     //--------------------------------------------------------------------
     425           0 :     ::rtl::OUString EFormsHelper::getCurrentBindingName() const SAL_THROW(())
     426             :     {
     427           0 :         ::rtl::OUString sBindingName;
     428             :         try
     429             :         {
     430           0 :             Reference< XPropertySet > xBinding( getCurrentBinding() );
     431           0 :             if ( xBinding.is() )
     432           0 :                 xBinding->getPropertyValue( PROPERTY_BINDING_ID ) >>= sBindingName;
     433             :         }
     434           0 :         catch( const Exception& )
     435             :         {
     436             :             OSL_FAIL( "EFormsHelper::getCurrentBindingName: caught an exception!" );
     437             :         }
     438           0 :         return sBindingName;
     439             :     }
     440             : 
     441             :     //--------------------------------------------------------------------
     442           0 :     Reference< XListEntrySource > EFormsHelper::getCurrentListSourceBinding() const SAL_THROW(())
     443             :     {
     444           0 :         Reference< XListEntrySource > xReturn;
     445             :         try
     446             :         {
     447           0 :             Reference< XListEntrySink > xAsSink( m_xControlModel, UNO_QUERY );
     448             :             OSL_ENSURE( xAsSink.is(), "EFormsHelper::getCurrentListSourceBinding: you should have used isListEntrySink before!" );
     449           0 :             if ( xAsSink.is() )
     450           0 :                 xReturn = xAsSink->getListEntrySource();
     451             :         }
     452           0 :         catch( const Exception& )
     453             :         {
     454             :             OSL_FAIL( "EFormsHelper::getCurrentListSourceBinding: caught an exception!" );
     455             :         }
     456           0 :         return xReturn;
     457             :     }
     458             : 
     459             :     //--------------------------------------------------------------------
     460           0 :     void EFormsHelper::setListSourceBinding( const Reference< XListEntrySource >& _rxListSource ) SAL_THROW(())
     461             :     {
     462             :         try
     463             :         {
     464           0 :             Reference< XListEntrySink > xAsSink( m_xControlModel, UNO_QUERY );
     465             :             OSL_ENSURE( xAsSink.is(), "EFormsHelper::setListSourceBinding: you should have used isListEntrySink before!" );
     466           0 :             if ( xAsSink.is() )
     467           0 :                 xAsSink->setListEntrySource( _rxListSource );
     468             :         }
     469           0 :         catch( const Exception& )
     470             :         {
     471             :             OSL_FAIL( "EFormsHelper::setListSourceBinding: caught an exception!" );
     472             :         }
     473           0 :     }
     474             : 
     475             :     //--------------------------------------------------------------------
     476           0 :     void EFormsHelper::setBinding( const Reference< ::com::sun::star::beans::XPropertySet >& _rxBinding ) SAL_THROW(())
     477             :     {
     478           0 :         if ( !m_xBindableControl.is() )
     479           0 :             return;
     480             : 
     481             :         try
     482             :         {
     483           0 :             Reference< XPropertySet > xOldBinding( m_xBindableControl->getValueBinding(), UNO_QUERY );
     484             : 
     485           0 :             Reference< XValueBinding > xBinding( _rxBinding, UNO_QUERY );
     486             :             OSL_ENSURE( xBinding.is() || !_rxBinding.is(), "EFormsHelper::setBinding: invalid binding!" );
     487             : 
     488           0 :             impl_toggleBindingPropertyListening_throw( false, NULL );
     489           0 :             m_xBindableControl->setValueBinding( xBinding );
     490           0 :             impl_toggleBindingPropertyListening_throw( true, NULL );
     491             : 
     492           0 :             ::std::set< ::rtl::OUString > aSet;
     493           0 :             firePropertyChanges( xOldBinding, _rxBinding, aSet );
     494             :         }
     495           0 :         catch( const Exception& )
     496             :         {
     497             :             OSL_FAIL( "EFormsHelper::setBinding: caught an exception!" );
     498             :         }
     499             :     }
     500             : 
     501             :     //--------------------------------------------------------------------
     502           0 :     Reference< XPropertySet > EFormsHelper::getOrCreateBindingForModel( const ::rtl::OUString& _rTargetModel, const ::rtl::OUString& _rBindingName ) const SAL_THROW(())
     503             :     {
     504             :         OSL_ENSURE( !_rBindingName.isEmpty(), "EFormsHelper::getOrCreateBindingForModel: invalid binding name!" );
     505           0 :         return implGetOrCreateBinding( _rTargetModel, _rBindingName );
     506             :     }
     507             : 
     508             :     //--------------------------------------------------------------------
     509           0 :     Reference< XPropertySet > EFormsHelper::implGetOrCreateBinding( const ::rtl::OUString& _rTargetModel, const ::rtl::OUString& _rBindingName ) const SAL_THROW(())
     510             :     {
     511             :         OSL_ENSURE( !( _rTargetModel.isEmpty() && !_rBindingName.isEmpty() ), "EFormsHelper::implGetOrCreateBinding: no model, but a binding name?" );
     512             : 
     513           0 :         Reference< XPropertySet > xBinding;
     514             :         try
     515             :         {
     516           0 :             ::rtl::OUString sTargetModel( _rTargetModel );
     517             :             // determine the model which the binding should belong to
     518           0 :             if ( sTargetModel.isEmpty() )
     519             :             {
     520           0 :                 ::std::vector< ::rtl::OUString > aModelNames;
     521           0 :                 getFormModelNames( aModelNames );
     522           0 :                 if ( !aModelNames.empty() )
     523           0 :                     sTargetModel = *aModelNames.begin();
     524           0 :                 OSL_ENSURE( !sTargetModel.isEmpty(), "EFormsHelper::implGetOrCreateBinding: unable to obtain a default model!" );
     525             :             }
     526           0 :             Reference< xforms::XModel > xModel( getFormModelByName( sTargetModel ) );
     527           0 :             Reference< XNameAccess > xBindingNames( xModel.is() ? xModel->getBindings() : Reference< XSet >(), UNO_QUERY );
     528           0 :             if ( xBindingNames.is() )
     529             :             {
     530             :                 // get or create the binding instance
     531           0 :                 if ( !_rBindingName.isEmpty() )
     532             :                 {
     533           0 :                     if ( xBindingNames->hasByName( _rBindingName ) )
     534           0 :                         OSL_VERIFY( xBindingNames->getByName( _rBindingName ) >>= xBinding );
     535             :                     else
     536             :                     {
     537           0 :                         xBinding = xModel->createBinding( );
     538           0 :                         if ( xBinding.is() )
     539             :                         {
     540           0 :                             xBinding->setPropertyValue( PROPERTY_BINDING_ID, makeAny( _rBindingName ) );
     541           0 :                             xModel->getBindings()->insert( makeAny( xBinding ) );
     542             :                         }
     543             :                     }
     544             :                 }
     545             :                 else
     546             :                 {
     547           0 :                     xBinding = xModel->createBinding( );
     548           0 :                     if ( xBinding.is() )
     549             :                     {
     550             :                         // find a nice name for it
     551           0 :                         String sBaseName( PcrRes( RID_STR_BINDING_UI_NAME ) );
     552           0 :                         sBaseName += rtl::OUString(" ");
     553           0 :                         String sNewName;
     554           0 :                         sal_Int32 nNumber = 1;
     555           0 :                         do
     556             :                         {
     557           0 :                             sNewName = sBaseName + ::rtl::OUString::valueOf( nNumber++ );
     558             :                         }
     559           0 :                         while ( xBindingNames->hasByName( sNewName ) );
     560           0 :                         Reference< XNamed > xName( xBinding, UNO_QUERY_THROW );
     561           0 :                         xName->setName( sNewName );
     562             :                         // and insert into the model
     563           0 :                         xModel->getBindings()->insert( makeAny( xBinding ) );
     564             :                     }
     565             :                 }
     566           0 :             }
     567             :         }
     568           0 :         catch( const Exception& )
     569             :         {
     570             :             DBG_UNHANDLED_EXCEPTION();
     571             :         }
     572             : 
     573           0 :         return xBinding;
     574             :     }
     575             : 
     576             :     //--------------------------------------------------------------------
     577             :     namespace
     578             :     {
     579             :         //................................................................
     580             :         struct PropertyBagInserter : public ::std::unary_function< Property, void >
     581             :         {
     582             :         private:
     583             :             PropertyBag& m_rProperties;
     584             : 
     585             :         public:
     586           0 :             PropertyBagInserter( PropertyBag& rProperties ) : m_rProperties( rProperties ) { }
     587             : 
     588           0 :             void operator()( const Property& _rProp )
     589             :             {
     590           0 :                 m_rProperties.insert( _rProp );
     591           0 :             }
     592             :         };
     593             : 
     594             :         //................................................................
     595           0 :         Reference< XPropertySetInfo > collectPropertiesGetInfo( const Reference< XPropertySet >& _rxProps, PropertyBag& _rBag )
     596             :         {
     597           0 :             Reference< XPropertySetInfo > xInfo;
     598           0 :             if ( _rxProps.is() )
     599           0 :                 xInfo = _rxProps->getPropertySetInfo();
     600           0 :             if ( xInfo.is() )
     601             :             {
     602           0 :                 Sequence< Property > aProperties = xInfo->getProperties();
     603           0 :                 ::std::for_each( aProperties.getConstArray(), aProperties.getConstArray() + aProperties.getLength(),
     604             :                     PropertyBagInserter( _rBag )
     605           0 :                 );
     606             :             }
     607           0 :             return xInfo;
     608             :         }
     609             :     }
     610             : 
     611             :     //--------------------------------------------------------------------
     612           0 :     ::rtl::OUString EFormsHelper::getModelElementUIName( const EFormsHelper::ModelElementType _eType, const Reference< XPropertySet >& _rxElement ) const SAL_THROW(())
     613             :     {
     614           0 :         ::rtl::OUString sUIName;
     615             :         try
     616             :         {
     617             :             // determine the model which the element belongs to
     618           0 :             Reference< xforms::XFormsUIHelper1 > xHelper;
     619           0 :             if ( _rxElement.is() )
     620           0 :                 _rxElement->getPropertyValue( PROPERTY_MODEL ) >>= xHelper;
     621             :             OSL_ENSURE( xHelper.is(), "EFormsHelper::getModelElementUIName: invalid element or model!" );
     622           0 :             if ( xHelper.is() )
     623             :             {
     624           0 :                 ::rtl::OUString sElementName = ( _eType == Submission ) ? xHelper->getSubmissionName( _rxElement, sal_True ) : xHelper->getBindingName( _rxElement, sal_True );
     625           0 :                 Reference< xforms::XModel > xModel( xHelper, UNO_QUERY_THROW );
     626           0 :                 sUIName = composeModelElementUIName( xModel->getID(), sElementName );
     627           0 :             }
     628             :         }
     629           0 :         catch( const Exception& )
     630             :         {
     631             :             OSL_FAIL( "EFormsHelper::getModelElementUIName: caught an exception!" );
     632             :         }
     633             : 
     634           0 :         return sUIName;
     635             :     }
     636             : 
     637             :     //--------------------------------------------------------------------
     638           0 :     Reference< XPropertySet > EFormsHelper::getModelElementFromUIName( const EFormsHelper::ModelElementType _eType, const ::rtl::OUString& _rUIName ) const SAL_THROW(())
     639             :     {
     640           0 :         const MapStringToPropertySet& rMapUINameToElement( ( _eType == Submission ) ? m_aSubmissionUINames : m_aBindingUINames );
     641           0 :         MapStringToPropertySet::const_iterator pos = rMapUINameToElement.find( _rUIName );
     642             :         OSL_ENSURE( pos != rMapUINameToElement.end(), "EFormsHelper::getModelElementFromUIName: didn't find it!" );
     643             : 
     644           0 :         return ( pos != rMapUINameToElement.end() ) ? pos->second : Reference< XPropertySet >();
     645             :     }
     646             : 
     647             :     //--------------------------------------------------------------------
     648           0 :     void EFormsHelper::getAllElementUINames( const ModelElementType _eType, ::std::vector< ::rtl::OUString >& /* [out] */ _rElementNames, bool _bPrepentEmptyEntry )
     649             :     {
     650           0 :         MapStringToPropertySet& rMapUINameToElement( ( _eType == Submission ) ? m_aSubmissionUINames : m_aBindingUINames );
     651           0 :         rMapUINameToElement.clear();
     652           0 :         _rElementNames.resize( 0 );
     653             : 
     654           0 :         if ( _bPrepentEmptyEntry )
     655           0 :             rMapUINameToElement[ ::rtl::OUString() ] = Reference< XPropertySet >();
     656             : 
     657             :         try
     658             :         {
     659             :             // obtain the model names
     660           0 :             ::std::vector< ::rtl::OUString > aModels;
     661           0 :             getFormModelNames( aModels );
     662           0 :             _rElementNames.reserve( aModels.size() * 2 );    // heuristics
     663             : 
     664             :             // for every model, obtain the element
     665           0 :             for ( ::std::vector< ::rtl::OUString >::const_iterator pModelName = aModels.begin();
     666           0 :                   pModelName != aModels.end();
     667             :                   ++pModelName
     668             :                 )
     669             :             {
     670           0 :                 Reference< xforms::XModel > xModel = getFormModelByName( *pModelName );
     671             :                 OSL_ENSURE( xModel.is(), "EFormsHelper::getAllElementUINames: inconsistency in the models!" );
     672           0 :                 Reference< xforms::XFormsUIHelper1 > xHelper( xModel, UNO_QUERY );
     673             : 
     674           0 :                 Reference< XIndexAccess > xElements;
     675           0 :                 if ( xModel.is() )
     676           0 :                     xElements = xElements.query( ( _eType == Submission ) ? xModel->getSubmissions() : xModel->getBindings() );
     677           0 :                 if ( !xElements.is() )
     678             :                     break;
     679             : 
     680           0 :                 sal_Int32 nElementCount = xElements->getCount();
     681           0 :                 for ( sal_Int32 i = 0; i < nElementCount; ++i )
     682             :                 {
     683           0 :                     Reference< XPropertySet > xElement( xElements->getByIndex( i ), UNO_QUERY );
     684             :                     OSL_ENSURE( xElement.is(), "EFormsHelper::getAllElementUINames: empty element!" );
     685           0 :                     if ( !xElement.is() )
     686           0 :                         continue;
     687             : #if OSL_DEBUG_LEVEL > 0
     688             :                     {
     689             :                         Reference< xforms::XModel > xElementsModel;
     690             :                         xElement->getPropertyValue( PROPERTY_MODEL ) >>= xElementsModel;
     691             :                         OSL_ENSURE( xElementsModel == xModel, "EFormsHelper::getAllElementUINames: inconsistency in the model-element relationship!" );
     692             :                         if ( !( xElementsModel == xModel ) )
     693             :                             xElement->setPropertyValue( PROPERTY_MODEL, makeAny( xModel ) );
     694             :                     }
     695             : #endif
     696           0 :                     ::rtl::OUString sElementName = ( _eType == Submission ) ? xHelper->getSubmissionName( xElement, sal_True ) : xHelper->getBindingName( xElement, sal_True );
     697           0 :                     ::rtl::OUString sUIName = composeModelElementUIName( *pModelName, sElementName );
     698             : 
     699             :                     OSL_ENSURE( rMapUINameToElement.find( sUIName ) == rMapUINameToElement.end(), "EFormsHelper::getAllElementUINames: duplicate name!" );
     700           0 :                     rMapUINameToElement.insert( MapStringToPropertySet::value_type( sUIName, xElement ) );
     701           0 :                 }
     702           0 :             }
     703             :         }
     704           0 :         catch( const Exception& )
     705             :         {
     706             :             OSL_FAIL( "EFormsHelper::getAllElementUINames: caught an exception!" );
     707             :         }
     708             : 
     709           0 :         _rElementNames.resize( rMapUINameToElement.size() );
     710           0 :         ::std::transform( rMapUINameToElement.begin(), rMapUINameToElement.end(), _rElementNames.begin(), ::o3tl::select1st< MapStringToPropertySet::value_type >() );
     711           0 :     }
     712             : 
     713             :     //--------------------------------------------------------------------
     714           0 :     void EFormsHelper::firePropertyChange( const ::rtl::OUString& _rName, const Any& _rOldValue, const Any& _rNewValue ) const
     715             :     {
     716           0 :         if ( m_aPropertyListeners.empty() )
     717           0 :             return;
     718             : 
     719           0 :         if ( _rOldValue == _rNewValue )
     720           0 :             return;
     721             : 
     722             :         try
     723             :         {
     724           0 :             PropertyChangeEvent aEvent;
     725             : 
     726           0 :             aEvent.Source = m_xBindableControl.get();
     727           0 :             aEvent.PropertyName = _rName;
     728           0 :             aEvent.OldValue = _rOldValue;
     729           0 :             aEvent.NewValue = _rNewValue;
     730             : 
     731           0 :             const_cast< EFormsHelper* >( this )->m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
     732             :         }
     733           0 :         catch( const Exception& )
     734             :         {
     735             :             OSL_FAIL( "EFormsHelper::firePropertyChange: caught an exception!" );
     736             :         }
     737             :     }
     738             : 
     739             :     //--------------------------------------------------------------------
     740           0 :     void EFormsHelper::firePropertyChanges( const Reference< XPropertySet >& _rxOldProps, const Reference< XPropertySet >& _rxNewProps, ::std::set< ::rtl::OUString >& _rFilter ) const
     741             :     {
     742           0 :         if ( m_aPropertyListeners.empty() )
     743           0 :             return;
     744             : 
     745             :         try
     746             :         {
     747           0 :             PropertyBag aProperties;
     748           0 :             Reference< XPropertySetInfo > xOldInfo = collectPropertiesGetInfo( _rxOldProps, aProperties );
     749           0 :             Reference< XPropertySetInfo > xNewInfo = collectPropertiesGetInfo( _rxNewProps, aProperties );
     750             : 
     751           0 :             for ( PropertyBag::const_iterator aProp = aProperties.begin();
     752           0 :                   aProp != aProperties.end();
     753             :                   ++aProp
     754             :                 )
     755             :             {
     756           0 :                 if ( _rFilter.find( aProp->Name ) != _rFilter.end() )
     757           0 :                     continue;
     758             : 
     759           0 :                 Any aOldValue( NULL, aProp->Type );
     760           0 :                 if ( xOldInfo.is() && xOldInfo->hasPropertyByName( aProp->Name ) )
     761           0 :                     aOldValue = _rxOldProps->getPropertyValue( aProp->Name );
     762             : 
     763           0 :                 Any aNewValue( NULL, aProp->Type );
     764           0 :                 if ( xNewInfo.is() && xNewInfo->hasPropertyByName( aProp->Name ) )
     765           0 :                     aNewValue = _rxNewProps->getPropertyValue( aProp->Name );
     766             : 
     767           0 :                 firePropertyChange( aProp->Name, aOldValue, aNewValue );
     768           0 :             }
     769             :         }
     770           0 :         catch( const Exception& )
     771             :         {
     772             :             OSL_FAIL( "EFormsHelper::firePropertyChanges: caught an exception!" );
     773             :         }
     774             :     }
     775             : 
     776             : //........................................................................
     777             : } // namespace pcr
     778             : //........................................................................
     779             : 
     780             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10