LCOV - code coverage report
Current view: top level - libreoffice/svx/source/form - fmpgeimp.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 35 294 11.9 %
Date: 2012-12-27 Functions: 5 24 20.8 %
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             : 
      21             : #include "svx/svxerr.hxx"
      22             : #include "fmpgeimp.hxx"
      23             : #include "fmundo.hxx"
      24             : #include "svx/fmtools.hxx"
      25             : #include "fmprop.hrc"
      26             : #include "fmservs.hxx"
      27             : #include "fmobj.hxx"
      28             : #include "formcontrolfactory.hxx"
      29             : #include "svx/svditer.hxx"
      30             : #include "svx/fmresids.hrc"
      31             : #include "svx/dbtoolsclient.hxx"
      32             : #include "treevisitor.hxx"
      33             : 
      34             : #include <com/sun/star/sdb/CommandType.hpp>
      35             : #include <com/sun/star/util/XCloneable.hpp>
      36             : #include <com/sun/star/container/EnumerableMap.hpp>
      37             : #include <com/sun/star/drawing/XControlShape.hpp>
      38             : 
      39             : #include <sfx2/objsh.hxx>
      40             : #include <svx/fmglob.hxx>
      41             : #include <svx/fmpage.hxx>
      42             : #include <svx/fmmodel.hxx>
      43             : #include <tools/resid.hxx>
      44             : #include <tools/diagnose_ex.h>
      45             : #include <tools/shl.hxx>
      46             : #include <vcl/stdtext.hxx>
      47             : #include <svx/dialmgr.hxx>
      48             : #include <comphelper/processfactory.hxx>
      49             : #include <comphelper/uno3.hxx>
      50             : #include <comphelper/types.hxx>
      51             : #include <unotools/streamwrap.hxx>
      52             : #include <rtl/logfile.hxx>
      53             : 
      54             : using namespace ::com::sun::star::uno;
      55             : using namespace ::com::sun::star::lang;
      56             : using namespace ::com::sun::star::sdbc;
      57             : using namespace ::com::sun::star::sdb;
      58             : using namespace ::com::sun::star::container;
      59             : using namespace ::com::sun::star::beans;
      60             : using namespace ::com::sun::star::form;
      61             : using ::com::sun::star::util::XCloneable;
      62             : using ::com::sun::star::awt::XControlModel;
      63             : using ::com::sun::star::container::XMap;
      64             : using ::com::sun::star::container::EnumerableMap;
      65             : using ::com::sun::star::drawing::XControlShape;
      66             : using namespace ::svxform;
      67             : 
      68             : DBG_NAME(FmFormPageImpl)
      69             : //------------------------------------------------------------------------------
      70         512 : FmFormPageImpl::FmFormPageImpl( FmFormPage& _rPage )
      71             :                :m_rPage( _rPage )
      72             :                ,m_bFirstActivation( sal_True )
      73             :                ,m_bAttemptedFormCreation( false )
      74         512 :                ,m_bInFind( false )
      75             : {
      76             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::FmFormPageImpl" );
      77             :     DBG_CTOR(FmFormPageImpl,NULL);
      78         512 : }
      79             : 
      80             : //------------------------------------------------------------------------------
      81             : namespace
      82             : {
      83             :     typedef Reference< XInterface > FormComponent;
      84             : 
      85             :     class FormComponentInfo
      86             :     {
      87             :     public:
      88           0 :         size_t childCount( const FormComponent& _component ) const
      89             :         {
      90           0 :             Reference< XIndexAccess > xContainer( _component, UNO_QUERY );
      91           0 :             if ( xContainer.is() )
      92           0 :                 return xContainer->getCount();
      93           0 :             return 0;
      94             :         }
      95             : 
      96           0 :         FormComponent getChild( const FormComponent& _component, size_t _index ) const
      97             :         {
      98           0 :             Reference< XIndexAccess > xContainer( _component, UNO_QUERY_THROW );
      99           0 :             return FormComponent( xContainer->getByIndex( _index ), UNO_QUERY );
     100             :         }
     101             :     };
     102             : 
     103             :     typedef ::std::pair< FormComponent, FormComponent > FormComponentPair;
     104             : 
     105             :     class FormHierarchyComparator
     106             :     {
     107             :     public:
     108           0 :         FormHierarchyComparator()
     109             :         {
     110           0 :         }
     111             : 
     112           0 :         size_t childCount( const FormComponentPair& _components ) const
     113             :         {
     114           0 :             size_t lhsCount = m_aComponentInfo.childCount( _components.first );
     115           0 :             size_t rhsCount = m_aComponentInfo.childCount( _components.second );
     116           0 :             if  ( lhsCount != rhsCount )
     117           0 :                 throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Found inconsistent form component hierarchies (1)!" ) ), NULL );
     118           0 :             return lhsCount;
     119             :         }
     120             : 
     121           0 :         FormComponentPair getChild( const FormComponentPair& _components, size_t _index ) const
     122             :         {
     123             :             return FormComponentPair(
     124             :                 m_aComponentInfo.getChild( _components.first, _index ),
     125             :                 m_aComponentInfo.getChild( _components.second, _index )
     126           0 :             );
     127             :         }
     128             :     private:
     129             :         FormComponentInfo   m_aComponentInfo;
     130             :     };
     131             : 
     132             :     typedef ::std::map< Reference< XControlModel >, Reference< XControlModel >, ::comphelper::OInterfaceCompare< XControlModel > > MapControlModels;
     133             : 
     134             :     class FormComponentAssignment
     135             :     {
     136             :     public:
     137           0 :         FormComponentAssignment( MapControlModels& _out_controlModelMap )
     138           0 :             :m_rControlModelMap( _out_controlModelMap )
     139             :         {
     140           0 :         }
     141             : 
     142           0 :         void    process( const FormComponentPair& _component )
     143             :         {
     144           0 :             Reference< XControlModel > lhsControlModel( _component.first, UNO_QUERY );
     145           0 :             Reference< XControlModel > rhsControlModel( _component.second, UNO_QUERY );
     146           0 :             if ( lhsControlModel.is() != rhsControlModel.is() )
     147           0 :                 throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Found inconsistent form component hierarchies (2)!" ) ), NULL );
     148             : 
     149           0 :             if ( lhsControlModel.is() )
     150           0 :                 m_rControlModelMap[ lhsControlModel ] = rhsControlModel;
     151           0 :         }
     152             : 
     153             :     private:
     154             :         MapControlModels&   m_rControlModelMap;
     155             :     };
     156             : }
     157             : 
     158             : //------------------------------------------------------------------------------
     159           0 : void FmFormPageImpl::initFrom( FmFormPageImpl& i_foreignImpl )
     160             : {
     161             :     // clone the Forms collection
     162           0 :     const Reference< XNameContainer > xForeignForms( const_cast< FmFormPageImpl& >( i_foreignImpl ).getForms( false ) );
     163           0 :     const Reference< XCloneable > xCloneable( xForeignForms, UNO_QUERY );
     164           0 :     if ( !xCloneable.is() )
     165             :     {
     166             :         // great, nothing to do
     167             :         OSL_ENSURE( !xForeignForms.is(), "FmFormPageImpl::FmFormPageImpl: a non-cloneable forms container!?" );
     168           0 :         return;
     169             :     }
     170             : 
     171             :     try
     172             :     {
     173           0 :         m_xForms.set( xCloneable->createClone(), UNO_QUERY_THROW );
     174             : 
     175             :         // create a mapping between the original control models and their clones
     176           0 :         MapControlModels aModelAssignment;
     177             : 
     178             :         typedef TreeVisitor< FormComponentPair, FormHierarchyComparator, FormComponentAssignment >   FormComponentVisitor;
     179           0 :         FormComponentVisitor aVisitor = FormComponentVisitor( FormHierarchyComparator() );
     180             : 
     181           0 :         FormComponentAssignment aAssignmentProcessor( aModelAssignment );
     182           0 :         aVisitor.process( FormComponentPair( xCloneable, m_xForms ), aAssignmentProcessor );
     183             : 
     184             :         // assign the cloned models to their SdrObjects
     185           0 :         SdrObjListIter aForeignIter( i_foreignImpl.m_rPage );
     186           0 :         SdrObjListIter aOwnIter( m_rPage );
     187             : 
     188             :         OSL_ENSURE( aForeignIter.IsMore() == aOwnIter.IsMore(), "FmFormPageImpl::FmFormPageImpl: inconsistent number of objects (1)!" );
     189           0 :         while ( aForeignIter.IsMore() && aOwnIter.IsMore() )
     190             :         {
     191           0 :             FmFormObj* pForeignObj = dynamic_cast< FmFormObj* >( aForeignIter.Next() );
     192           0 :             FmFormObj* pOwnObj = dynamic_cast< FmFormObj* >( aOwnIter.Next() );
     193             : 
     194           0 :             bool bForeignIsForm = pForeignObj && ( pForeignObj->GetObjInventor() == FmFormInventor );
     195           0 :             bool bOwnIsForm = pOwnObj && ( pOwnObj->GetObjInventor() == FmFormInventor );
     196             : 
     197           0 :             if ( bForeignIsForm != bOwnIsForm )
     198             :             {
     199             :                 // if this fires, don't attempt to do further assignments, something's completely messed up
     200             :                 SAL_WARN( "svx.form", "FmFormPageImpl::FmFormPageImpl: inconsistent ordering of objects!" );
     201             :                 break;
     202             :             }
     203             : 
     204           0 :             if ( !bForeignIsForm )
     205             :                 // no form control -> next round
     206           0 :                 continue;
     207             : 
     208           0 :             Reference< XControlModel > xForeignModel( pForeignObj->GetUnoControlModel() );
     209           0 :             if ( !xForeignModel.is() )
     210             :             {
     211             :                 // if this fires, the SdrObject does not have a UNO Control Model. This is pathological, but well ...
     212             :                 // So the cloned SdrObject will also not have a UNO Control Model.
     213             :                 SAL_WARN( "svx.form", "FmFormPageImpl::FmFormPageImpl: control shape without control!" );
     214           0 :                 continue;
     215             :             }
     216             : 
     217           0 :             MapControlModels::const_iterator assignment = aModelAssignment.find( xForeignModel );
     218           0 :             if ( assignment == aModelAssignment.end() )
     219             :             {
     220             :                 // if this fires, the source SdrObject has a model, but it is not part of the model hierarchy in
     221             :                 // i_foreignImpl.getForms().
     222             :                 // Pathological, too ...
     223             :                 SAL_WARN( "svx.form", "FmFormPageImpl::FmFormPageImpl: no clone found for this model!" );
     224           0 :                 continue;
     225             :             }
     226             : 
     227           0 :             pOwnObj->SetUnoControlModel( assignment->second );
     228           0 :         }
     229           0 :         OSL_ENSURE( aForeignIter.IsMore() == aOwnIter.IsMore(), "FmFormPageImpl::FmFormPageImpl: inconsistent number of objects (2)!" );
     230             :     }
     231           0 :     catch( const Exception& )
     232             :     {
     233             :         DBG_UNHANDLED_EXCEPTION();
     234           0 :     }
     235             : }
     236             : 
     237             : //------------------------------------------------------------------------------
     238           0 : Reference< XMap > FmFormPageImpl::getControlToShapeMap()
     239             : {
     240           0 :     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
     241           0 :     if ( xControlShapeMap.is() )
     242           0 :         return xControlShapeMap;
     243             : 
     244           0 :     xControlShapeMap = impl_createControlShapeMap_nothrow();
     245           0 :     m_aControlShapeMap = xControlShapeMap;
     246           0 :     return xControlShapeMap;
     247             : }
     248             : 
     249             : //------------------------------------------------------------------------------
     250             : namespace
     251             : {
     252           0 :     static void lcl_insertFormObject_throw( const FmFormObj& _object, const Reference< XMap >& _map )
     253             :     {
     254             :         // the control model
     255           0 :         Reference< XControlModel > xControlModel( _object.GetUnoControlModel(), UNO_QUERY );
     256             :         OSL_ENSURE( xControlModel.is(), "lcl_insertFormObject_throw: suspicious: no control model!" );
     257           0 :         if ( !xControlModel.is() )
     258             :             return;
     259             : 
     260           0 :         Reference< XControlShape > xControlShape( const_cast< FmFormObj& >( _object ).getUnoShape(), UNO_QUERY );
     261             :         OSL_ENSURE( xControlShape.is(), "lcl_insertFormObject_throw: suspicious: no control shape!" );
     262           0 :         if ( !xControlShape.is() )
     263             :             return;
     264             : 
     265           0 :         _map->put( makeAny( xControlModel ), makeAny( xControlShape ) );
     266             :     }
     267             : 
     268           0 :     static void lcl_removeFormObject_throw( const FmFormObj& _object, const Reference< XMap >& _map, bool i_ignoreNonExistence = false )
     269             :     {
     270             :         // the control model
     271           0 :         Reference< XControlModel > xControlModel( _object.GetUnoControlModel(), UNO_QUERY );
     272             :         OSL_ENSURE( xControlModel.is(), "lcl_removeFormObject: suspicious: no control model!" );
     273           0 :         if ( !xControlModel.is() )
     274           0 :             return;
     275             : 
     276             :     #if OSL_DEBUG_LEVEL > 0
     277             :         Any aOldAssignment =
     278             :     #endif
     279           0 :             _map->remove( makeAny( xControlModel ) );
     280             :     #if OSL_DEBUG_LEVEL > 0
     281             :         (void)aOldAssignment;
     282             :     #endif
     283             :         OSL_ENSURE( !i_ignoreNonExistence ||
     284             :             ( aOldAssignment == makeAny( Reference< XControlShape >( const_cast< FmFormObj& >( _object ).getUnoShape(), UNO_QUERY ) ) ),
     285             :                 "lcl_removeFormObject: map was inconsistent!" );
     286           0 :         (void)i_ignoreNonExistence;
     287             :     }
     288             : }
     289             : 
     290             : //------------------------------------------------------------------------------
     291           0 : Reference< XMap > FmFormPageImpl::impl_createControlShapeMap_nothrow()
     292             : {
     293           0 :     Reference< XMap > xMap;
     294             : 
     295             :     try
     296             :     {
     297             :         xMap.set( EnumerableMap::create( comphelper::getProcessComponentContext(),
     298           0 :             ::cppu::UnoType< XControlModel >::get(),
     299           0 :             ::cppu::UnoType< XControlShape >::get()
     300           0 :         ).get(), UNO_SET_THROW );
     301             : 
     302           0 :         SdrObjListIter aPageIter( m_rPage );
     303           0 :         while ( aPageIter.IsMore() )
     304             :         {
     305             :             // only FmFormObjs are what we're interested in
     306           0 :             FmFormObj* pCurrent = FmFormObj::GetFormObject( aPageIter.Next() );
     307           0 :             if ( !pCurrent )
     308           0 :                 continue;
     309             : 
     310           0 :             lcl_insertFormObject_throw( *pCurrent, xMap );
     311           0 :         }
     312             :     }
     313           0 :     catch( const Exception& )
     314             :     {
     315             :         DBG_UNHANDLED_EXCEPTION();
     316             :     }
     317           0 :     return xMap;
     318             : }
     319             : 
     320             : //------------------------------------------------------------------------------
     321        1450 : const Reference< XNameContainer >& FmFormPageImpl::getForms( bool _bForceCreate )
     322             : {
     323             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::getForms" );
     324        1450 :     if ( m_xForms.is() || !_bForceCreate )
     325        1439 :         return m_xForms;
     326             : 
     327          11 :     if ( !m_bAttemptedFormCreation )
     328             :     {
     329          11 :         m_bAttemptedFormCreation = true;
     330             : 
     331          11 :         const ::rtl::OUString sFormsCollectionServiceName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.form.Forms") );
     332             :         m_xForms = Reference< XNameContainer > (
     333          22 :             ::comphelper::getProcessServiceFactory()->createInstance( sFormsCollectionServiceName ),
     334             :             UNO_QUERY
     335          11 :         );
     336             :         DBG_ASSERT( m_xForms.is(), "FmFormPageImpl::getForms: could not create a forms collection!" );
     337             : 
     338          11 :         if ( m_aFormsCreationHdl.IsSet() )
     339             :         {
     340           0 :             m_aFormsCreationHdl.Call( this );
     341             :         }
     342             : 
     343          11 :         FmFormModel* pFormsModel = PTR_CAST( FmFormModel, m_rPage.GetModel() );
     344             : 
     345             :         // give the newly created collection a place in the universe
     346          11 :         Reference< XChild > xAsChild( m_xForms, UNO_QUERY );
     347          11 :         if ( xAsChild.is() )
     348             :         {
     349          10 :             SfxObjectShell* pObjShell = pFormsModel ? pFormsModel->GetObjectShell() : NULL;
     350          10 :             if ( pObjShell )
     351          10 :                 xAsChild->setParent( pObjShell->GetModel() );
     352             :         }
     353             : 
     354             :         // tell the UNDO environment that we have a new forms collection
     355          11 :         if ( pFormsModel )
     356          11 :             pFormsModel->GetUndoEnv().AddForms( m_xForms );
     357             :     }
     358          11 :     return m_xForms;
     359             : }
     360             : 
     361             : //------------------------------------------------------------------------------
     362         638 : FmFormPageImpl::~FmFormPageImpl()
     363             : {
     364         319 :     xCurrentForm = NULL;
     365             : 
     366         319 :     ::comphelper::disposeComponent( m_xForms );
     367             :     DBG_DTOR(FmFormPageImpl,NULL);
     368         319 : }
     369             : 
     370             : //------------------------------------------------------------------------------
     371           0 : bool FmFormPageImpl::validateCurForm()
     372             : {
     373             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::validateCurForm" );
     374           0 :     if ( !xCurrentForm.is() )
     375           0 :         return false;
     376             : 
     377           0 :     Reference< XChild > xAsChild( xCurrentForm, UNO_QUERY );
     378             :     DBG_ASSERT( xAsChild.is(), "FmFormPageImpl::validateCurForm: a form which is no child??" );
     379           0 :     if ( !xAsChild.is() || !xAsChild->getParent().is() )
     380           0 :         xCurrentForm.clear();
     381             : 
     382           0 :     return xCurrentForm.is();
     383             : }
     384             : 
     385             : //------------------------------------------------------------------------------
     386           0 : void FmFormPageImpl::setCurForm(Reference< ::com::sun::star::form::XForm >  xForm)
     387             : {
     388             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::setCurForm" );
     389           0 :     xCurrentForm = xForm;
     390           0 : }
     391             : 
     392             : //------------------------------------------------------------------------------
     393           0 : Reference< XForm >  FmFormPageImpl::getDefaultForm()
     394             : {
     395             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::getDefaultForm" );
     396           0 :     Reference< XForm > xForm;
     397             : 
     398           0 :     Reference< XNameContainer > xForms( getForms() );
     399             : 
     400             :     // by default, we use our "current form"
     401           0 :     if ( !validateCurForm() )
     402             :     {
     403             :         // check whether there is a "standard" form
     404           0 :         if ( xForms->hasElements() )
     405             :         {
     406             :             // suche die Standardform
     407           0 :             ::rtl::OUString sStandardFormname = String( SVX_RES( RID_STR_STDFORMNAME ) );
     408             : 
     409             :             try
     410             :             {
     411           0 :                 if ( xForms->hasByName( sStandardFormname ) )
     412           0 :                     xForm.set( xForms->getByName( sStandardFormname ), UNO_QUERY_THROW );
     413             :                 else
     414             :                 {
     415           0 :                     Reference< XIndexAccess > xFormsByIndex( xForms, UNO_QUERY_THROW );
     416           0 :                     xForm.set( xFormsByIndex->getByIndex(0), UNO_QUERY_THROW );
     417             :                 }
     418             :             }
     419           0 :             catch( const Exception& )
     420             :             {
     421             :                 DBG_UNHANDLED_EXCEPTION();
     422           0 :             }
     423             :         }
     424             :     }
     425             :     else
     426             :     {
     427           0 :         xForm = xCurrentForm;
     428             :     }
     429             : 
     430             :     // did not find an existing suitable form -> create a new one
     431           0 :     if ( !xForm.is() )
     432             :     {
     433           0 :         SdrModel* pModel = m_rPage.GetModel();
     434             : 
     435           0 :         if( pModel->IsUndoEnabled() )
     436             :         {
     437           0 :             XubString aStr(SVX_RES(RID_STR_FORM));
     438           0 :             XubString aUndoStr(SVX_RES(RID_STR_UNDO_CONTAINER_INSERT));
     439           0 :             aUndoStr.SearchAndReplace(rtl::OUString('#'), aStr);
     440           0 :             pModel->BegUndo(aUndoStr);
     441             :         }
     442             : 
     443             :         try
     444             :         {
     445           0 :             xForm.set( ::comphelper::getProcessServiceFactory()->createInstance( FM_SUN_COMPONENT_FORM ), UNO_QUERY );
     446             : 
     447             :             // a form should always have the command type table as default
     448           0 :             Reference< XPropertySet > xFormProps( xForm, UNO_QUERY_THROW );
     449           0 :             xFormProps->setPropertyValue( FM_PROP_COMMANDTYPE, makeAny( sal_Int32( CommandType::TABLE ) ) );
     450             : 
     451             :             // and the "Standard" name
     452           0 :             ::rtl::OUString sName = String( SVX_RES( RID_STR_STDFORMNAME ) );
     453           0 :             xFormProps->setPropertyValue( FM_PROP_NAME, makeAny( sName ) );
     454             : 
     455           0 :             Reference< XIndexContainer > xContainer( xForms, UNO_QUERY );
     456           0 :             if( pModel->IsUndoEnabled() )
     457             :             {
     458             :                 pModel->AddUndo(new FmUndoContainerAction(*(FmFormModel*)pModel,
     459             :                                                            FmUndoContainerAction::Inserted,
     460             :                                                            xContainer,
     461             :                                                            xForm,
     462           0 :                                                            xContainer->getCount()));
     463             :             }
     464           0 :             xForms->insertByName( sName, makeAny( xForm ) );
     465           0 :             xCurrentForm = xForm;
     466             :         }
     467           0 :         catch( const Exception& )
     468             :         {
     469             :             DBG_UNHANDLED_EXCEPTION();
     470           0 :             xForm.clear();
     471             :         }
     472             : 
     473           0 :         if( pModel->IsUndoEnabled() )
     474           0 :             pModel->EndUndo();
     475             :     }
     476             : 
     477           0 :     return xForm;
     478             : }
     479             : 
     480             : //------------------------------------------------------------------------------
     481           0 : Reference< ::com::sun::star::form::XForm >  FmFormPageImpl::findPlaceInFormComponentHierarchy(
     482             :     const Reference< XFormComponent > & rContent, const Reference< XDataSource > & rDatabase,
     483             :     const ::rtl::OUString& rDBTitle, const ::rtl::OUString& rCursorSource, sal_Int32 nCommandType )
     484             : {
     485             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::findPlaceInFormComponentHierarchy" );
     486             :     // if the control already is child of a form, don't do anything
     487           0 :     if (!rContent.is() || rContent->getParent().is())
     488           0 :         return NULL;
     489             : 
     490           0 :     Reference< XForm >  xForm;
     491             : 
     492             :     // Wenn Datenbank und CursorSource gesetzt sind, dann wird
     493             :     // die Form anhand dieser Kriterien gesucht, ansonsten nur aktuelle
     494             :     // und die StandardForm
     495           0 :     if (rDatabase.is() && !rCursorSource.isEmpty())
     496             :     {
     497           0 :         validateCurForm();
     498             : 
     499             :         // erst in der aktuellen form suchen
     500           0 :         xForm = findFormForDataSource( xCurrentForm, rDatabase, rCursorSource, nCommandType );
     501             : 
     502           0 :         Reference< ::com::sun::star::container::XIndexAccess >  xFormsByIndex( getForms(), UNO_QUERY );
     503             :         DBG_ASSERT(xFormsByIndex.is(), "FmFormPageImpl::findPlaceInFormComponentHierarchy : no index access for my forms collection !");
     504           0 :         sal_Int32 nCount = xFormsByIndex->getCount();
     505           0 :         for (sal_Int32 i = 0; !xForm.is() && i < nCount; i++)
     506             :         {
     507           0 :             Reference< ::com::sun::star::form::XForm >  xToSearch;
     508           0 :             xFormsByIndex->getByIndex(i) >>= xToSearch;
     509           0 :             xForm = findFormForDataSource( xToSearch, rDatabase, rCursorSource, nCommandType );
     510           0 :         }
     511             : 
     512             :         // wenn keine ::com::sun::star::form gefunden, dann eine neue erzeugen
     513           0 :         if (!xForm.is())
     514             :         {
     515           0 :             SdrModel* pModel = m_rPage.GetModel();
     516             : 
     517           0 :             const bool bUndo = pModel->IsUndoEnabled();
     518             : 
     519           0 :             if( bUndo )
     520             :             {
     521           0 :                 XubString aStr(SVX_RES(RID_STR_FORM));
     522           0 :                 XubString aUndoStr(SVX_RES(RID_STR_UNDO_CONTAINER_INSERT));
     523           0 :                 aUndoStr.SearchAndReplace(rtl::OUString('#'), aStr);
     524           0 :                 pModel->BegUndo(aUndoStr);
     525             :             }
     526             : 
     527           0 :             xForm = Reference< ::com::sun::star::form::XForm >(::comphelper::getProcessServiceFactory()->createInstance(FM_SUN_COMPONENT_FORM), UNO_QUERY);
     528             :             // a form should always have the command type table as default
     529           0 :             Reference< ::com::sun::star::beans::XPropertySet > xFormProps(xForm, UNO_QUERY);
     530           0 :             try { xFormProps->setPropertyValue(FM_PROP_COMMANDTYPE, makeAny(sal_Int32(CommandType::TABLE))); }
     531           0 :             catch(Exception&) { }
     532             : 
     533           0 :             if (!rDBTitle.isEmpty())
     534           0 :                 xFormProps->setPropertyValue(FM_PROP_DATASOURCE,makeAny(rDBTitle));
     535             :             else
     536             :             {
     537           0 :                 Reference< ::com::sun::star::beans::XPropertySet >  xDatabaseProps(rDatabase, UNO_QUERY);
     538           0 :                 Any aDatabaseUrl = xDatabaseProps->getPropertyValue(FM_PROP_URL);
     539           0 :                 xFormProps->setPropertyValue(FM_PROP_DATASOURCE, aDatabaseUrl);
     540             :             }
     541             : 
     542           0 :             xFormProps->setPropertyValue(FM_PROP_COMMAND,makeAny(rCursorSource));
     543           0 :             xFormProps->setPropertyValue(FM_PROP_COMMANDTYPE, makeAny(nCommandType));
     544             : 
     545           0 :             Reference< ::com::sun::star::container::XNameAccess >  xNamedSet( getForms(), UNO_QUERY );
     546             : 
     547           0 :             const bool bTableOrQuery = ( CommandType::TABLE == nCommandType ) || ( CommandType::QUERY == nCommandType );
     548             :             ::rtl::OUString sName = FormControlFactory::getUniqueName( xNamedSet,
     549           0 :                 bTableOrQuery ? rCursorSource : ::rtl::OUString( String( SVX_RES( RID_STR_STDFORMNAME ) ) ) );
     550             : 
     551           0 :             xFormProps->setPropertyValue( FM_PROP_NAME, makeAny( sName ) );
     552             : 
     553           0 :             if( bUndo )
     554             :             {
     555           0 :                 Reference< ::com::sun::star::container::XIndexContainer >  xContainer( getForms(), UNO_QUERY );
     556             :                 pModel->AddUndo(new FmUndoContainerAction(*(FmFormModel*)pModel,
     557             :                                                          FmUndoContainerAction::Inserted,
     558             :                                                          xContainer,
     559             :                                                          xForm,
     560           0 :                                                          xContainer->getCount()));
     561             :             }
     562             : 
     563           0 :             getForms()->insertByName( sName, makeAny( xForm ) );
     564             : 
     565           0 :             if( bUndo )
     566           0 :                 pModel->EndUndo();
     567             :         }
     568           0 :         xCurrentForm = xForm;
     569             :     }
     570             : 
     571           0 :     xForm = getDefaultForm();
     572           0 :     return xForm;
     573             : }
     574             : 
     575             : //------------------------------------------------------------------------------
     576           0 : Reference< XForm >  FmFormPageImpl::findFormForDataSource(
     577             :         const Reference< XForm > & rForm, const Reference< XDataSource > & _rxDatabase,
     578             :         const ::rtl::OUString& _rCursorSource, sal_Int32 nCommandType)
     579             : {
     580             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "svx", "Ocke.Janssen@sun.com", "FmFormPageImpl::findFormForDataSource" );
     581           0 :     Reference< XForm >          xResultForm;
     582           0 :     Reference< XRowSet >        xDBForm(rForm, UNO_QUERY);
     583           0 :     Reference< XPropertySet >   xFormProps(rForm, UNO_QUERY);
     584           0 :     if (!xDBForm.is() || !xFormProps.is())
     585             :         return xResultForm;
     586             : 
     587             :     OSL_ENSURE(_rxDatabase.is(), "FmFormPageImpl::findFormForDataSource: invalid data source!");
     588           0 :     ::rtl::OUString sLookupName;            // the name of the data source we're looking for
     589           0 :     ::rtl::OUString sFormDataSourceName;    // the name of the data source the current connection in the form is based on
     590             :     try
     591             :     {
     592           0 :         Reference< XPropertySet > xDSProps(_rxDatabase, UNO_QUERY);
     593           0 :         if (xDSProps.is())
     594           0 :             xDSProps->getPropertyValue(FM_PROP_NAME) >>= sLookupName;
     595             : 
     596           0 :         xFormProps->getPropertyValue(FM_PROP_DATASOURCE) >>= sFormDataSourceName;
     597             :         // if there's no DataSourceName set at the form, check whether we can deduce one from its
     598             :         // ActiveConnection
     599           0 :         if (sFormDataSourceName.isEmpty())
     600             :         {
     601           0 :             Reference< XConnection > xFormConnection;
     602           0 :             xFormProps->getPropertyValue( FM_PROP_ACTIVE_CONNECTION ) >>= xFormConnection;
     603           0 :             if ( !xFormConnection.is() )
     604           0 :                 OStaticDataAccessTools().isEmbeddedInDatabase( xFormProps, xFormConnection );
     605           0 :             if (xFormConnection.is())
     606             :             {
     607           0 :                 Reference< XChild > xConnAsChild(xFormConnection, UNO_QUERY);
     608           0 :                 if (xConnAsChild.is())
     609             :                 {
     610           0 :                     Reference< XDataSource > xFormDS(xConnAsChild->getParent(), UNO_QUERY);
     611           0 :                     if (xFormDS.is())
     612             :                     {
     613           0 :                         xDSProps = xDSProps.query(xFormDS);
     614           0 :                         if (xDSProps.is())
     615           0 :                             xDSProps->getPropertyValue(FM_PROP_NAME) >>= sFormDataSourceName;
     616           0 :                     }
     617           0 :                 }
     618           0 :             }
     619           0 :         }
     620             :     }
     621           0 :     catch(const Exception& e)
     622             :     {
     623             :         (void)e;
     624             :         OSL_FAIL("FmFormPageImpl::findFormForDataSource: caught an exception!");
     625             :     }
     626             : 
     627           0 :     if (sLookupName == sFormDataSourceName)
     628             :     {
     629             :         // jetzt noch ueberpruefen ob CursorSource und Type uebereinstimmen
     630           0 :         ::rtl::OUString aCursorSource = ::comphelper::getString(xFormProps->getPropertyValue(FM_PROP_COMMAND));
     631           0 :         sal_Int32 nType = ::comphelper::getINT32(xFormProps->getPropertyValue(FM_PROP_COMMANDTYPE));
     632           0 :         if (aCursorSource.isEmpty() || ((nType == nCommandType) && (aCursorSource == _rCursorSource))) // found the form
     633             :         {
     634           0 :             xResultForm = rForm;
     635             :             // Ist noch keine Datenquelle gesetzt, wird dieses hier nachgeholt
     636           0 :             if (aCursorSource.isEmpty())
     637             :             {
     638           0 :                 xFormProps->setPropertyValue(FM_PROP_COMMAND, makeAny(_rCursorSource));
     639           0 :                 xFormProps->setPropertyValue(FM_PROP_COMMANDTYPE, makeAny((sal_Int32)nCommandType));
     640             :             }
     641           0 :         }
     642             :     }
     643             : 
     644             :     // as long as xResultForm is NULL, search the child forms of rForm
     645           0 :     Reference< XIndexAccess >  xComponents(rForm, UNO_QUERY);
     646           0 :     sal_Int32 nCount = xComponents->getCount();
     647           0 :     for (sal_Int32 i = 0; !xResultForm.is() && i < nCount; ++i)
     648             :     {
     649           0 :         Reference< ::com::sun::star::form::XForm >  xSearchForm;
     650           0 :         xComponents->getByIndex(i) >>= xSearchForm;
     651             :         // continue searching in the sub form
     652           0 :         if (xSearchForm.is())
     653           0 :             xResultForm = findFormForDataSource( xSearchForm, _rxDatabase, _rCursorSource, nCommandType );
     654           0 :     }
     655           0 :     return xResultForm;
     656             : }
     657             : 
     658             : //------------------------------------------------------------------------------
     659           0 : ::rtl::OUString FmFormPageImpl::setUniqueName(const Reference< XFormComponent > & xFormComponent, const Reference< XForm > & xControls)
     660             : {
     661             : #if OSL_DEBUG_LEVEL > 0
     662             :     try
     663             :     {
     664             :         Reference< XChild > xChild( xFormComponent, UNO_QUERY_THROW );
     665             :         OSL_ENSURE( !xChild->getParent().is(), "FmFormPageImpl::setUniqueName: to be called before insertion!" );
     666             :     }
     667             :     catch( const Exception& )
     668             :     {
     669             :         DBG_UNHANDLED_EXCEPTION();
     670             :     }
     671             : #endif
     672           0 :     ::rtl::OUString sName;
     673           0 :     Reference< ::com::sun::star::beans::XPropertySet >  xSet(xFormComponent, UNO_QUERY);
     674           0 :     if (xSet.is())
     675             :     {
     676           0 :         sName = ::comphelper::getString( xSet->getPropertyValue( FM_PROP_NAME ) );
     677           0 :         Reference< ::com::sun::star::container::XNameAccess >  xNameAcc(xControls, UNO_QUERY);
     678             : 
     679           0 :         if (sName.isEmpty() || xNameAcc->hasByName(sName))
     680             :         {
     681             :             // setzen eines default Namens ueber die ClassId
     682           0 :             sal_Int16 nClassId( FormComponentType::CONTROL );
     683           0 :             xSet->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
     684             : 
     685             :             ::rtl::OUString sDefaultName = FormControlFactory::getDefaultUniqueName_ByComponentType(
     686           0 :                 Reference< XNameAccess >( xControls, UNO_QUERY ), xSet );
     687             : 
     688             :             // bei Radiobuttons, die einen Namen haben, diesen nicht ueberschreiben!
     689           0 :             if (sName.isEmpty() || nClassId != ::com::sun::star::form::FormComponentType::RADIOBUTTON)
     690             :             {
     691           0 :                 xSet->setPropertyValue(FM_PROP_NAME, makeAny(sDefaultName));
     692             :             }
     693             : 
     694           0 :             sName = sDefaultName;
     695           0 :         }
     696             :     }
     697           0 :     return sName;
     698             : }
     699             : 
     700             : //----------------------------------------------------------------------------------------------------------------------
     701           1 : void FmFormPageImpl::formModelAssigned( const FmFormObj& _object )
     702             : {
     703           1 :     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
     704           1 :     if ( !xControlShapeMap.is() )
     705             :         // our map does not exist -> not interested in this event
     706           1 :         return;
     707             : 
     708             :     try
     709             :     {
     710           0 :         lcl_removeFormObject_throw( _object,  xControlShapeMap, false );
     711           0 :         lcl_insertFormObject_throw( _object,  xControlShapeMap );
     712             :     }
     713           0 :     catch( const Exception& )
     714             :     {
     715             :         DBG_UNHANDLED_EXCEPTION();
     716           1 :     }
     717             : }
     718             : 
     719             : //----------------------------------------------------------------------------------------------------------------------
     720           9 : void FmFormPageImpl::formObjectInserted( const FmFormObj& _object )
     721             : {
     722           9 :     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
     723           9 :     if ( !xControlShapeMap.is() )
     724             :         // our map does not exist -> not interested in this event
     725           9 :         return;
     726             : 
     727             :     try
     728             :     {
     729           0 :         lcl_insertFormObject_throw( _object,  xControlShapeMap );
     730             :     }
     731           0 :     catch( const Exception& )
     732             :     {
     733             :         DBG_UNHANDLED_EXCEPTION();
     734           9 :     }
     735             : }
     736             : 
     737             : //----------------------------------------------------------------------------------------------------------------------
     738           0 : void FmFormPageImpl::formObjectRemoved( const FmFormObj& _object )
     739             : {
     740           0 :     Reference< XMap > xControlShapeMap( m_aControlShapeMap.get(), UNO_QUERY );
     741           0 :     if ( !xControlShapeMap.is() )
     742             :         // our map does not exist -> not interested in this event
     743           0 :         return;
     744             : 
     745             :     try
     746             :     {
     747           0 :         lcl_removeFormObject_throw( _object, xControlShapeMap );
     748             :     }
     749           0 :     catch( const Exception& )
     750             :     {
     751             :         DBG_UNHANDLED_EXCEPTION();
     752           0 :     }
     753             : }
     754             : 
     755             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10