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_svx.hxx"
26
27 #include "fmcontrolbordermanager.hxx"
28 #include "fmcontrollayout.hxx"
29 #include "formcontroller.hxx"
30 #include "formfeaturedispatcher.hxx"
31 #include "fmdocumentclassification.hxx"
32 #include "formcontrolling.hxx"
33 #include "fmprop.hrc"
34 #include "svx/dialmgr.hxx"
35 #include "svx/fmresids.hrc"
36 #include "fmservs.hxx"
37 #include "svx/fmtools.hxx"
38 #include "fmurl.hxx"
39
40 /** === begin UNO includes === **/
41 #include <com/sun/star/awt/FocusChangeReason.hpp>
42 #include <com/sun/star/awt/XCheckBox.hpp>
43 #include <com/sun/star/awt/XComboBox.hpp>
44 #include <com/sun/star/awt/XListBox.hpp>
45 #include <com/sun/star/awt/XVclWindowPeer.hpp>
46 #include <com/sun/star/beans/NamedValue.hpp>
47 #include <com/sun/star/beans/PropertyAttribute.hpp>
48 #include <com/sun/star/container/XIdentifierReplace.hpp>
49 #include <com/sun/star/form/TabulatorCycle.hpp>
50 #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
51 #include <com/sun/star/form/XBoundComponent.hpp>
52 #include <com/sun/star/form/XBoundControl.hpp>
53 #include <com/sun/star/form/XGridControl.hpp>
54 #include <com/sun/star/form/XLoadable.hpp>
55 #include <com/sun/star/form/XReset.hpp>
56 #include <com/sun/star/frame/XController.hpp>
57 #include <com/sun/star/sdb/ParametersRequest.hpp>
58 #include <com/sun/star/sdb/RowChangeAction.hpp>
59 #include <com/sun/star/sdb/XInteractionSupplyParameters.hpp>
60 #include <com/sun/star/sdbc/ColumnValue.hpp>
61 #include <com/sun/star/sdbc/DataType.hpp>
62 #include <com/sun/star/util/XURLTransformer.hpp>
63 #include <com/sun/star/form/runtime/FormOperations.hpp>
64 #include <com/sun/star/form/runtime/FormFeature.hpp>
65 #include <com/sun/star/container/XContainer.hpp>
66 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
67 #include <com/sun/star/util/XNumberFormatter.hpp>
68 #include <com/sun/star/sdb/SQLContext.hpp>
69 #include <com/sun/star/sdb/XColumn.hpp>
70 /** === end UNO includes === **/
71
72 #include <comphelper/enumhelper.hxx>
73 #include <comphelper/extract.hxx>
74 #include <comphelper/interaction.hxx>
75 #include <comphelper/namedvaluecollection.hxx>
76 #include <comphelper/propagg.hxx>
77 #include <comphelper/property.hxx>
78 #include <comphelper/sequence.hxx>
79 #include <comphelper/uno3.hxx>
80 #include <comphelper/flagguard.hxx>
81 #include <cppuhelper/queryinterface.hxx>
82 #include <cppuhelper/typeprovider.hxx>
83 #include <toolkit/controls/unocontrol.hxx>
84 #include <toolkit/helper/vclunohelper.hxx>
85 #include <tools/debug.hxx>
86 #include <tools/diagnose_ex.h>
87 #include <tools/shl.hxx>
88 #include <vcl/msgbox.hxx>
89 #include <vcl/svapp.hxx>
90 #include <vos/mutex.hxx>
91 #include <rtl/logfile.hxx>
92
93 #include <algorithm>
94 #include <functional>
95 #include <iterator>
96
97 using namespace ::com::sun::star;
98 using namespace ::comphelper;
99 using namespace ::connectivity;
100 using namespace ::connectivity::simple;
101
102 //------------------------------------------------------------------
103 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL
FormController_NewInstance_Impl(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _rxORB)104 FormController_NewInstance_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & _rxORB )
105 {
106 return *( new ::svxform::FormController( _rxORB ) );
107 }
108
109 namespace svxform
110 {
111
112 /** === begin UNO using === **/
113 using ::com::sun::star::sdb::XColumn;
114 using ::com::sun::star::awt::XControl;
115 using ::com::sun::star::awt::XTabController;
116 using ::com::sun::star::awt::XToolkit;
117 using ::com::sun::star::awt::XWindowPeer;
118 using ::com::sun::star::form::XGrid;
119 using ::com::sun::star::beans::XPropertySet;
120 using ::com::sun::star::uno::UNO_SET_THROW;
121 using ::com::sun::star::uno::UNO_QUERY_THROW;
122 using ::com::sun::star::container::XIndexAccess;
123 using ::com::sun::star::uno::Exception;
124 using ::com::sun::star::uno::XInterface;
125 using ::com::sun::star::uno::UNO_QUERY;
126 using ::com::sun::star::uno::Sequence;
127 using ::com::sun::star::uno::Reference;
128 using ::com::sun::star::beans::XPropertySetInfo;
129 using ::com::sun::star::beans::PropertyValue;
130 using ::com::sun::star::uno::RuntimeException;
131 using ::com::sun::star::lang::IndexOutOfBoundsException;
132 using ::com::sun::star::sdb::XInteractionSupplyParameters;
133 using ::com::sun::star::awt::XTextComponent;
134 using ::com::sun::star::awt::XTextListener;
135 using ::com::sun::star::uno::Any;
136 using ::com::sun::star::frame::XDispatch;
137 using ::com::sun::star::lang::XMultiServiceFactory;
138 using ::com::sun::star::uno::XAggregation;
139 using ::com::sun::star::uno::Type;
140 using ::com::sun::star::lang::IllegalArgumentException;
141 using ::com::sun::star::sdbc::XConnection;
142 using ::com::sun::star::sdbc::XRowSet;
143 using ::com::sun::star::sdbc::XDatabaseMetaData;
144 using ::com::sun::star::util::XNumberFormatsSupplier;
145 using ::com::sun::star::util::XNumberFormatter;
146 using ::com::sun::star::sdbcx::XColumnsSupplier;
147 using ::com::sun::star::container::XNameAccess;
148 using ::com::sun::star::lang::EventObject;
149 using ::com::sun::star::beans::Property;
150 using ::com::sun::star::container::XEnumeration;
151 using ::com::sun::star::form::XFormComponent;
152 using ::com::sun::star::form::runtime::XFormOperations;
153 using ::com::sun::star::form::runtime::FilterEvent;
154 using ::com::sun::star::form::runtime::XFilterControllerListener;
155 using ::com::sun::star::awt::XControlContainer;
156 using ::com::sun::star::container::XIdentifierReplace;
157 using ::com::sun::star::lang::WrappedTargetException;
158 using ::com::sun::star::form::XFormControllerListener;
159 using ::com::sun::star::awt::XWindow;
160 using ::com::sun::star::sdbc::XResultSet;
161 using ::com::sun::star::awt::XControlModel;
162 using ::com::sun::star::awt::XTabControllerModel;
163 using ::com::sun::star::beans::PropertyChangeEvent;
164 using ::com::sun::star::form::validation::XValidatableFormComponent;
165 using ::com::sun::star::form::XLoadable;
166 using ::com::sun::star::script::XEventAttacherManager;
167 using ::com::sun::star::form::XBoundControl;
168 using ::com::sun::star::beans::XPropertyChangeListener;
169 using ::com::sun::star::awt::TextEvent;
170 using ::com::sun::star::form::XBoundComponent;
171 using ::com::sun::star::awt::XCheckBox;
172 using ::com::sun::star::awt::XComboBox;
173 using ::com::sun::star::awt::XListBox;
174 using ::com::sun::star::awt::ItemEvent;
175 using ::com::sun::star::util::XModifyListener;
176 using ::com::sun::star::form::XReset;
177 using ::com::sun::star::frame::XDispatchProviderInterception;
178 using ::com::sun::star::form::XGridControl;
179 using ::com::sun::star::awt::XVclWindowPeer;
180 using ::com::sun::star::form::validation::XValidator;
181 using ::com::sun::star::awt::FocusEvent;
182 using ::com::sun::star::sdb::SQLContext;
183 using ::com::sun::star::container::XChild;
184 using ::com::sun::star::form::TabulatorCycle_RECORDS;
185 using ::com::sun::star::container::ContainerEvent;
186 using ::com::sun::star::lang::DisposedException;
187 using ::com::sun::star::lang::Locale;
188 using ::com::sun::star::beans::NamedValue;
189 using ::com::sun::star::lang::NoSupportException;
190 using ::com::sun::star::sdb::RowChangeEvent;
191 using ::com::sun::star::frame::XStatusListener;
192 using ::com::sun::star::frame::XDispatchProviderInterceptor;
193 using ::com::sun::star::sdb::SQLErrorEvent;
194 using ::com::sun::star::form::DatabaseParameterEvent;
195 using ::com::sun::star::sdb::ParametersRequest;
196 using ::com::sun::star::task::XInteractionRequest;
197 using ::com::sun::star::util::URL;
198 using ::com::sun::star::frame::FeatureStateEvent;
199 using ::com::sun::star::form::runtime::XFormControllerContext;
200 using ::com::sun::star::task::XInteractionHandler;
201 using ::com::sun::star::form::runtime::FormOperations;
202 using ::com::sun::star::container::XContainer;
203 using ::com::sun::star::sdbc::SQLWarning;
204 /** === end UNO using === **/
205 namespace ColumnValue = ::com::sun::star::sdbc::ColumnValue;
206 namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
207 namespace FocusChangeReason = ::com::sun::star::awt::FocusChangeReason;
208 namespace RowChangeAction = ::com::sun::star::sdb::RowChangeAction;
209 namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
210 namespace DataType = ::com::sun::star::sdbc::DataType;
211
212 //==============================================================================
213 // ColumnInfo
214 //==============================================================================
215 struct ColumnInfo
216 {
217 // information about the column itself
218 Reference< XColumn > xColumn;
219 sal_Int32 nNullable;
220 sal_Bool bAutoIncrement;
221 sal_Bool bReadOnly;
222 ::rtl::OUString sName;
223
224 // information about the control(s) bound to this column
225
226 /// the first control which is bound to the given column, and which requires input
227 Reference< XControl > xFirstControlWithInputRequired;
228 /** the first grid control which contains a column which is bound to the given database column, and requires
229 input
230 */
231 Reference< XGrid > xFirstGridWithInputRequiredColumn;
232 /** if xFirstControlWithInputRequired is a grid control, then nRequiredGridColumn specifies the position
233 of the grid column which is actually bound
234 */
235 sal_Int32 nRequiredGridColumn;
236
ColumnInfosvxform::ColumnInfo237 ColumnInfo()
238 :xColumn()
239 ,nNullable( ColumnValue::NULLABLE_UNKNOWN )
240 ,bAutoIncrement( sal_False )
241 ,bReadOnly( sal_False )
242 ,sName()
243 ,xFirstControlWithInputRequired()
244 ,xFirstGridWithInputRequiredColumn()
245 ,nRequiredGridColumn( -1 )
246 {
247 }
248 };
249
250 //==============================================================================
251 //= ColumnInfoCache
252 //==============================================================================
253 class ColumnInfoCache
254 {
255 public:
256 ColumnInfoCache( const Reference< XColumnsSupplier >& _rxColSupplier );
257
getColumnCount() const258 size_t getColumnCount() const { return m_aColumns.size(); }
259 const ColumnInfo& getColumnInfo( size_t _pos );
260
controlsInitialized() const261 bool controlsInitialized() const { return m_bControlsInitialized; }
262 void initializeControls( const Sequence< Reference< XControl > >& _rControls );
263 void deinitializeControls();
264
265 private:
266 typedef ::std::vector< ColumnInfo > ColumnInfos;
267 ColumnInfos m_aColumns;
268 bool m_bControlsInitialized;
269 };
270
271 //------------------------------------------------------------------------------
ColumnInfoCache(const Reference<XColumnsSupplier> & _rxColSupplier)272 ColumnInfoCache::ColumnInfoCache( const Reference< XColumnsSupplier >& _rxColSupplier )
273 :m_aColumns()
274 ,m_bControlsInitialized( false )
275 {
276 try
277 {
278 m_aColumns.clear();
279
280 Reference< XColumnsSupplier > xSupplyCols( _rxColSupplier, UNO_SET_THROW );
281 Reference< XIndexAccess > xColumns( xSupplyCols->getColumns(), UNO_QUERY_THROW );
282 sal_Int32 nColumnCount = xColumns->getCount();
283 m_aColumns.reserve( nColumnCount );
284
285 Reference< XPropertySet > xColumnProps;
286 for ( sal_Int32 i = 0; i < nColumnCount; ++i )
287 {
288 ColumnInfo aColInfo;
289 aColInfo.xColumn.set( xColumns->getByIndex(i), UNO_QUERY_THROW );
290
291 xColumnProps.set( aColInfo.xColumn, UNO_QUERY_THROW );
292 OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_ISNULLABLE ) >>= aColInfo.nNullable );
293 OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_AUTOINCREMENT ) >>= aColInfo.bAutoIncrement );
294 OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_NAME ) >>= aColInfo.sName );
295 OSL_VERIFY( xColumnProps->getPropertyValue( FM_PROP_ISREADONLY ) >>= aColInfo.bReadOnly );
296
297 m_aColumns.push_back( aColInfo );
298 }
299 }
300 catch( const Exception& )
301 {
302 DBG_UNHANDLED_EXCEPTION();
303 }
304 }
305
306 //------------------------------------------------------------------------------
307 namespace
308 {
lcl_isBoundTo(const Reference<XPropertySet> & _rxControlModel,const Reference<XInterface> & _rxNormDBField)309 bool lcl_isBoundTo( const Reference< XPropertySet >& _rxControlModel, const Reference< XInterface >& _rxNormDBField )
310 {
311 Reference< XInterface > xNormBoundField( _rxControlModel->getPropertyValue( FM_PROP_BOUNDFIELD ), UNO_QUERY );
312 return ( xNormBoundField.get() == _rxNormDBField.get() );
313 }
314
lcl_isInputRequired(const Reference<XPropertySet> & _rxControlModel)315 bool lcl_isInputRequired( const Reference< XPropertySet >& _rxControlModel )
316 {
317 sal_Bool bInputRequired = sal_True;
318 OSL_VERIFY( _rxControlModel->getPropertyValue( FM_PROP_INPUT_REQUIRED ) >>= bInputRequired );
319 return ( bInputRequired != sal_False );
320 }
321
lcl_resetColumnControlInfo(ColumnInfo & _rColInfo)322 void lcl_resetColumnControlInfo( ColumnInfo& _rColInfo )
323 {
324 _rColInfo.xFirstControlWithInputRequired.clear();
325 _rColInfo.xFirstGridWithInputRequiredColumn.clear();
326 _rColInfo.nRequiredGridColumn = -1;
327 }
328 }
329
330 //------------------------------------------------------------------------------
deinitializeControls()331 void ColumnInfoCache::deinitializeControls()
332 {
333 for ( ColumnInfos::iterator col = m_aColumns.begin();
334 col != m_aColumns.end();
335 ++col
336 )
337 {
338 lcl_resetColumnControlInfo( *col );
339 }
340 }
341
342 //------------------------------------------------------------------------------
initializeControls(const Sequence<Reference<XControl>> & _rControls)343 void ColumnInfoCache::initializeControls( const Sequence< Reference< XControl > >& _rControls )
344 {
345 try
346 {
347 // for every of our known columns, find the controls which are bound to this column
348 for ( ColumnInfos::iterator col = m_aColumns.begin();
349 col != m_aColumns.end();
350 ++col
351 )
352 {
353 OSL_ENSURE( !col->xFirstControlWithInputRequired.is() && !col->xFirstGridWithInputRequiredColumn.is()
354 && ( col->nRequiredGridColumn == -1 ), "ColumnInfoCache::initializeControls: called me twice?" );
355
356 lcl_resetColumnControlInfo( *col );
357
358 Reference< XInterface > xNormColumn( col->xColumn, UNO_QUERY_THROW );
359
360 const Reference< XControl >* pControl( _rControls.getConstArray() );
361 const Reference< XControl >* pControlEnd( pControl + _rControls.getLength() );
362 for ( ; pControl != pControlEnd; ++pControl )
363 {
364 if ( !pControl->is() )
365 continue;
366
367 Reference< XPropertySet > xModel( (*pControl)->getModel(), UNO_QUERY_THROW );
368 Reference< XPropertySetInfo > xModelPSI( xModel->getPropertySetInfo(), UNO_SET_THROW );
369
370 // special handling for grid controls
371 Reference< XGrid > xGrid( *pControl, UNO_QUERY );
372 if ( xGrid.is() )
373 {
374 Reference< XIndexAccess > xGridColAccess( xModel, UNO_QUERY_THROW );
375 sal_Int32 gridColCount = xGridColAccess->getCount();
376 sal_Int32 gridCol = 0;
377 for ( gridCol = 0; gridCol < gridColCount; ++gridCol )
378 {
379 Reference< XPropertySet > xGridColumnModel( xGridColAccess->getByIndex( gridCol ), UNO_QUERY_THROW );
380
381 if ( !lcl_isBoundTo( xGridColumnModel, xNormColumn )
382 || !lcl_isInputRequired( xGridColumnModel )
383 )
384 continue; // with next grid column
385
386 break;
387 }
388
389 if ( gridCol < gridColCount )
390 {
391 // found a grid column which is bound to the given
392 col->xFirstGridWithInputRequiredColumn = xGrid;
393 col->nRequiredGridColumn = gridCol;
394 break;
395 }
396
397 continue; // with next control
398 }
399
400 if ( !xModelPSI->hasPropertyByName( FM_PROP_BOUNDFIELD )
401 || !lcl_isBoundTo( xModel, xNormColumn )
402 || !lcl_isInputRequired( xModel )
403 )
404 continue; // with next control
405
406 break;
407 }
408
409 if ( pControl == pControlEnd )
410 // did not find a control which is bound to this particular column, and for which the input is required
411 continue; // with next DB column
412
413 col->xFirstControlWithInputRequired = *pControl;
414 }
415 }
416 catch( const Exception& )
417 {
418 DBG_UNHANDLED_EXCEPTION();
419 }
420
421 m_bControlsInitialized = true;
422 }
423
424 //------------------------------------------------------------------------------
getColumnInfo(size_t _pos)425 const ColumnInfo& ColumnInfoCache::getColumnInfo( size_t _pos )
426 {
427 if ( _pos >= m_aColumns.size() )
428 throw IndexOutOfBoundsException();
429
430 return m_aColumns[ _pos ];
431 }
432
433 //==================================================================
434 // OParameterContinuation
435 //==================================================================
436 class OParameterContinuation : public OInteraction< XInteractionSupplyParameters >
437 {
438 Sequence< PropertyValue > m_aValues;
439
440 public:
OParameterContinuation()441 OParameterContinuation() { }
442
getValues() const443 Sequence< PropertyValue > getValues() const { return m_aValues; }
444
445 // XInteractionSupplyParameters
446 virtual void SAL_CALL setParameters( const Sequence< PropertyValue >& _rValues );
447 };
448
449 //------------------------------------------------------------------
setParameters(const Sequence<PropertyValue> & _rValues)450 void SAL_CALL OParameterContinuation::setParameters( const Sequence< PropertyValue >& _rValues )
451 {
452 m_aValues = _rValues;
453 }
454
455 //==================================================================
456 // FmXAutoControl
457 //==================================================================
458 struct FmFieldInfo
459 {
460 rtl::OUString aFieldName;
461 Reference< XPropertySet > xField;
462 Reference< XTextComponent > xText;
463
FmFieldInfosvxform::FmFieldInfo464 FmFieldInfo(const Reference< XPropertySet >& _xField, const Reference< XTextComponent >& _xText)
465 :xField(_xField)
466 ,xText(_xText)
467 {xField->getPropertyValue(FM_PROP_NAME) >>= aFieldName;}
468 };
469
470 //==================================================================
471 // FmXAutoControl
472 //==================================================================
473 class FmXAutoControl: public UnoControl
474
475 {
476 friend Reference< XInterface > SAL_CALL FmXAutoControl_NewInstance_Impl();
477
478 public:
FmXAutoControl(const::comphelper::ComponentContext & i_context)479 FmXAutoControl( const ::comphelper::ComponentContext& i_context )
480 :UnoControl( i_context.getLegacyServiceFactory() )
481 {
482 }
483
GetComponentServiceName()484 virtual ::rtl::OUString GetComponentServiceName() {return ::rtl::OUString::createFromAscii("Edit");}
485 virtual void SAL_CALL createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer );
486
487 protected:
488 virtual void ImplSetPeerProperty( const ::rtl::OUString& rPropName, const Any& rVal );
489 };
490
491 //------------------------------------------------------------------------------
createPeer(const Reference<XToolkit> & rxToolkit,const Reference<XWindowPeer> & rParentPeer)492 void FmXAutoControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer )
493 {
494 UnoControl::createPeer( rxToolkit, rParentPeer );
495
496 Reference< XTextComponent > xText(getPeer() , UNO_QUERY);
497 if (xText.is())
498 {
499 xText->setText(::rtl::OUString(String(SVX_RES(RID_STR_AUTOFIELD))));
500 xText->setEditable(sal_False);
501 }
502 }
503
504 //------------------------------------------------------------------------------
ImplSetPeerProperty(const::rtl::OUString & rPropName,const Any & rVal)505 void FmXAutoControl::ImplSetPeerProperty( const ::rtl::OUString& rPropName, const Any& rVal )
506 {
507 // these properties are ignored
508 if (rPropName == FM_PROP_TEXT)
509 return;
510
511 UnoControl::ImplSetPeerProperty( rPropName, rVal );
512 }
513
514 //------------------------------------------------------------------------------
515 IMPL_LINK( FormController, OnActivateTabOrder, void*, /*EMPTYTAG*/ )
516 {
517 activateTabOrder();
518 return 1;
519 }
520
521 //------------------------------------------------------------------------------
522 struct UpdateAllListeners
523 {
524 typedef Reference< XDispatch > argument_type;
525 typedef bool result_type;
operator ()svxform::UpdateAllListeners526 bool operator()( const Reference< XDispatch >& _rxDispatcher ) const
527 {
528 static_cast< ::svx::OSingleFeatureDispatcher* >( _rxDispatcher.get() )->updateAllListeners();
529 // the return is a dummy only so we can use this struct in a std::compose1 call
530 return true;
531 }
532 };
533 //..............................................................................
534 IMPL_LINK( FormController, OnInvalidateFeatures, void*, /*_pNotInterestedInThisParam*/ )
535 {
536 ::osl::MutexGuard aGuard( m_aMutex );
537 for ( ::std::set< sal_Int16 >::const_iterator aLoop = m_aInvalidFeatures.begin();
538 aLoop != m_aInvalidFeatures.end();
539 ++aLoop
540 )
541 {
542 DispatcherContainer::const_iterator aDispatcherPos = m_aFeatureDispatchers.find( *aLoop );
543 if ( aDispatcherPos != m_aFeatureDispatchers.end() )
544 {
545 // TODO: for the real and actual listener notifications, we should release
546 // our mutex
547 UpdateAllListeners( )( aDispatcherPos->second );
548 }
549 }
550 return 1;
551 }
552
553 /*************************************************************************/
554
DBG_NAME(FormController)555 DBG_NAME( FormController )
556 //------------------------------------------------------------------
557 FormController::FormController(const Reference< XMultiServiceFactory > & _rxORB )
558 :FormController_BASE( m_aMutex )
559 ,OPropertySetHelper( FormController_BASE::rBHelper )
560 ,OSQLParserClient( _rxORB )
561 ,m_aContext( _rxORB )
562 ,m_aActivateListeners(m_aMutex)
563 ,m_aModifyListeners(m_aMutex)
564 ,m_aErrorListeners(m_aMutex)
565 ,m_aDeleteListeners(m_aMutex)
566 ,m_aRowSetApproveListeners(m_aMutex)
567 ,m_aParameterListeners(m_aMutex)
568 ,m_aFilterListeners(m_aMutex)
569 ,m_pControlBorderManager( new ::svxform::ControlBorderManager )
570 ,m_xFormOperations()
571 ,m_aMode( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DataMode" ) ) )
572 ,m_aLoadEvent( LINK( this, FormController, OnLoad ) )
573 ,m_aToggleEvent( LINK( this, FormController, OnToggleAutoFields ) )
574 ,m_aActivationEvent( LINK( this, FormController, OnActivated ) )
575 ,m_aDeactivationEvent( LINK( this, FormController, OnDeactivated ) )
576 ,m_nCurrentFilterPosition(-1)
577 ,m_bCurrentRecordModified(sal_False)
578 ,m_bCurrentRecordNew(sal_False)
579 ,m_bLocked(sal_False)
580 ,m_bDBConnection(sal_False)
581 ,m_bCycle(sal_False)
582 ,m_bCanInsert(sal_False)
583 ,m_bCanUpdate(sal_False)
584 ,m_bCommitLock(sal_False)
585 ,m_bModified(sal_False)
586 ,m_bControlsSorted(sal_False)
587 ,m_bFiltering(sal_False)
588 ,m_bAttachEvents(sal_True)
589 ,m_bDetachEvents(sal_True)
590 ,m_bAttemptedHandlerCreation( false )
591 ,m_bSuspendFilterTextListening( false )
592 {
593 DBG_CTOR( FormController, NULL );
594
595 ::comphelper::increment(m_refCount);
596 {
597 {
598 m_xAggregate = Reference< XAggregation >(
599 m_aContext.createComponent( "com.sun.star.awt.TabController" ),
600 UNO_QUERY
601 );
602 DBG_ASSERT( m_xAggregate.is(), "FormController::FormController : could not create my aggregate !" );
603 m_xTabController = Reference< XTabController >( m_xAggregate, UNO_QUERY );
604 }
605
606 if ( m_xAggregate.is() )
607 m_xAggregate->setDelegator( *this );
608 }
609 ::comphelper::decrement(m_refCount);
610
611 m_aTabActivationTimer.SetTimeout( 500 );
612 m_aTabActivationTimer.SetTimeoutHdl( LINK( this, FormController, OnActivateTabOrder ) );
613
614 m_aFeatureInvalidationTimer.SetTimeout( 200 );
615 m_aFeatureInvalidationTimer.SetTimeoutHdl( LINK( this, FormController, OnInvalidateFeatures ) );
616 }
617
618 //------------------------------------------------------------------
~FormController()619 FormController::~FormController()
620 {
621 {
622 ::osl::MutexGuard aGuard( m_aMutex );
623
624 m_aLoadEvent.CancelPendingCall();
625 m_aToggleEvent.CancelPendingCall();
626 m_aActivationEvent.CancelPendingCall();
627 m_aDeactivationEvent.CancelPendingCall();
628
629 if ( m_aTabActivationTimer.IsActive() )
630 m_aTabActivationTimer.Stop();
631 }
632
633 if ( m_aFeatureInvalidationTimer.IsActive() )
634 m_aFeatureInvalidationTimer.Stop();
635
636 disposeAllFeaturesAndDispatchers();
637
638 if ( m_xFormOperations.is() )
639 m_xFormOperations->dispose();
640 m_xFormOperations.clear();
641
642 // Freigeben der Aggregation
643 if ( m_xAggregate.is() )
644 {
645 m_xAggregate->setDelegator( NULL );
646 m_xAggregate.clear();
647 }
648
649 DELETEZ( m_pControlBorderManager );
650
651 DBG_DTOR( FormController, NULL );
652 }
653
654 // -----------------------------------------------------------------------------
acquire()655 void SAL_CALL FormController::acquire() throw ()
656 {
657 FormController_BASE::acquire();
658 }
659
660 // -----------------------------------------------------------------------------
release()661 void SAL_CALL FormController::release() throw ()
662 {
663 FormController_BASE::release();
664 }
665
666 //------------------------------------------------------------------
queryInterface(const Type & _rType)667 Any SAL_CALL FormController::queryInterface( const Type& _rType )
668 {
669 Any aRet = FormController_BASE::queryInterface( _rType );
670 if ( !aRet.hasValue() )
671 aRet = OPropertySetHelper::queryInterface( _rType );
672 if ( !aRet.hasValue() )
673 aRet = m_xAggregate->queryAggregation( _rType );
674 return aRet;
675 }
676
677 //------------------------------------------------------------------------------
getImplementationId()678 Sequence< sal_Int8 > SAL_CALL FormController::getImplementationId()
679 {
680 static ::cppu::OImplementationId* pId = NULL;
681 if ( !pId )
682 {
683 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
684 if ( !pId )
685 {
686 static ::cppu::OImplementationId aId;
687 pId = &aId;
688 }
689 }
690 return pId->getImplementationId();
691 }
692
693 //------------------------------------------------------------------------------
getTypes()694 Sequence< Type > SAL_CALL FormController::getTypes( )
695 {
696 return comphelper::concatSequences(
697 FormController_BASE::getTypes(),
698 ::cppu::OPropertySetHelper::getTypes()
699 );
700 }
701
702 // XServiceInfo
703 //------------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)704 sal_Bool SAL_CALL FormController::supportsService(const ::rtl::OUString& ServiceName)
705 {
706 Sequence< ::rtl::OUString> aSNL(getSupportedServiceNames());
707 const ::rtl::OUString * pArray = aSNL.getConstArray();
708 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
709 if( pArray[i] == ServiceName )
710 return sal_True;
711 return sal_False;
712 }
713
714 //------------------------------------------------------------------------------
getImplementationName()715 ::rtl::OUString SAL_CALL FormController::getImplementationName()
716 {
717 return ::rtl::OUString::createFromAscii( "org.openoffice.comp.svx.FormController" );
718 }
719
720 //------------------------------------------------------------------------------
getSupportedServiceNames(void)721 Sequence< ::rtl::OUString> SAL_CALL FormController::getSupportedServiceNames(void)
722 {
723 // service names which are supported only, but cannot be used to created an
724 // instance at a service factory
725 Sequence< ::rtl::OUString > aNonCreatableServiceNames( 1 );
726 aNonCreatableServiceNames[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.FormControllerDispatcher" ) );
727
728 // services which can be used to created an instance at a service factory
729 Sequence< ::rtl::OUString > aCreatableServiceNames( getSupportedServiceNames_Static() );
730 return ::comphelper::concatSequences( aCreatableServiceNames, aNonCreatableServiceNames );
731 }
732
733 //------------------------------------------------------------------------------
approveReset(const EventObject &)734 sal_Bool SAL_CALL FormController::approveReset(const EventObject& /*rEvent*/)
735 {
736 return sal_True;
737 }
738
739 //------------------------------------------------------------------------------
resetted(const EventObject & rEvent)740 void SAL_CALL FormController::resetted(const EventObject& rEvent)
741 {
742 ::osl::MutexGuard aGuard(m_aMutex);
743 if (getCurrentControl().is() && (getCurrentControl()->getModel() == rEvent.Source))
744 m_bModified = sal_False;
745 }
746
747 //------------------------------------------------------------------------------
getSupportedServiceNames_Static(void)748 Sequence< ::rtl::OUString> FormController::getSupportedServiceNames_Static(void)
749 {
750 static Sequence< ::rtl::OUString> aServices;
751 if (!aServices.getLength())
752 {
753 aServices.realloc(2);
754 aServices.getArray()[0] = FM_FORM_CONTROLLER;
755 aServices.getArray()[1] = ::rtl::OUString::createFromAscii("com.sun.star.awt.control.TabController");
756 }
757 return aServices;
758 }
759
760 // -----------------------------------------------------------------------------
761 namespace
762 {
763 struct ResetComponentText
764 {
765 typedef Reference< XTextComponent > argument_type;
766 typedef void result_type;
operator ()svxform::__anon3c3089620211::ResetComponentText767 void operator()( const Reference< XTextComponent >& _rxText )
768 {
769 _rxText->setText( ::rtl::OUString() );
770 }
771 };
772
773 struct RemoveComponentTextListener
774 {
775 typedef Reference< XTextComponent > argument_type;
776 typedef void result_type;
RemoveComponentTextListenersvxform::__anon3c3089620211::RemoveComponentTextListener777 RemoveComponentTextListener( const Reference< XTextListener >& _rxListener )
778 :m_xListener( _rxListener )
779 {
780 }
781
operator ()svxform::__anon3c3089620211::RemoveComponentTextListener782 void operator()( const Reference< XTextComponent >& _rxText )
783 {
784 _rxText->removeTextListener( m_xListener );
785 }
786
787 private:
788 Reference< XTextListener > m_xListener;
789 };
790 }
791
792 // -----------------------------------------------------------------------------
impl_setTextOnAllFilter_throw()793 void FormController::impl_setTextOnAllFilter_throw()
794 {
795 m_bSuspendFilterTextListening = true;
796 ::comphelper::FlagGuard aResetFlag( m_bSuspendFilterTextListening );
797
798 // reset the text for all controls
799 ::std::for_each( m_aFilterComponents.begin(), m_aFilterComponents.end(), ResetComponentText() );
800
801 if ( m_aFilterRows.empty() )
802 // nothing to do anymore
803 return;
804
805 if ( m_nCurrentFilterPosition < 0 )
806 return;
807
808 // set the text for all filters
809 OSL_ENSURE( m_aFilterRows.size() > (size_t)m_nCurrentFilterPosition,
810 "FormController::impl_setTextOnAllFilter_throw: m_nCurrentFilterPosition too big" );
811
812 if ( (size_t)m_nCurrentFilterPosition < m_aFilterRows.size() )
813 {
814 FmFilterRow& rRow = m_aFilterRows[ m_nCurrentFilterPosition ];
815 for ( FmFilterRow::const_iterator iter2 = rRow.begin();
816 iter2 != rRow.end();
817 ++iter2
818 )
819 {
820 iter2->first->setText( iter2->second );
821 }
822 }
823 }
824 // OPropertySetHelper
825 //------------------------------------------------------------------------------
convertFastPropertyValue(Any &,Any &,sal_Int32,const Any &)826 sal_Bool FormController::convertFastPropertyValue( Any & /*rConvertedValue*/, Any & /*rOldValue*/,
827 sal_Int32 /*nHandle*/, const Any& /*rValue*/ )
828 {
829 return sal_False;
830 }
831
832 //------------------------------------------------------------------------------
setFastPropertyValue_NoBroadcast(sal_Int32,const Any &)833 void FormController::setFastPropertyValue_NoBroadcast( sal_Int32 /*nHandle*/, const Any& /*rValue*/ )
834 {
835 }
836
837 //------------------------------------------------------------------------------
getFastPropertyValue(Any & rValue,sal_Int32 nHandle) const838 void FormController::getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const
839 {
840 switch (nHandle)
841 {
842 case FM_ATTR_FILTER:
843 {
844 ::rtl::OUStringBuffer aFilter;
845 OStaticDataAccessTools aStaticTools;
846 Reference<XConnection> xConnection(aStaticTools.getRowSetConnection(Reference< XRowSet>(m_xModelAsIndex, UNO_QUERY)));
847 if (xConnection.is())
848 {
849 Reference< XDatabaseMetaData> xMetaData(xConnection->getMetaData());
850 Reference< XNumberFormatsSupplier> xFormatSupplier( aStaticTools.getNumberFormats( xConnection, sal_True ) );
851 Reference< XNumberFormatter> xFormatter( m_aContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY_THROW );
852 xFormatter->attachNumberFormatsSupplier(xFormatSupplier);
853
854 Reference< XColumnsSupplier> xSupplyCols(m_xModelAsIndex, UNO_QUERY);
855 Reference< XNameAccess> xFields(xSupplyCols->getColumns(), UNO_QUERY);
856
857 ::rtl::OUString aQuote( xMetaData->getIdentifierQuoteString() );
858
859 // now add the filter rows
860 try
861 {
862 for ( FmFilterRows::const_iterator row = m_aFilterRows.begin(); row != m_aFilterRows.end(); ++row )
863 {
864 const FmFilterRow& rRow = *row;
865
866 if ( rRow.empty() )
867 continue;
868
869 ::rtl::OUStringBuffer aRowFilter;
870 for ( FmFilterRow::const_iterator condition = rRow.begin(); condition != rRow.end(); ++condition )
871 {
872 // get the field of the controls map
873 Reference< XControl > xControl( condition->first, UNO_QUERY_THROW );
874 Reference< XPropertySet > xModelProps( xControl->getModel(), UNO_QUERY_THROW );
875 Reference< XPropertySet > xField( xModelProps->getPropertyValue( FM_PROP_BOUNDFIELD ), UNO_QUERY_THROW );
876
877 ::rtl::OUString sFilterValue( condition->second );
878
879 ::rtl::OUString sErrorMsg, sCriteria;
880 const ::rtl::Reference< ISQLParseNode > xParseNode =
881 predicateTree( sErrorMsg, sFilterValue, xFormatter, xField );
882 OSL_ENSURE( xParseNode.is(), "FormController::getFastPropertyValue: could not parse the field value predicate!" );
883 if ( xParseNode.is() )
884 {
885 // don't use a parse context here, we need it unlocalized
886 xParseNode->parseNodeToStr( sCriteria, xConnection, NULL );
887 if ( condition != rRow.begin() )
888 aRowFilter.appendAscii( " AND " );
889 aRowFilter.append( sCriteria );
890 }
891 }
892 if ( aRowFilter.getLength() > 0 )
893 {
894 if ( aFilter.getLength() )
895 aFilter.appendAscii( " OR " );
896
897 aFilter.appendAscii( "( " );
898 aFilter.append( aRowFilter.makeStringAndClear() );
899 aFilter.appendAscii( " )" );
900 }
901 }
902 }
903 catch( const Exception& )
904 {
905 DBG_UNHANDLED_EXCEPTION();
906 aFilter.setLength(0);
907 }
908 }
909 rValue <<= aFilter.makeStringAndClear();
910 }
911 break;
912
913 case FM_ATTR_FORM_OPERATIONS:
914 rValue <<= m_xFormOperations;
915 break;
916 }
917 }
918
919 //------------------------------------------------------------------------------
getPropertySetInfo()920 Reference< XPropertySetInfo > FormController::getPropertySetInfo()
921 {
922 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
923 return xInfo;
924 }
925
926 //------------------------------------------------------------------------------
927 #define DECL_PROP_CORE(varname, type) \
928 pDesc[nPos++] = Property(FM_PROP_##varname, FM_ATTR_##varname, ::getCppuType((const type*)0),
929
930
931 #define DECL_PROP1(varname, type, attrib1) \
932 DECL_PROP_CORE(varname, type) PropertyAttribute::attrib1)
933
934 //------------------------------------------------------------------------------
fillProperties(Sequence<Property> & _rProps,Sequence<Property> &) const935 void FormController::fillProperties(
936 Sequence< Property >& /* [out] */ _rProps,
937 Sequence< Property >& /* [out] */ /*_rAggregateProps*/
938 ) const
939 {
940 _rProps.realloc(2);
941 sal_Int32 nPos = 0;
942 Property* pDesc = _rProps.getArray();
943 DECL_PROP1(FILTER, rtl::OUString, READONLY);
944 DECL_PROP1(FORM_OPERATIONS, Reference< XFormOperations >, READONLY);
945 }
946
947 //------------------------------------------------------------------------------
getInfoHelper()948 ::cppu::IPropertyArrayHelper& FormController::getInfoHelper()
949 {
950 return *getArrayHelper();
951 }
952
953 // XFilterController
954 //------------------------------------------------------------------------------
addFilterControllerListener(const Reference<XFilterControllerListener> & _Listener)955 void SAL_CALL FormController::addFilterControllerListener( const Reference< XFilterControllerListener >& _Listener )
956 {
957 m_aFilterListeners.addInterface( _Listener );
958 }
959
960 //------------------------------------------------------------------------------
removeFilterControllerListener(const Reference<XFilterControllerListener> & _Listener)961 void SAL_CALL FormController::removeFilterControllerListener( const Reference< XFilterControllerListener >& _Listener )
962 {
963 m_aFilterListeners.removeInterface( _Listener );
964 }
965
966 //------------------------------------------------------------------------------
getFilterComponents()967 ::sal_Int32 SAL_CALL FormController::getFilterComponents()
968 {
969 ::osl::MutexGuard aGuard( m_aMutex );
970 impl_checkDisposed_throw();
971
972 return m_aFilterComponents.size();
973 }
974
975 //------------------------------------------------------------------------------
getDisjunctiveTerms()976 ::sal_Int32 SAL_CALL FormController::getDisjunctiveTerms()
977 {
978 ::osl::MutexGuard aGuard( m_aMutex );
979 impl_checkDisposed_throw();
980
981 return m_aFilterRows.size();
982 }
983
984 //------------------------------------------------------------------------------
setPredicateExpression(::sal_Int32 _Component,::sal_Int32 _Term,const::rtl::OUString & _PredicateExpression)985 void SAL_CALL FormController::setPredicateExpression( ::sal_Int32 _Component, ::sal_Int32 _Term, const ::rtl::OUString& _PredicateExpression )
986 {
987 ::osl::MutexGuard aGuard( m_aMutex );
988 impl_checkDisposed_throw();
989
990 if ( ( _Component < 0 ) || ( _Component >= getFilterComponents() ) || ( _Term < 0 ) || ( _Term >= getDisjunctiveTerms() ) )
991 throw IndexOutOfBoundsException( ::rtl::OUString(), *this );
992
993 Reference< XTextComponent > xText( m_aFilterComponents[ _Component ] );
994 xText->setText( _PredicateExpression );
995
996 FmFilterRow& rFilterRow = m_aFilterRows[ _Term ];
997 if ( _PredicateExpression.getLength() )
998 rFilterRow[ xText ] = _PredicateExpression;
999 else
1000 rFilterRow.erase( xText );
1001 }
1002
1003 //------------------------------------------------------------------------------
getFilterComponent(::sal_Int32 _Component)1004 Reference< XControl > FormController::getFilterComponent( ::sal_Int32 _Component )
1005 {
1006 ::osl::MutexGuard aGuard( m_aMutex );
1007 impl_checkDisposed_throw();
1008
1009 if ( ( _Component < 0 ) || ( _Component >= getFilterComponents() ) )
1010 throw IndexOutOfBoundsException( ::rtl::OUString(), *this );
1011
1012 return Reference< XControl >( m_aFilterComponents[ _Component ], UNO_QUERY );
1013 }
1014
1015 //------------------------------------------------------------------------------
getPredicateExpressions()1016 Sequence< Sequence< ::rtl::OUString > > FormController::getPredicateExpressions()
1017 {
1018 ::osl::MutexGuard aGuard( m_aMutex );
1019 impl_checkDisposed_throw();
1020
1021 Sequence< Sequence< ::rtl::OUString > > aExpressions( m_aFilterRows.size() );
1022 sal_Int32 termIndex = 0;
1023 for ( FmFilterRows::const_iterator row = m_aFilterRows.begin();
1024 row != m_aFilterRows.end();
1025 ++row, ++termIndex
1026 )
1027 {
1028 const FmFilterRow& rRow( *row );
1029
1030 Sequence< ::rtl::OUString > aConjunction( m_aFilterComponents.size() );
1031 sal_Int32 componentIndex = 0;
1032 for ( FilterComponents::const_iterator comp = m_aFilterComponents.begin();
1033 comp != m_aFilterComponents.end();
1034 ++comp, ++componentIndex
1035 )
1036 {
1037 FmFilterRow::const_iterator predicate = rRow.find( *comp );
1038 if ( predicate != rRow.end() )
1039 aConjunction[ componentIndex ] = predicate->second;
1040 }
1041
1042 aExpressions[ termIndex ] = aConjunction;
1043 }
1044
1045 return aExpressions;
1046 }
1047
1048 //------------------------------------------------------------------------------
removeDisjunctiveTerm(::sal_Int32 _Term)1049 void SAL_CALL FormController::removeDisjunctiveTerm( ::sal_Int32 _Term )
1050 {
1051 // SYNCHRONIZED -->
1052 ::osl::ClearableMutexGuard aGuard( m_aMutex );
1053 impl_checkDisposed_throw();
1054
1055 if ( ( _Term < 0 ) || ( _Term >= getDisjunctiveTerms() ) )
1056 throw IndexOutOfBoundsException( ::rtl::OUString(), *this );
1057
1058 // if the to-be-deleted row is our current row, we need to shift
1059 if ( _Term == m_nCurrentFilterPosition )
1060 {
1061 if ( m_nCurrentFilterPosition < sal_Int32( m_aFilterRows.size() - 1 ) )
1062 ++m_nCurrentFilterPosition;
1063 else
1064 --m_nCurrentFilterPosition;
1065 }
1066
1067 FmFilterRows::iterator pos = m_aFilterRows.begin() + _Term;
1068 m_aFilterRows.erase( pos );
1069
1070 // adjust m_nCurrentFilterPosition if the removed row preceded it
1071 if ( _Term < m_nCurrentFilterPosition )
1072 --m_nCurrentFilterPosition;
1073
1074 OSL_POSTCOND( ( m_nCurrentFilterPosition < 0 ) == ( m_aFilterRows.empty() ),
1075 "FormController::removeDisjunctiveTerm: inconsistency!" );
1076
1077 // update the texts in the filter controls
1078 impl_setTextOnAllFilter_throw();
1079
1080 FilterEvent aEvent;
1081 aEvent.Source = *this;
1082 aEvent.DisjunctiveTerm = _Term;
1083 aGuard.clear();
1084 // <-- SYNCHRONIZED
1085
1086 m_aFilterListeners.notifyEach( &XFilterControllerListener::disjunctiveTermRemoved, aEvent );
1087 }
1088
1089 //------------------------------------------------------------------------------
appendEmptyDisjunctiveTerm()1090 void SAL_CALL FormController::appendEmptyDisjunctiveTerm()
1091 {
1092 // SYNCHRONIZED -->
1093 ::osl::ClearableMutexGuard aGuard( m_aMutex );
1094 impl_checkDisposed_throw();
1095
1096 impl_appendEmptyFilterRow( aGuard );
1097 // <-- SYNCHRONIZED
1098 }
1099
1100 //------------------------------------------------------------------------------
getActiveTerm()1101 ::sal_Int32 SAL_CALL FormController::getActiveTerm()
1102 {
1103 ::osl::MutexGuard aGuard( m_aMutex );
1104 impl_checkDisposed_throw();
1105
1106 return m_nCurrentFilterPosition;
1107 }
1108
1109 //------------------------------------------------------------------------------
setActiveTerm(::sal_Int32 _ActiveTerm)1110 void SAL_CALL FormController::setActiveTerm( ::sal_Int32 _ActiveTerm )
1111 {
1112 ::osl::MutexGuard aGuard( m_aMutex );
1113 impl_checkDisposed_throw();
1114
1115 if ( ( _ActiveTerm < 0 ) || ( _ActiveTerm >= getDisjunctiveTerms() ) )
1116 throw IndexOutOfBoundsException( ::rtl::OUString(), *this );
1117
1118 if ( _ActiveTerm == getActiveTerm() )
1119 return;
1120
1121 m_nCurrentFilterPosition = _ActiveTerm;
1122 impl_setTextOnAllFilter_throw();
1123 }
1124
1125 // XElementAccess
1126 //------------------------------------------------------------------------------
hasElements(void)1127 sal_Bool SAL_CALL FormController::hasElements(void)
1128 {
1129 ::osl::MutexGuard aGuard( m_aMutex );
1130 return !m_aChilds.empty();
1131 }
1132
1133 //------------------------------------------------------------------------------
getElementType(void)1134 Type SAL_CALL FormController::getElementType(void)
1135 {
1136 return ::getCppuType((const Reference< XFormController>*)0);
1137
1138 }
1139
1140 // XEnumerationAccess
1141 //------------------------------------------------------------------------------
createEnumeration(void)1142 Reference< XEnumeration > SAL_CALL FormController::createEnumeration(void)
1143 {
1144 ::osl::MutexGuard aGuard( m_aMutex );
1145 return new ::comphelper::OEnumerationByIndex(this);
1146 }
1147
1148 // XIndexAccess
1149 //------------------------------------------------------------------------------
getCount(void)1150 sal_Int32 SAL_CALL FormController::getCount(void)
1151 {
1152 ::osl::MutexGuard aGuard( m_aMutex );
1153 return m_aChilds.size();
1154 }
1155
1156 //------------------------------------------------------------------------------
getByIndex(sal_Int32 Index)1157 Any SAL_CALL FormController::getByIndex(sal_Int32 Index)
1158 {
1159 ::osl::MutexGuard aGuard( m_aMutex );
1160 if (Index < 0 ||
1161 Index >= (sal_Int32)m_aChilds.size())
1162 throw IndexOutOfBoundsException();
1163
1164 return makeAny( m_aChilds[ Index ] );
1165 }
1166
1167 // EventListener
1168 //------------------------------------------------------------------------------
disposing(const EventObject & e)1169 void SAL_CALL FormController::disposing(const EventObject& e)
1170 {
1171 // Ist der Container disposed worden
1172 ::osl::MutexGuard aGuard( m_aMutex );
1173 Reference< XControlContainer > xContainer(e.Source, UNO_QUERY);
1174 if (xContainer.is())
1175 {
1176 setContainer(Reference< XControlContainer > ());
1177 }
1178 else
1179 {
1180 // ist ein Control disposed worden
1181 Reference< XControl > xControl(e.Source, UNO_QUERY);
1182 if (xControl.is())
1183 {
1184 if (getContainer().is())
1185 removeControl(xControl);
1186 }
1187 }
1188 }
1189
1190 // OComponentHelper
1191 //-----------------------------------------------------------------------------
disposeAllFeaturesAndDispatchers()1192 void FormController::disposeAllFeaturesAndDispatchers() SAL_THROW(())
1193 {
1194 for ( DispatcherContainer::iterator aDispatcher = m_aFeatureDispatchers.begin();
1195 aDispatcher != m_aFeatureDispatchers.end();
1196 ++aDispatcher
1197 )
1198 {
1199 try
1200 {
1201 ::comphelper::disposeComponent( aDispatcher->second );
1202 }
1203 catch( const Exception& )
1204 {
1205 DBG_UNHANDLED_EXCEPTION();
1206 }
1207 }
1208 m_aFeatureDispatchers.clear();
1209 }
1210
1211 //-----------------------------------------------------------------------------
disposing(void)1212 void FormController::disposing(void)
1213 {
1214 EventObject aEvt( *this );
1215
1216 // if we're still active, simulate a "deactivated" event
1217 if ( m_xActiveControl.is() )
1218 m_aActivateListeners.notifyEach( &XFormControllerListener::formDeactivated, aEvt );
1219
1220 // notify all our listeners
1221 m_aActivateListeners.disposeAndClear(aEvt);
1222 m_aModifyListeners.disposeAndClear(aEvt);
1223 m_aErrorListeners.disposeAndClear(aEvt);
1224 m_aDeleteListeners.disposeAndClear(aEvt);
1225 m_aRowSetApproveListeners.disposeAndClear(aEvt);
1226 m_aParameterListeners.disposeAndClear(aEvt);
1227 m_aFilterListeners.disposeAndClear(aEvt);
1228
1229 removeBoundFieldListener();
1230 stopFiltering();
1231
1232 m_pControlBorderManager->restoreAll();
1233
1234 m_aFilterRows.clear();
1235
1236 ::osl::MutexGuard aGuard( m_aMutex );
1237 m_xActiveControl = NULL;
1238 implSetCurrentControl( NULL );
1239
1240 // clean up our children
1241 for (FmFormControllers::const_iterator i = m_aChilds.begin();
1242 i != m_aChilds.end(); i++)
1243 {
1244 // search the position of the model within the form
1245 Reference< XFormComponent > xForm((*i)->getModel(), UNO_QUERY);
1246 sal_uInt32 nPos = m_xModelAsIndex->getCount();
1247 Reference< XFormComponent > xTemp;
1248 for( ; nPos; )
1249 {
1250
1251 m_xModelAsIndex->getByIndex( --nPos ) >>= xTemp;
1252 if ( xForm.get() == xTemp.get() )
1253 {
1254 Reference< XInterface > xIfc( *i, UNO_QUERY );
1255 m_xModelAsManager->detach( nPos, xIfc );
1256 break;
1257 }
1258 }
1259
1260 Reference< XComponent > (*i, UNO_QUERY)->dispose();
1261 }
1262 m_aChilds.clear();
1263
1264 disposeAllFeaturesAndDispatchers();
1265
1266 if ( m_xFormOperations.is() )
1267 m_xFormOperations->dispose();
1268 m_xFormOperations.clear();
1269
1270 if (m_bDBConnection)
1271 unload();
1272
1273 setContainer( NULL );
1274 setModel( NULL );
1275 setParent( NULL );
1276
1277 ::comphelper::disposeComponent( m_xComposer );
1278
1279 m_bDBConnection = sal_False;
1280 }
1281
1282 //------------------------------------------------------------------------------
1283 namespace
1284 {
lcl_shouldUseDynamicControlBorder(const Reference<XInterface> & _rxForm,const Any & _rDynamicColorProp)1285 static bool lcl_shouldUseDynamicControlBorder( const Reference< XInterface >& _rxForm, const Any& _rDynamicColorProp )
1286 {
1287 bool bDoUse = false;
1288 if ( !( _rDynamicColorProp >>= bDoUse ) )
1289 {
1290 DocumentType eDocType = DocumentClassification::classifyHostDocument( _rxForm );
1291 return ControlLayouter::useDynamicBorderColor( eDocType );
1292 }
1293 return bDoUse;
1294 }
1295 }
1296
1297 //------------------------------------------------------------------------------
propertyChange(const PropertyChangeEvent & evt)1298 void SAL_CALL FormController::propertyChange(const PropertyChangeEvent& evt)
1299 {
1300 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1301 if ( evt.PropertyName == FM_PROP_BOUNDFIELD )
1302 {
1303 Reference<XPropertySet> xOldBound;
1304 evt.OldValue >>= xOldBound;
1305 if ( !xOldBound.is() && evt.NewValue.hasValue() )
1306 {
1307 Reference< XControlModel > xControlModel(evt.Source,UNO_QUERY);
1308 Reference< XControl > xControl = findControl(m_aControls,xControlModel,sal_False,sal_False);
1309 if ( xControl.is() )
1310 {
1311 startControlModifyListening( xControl );
1312 Reference<XPropertySet> xProp(xControlModel,UNO_QUERY);
1313 if ( xProp.is() )
1314 xProp->removePropertyChangeListener(FM_PROP_BOUNDFIELD, this);
1315 }
1316 }
1317 }
1318 else
1319 {
1320 sal_Bool bModifiedChanged = (evt.PropertyName == FM_PROP_ISMODIFIED);
1321 sal_Bool bNewChanged = (evt.PropertyName == FM_PROP_ISNEW);
1322 if (bModifiedChanged || bNewChanged)
1323 {
1324 ::osl::MutexGuard aGuard( m_aMutex );
1325 if (bModifiedChanged)
1326 m_bCurrentRecordModified = ::comphelper::getBOOL(evt.NewValue);
1327 else
1328 m_bCurrentRecordNew = ::comphelper::getBOOL(evt.NewValue);
1329
1330 // toggle the locking
1331 if (m_bLocked != determineLockState())
1332 {
1333 m_bLocked = !m_bLocked;
1334 setLocks();
1335 if (isListeningForChanges())
1336 startListening();
1337 else
1338 stopListening();
1339 }
1340
1341 if ( bNewChanged )
1342 m_aToggleEvent.Call();
1343
1344 if (!m_bCurrentRecordModified)
1345 m_bModified = sal_False;
1346 }
1347 else if ( evt.PropertyName == FM_PROP_DYNAMIC_CONTROL_BORDER )
1348 {
1349 bool bEnable = lcl_shouldUseDynamicControlBorder( evt.Source, evt.NewValue );
1350 if ( bEnable )
1351 {
1352 m_pControlBorderManager->enableDynamicBorderColor();
1353 if ( m_xActiveControl.is() )
1354 m_pControlBorderManager->focusGained( m_xActiveControl.get() );
1355 }
1356 else
1357 {
1358 m_pControlBorderManager->disableDynamicBorderColor();
1359 }
1360 }
1361 }
1362 }
1363
1364 //------------------------------------------------------------------------------
replaceControl(const Reference<XControl> & _rxExistentControl,const Reference<XControl> & _rxNewControl)1365 bool FormController::replaceControl( const Reference< XControl >& _rxExistentControl, const Reference< XControl >& _rxNewControl )
1366 {
1367 bool bSuccess = false;
1368 try
1369 {
1370 Reference< XIdentifierReplace > xContainer( getContainer(), UNO_QUERY );
1371 DBG_ASSERT( xContainer.is(), "FormController::replaceControl: yes, it's not required by the service description, but XItentifierReplaces would be nice!" );
1372 if ( xContainer.is() )
1373 {
1374 // look up the ID of _rxExistentControl
1375 Sequence< sal_Int32 > aIdentifiers( xContainer->getIdentifiers() );
1376 const sal_Int32* pIdentifiers = aIdentifiers.getConstArray();
1377 const sal_Int32* pIdentifiersEnd = aIdentifiers.getConstArray() + aIdentifiers.getLength();
1378 for ( ; pIdentifiers != pIdentifiersEnd; ++pIdentifiers )
1379 {
1380 Reference< XControl > xCheck( xContainer->getByIdentifier( *pIdentifiers ), UNO_QUERY );
1381 if ( xCheck == _rxExistentControl )
1382 break;
1383 }
1384 DBG_ASSERT( pIdentifiers != pIdentifiersEnd, "FormController::replaceControl: did not find the control in the container!" );
1385 if ( pIdentifiers != pIdentifiersEnd )
1386 {
1387 bool bReplacedWasActive = ( m_xActiveControl.get() == _rxExistentControl.get() );
1388 bool bReplacedWasCurrent = ( m_xCurrentControl.get() == _rxExistentControl.get() );
1389
1390 if ( bReplacedWasActive )
1391 {
1392 m_xActiveControl = NULL;
1393 implSetCurrentControl( NULL );
1394 }
1395 else if ( bReplacedWasCurrent )
1396 {
1397 implSetCurrentControl( _rxNewControl );
1398 }
1399
1400 // carry over the model
1401 _rxNewControl->setModel( _rxExistentControl->getModel() );
1402
1403 xContainer->replaceByIdentifer( *pIdentifiers, makeAny( _rxNewControl ) );
1404 bSuccess = true;
1405
1406 if ( bReplacedWasActive )
1407 {
1408 Reference< XWindow > xControlWindow( _rxNewControl, UNO_QUERY );
1409 if ( xControlWindow.is() )
1410 xControlWindow->setFocus();
1411 }
1412 }
1413 }
1414 }
1415 catch( const Exception& )
1416 {
1417 DBG_UNHANDLED_EXCEPTION();
1418 }
1419
1420 Reference< XControl > xDisposeIt( bSuccess ? _rxExistentControl : _rxNewControl );
1421 ::comphelper::disposeComponent( xDisposeIt );
1422 return bSuccess;
1423 }
1424
1425 //------------------------------------------------------------------------------
toggleAutoFields(sal_Bool bAutoFields)1426 void FormController::toggleAutoFields(sal_Bool bAutoFields)
1427 {
1428 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1429
1430
1431 Sequence< Reference< XControl > > aControlsCopy( m_aControls );
1432 const Reference< XControl >* pControls = aControlsCopy.getConstArray();
1433 sal_Int32 nControls = aControlsCopy.getLength();
1434
1435 if (bAutoFields)
1436 {
1437 // as we don't want new controls to be attached to the scripting environment
1438 // we change attach flags
1439 m_bAttachEvents = sal_False;
1440 for (sal_Int32 i = nControls; i > 0;)
1441 {
1442 Reference< XControl > xControl = pControls[--i];
1443 if (xControl.is())
1444 {
1445 Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
1446 if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
1447 {
1448 // does the model use a bound field ?
1449 Reference< XPropertySet > xField;
1450 xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
1451
1452 // is it a autofield?
1453 if ( xField.is()
1454 && ::comphelper::hasProperty( FM_PROP_AUTOINCREMENT, xField )
1455 && ::comphelper::getBOOL( xField->getPropertyValue( FM_PROP_AUTOINCREMENT ) )
1456 )
1457 {
1458 replaceControl( xControl, new FmXAutoControl( m_aContext ) );
1459 }
1460 }
1461 }
1462 }
1463 m_bAttachEvents = sal_True;
1464 }
1465 else
1466 {
1467 m_bDetachEvents = sal_False;
1468 for (sal_Int32 i = nControls; i > 0;)
1469 {
1470 Reference< XControl > xControl = pControls[--i];
1471 if (xControl.is())
1472 {
1473 Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
1474 if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
1475 {
1476 // does the model use a bound field ?
1477 Reference< XPropertySet > xField;
1478 xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
1479
1480 // is it a autofield?
1481 if ( xField.is()
1482 && ::comphelper::hasProperty( FM_PROP_AUTOINCREMENT, xField )
1483 && ::comphelper::getBOOL( xField->getPropertyValue(FM_PROP_AUTOINCREMENT ) )
1484 )
1485 {
1486 ::rtl::OUString sServiceName;
1487 OSL_VERIFY( xSet->getPropertyValue( FM_PROP_DEFAULTCONTROL ) >>= sServiceName );
1488 Reference< XControl > xNewControl( m_aContext.createComponent( sServiceName ), UNO_QUERY );
1489 replaceControl( xControl, xNewControl );
1490 }
1491 }
1492 }
1493 }
1494 m_bDetachEvents = sal_True;
1495 }
1496 }
1497
1498 //------------------------------------------------------------------------------
IMPL_LINK(FormController,OnToggleAutoFields,void *,EMPTYARG)1499 IMPL_LINK(FormController, OnToggleAutoFields, void*, EMPTYARG)
1500 {
1501 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1502
1503 toggleAutoFields(m_bCurrentRecordNew);
1504 return 1L;
1505 }
1506
1507 // XTextListener
1508 //------------------------------------------------------------------------------
textChanged(const TextEvent & e)1509 void SAL_CALL FormController::textChanged(const TextEvent& e)
1510 {
1511 // SYNCHRONIZED -->
1512 ::osl::ClearableMutexGuard aGuard( m_aMutex );
1513 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1514 if ( !m_bFiltering )
1515 {
1516 impl_onModify();
1517 return;
1518 }
1519
1520 if ( m_bSuspendFilterTextListening )
1521 return;
1522
1523 Reference< XTextComponent > xText(e.Source,UNO_QUERY);
1524 ::rtl::OUString aText = xText->getText();
1525
1526 if ( m_aFilterRows.empty() )
1527 appendEmptyDisjunctiveTerm();
1528
1529 // Suchen der aktuellen Row
1530 if ( ( (size_t)m_nCurrentFilterPosition >= m_aFilterRows.size() ) || ( m_nCurrentFilterPosition < 0 ) )
1531 {
1532 OSL_ENSURE( false, "FormController::textChanged: m_nCurrentFilterPosition is wrong!" );
1533 return;
1534 }
1535
1536 FmFilterRow& rRow = m_aFilterRows[ m_nCurrentFilterPosition ];
1537
1538 // do we have a new filter
1539 if (aText.getLength())
1540 rRow[xText] = aText;
1541 else
1542 {
1543 // do we have the control in the row
1544 FmFilterRow::iterator iter = rRow.find(xText);
1545 // erase the entry out of the row
1546 if (iter != rRow.end())
1547 rRow.erase(iter);
1548 }
1549
1550 // multiplex the event to our FilterControllerListeners
1551 FilterEvent aEvent;
1552 aEvent.Source = *this;
1553 aEvent.FilterComponent = ::std::find( m_aFilterComponents.begin(), m_aFilterComponents.end(), xText ) - m_aFilterComponents.begin();
1554 aEvent.DisjunctiveTerm = getActiveTerm();
1555 aEvent.PredicateExpression = aText;
1556
1557 aGuard.clear();
1558 // <-- SYNCHRONIZED
1559
1560 // notify the changed filter expression
1561 m_aFilterListeners.notifyEach( &XFilterControllerListener::predicateExpressionChanged, aEvent );
1562 }
1563
1564 // XItemListener
1565 //------------------------------------------------------------------------------
itemStateChanged(const ItemEvent &)1566 void SAL_CALL FormController::itemStateChanged(const ItemEvent& /*rEvent*/)
1567 {
1568 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1569 impl_onModify();
1570 }
1571
1572 // XModificationBroadcaster
1573 //------------------------------------------------------------------------------
addModifyListener(const Reference<XModifyListener> & l)1574 void SAL_CALL FormController::addModifyListener(const Reference< XModifyListener > & l)
1575 {
1576 ::osl::MutexGuard aGuard( m_aMutex );
1577 impl_checkDisposed_throw();
1578 m_aModifyListeners.addInterface( l );
1579 }
1580
1581 //------------------------------------------------------------------------------
removeModifyListener(const Reference<XModifyListener> & l)1582 void FormController::removeModifyListener(const Reference< XModifyListener > & l)
1583 {
1584 ::osl::MutexGuard aGuard( m_aMutex );
1585 impl_checkDisposed_throw();
1586 m_aModifyListeners.removeInterface( l );
1587 }
1588
1589 // XModificationListener
1590 //------------------------------------------------------------------------------
modified(const EventObject & _rEvent)1591 void FormController::modified( const EventObject& _rEvent )
1592 {
1593 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1594
1595 try
1596 {
1597 if ( _rEvent.Source != m_xActiveControl )
1598 { // let this control grab the focus
1599 // (this case may happen if somebody moves the scroll wheel of the mouse over a control
1600 // which does not have the focus)
1601 // 85511 - 29.05.2001 - frank.schoenheit@germany.sun.com
1602 //
1603 // also, it happens when an image control gets a new image by double-clicking it
1604 // #i88458# / 2009-01-12 / frank.schoenheit@sun.com
1605 Reference< XWindow > xControlWindow( _rEvent.Source, UNO_QUERY_THROW );
1606 xControlWindow->setFocus();
1607 }
1608 }
1609 catch( const Exception& )
1610 {
1611 DBG_UNHANDLED_EXCEPTION();
1612 }
1613
1614 impl_onModify();
1615 }
1616
1617 //------------------------------------------------------------------------------
impl_checkDisposed_throw() const1618 void FormController::impl_checkDisposed_throw() const
1619 {
1620 if ( impl_isDisposed_nofail() )
1621 throw DisposedException( ::rtl::OUString(), *const_cast< FormController* >( this ) );
1622 }
1623
1624 //------------------------------------------------------------------------------
impl_onModify()1625 void FormController::impl_onModify()
1626 {
1627 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1628
1629 {
1630 ::osl::MutexGuard aGuard( m_aMutex );
1631 if ( !m_bModified )
1632 m_bModified = sal_True;
1633 }
1634
1635 EventObject aEvt(static_cast<cppu::OWeakObject*>(this));
1636 m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvt );
1637 }
1638
1639 //------------------------------------------------------------------------------
impl_addFilterRow(const FmFilterRow & _row)1640 void FormController::impl_addFilterRow( const FmFilterRow& _row )
1641 {
1642 m_aFilterRows.push_back( _row );
1643
1644 if ( m_aFilterRows.size() == 1 )
1645 { // that's the first row ever
1646 OSL_ENSURE( m_nCurrentFilterPosition == -1, "FormController::impl_addFilterRow: inconsistency!" );
1647 m_nCurrentFilterPosition = 0;
1648 }
1649 }
1650
1651 //------------------------------------------------------------------------------
impl_appendEmptyFilterRow(::osl::ClearableMutexGuard & _rClearBeforeNotify)1652 void FormController::impl_appendEmptyFilterRow( ::osl::ClearableMutexGuard& _rClearBeforeNotify )
1653 {
1654 // SYNCHRONIZED -->
1655 impl_addFilterRow( FmFilterRow() );
1656
1657 // notify the listeners
1658 FilterEvent aEvent;
1659 aEvent.Source = *this;
1660 aEvent.DisjunctiveTerm = (sal_Int32)m_aFilterRows.size() - 1;
1661 _rClearBeforeNotify.clear();
1662 // <-- SYNCHRONIZED
1663 m_aFilterListeners.notifyEach( &XFilterControllerListener::disjunctiveTermAdded, aEvent );
1664 }
1665
1666 //------------------------------------------------------------------------------
determineLockState() const1667 sal_Bool FormController::determineLockState() const
1668 {
1669 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1670 // a.) in filter mode we are always locked
1671 // b.) if we have no valid model or our model (a result set) is not alive -> we're locked
1672 // c.) if we are inserting everything is OK and we are not locked
1673 // d.) if are not updatable or on invalid position
1674 Reference< XResultSet > xResultSet(m_xModelAsIndex, UNO_QUERY);
1675 if (m_bFiltering || !xResultSet.is() || !isRowSetAlive(xResultSet))
1676 return sal_True;
1677 else
1678 return (m_bCanInsert && m_bCurrentRecordNew) ? sal_False
1679 : xResultSet->isBeforeFirst() || xResultSet->isAfterLast() || xResultSet->rowDeleted() || !m_bCanUpdate;
1680 }
1681
1682 // FocusListener
1683 //------------------------------------------------------------------------------
focusGained(const FocusEvent & e)1684 void FormController::focusGained(const FocusEvent& e)
1685 {
1686 // SYNCHRONIZED -->
1687 ::osl::ClearableMutexGuard aGuard( m_aMutex );
1688 impl_checkDisposed_throw();
1689
1690 m_pControlBorderManager->focusGained( e.Source );
1691
1692 Reference< XControl > xControl(e.Source, UNO_QUERY);
1693 if (m_bDBConnection)
1694 {
1695 // do we need to keep the locking of the commit
1696 // we hold the lock as long as the control differs from the current
1697 // otherwise we disabled the lock
1698 m_bCommitLock = m_bCommitLock && (XControl*)xControl.get() != (XControl*)m_xCurrentControl.get();
1699 if (m_bCommitLock)
1700 return;
1701
1702 // when do we have to commit a value to form or a filter
1703 // a.) if the current value is modified
1704 // b.) there must be a current control
1705 // c.) and it must be different from the new focus owning control or
1706 // d.) the focus is moving around (so we have only one control)
1707
1708 if ( ( m_bModified || m_bFiltering )
1709 && m_xCurrentControl.is()
1710 && ( ( xControl.get() != m_xCurrentControl.get() )
1711 || ( ( e.FocusFlags & FocusChangeReason::AROUND )
1712 && ( m_bCycle || m_bFiltering )
1713 )
1714 )
1715 )
1716 {
1717 // check the old control if the content is ok
1718 #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
1719 Reference< XBoundControl > xLockingTest(m_xCurrentControl, UNO_QUERY);
1720 sal_Bool bControlIsLocked = xLockingTest.is() && xLockingTest->getLock();
1721 OSL_ENSURE(!bControlIsLocked, "FormController::Gained: I'm modified and the current control is locked ? How this ?");
1722 // normalerweise sollte ein gelocktes Control nicht modified sein, also muss wohl mein bModified aus einem anderen Kontext
1723 // gesetzt worden sein, was ich nicht verstehen wuerde ...
1724 #endif
1725 DBG_ASSERT(m_xCurrentControl.is(), "kein CurrentControl gesetzt");
1726 // zunaechst das Control fragen ob es das IFace unterstuetzt
1727 Reference< XBoundComponent > xBound(m_xCurrentControl, UNO_QUERY);
1728 if (!xBound.is() && m_xCurrentControl.is())
1729 xBound = Reference< XBoundComponent > (m_xCurrentControl->getModel(), UNO_QUERY);
1730
1731 // lock if we lose the focus during commit
1732 m_bCommitLock = sal_True;
1733
1734 // Commit nicht erfolgreich, Focus zuruecksetzen
1735 if (xBound.is() && !xBound->commit())
1736 {
1737 // the commit failed and we don't commit again until the current control
1738 // which couldn't be commit gains the focus again
1739 Reference< XWindow > xWindow(m_xCurrentControl, UNO_QUERY);
1740 if (xWindow.is())
1741 xWindow->setFocus();
1742 return;
1743 }
1744 else
1745 {
1746 m_bModified = sal_False;
1747 m_bCommitLock = sal_False;
1748 }
1749 }
1750
1751 if (!m_bFiltering && m_bCycle && (e.FocusFlags & FocusChangeReason::AROUND) && m_xCurrentControl.is())
1752 {
1753 SQLErrorEvent aErrorEvent;
1754 OSL_ENSURE( m_xFormOperations.is(), "FormController::focusGained: hmm?" );
1755 // should have been created in setModel
1756 try
1757 {
1758 if ( e.FocusFlags & FocusChangeReason::FORWARD )
1759 {
1760 if ( m_xFormOperations.is() && m_xFormOperations->isEnabled( FormFeature::MoveToNext ) )
1761 m_xFormOperations->execute( FormFeature::MoveToNext );
1762 }
1763 else // backward
1764 {
1765 if ( m_xFormOperations.is() && m_xFormOperations->isEnabled( FormFeature::MoveToPrevious ) )
1766 m_xFormOperations->execute( FormFeature::MoveToPrevious );
1767 }
1768 }
1769 catch ( const Exception& )
1770 {
1771 // don't handle this any further. That's an ... admissible error.
1772 DBG_UNHANDLED_EXCEPTION();
1773 }
1774 }
1775 }
1776
1777 // Immer noch ein und dasselbe Control
1778 if ( ( m_xActiveControl == xControl )
1779 && ( xControl == m_xCurrentControl )
1780 )
1781 {
1782 DBG_ASSERT(m_xCurrentControl.is(), "Kein CurrentControl selektiert");
1783 return;
1784 }
1785
1786 sal_Bool bActivated = !m_xActiveControl.is() && xControl.is();
1787
1788 m_xActiveControl = xControl;
1789
1790 implSetCurrentControl( xControl );
1791 OSL_POSTCOND( m_xCurrentControl.is(), "implSetCurrentControl did nonsense!" );
1792
1793 if ( bActivated )
1794 {
1795 // (asynchronously) call activation handlers
1796 m_aActivationEvent.Call();
1797
1798 // call modify listeners
1799 if ( m_bModified )
1800 m_aModifyListeners.notifyEach( &XModifyListener::modified, EventObject( *this ) );
1801 }
1802
1803 // invalidate all features which depend on the currently focused control
1804 if ( m_bDBConnection && !m_bFiltering )
1805 implInvalidateCurrentControlDependentFeatures();
1806
1807 if ( !m_xCurrentControl.is() )
1808 return;
1809
1810 // Control erhaelt Focus, dann eventuell in den sichtbaren Bereich
1811 Reference< XFormControllerContext > xContext( m_xContext );
1812 Reference< XControl > xCurrentControl( m_xCurrentControl );
1813 aGuard.clear();
1814 // <-- SYNCHRONIZED
1815
1816 if ( xContext.is() )
1817 xContext->makeVisible( xCurrentControl );
1818 }
1819
1820 //------------------------------------------------------------------------------
1821 IMPL_LINK( FormController, OnActivated, void*, /**/ )
1822 {
1823 EventObject aEvent;
1824 aEvent.Source = *this;
1825 m_aActivateListeners.notifyEach( &XFormControllerListener::formActivated, aEvent );
1826
1827 return 0L;
1828 }
1829
1830 //------------------------------------------------------------------------------
1831 IMPL_LINK( FormController, OnDeactivated, void*, /**/ )
1832 {
1833 EventObject aEvent;
1834 aEvent.Source = *this;
1835 m_aActivateListeners.notifyEach( &XFormControllerListener::formDeactivated, aEvent );
1836
1837 return 0L;
1838 }
1839
1840 //------------------------------------------------------------------------------
focusLost(const FocusEvent & e)1841 void FormController::focusLost(const FocusEvent& e)
1842 {
1843 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
1844
1845 m_pControlBorderManager->focusLost( e.Source );
1846
1847 Reference< XControl > xControl(e.Source, UNO_QUERY);
1848 Reference< XWindowPeer > xNext(e.NextFocus, UNO_QUERY);
1849 Reference< XControl > xNextControl = isInList(xNext);
1850 if (!xNextControl.is())
1851 {
1852 m_xActiveControl = NULL;
1853 m_aDeactivationEvent.Call();
1854 }
1855 }
1856
1857 //--------------------------------------------------------------------
mousePressed(const awt::MouseEvent &)1858 void SAL_CALL FormController::mousePressed( const awt::MouseEvent& /*_rEvent*/ )
1859 {
1860 // not interested in
1861 }
1862
1863 //--------------------------------------------------------------------
mouseReleased(const awt::MouseEvent &)1864 void SAL_CALL FormController::mouseReleased( const awt::MouseEvent& /*_rEvent*/ )
1865 {
1866 // not interested in
1867 }
1868
1869 //--------------------------------------------------------------------
mouseEntered(const awt::MouseEvent & _rEvent)1870 void SAL_CALL FormController::mouseEntered( const awt::MouseEvent& _rEvent )
1871 {
1872 m_pControlBorderManager->mouseEntered( _rEvent.Source );
1873 }
1874
1875 //--------------------------------------------------------------------
mouseExited(const awt::MouseEvent & _rEvent)1876 void SAL_CALL FormController::mouseExited( const awt::MouseEvent& _rEvent )
1877 {
1878 m_pControlBorderManager->mouseExited( _rEvent.Source );
1879 }
1880
1881 //--------------------------------------------------------------------
componentValidityChanged(const EventObject & _rSource)1882 void SAL_CALL FormController::componentValidityChanged( const EventObject& _rSource )
1883 {
1884 Reference< XControl > xControl( findControl( m_aControls, Reference< XControlModel >( _rSource.Source, UNO_QUERY ), sal_False, sal_False ) );
1885 Reference< XValidatableFormComponent > xValidatable( _rSource.Source, UNO_QUERY );
1886
1887 OSL_ENSURE( xControl.is() && xValidatable.is(), "FormController::componentValidityChanged: huh?" );
1888
1889 if ( xControl.is() && xValidatable.is() )
1890 m_pControlBorderManager->validityChanged( xControl, xValidatable );
1891 }
1892
1893 //--------------------------------------------------------------------
setModel(const Reference<XTabControllerModel> & Model)1894 void FormController::setModel(const Reference< XTabControllerModel > & Model)
1895 {
1896 ::osl::MutexGuard aGuard( m_aMutex );
1897 impl_checkDisposed_throw();
1898
1899 DBG_ASSERT(m_xTabController.is(), "FormController::setModel : invalid aggregate !");
1900
1901 try
1902 {
1903 // disconnect from the old model
1904 if (m_xModelAsIndex.is())
1905 {
1906 if (m_bDBConnection)
1907 {
1908 // we are currently working on the model
1909 EventObject aEvt(m_xModelAsIndex);
1910 unloaded(aEvt);
1911 }
1912
1913 Reference< XLoadable > xForm(m_xModelAsIndex, UNO_QUERY);
1914 if (xForm.is())
1915 xForm->removeLoadListener(this);
1916
1917 Reference< XSQLErrorBroadcaster > xBroadcaster(m_xModelAsIndex, UNO_QUERY);
1918 if (xBroadcaster.is())
1919 xBroadcaster->removeSQLErrorListener(this);
1920
1921 Reference< XDatabaseParameterBroadcaster > xParamBroadcaster(m_xModelAsIndex, UNO_QUERY);
1922 if (xParamBroadcaster.is())
1923 xParamBroadcaster->removeParameterListener(this);
1924
1925 }
1926
1927 disposeAllFeaturesAndDispatchers();
1928
1929 if ( m_xFormOperations.is() )
1930 m_xFormOperations->dispose();
1931 m_xFormOperations.clear();
1932
1933 // set the new model wait for the load event
1934 if (m_xTabController.is())
1935 m_xTabController->setModel(Model);
1936 m_xModelAsIndex = Reference< XIndexAccess > (Model, UNO_QUERY);
1937 m_xModelAsManager = Reference< XEventAttacherManager > (Model, UNO_QUERY);
1938
1939 // only if both ifaces exit, the controller will work successful
1940 if (!m_xModelAsIndex.is() || !m_xModelAsManager.is())
1941 {
1942 m_xModelAsManager = NULL;
1943 m_xModelAsIndex = NULL;
1944 }
1945
1946 if (m_xModelAsIndex.is())
1947 {
1948 // re-create m_xFormOperations
1949 m_xFormOperations.set( FormOperations::createWithFormController( m_aContext.getUNOContext(), this ), UNO_SET_THROW );
1950 m_xFormOperations->setFeatureInvalidation( this );
1951
1952 // adding load and ui interaction listeners
1953 Reference< XLoadable > xForm(Model, UNO_QUERY);
1954 if (xForm.is())
1955 xForm->addLoadListener(this);
1956
1957 Reference< XSQLErrorBroadcaster > xBroadcaster(Model, UNO_QUERY);
1958 if (xBroadcaster.is())
1959 xBroadcaster->addSQLErrorListener(this);
1960
1961 Reference< XDatabaseParameterBroadcaster > xParamBroadcaster(Model, UNO_QUERY);
1962 if (xParamBroadcaster.is())
1963 xParamBroadcaster->addParameterListener(this);
1964
1965 // well, is the database already loaded?
1966 // then we have to simulate a load event
1967 Reference< XLoadable > xCursor(m_xModelAsIndex, UNO_QUERY);
1968 if (xCursor.is() && xCursor->isLoaded())
1969 {
1970 EventObject aEvt(xCursor);
1971 loaded(aEvt);
1972 }
1973
1974 Reference< XPropertySet > xModelProps( m_xModelAsIndex, UNO_QUERY );
1975 Reference< XPropertySetInfo > xPropInfo( xModelProps->getPropertySetInfo() );
1976 if ( xPropInfo.is()
1977 && xPropInfo->hasPropertyByName( FM_PROP_DYNAMIC_CONTROL_BORDER )
1978 && xPropInfo->hasPropertyByName( FM_PROP_CONTROL_BORDER_COLOR_FOCUS )
1979 && xPropInfo->hasPropertyByName( FM_PROP_CONTROL_BORDER_COLOR_MOUSE )
1980 && xPropInfo->hasPropertyByName( FM_PROP_CONTROL_BORDER_COLOR_INVALID )
1981 )
1982 {
1983 bool bEnableDynamicControlBorder = lcl_shouldUseDynamicControlBorder(
1984 xModelProps.get(), xModelProps->getPropertyValue( FM_PROP_DYNAMIC_CONTROL_BORDER ) );
1985 if ( bEnableDynamicControlBorder )
1986 m_pControlBorderManager->enableDynamicBorderColor();
1987 else
1988 m_pControlBorderManager->disableDynamicBorderColor();
1989
1990 sal_Int32 nColor = 0;
1991 if ( xModelProps->getPropertyValue( FM_PROP_CONTROL_BORDER_COLOR_FOCUS ) >>= nColor )
1992 m_pControlBorderManager->setStatusColor( CONTROL_STATUS_FOCUSED, nColor );
1993 if ( xModelProps->getPropertyValue( FM_PROP_CONTROL_BORDER_COLOR_MOUSE ) >>= nColor )
1994 m_pControlBorderManager->setStatusColor( CONTROL_STATUS_MOUSE_HOVER, nColor );
1995 if ( xModelProps->getPropertyValue( FM_PROP_CONTROL_BORDER_COLOR_INVALID ) >>= nColor )
1996 m_pControlBorderManager->setStatusColor( CONTROL_STATUS_INVALID, nColor );
1997 }
1998 }
1999 }
2000 catch( const Exception& )
2001 {
2002 DBG_UNHANDLED_EXCEPTION();
2003 }
2004 }
2005
2006 //------------------------------------------------------------------------------
getModel()2007 Reference< XTabControllerModel > FormController::getModel()
2008 {
2009 ::osl::MutexGuard aGuard( m_aMutex );
2010 impl_checkDisposed_throw();
2011
2012 DBG_ASSERT(m_xTabController.is(), "FormController::getModel : invalid aggregate !");
2013 if (!m_xTabController.is())
2014 return Reference< XTabControllerModel > ();
2015 return m_xTabController->getModel();
2016 }
2017
2018 //------------------------------------------------------------------------------
addToEventAttacher(const Reference<XControl> & xControl)2019 void FormController::addToEventAttacher(const Reference< XControl > & xControl)
2020 {
2021 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2022 OSL_ENSURE( xControl.is(), "FormController::addToEventAttacher: invalid control - how did you reach this?" );
2023 if ( !xControl.is() )
2024 return; /* throw IllegalArgumentException(); */
2025
2026 // anmelden beim Eventattacher
2027 Reference< XFormComponent > xComp(xControl->getModel(), UNO_QUERY);
2028 if (xComp.is() && m_xModelAsIndex.is())
2029 {
2030 // Und die Position des ControlModel darin suchen
2031 sal_uInt32 nPos = m_xModelAsIndex->getCount();
2032 Reference< XFormComponent > xTemp;
2033 for( ; nPos; )
2034 {
2035 m_xModelAsIndex->getByIndex(--nPos) >>= xTemp;
2036 if ((XFormComponent*)xComp.get() == (XFormComponent*)xTemp.get())
2037 {
2038 Reference< XInterface > xIfc(xControl, UNO_QUERY);
2039 m_xModelAsManager->attach( nPos, xIfc, makeAny(xControl) );
2040 break;
2041 }
2042 }
2043 }
2044 }
2045
2046 //------------------------------------------------------------------------------
removeFromEventAttacher(const Reference<XControl> & xControl)2047 void FormController::removeFromEventAttacher(const Reference< XControl > & xControl)
2048 {
2049 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2050 OSL_ENSURE( xControl.is(), "FormController::removeFromEventAttacher: invalid control - how did you reach this?" );
2051 if ( !xControl.is() )
2052 return; /* throw IllegalArgumentException(); */
2053
2054 // abmelden beim Eventattacher
2055 Reference< XFormComponent > xComp(xControl->getModel(), UNO_QUERY);
2056 if ( xComp.is() && m_xModelAsIndex.is() )
2057 {
2058 // Und die Position des ControlModel darin suchen
2059 sal_uInt32 nPos = m_xModelAsIndex->getCount();
2060 Reference< XFormComponent > xTemp;
2061 for( ; nPos; )
2062 {
2063 m_xModelAsIndex->getByIndex(--nPos) >>= xTemp;
2064 if ((XFormComponent*)xComp.get() == (XFormComponent*)xTemp.get())
2065 {
2066 Reference< XInterface > xIfc(xControl, UNO_QUERY);
2067 m_xModelAsManager->detach( nPos, xIfc );
2068 break;
2069 }
2070 }
2071 }
2072 }
2073
2074 //------------------------------------------------------------------------------
setContainer(const Reference<XControlContainer> & xContainer)2075 void FormController::setContainer(const Reference< XControlContainer > & xContainer)
2076 {
2077 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2078 Reference< XTabControllerModel > xTabModel(getModel());
2079 DBG_ASSERT(xTabModel.is() || !xContainer.is(), "No Model defined");
2080 // if we have a new container we need a model
2081 DBG_ASSERT(m_xTabController.is(), "FormController::setContainer : invalid aggregate !");
2082
2083 ::osl::MutexGuard aGuard( m_aMutex );
2084 Reference< XContainer > xCurrentContainer;
2085 if (m_xTabController.is())
2086 xCurrentContainer = Reference< XContainer > (m_xTabController->getContainer(), UNO_QUERY);
2087 if (xCurrentContainer.is())
2088 {
2089 xCurrentContainer->removeContainerListener(this);
2090
2091 if ( m_aTabActivationTimer.IsActive() )
2092 m_aTabActivationTimer.Stop();
2093
2094 // clear the filter map
2095 ::std::for_each( m_aFilterComponents.begin(), m_aFilterComponents.end(), RemoveComponentTextListener( this ) );
2096 m_aFilterComponents.clear();
2097
2098 // einsammeln der Controls
2099 const Reference< XControl >* pControls = m_aControls.getConstArray();
2100 const Reference< XControl >* pControlsEnd = pControls + m_aControls.getLength();
2101 while ( pControls != pControlsEnd )
2102 implControlRemoved( *pControls++, true );
2103
2104 // Datenbank spezifische Dinge vornehmen
2105 if (m_bDBConnection && isListeningForChanges())
2106 stopListening();
2107
2108 m_aControls.realloc( 0 );
2109 }
2110
2111 if (m_xTabController.is())
2112 m_xTabController->setContainer(xContainer);
2113
2114 // Welche Controls gehoeren zum Container ?
2115 if (xContainer.is() && xTabModel.is())
2116 {
2117 Sequence< Reference< XControlModel > > aModels = xTabModel->getControlModels();
2118 const Reference< XControlModel > * pModels = aModels.getConstArray();
2119 Sequence< Reference< XControl > > aAllControls = xContainer->getControls();
2120
2121 sal_Int32 nCount = aModels.getLength();
2122 m_aControls = Sequence< Reference< XControl > >( nCount );
2123 Reference< XControl > * pControls = m_aControls.getArray();
2124
2125 // einsammeln der Controls
2126 sal_Int32 i, j;
2127 for (i = 0, j = 0; i < nCount; ++i, ++pModels )
2128 {
2129 Reference< XControl > xControl = findControl( aAllControls, *pModels, sal_False, sal_True );
2130 if ( xControl.is() )
2131 {
2132 pControls[j++] = xControl;
2133 implControlInserted( xControl, true );
2134 }
2135 }
2136
2137 // not every model had an associated control
2138 if (j != i)
2139 m_aControls.realloc(j);
2140
2141 // am Container horchen
2142 Reference< XContainer > xNewContainer(xContainer, UNO_QUERY);
2143 if (xNewContainer.is())
2144 xNewContainer->addContainerListener(this);
2145
2146 // Datenbank spezifische Dinge vornehmen
2147 if (m_bDBConnection)
2148 {
2149 m_bLocked = determineLockState();
2150 setLocks();
2151 if (!isLocked())
2152 startListening();
2153 }
2154 }
2155 // befinden sich die Controls in der richtigen Reihenfolge
2156 m_bControlsSorted = sal_True;
2157 }
2158
2159 //------------------------------------------------------------------------------
getContainer()2160 Reference< XControlContainer > FormController::getContainer()
2161 {
2162 ::osl::MutexGuard aGuard( m_aMutex );
2163 impl_checkDisposed_throw();
2164
2165 DBG_ASSERT(m_xTabController.is(), "FormController::getContainer : invalid aggregate !");
2166 if (!m_xTabController.is())
2167 return Reference< XControlContainer > ();
2168 return m_xTabController->getContainer();
2169 }
2170
2171 //------------------------------------------------------------------------------
getControls(void)2172 Sequence< Reference< XControl > > FormController::getControls(void)
2173 {
2174 ::osl::MutexGuard aGuard( m_aMutex );
2175 impl_checkDisposed_throw();
2176
2177 if (!m_bControlsSorted)
2178 {
2179 Reference< XTabControllerModel > xModel = getModel();
2180 if (!xModel.is())
2181 return m_aControls;
2182
2183 Sequence< Reference< XControlModel > > aControlModels = xModel->getControlModels();
2184 const Reference< XControlModel > * pModels = aControlModels.getConstArray();
2185 sal_Int32 nModels = aControlModels.getLength();
2186
2187 Sequence< Reference< XControl > > aNewControls(nModels);
2188
2189 Reference< XControl > * pControls = aNewControls.getArray();
2190 Reference< XControl > xControl;
2191
2192 // Umsortieren der Controls entsprechend der TabReihenfolge
2193 sal_Int32 j = 0;
2194 for (sal_Int32 i = 0; i < nModels; ++i, ++pModels )
2195 {
2196 xControl = findControl( m_aControls, *pModels, sal_True, sal_True );
2197 if ( xControl.is() )
2198 pControls[j++] = xControl;
2199 }
2200
2201 // not every model had an associated control
2202 if ( j != nModels )
2203 aNewControls.realloc( j );
2204
2205 m_aControls = aNewControls;
2206 m_bControlsSorted = sal_True;
2207 }
2208 return m_aControls;
2209 }
2210
2211 //------------------------------------------------------------------------------
autoTabOrder()2212 void FormController::autoTabOrder()
2213 {
2214 ::osl::MutexGuard aGuard( m_aMutex );
2215 impl_checkDisposed_throw();
2216
2217 DBG_ASSERT(m_xTabController.is(), "FormController::autoTabOrder : invalid aggregate !");
2218 if (m_xTabController.is())
2219 m_xTabController->autoTabOrder();
2220 }
2221
2222 //------------------------------------------------------------------------------
activateTabOrder()2223 void FormController::activateTabOrder()
2224 {
2225 ::osl::MutexGuard aGuard( m_aMutex );
2226 impl_checkDisposed_throw();
2227
2228 DBG_ASSERT(m_xTabController.is(), "FormController::activateTabOrder : invalid aggregate !");
2229 if (m_xTabController.is())
2230 m_xTabController->activateTabOrder();
2231 }
2232
2233 //------------------------------------------------------------------------------
setControlLock(const Reference<XControl> & xControl)2234 void FormController::setControlLock(const Reference< XControl > & xControl)
2235 {
2236 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2237 sal_Bool bLocked = isLocked();
2238
2239 // es wird gelockt
2240 // a.) wenn der ganze Datensatz gesperrt ist
2241 // b.) wenn das zugehoerige Feld gespeert ist
2242 Reference< XBoundControl > xBound(xControl, UNO_QUERY);
2243 if (xBound.is() && (( (bLocked && bLocked != xBound->getLock()) ||
2244 !bLocked))) // beim entlocken immer einzelne Felder ueberpr�fen
2245 {
2246 // gibt es eine Datenquelle
2247 Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
2248 if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
2249 {
2250 // wie sieht mit den Properties ReadOnly und Enable aus
2251 sal_Bool bTouch = sal_True;
2252 if (::comphelper::hasProperty(FM_PROP_ENABLED, xSet))
2253 bTouch = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ENABLED));
2254 if (::comphelper::hasProperty(FM_PROP_READONLY, xSet))
2255 bTouch = !::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_READONLY));
2256
2257 if (bTouch)
2258 {
2259 Reference< XPropertySet > xField;
2260 xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
2261 if (xField.is())
2262 {
2263 if (bLocked)
2264 xBound->setLock(bLocked);
2265 else
2266 {
2267 try
2268 {
2269 Any aVal = xField->getPropertyValue(FM_PROP_ISREADONLY);
2270 if (aVal.hasValue() && ::comphelper::getBOOL(aVal))
2271 xBound->setLock(sal_True);
2272 else
2273 xBound->setLock(bLocked);
2274 }
2275 catch( const Exception& )
2276 {
2277 DBG_UNHANDLED_EXCEPTION();
2278 }
2279
2280 }
2281 }
2282 }
2283 }
2284 }
2285 }
2286
2287 //------------------------------------------------------------------------------
setLocks()2288 void FormController::setLocks()
2289 {
2290 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2291 // alle Controls, die mit einer Datenquelle verbunden sind locken/unlocken
2292 const Reference< XControl >* pControls = m_aControls.getConstArray();
2293 const Reference< XControl >* pControlsEnd = pControls + m_aControls.getLength();
2294 while ( pControls != pControlsEnd )
2295 setControlLock( *pControls++ );
2296 }
2297
2298 //------------------------------------------------------------------------------
2299 namespace
2300 {
lcl_shouldListenForModifications(const Reference<XControl> & _rxControl,const Reference<XPropertyChangeListener> & _rxBoundFieldListener)2301 bool lcl_shouldListenForModifications( const Reference< XControl >& _rxControl, const Reference< XPropertyChangeListener >& _rxBoundFieldListener )
2302 {
2303 bool bShould = false;
2304
2305 Reference< XBoundComponent > xBound( _rxControl, UNO_QUERY );
2306 if ( xBound.is() )
2307 {
2308 bShould = true;
2309 }
2310 else if ( _rxControl.is() )
2311 {
2312 Reference< XPropertySet > xModelProps( _rxControl->getModel(), UNO_QUERY );
2313 if ( xModelProps.is() && ::comphelper::hasProperty( FM_PROP_BOUNDFIELD, xModelProps ) )
2314 {
2315 Reference< XPropertySet > xField;
2316 xModelProps->getPropertyValue( FM_PROP_BOUNDFIELD ) >>= xField;
2317 bShould = xField.is();
2318
2319 if ( !bShould && _rxBoundFieldListener.is() )
2320 xModelProps->addPropertyChangeListener( FM_PROP_BOUNDFIELD, _rxBoundFieldListener );
2321 }
2322 }
2323
2324 return bShould;
2325 }
2326 }
2327
2328 //------------------------------------------------------------------------------
startControlModifyListening(const Reference<XControl> & xControl)2329 void FormController::startControlModifyListening(const Reference< XControl > & xControl)
2330 {
2331 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2332
2333 bool bModifyListening = lcl_shouldListenForModifications( xControl, this );
2334
2335 // artificial while
2336 while ( bModifyListening )
2337 {
2338 Reference< XModifyBroadcaster > xMod(xControl, UNO_QUERY);
2339 if (xMod.is())
2340 {
2341 xMod->addModifyListener(this);
2342 break;
2343 }
2344
2345 // alle die Text um vorzeitig ein modified zu erkennen
2346 Reference< XTextComponent > xText(xControl, UNO_QUERY);
2347 if (xText.is())
2348 {
2349 xText->addTextListener(this);
2350 break;
2351 }
2352
2353 Reference< XCheckBox > xBox(xControl, UNO_QUERY);
2354 if (xBox.is())
2355 {
2356 xBox->addItemListener(this);
2357 break;
2358 }
2359
2360 Reference< XComboBox > xCbBox(xControl, UNO_QUERY);
2361 if (xCbBox.is())
2362 {
2363 xCbBox->addItemListener(this);
2364 break;
2365 }
2366
2367 Reference< XListBox > xListBox(xControl, UNO_QUERY);
2368 if (xListBox.is())
2369 {
2370 xListBox->addItemListener(this);
2371 break;
2372 }
2373 break;
2374 }
2375 }
2376
2377 //------------------------------------------------------------------------------
stopControlModifyListening(const Reference<XControl> & xControl)2378 void FormController::stopControlModifyListening(const Reference< XControl > & xControl)
2379 {
2380 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2381
2382 bool bModifyListening = lcl_shouldListenForModifications( xControl, NULL );
2383
2384 // kuenstliches while
2385 while (bModifyListening)
2386 {
2387 Reference< XModifyBroadcaster > xMod(xControl, UNO_QUERY);
2388 if (xMod.is())
2389 {
2390 xMod->removeModifyListener(this);
2391 break;
2392 }
2393 // alle die Text um vorzeitig ein modified zu erkennen
2394 Reference< XTextComponent > xText(xControl, UNO_QUERY);
2395 if (xText.is())
2396 {
2397 xText->removeTextListener(this);
2398 break;
2399 }
2400
2401 Reference< XCheckBox > xBox(xControl, UNO_QUERY);
2402 if (xBox.is())
2403 {
2404 xBox->removeItemListener(this);
2405 break;
2406 }
2407
2408 Reference< XComboBox > xCbBox(xControl, UNO_QUERY);
2409 if (xCbBox.is())
2410 {
2411 xCbBox->removeItemListener(this);
2412 break;
2413 }
2414
2415 Reference< XListBox > xListBox(xControl, UNO_QUERY);
2416 if (xListBox.is())
2417 {
2418 xListBox->removeItemListener(this);
2419 break;
2420 }
2421 break;
2422 }
2423 }
2424
2425 //------------------------------------------------------------------------------
startListening()2426 void FormController::startListening()
2427 {
2428 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2429 m_bModified = sal_False;
2430
2431 // jetzt anmelden bei gebundenen feldern
2432 const Reference< XControl >* pControls = m_aControls.getConstArray();
2433 const Reference< XControl >* pControlsEnd = pControls + m_aControls.getLength();
2434 while ( pControls != pControlsEnd )
2435 startControlModifyListening( *pControls++ );
2436 }
2437
2438 //------------------------------------------------------------------------------
stopListening()2439 void FormController::stopListening()
2440 {
2441 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2442 m_bModified = sal_False;
2443
2444 // jetzt anmelden bei gebundenen feldern
2445 const Reference< XControl >* pControls = m_aControls.getConstArray();
2446 const Reference< XControl >* pControlsEnd = pControls + m_aControls.getLength();
2447 while ( pControls != pControlsEnd )
2448 stopControlModifyListening( *pControls++ );
2449 }
2450
2451
2452 //------------------------------------------------------------------------------
findControl(Sequence<Reference<XControl>> & _rControls,const Reference<XControlModel> & xCtrlModel,sal_Bool _bRemove,sal_Bool _bOverWrite) const2453 Reference< XControl > FormController::findControl(Sequence< Reference< XControl > >& _rControls, const Reference< XControlModel > & xCtrlModel ,sal_Bool _bRemove,sal_Bool _bOverWrite) const
2454 {
2455 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2456 DBG_ASSERT( xCtrlModel.is(), "findControl - welches ?!" );
2457
2458 Reference< XControl >* pControls = _rControls.getArray();
2459 Reference< XControlModel > xModel;
2460 for ( sal_Int32 i = 0, nCount = _rControls.getLength(); i < nCount; ++i, ++pControls )
2461 {
2462 if ( pControls->is() )
2463 {
2464 xModel = (*pControls)->getModel();
2465 if ( xModel.get() == xCtrlModel.get() )
2466 {
2467 Reference< XControl > xControl( *pControls );
2468 if ( _bRemove )
2469 ::comphelper::removeElementAt( _rControls, i );
2470 else if ( _bOverWrite )
2471 *pControls = Reference< XControl >();
2472 return xControl;
2473 }
2474 }
2475 }
2476 return Reference< XControl > ();
2477 }
2478
2479 //------------------------------------------------------------------------------
implControlInserted(const Reference<XControl> & _rxControl,bool _bAddToEventAttacher)2480 void FormController::implControlInserted( const Reference< XControl>& _rxControl, bool _bAddToEventAttacher )
2481 {
2482 Reference< XWindow > xWindow( _rxControl, UNO_QUERY );
2483 if ( xWindow.is() )
2484 {
2485 xWindow->addFocusListener( this );
2486 xWindow->addMouseListener( this );
2487
2488 if ( _bAddToEventAttacher )
2489 addToEventAttacher( _rxControl );
2490 }
2491
2492 // add a dispatch interceptor to the control (if supported)
2493 Reference< XDispatchProviderInterception > xInterception( _rxControl, UNO_QUERY );
2494 if ( xInterception.is() )
2495 createInterceptor( xInterception );
2496
2497 if ( _rxControl.is() )
2498 {
2499 Reference< XControlModel > xModel( _rxControl->getModel() );
2500
2501 // we want to know about the reset of the model of our controls
2502 // (for correctly resetting m_bModified)
2503 Reference< XReset > xReset( xModel, UNO_QUERY );
2504 if ( xReset.is() )
2505 xReset->addResetListener( this );
2506
2507 // and we want to know about the validity, to visually indicate it
2508 Reference< XValidatableFormComponent > xValidatable( xModel, UNO_QUERY );
2509 if ( xValidatable.is() )
2510 {
2511 xValidatable->addFormComponentValidityListener( this );
2512 m_pControlBorderManager->validityChanged( _rxControl, xValidatable );
2513 }
2514 }
2515
2516 }
2517
2518 //------------------------------------------------------------------------------
implControlRemoved(const Reference<XControl> & _rxControl,bool _bRemoveFromEventAttacher)2519 void FormController::implControlRemoved( const Reference< XControl>& _rxControl, bool _bRemoveFromEventAttacher )
2520 {
2521 Reference< XWindow > xWindow( _rxControl, UNO_QUERY );
2522 if ( xWindow.is() )
2523 {
2524 xWindow->removeFocusListener( this );
2525 xWindow->removeMouseListener( this );
2526
2527 if ( _bRemoveFromEventAttacher )
2528 removeFromEventAttacher( _rxControl );
2529 }
2530
2531 Reference< XDispatchProviderInterception > xInterception( _rxControl, UNO_QUERY);
2532 if ( xInterception.is() )
2533 deleteInterceptor( xInterception );
2534
2535 if ( _rxControl.is() )
2536 {
2537 Reference< XControlModel > xModel( _rxControl->getModel() );
2538
2539 Reference< XReset > xReset( xModel, UNO_QUERY );
2540 if ( xReset.is() )
2541 xReset->removeResetListener( this );
2542
2543 Reference< XValidatableFormComponent > xValidatable( xModel, UNO_QUERY );
2544 if ( xValidatable.is() )
2545 xValidatable->removeFormComponentValidityListener( this );
2546 }
2547 }
2548
2549 //------------------------------------------------------------------------------
implSetCurrentControl(const Reference<XControl> & _rxControl)2550 void FormController::implSetCurrentControl( const Reference< XControl >& _rxControl )
2551 {
2552 if ( m_xCurrentControl.get() == _rxControl.get() )
2553 return;
2554
2555 Reference< XGridControl > xGridControl( m_xCurrentControl, UNO_QUERY );
2556 if ( xGridControl.is() )
2557 xGridControl->removeGridControlListener( this );
2558
2559 m_xCurrentControl = _rxControl;
2560
2561 xGridControl.set( m_xCurrentControl, UNO_QUERY );
2562 if ( xGridControl.is() )
2563 xGridControl->addGridControlListener( this );
2564 }
2565
2566 //------------------------------------------------------------------------------
insertControl(const Reference<XControl> & xControl)2567 void FormController::insertControl(const Reference< XControl > & xControl)
2568 {
2569 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2570 m_bControlsSorted = sal_False;
2571 m_aControls.realloc(m_aControls.getLength() + 1);
2572 m_aControls.getArray()[m_aControls.getLength() - 1] = xControl;
2573
2574 if ( m_pColumnInfoCache.get() )
2575 m_pColumnInfoCache->deinitializeControls();
2576
2577 implControlInserted( xControl, m_bAttachEvents );
2578
2579 if (m_bDBConnection && !m_bFiltering)
2580 setControlLock(xControl);
2581
2582 if (isListeningForChanges() && m_bAttachEvents)
2583 startControlModifyListening( xControl );
2584 }
2585
2586 //------------------------------------------------------------------------------
removeControl(const Reference<XControl> & xControl)2587 void FormController::removeControl(const Reference< XControl > & xControl)
2588 {
2589 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2590 const Reference< XControl >* pControls = m_aControls.getConstArray();
2591 const Reference< XControl >* pControlsEnd = pControls + m_aControls.getLength();
2592 while ( pControls != pControlsEnd )
2593 {
2594 if ( xControl.get() == (*pControls++).get() )
2595 {
2596 ::comphelper::removeElementAt( m_aControls, pControls - m_aControls.getConstArray() - 1 );
2597 break;
2598 }
2599 }
2600
2601 FilterComponents::iterator componentPos = ::std::find( m_aFilterComponents.begin(), m_aFilterComponents.end(), xControl );
2602 if ( componentPos != m_aFilterComponents.end() )
2603 m_aFilterComponents.erase( componentPos );
2604
2605 implControlRemoved( xControl, m_bDetachEvents );
2606
2607 if ( isListeningForChanges() && m_bDetachEvents )
2608 stopControlModifyListening( xControl );
2609 }
2610
2611 // XLoadListener
2612 //------------------------------------------------------------------------------
loaded(const EventObject & rEvent)2613 void FormController::loaded(const EventObject& rEvent)
2614 {
2615 OSL_ENSURE( rEvent.Source == m_xModelAsIndex, "FormController::loaded: where did this come from?" );
2616
2617 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2618 ::osl::MutexGuard aGuard( m_aMutex );
2619 Reference< XRowSet > xForm(rEvent.Source, UNO_QUERY);
2620 // do we have a connected data source
2621 OStaticDataAccessTools aStaticTools;
2622 if (xForm.is() && aStaticTools.getRowSetConnection(xForm).is())
2623 {
2624 Reference< XPropertySet > xSet(xForm, UNO_QUERY);
2625 if (xSet.is())
2626 {
2627 Any aVal = xSet->getPropertyValue(FM_PROP_CYCLE);
2628 sal_Int32 aVal2 = 0;
2629 ::cppu::enum2int(aVal2,aVal);
2630 m_bCycle = !aVal.hasValue() || aVal2 == TabulatorCycle_RECORDS;
2631 m_bCanUpdate = aStaticTools.canUpdate(xSet);
2632 m_bCanInsert = aStaticTools.canInsert(xSet);
2633 m_bCurrentRecordModified = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISMODIFIED));
2634 m_bCurrentRecordNew = ::comphelper::getBOOL(xSet->getPropertyValue(FM_PROP_ISNEW));
2635
2636 startFormListening( xSet, sal_False );
2637
2638 // set the locks for the current controls
2639 if (getContainer().is())
2640 {
2641 m_aLoadEvent.Call();
2642 }
2643 }
2644 else
2645 {
2646 m_bCanInsert = m_bCanUpdate = m_bCycle = sal_False;
2647 m_bCurrentRecordModified = sal_False;
2648 m_bCurrentRecordNew = sal_False;
2649 m_bLocked = sal_False;
2650 }
2651 m_bDBConnection = sal_True;
2652 }
2653 else
2654 {
2655 m_bDBConnection = sal_False;
2656 m_bCanInsert = m_bCanUpdate = m_bCycle = sal_False;
2657 m_bCurrentRecordModified = sal_False;
2658 m_bCurrentRecordNew = sal_False;
2659 m_bLocked = sal_False;
2660 }
2661
2662 Reference< XColumnsSupplier > xFormColumns( xForm, UNO_QUERY );
2663 m_pColumnInfoCache.reset( xFormColumns.is() ? new ColumnInfoCache( xFormColumns ) : NULL );
2664
2665 updateAllDispatchers();
2666 }
2667
2668 //------------------------------------------------------------------------------
updateAllDispatchers() const2669 void FormController::updateAllDispatchers() const
2670 {
2671 ::std::for_each(
2672 m_aFeatureDispatchers.begin(),
2673 m_aFeatureDispatchers.end(),
2674 ::std::compose1(
2675 UpdateAllListeners(),
2676 ::std::select2nd< DispatcherContainer::value_type >()
2677 )
2678 );
2679 }
2680
2681 //------------------------------------------------------------------------------
IMPL_LINK(FormController,OnLoad,void *,EMPTYARG)2682 IMPL_LINK(FormController, OnLoad, void*, EMPTYARG)
2683 {
2684 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2685 m_bLocked = determineLockState();
2686
2687 setLocks();
2688
2689 if (!m_bLocked)
2690 startListening();
2691
2692 // just one exception toggle the auto values
2693 if (m_bCurrentRecordNew)
2694 toggleAutoFields(sal_True);
2695
2696 return 1L;
2697 }
2698
2699 //------------------------------------------------------------------------------
unloaded(const EventObject &)2700 void FormController::unloaded(const EventObject& /*rEvent*/)
2701 {
2702 ::osl::MutexGuard aGuard( m_aMutex );
2703 impl_checkDisposed_throw();
2704
2705 updateAllDispatchers();
2706 }
2707
2708 //------------------------------------------------------------------------------
reloading(const EventObject &)2709 void FormController::reloading(const EventObject& /*aEvent*/)
2710 {
2711 ::osl::MutexGuard aGuard( m_aMutex );
2712 impl_checkDisposed_throw();
2713
2714 // do the same like in unloading
2715 // just one exception toggle the auto values
2716 m_aToggleEvent.CancelPendingCall();
2717 unload();
2718 }
2719
2720 //------------------------------------------------------------------------------
reloaded(const EventObject & aEvent)2721 void FormController::reloaded(const EventObject& aEvent)
2722 {
2723 ::osl::MutexGuard aGuard( m_aMutex );
2724 impl_checkDisposed_throw();
2725
2726 loaded(aEvent);
2727 }
2728
2729 //------------------------------------------------------------------------------
unloading(const EventObject &)2730 void FormController::unloading(const EventObject& /*aEvent*/)
2731 {
2732 ::osl::MutexGuard aGuard( m_aMutex );
2733 impl_checkDisposed_throw();
2734
2735 unload();
2736 }
2737
2738 //------------------------------------------------------------------------------
unload()2739 void FormController::unload()
2740 {
2741 ::osl::MutexGuard aGuard( m_aMutex );
2742 impl_checkDisposed_throw();
2743
2744 m_aLoadEvent.CancelPendingCall();
2745
2746 // be sure not to have autofields
2747 if (m_bCurrentRecordNew)
2748 toggleAutoFields(sal_False);
2749
2750 // remove bound field listing again
2751 removeBoundFieldListener();
2752
2753 if (m_bDBConnection && isListeningForChanges())
2754 stopListening();
2755
2756 Reference< XPropertySet > xSet( m_xModelAsIndex, UNO_QUERY );
2757 if ( m_bDBConnection && xSet.is() )
2758 stopFormListening( xSet, sal_False );
2759
2760 m_bDBConnection = sal_False;
2761 m_bCanInsert = m_bCanUpdate = m_bCycle = sal_False;
2762 m_bCurrentRecordModified = m_bCurrentRecordNew = m_bLocked = sal_False;
2763
2764 m_pColumnInfoCache.reset();
2765 }
2766
2767 // -----------------------------------------------------------------------------
removeBoundFieldListener()2768 void FormController::removeBoundFieldListener()
2769 {
2770 const Reference< XControl >* pControls = m_aControls.getConstArray();
2771 const Reference< XControl >* pControlsEnd = pControls + m_aControls.getLength();
2772 while ( pControls != pControlsEnd )
2773 {
2774 Reference< XPropertySet > xProp( *pControls++, UNO_QUERY );
2775 if ( xProp.is() )
2776 xProp->removePropertyChangeListener( FM_PROP_BOUNDFIELD, this );
2777 }
2778 }
2779
2780 //------------------------------------------------------------------------------
startFormListening(const Reference<XPropertySet> & _rxForm,sal_Bool _bPropertiesOnly)2781 void FormController::startFormListening( const Reference< XPropertySet >& _rxForm, sal_Bool _bPropertiesOnly )
2782 {
2783 try
2784 {
2785 if ( m_bCanInsert || m_bCanUpdate ) // form can be modified
2786 {
2787 _rxForm->addPropertyChangeListener( FM_PROP_ISNEW, this );
2788 _rxForm->addPropertyChangeListener( FM_PROP_ISMODIFIED, this );
2789
2790 if ( !_bPropertiesOnly )
2791 {
2792 // set the Listener for UI interaction
2793 Reference< XRowSetApproveBroadcaster > xApprove( _rxForm, UNO_QUERY );
2794 if ( xApprove.is() )
2795 xApprove->addRowSetApproveListener( this );
2796
2797 // listener for row set changes
2798 Reference< XRowSet > xRowSet( _rxForm, UNO_QUERY );
2799 if ( xRowSet.is() )
2800 xRowSet->addRowSetListener( this );
2801 }
2802 }
2803
2804 Reference< XPropertySetInfo > xInfo = _rxForm->getPropertySetInfo();
2805 if ( xInfo.is() && xInfo->hasPropertyByName( FM_PROP_DYNAMIC_CONTROL_BORDER ) )
2806 _rxForm->addPropertyChangeListener( FM_PROP_DYNAMIC_CONTROL_BORDER, this );
2807 }
2808 catch( const Exception& )
2809 {
2810 DBG_UNHANDLED_EXCEPTION();
2811 }
2812 }
2813
2814 //------------------------------------------------------------------------------
stopFormListening(const Reference<XPropertySet> & _rxForm,sal_Bool _bPropertiesOnly)2815 void FormController::stopFormListening( const Reference< XPropertySet >& _rxForm, sal_Bool _bPropertiesOnly )
2816 {
2817 try
2818 {
2819 if ( m_bCanInsert || m_bCanUpdate )
2820 {
2821 _rxForm->removePropertyChangeListener( FM_PROP_ISNEW, this );
2822 _rxForm->removePropertyChangeListener( FM_PROP_ISMODIFIED, this );
2823
2824 if ( !_bPropertiesOnly )
2825 {
2826 Reference< XRowSetApproveBroadcaster > xApprove( _rxForm, UNO_QUERY );
2827 if (xApprove.is())
2828 xApprove->removeRowSetApproveListener(this);
2829
2830 Reference< XRowSet > xRowSet( _rxForm, UNO_QUERY );
2831 if ( xRowSet.is() )
2832 xRowSet->removeRowSetListener( this );
2833 }
2834 }
2835
2836 Reference< XPropertySetInfo > xInfo = _rxForm->getPropertySetInfo();
2837 if ( xInfo.is() && xInfo->hasPropertyByName( FM_PROP_DYNAMIC_CONTROL_BORDER ) )
2838 _rxForm->removePropertyChangeListener( FM_PROP_DYNAMIC_CONTROL_BORDER, this );
2839 }
2840 catch( const Exception& )
2841 {
2842 DBG_UNHANDLED_EXCEPTION();
2843 }
2844 }
2845
2846 // com::sun::star::sdbc::XRowSetListener
2847 //------------------------------------------------------------------------------
cursorMoved(const EventObject &)2848 void FormController::cursorMoved(const EventObject& /*event*/)
2849 {
2850 ::osl::MutexGuard aGuard( m_aMutex );
2851 impl_checkDisposed_throw();
2852
2853 // toggle the locking ?
2854 if (m_bLocked != determineLockState())
2855 {
2856 m_bLocked = !m_bLocked;
2857 setLocks();
2858 if (isListeningForChanges())
2859 startListening();
2860 else
2861 stopListening();
2862 }
2863
2864 // neither the current control nor the current record are modified anymore
2865 m_bCurrentRecordModified = m_bModified = sal_False;
2866 }
2867
2868 //------------------------------------------------------------------------------
rowChanged(const EventObject &)2869 void FormController::rowChanged(const EventObject& /*event*/)
2870 {
2871 // not interested in ...
2872 }
2873 //------------------------------------------------------------------------------
rowSetChanged(const EventObject &)2874 void FormController::rowSetChanged(const EventObject& /*event*/)
2875 {
2876 // not interested in ...
2877 }
2878
2879
2880 // XContainerListener
2881 //------------------------------------------------------------------------------
elementInserted(const ContainerEvent & evt)2882 void SAL_CALL FormController::elementInserted(const ContainerEvent& evt)
2883 {
2884 ::osl::MutexGuard aGuard( m_aMutex );
2885 impl_checkDisposed_throw();
2886
2887 Reference< XControl > xControl( evt.Element, UNO_QUERY );
2888 if ( !xControl.is() )
2889 return;
2890
2891 Reference< XFormComponent > xModel(xControl->getModel(), UNO_QUERY);
2892 if (xModel.is() && m_xModelAsIndex == xModel->getParent())
2893 {
2894 insertControl(xControl);
2895
2896 if ( m_aTabActivationTimer.IsActive() )
2897 m_aTabActivationTimer.Stop();
2898
2899 m_aTabActivationTimer.Start();
2900 }
2901 // are we in filtermode and a XModeSelector has inserted an element
2902 else if (m_bFiltering && Reference< XModeSelector > (evt.Source, UNO_QUERY).is())
2903 {
2904 xModel = Reference< XFormComponent > (evt.Source, UNO_QUERY);
2905 if (xModel.is() && m_xModelAsIndex == xModel->getParent())
2906 {
2907 Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
2908 if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
2909 {
2910 // does the model use a bound field ?
2911 Reference< XPropertySet > xField;
2912 xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
2913
2914 Reference< XTextComponent > xText(xControl, UNO_QUERY);
2915 // may we filter the field?
2916 if (xText.is() && xField.is() && ::comphelper::hasProperty(FM_PROP_SEARCHABLE, xField) &&
2917 ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_SEARCHABLE)))
2918 {
2919 m_aFilterComponents.push_back( xText );
2920 xText->addTextListener( this );
2921 }
2922 }
2923 }
2924 }
2925 }
2926
2927 //------------------------------------------------------------------------------
elementReplaced(const ContainerEvent & evt)2928 void SAL_CALL FormController::elementReplaced(const ContainerEvent& evt)
2929 {
2930 // simulate an elementRemoved
2931 ContainerEvent aRemoveEvent( evt );
2932 aRemoveEvent.Element = evt.ReplacedElement;
2933 aRemoveEvent.ReplacedElement = Any();
2934 elementRemoved( aRemoveEvent );
2935
2936 // simulate an elementInserted
2937 ContainerEvent aInsertEvent( evt );
2938 aInsertEvent.ReplacedElement = Any();
2939 elementInserted( aInsertEvent );
2940 }
2941
2942 //------------------------------------------------------------------------------
elementRemoved(const ContainerEvent & evt)2943 void SAL_CALL FormController::elementRemoved(const ContainerEvent& evt)
2944 {
2945 ::osl::MutexGuard aGuard( m_aMutex );
2946 impl_checkDisposed_throw();
2947
2948 Reference< XControl > xControl;
2949 evt.Element >>= xControl;
2950 if (!xControl.is())
2951 return;
2952
2953 Reference< XFormComponent > xModel(xControl->getModel(), UNO_QUERY);
2954 if (xModel.is() && m_xModelAsIndex == xModel->getParent())
2955 {
2956 removeControl(xControl);
2957 // TabOrder nicht neu berechnen, da das intern schon funktionieren mu�!
2958 }
2959 // are we in filtermode and a XModeSelector has inserted an element
2960 else if (m_bFiltering && Reference< XModeSelector > (evt.Source, UNO_QUERY).is())
2961 {
2962 FilterComponents::iterator componentPos = ::std::find(
2963 m_aFilterComponents.begin(), m_aFilterComponents.end(), xControl );
2964 if ( componentPos != m_aFilterComponents.end() )
2965 m_aFilterComponents.erase( componentPos );
2966 }
2967 }
2968
2969 //------------------------------------------------------------------------------
isInList(const Reference<XWindowPeer> & xPeer) const2970 Reference< XControl > FormController::isInList(const Reference< XWindowPeer > & xPeer) const
2971 {
2972 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
2973 const Reference< XControl >* pControls = m_aControls.getConstArray();
2974
2975 sal_uInt32 nCtrls = m_aControls.getLength();
2976 for ( sal_uInt32 n = 0; n < nCtrls && xPeer.is(); ++n, ++pControls )
2977 {
2978 if ( pControls->is() )
2979 {
2980 Reference< XVclWindowPeer > xCtrlPeer( (*pControls)->getPeer(), UNO_QUERY);
2981 if ( ( xCtrlPeer.get() == xPeer.get() ) || xCtrlPeer->isChild( xPeer ) )
2982 return *pControls;
2983 }
2984 }
2985 return Reference< XControl > ();
2986 }
2987
2988 //------------------------------------------------------------------------------
activateFirst()2989 void FormController::activateFirst()
2990 {
2991 ::osl::MutexGuard aGuard( m_aMutex );
2992 impl_checkDisposed_throw();
2993
2994 DBG_ASSERT(m_xTabController.is(), "FormController::activateFirst : invalid aggregate !");
2995 if (m_xTabController.is())
2996 m_xTabController->activateFirst();
2997 }
2998
2999 //------------------------------------------------------------------------------
activateLast()3000 void FormController::activateLast()
3001 {
3002 ::osl::MutexGuard aGuard( m_aMutex );
3003 impl_checkDisposed_throw();
3004
3005 DBG_ASSERT(m_xTabController.is(), "FormController::activateLast : invalid aggregate !");
3006 if (m_xTabController.is())
3007 m_xTabController->activateLast();
3008 }
3009
3010 // XFormController
3011 //------------------------------------------------------------------------------
getFormOperations()3012 Reference< XFormOperations > SAL_CALL FormController::getFormOperations()
3013 {
3014 ::osl::MutexGuard aGuard( m_aMutex );
3015 impl_checkDisposed_throw();
3016
3017 return m_xFormOperations;
3018 }
3019
3020 //------------------------------------------------------------------------------
getCurrentControl(void)3021 Reference< XControl> SAL_CALL FormController::getCurrentControl(void)
3022 {
3023 ::osl::MutexGuard aGuard( m_aMutex );
3024 impl_checkDisposed_throw();
3025 return m_xCurrentControl;
3026 }
3027
3028 //------------------------------------------------------------------------------
addActivateListener(const Reference<XFormControllerListener> & l)3029 void SAL_CALL FormController::addActivateListener(const Reference< XFormControllerListener > & l)
3030 {
3031 ::osl::MutexGuard aGuard( m_aMutex );
3032 impl_checkDisposed_throw();
3033 m_aActivateListeners.addInterface(l);
3034 }
3035 //------------------------------------------------------------------------------
removeActivateListener(const Reference<XFormControllerListener> & l)3036 void SAL_CALL FormController::removeActivateListener(const Reference< XFormControllerListener > & l)
3037 {
3038 ::osl::MutexGuard aGuard( m_aMutex );
3039 impl_checkDisposed_throw();
3040 m_aActivateListeners.removeInterface(l);
3041 }
3042
3043 //------------------------------------------------------------------------------
addChildController(const Reference<XFormController> & _ChildController)3044 void SAL_CALL FormController::addChildController( const Reference< XFormController >& _ChildController )
3045 {
3046 ::osl::MutexGuard aGuard( m_aMutex );
3047 impl_checkDisposed_throw();
3048
3049 if ( !_ChildController.is() )
3050 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
3051 // TODO: (localized) error message
3052
3053 // the parent of our (to-be-)child must be our own model
3054 Reference< XFormComponent > xFormOfChild( _ChildController->getModel(), UNO_QUERY );
3055 if ( !xFormOfChild.is() )
3056 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
3057 // TODO: (localized) error message
3058
3059 if ( xFormOfChild->getParent() != m_xModelAsIndex )
3060 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
3061 // TODO: (localized) error message
3062
3063 m_aChilds.push_back( _ChildController );
3064 _ChildController->setParent( *this );
3065
3066 // search the position of the model within the form
3067 sal_uInt32 nPos = m_xModelAsIndex->getCount();
3068 Reference< XFormComponent > xTemp;
3069 for( ; nPos; )
3070 {
3071 m_xModelAsIndex->getByIndex(--nPos) >>= xTemp;
3072 if ( xFormOfChild == xTemp )
3073 {
3074 Reference< XInterface > xIfc( _ChildController, UNO_QUERY );
3075 m_xModelAsManager->attach( nPos, xIfc, makeAny( _ChildController) );
3076 break;
3077 }
3078 }
3079 }
3080
3081 //------------------------------------------------------------------------------
getContext()3082 Reference< XFormControllerContext > SAL_CALL FormController::getContext()
3083 {
3084 ::osl::MutexGuard aGuard( m_aMutex );
3085 impl_checkDisposed_throw();
3086 return m_xContext;
3087 }
3088
3089 //------------------------------------------------------------------------------
setContext(const Reference<XFormControllerContext> & _context)3090 void SAL_CALL FormController::setContext( const Reference< XFormControllerContext >& _context )
3091 {
3092 ::osl::MutexGuard aGuard( m_aMutex );
3093 impl_checkDisposed_throw();
3094 m_xContext = _context;
3095 }
3096
3097 //------------------------------------------------------------------------------
getInteractionHandler()3098 Reference< XInteractionHandler > SAL_CALL FormController::getInteractionHandler()
3099 {
3100 ::osl::MutexGuard aGuard( m_aMutex );
3101 impl_checkDisposed_throw();
3102 return m_xInteractionHandler;
3103 }
3104
3105 //------------------------------------------------------------------------------
setInteractionHandler(const Reference<XInteractionHandler> & _interactionHandler)3106 void SAL_CALL FormController::setInteractionHandler( const Reference< XInteractionHandler >& _interactionHandler )
3107 {
3108 ::osl::MutexGuard aGuard( m_aMutex );
3109 impl_checkDisposed_throw();
3110 m_xInteractionHandler = _interactionHandler;
3111 }
3112
3113 //------------------------------------------------------------------------------
setFilter(::std::vector<FmFieldInfo> & rFieldInfos)3114 void FormController::setFilter(::std::vector<FmFieldInfo>& rFieldInfos)
3115 {
3116 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3117 // create the composer
3118 Reference< XRowSet > xForm(m_xModelAsIndex, UNO_QUERY);
3119 Reference< XConnection > xConnection(OStaticDataAccessTools().getRowSetConnection(xForm));
3120 if (xForm.is())
3121 {
3122 try
3123 {
3124 Reference< XMultiServiceFactory > xFactory( xConnection, UNO_QUERY_THROW );
3125 m_xComposer.set(
3126 xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ),
3127 UNO_QUERY_THROW );
3128
3129 Reference< XPropertySet > xSet( xForm, UNO_QUERY );
3130 ::rtl::OUString sStatement = ::comphelper::getString( xSet->getPropertyValue( FM_PROP_ACTIVECOMMAND ) );
3131 ::rtl::OUString sFilter = ::comphelper::getString( xSet->getPropertyValue( FM_PROP_FILTER ) );
3132 m_xComposer->setElementaryQuery( sStatement );
3133 m_xComposer->setFilter( sFilter );
3134 }
3135 catch( const Exception& )
3136 {
3137 DBG_UNHANDLED_EXCEPTION();
3138 }
3139 }
3140
3141 if (m_xComposer.is())
3142 {
3143 Sequence < PropertyValue> aLevel;
3144 Sequence< Sequence < PropertyValue > > aFilterRows = m_xComposer->getStructuredFilter();
3145
3146 // ok, we receive the list of filters as sequence of fieldnames, value
3147 // now we have to transform the fieldname into UI names, that could be a label of the field or
3148 // a aliasname or the fieldname itself
3149
3150 // first adjust the field names if necessary
3151 Reference< XNameAccess > xQueryColumns =
3152 Reference< XColumnsSupplier >( m_xComposer, UNO_QUERY_THROW )->getColumns();
3153
3154 for (::std::vector<FmFieldInfo>::iterator iter = rFieldInfos.begin();
3155 iter != rFieldInfos.end(); iter++)
3156 {
3157 if ( xQueryColumns->hasByName((*iter).aFieldName) )
3158 {
3159 if ( (xQueryColumns->getByName((*iter).aFieldName) >>= (*iter).xField) && (*iter).xField.is() )
3160 (*iter).xField->getPropertyValue(FM_PROP_REALNAME) >>= (*iter).aFieldName;
3161 }
3162 }
3163
3164 Reference< XDatabaseMetaData> xMetaData(xConnection->getMetaData());
3165 // now transfer the filters into Value/TextComponent pairs
3166 ::comphelper::UStringMixEqual aCompare(xMetaData->storesMixedCaseQuotedIdentifiers());
3167
3168 // need to parse criteria localized
3169 OStaticDataAccessTools aStaticTools;
3170 Reference< XNumberFormatsSupplier> xFormatSupplier( aStaticTools.getNumberFormats(xConnection, sal_True));
3171 Reference< XNumberFormatter> xFormatter( m_aContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY );
3172 xFormatter->attachNumberFormatsSupplier(xFormatSupplier);
3173 Locale aAppLocale = Application::GetSettings().GetUILocale();
3174 LocaleDataWrapper aLocaleWrapper( m_aContext.getLegacyServiceFactory(), aAppLocale );
3175
3176 // retrieving the filter
3177 const Sequence < PropertyValue >* pRow = aFilterRows.getConstArray();
3178 for (sal_Int32 i = 0, nLen = aFilterRows.getLength(); i < nLen; ++i)
3179 {
3180 FmFilterRow aRow;
3181
3182 // search a field for the given name
3183 const PropertyValue* pRefValues = pRow[i].getConstArray();
3184 for (sal_Int32 j = 0, nLen1 = pRow[i].getLength(); j < nLen1; j++)
3185 {
3186 // look for the text component
3187 Reference< XPropertySet > xField;
3188 try
3189 {
3190 Reference< XPropertySet > xSet;
3191 ::rtl::OUString aRealName;
3192
3193 // first look with the given name
3194 if (xQueryColumns->hasByName(pRefValues[j].Name))
3195 {
3196 xQueryColumns->getByName(pRefValues[j].Name) >>= xSet;
3197
3198 // get the RealName
3199 xSet->getPropertyValue(::rtl::OUString::createFromAscii("RealName")) >>= aRealName;
3200
3201 // compare the condition field name and the RealName
3202 if (aCompare(aRealName, pRefValues[j].Name))
3203 xField = xSet;
3204 }
3205 if (!xField.is())
3206 {
3207 // no we have to check every column to find the realname
3208 Reference< XIndexAccess > xColumnsByIndex(xQueryColumns, UNO_QUERY);
3209 for (sal_Int32 n = 0, nCount = xColumnsByIndex->getCount(); n < nCount; n++)
3210 {
3211 xColumnsByIndex->getByIndex(n) >>= xSet;
3212 xSet->getPropertyValue(::rtl::OUString::createFromAscii("RealName")) >>= aRealName;
3213 if (aCompare(aRealName, pRefValues[j].Name))
3214 {
3215 // get the column by its alias
3216 xField = xSet;
3217 break;
3218 }
3219 }
3220 }
3221 if (!xField.is())
3222 continue;
3223 }
3224 catch (const Exception&)
3225 {
3226 continue;
3227 }
3228
3229 // find the text component
3230 for (::std::vector<FmFieldInfo>::iterator iter = rFieldInfos.begin();
3231 iter != rFieldInfos.end(); iter++)
3232 {
3233 // we found the field so insert a new entry to the filter row
3234 if ((*iter).xField == xField)
3235 {
3236 // do we already have the control ?
3237 if (aRow.find((*iter).xText) != aRow.end())
3238 {
3239 ::rtl::OUString aCompText = aRow[(*iter).xText];
3240 aCompText += ::rtl::OUString::createFromAscii(" ");
3241 ::rtl::OString aVal = m_xParser->getContext().getIntlKeywordAscii(OParseContext::KEY_AND);
3242 aCompText += ::rtl::OUString(aVal.getStr(),aVal.getLength(),RTL_TEXTENCODING_ASCII_US);
3243 aCompText += ::rtl::OUString::createFromAscii(" ");
3244 aCompText += ::comphelper::getString(pRefValues[j].Value);
3245 aRow[(*iter).xText] = aCompText;
3246 }
3247 else
3248 {
3249 ::rtl::OUString sPredicate,sErrorMsg;
3250 pRefValues[j].Value >>= sPredicate;
3251 ::rtl::Reference< ISQLParseNode > xParseNode = predicateTree(sErrorMsg, sPredicate, xFormatter, xField);
3252 if ( xParseNode.is() )
3253 {
3254 ::rtl::OUString sCriteria;
3255 xParseNode->parseNodeToPredicateStr( sCriteria
3256 ,xConnection
3257 ,xFormatter
3258 ,xField
3259 ,aAppLocale
3260 ,(sal_Char)aLocaleWrapper.getNumDecimalSep().GetChar(0)
3261 ,getParseContext());
3262 aRow[(*iter).xText] = sCriteria;
3263 }
3264 }
3265 }
3266 }
3267 }
3268
3269 if (aRow.empty())
3270 continue;
3271
3272 impl_addFilterRow( aRow );
3273 }
3274 }
3275
3276 // now set the filter controls
3277 for ( ::std::vector<FmFieldInfo>::iterator field = rFieldInfos.begin();
3278 field != rFieldInfos.end();
3279 ++field
3280 )
3281 {
3282 m_aFilterComponents.push_back( field->xText );
3283 }
3284 }
3285
3286 //------------------------------------------------------------------------------
startFiltering()3287 void FormController::startFiltering()
3288 {
3289 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3290
3291 OStaticDataAccessTools aStaticTools;
3292 Reference< XConnection > xConnection( aStaticTools.getRowSetConnection( Reference< XRowSet >( m_xModelAsIndex, UNO_QUERY ) ) );
3293 if ( !xConnection.is() )
3294 // nothing to do - can't filter a form which is not connected
3295 // 98023 - 19.03.2002 - fs@openoffice.org
3296 return;
3297
3298 // stop listening for controls
3299 if (isListeningForChanges())
3300 stopListening();
3301
3302 m_bFiltering = sal_True;
3303
3304 // as we don't want new controls to be attached to the scripting environment
3305 // we change attach flags
3306 m_bAttachEvents = sal_False;
3307
3308 // Austauschen der Kontrols fuer das aktuelle Formular
3309 Sequence< Reference< XControl > > aControlsCopy( m_aControls );
3310 const Reference< XControl >* pControls = aControlsCopy.getConstArray();
3311 sal_Int32 nControlCount = aControlsCopy.getLength();
3312
3313 // the control we have to activate after replacement
3314 Reference< XDatabaseMetaData > xMetaData(xConnection->getMetaData());
3315 Reference< XNumberFormatsSupplier > xFormatSupplier = aStaticTools.getNumberFormats(xConnection, sal_True);
3316 Reference< XNumberFormatter > xFormatter( m_aContext.createComponent( "com.sun.star.util.NumberFormatter" ), UNO_QUERY );
3317 xFormatter->attachNumberFormatsSupplier(xFormatSupplier);
3318
3319 // structure for storing the field info
3320 ::std::vector<FmFieldInfo> aFieldInfos;
3321
3322 for (sal_Int32 i = nControlCount; i > 0;)
3323 {
3324 Reference< XControl > xControl = pControls[--i];
3325 if (xControl.is())
3326 {
3327 // no events for the control anymore
3328 removeFromEventAttacher(xControl);
3329
3330 // do we have a mode selector
3331 Reference< XModeSelector > xSelector(xControl, UNO_QUERY);
3332 if (xSelector.is())
3333 {
3334 xSelector->setMode( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterMode" ) ) );
3335
3336 // listening for new controls of the selector
3337 Reference< XContainer > xContainer(xSelector, UNO_QUERY);
3338 if (xContainer.is())
3339 xContainer->addContainerListener(this);
3340
3341 Reference< XEnumerationAccess > xElementAccess(xSelector, UNO_QUERY);
3342 if (xElementAccess.is())
3343 {
3344 Reference< XEnumeration > xEnumeration(xElementAccess->createEnumeration());
3345 Reference< XControl > xSubControl;
3346 while (xEnumeration->hasMoreElements())
3347 {
3348 xEnumeration->nextElement() >>= xSubControl;
3349 if (xSubControl.is())
3350 {
3351 Reference< XPropertySet > xSet(xSubControl->getModel(), UNO_QUERY);
3352 if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
3353 {
3354 // does the model use a bound field ?
3355 Reference< XPropertySet > xField;
3356 xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
3357
3358 Reference< XTextComponent > xText(xSubControl, UNO_QUERY);
3359 // may we filter the field?
3360 if (xText.is() && xField.is() && ::comphelper::hasProperty(FM_PROP_SEARCHABLE, xField) &&
3361 ::comphelper::getBOOL(xField->getPropertyValue(FM_PROP_SEARCHABLE)))
3362 {
3363 aFieldInfos.push_back(FmFieldInfo(xField, xText));
3364 xText->addTextListener(this);
3365 }
3366 }
3367 }
3368 }
3369 }
3370 continue;
3371 }
3372
3373 Reference< XPropertySet > xModel( xControl->getModel(), UNO_QUERY );
3374 if (xModel.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xModel))
3375 {
3376 // does the model use a bound field ?
3377 Any aVal = xModel->getPropertyValue(FM_PROP_BOUNDFIELD);
3378 Reference< XPropertySet > xField;
3379 aVal >>= xField;
3380
3381 // may we filter the field?
3382
3383 if ( xField.is()
3384 && ::comphelper::hasProperty( FM_PROP_SEARCHABLE, xField )
3385 && ::comphelper::getBOOL( xField->getPropertyValue( FM_PROP_SEARCHABLE ) )
3386 )
3387 {
3388 // create a filter control
3389 Sequence< Any > aCreationArgs( 3 );
3390 aCreationArgs[ 0 ] <<= NamedValue( ::rtl::OUString::createFromAscii( "MessageParent" ), makeAny( VCLUnoHelper::GetInterface( getDialogParentWindow() ) ) );
3391 aCreationArgs[ 1 ] <<= NamedValue( ::rtl::OUString::createFromAscii( "NumberFormatter" ), makeAny( xFormatter ) );
3392 aCreationArgs[ 2 ] <<= NamedValue( ::rtl::OUString::createFromAscii( "ControlModel" ), makeAny( xModel ) );
3393 Reference< XControl > xFilterControl(
3394 m_aContext.createComponentWithArguments( "com.sun.star.form.control.FilterControl", aCreationArgs ),
3395 UNO_QUERY
3396 );
3397 DBG_ASSERT( xFilterControl.is(), "FormController::startFiltering: could not create a filter control!" );
3398
3399 if ( replaceControl( xControl, xFilterControl ) )
3400 {
3401 Reference< XTextComponent > xFilterText( xFilterControl, UNO_QUERY );
3402 aFieldInfos.push_back( FmFieldInfo( xField, xFilterText ) );
3403 xFilterText->addTextListener(this);
3404 }
3405 }
3406 }
3407 else
3408 {
3409 // abmelden vom EventManager
3410 }
3411 }
3412 }
3413
3414 // we have all filter controls now, so the next step is to read the filters from the form
3415 // resolve all aliases and set the current filter to the according structure
3416 setFilter(aFieldInfos);
3417
3418 Reference< XPropertySet > xSet( m_xModelAsIndex, UNO_QUERY );
3419 if ( xSet.is() )
3420 stopFormListening( xSet, sal_True );
3421
3422 impl_setTextOnAllFilter_throw();
3423
3424 // lock all controls which are not used for filtering
3425 m_bLocked = determineLockState();
3426 setLocks();
3427 m_bAttachEvents = sal_True;
3428 }
3429
3430 //------------------------------------------------------------------------------
stopFiltering()3431 void FormController::stopFiltering()
3432 {
3433 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3434 if ( !m_bFiltering ) // #104693# OJ
3435 { // nothing to do
3436 return;
3437 }
3438
3439 m_bFiltering = sal_False;
3440 m_bDetachEvents = sal_False;
3441
3442 ::comphelper::disposeComponent(m_xComposer);
3443
3444 // Austauschen der Kontrols fuer das aktuelle Formular
3445 Sequence< Reference< XControl > > aControlsCopy( m_aControls );
3446 const Reference< XControl > * pControls = aControlsCopy.getConstArray();
3447 sal_Int32 nControlCount = aControlsCopy.getLength();
3448
3449 // clear the filter control map
3450 ::std::for_each( m_aFilterComponents.begin(), m_aFilterComponents.end(), RemoveComponentTextListener( this ) );
3451 m_aFilterComponents.clear();
3452
3453 for ( sal_Int32 i = nControlCount; i > 0; )
3454 {
3455 Reference< XControl > xControl = pControls[--i];
3456 if (xControl.is())
3457 {
3458 // now enable eventhandling again
3459 addToEventAttacher(xControl);
3460
3461 Reference< XModeSelector > xSelector(xControl, UNO_QUERY);
3462 if (xSelector.is())
3463 {
3464 xSelector->setMode( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DataMode" ) ) );
3465
3466 // listening for new controls of the selector
3467 Reference< XContainer > xContainer(xSelector, UNO_QUERY);
3468 if (xContainer.is())
3469 xContainer->removeContainerListener(this);
3470 continue;
3471 }
3472
3473 Reference< XPropertySet > xSet(xControl->getModel(), UNO_QUERY);
3474 if (xSet.is() && ::comphelper::hasProperty(FM_PROP_BOUNDFIELD, xSet))
3475 {
3476 // does the model use a bound field ?
3477 Reference< XPropertySet > xField;
3478 xSet->getPropertyValue(FM_PROP_BOUNDFIELD) >>= xField;
3479
3480 // may we filter the field?
3481 if ( xField.is()
3482 && ::comphelper::hasProperty( FM_PROP_SEARCHABLE, xField )
3483 && ::comphelper::getBOOL( xField->getPropertyValue( FM_PROP_SEARCHABLE ) )
3484 )
3485 {
3486 ::rtl::OUString sServiceName;
3487 OSL_VERIFY( xSet->getPropertyValue( FM_PROP_DEFAULTCONTROL ) >>= sServiceName );
3488 Reference< XControl > xNewControl( m_aContext.createComponent( sServiceName ), UNO_QUERY );
3489 replaceControl( xControl, xNewControl );
3490 }
3491 }
3492 }
3493 }
3494
3495 Reference< XPropertySet > xSet( m_xModelAsIndex, UNO_QUERY );
3496 if ( xSet.is() )
3497 startFormListening( xSet, sal_True );
3498
3499 m_bDetachEvents = sal_True;
3500
3501 m_aFilterRows.clear();
3502 m_nCurrentFilterPosition = -1;
3503
3504 // release the locks if possible
3505 // lock all controls which are not used for filtering
3506 m_bLocked = determineLockState();
3507 setLocks();
3508
3509 // restart listening for control modifications
3510 if (isListeningForChanges())
3511 startListening();
3512 }
3513
3514 // XModeSelector
3515 //------------------------------------------------------------------------------
setMode(const::rtl::OUString & Mode)3516 void FormController::setMode(const ::rtl::OUString& Mode)
3517 {
3518 ::osl::MutexGuard aGuard( m_aMutex );
3519 impl_checkDisposed_throw();
3520
3521 if (!supportsMode(Mode))
3522 throw NoSupportException();
3523
3524 if (Mode == m_aMode)
3525 return;
3526
3527 m_aMode = Mode;
3528
3529 if ( Mode.equalsAscii( "FilterMode" ) )
3530 startFiltering();
3531 else
3532 stopFiltering();
3533
3534 for (FmFormControllers::const_iterator i = m_aChilds.begin();
3535 i != m_aChilds.end(); ++i)
3536 {
3537 Reference< XModeSelector > xMode(*i, UNO_QUERY);
3538 if ( xMode.is() )
3539 xMode->setMode(Mode);
3540 }
3541 }
3542
3543 //------------------------------------------------------------------------------
getMode(void)3544 ::rtl::OUString SAL_CALL FormController::getMode(void)
3545 {
3546 ::osl::MutexGuard aGuard( m_aMutex );
3547 impl_checkDisposed_throw();
3548
3549 return m_aMode;
3550 }
3551
3552 //------------------------------------------------------------------------------
getSupportedModes(void)3553 Sequence< ::rtl::OUString > SAL_CALL FormController::getSupportedModes(void)
3554 {
3555 ::osl::MutexGuard aGuard( m_aMutex );
3556 impl_checkDisposed_throw();
3557
3558 static Sequence< ::rtl::OUString > aModes;
3559 if (!aModes.getLength())
3560 {
3561 aModes.realloc(2);
3562 ::rtl::OUString* pModes = aModes.getArray();
3563 pModes[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DataMode" ) );
3564 pModes[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterMode" ) );
3565 }
3566 return aModes;
3567 }
3568
3569 //------------------------------------------------------------------------------
supportsMode(const::rtl::OUString & Mode)3570 sal_Bool SAL_CALL FormController::supportsMode(const ::rtl::OUString& Mode)
3571 {
3572 ::osl::MutexGuard aGuard( m_aMutex );
3573 impl_checkDisposed_throw();
3574
3575 Sequence< ::rtl::OUString > aModes(getSupportedModes());
3576 const ::rtl::OUString* pModes = aModes.getConstArray();
3577 for (sal_Int32 i = aModes.getLength(); i > 0; )
3578 {
3579 if (pModes[--i] == Mode)
3580 return sal_True;
3581 }
3582 return sal_False;
3583 }
3584
3585 //------------------------------------------------------------------------------
getDialogParentWindow()3586 Window* FormController::getDialogParentWindow()
3587 {
3588 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
3589 Window* pParentWindow = NULL;
3590 try
3591 {
3592 Reference< XControl > xContainerControl( getContainer(), UNO_QUERY_THROW );
3593 Reference< XWindowPeer > xContainerPeer( xContainerControl->getPeer(), UNO_QUERY_THROW );
3594 pParentWindow = VCLUnoHelper::GetWindow( xContainerPeer );
3595 }
3596 catch( const Exception& )
3597 {
3598 DBG_UNHANDLED_EXCEPTION();
3599 }
3600 return pParentWindow;
3601 }
3602 //------------------------------------------------------------------------------
checkFormComponentValidity(::rtl::OUString & _rFirstInvalidityExplanation,Reference<XControlModel> & _rxFirstInvalidModel)3603 bool FormController::checkFormComponentValidity( ::rtl::OUString& /* [out] */ _rFirstInvalidityExplanation, Reference< XControlModel >& /* [out] */ _rxFirstInvalidModel ) SAL_THROW(())
3604 {
3605 try
3606 {
3607 Reference< XEnumerationAccess > xControlEnumAcc( getModel(), UNO_QUERY );
3608 Reference< XEnumeration > xControlEnumeration;
3609 if ( xControlEnumAcc.is() )
3610 xControlEnumeration = xControlEnumAcc->createEnumeration();
3611 OSL_ENSURE( xControlEnumeration.is(), "FormController::checkFormComponentValidity: cannot enumerate the controls!" );
3612 if ( !xControlEnumeration.is() )
3613 // assume all valid
3614 return true;
3615
3616 Reference< XValidatableFormComponent > xValidatable;
3617 while ( xControlEnumeration->hasMoreElements() )
3618 {
3619 if ( !( xControlEnumeration->nextElement() >>= xValidatable ) )
3620 // control does not support validation
3621 continue;
3622
3623 if ( xValidatable->isValid() )
3624 continue;
3625
3626 Reference< XValidator > xValidator( xValidatable->getValidator() );
3627 OSL_ENSURE( xValidator.is(), "FormController::checkFormComponentValidity: invalid, but no validator?" );
3628 if ( !xValidator.is() )
3629 // this violates the interface definition of css.form.validation.XValidatableFormComponent ...
3630 continue;
3631
3632 _rFirstInvalidityExplanation = xValidator->explainInvalid( xValidatable->getCurrentValue() );
3633 _rxFirstInvalidModel = _rxFirstInvalidModel.query( xValidatable );
3634 return false;
3635 }
3636 }
3637 catch( const Exception& )
3638 {
3639 DBG_UNHANDLED_EXCEPTION();
3640 }
3641 return true;
3642 }
3643
3644 //------------------------------------------------------------------------------
locateControl(const Reference<XControlModel> & _rxModel)3645 Reference< XControl > FormController::locateControl( const Reference< XControlModel >& _rxModel ) SAL_THROW(())
3646 {
3647 try
3648 {
3649 Sequence< Reference< XControl > > aControls( getControls() );
3650 const Reference< XControl >* pControls = aControls.getConstArray();
3651 const Reference< XControl >* pControlsEnd = aControls.getConstArray() + aControls.getLength();
3652
3653 for ( ; pControls != pControlsEnd; ++pControls )
3654 {
3655 OSL_ENSURE( pControls->is(), "FormController::locateControl: NULL-control?" );
3656 if ( pControls->is() )
3657 {
3658 if ( ( *pControls)->getModel() == _rxModel )
3659 return *pControls;
3660 }
3661 }
3662 OSL_ENSURE( sal_False, "FormController::locateControl: did not find a control for this model!" );
3663 }
3664 catch( const Exception& )
3665 {
3666 DBG_UNHANDLED_EXCEPTION();
3667 }
3668 return NULL;
3669 }
3670
3671 //------------------------------------------------------------------------------
3672 namespace
3673 {
displayErrorSetFocus(const String & _rMessage,const Reference<XControl> & _rxFocusControl,Window * _pDialogParent)3674 void displayErrorSetFocus( const String& _rMessage, const Reference< XControl >& _rxFocusControl, Window* _pDialogParent )
3675 {
3676 SQLContext aError;
3677 aError.Message = String( SVX_RES( RID_STR_WRITEERROR ) );
3678 aError.Details = _rMessage;
3679 displayException( aError, _pDialogParent );
3680
3681 if ( _rxFocusControl.is() )
3682 {
3683 Reference< XWindow > xControlWindow( _rxFocusControl, UNO_QUERY );
3684 OSL_ENSURE( xControlWindow.is(), "displayErrorSetFocus: invalid control!" );
3685 if ( xControlWindow.is() )
3686 xControlWindow->setFocus();
3687 }
3688 }
3689
lcl_shouldValidateRequiredFields_nothrow(const Reference<XInterface> & _rxForm)3690 sal_Bool lcl_shouldValidateRequiredFields_nothrow( const Reference< XInterface >& _rxForm )
3691 {
3692 try
3693 {
3694 static ::rtl::OUString s_sFormsCheckRequiredFields( RTL_CONSTASCII_USTRINGPARAM( "FormsCheckRequiredFields" ) );
3695
3696 // first, check whether the form has a property telling us the answer
3697 // this allows people to use the XPropertyContainer interface of a form to control
3698 // the behaviour on a per-form basis.
3699 Reference< XPropertySet > xFormProps( _rxForm, UNO_QUERY_THROW );
3700 Reference< XPropertySetInfo > xPSI( xFormProps->getPropertySetInfo() );
3701 if ( xPSI->hasPropertyByName( s_sFormsCheckRequiredFields ) )
3702 {
3703 sal_Bool bShouldValidate = true;
3704 OSL_VERIFY( xFormProps->getPropertyValue( s_sFormsCheckRequiredFields ) >>= bShouldValidate );
3705 return bShouldValidate;
3706 }
3707
3708 // next, check the data source which created the connection
3709 Reference< XChild > xConnectionAsChild( xFormProps->getPropertyValue( FM_PROP_ACTIVE_CONNECTION ), UNO_QUERY_THROW );
3710 Reference< XPropertySet > xDataSource( xConnectionAsChild->getParent(), UNO_QUERY );
3711 if ( !xDataSource.is() )
3712 // seldom (but possible): this is not a connection created by a data source
3713 return sal_True;
3714
3715 Reference< XPropertySet > xDataSourceSettings(
3716 xDataSource->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Settings" ) ) ),
3717 UNO_QUERY_THROW );
3718
3719 sal_Bool bShouldValidate = true;
3720 OSL_VERIFY( xDataSourceSettings->getPropertyValue( s_sFormsCheckRequiredFields ) >>= bShouldValidate );
3721 return bShouldValidate;
3722 }
3723 catch( const Exception& )
3724 {
3725 DBG_UNHANDLED_EXCEPTION();
3726 }
3727
3728 return sal_True;
3729 }
3730 }
3731
3732 // XRowSetApproveListener
3733 //------------------------------------------------------------------------------
approveRowChange(const RowChangeEvent & _rEvent)3734 sal_Bool SAL_CALL FormController::approveRowChange(const RowChangeEvent& _rEvent)
3735 {
3736 ::osl::ClearableMutexGuard aGuard( m_aMutex );
3737 impl_checkDisposed_throw();
3738
3739 ::cppu::OInterfaceIteratorHelper aIter(m_aRowSetApproveListeners);
3740 sal_Bool bValid = sal_True;
3741 if (aIter.hasMoreElements())
3742 {
3743 RowChangeEvent aEvt( _rEvent );
3744 aEvt.Source = *this;
3745 bValid = ((XRowSetApproveListener*)aIter.next())->approveRowChange(aEvt);
3746 }
3747
3748 if ( !bValid )
3749 return bValid;
3750
3751 if ( ( _rEvent.Action != RowChangeAction::INSERT )
3752 && ( _rEvent.Action != RowChangeAction::UPDATE )
3753 )
3754 return bValid;
3755
3756 // if some of the control models are bound to validators, check them
3757 ::rtl::OUString sInvalidityExplanation;
3758 Reference< XControlModel > xInvalidModel;
3759 if ( !checkFormComponentValidity( sInvalidityExplanation, xInvalidModel ) )
3760 {
3761 Reference< XControl > xControl( locateControl( xInvalidModel ) );
3762 aGuard.clear();
3763 displayErrorSetFocus( sInvalidityExplanation, xControl, getDialogParentWindow() );
3764 return false;
3765 }
3766
3767 // check values on NULL and required flag
3768 if ( !lcl_shouldValidateRequiredFields_nothrow( _rEvent.Source ) )
3769 return sal_True;
3770
3771 OSL_ENSURE( m_pColumnInfoCache.get(), "FormController::approveRowChange: no column infos!" );
3772 if ( !m_pColumnInfoCache.get() )
3773 return sal_True;
3774
3775 try
3776 {
3777 if ( !m_pColumnInfoCache->controlsInitialized() )
3778 m_pColumnInfoCache->initializeControls( getControls() );
3779
3780 size_t colCount = m_pColumnInfoCache->getColumnCount();
3781 for ( size_t col = 0; col < colCount; ++col )
3782 {
3783 const ColumnInfo& rColInfo = m_pColumnInfoCache->getColumnInfo( col );
3784 if ( rColInfo.nNullable != ColumnValue::NO_NULLS )
3785 continue;
3786
3787 if ( rColInfo.bAutoIncrement )
3788 continue;
3789
3790 if ( rColInfo.bReadOnly )
3791 continue;
3792
3793 if ( !rColInfo.xFirstControlWithInputRequired.is() && !rColInfo.xFirstGridWithInputRequiredColumn.is() )
3794 continue;
3795
3796 // TODO: in case of binary fields, this "getString" below is extremely expensive
3797 if ( rColInfo.xColumn->getString().getLength() || !rColInfo.xColumn->wasNull() )
3798 continue;
3799
3800 String sMessage( SVX_RES( RID_ERR_FIELDREQUIRED ) );
3801 sMessage.SearchAndReplace( '#', rColInfo.sName );
3802
3803 // the control to focus
3804 Reference< XControl > xControl( rColInfo.xFirstControlWithInputRequired );
3805 if ( !xControl.is() )
3806 xControl.set( rColInfo.xFirstGridWithInputRequiredColumn, UNO_QUERY );
3807
3808 aGuard.clear();
3809 displayErrorSetFocus( sMessage, rColInfo.xFirstControlWithInputRequired, getDialogParentWindow() );
3810 return sal_False;
3811 }
3812 }
3813 catch( const Exception& )
3814 {
3815 DBG_UNHANDLED_EXCEPTION();
3816 }
3817
3818 return true;
3819 }
3820
3821 //------------------------------------------------------------------------------
approveCursorMove(const EventObject & event)3822 sal_Bool SAL_CALL FormController::approveCursorMove(const EventObject& event)
3823 {
3824 ::osl::MutexGuard aGuard( m_aMutex );
3825 impl_checkDisposed_throw();
3826
3827 ::cppu::OInterfaceIteratorHelper aIter(m_aRowSetApproveListeners);
3828 if (aIter.hasMoreElements())
3829 {
3830 EventObject aEvt(event);
3831 aEvt.Source = *this;
3832 return ((XRowSetApproveListener*)aIter.next())->approveCursorMove(aEvt);
3833 }
3834
3835 return sal_True;
3836 }
3837
3838 //------------------------------------------------------------------------------
approveRowSetChange(const EventObject & event)3839 sal_Bool SAL_CALL FormController::approveRowSetChange(const EventObject& event)
3840 {
3841 ::osl::MutexGuard aGuard( m_aMutex );
3842 impl_checkDisposed_throw();
3843
3844 ::cppu::OInterfaceIteratorHelper aIter(m_aRowSetApproveListeners);
3845 if (aIter.hasMoreElements())
3846 {
3847 EventObject aEvt(event);
3848 aEvt.Source = *this;
3849 return ((XRowSetApproveListener*)aIter.next())->approveRowSetChange(aEvt);
3850 }
3851
3852 return sal_True;
3853 }
3854
3855 // XRowSetApproveBroadcaster
3856 //------------------------------------------------------------------------------
addRowSetApproveListener(const Reference<XRowSetApproveListener> & _rxListener)3857 void SAL_CALL FormController::addRowSetApproveListener(const Reference< XRowSetApproveListener > & _rxListener)
3858 {
3859 ::osl::MutexGuard aGuard( m_aMutex );
3860 impl_checkDisposed_throw();
3861
3862 m_aRowSetApproveListeners.addInterface(_rxListener);
3863 }
3864
3865 //------------------------------------------------------------------------------
removeRowSetApproveListener(const Reference<XRowSetApproveListener> & _rxListener)3866 void SAL_CALL FormController::removeRowSetApproveListener(const Reference< XRowSetApproveListener > & _rxListener)
3867 {
3868 ::osl::MutexGuard aGuard( m_aMutex );
3869 impl_checkDisposed_throw();
3870
3871 m_aRowSetApproveListeners.removeInterface(_rxListener);
3872 }
3873
3874 // XErrorListener
3875 //------------------------------------------------------------------------------
errorOccured(const SQLErrorEvent & aEvent)3876 void SAL_CALL FormController::errorOccured(const SQLErrorEvent& aEvent)
3877 {
3878 ::osl::ClearableMutexGuard aGuard( m_aMutex );
3879 impl_checkDisposed_throw();
3880
3881 ::cppu::OInterfaceIteratorHelper aIter(m_aErrorListeners);
3882 if (aIter.hasMoreElements())
3883 {
3884 SQLErrorEvent aEvt(aEvent);
3885 aEvt.Source = *this;
3886 ((XSQLErrorListener*)aIter.next())->errorOccured(aEvt);
3887 }
3888 else
3889 {
3890 aGuard.clear();
3891 displayException( aEvent );
3892 }
3893 }
3894
3895 // XErrorBroadcaster
3896 //------------------------------------------------------------------------------
addSQLErrorListener(const Reference<XSQLErrorListener> & aListener)3897 void SAL_CALL FormController::addSQLErrorListener(const Reference< XSQLErrorListener > & aListener)
3898 {
3899 ::osl::MutexGuard aGuard( m_aMutex );
3900 impl_checkDisposed_throw();
3901
3902 m_aErrorListeners.addInterface(aListener);
3903 }
3904
3905 //------------------------------------------------------------------------------
removeSQLErrorListener(const Reference<XSQLErrorListener> & aListener)3906 void SAL_CALL FormController::removeSQLErrorListener(const Reference< XSQLErrorListener > & aListener)
3907 {
3908 ::osl::MutexGuard aGuard( m_aMutex );
3909 impl_checkDisposed_throw();
3910
3911 m_aErrorListeners.removeInterface(aListener);
3912 }
3913
3914 // XDatabaseParameterBroadcaster2
3915 //------------------------------------------------------------------------------
addDatabaseParameterListener(const Reference<XDatabaseParameterListener> & aListener)3916 void SAL_CALL FormController::addDatabaseParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3917 {
3918 ::osl::MutexGuard aGuard( m_aMutex );
3919 impl_checkDisposed_throw();
3920
3921 m_aParameterListeners.addInterface(aListener);
3922 }
3923
3924 //------------------------------------------------------------------------------
removeDatabaseParameterListener(const Reference<XDatabaseParameterListener> & aListener)3925 void SAL_CALL FormController::removeDatabaseParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3926 {
3927 ::osl::MutexGuard aGuard( m_aMutex );
3928 impl_checkDisposed_throw();
3929
3930 m_aParameterListeners.removeInterface(aListener);
3931 }
3932
3933 // XDatabaseParameterBroadcaster
3934 //------------------------------------------------------------------------------
addParameterListener(const Reference<XDatabaseParameterListener> & aListener)3935 void SAL_CALL FormController::addParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3936 {
3937 FormController::addDatabaseParameterListener( aListener );
3938 }
3939
3940 //------------------------------------------------------------------------------
removeParameterListener(const Reference<XDatabaseParameterListener> & aListener)3941 void SAL_CALL FormController::removeParameterListener(const Reference< XDatabaseParameterListener > & aListener)
3942 {
3943 FormController::removeDatabaseParameterListener( aListener );
3944 }
3945
3946 // XDatabaseParameterListener
3947 //------------------------------------------------------------------------------
approveParameter(const DatabaseParameterEvent & aEvent)3948 sal_Bool SAL_CALL FormController::approveParameter(const DatabaseParameterEvent& aEvent)
3949 {
3950 ::vos::OGuard aSolarGuard(Application::GetSolarMutex());
3951 ::osl::MutexGuard aGuard( m_aMutex );
3952 impl_checkDisposed_throw();
3953
3954 ::cppu::OInterfaceIteratorHelper aIter(m_aParameterListeners);
3955 if (aIter.hasMoreElements())
3956 {
3957 DatabaseParameterEvent aEvt(aEvent);
3958 aEvt.Source = *this;
3959 return ((XDatabaseParameterListener*)aIter.next())->approveParameter(aEvt);
3960 }
3961 else
3962 {
3963 // default handling: instantiate an interaction handler and let it handle the parameter request
3964 try
3965 {
3966 if ( !ensureInteractionHandler() )
3967 return sal_False;
3968
3969 // two continuations allowed: OK and Cancel
3970 OParameterContinuation* pParamValues = new OParameterContinuation;
3971 OInteractionAbort* pAbort = new OInteractionAbort;
3972 // the request
3973 ParametersRequest aRequest;
3974 aRequest.Parameters = aEvent.Parameters;
3975 aRequest.Connection = OStaticDataAccessTools().getRowSetConnection(Reference< XRowSet >(aEvent.Source, UNO_QUERY));
3976 OInteractionRequest* pParamRequest = new OInteractionRequest(makeAny(aRequest));
3977 Reference< XInteractionRequest > xParamRequest(pParamRequest);
3978 // some knittings
3979 pParamRequest->addContinuation(pParamValues);
3980 pParamRequest->addContinuation(pAbort);
3981
3982 // handle the request
3983 m_xInteractionHandler->handle(xParamRequest);
3984
3985 if (!pParamValues->wasSelected())
3986 // canceled
3987 return sal_False;
3988
3989 // transfer the values into the parameter supplier
3990 Sequence< PropertyValue > aFinalValues = pParamValues->getValues();
3991 if (aFinalValues.getLength() != aRequest.Parameters->getCount())
3992 {
3993 DBG_ERROR("FormController::approveParameter: the InteractionHandler returned nonsense!");
3994 return sal_False;
3995 }
3996 const PropertyValue* pFinalValues = aFinalValues.getConstArray();
3997 for (sal_Int32 i=0; i<aFinalValues.getLength(); ++i, ++pFinalValues)
3998 {
3999 Reference< XPropertySet > xParam;
4000 ::cppu::extractInterface(xParam, aRequest.Parameters->getByIndex(i));
4001 if (xParam.is())
4002 {
4003 #ifdef DBG_UTIL
4004 ::rtl::OUString sName;
4005 xParam->getPropertyValue(FM_PROP_NAME) >>= sName;
4006 DBG_ASSERT(sName.equals(pFinalValues->Name), "FormController::approveParameter: suspicious value names!");
4007 #endif
4008 try { xParam->setPropertyValue(FM_PROP_VALUE, pFinalValues->Value); }
4009 catch(Exception&)
4010 {
4011 DBG_ERROR("FormController::approveParameter: setting one of the properties failed!");
4012 }
4013 }
4014 }
4015 }
4016 catch(Exception&)
4017 {
4018 DBG_UNHANDLED_EXCEPTION();
4019 }
4020 }
4021 return sal_True;
4022 }
4023
4024 // XConfirmDeleteBroadcaster
4025 //------------------------------------------------------------------------------
addConfirmDeleteListener(const Reference<XConfirmDeleteListener> & aListener)4026 void SAL_CALL FormController::addConfirmDeleteListener(const Reference< XConfirmDeleteListener > & aListener)
4027 {
4028 ::osl::MutexGuard aGuard( m_aMutex );
4029 impl_checkDisposed_throw();
4030
4031 m_aDeleteListeners.addInterface(aListener);
4032 }
4033
4034 //------------------------------------------------------------------------------
removeConfirmDeleteListener(const Reference<XConfirmDeleteListener> & aListener)4035 void SAL_CALL FormController::removeConfirmDeleteListener(const Reference< XConfirmDeleteListener > & aListener)
4036 {
4037 ::osl::MutexGuard aGuard( m_aMutex );
4038 impl_checkDisposed_throw();
4039
4040 m_aDeleteListeners.removeInterface(aListener);
4041 }
4042
4043 // XConfirmDeleteListener
4044 //------------------------------------------------------------------------------
confirmDelete(const RowChangeEvent & aEvent)4045 sal_Bool SAL_CALL FormController::confirmDelete(const RowChangeEvent& aEvent)
4046 {
4047 ::osl::MutexGuard aGuard( m_aMutex );
4048 impl_checkDisposed_throw();
4049
4050 ::cppu::OInterfaceIteratorHelper aIter(m_aDeleteListeners);
4051 if (aIter.hasMoreElements())
4052 {
4053 RowChangeEvent aEvt(aEvent);
4054 aEvt.Source = *this;
4055 return ((XConfirmDeleteListener*)aIter.next())->confirmDelete(aEvt);
4056 }
4057 // default handling: instantiate an interaction handler and let it handle the request
4058
4059 String sTitle;
4060 sal_Int32 nLength = aEvent.Rows;
4061 if ( nLength > 1 )
4062 {
4063 sTitle = SVX_RES( RID_STR_DELETECONFIRM_RECORDS );
4064 sTitle.SearchAndReplace( '#', String::CreateFromInt32( nLength ) );
4065 }
4066 else
4067 sTitle = SVX_RES( RID_STR_DELETECONFIRM_RECORD );
4068
4069 try
4070 {
4071 if ( !ensureInteractionHandler() )
4072 return sal_False;
4073
4074 // two continuations allowed: Yes and No
4075 OInteractionApprove* pApprove = new OInteractionApprove;
4076 OInteractionDisapprove* pDisapprove = new OInteractionDisapprove;
4077
4078 // the request
4079 SQLWarning aWarning;
4080 aWarning.Message = sTitle;
4081 SQLWarning aDetails;
4082 aDetails.Message = String( SVX_RES( RID_STR_DELETECONFIRM ) );
4083 aWarning.NextException <<= aDetails;
4084
4085 OInteractionRequest* pRequest = new OInteractionRequest( makeAny( aWarning ) );
4086 Reference< XInteractionRequest > xRequest( pRequest );
4087
4088 // some knittings
4089 pRequest->addContinuation( pApprove );
4090 pRequest->addContinuation( pDisapprove );
4091
4092 // handle the request
4093 m_xInteractionHandler->handle( xRequest );
4094
4095 if ( pApprove->wasSelected() )
4096 return sal_True;
4097 }
4098 catch( const Exception& )
4099 {
4100 DBG_UNHANDLED_EXCEPTION();
4101 }
4102
4103 return sal_False;
4104 }
4105
4106 //------------------------------------------------------------------------------
invalidateFeatures(const Sequence<::sal_Int16> & _Features)4107 void SAL_CALL FormController::invalidateFeatures( const Sequence< ::sal_Int16 >& _Features )
4108 {
4109 ::osl::MutexGuard aGuard( m_aMutex );
4110 // for now, just copy the ids of the features, because ....
4111 ::std::copy( _Features.getConstArray(), _Features.getConstArray() + _Features.getLength(),
4112 ::std::insert_iterator< ::std::set< sal_Int16 > >( m_aInvalidFeatures, m_aInvalidFeatures.begin() )
4113 );
4114
4115 // ... we will do the real invalidation asynchronously
4116 if ( !m_aFeatureInvalidationTimer.IsActive() )
4117 m_aFeatureInvalidationTimer.Start();
4118 }
4119
4120 //------------------------------------------------------------------------------
invalidateAllFeatures()4121 void SAL_CALL FormController::invalidateAllFeatures( )
4122 {
4123 ::osl::ClearableMutexGuard aGuard( m_aMutex );
4124
4125 Sequence< sal_Int16 > aInterceptedFeatures( m_aFeatureDispatchers.size() );
4126 ::std::transform(
4127 m_aFeatureDispatchers.begin(),
4128 m_aFeatureDispatchers.end(),
4129 aInterceptedFeatures.getArray(),
4130 ::std::select1st< DispatcherContainer::value_type >()
4131 );
4132
4133 aGuard.clear();
4134 if ( aInterceptedFeatures.getLength() )
4135 invalidateFeatures( aInterceptedFeatures );
4136 }
4137
4138 //------------------------------------------------------------------------------
4139 Reference< XDispatch >
interceptedQueryDispatch(const URL & aURL,const::rtl::OUString &,sal_Int32)4140 FormController::interceptedQueryDispatch( const URL& aURL,
4141 const ::rtl::OUString& /*aTargetFrameName*/, sal_Int32 /*nSearchFlags*/)
4142 {
4143 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
4144 Reference< XDispatch > xReturn;
4145 // dispatches handled by ourself
4146 if ( ( aURL.Complete == FMURL_CONFIRM_DELETION )
4147 || ( ( aURL.Complete.equalsAscii( "private:/InteractionHandler" ) )
4148 && ensureInteractionHandler()
4149 )
4150 )
4151 xReturn = static_cast< XDispatch* >( this );
4152
4153 // dispatches of FormSlot-URLs we have to translate
4154 if ( !xReturn.is() && m_xFormOperations.is() )
4155 {
4156 // find the slot id which corresponds to the URL
4157 sal_Int32 nFeatureSlotId = ::svx::FeatureSlotTranslation::getControllerFeatureSlotIdForURL( aURL.Main );
4158 sal_Int16 nFormFeature = ( nFeatureSlotId != -1 ) ? ::svx::FeatureSlotTranslation::getFormFeatureForSlotId( nFeatureSlotId ) : -1;
4159 if ( nFormFeature > 0 )
4160 {
4161 // get the dispatcher for this feature, create if necessary
4162 DispatcherContainer::const_iterator aDispatcherPos = m_aFeatureDispatchers.find( nFormFeature );
4163 if ( aDispatcherPos == m_aFeatureDispatchers.end() )
4164 {
4165 aDispatcherPos = m_aFeatureDispatchers.insert(
4166 DispatcherContainer::value_type( nFormFeature, new ::svx::OSingleFeatureDispatcher( aURL, nFormFeature, m_xFormOperations, m_aMutex ) )
4167 ).first;
4168 }
4169
4170 OSL_ENSURE( aDispatcherPos->second.is(), "FormController::interceptedQueryDispatch: should have a dispatcher by now!" );
4171 return aDispatcherPos->second;
4172 }
4173 }
4174
4175 // no more to offer
4176 return xReturn;
4177 }
4178
4179 //------------------------------------------------------------------------------
dispatch(const URL & _rURL,const Sequence<PropertyValue> & _rArgs)4180 void SAL_CALL FormController::dispatch( const URL& _rURL, const Sequence< PropertyValue >& _rArgs )
4181 {
4182 if ( _rArgs.getLength() != 1 )
4183 {
4184 DBG_ERROR( "FormController::dispatch: no arguments -> no dispatch!" );
4185 return;
4186 }
4187
4188 if ( _rURL.Complete.equalsAscii( "private:/InteractionHandler" ) )
4189 {
4190 Reference< XInteractionRequest > xRequest;
4191 OSL_VERIFY( _rArgs[0].Value >>= xRequest );
4192 if ( xRequest.is() )
4193 handle( xRequest );
4194 return;
4195 }
4196
4197 if ( _rURL.Complete == FMURL_CONFIRM_DELETION )
4198 {
4199 DBG_ERROR( "FormController::dispatch: How do you expect me to return something via this call?" );
4200 // confirmDelete has a return value - dispatch hasn't
4201 return;
4202 }
4203
4204 DBG_ERROR( "FormController::dispatch: unknown URL!" );
4205 }
4206
4207 //------------------------------------------------------------------------------
addStatusListener(const Reference<XStatusListener> & _rxListener,const URL & _rURL)4208 void SAL_CALL FormController::addStatusListener( const Reference< XStatusListener >& _rxListener, const URL& _rURL )
4209 {
4210 if (_rURL.Complete == FMURL_CONFIRM_DELETION)
4211 {
4212 if (_rxListener.is())
4213 { // send an initial statusChanged event
4214 FeatureStateEvent aEvent;
4215 aEvent.FeatureURL = _rURL;
4216 aEvent.IsEnabled = sal_True;
4217 _rxListener->statusChanged(aEvent);
4218 // and don't add the listener at all (the status will never change)
4219 }
4220 }
4221 else
4222 OSL_ENSURE(sal_False, "FormController::addStatusListener: invalid (unsupported) URL!");
4223 }
4224
4225 //------------------------------------------------------------------------------
getParent()4226 Reference< XInterface > SAL_CALL FormController::getParent()
4227 {
4228 return m_xParent;
4229 }
4230
4231 //------------------------------------------------------------------------------
setParent(const Reference<XInterface> & Parent)4232 void SAL_CALL FormController::setParent( const Reference< XInterface >& Parent)
4233 {
4234 m_xParent = Parent;
4235 }
4236
4237 //------------------------------------------------------------------------------
removeStatusListener(const Reference<XStatusListener> &,const URL & _rURL)4238 void SAL_CALL FormController::removeStatusListener( const Reference< XStatusListener >& /*_rxListener*/, const URL& _rURL )
4239 {
4240 (void)_rURL;
4241 OSL_ENSURE(_rURL.Complete == FMURL_CONFIRM_DELETION, "FormController::removeStatusListener: invalid (unsupported) URL!");
4242 // we never really added the listener, so we don't need to remove it
4243 }
4244
4245 //------------------------------------------------------------------------------
createInterceptor(const Reference<XDispatchProviderInterception> & _xInterception)4246 Reference< XDispatchProviderInterceptor > FormController::createInterceptor(const Reference< XDispatchProviderInterception > & _xInterception)
4247 {
4248 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
4249 #ifdef DBG_UTIL
4250 // check if we already have a interceptor for the given object
4251 for ( ConstInterceptorsIterator aIter = m_aControlDispatchInterceptors.begin();
4252 aIter != m_aControlDispatchInterceptors.end();
4253 ++aIter
4254 )
4255 {
4256 if ((*aIter)->getIntercepted() == _xInterception)
4257 DBG_ERROR("FormController::createInterceptor : we already do intercept this objects dispatches !");
4258 }
4259 #endif
4260
4261 DispatchInterceptionMultiplexer* pInterceptor = new DispatchInterceptionMultiplexer( _xInterception, this );
4262 pInterceptor->acquire();
4263 m_aControlDispatchInterceptors.insert( m_aControlDispatchInterceptors.end(), pInterceptor );
4264
4265 return pInterceptor;
4266 }
4267
4268 //------------------------------------------------------------------------------
ensureInteractionHandler()4269 bool FormController::ensureInteractionHandler()
4270 {
4271 if ( m_xInteractionHandler.is() )
4272 return true;
4273 if ( m_bAttemptedHandlerCreation )
4274 return false;
4275 m_bAttemptedHandlerCreation = true;
4276
4277 m_xInteractionHandler.set( m_aContext.createComponent( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.InteractionHandler" ) ) ), UNO_QUERY );
4278 OSL_ENSURE( m_xInteractionHandler.is(), "FormController::ensureInteractionHandler: could not create an interaction handler!" );
4279 return m_xInteractionHandler.is();
4280 }
4281
4282 //------------------------------------------------------------------------------
handle(const Reference<XInteractionRequest> & _rRequest)4283 void SAL_CALL FormController::handle( const Reference< XInteractionRequest >& _rRequest )
4284 {
4285 if ( !ensureInteractionHandler() )
4286 return;
4287 m_xInteractionHandler->handle( _rRequest );
4288 }
4289
4290 //------------------------------------------------------------------------------
deleteInterceptor(const Reference<XDispatchProviderInterception> & _xInterception)4291 void FormController::deleteInterceptor(const Reference< XDispatchProviderInterception > & _xInterception)
4292 {
4293 OSL_ENSURE( !impl_isDisposed_nofail(), "FormController: already disposed!" );
4294 // search the interceptor responsible for the given object
4295 InterceptorsIterator aIter;
4296 for ( aIter = m_aControlDispatchInterceptors.begin();
4297 aIter != m_aControlDispatchInterceptors.end();
4298 ++aIter
4299 )
4300 {
4301 if ((*aIter)->getIntercepted() == _xInterception)
4302 break;
4303 }
4304 if (aIter == m_aControlDispatchInterceptors.end())
4305 {
4306 return;
4307 }
4308
4309 // log off the interception from it's interception object
4310 DispatchInterceptionMultiplexer* pInterceptorImpl = *aIter;
4311 pInterceptorImpl->dispose();
4312 pInterceptorImpl->release();
4313
4314 // remove the interceptor from our array
4315 m_aControlDispatchInterceptors.erase(aIter);
4316 }
4317
4318 //--------------------------------------------------------------------
implInvalidateCurrentControlDependentFeatures()4319 void FormController::implInvalidateCurrentControlDependentFeatures()
4320 {
4321 Sequence< sal_Int16 > aCurrentControlDependentFeatures(4);
4322
4323 aCurrentControlDependentFeatures[0] = FormFeature::SortAscending;
4324 aCurrentControlDependentFeatures[1] = FormFeature::SortDescending;
4325 aCurrentControlDependentFeatures[2] = FormFeature::AutoFilter;
4326 aCurrentControlDependentFeatures[3] = FormFeature::RefreshCurrentControl;
4327
4328 invalidateFeatures( aCurrentControlDependentFeatures );
4329 }
4330
4331 //--------------------------------------------------------------------
columnChanged(const EventObject &)4332 void SAL_CALL FormController::columnChanged( const EventObject& /*_event*/ )
4333 {
4334 implInvalidateCurrentControlDependentFeatures();
4335 }
4336
4337 } // namespace svxform
4338