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