1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*cde9e8dcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*cde9e8dcSAndrew Rist * distributed with this work for additional information
6*cde9e8dcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*cde9e8dcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist * with the License. You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist *
11*cde9e8dcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist *
13*cde9e8dcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist * KIND, either express or implied. See the License for the
17*cde9e8dcSAndrew Rist * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist * under the License.
19*cde9e8dcSAndrew Rist *
20*cde9e8dcSAndrew Rist *************************************************************/
21*cde9e8dcSAndrew Rist
22*cde9e8dcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "UndoGuard.hxx"
28cdf0e10cSrcweir #include "ChartModelClone.hxx"
29cdf0e10cSrcweir #include "UndoActions.hxx"
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include <tools/diagnose_ex.h>
34cdf0e10cSrcweir
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir
37cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
38cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
39cdf0e10cSrcweir using ::rtl::OUString;
40cdf0e10cSrcweir
41cdf0e10cSrcweir namespace chart
42cdf0e10cSrcweir {
43cdf0e10cSrcweir
44cdf0e10cSrcweir //-----------------------------------------------------------------------------
45cdf0e10cSrcweir
UndoGuard(const OUString & i_undoString,const uno::Reference<document::XUndoManager> & i_undoManager,const ModelFacet i_facet)46cdf0e10cSrcweir UndoGuard::UndoGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager > & i_undoManager,
47cdf0e10cSrcweir const ModelFacet i_facet )
48cdf0e10cSrcweir :m_xChartModel( i_undoManager->getParent(), uno::UNO_QUERY_THROW )
49cdf0e10cSrcweir ,m_xUndoManager( i_undoManager )
50cdf0e10cSrcweir ,m_pDocumentSnapshot()
51cdf0e10cSrcweir ,m_aUndoString( i_undoString )
52cdf0e10cSrcweir ,m_bActionPosted( false )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir m_pDocumentSnapshot.reset( new ChartModelClone( m_xChartModel, i_facet ) );
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
57cdf0e10cSrcweir //-----------------------------------------------------------------------------
58cdf0e10cSrcweir
~UndoGuard()59cdf0e10cSrcweir UndoGuard::~UndoGuard()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir if ( !!m_pDocumentSnapshot )
62cdf0e10cSrcweir discardSnapshot();
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
65cdf0e10cSrcweir //-----------------------------------------------------------------------------
66cdf0e10cSrcweir
commit()67cdf0e10cSrcweir void UndoGuard::commit()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir if ( !m_bActionPosted && !!m_pDocumentSnapshot )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir try
72cdf0e10cSrcweir {
73cdf0e10cSrcweir const Reference< document::XUndoAction > xAction( new impl::UndoElement( m_aUndoString, m_xChartModel, m_pDocumentSnapshot ) );
74cdf0e10cSrcweir m_pDocumentSnapshot.reset(); // don't dispose, it's data went over to the UndoElement
75cdf0e10cSrcweir m_xUndoManager->addUndoAction( xAction );
76cdf0e10cSrcweir }
77cdf0e10cSrcweir catch( const uno::Exception& )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir }
82cdf0e10cSrcweir m_bActionPosted = true;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
85cdf0e10cSrcweir //-----------------------------------------------------------------------------
86cdf0e10cSrcweir
rollback()87cdf0e10cSrcweir void UndoGuard::rollback()
88cdf0e10cSrcweir {
89cdf0e10cSrcweir ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
90cdf0e10cSrcweir m_pDocumentSnapshot->applyToModel( m_xChartModel );
91cdf0e10cSrcweir discardSnapshot();
92cdf0e10cSrcweir }
93cdf0e10cSrcweir
94cdf0e10cSrcweir //-----------------------------------------------------------------------------
discardSnapshot()95cdf0e10cSrcweir void UndoGuard::discardSnapshot()
96cdf0e10cSrcweir {
97cdf0e10cSrcweir ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" );
98cdf0e10cSrcweir m_pDocumentSnapshot->dispose();
99cdf0e10cSrcweir m_pDocumentSnapshot.reset();
100cdf0e10cSrcweir }
101cdf0e10cSrcweir
102cdf0e10cSrcweir //-----------------------------------------------------------------------------
103cdf0e10cSrcweir
UndoLiveUpdateGuard(const OUString & i_undoString,const uno::Reference<document::XUndoManager> & i_undoManager)104cdf0e10cSrcweir UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
105cdf0e10cSrcweir :UndoGuard( i_undoString, i_undoManager, E_MODEL )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir }
108cdf0e10cSrcweir
~UndoLiveUpdateGuard()109cdf0e10cSrcweir UndoLiveUpdateGuard::~UndoLiveUpdateGuard()
110cdf0e10cSrcweir {
111cdf0e10cSrcweir if ( !isActionPosted() )
112cdf0e10cSrcweir rollback();
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir //-----------------------------------------------------------------------------
116cdf0e10cSrcweir
UndoLiveUpdateGuardWithData(const OUString & i_undoString,const uno::Reference<document::XUndoManager> & i_undoManager)117cdf0e10cSrcweir UndoLiveUpdateGuardWithData::UndoLiveUpdateGuardWithData(
118cdf0e10cSrcweir const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
119cdf0e10cSrcweir :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_DATA )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir }
122cdf0e10cSrcweir
~UndoLiveUpdateGuardWithData()123cdf0e10cSrcweir UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir if ( !isActionPosted() )
126cdf0e10cSrcweir rollback();
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
129cdf0e10cSrcweir //-----------------------------------------------------------------------------
130cdf0e10cSrcweir
UndoGuardWithSelection(const OUString & i_undoString,const uno::Reference<document::XUndoManager> & i_undoManager)131cdf0e10cSrcweir UndoGuardWithSelection::UndoGuardWithSelection(
132cdf0e10cSrcweir const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager )
133cdf0e10cSrcweir :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_SELECTION )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir //-----------------------------------------------------------------------------
138cdf0e10cSrcweir
~UndoGuardWithSelection()139cdf0e10cSrcweir UndoGuardWithSelection::~UndoGuardWithSelection()
140cdf0e10cSrcweir {
141cdf0e10cSrcweir if ( !isActionPosted() )
142cdf0e10cSrcweir rollback();
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir //-----------------------------------------------------------------------------
146cdf0e10cSrcweir
UndoContext(const Reference<document::XUndoManager> & i_undoManager,const::rtl::OUString & i_undoTitle)147cdf0e10cSrcweir UndoContext::UndoContext( const Reference< document::XUndoManager > & i_undoManager, const ::rtl::OUString& i_undoTitle )
148cdf0e10cSrcweir :m_xUndoManager( i_undoManager )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
151cdf0e10cSrcweir m_xUndoManager->enterUndoContext( i_undoTitle );
152cdf0e10cSrcweir }
153cdf0e10cSrcweir
154cdf0e10cSrcweir //-----------------------------------------------------------------------------
155cdf0e10cSrcweir
~UndoContext()156cdf0e10cSrcweir UndoContext::~UndoContext()
157cdf0e10cSrcweir {
158cdf0e10cSrcweir m_xUndoManager->leaveUndoContext();
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
161cdf0e10cSrcweir //-----------------------------------------------------------------------------
162cdf0e10cSrcweir
HiddenUndoContext(const Reference<document::XUndoManager> & i_undoManager)163cdf0e10cSrcweir HiddenUndoContext::HiddenUndoContext( const Reference< document::XUndoManager > & i_undoManager )
164cdf0e10cSrcweir :m_xUndoManager( i_undoManager )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" );
167cdf0e10cSrcweir try
168cdf0e10cSrcweir {
169cdf0e10cSrcweir m_xUndoManager->enterHiddenUndoContext();
170cdf0e10cSrcweir }
171cdf0e10cSrcweir catch( const uno::Exception& )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
174cdf0e10cSrcweir m_xUndoManager.clear();
175cdf0e10cSrcweir // prevents the leaveUndoContext in the dtor
176cdf0e10cSrcweir }
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir //-----------------------------------------------------------------------------
180cdf0e10cSrcweir
~HiddenUndoContext()181cdf0e10cSrcweir HiddenUndoContext::~HiddenUndoContext()
182cdf0e10cSrcweir {
183cdf0e10cSrcweir try
184cdf0e10cSrcweir {
185cdf0e10cSrcweir if ( m_xUndoManager.is() )
186cdf0e10cSrcweir m_xUndoManager->leaveUndoContext();
187cdf0e10cSrcweir }
188cdf0e10cSrcweir catch( const uno::Exception& )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
191cdf0e10cSrcweir }
192cdf0e10cSrcweir }
193cdf0e10cSrcweir
194cdf0e10cSrcweir } // namespace chart
195