1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: UndoActions.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "precompiled_reportdesign.hxx"
32 #include "UndoActions.hxx"
33 #include "UndoEnv.hxx"
34 #include "formatnormalizer.hxx"
35 #include "conditionupdater.hxx"
36 #include "corestrings.hrc"
37 #include "rptui_slotid.hrc"
39 #include "ModuleHelper.hxx"
40 #include "RptObject.hxx"
41 #include "RptPage.hxx"
42 #include "RptResId.hrc"
43 #include "RptModel.hxx"
45 /** === begin UNO includes === **/
46 #include <com/sun/star/script/XEventAttacherManager.hpp>
47 #include <com/sun/star/container/XChild.hpp>
48 #include <com/sun/star/container/XNameContainer.hpp>
49 #include <com/sun/star/beans/PropertyAttribute.hpp>
50 #include <com/sun/star/util/XModifyBroadcaster.hpp>
51 /** === end UNO includes === **/
53 #include <connectivity/dbtools.hxx>
54 #include <svtools/smplhint.hxx>
55 #include <tools/diagnose_ex.h>
56 #include <comphelper/stl_types.hxx>
57 #include <vcl/svapp.hxx>
58 #include <dbaccess/singledoccontroller.hxx>
59 #include <svx/unoshape.hxx>
60 #include <vos/mutex.hxx>
64 using namespace ::com::sun::star
;
67 using namespace script
;
68 using namespace beans
;
71 using namespace container
;
72 using namespace report
;
73 //----------------------------------------------------------------------------
74 ::std::mem_fun_t
<uno::Reference
<report::XSection
> , OGroupHelper
> OGroupHelper::getMemberFunction(const Reference
< XSection
>& _xSection
)
76 ::std::mem_fun_t
<uno::Reference
<report::XSection
> , OGroupHelper
> pMemFunSection
= ::std::mem_fun(&OGroupHelper::getFooter
);
77 uno::Reference
< report::XGroup
> xGroup
= _xSection
->getGroup();
78 if ( xGroup
->getHeaderOn() && xGroup
->getHeader() == _xSection
)
79 pMemFunSection
= ::std::mem_fun(&OGroupHelper::getHeader
);
80 return pMemFunSection
;
82 // -----------------------------------------------------------------------------
83 ::std::mem_fun_t
<uno::Reference
<report::XSection
> , OReportHelper
> OReportHelper::getMemberFunction(const Reference
< XSection
>& _xSection
)
85 uno::Reference
< report::XReportDefinition
> xReportDefinition(_xSection
->getReportDefinition());
86 ::std::mem_fun_t
<uno::Reference
<report::XSection
> , OReportHelper
> pMemFunSection
= ::std::mem_fun(&OReportHelper::getReportFooter
);
87 if ( xReportDefinition
->getReportHeaderOn() && xReportDefinition
->getReportHeader() == _xSection
)
88 pMemFunSection
= ::std::mem_fun(&OReportHelper::getReportHeader
);
89 else if ( xReportDefinition
->getPageHeaderOn() && xReportDefinition
->getPageHeader() == _xSection
)
90 pMemFunSection
= ::std::mem_fun(&OReportHelper::getPageHeader
);
91 else if ( xReportDefinition
->getPageFooterOn() && xReportDefinition
->getPageFooter() == _xSection
)
92 pMemFunSection
= ::std::mem_fun(&OReportHelper::getPageFooter
);
93 else if ( xReportDefinition
->getDetail() == _xSection
)
94 pMemFunSection
= ::std::mem_fun(&OReportHelper::getDetail
);
95 return pMemFunSection
;
98 //------------------------------------------------------------------------------
99 TYPEINIT1( OCommentUndoAction
, SdrUndoAction
);
100 DBG_NAME(rpt_OCommentUndoAction
)
101 //----------------------------------------------------------------------------
102 OCommentUndoAction::OCommentUndoAction(SdrModel
& _rMod
,USHORT nCommentID
)
103 :SdrUndoAction(_rMod
)
105 DBG_CTOR(rpt_OCommentUndoAction
,NULL
);
106 m_pController
= static_cast< OReportModel
& >( _rMod
).getController();
108 m_strComment
= String(ModuleRes(nCommentID
));
110 OCommentUndoAction::~OCommentUndoAction()
112 DBG_DTOR(rpt_OCommentUndoAction
,NULL
);
114 //----------------------------------------------------------------------------
115 void OCommentUndoAction::Undo()
118 //----------------------------------------------------------------------------
119 void OCommentUndoAction::Redo()
122 DBG_NAME( rpt_OUndoContainerAction
);
123 //------------------------------------------------------------------------------
124 OUndoContainerAction::OUndoContainerAction(SdrModel
& _rMod
126 ,const uno::Reference
< container::XIndexContainer
> _xContainer
127 ,const Reference
< XInterface
> & xElem
129 :OCommentUndoAction(_rMod
,_nCommentId
)
131 ,m_xContainer(_xContainer
)
132 ,m_eAction( _eAction
)
134 DBG_CTOR( rpt_OUndoContainerAction
,NULL
);
136 if ( m_eAction
== Removed
)
137 // we now own the element
138 m_xOwnElement
= m_xElement
;
140 //------------------------------------------------------------------------------
141 OUndoContainerAction::~OUndoContainerAction()
143 // if we own the object ....
144 Reference
< XComponent
> xComp( m_xOwnElement
, UNO_QUERY
);
147 // and the object does not have a parent
148 Reference
< XChild
> xChild( m_xOwnElement
, UNO_QUERY
);
149 if ( xChild
.is() && !xChild
->getParent().is() )
151 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( rMod
).GetUndoEnv();
152 rEnv
.RemoveElement( m_xOwnElement
);
154 #if OSL_DEBUG_LEVEL > 0
155 SvxShape
* pShape
= SvxShape::getImplementation( xChild
);
156 SdrObject
* pObject
= pShape
? pShape
->GetSdrObject() : NULL
;
157 OSL_ENSURE( pObject
? pShape
->HasSdrObjectOwnership() && !pObject
->IsInserted() : true ,
158 "OUndoContainerAction::~OUndoContainerAction: inconsistency in the shape/object ownership!" );
163 comphelper::disposeComponent( xComp
);
165 catch ( const uno::Exception
& )
167 DBG_UNHANDLED_EXCEPTION();
171 DBG_DTOR( rpt_OUndoContainerAction
,NULL
);
173 //------------------------------------------------------------------------------
174 void OUndoContainerAction::implReInsert( ) SAL_THROW( ( Exception
) )
176 if ( m_xContainer
.is() )
178 // insert the element
179 m_xContainer
->insertByIndex( m_xContainer
->getCount(),uno::makeAny(m_xElement
) );
181 // we don't own the object anymore
182 m_xOwnElement
= NULL
;
185 //------------------------------------------------------------------------------
186 void OUndoContainerAction::implReRemove( ) SAL_THROW( ( Exception
) )
188 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( rMod
).GetUndoEnv();
191 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
192 if ( m_xContainer
.is() )
194 const sal_Int32 nCount
= m_xContainer
->getCount();
195 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
197 uno::Reference
< uno::XInterface
> xObj(m_xContainer
->getByIndex(i
),uno::UNO_QUERY
);
198 if ( xObj
== m_xElement
)
200 m_xContainer
->removeByIndex( i
);
206 catch(uno::Exception
&){}
207 // from now on, we own this object
208 m_xOwnElement
= m_xElement
;
211 //------------------------------------------------------------------------------
212 void OUndoContainerAction::Undo()
214 if ( m_xElement
.is() )
216 // prevents that an undo action will be created for elementInserted
229 OSL_ENSURE(0,"Illegal case value");
233 catch( const Exception
& )
235 OSL_ENSURE( sal_False
, "OUndoContainerAction::Undo: caught an exception!" );
240 //------------------------------------------------------------------------------
241 void OUndoContainerAction::Redo()
243 if ( m_xElement
.is() )
257 OSL_ENSURE(0,"Illegal case value");
261 catch( const Exception
& )
263 OSL_ENSURE( sal_False
, "OUndoContainerAction::Redo: caught an exception!" );
267 // -----------------------------------------------------------------------------
268 OUndoGroupSectionAction::OUndoGroupSectionAction(SdrModel
& _rMod
270 ,::std::mem_fun_t
< uno::Reference
< report::XSection
>
271 ,OGroupHelper
> _pMemberFunction
272 ,const uno::Reference
< report::XGroup
>& _xGroup
273 ,const Reference
< XInterface
> & xElem
275 :OUndoContainerAction(_rMod
,_eAction
,NULL
,xElem
,_nCommentId
)
276 ,m_aGroupHelper(_xGroup
)
277 ,m_pMemberFunction(_pMemberFunction
)
280 //------------------------------------------------------------------------------
281 void OUndoGroupSectionAction::implReInsert( ) SAL_THROW( ( Exception
) )
283 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( rMod
).GetUndoEnv();
286 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
287 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aGroupHelper
);
289 xSection
->add(uno::Reference
< drawing::XShape
>(m_xElement
,uno::UNO_QUERY
));
291 catch(uno::Exception
&){}
293 // we don't own the object anymore
294 m_xOwnElement
= NULL
;
297 //------------------------------------------------------------------------------
298 void OUndoGroupSectionAction::implReRemove( ) SAL_THROW( ( Exception
) )
300 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( rMod
).GetUndoEnv();
303 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
304 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aGroupHelper
);
306 xSection
->remove(uno::Reference
< drawing::XShape
>(m_xElement
,uno::UNO_QUERY
));
308 catch(uno::Exception
&){}
310 // from now on, we own this object
311 m_xOwnElement
= m_xElement
;
313 //----------------------------------------------------------------------------
314 OUndoReportSectionAction::OUndoReportSectionAction(SdrModel
& _rMod
316 ,::std::mem_fun_t
< uno::Reference
< report::XSection
>
317 ,OReportHelper
> _pMemberFunction
318 ,const uno::Reference
< report::XReportDefinition
>& _xReport
319 ,const Reference
< XInterface
> & xElem
321 :OUndoContainerAction(_rMod
,_eAction
,NULL
,xElem
,_nCommentId
)
322 ,m_aReportHelper(_xReport
)
323 ,m_pMemberFunction(_pMemberFunction
)
326 //------------------------------------------------------------------------------
327 void OUndoReportSectionAction::implReInsert( ) SAL_THROW( ( Exception
) )
329 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( rMod
).GetUndoEnv();
332 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
333 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aReportHelper
);
336 uno::Reference
< drawing::XShape
> xShape(m_xElement
,uno::UNO_QUERY_THROW
);
337 awt::Point aPos
= xShape
->getPosition();
338 awt::Size aSize
= xShape
->getSize();
339 xSection
->add(xShape
);
340 xShape
->setPosition( aPos
);
341 xShape
->setSize( aSize
);
344 catch(uno::Exception
&){}
345 // we don't own the object anymore
346 m_xOwnElement
= NULL
;
349 //------------------------------------------------------------------------------
350 void OUndoReportSectionAction::implReRemove( ) SAL_THROW( ( Exception
) )
352 OXUndoEnvironment
& rEnv
= static_cast< OReportModel
& >( rMod
).GetUndoEnv();
355 OXUndoEnvironment::OUndoEnvLock
aLock(rEnv
);
356 uno::Reference
< report::XSection
> xSection
= m_pMemberFunction(&m_aReportHelper
);
358 xSection
->remove(uno::Reference
< drawing::XShape
>(m_xElement
,uno::UNO_QUERY
));
360 catch(uno::Exception
&){}
361 // from now on, we own this object
362 m_xOwnElement
= m_xElement
;
364 //------------------------------------------------------------------------------
365 ORptUndoPropertyAction::ORptUndoPropertyAction(SdrModel
& rNewMod
, const PropertyChangeEvent
& evt
)
366 :OCommentUndoAction(rNewMod
,0)
367 ,m_xObj(evt
.Source
, UNO_QUERY
)
368 ,m_aPropertyName(evt
.PropertyName
)
369 ,m_aNewValue(evt
.NewValue
)
370 ,m_aOldValue(evt
.OldValue
)
373 //------------------------------------------------------------------------------
374 void ORptUndoPropertyAction::Undo()
376 setProperty(sal_True
);
379 //------------------------------------------------------------------------------
380 void ORptUndoPropertyAction::Redo()
382 setProperty(sal_False
);
384 // -----------------------------------------------------------------------------
385 Reference
< XPropertySet
> ORptUndoPropertyAction::getObject()
389 // -----------------------------------------------------------------------------
390 void ORptUndoPropertyAction::setProperty(sal_Bool _bOld
)
392 Reference
< XPropertySet
> xObj
= getObject();
398 xObj
->setPropertyValue( m_aPropertyName
, _bOld
? m_aOldValue
: m_aNewValue
);
400 catch( const Exception
& )
402 OSL_ENSURE( sal_False
, "ORptUndoPropertyAction::Redo: caught an exception!" );
407 //------------------------------------------------------------------------------
408 String
ORptUndoPropertyAction::GetComment() const
410 String
aStr(ModuleRes(RID_STR_UNDO_PROPERTY
));
412 aStr
.SearchAndReplace( '#', m_aPropertyName
);
415 // -----------------------------------------------------------------------------
416 OUndoPropertyGroupSectionAction::OUndoPropertyGroupSectionAction(SdrModel
& _rMod
417 ,const PropertyChangeEvent
& evt
418 ,::std::mem_fun_t
< uno::Reference
< report::XSection
>
419 ,OGroupHelper
> _pMemberFunction
420 ,const uno::Reference
< report::XGroup
>& _xGroup
422 :ORptUndoPropertyAction(_rMod
,evt
)
423 ,m_aGroupHelper(_xGroup
)
424 ,m_pMemberFunction(_pMemberFunction
)
427 // -----------------------------------------------------------------------------
428 Reference
< XPropertySet
> OUndoPropertyGroupSectionAction::getObject()
430 return m_pMemberFunction(&m_aGroupHelper
).get();
432 // -----------------------------------------------------------------------------
433 OUndoPropertyReportSectionAction::OUndoPropertyReportSectionAction(SdrModel
& _rMod
434 ,const PropertyChangeEvent
& evt
435 ,::std::mem_fun_t
< uno::Reference
< report::XSection
>
436 ,OReportHelper
> _pMemberFunction
437 ,const uno::Reference
< report::XReportDefinition
>& _xReport
439 :ORptUndoPropertyAction(_rMod
,evt
)
440 ,m_aReportHelper(_xReport
)
441 ,m_pMemberFunction(_pMemberFunction
)
444 // -----------------------------------------------------------------------------
445 Reference
< XPropertySet
> OUndoPropertyReportSectionAction::getObject()
447 return m_pMemberFunction(&m_aReportHelper
).get();
449 //============================================================================
451 //============================================================================