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_chart2.hxx" 26 27 #include "UndoGuard.hxx" 28 #include "ChartModelClone.hxx" 29 #include "UndoActions.hxx" 30 31 #include <com/sun/star/container/XChild.hpp> 32 33 #include <tools/diagnose_ex.h> 34 35 using namespace ::com::sun::star; 36 37 using ::com::sun::star::uno::Reference; 38 using ::com::sun::star::uno::Sequence; 39 using ::rtl::OUString; 40 41 namespace chart 42 { 43 44 //----------------------------------------------------------------------------- 45 46 UndoGuard::UndoGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager > & i_undoManager, 47 const ModelFacet i_facet ) 48 :m_xChartModel( i_undoManager->getParent(), uno::UNO_QUERY_THROW ) 49 ,m_xUndoManager( i_undoManager ) 50 ,m_pDocumentSnapshot() 51 ,m_aUndoString( i_undoString ) 52 ,m_bActionPosted( false ) 53 { 54 m_pDocumentSnapshot.reset( new ChartModelClone( m_xChartModel, i_facet ) ); 55 } 56 57 //----------------------------------------------------------------------------- 58 59 UndoGuard::~UndoGuard() 60 { 61 if ( !!m_pDocumentSnapshot ) 62 discardSnapshot(); 63 } 64 65 //----------------------------------------------------------------------------- 66 67 void UndoGuard::commit() 68 { 69 if ( !m_bActionPosted && !!m_pDocumentSnapshot ) 70 { 71 try 72 { 73 const Reference< document::XUndoAction > xAction( new impl::UndoElement( m_aUndoString, m_xChartModel, m_pDocumentSnapshot ) ); 74 m_pDocumentSnapshot.reset(); // don't dispose, it's data went over to the UndoElement 75 m_xUndoManager->addUndoAction( xAction ); 76 } 77 catch( const uno::Exception& ) 78 { 79 DBG_UNHANDLED_EXCEPTION(); 80 } 81 } 82 m_bActionPosted = true; 83 } 84 85 //----------------------------------------------------------------------------- 86 87 void UndoGuard::rollback() 88 { 89 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" ); 90 m_pDocumentSnapshot->applyToModel( m_xChartModel ); 91 discardSnapshot(); 92 } 93 94 //----------------------------------------------------------------------------- 95 void UndoGuard::discardSnapshot() 96 { 97 ENSURE_OR_RETURN_VOID( !!m_pDocumentSnapshot, "no snapshot!" ); 98 m_pDocumentSnapshot->dispose(); 99 m_pDocumentSnapshot.reset(); 100 } 101 102 //----------------------------------------------------------------------------- 103 104 UndoLiveUpdateGuard::UndoLiveUpdateGuard( const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager ) 105 :UndoGuard( i_undoString, i_undoManager, E_MODEL ) 106 { 107 } 108 109 UndoLiveUpdateGuard::~UndoLiveUpdateGuard() 110 { 111 if ( !isActionPosted() ) 112 rollback(); 113 } 114 115 //----------------------------------------------------------------------------- 116 117 UndoLiveUpdateGuardWithData::UndoLiveUpdateGuardWithData( 118 const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager ) 119 :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_DATA ) 120 { 121 } 122 123 UndoLiveUpdateGuardWithData::~UndoLiveUpdateGuardWithData() 124 { 125 if ( !isActionPosted() ) 126 rollback(); 127 } 128 129 //----------------------------------------------------------------------------- 130 131 UndoGuardWithSelection::UndoGuardWithSelection( 132 const OUString& i_undoString, const uno::Reference< document::XUndoManager >& i_undoManager ) 133 :UndoGuard( i_undoString, i_undoManager, E_MODEL_WITH_SELECTION ) 134 { 135 } 136 137 //----------------------------------------------------------------------------- 138 139 UndoGuardWithSelection::~UndoGuardWithSelection() 140 { 141 if ( !isActionPosted() ) 142 rollback(); 143 } 144 145 //----------------------------------------------------------------------------- 146 147 UndoContext::UndoContext( const Reference< document::XUndoManager > & i_undoManager, const ::rtl::OUString& i_undoTitle ) 148 :m_xUndoManager( i_undoManager ) 149 { 150 ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" ); 151 m_xUndoManager->enterUndoContext( i_undoTitle ); 152 } 153 154 //----------------------------------------------------------------------------- 155 156 UndoContext::~UndoContext() 157 { 158 m_xUndoManager->leaveUndoContext(); 159 } 160 161 //----------------------------------------------------------------------------- 162 163 HiddenUndoContext::HiddenUndoContext( const Reference< document::XUndoManager > & i_undoManager ) 164 :m_xUndoManager( i_undoManager ) 165 { 166 ENSURE_OR_THROW( m_xUndoManager.is(), "invalid undo manager!" ); 167 try 168 { 169 m_xUndoManager->enterHiddenUndoContext(); 170 } 171 catch( const uno::Exception& ) 172 { 173 DBG_UNHANDLED_EXCEPTION(); 174 m_xUndoManager.clear(); 175 // prevents the leaveUndoContext in the dtor 176 } 177 } 178 179 //----------------------------------------------------------------------------- 180 181 HiddenUndoContext::~HiddenUndoContext() 182 { 183 try 184 { 185 if ( m_xUndoManager.is() ) 186 m_xUndoManager->leaveUndoContext(); 187 } 188 catch( const uno::Exception& ) 189 { 190 DBG_UNHANDLED_EXCEPTION(); 191 } 192 } 193 194 } // namespace chart 195