Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include "UndoActions.hxx"
20 : #include "UndoEnv.hxx"
21 : #include "formatnormalizer.hxx"
22 : #include "conditionupdater.hxx"
23 : #include "corestrings.hrc"
24 : #include "rptui_slotid.hrc"
25 : #include "RptDef.hxx"
26 : #include "ModuleHelper.hxx"
27 : #include "RptObject.hxx"
28 : #include "RptPage.hxx"
29 : #include "RptResId.hrc"
30 : #include "RptModel.hxx"
31 :
32 : #include <com/sun/star/script/XEventAttacherManager.hpp>
33 : #include <com/sun/star/container/XChild.hpp>
34 : #include <com/sun/star/container/XNameContainer.hpp>
35 : #include <com/sun/star/beans/Introspection.hpp>
36 : #include <com/sun/star/beans/PropertyAttribute.hpp>
37 : #include <com/sun/star/util/XModifyBroadcaster.hpp>
38 : #include <com/sun/star/beans/XIntrospectionAccess.hpp>
39 : #include <com/sun/star/beans/XIntrospection.hpp>
40 :
41 : #include <connectivity/dbtools.hxx>
42 : #include <svl/smplhint.hxx>
43 : #include <tools/diagnose_ex.h>
44 : #include <comphelper/stl_types.hxx>
45 : #include <comphelper/componentcontext.hxx>
46 : #include <vcl/svapp.hxx>
47 : #include <dbaccess/dbsubcomponentcontroller.hxx>
48 : #include <svx/unoshape.hxx>
49 : #include <osl/mutex.hxx>
50 :
51 : namespace rptui
52 : {
53 : using namespace ::com::sun::star;
54 : using namespace uno;
55 : using namespace lang;
56 : using namespace script;
57 : using namespace beans;
58 : using namespace awt;
59 : using namespace util;
60 : using namespace container;
61 : using namespace report;
62 : //----------------------------------------------------------------------------
63 :
64 :
65 : struct PropertyInfo
66 : {
67 : bool bIsReadonlyOrTransient;
68 :
69 : PropertyInfo()
70 : :bIsReadonlyOrTransient( false )
71 : {
72 : }
73 :
74 0 : PropertyInfo( const bool i_bIsTransientOrReadOnly )
75 0 : :bIsReadonlyOrTransient( i_bIsTransientOrReadOnly )
76 : {
77 0 : }
78 : };
79 :
80 : typedef ::boost::unordered_map< ::rtl::OUString, PropertyInfo, ::rtl::OUStringHash > PropertiesInfo;
81 :
82 0 : struct ObjectInfo
83 : {
84 : PropertiesInfo aProperties;
85 : Reference< XPropertySet > xPropertyIntrospection;
86 :
87 0 : ObjectInfo()
88 : :aProperties()
89 0 : ,xPropertyIntrospection()
90 : {
91 0 : }
92 : };
93 :
94 : typedef ::std::map< Reference< XPropertySet >, ObjectInfo, ::comphelper::OInterfaceCompare< XPropertySet > > PropertySetInfoCache;
95 :
96 : // -----------------------------------------------------------------------------
97 :
98 0 : class OXUndoEnvironmentImpl
99 : {
100 : OXUndoEnvironmentImpl(OXUndoEnvironmentImpl&);
101 : void operator =(OXUndoEnvironmentImpl&);
102 : public:
103 : OReportModel& m_rModel;
104 : PropertySetInfoCache m_aPropertySetCache;
105 : FormatNormalizer m_aFormatNormalizer;
106 : ConditionUpdater m_aConditionUpdater;
107 : ::osl::Mutex m_aMutex;
108 : ::std::vector< uno::Reference< container::XChild> > m_aSections;
109 : Reference< XIntrospection > m_xIntrospection;
110 : oslInterlockedCount m_nLocks;
111 : sal_Bool m_bReadOnly;
112 : sal_Bool m_bIsUndo;
113 :
114 : OXUndoEnvironmentImpl(OReportModel& _rModel);
115 : };
116 :
117 0 : OXUndoEnvironmentImpl::OXUndoEnvironmentImpl(OReportModel& _rModel) : m_rModel(_rModel)
118 : ,m_aFormatNormalizer( _rModel )
119 : ,m_aConditionUpdater()
120 : ,m_nLocks(0)
121 : ,m_bReadOnly(sal_False)
122 0 : ,m_bIsUndo(sal_False)
123 : {
124 0 : }
125 :
126 : //------------------------------------------------------------------------------
127 : DBG_NAME( rpt_OXUndoEnvironment );
128 : //------------------------------------------------------------------------------
129 0 : OXUndoEnvironment::OXUndoEnvironment(OReportModel& _rModel)
130 0 : :m_pImpl(new OXUndoEnvironmentImpl(_rModel) )
131 : {
132 : DBG_CTOR( rpt_OXUndoEnvironment,NULL);
133 0 : StartListening(m_pImpl->m_rModel);
134 0 : }
135 :
136 : //------------------------------------------------------------------------------
137 0 : OXUndoEnvironment::~OXUndoEnvironment()
138 : {
139 : DBG_DTOR( rpt_OXUndoEnvironment,NULL);
140 0 : }
141 : // -----------------------------------------------------------------------------
142 0 : void OXUndoEnvironment::Lock()
143 : {
144 : OSL_ENSURE(m_refCount,"Illegal call to dead object!");
145 0 : osl_atomic_increment( &m_pImpl->m_nLocks );
146 0 : }
147 0 : void OXUndoEnvironment::UnLock()
148 : {
149 : OSL_ENSURE(m_refCount,"Illegal call to dead object!");
150 :
151 0 : osl_atomic_decrement( &m_pImpl->m_nLocks );
152 0 : }
153 0 : sal_Bool OXUndoEnvironment::IsLocked() const { return m_pImpl->m_nLocks != 0; }
154 : // -----------------------------------------------------------------------------
155 0 : void OXUndoEnvironment::RemoveSection(OReportPage* _pPage)
156 : {
157 0 : if ( _pPage )
158 : {
159 0 : Reference< XInterface > xSection(_pPage->getSection());
160 0 : if ( xSection.is() )
161 0 : RemoveElement( xSection );
162 : }
163 0 : }
164 : //------------------------------------------------------------------------------
165 0 : void OXUndoEnvironment::Clear(const Accessor& /*_r*/)
166 : {
167 0 : OUndoEnvLock aLock(*this);
168 :
169 0 : m_pImpl->m_aPropertySetCache.clear();
170 :
171 0 : sal_uInt16 nCount = m_pImpl->m_rModel.GetPageCount();
172 : sal_uInt16 i;
173 0 : for (i = 0; i < nCount; i++)
174 : {
175 0 : OReportPage* pPage = PTR_CAST( OReportPage, m_pImpl->m_rModel.GetPage(i) );
176 0 : RemoveSection(pPage);
177 : }
178 :
179 0 : nCount = m_pImpl->m_rModel.GetMasterPageCount();
180 0 : for (i = 0; i < nCount; i++)
181 : {
182 0 : OReportPage* pPage = PTR_CAST( OReportPage, m_pImpl->m_rModel.GetMasterPage(i) );
183 0 : RemoveSection(pPage);
184 : }
185 :
186 0 : m_pImpl->m_aSections.clear();
187 :
188 0 : if (IsListening(m_pImpl->m_rModel))
189 0 : EndListening(m_pImpl->m_rModel);
190 0 : }
191 :
192 : //------------------------------------------------------------------------------
193 0 : void OXUndoEnvironment::ModeChanged()
194 : {
195 0 : m_pImpl->m_bReadOnly = !m_pImpl->m_bReadOnly;
196 :
197 0 : if (!m_pImpl->m_bReadOnly)
198 0 : StartListening(m_pImpl->m_rModel);
199 : else
200 0 : EndListening(m_pImpl->m_rModel);
201 0 : }
202 :
203 : //------------------------------------------------------------------------------
204 0 : void OXUndoEnvironment::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
205 : {
206 0 : if (rHint.ISA(SfxSimpleHint) && ((SfxSimpleHint&)rHint).GetId() == SFX_HINT_MODECHANGED )
207 0 : ModeChanged();
208 0 : }
209 : // -----------------------------------------------------------------------------
210 : // XEventListener
211 : //------------------------------------------------------------------------------
212 0 : void SAL_CALL OXUndoEnvironment::disposing(const EventObject& e) throw( RuntimeException )
213 : {
214 : // check if it's an object we have cached informations about
215 0 : Reference< XPropertySet > xSourceSet(e.Source, UNO_QUERY);
216 0 : if ( xSourceSet.is() )
217 : {
218 0 : uno::Reference< report::XSection> xSection(xSourceSet,uno::UNO_QUERY);
219 0 : if ( xSection.is() )
220 0 : RemoveSection(xSection);
221 : else
222 0 : RemoveElement(xSourceSet);
223 0 : }
224 0 : }
225 :
226 : // XPropertyChangeListener
227 : //------------------------------------------------------------------------------
228 0 : void SAL_CALL OXUndoEnvironment::propertyChange( const PropertyChangeEvent& _rEvent ) throw(uno::RuntimeException)
229 : {
230 :
231 0 : ::osl::ClearableMutexGuard aGuard( m_pImpl->m_aMutex );
232 :
233 0 : if ( IsLocked() )
234 : return;
235 :
236 0 : Reference< XPropertySet > xSet( _rEvent.Source, UNO_QUERY );
237 0 : if (!xSet.is())
238 : return;
239 :
240 0 : dbaui::DBSubComponentController* pController = m_pImpl->m_rModel.getController();
241 0 : if ( !pController )
242 : return;
243 :
244 : // no Undo for transient and readonly props.
245 : // let's see if we know something about the set
246 0 : PropertySetInfoCache::iterator objectPos = m_pImpl->m_aPropertySetCache.find(xSet);
247 0 : if (objectPos == m_pImpl->m_aPropertySetCache.end())
248 : {
249 0 : objectPos = m_pImpl->m_aPropertySetCache.insert( PropertySetInfoCache::value_type(
250 : xSet, ObjectInfo()
251 0 : ) ).first;
252 : DBG_ASSERT(objectPos != m_pImpl->m_aPropertySetCache.end(), "OXUndoEnvironment::propertyChange : just inserted it ... why it's not there ?");
253 : }
254 0 : if ( objectPos == m_pImpl->m_aPropertySetCache.end() )
255 : return;
256 :
257 : // now we have access to the cached info about the set
258 : // let's see what we know about the property
259 0 : ObjectInfo& rObjectInfo = objectPos->second;
260 0 : PropertiesInfo::iterator aPropertyPos = rObjectInfo.aProperties.find( _rEvent.PropertyName );
261 0 : if ( aPropertyPos == rObjectInfo.aProperties.end() )
262 : { // nothing 'til now ... have to change this ....
263 : // the attributes
264 0 : Reference< XPropertySetInfo > xPSI( xSet->getPropertySetInfo(), UNO_SET_THROW );
265 0 : sal_Int32 nPropertyAttributes = 0;
266 : try
267 : {
268 0 : if ( xPSI->hasPropertyByName( _rEvent.PropertyName ) )
269 : {
270 0 : nPropertyAttributes = xPSI->getPropertyByName( _rEvent.PropertyName ).Attributes;
271 : }
272 : else
273 : {
274 : // it's perfectly valid for a component to notify a change in a property which it doesn't have - as long
275 : // as it has an attribute with this name
276 0 : if ( !rObjectInfo.xPropertyIntrospection.is() )
277 : {
278 0 : if ( !m_pImpl->m_xIntrospection.is() )
279 : {
280 0 : ::comphelper::ComponentContext aContext( m_pImpl->m_rModel.getController()->getORB() );
281 0 : m_pImpl->m_xIntrospection = Introspection::create( aContext.getUNOContext() );
282 : }
283 : Reference< XIntrospectionAccess > xIntrospection(
284 0 : m_pImpl->m_xIntrospection->inspect( makeAny( _rEvent.Source ) ),
285 : UNO_SET_THROW
286 0 : );
287 0 : rObjectInfo.xPropertyIntrospection.set( xIntrospection->queryAdapter( XPropertySet::static_type() ), UNO_QUERY_THROW );
288 : }
289 0 : if ( rObjectInfo.xPropertyIntrospection.is() )
290 : {
291 0 : xPSI.set( rObjectInfo.xPropertyIntrospection->getPropertySetInfo(), UNO_SET_THROW );
292 0 : nPropertyAttributes = xPSI->getPropertyByName( _rEvent.PropertyName ).Attributes;
293 : }
294 : }
295 : }
296 0 : catch( const Exception& )
297 : {
298 : DBG_UNHANDLED_EXCEPTION();
299 : }
300 : const bool bTransReadOnly =
301 : ( ( nPropertyAttributes & PropertyAttribute::READONLY ) != 0 )
302 0 : || ( ( nPropertyAttributes & PropertyAttribute::TRANSIENT ) != 0 );
303 :
304 : // insert the new entry
305 : aPropertyPos = rObjectInfo.aProperties.insert( PropertiesInfo::value_type(
306 : _rEvent.PropertyName,
307 : PropertyInfo( bTransReadOnly )
308 0 : ) ).first;
309 0 : DBG_ASSERT(aPropertyPos != rObjectInfo.aProperties.end(), "OXUndoEnvironment::propertyChange : just inserted it ... why it's not there ?");
310 : }
311 :
312 0 : implSetModified();
313 :
314 : // now we have access to the cached info about the property affected
315 : // and are able to decide whether or not we need an undo action
316 :
317 : // no UNDO for transient/readonly properties
318 0 : if ( aPropertyPos->second.bIsReadonlyOrTransient )
319 : return;
320 :
321 : // give components with sub responsibilities a chance
322 0 : m_pImpl->m_aFormatNormalizer.notifyPropertyChange( _rEvent );
323 0 : m_pImpl->m_aConditionUpdater.notifyPropertyChange( _rEvent );
324 :
325 0 : aGuard.clear();
326 : // TODO: this is a potential race condition: two threads here could in theory
327 : // add their undo actions out-of-order
328 :
329 0 : SolarMutexGuard aSolarGuard;
330 0 : ORptUndoPropertyAction* pUndo = NULL;
331 : try
332 : {
333 0 : uno::Reference< report::XSection> xSection( xSet, uno::UNO_QUERY );
334 0 : if ( xSection.is() )
335 : {
336 0 : uno::Reference< report::XGroup> xGroup = xSection->getGroup();
337 0 : if ( xGroup.is() )
338 0 : pUndo = new OUndoPropertyGroupSectionAction( m_pImpl->m_rModel, _rEvent, OGroupHelper::getMemberFunction( xSection ), xGroup );
339 : else
340 0 : pUndo = new OUndoPropertyReportSectionAction( m_pImpl->m_rModel, _rEvent, OReportHelper::getMemberFunction( xSection ), xSection->getReportDefinition() );
341 0 : }
342 : }
343 0 : catch(const Exception&)
344 : {
345 : DBG_UNHANDLED_EXCEPTION();
346 : }
347 :
348 0 : if ( pUndo == NULL )
349 0 : pUndo = new ORptUndoPropertyAction( m_pImpl->m_rModel, _rEvent );
350 :
351 0 : m_pImpl->m_rModel.GetSdrUndoManager()->AddUndoAction( pUndo );
352 0 : pController->InvalidateAll();
353 : }
354 : // -----------------------------------------------------------------------------
355 0 : ::std::vector< uno::Reference< container::XChild> >::const_iterator OXUndoEnvironment::getSection(const Reference<container::XChild>& _xContainer) const
356 : {
357 0 : ::std::vector< uno::Reference< container::XChild> >::const_iterator aFind = m_pImpl->m_aSections.end();
358 0 : if ( _xContainer.is() )
359 : {
360 0 : aFind = ::std::find(m_pImpl->m_aSections.begin(),m_pImpl->m_aSections.end(),_xContainer);
361 :
362 0 : if ( aFind == m_pImpl->m_aSections.end() )
363 : {
364 0 : Reference<container::XChild> xParent(_xContainer->getParent(),uno::UNO_QUERY);
365 0 : aFind = getSection(xParent);
366 : }
367 : }
368 0 : return aFind;
369 : }
370 : // XContainerListener
371 : //------------------------------------------------------------------------------
372 0 : void SAL_CALL OXUndoEnvironment::elementInserted(const ContainerEvent& evt) throw(uno::RuntimeException)
373 : {
374 0 : SolarMutexGuard aSolarGuard;
375 0 : ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
376 :
377 : // neues Object zum lauschen
378 0 : Reference< uno::XInterface > xIface( evt.Element, UNO_QUERY );
379 0 : if ( !IsLocked() )
380 : {
381 0 : Reference< report::XReportComponent > xReportComponent( xIface, UNO_QUERY );
382 0 : if ( xReportComponent.is() )
383 : {
384 0 : Reference< report::XSection > xContainer(evt.Source,uno::UNO_QUERY);
385 :
386 0 : ::std::vector< uno::Reference< container::XChild> >::const_iterator aFind = getSection(xContainer.get());
387 :
388 0 : if ( aFind != m_pImpl->m_aSections.end() )
389 : {
390 0 : OUndoEnvLock aLock(*this);
391 : try
392 : {
393 0 : OReportPage* pPage = m_pImpl->m_rModel.getPage(uno::Reference< report::XSection>(*aFind,uno::UNO_QUERY));
394 : OSL_ENSURE(pPage,"No page could be found for section!");
395 0 : if ( pPage )
396 0 : pPage->insertObject(xReportComponent);
397 : }
398 0 : catch(uno::Exception&)
399 : {
400 : DBG_UNHANDLED_EXCEPTION();
401 0 : }
402 :
403 0 : }
404 : }
405 : else
406 : {
407 0 : uno::Reference< report::XFunctions> xContainer(evt.Source,uno::UNO_QUERY);
408 0 : if ( xContainer.is() )
409 : {
410 0 : m_pImpl->m_rModel.GetSdrUndoManager()->AddUndoAction(
411 0 : new OUndoContainerAction( m_pImpl->m_rModel, rptui::Inserted, xContainer.get(),
412 0 : xIface, RID_STR_UNDO_ADDFUNCTION ) );
413 0 : }
414 0 : }
415 : }
416 :
417 0 : AddElement(xIface);
418 :
419 0 : implSetModified();
420 0 : }
421 :
422 : //------------------------------------------------------------------------------
423 0 : void OXUndoEnvironment::implSetModified()
424 : {
425 0 : m_pImpl->m_rModel.SetModified( sal_True );
426 0 : }
427 :
428 : //------------------------------------------------------------------------------
429 0 : void SAL_CALL OXUndoEnvironment::elementReplaced(const ContainerEvent& evt) throw(uno::RuntimeException)
430 : {
431 0 : SolarMutexGuard aSolarGuard;
432 0 : ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
433 :
434 0 : Reference< XInterface > xIface(evt.ReplacedElement,uno::UNO_QUERY);
435 : OSL_ENSURE(xIface.is(), "OXUndoEnvironment::elementReplaced: invalid container notification!");
436 0 : RemoveElement(xIface);
437 :
438 0 : xIface.set(evt.Element,uno::UNO_QUERY);
439 0 : AddElement(xIface);
440 :
441 0 : implSetModified();
442 0 : }
443 :
444 : //------------------------------------------------------------------------------
445 0 : void SAL_CALL OXUndoEnvironment::elementRemoved(const ContainerEvent& evt) throw(uno::RuntimeException)
446 : {
447 0 : SolarMutexGuard aSolarGuard;
448 0 : ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
449 :
450 0 : Reference< uno::XInterface > xIface( evt.Element, UNO_QUERY );
451 0 : if ( !IsLocked() )
452 : {
453 0 : Reference< report::XSection > xContainer(evt.Source,uno::UNO_QUERY);
454 0 : ::std::vector< uno::Reference< container::XChild> >::const_iterator aFind = getSection(xContainer.get());
455 :
456 0 : Reference< report::XReportComponent > xReportComponent( xIface, UNO_QUERY );
457 0 : if ( aFind != m_pImpl->m_aSections.end() && xReportComponent.is() )
458 : {
459 0 : OXUndoEnvironment::OUndoEnvLock aLock(*this);
460 : try
461 : {
462 0 : OReportPage* pPage = m_pImpl->m_rModel.getPage(uno::Reference< report::XSection >( *aFind, uno::UNO_QUERY_THROW ) );
463 : OSL_ENSURE( pPage, "OXUndoEnvironment::elementRemoved: no page for the section!" );
464 0 : if ( pPage )
465 0 : pPage->removeSdrObject(xReportComponent);
466 : }
467 0 : catch(const uno::Exception&)
468 : {
469 : DBG_UNHANDLED_EXCEPTION();
470 0 : }
471 : }
472 : else
473 : {
474 0 : uno::Reference< report::XFunctions> xFunctions(evt.Source,uno::UNO_QUERY);
475 0 : if ( xFunctions.is() )
476 : {
477 0 : m_pImpl->m_rModel.GetSdrUndoManager()->AddUndoAction( new OUndoContainerAction(
478 0 : m_pImpl->m_rModel, rptui::Removed, xFunctions.get(), xIface, RID_STR_UNDO_ADDFUNCTION ) );
479 0 : }
480 0 : }
481 : }
482 :
483 0 : if ( xIface.is() )
484 0 : RemoveElement(xIface);
485 :
486 0 : implSetModified();
487 0 : }
488 :
489 : //------------------------------------------------------------------------------
490 0 : void SAL_CALL OXUndoEnvironment::modified( const EventObject& /*aEvent*/ ) throw (RuntimeException)
491 : {
492 0 : implSetModified();
493 0 : }
494 :
495 : //------------------------------------------------------------------------------
496 0 : void OXUndoEnvironment::AddSection(const Reference< report::XSection > & _xSection)
497 : {
498 0 : OUndoEnvLock aLock(*this);
499 : try
500 : {
501 0 : uno::Reference<container::XChild> xChild = _xSection.get();
502 0 : uno::Reference<report::XGroup> xGroup(xChild->getParent(),uno::UNO_QUERY);
503 0 : m_pImpl->m_aSections.push_back(xChild);
504 0 : Reference< XInterface > xInt(_xSection);
505 0 : AddElement(xInt);
506 : }
507 0 : catch(const uno::Exception&)
508 : {
509 : DBG_UNHANDLED_EXCEPTION();
510 0 : }
511 0 : }
512 :
513 : //------------------------------------------------------------------------------
514 0 : void OXUndoEnvironment::RemoveSection(const Reference< report::XSection > & _xSection)
515 : {
516 0 : OUndoEnvLock aLock(*this);
517 : try
518 : {
519 0 : uno::Reference<container::XChild> xChild(_xSection.get());
520 0 : m_pImpl->m_aSections.erase(::std::remove(m_pImpl->m_aSections.begin(),m_pImpl->m_aSections.end(),
521 0 : xChild), m_pImpl->m_aSections.end());
522 0 : Reference< XInterface > xInt(_xSection);
523 0 : RemoveElement(xInt);
524 : }
525 0 : catch(uno::Exception&){}
526 0 : }
527 :
528 : //------------------------------------------------------------------------------
529 0 : void OXUndoEnvironment::TogglePropertyListening(const Reference< XInterface > & Element)
530 : {
531 : // am Container horchen
532 0 : Reference< XIndexAccess > xContainer(Element, UNO_QUERY);
533 0 : if (xContainer.is())
534 : {
535 0 : Reference< XInterface > xInterface;
536 0 : sal_Int32 nCount = xContainer->getCount();
537 0 : for(sal_Int32 i = 0;i != nCount;++i)
538 : {
539 0 : xInterface.set(xContainer->getByIndex( i ),uno::UNO_QUERY);
540 0 : TogglePropertyListening(xInterface);
541 0 : }
542 : }
543 :
544 0 : Reference< XPropertySet > xSet(Element, UNO_QUERY);
545 0 : if (xSet.is())
546 : {
547 0 : if (!m_pImpl->m_bReadOnly)
548 0 : xSet->addPropertyChangeListener( ::rtl::OUString(), this );
549 : else
550 0 : xSet->removePropertyChangeListener( ::rtl::OUString(), this );
551 0 : }
552 0 : }
553 :
554 :
555 : //------------------------------------------------------------------------------
556 0 : void OXUndoEnvironment::switchListening( const Reference< XIndexAccess >& _rxContainer, bool _bStartListening ) SAL_THROW(())
557 : {
558 : OSL_PRECOND( _rxContainer.is(), "OXUndoEnvironment::switchListening: invalid container!" );
559 0 : if ( !_rxContainer.is() )
560 0 : return;
561 :
562 : try
563 : {
564 : // also handle all children of this element
565 0 : Reference< XInterface > xInterface;
566 0 : sal_Int32 nCount = _rxContainer->getCount();
567 0 : for(sal_Int32 i = 0;i != nCount;++i)
568 : {
569 0 : xInterface.set(_rxContainer->getByIndex( i ),uno::UNO_QUERY);
570 0 : if ( _bStartListening )
571 0 : AddElement( xInterface );
572 : else
573 0 : RemoveElement( xInterface );
574 : }
575 :
576 : // be notified of any changes in the container elements
577 0 : Reference< XContainer > xSimpleContainer( _rxContainer, UNO_QUERY );
578 0 : if ( xSimpleContainer.is() )
579 : {
580 0 : if ( _bStartListening )
581 0 : xSimpleContainer->addContainerListener( this );
582 : else
583 0 : xSimpleContainer->removeContainerListener( this );
584 0 : }
585 : }
586 0 : catch( const Exception& )
587 : {
588 : DBG_UNHANDLED_EXCEPTION();
589 : }
590 : }
591 :
592 : //------------------------------------------------------------------------------
593 0 : void OXUndoEnvironment::switchListening( const Reference< XInterface >& _rxObject, bool _bStartListening ) SAL_THROW(())
594 : {
595 : OSL_PRECOND( _rxObject.is(), "OXUndoEnvironment::switchListening: how should I listen at a NULL object?" );
596 :
597 : try
598 : {
599 0 : if ( !m_pImpl->m_bReadOnly )
600 : {
601 0 : Reference< XPropertySet > xProps( _rxObject, UNO_QUERY );
602 0 : if ( xProps.is() )
603 : {
604 0 : if ( _bStartListening )
605 0 : xProps->addPropertyChangeListener( ::rtl::OUString(), this );
606 : else
607 0 : xProps->removePropertyChangeListener( ::rtl::OUString(), this );
608 0 : }
609 : }
610 :
611 0 : Reference< XModifyBroadcaster > xBroadcaster( _rxObject, UNO_QUERY );
612 0 : if ( xBroadcaster.is() )
613 : {
614 0 : if ( _bStartListening )
615 0 : xBroadcaster->addModifyListener( this );
616 : else
617 0 : xBroadcaster->removeModifyListener( this );
618 0 : }
619 : }
620 0 : catch( const Exception& )
621 : {
622 : }
623 0 : }
624 :
625 : //------------------------------------------------------------------------------
626 0 : void OXUndoEnvironment::AddElement(const Reference< XInterface >& _rxElement )
627 : {
628 0 : if ( !IsLocked() )
629 0 : m_pImpl->m_aFormatNormalizer.notifyElementInserted( _rxElement );
630 :
631 : // if it's a container, start listening at all elements
632 0 : Reference< XIndexAccess > xContainer( _rxElement, UNO_QUERY );
633 0 : if ( xContainer.is() )
634 0 : switchListening( xContainer, true );
635 :
636 0 : switchListening( _rxElement, true );
637 0 : }
638 :
639 : //------------------------------------------------------------------------------
640 0 : void OXUndoEnvironment::RemoveElement(const Reference< XInterface >& _rxElement)
641 : {
642 0 : uno::Reference<beans::XPropertySet> xProp(_rxElement,uno::UNO_QUERY);
643 0 : if (!m_pImpl->m_aPropertySetCache.empty())
644 0 : m_pImpl->m_aPropertySetCache.erase(xProp);
645 0 : switchListening( _rxElement, false );
646 :
647 0 : Reference< XIndexAccess > xContainer( _rxElement, UNO_QUERY );
648 0 : if ( xContainer.is() )
649 0 : switchListening( xContainer, false );
650 0 : }
651 :
652 0 : void OXUndoEnvironment::SetUndoMode(sal_Bool _bUndo)
653 : {
654 0 : m_pImpl->m_bIsUndo = _bUndo;
655 0 : }
656 :
657 0 : sal_Bool OXUndoEnvironment::IsUndoMode() const
658 : {
659 0 : return m_pImpl->m_bIsUndo;
660 : }
661 : //============================================================================
662 : } // rptui
663 : //============================================================================
664 :
665 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|