merge the formfield patch from ooo-build
[ooovba.git] / framework / source / helper / ocomponentaccess.cxx
blob274f3cdd6b04d228bd90aa71c3e3064acf89498b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ocomponentaccess.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
37 #include <helper/ocomponentaccess.hxx>
38 #include <helper/ocomponentenumeration.hxx>
40 #ifndef _FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
41 #include <threadhelp/resetableguard.hxx>
42 #endif
44 //_________________________________________________________________________________________________________________
45 // interface includes
46 //_________________________________________________________________________________________________________________
47 #include <com/sun/star/frame/FrameSearchFlag.hpp>
49 //_________________________________________________________________________________________________________________
50 // includes of other projects
51 //_________________________________________________________________________________________________________________
52 #include <vcl/svapp.hxx>
54 //_________________________________________________________________________________________________________________
55 // namespace
56 //_________________________________________________________________________________________________________________
58 namespace framework{
60 using namespace ::com::sun::star::container ;
61 using namespace ::com::sun::star::frame ;
62 using namespace ::com::sun::star::lang ;
63 using namespace ::com::sun::star::uno ;
64 using namespace ::cppu ;
65 using namespace ::osl ;
66 using namespace ::rtl ;
68 //_________________________________________________________________________________________________________________
69 // non exported const
70 //_________________________________________________________________________________________________________________
72 //_________________________________________________________________________________________________________________
73 // non exported definitions
74 //_________________________________________________________________________________________________________________
76 //_________________________________________________________________________________________________________________
77 // declarations
78 //_________________________________________________________________________________________________________________
80 //*****************************************************************************************************************
81 // constructor
82 //*****************************************************************************************************************
83 OComponentAccess::OComponentAccess( const Reference< XDesktop >& xOwner )
84 // Init baseclasses first
85 : ThreadHelpBase ( &Application::GetSolarMutex() )
86 // Init member
87 , m_xOwner ( xOwner )
89 // Safe impossible cases
90 LOG_ASSERT( impldbg_checkParameter_OComponentAccessCtor( xOwner ), "OComponentAccess::OComponentAccess()\nInvalid parameter detected!\n" )
93 //*****************************************************************************************************************
94 // destructor
95 //*****************************************************************************************************************
96 OComponentAccess::~OComponentAccess()
100 //*****************************************************************************************************************
101 // XEnumerationAccess
102 //*****************************************************************************************************************
103 Reference< XEnumeration > SAL_CALL OComponentAccess::createEnumeration() throw( RuntimeException )
105 // Ready for multithreading
106 ResetableGuard aGuard( m_aLock );
108 // Set default return value, if method failed.
109 // If no desktop exist and there is no task container - return an empty enumeration!
110 Reference< XEnumeration > xReturn = Reference< XEnumeration >();
112 // Try to "lock" the desktop for access to task container.
113 Reference< XInterface > xLock = m_xOwner.get();
114 if ( xLock.is() == sal_True )
116 // Desktop exist => pointer to task container must be valid.
117 // Initialize a new enumeration ... if some tasks and his components exist!
118 // (OTasksEnumeration will make an assert, if we initialize the new instance without valid values!)
120 Sequence< Reference< XComponent > > seqComponents;
121 impl_collectAllChildComponents( Reference< XFramesSupplier >( xLock, UNO_QUERY ), seqComponents );
122 OComponentEnumeration* pEnumeration = new OComponentEnumeration( seqComponents );
123 xReturn = Reference< XEnumeration >( (OWeakObject*)pEnumeration, UNO_QUERY );
126 // Return result of this operation.
127 return xReturn;
130 //*****************************************************************************************************************
131 // XElementAccess
132 //*****************************************************************************************************************
133 Type SAL_CALL OComponentAccess::getElementType() throw( RuntimeException )
135 // Elements in list an enumeration are components!
136 // Return the uno-type of XComponent.
137 return ::getCppuType((const Reference< XComponent >*)NULL);
140 //*****************************************************************************************************************
141 // XElementAccess
142 //*****************************************************************************************************************
143 sal_Bool SAL_CALL OComponentAccess::hasElements() throw( RuntimeException )
145 // Ready for multithreading
146 ResetableGuard aGuard( m_aLock );
148 // Set default return value, if method failed.
149 sal_Bool bReturn = sal_False;
151 // Try to "lock" the desktop for access to task container.
152 Reference< XFramesSupplier > xLock( m_xOwner.get(), UNO_QUERY );
153 if ( xLock.is() == sal_True )
155 // Ask container of owner for existing elements.
156 bReturn = xLock->getFrames()->hasElements();
159 // Return result of this operation.
160 return bReturn;
163 //*****************************************************************************************************************
164 // private method
165 //*****************************************************************************************************************
166 void OComponentAccess::impl_collectAllChildComponents( const Reference< XFramesSupplier >& xNode ,
167 Sequence< Reference< XComponent > >& seqComponents )
169 // If valid node was given ...
170 if( xNode.is() == sal_True )
172 // ... continue collection at these.
174 // Get the container of current node, collect the components of existing child frames
175 // and go down to next level in tree (recursive!).
177 sal_Int32 nComponentCount = seqComponents.getLength();
179 const Reference< XFrames > xContainer = xNode->getFrames();
180 const Sequence< Reference< XFrame > > seqFrames = xContainer->queryFrames( FrameSearchFlag::CHILDREN );
182 const sal_Int32 nFrameCount = seqFrames.getLength();
183 for( sal_Int32 nFrame=0; nFrame<nFrameCount; ++nFrame )
185 Reference< XComponent > xComponent = impl_getFrameComponent( seqFrames[nFrame] );
186 if( xComponent.is() == sal_True )
188 nComponentCount++;
189 seqComponents.realloc( nComponentCount );
190 seqComponents[nComponentCount-1] = xComponent;
194 // ... otherwise break a recursive path and go back at current stack!
197 //*****************************************************************************************************************
198 // private method
199 //*****************************************************************************************************************
200 Reference< XComponent > OComponentAccess::impl_getFrameComponent( const Reference< XFrame >& xFrame ) const
202 // Set default return value, if method failed.
203 Reference< XComponent > xComponent = Reference< XComponent >();
204 // Does no controller exists?
205 Reference< XController > xController = xFrame->getController();
206 if ( xController.is() == sal_False )
208 // Controller not exist - use the VCL-component.
209 xComponent = Reference< XComponent >( xFrame->getComponentWindow(), UNO_QUERY );
211 else
213 // Does no model exists?
214 Reference< XModel > xModel( xController->getModel(), UNO_QUERY );
215 if ( xModel.is() == sal_True )
217 // Model exist - use the model as component.
218 xComponent = Reference< XComponent >( xModel, UNO_QUERY );
220 else
222 // Model not exist - use the controller as component.
223 xComponent = Reference< XComponent >( xController, UNO_QUERY );
227 return xComponent;
230 //_________________________________________________________________________________________________________________
231 // debug methods
232 //_________________________________________________________________________________________________________________
234 /*-----------------------------------------------------------------------------------------------------------------
235 The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
236 we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
238 ATTENTION
240 If you miss a test for one of this parameters, contact the autor or add it himself !(?)
241 But ... look for right testing! See using of this methods!
242 -----------------------------------------------------------------------------------------------------------------*/
244 #ifdef ENABLE_ASSERTIONS
246 //*****************************************************************************************************************
247 sal_Bool OComponentAccess::impldbg_checkParameter_OComponentAccessCtor( const Reference< XDesktop >& xOwner )
249 // Set default return value.
250 sal_Bool bOK = sal_True;
251 // Check parameter.
252 if (
253 ( &xOwner == NULL ) ||
254 ( xOwner.is() == sal_False )
257 bOK = sal_False ;
259 // Return result of check.
260 return bOK ;
263 #endif // #ifdef ENABLE_ASSERTIONS
265 } // namespace framework