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