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: TableUndo.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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBAUI_TABLEUNDO_HXX
34 #include "TableUndo.hxx"
37 #include "dbu_tbl.hrc"
39 #ifndef DBAUI_TABLEEDITORCONTROL_HXX
40 #include "TEditControl.hxx"
42 #ifndef DBAUI_TABLEROW_HXX
43 #include "TableRow.hxx"
45 #ifndef DBACCESS_UI_BROWSER_ID_HXX
46 #include "browserids.hxx"
48 #ifndef DBUI_TABLECONTROLLER_HXX
49 #include "TableController.hxx"
51 #ifndef DBAUI_TABLEDESIGNVIEW_HXX
52 #include "TableDesignView.hxx"
54 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
55 #include "FieldDescriptions.hxx"
57 #ifndef _TOOLS_DEBUG_HXX
58 #include <tools/debug.hxx>
61 using namespace dbaui
;
62 using namespace ::svt
;
64 TYPEINIT1( OCommentUndoAction
, SfxUndoAction
);
65 TYPEINIT1( OTableDesignUndoAct
, OCommentUndoAction
);
66 TYPEINIT1( OTableEditorUndoAct
, OTableDesignUndoAct
);
67 TYPEINIT1( OTableDesignCellUndoAct
, OTableDesignUndoAct
);
68 TYPEINIT1( OTableEditorTypeSelUndoAct
, OTableEditorUndoAct
);
69 TYPEINIT1( OTableEditorDelUndoAct
, OTableEditorUndoAct
);
70 TYPEINIT1( OTableEditorInsUndoAct
, OTableEditorUndoAct
);
71 TYPEINIT1( OTableEditorInsNewUndoAct
, OTableEditorUndoAct
);
72 TYPEINIT1( OPrimKeyUndoAct
, OTableEditorUndoAct
);
74 //==============================================================================
75 // class OTableDesignUndoAct
76 //==============================================================================
77 DBG_NAME(OTableDesignUndoAct
);
78 OTableDesignUndoAct::OTableDesignUndoAct( OTableRowView
* pOwner
,USHORT nCommentID
) : OCommentUndoAction(nCommentID
)
79 ,m_pTabDgnCtrl( pOwner
)
81 DBG_CTOR(OTableDesignUndoAct
,NULL
);
82 m_pTabDgnCtrl
->m_nCurUndoActId
++;
85 //-------------------------------------------------------------------------
86 OTableDesignUndoAct::~OTableDesignUndoAct()
88 DBG_DTOR(OTableDesignUndoAct
,NULL
);
91 //-------------------------------------------------------------------------
92 void OTableDesignUndoAct::Undo()
94 m_pTabDgnCtrl
->m_nCurUndoActId
--;
96 //////////////////////////////////////////////////////////////////////
97 // Wenn erstes Undo zurueckgenommen wurde, ist Doc nicht modifiziert worden
98 if( m_pTabDgnCtrl
->m_nCurUndoActId
== 0 )
100 m_pTabDgnCtrl
->GetView()->getController().setModified(sal_False
);
101 m_pTabDgnCtrl
->GetView()->getController().InvalidateFeature(SID_SAVEDOC
);
105 //-------------------------------------------------------------------------
106 void OTableDesignUndoAct::Redo()
108 m_pTabDgnCtrl
->m_nCurUndoActId
++;
110 //////////////////////////////////////////////////////////////////////
111 // Wenn Redo fuer erste Undo-Action, muss Modified-Flag wieder gesetzt werden
112 if( m_pTabDgnCtrl
->m_nCurUndoActId
> 0 )
114 m_pTabDgnCtrl
->GetView()->getController().setModified(sal_True
);
115 m_pTabDgnCtrl
->GetView()->getController().InvalidateFeature(SID_SAVEDOC
);
118 //==============================================================================
119 // class OTableDesignCellUndoAct
120 //==============================================================================
121 DBG_NAME(OTableDesignCellUndoAct
);
122 OTableDesignCellUndoAct::OTableDesignCellUndoAct( OTableRowView
* pOwner
, long nRowID
, USHORT nColumn
) :
123 OTableDesignUndoAct( pOwner
,STR_TABED_UNDO_CELLMODIFIED
)
127 DBG_CTOR(OTableDesignCellUndoAct
,NULL
);
128 //////////////////////////////////////////////////////////////////////
129 // Text an der Position (m_nRow, m_nCol) auslesen
130 m_sOldText
= m_pTabDgnCtrl
->GetCellData( m_nRow
, m_nCol
);
133 //-------------------------------------------------------------------------
134 OTableDesignCellUndoAct::~OTableDesignCellUndoAct()
136 DBG_DTOR(OTableDesignCellUndoAct
,NULL
);
139 //-------------------------------------------------------------------------
140 void OTableDesignCellUndoAct::Undo()
142 //////////////////////////////////////////////////////////////////////
143 // Neuen Text der alten Zelle speichern und alten wieder einsetzen
144 m_pTabDgnCtrl
->ActivateCell( m_nRow
, m_nCol
);
145 m_sNewText
= m_pTabDgnCtrl
->GetCellData( m_nRow
, m_nCol
);
146 m_pTabDgnCtrl
->SetCellData( m_nRow
, m_nCol
, m_sOldText
);
147 //////////////////////////////////////////////////////////////////////
148 // Wenn erstes Undo zurueckgenommen wurde, ist Zelle nicht mehr modifiziert
149 if (m_pTabDgnCtrl
->GetCurUndoActId() == 1)
151 CellControllerRef xController
= m_pTabDgnCtrl
->Controller();
152 if ( xController
.Is() )
153 xController
->ClearModified();
154 m_pTabDgnCtrl
->GetView()->getController().setModified(sal_False
);
158 OTableDesignUndoAct::Undo();
161 //-------------------------------------------------------------------------
162 void OTableDesignCellUndoAct::Redo()
164 //////////////////////////////////////////////////////////////////////
165 // Neuen Text wieder einseten
166 m_pTabDgnCtrl
->ActivateCell( m_nRow
, m_nCol
);
167 m_pTabDgnCtrl
->SetCellData( m_nRow
, m_nCol
, m_sNewText
);
169 OTableDesignUndoAct::Redo();
172 //==============================================================================
173 // class OTableEditorUndoAct
174 //==============================================================================
175 DBG_NAME(OTableEditorUndoAct
);
176 OTableEditorUndoAct::OTableEditorUndoAct( OTableEditorCtrl
* pOwner
,USHORT _nCommentID
) :
177 OTableDesignUndoAct( pOwner
,_nCommentID
)
180 DBG_CTOR(OTableEditorUndoAct
,NULL
);
183 //-------------------------------------------------------------------------
184 OTableEditorUndoAct::~OTableEditorUndoAct()
186 DBG_DTOR(OTableEditorUndoAct
,NULL
);
189 //==============================================================================
190 // class OTableEditorTypeSelUndoAct
191 //==============================================================================
192 DBG_NAME(OTableEditorTypeSelUndoAct
);
193 OTableEditorTypeSelUndoAct::OTableEditorTypeSelUndoAct( OTableEditorCtrl
* pOwner
, long nRowID
, USHORT nColumn
, const TOTypeInfoSP
& _pOldType
)
194 :OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_TYPE_CHANGED
)
197 ,m_pOldType( _pOldType
)
199 DBG_CTOR(OTableEditorTypeSelUndoAct
,NULL
);
202 //-------------------------------------------------------------------------
203 OTableEditorTypeSelUndoAct::~OTableEditorTypeSelUndoAct()
205 DBG_DTOR(OTableEditorTypeSelUndoAct
,NULL
);
208 //-------------------------------------------------------------------------
209 void OTableEditorTypeSelUndoAct::Undo()
211 //////////////////////////////////////////////////////////////////////
213 OFieldDescription
* pFieldDesc
= pTabEdCtrl
->GetFieldDescr(m_nRow
);
215 m_pNewType
= pFieldDesc
->getTypeInfo();
217 m_pNewType
= TOTypeInfoSP();
218 pTabEdCtrl
->SetCellData(m_nRow
,m_nCol
,m_pOldType
);
219 pTabEdCtrl
->SwitchType( m_pOldType
);
221 OTableEditorUndoAct::Undo();
224 //-------------------------------------------------------------------------
225 void OTableEditorTypeSelUndoAct::Redo()
227 //////////////////////////////////////////////////////////////////////
229 pTabEdCtrl
->GoToRowColumnId( m_nRow
,m_nCol
);
230 pTabEdCtrl
->SetCellData(m_nRow
,m_nCol
,m_pNewType
);
232 OTableEditorUndoAct::Redo();
235 //==============================================================================
236 // class OTableEditorDelUndoAct
237 //==============================================================================
238 DBG_NAME(OTableEditorDelUndoAct
);
239 OTableEditorDelUndoAct::OTableEditorDelUndoAct( OTableEditorCtrl
* pOwner
) :
240 OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_ROWDELETED
)
242 DBG_CTOR(OTableEditorDelUndoAct
,NULL
);
243 //////////////////////////////////////////////////////////////////////
244 // DeletedRowList fuellen
245 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pOwner
->GetRowList();
246 long nIndex
= pOwner
->FirstSelectedRow();
247 ::boost::shared_ptr
<OTableRow
> pOriginalRow
;
248 ::boost::shared_ptr
<OTableRow
> pNewRow
;
252 pOriginalRow
= (*pOriginalRows
)[nIndex
];
253 pNewRow
.reset(new OTableRow( *pOriginalRow
, nIndex
));
254 m_aDeletedRows
.push_back( pNewRow
);
256 nIndex
= pOwner
->NextSelectedRow();
260 //-------------------------------------------------------------------------
261 OTableEditorDelUndoAct::~OTableEditorDelUndoAct()
263 DBG_DTOR(OTableEditorDelUndoAct
,NULL
);
264 m_aDeletedRows
.clear();
267 //-------------------------------------------------------------------------
268 void OTableEditorDelUndoAct::Undo()
270 //////////////////////////////////////////////////////////////////////
271 // Geloeschte Zeilen wieder einfuegen
273 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aIter
= m_aDeletedRows
.begin();
274 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aEnd
= m_aDeletedRows
.end();
276 ::boost::shared_ptr
<OTableRow
> pNewOrigRow
;
277 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
279 for(;aIter
!= aEnd
;++aIter
)
281 pNewOrigRow
.reset(new OTableRow( **aIter
));
282 nPos
= (*aIter
)->GetPos();
283 pOriginalRows
->insert( pOriginalRows
->begin()+nPos
,pNewOrigRow
);
286 pTabEdCtrl
->DisplayData(pTabEdCtrl
->GetCurRow());
287 pTabEdCtrl
->Invalidate();
288 OTableEditorUndoAct::Undo();
291 //-------------------------------------------------------------------------
292 void OTableEditorDelUndoAct::Redo()
294 //////////////////////////////////////////////////////////////////////
295 // Zeilen wieder loeschen
297 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aIter
= m_aDeletedRows
.begin();
298 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aEnd
= m_aDeletedRows
.end();
299 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
301 for(;aIter
!= aEnd
;++aIter
)
303 nPos
= (*aIter
)->GetPos();
304 pOriginalRows
->erase( pOriginalRows
->begin()+nPos
);
307 pTabEdCtrl
->DisplayData(pTabEdCtrl
->GetCurRow());
308 pTabEdCtrl
->Invalidate();
309 OTableEditorUndoAct::Redo();
312 //-------------------------------------------------------------------------
313 //==============================================================================
314 // class OTableEditorInsUndoAct
315 //==============================================================================
316 DBG_NAME(OTableEditorInsUndoAct
);
317 OTableEditorInsUndoAct::OTableEditorInsUndoAct( OTableEditorCtrl
* pOwner
,
318 long nInsertPosition
,
319 const ::std::vector
< ::boost::shared_ptr
<OTableRow
> >& _vInsertedRows
)
320 :OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_ROWINSERTED
)
321 ,m_vInsertedRows(_vInsertedRows
)
322 ,m_nInsPos( nInsertPosition
)
324 DBG_CTOR(OTableEditorInsUndoAct
,NULL
);
327 //-------------------------------------------------------------------------
328 OTableEditorInsUndoAct::~OTableEditorInsUndoAct()
330 DBG_DTOR(OTableEditorInsUndoAct
,NULL
);
331 m_vInsertedRows
.clear();
334 //-------------------------------------------------------------------------
335 void OTableEditorInsUndoAct::Undo()
337 //////////////////////////////////////////////////////////////////////
338 // Eingefuegte Zeilen wieder loeschen
339 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
340 for( long i
=(m_nInsPos
+m_vInsertedRows
.size()-1); i
>(m_nInsPos
-1); i
-- )
342 pOriginalRows
->erase(pOriginalRows
->begin()+i
);
345 pTabEdCtrl
->RowRemoved( m_nInsPos
, m_vInsertedRows
.size(), TRUE
);
346 pTabEdCtrl
->InvalidateHandleColumn();
348 OTableEditorUndoAct::Undo();
351 //-------------------------------------------------------------------------
352 void OTableEditorInsUndoAct::Redo()
354 //////////////////////////////////////////////////////////////////////
355 // Zeilen wieder einfuegen
356 long nInsertRow
= m_nInsPos
;
357 ::boost::shared_ptr
<OTableRow
> pRow
;
358 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aIter
= m_vInsertedRows
.begin();
359 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >::iterator aEnd
= m_vInsertedRows
.end();
360 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
361 for(;aIter
!= aEnd
;++aIter
)
363 pRow
.reset(new OTableRow( **aIter
));
364 pRowList
->insert( pRowList
->begin()+nInsertRow
,pRow
);
368 pTabEdCtrl
->RowInserted( m_nInsPos
, m_vInsertedRows
.size(), TRUE
);
369 pTabEdCtrl
->InvalidateHandleColumn();
371 OTableEditorUndoAct::Redo();
374 //==============================================================================
375 // class OTableEditorInsNewUndoAct
376 //==============================================================================
377 DBG_NAME(OTableEditorInsNewUndoAct
);
378 OTableEditorInsNewUndoAct::OTableEditorInsNewUndoAct( OTableEditorCtrl
* pOwner
, long nInsertPosition
, long nInsertedRows
) :
379 OTableEditorUndoAct( pOwner
,STR_TABED_UNDO_NEWROWINSERTED
)
380 ,m_nInsPos( nInsertPosition
)
381 ,m_nInsRows( nInsertedRows
)
383 DBG_CTOR(OTableEditorInsNewUndoAct
,NULL
);
386 //-------------------------------------------------------------------------
387 OTableEditorInsNewUndoAct::~OTableEditorInsNewUndoAct()
389 DBG_DTOR(OTableEditorInsNewUndoAct
,NULL
);
392 //-------------------------------------------------------------------------
393 void OTableEditorInsNewUndoAct::Undo()
395 //////////////////////////////////////////////////////////////////////
396 // Eingefuegte Zeilen wieder loeschen
397 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pOriginalRows
= pTabEdCtrl
->GetRowList();
399 for( long i
=(m_nInsPos
+m_nInsRows
-1); i
>(m_nInsPos
-1); i
-- )
401 pOriginalRows
->erase(pOriginalRows
->begin()+i
);
404 pTabEdCtrl
->RowRemoved( m_nInsPos
, m_nInsRows
, TRUE
);
405 pTabEdCtrl
->InvalidateHandleColumn();
407 OTableEditorUndoAct::Undo();
410 //-------------------------------------------------------------------------
411 void OTableEditorInsNewUndoAct::Redo()
413 //////////////////////////////////////////////////////////////////////
414 // Zeilen wieder einfuegen
415 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
417 for( long i
=m_nInsPos
; i
<(m_nInsPos
+m_nInsRows
); i
++ )
418 pRowList
->insert( pRowList
->begin()+i
,::boost::shared_ptr
<OTableRow
>(new OTableRow()) );
420 pTabEdCtrl
->RowInserted( m_nInsPos
, m_nInsRows
, TRUE
);
421 pTabEdCtrl
->InvalidateHandleColumn();
423 OTableEditorUndoAct::Redo();
426 //-------------------------------------------------------------------------
427 //========================================================================
428 // class OPrimKeyUndoAct
429 //========================================================================
430 DBG_NAME(OPrimKeyUndoAct
);
431 //-------------------------------------------------------------------------
432 OPrimKeyUndoAct::OPrimKeyUndoAct( OTableEditorCtrl
* pOwner
, MultiSelection aDeletedKeys
, MultiSelection aInsertedKeys
) :
433 OTableEditorUndoAct( pOwner
,STR_TABLEDESIGN_UNDO_PRIMKEY
)
434 ,m_aDelKeys( aDeletedKeys
)
435 ,m_aInsKeys( aInsertedKeys
)
436 ,m_pEditorCtrl( pOwner
)
438 DBG_CTOR(OPrimKeyUndoAct
,NULL
);
441 //-------------------------------------------------------------------------
442 OPrimKeyUndoAct::~OPrimKeyUndoAct()
444 DBG_DTOR(OPrimKeyUndoAct
,NULL
);
447 //-------------------------------------------------------------------------
448 void OPrimKeyUndoAct::Undo()
450 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
451 ::boost::shared_ptr
<OTableRow
> pRow
;
454 //////////////////////////////////////////////////////////////////////
455 // Die eingefuegten Keys loeschen
456 for( nIndex
= m_aInsKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aInsKeys
.NextSelected() )
458 OSL_ENSURE(nIndex
<= static_cast<long>(pRowList
->size()),"Index for undo isn't valid!");
459 pRow
= (*pRowList
)[nIndex
];
460 pRow
->SetPrimaryKey( FALSE
);
463 //////////////////////////////////////////////////////////////////////
464 // Die geloeschten Keys herstellen
465 for( nIndex
= m_aDelKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aDelKeys
.NextSelected() )
467 OSL_ENSURE(nIndex
<= static_cast<long>(pRowList
->size()),"Index for undo isn't valid!");
468 pRow
= (*pRowList
)[nIndex
];
469 pRow
->SetPrimaryKey( TRUE
);
472 m_pEditorCtrl
->InvalidateHandleColumn();
473 OTableEditorUndoAct::Undo();
476 //-------------------------------------------------------------------------
477 void OPrimKeyUndoAct::Redo()
479 ::std::vector
< ::boost::shared_ptr
<OTableRow
> >* pRowList
= pTabEdCtrl
->GetRowList();
482 //////////////////////////////////////////////////////////////////////
483 // Die geloeschten Keys loeschen
484 for( nIndex
= m_aDelKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aDelKeys
.NextSelected() )
485 (*pRowList
)[nIndex
]->SetPrimaryKey( FALSE
);
487 //////////////////////////////////////////////////////////////////////
488 // Die eingefuegten Keys herstellen
489 for( nIndex
= m_aInsKeys
.FirstSelected(); nIndex
!= (long)SFX_ENDOFSELECTION
; nIndex
=m_aInsKeys
.NextSelected() )
490 (*pRowList
)[nIndex
]->SetPrimaryKey( TRUE
);
492 m_pEditorCtrl
->InvalidateHandleColumn();
493 OTableEditorUndoAct::Redo();