1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "UndoGuard.hxx"
31 #include "ChartModelClone.hxx"
32 #include "UndoActions.hxx"
34 #include <com/sun/star/container/XChild.hpp>
36 #include <tools/diagnose_ex.h>
38 using namespace ::com::sun::star
;
40 using ::com::sun::star::uno::Reference
;
41 using ::com::sun::star::uno::Sequence
;
42 using ::rtl::OUString
;
47 //-----------------------------------------------------------------------------
49 UndoGuard::UndoGuard( const OUString
& i_undoString
, const uno::Reference
< document::XUndoManager
> & i_undoManager
,
50 const ModelFacet i_facet
)
51 :m_xChartModel( i_undoManager
->getParent(), uno::UNO_QUERY_THROW
)
52 ,m_xUndoManager( i_undoManager
)
53 ,m_pDocumentSnapshot()
54 ,m_aUndoString( i_undoString
)
55 ,m_bActionPosted( false )
57 m_pDocumentSnapshot
.reset( new ChartModelClone( m_xChartModel
, i_facet
) );
60 //-----------------------------------------------------------------------------
62 UndoGuard::~UndoGuard()
64 if ( !!m_pDocumentSnapshot
)
68 //-----------------------------------------------------------------------------
70 void UndoGuard::commit()
72 if ( !m_bActionPosted
&& !!m_pDocumentSnapshot
)
76 const Reference
< document::XUndoAction
> xAction( new impl::UndoElement( m_aUndoString
, m_xChartModel
, m_pDocumentSnapshot
) );
77 m_pDocumentSnapshot
.reset(); // don't dispose, it's data went over to the UndoElement
78 m_xUndoManager
->addUndoAction( xAction
);
80 catch( const uno::Exception
& )
82 DBG_UNHANDLED_EXCEPTION();
85 m_bActionPosted
= true;
88 //-----------------------------------------------------------------------------
90 void UndoGuard::rollback()
92 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot
, "no snapshot!" );
93 m_pDocumentSnapshot
->applyToModel( m_xChartModel
);
97 //-----------------------------------------------------------------------------
98 void UndoGuard::discardSnapshot()
100 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot
, "no snapshot!" );
101 m_pDocumentSnapshot
->dispose();
102 m_pDocumentSnapshot
.reset();
105 //-----------------------------------------------------------------------------
107 UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString
& i_undoString
, const uno::Reference
< document::XUndoManager
>& i_undoManager
)
108 :UndoGuard( i_undoString
, i_undoManager
, E_MODEL
)
112 UndoLiveUpdateGuard::~UndoLiveUpdateGuard()
114 if ( !isActionPosted() )
118 //-----------------------------------------------------------------------------
120 UndoLiveUpdateGuardWithData::UndoLiveUpdateGuardWithData(
121 const OUString
& i_undoString
, const uno::Reference
< document::XUndoManager
>& i_undoManager
)
122 :UndoGuard( i_undoString
, i_undoManager
, E_MODEL_WITH_DATA
)
126 UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData()
128 if ( !isActionPosted() )
132 //-----------------------------------------------------------------------------
134 UndoGuardWithSelection::UndoGuardWithSelection(
135 const OUString
& i_undoString
, const uno::Reference
< document::XUndoManager
>& i_undoManager
)
136 :UndoGuard( i_undoString
, i_undoManager
, E_MODEL_WITH_SELECTION
)
140 //-----------------------------------------------------------------------------
142 UndoGuardWithSelection::~UndoGuardWithSelection()
144 if ( !isActionPosted() )
148 HiddenUndoContext::HiddenUndoContext( const Reference
< document::XUndoManager
> & i_undoManager
)
149 :m_xUndoManager( i_undoManager
)
151 ENSURE_OR_THROW( m_xUndoManager
.is(), "invalid undo manager!" );
154 m_xUndoManager
->enterHiddenUndoContext();
156 catch( const uno::Exception
& )
158 DBG_UNHANDLED_EXCEPTION();
159 m_xUndoManager
.clear();
160 // prevents the leaveUndoContext in the dtor
164 //-----------------------------------------------------------------------------
166 HiddenUndoContext::~HiddenUndoContext()
170 if ( m_xUndoManager
.is() )
171 m_xUndoManager
->leaveUndoContext();
173 catch( const uno::Exception
& )
175 DBG_UNHANDLED_EXCEPTION();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */