xref: /trunk/main/dbaccess/source/ui/tabledesign/TableUndo.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_TABLEUNDO_HXX
31 #include "TableUndo.hxx"
32 #endif
33 #ifndef _DBU_TBL_HRC_
34 #include "dbu_tbl.hrc"
35 #endif
36 #ifndef DBAUI_TABLEEDITORCONTROL_HXX
37 #include "TEditControl.hxx"
38 #endif
39 #ifndef DBAUI_TABLEROW_HXX
40 #include "TableRow.hxx"
41 #endif
42 #ifndef DBACCESS_UI_BROWSER_ID_HXX
43 #include "browserids.hxx"
44 #endif
45 #ifndef DBUI_TABLECONTROLLER_HXX
46 #include "TableController.hxx"
47 #endif
48 #ifndef DBAUI_TABLEDESIGNVIEW_HXX
49 #include "TableDesignView.hxx"
50 #endif
51 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
52 #include "FieldDescriptions.hxx"
53 #endif
54 #ifndef _TOOLS_DEBUG_HXX
55 #include <tools/debug.hxx>
56 #endif
57 
58 using namespace dbaui;
59 using namespace ::svt;
60 
61 TYPEINIT1( OCommentUndoAction,          SfxUndoAction );
62 TYPEINIT1( OTableDesignUndoAct,         OCommentUndoAction );
63 TYPEINIT1( OTableEditorUndoAct,         OTableDesignUndoAct );
64 TYPEINIT1( OTableDesignCellUndoAct,     OTableDesignUndoAct );
65 TYPEINIT1( OTableEditorTypeSelUndoAct,  OTableEditorUndoAct );
66 TYPEINIT1( OTableEditorDelUndoAct,      OTableEditorUndoAct );
67 TYPEINIT1( OTableEditorInsUndoAct,      OTableEditorUndoAct );
68 TYPEINIT1( OTableEditorInsNewUndoAct,   OTableEditorUndoAct );
69 TYPEINIT1( OPrimKeyUndoAct,             OTableEditorUndoAct );
70 
71 //==============================================================================
72 // class OTableDesignUndoAct
73 //==============================================================================
74 DBG_NAME(OTableDesignUndoAct);
75 OTableDesignUndoAct::OTableDesignUndoAct( OTableRowView* pOwner,sal_uInt16 nCommentID ) : OCommentUndoAction(nCommentID)
76     ,m_pTabDgnCtrl(  pOwner )
77 {
78     DBG_CTOR(OTableDesignUndoAct,NULL);
79     m_pTabDgnCtrl->m_nCurUndoActId++;
80 }
81 
82 //-------------------------------------------------------------------------
83 OTableDesignUndoAct::~OTableDesignUndoAct()
84 {
85     DBG_DTOR(OTableDesignUndoAct,NULL);
86 }
87 
88 //-------------------------------------------------------------------------
89 void OTableDesignUndoAct::Undo()
90 {
91     m_pTabDgnCtrl->m_nCurUndoActId--;
92 
93     //////////////////////////////////////////////////////////////////////
94     // Wenn erstes Undo zurueckgenommen wurde, ist Doc nicht modifiziert worden
95     if( m_pTabDgnCtrl->m_nCurUndoActId == 0 )
96     {
97         m_pTabDgnCtrl->GetView()->getController().setModified(sal_False);
98         m_pTabDgnCtrl->GetView()->getController().InvalidateFeature(SID_SAVEDOC);
99     }
100 }
101 
102 //-------------------------------------------------------------------------
103 void OTableDesignUndoAct::Redo()
104 {
105     m_pTabDgnCtrl->m_nCurUndoActId++;
106 
107     //////////////////////////////////////////////////////////////////////
108     // Wenn Redo fuer erste Undo-Action, muss Modified-Flag wieder gesetzt werden
109     if( m_pTabDgnCtrl->m_nCurUndoActId > 0 )
110     {
111         m_pTabDgnCtrl->GetView()->getController().setModified(sal_True);
112         m_pTabDgnCtrl->GetView()->getController().InvalidateFeature(SID_SAVEDOC);
113     }
114 }
115 //==============================================================================
116 // class OTableDesignCellUndoAct
117 //==============================================================================
118 DBG_NAME(OTableDesignCellUndoAct);
119 OTableDesignCellUndoAct::OTableDesignCellUndoAct( OTableRowView* pOwner, long nRowID, sal_uInt16 nColumn ) :
120      OTableDesignUndoAct( pOwner ,STR_TABED_UNDO_CELLMODIFIED)
121     ,m_nCol( nColumn )
122     ,m_nRow( nRowID )
123 {
124     DBG_CTOR(OTableDesignCellUndoAct,NULL);
125     //////////////////////////////////////////////////////////////////////
126     // Text an der Position (m_nRow, m_nCol) auslesen
127     m_sOldText = m_pTabDgnCtrl->GetCellData( m_nRow, m_nCol );
128 }
129 
130 //-------------------------------------------------------------------------
131 OTableDesignCellUndoAct::~OTableDesignCellUndoAct()
132 {
133     DBG_DTOR(OTableDesignCellUndoAct,NULL);
134 }
135 
136 //-------------------------------------------------------------------------
137 void OTableDesignCellUndoAct::Undo()
138 {
139     //////////////////////////////////////////////////////////////////////
140     // Neuen Text der alten Zelle speichern und alten wieder einsetzen
141     m_pTabDgnCtrl->ActivateCell( m_nRow, m_nCol );
142     m_sNewText = m_pTabDgnCtrl->GetCellData( m_nRow, m_nCol );
143     m_pTabDgnCtrl->SetCellData( m_nRow, m_nCol, m_sOldText );
144     //////////////////////////////////////////////////////////////////////
145     // Wenn erstes Undo zurueckgenommen wurde, ist Zelle nicht mehr modifiziert
146     if (m_pTabDgnCtrl->GetCurUndoActId() == 1)
147     {
148         CellControllerRef xController = m_pTabDgnCtrl->Controller();
149         if ( xController.Is() )
150             xController->ClearModified();
151         m_pTabDgnCtrl->GetView()->getController().setModified(sal_False);
152 
153     }
154 
155     OTableDesignUndoAct::Undo();
156 }
157 
158 //-------------------------------------------------------------------------
159 void OTableDesignCellUndoAct::Redo()
160 {
161     //////////////////////////////////////////////////////////////////////
162     // Neuen Text wieder einseten
163     m_pTabDgnCtrl->ActivateCell( m_nRow, m_nCol );
164     m_pTabDgnCtrl->SetCellData( m_nRow, m_nCol, m_sNewText );
165 
166     OTableDesignUndoAct::Redo();
167 }
168 
169 //==============================================================================
170 // class OTableEditorUndoAct
171 //==============================================================================
172 DBG_NAME(OTableEditorUndoAct);
173 OTableEditorUndoAct::OTableEditorUndoAct( OTableEditorCtrl* pOwner,sal_uInt16 _nCommentID ) :
174      OTableDesignUndoAct(  pOwner ,_nCommentID)
175     ,pTabEdCtrl(pOwner)
176 {
177     DBG_CTOR(OTableEditorUndoAct,NULL);
178 }
179 
180 //-------------------------------------------------------------------------
181 OTableEditorUndoAct::~OTableEditorUndoAct()
182 {
183     DBG_DTOR(OTableEditorUndoAct,NULL);
184 }
185 
186 //==============================================================================
187 // class OTableEditorTypeSelUndoAct
188 //==============================================================================
189 DBG_NAME(OTableEditorTypeSelUndoAct);
190 OTableEditorTypeSelUndoAct::OTableEditorTypeSelUndoAct( OTableEditorCtrl* pOwner, long nRowID, sal_uInt16 nColumn, const TOTypeInfoSP& _pOldType )
191     :OTableEditorUndoAct( pOwner ,STR_TABED_UNDO_TYPE_CHANGED)
192     ,m_nCol( nColumn )
193     ,m_nRow( nRowID )
194     ,m_pOldType( _pOldType )
195 {
196     DBG_CTOR(OTableEditorTypeSelUndoAct,NULL);
197 }
198 
199 //-------------------------------------------------------------------------
200 OTableEditorTypeSelUndoAct::~OTableEditorTypeSelUndoAct()
201 {
202     DBG_DTOR(OTableEditorTypeSelUndoAct,NULL);
203 }
204 
205 //-------------------------------------------------------------------------
206 void OTableEditorTypeSelUndoAct::Undo()
207 {
208     //////////////////////////////////////////////////////////////////////
209     // Typ zuruecksetzen
210     OFieldDescription* pFieldDesc = pTabEdCtrl->GetFieldDescr(m_nRow);
211     if(pFieldDesc)
212         m_pNewType = pFieldDesc->getTypeInfo();
213     else
214         m_pNewType = TOTypeInfoSP();
215     pTabEdCtrl->SetCellData(m_nRow,m_nCol,m_pOldType);
216     pTabEdCtrl->SwitchType( m_pOldType );
217 
218     OTableEditorUndoAct::Undo();
219 }
220 
221 //-------------------------------------------------------------------------
222 void OTableEditorTypeSelUndoAct::Redo()
223 {
224     //////////////////////////////////////////////////////////////////////
225     // Neuen Typ
226     pTabEdCtrl->GoToRowColumnId( m_nRow ,m_nCol);
227     pTabEdCtrl->SetCellData(m_nRow,m_nCol,m_pNewType);
228 
229     OTableEditorUndoAct::Redo();
230 }
231 
232 //==============================================================================
233 // class OTableEditorDelUndoAct
234 //==============================================================================
235 DBG_NAME(OTableEditorDelUndoAct);
236 OTableEditorDelUndoAct::OTableEditorDelUndoAct( OTableEditorCtrl* pOwner) :
237      OTableEditorUndoAct( pOwner ,STR_TABED_UNDO_ROWDELETED)
238 {
239     DBG_CTOR(OTableEditorDelUndoAct,NULL);
240     //////////////////////////////////////////////////////////////////////
241     // DeletedRowList fuellen
242     ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pOwner->GetRowList();
243     long nIndex = pOwner->FirstSelectedRow();
244      ::boost::shared_ptr<OTableRow>  pOriginalRow;
245      ::boost::shared_ptr<OTableRow>  pNewRow;
246 
247     while( nIndex >= 0 )
248     {
249         pOriginalRow = (*pOriginalRows)[nIndex];
250         pNewRow.reset(new OTableRow( *pOriginalRow, nIndex ));
251         m_aDeletedRows.push_back( pNewRow);
252 
253         nIndex = pOwner->NextSelectedRow();
254     }
255 }
256 
257 //-------------------------------------------------------------------------
258 OTableEditorDelUndoAct::~OTableEditorDelUndoAct()
259 {
260     DBG_DTOR(OTableEditorDelUndoAct,NULL);
261     m_aDeletedRows.clear();
262 }
263 
264 //-------------------------------------------------------------------------
265 void OTableEditorDelUndoAct::Undo()
266 {
267     //////////////////////////////////////////////////////////////////////
268     // Geloeschte Zeilen wieder einfuegen
269     sal_uLong nPos;
270     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_aDeletedRows.begin();
271     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_aDeletedRows.end();
272 
273      ::boost::shared_ptr<OTableRow>  pNewOrigRow;
274     ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
275 
276     for(;aIter != aEnd;++aIter)
277     {
278         pNewOrigRow.reset(new OTableRow( **aIter ));
279         nPos = (*aIter)->GetPos();
280         pOriginalRows->insert( pOriginalRows->begin()+nPos,pNewOrigRow);
281     }
282 
283     pTabEdCtrl->DisplayData(pTabEdCtrl->GetCurRow());
284     pTabEdCtrl->Invalidate();
285     OTableEditorUndoAct::Undo();
286 }
287 
288 //-------------------------------------------------------------------------
289 void OTableEditorDelUndoAct::Redo()
290 {
291     //////////////////////////////////////////////////////////////////////
292     // Zeilen wieder loeschen
293     sal_uLong nPos;
294     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_aDeletedRows.begin();
295     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_aDeletedRows.end();
296     ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
297 
298     for(;aIter != aEnd;++aIter)
299     {
300         nPos = (*aIter)->GetPos();
301         pOriginalRows->erase( pOriginalRows->begin()+nPos );
302     }
303 
304     pTabEdCtrl->DisplayData(pTabEdCtrl->GetCurRow());
305     pTabEdCtrl->Invalidate();
306     OTableEditorUndoAct::Redo();
307 }
308 
309 //-------------------------------------------------------------------------
310 //==============================================================================
311 // class OTableEditorInsUndoAct
312 //==============================================================================
313 DBG_NAME(OTableEditorInsUndoAct);
314 OTableEditorInsUndoAct::OTableEditorInsUndoAct( OTableEditorCtrl* pOwner,
315                                                long nInsertPosition ,
316                                                const ::std::vector<  ::boost::shared_ptr<OTableRow> >& _vInsertedRows)
317     :OTableEditorUndoAct( pOwner,STR_TABED_UNDO_ROWINSERTED )
318     ,m_vInsertedRows(_vInsertedRows)
319     ,m_nInsPos( nInsertPosition )
320 {
321     DBG_CTOR(OTableEditorInsUndoAct,NULL);
322 }
323 
324 //-------------------------------------------------------------------------
325 OTableEditorInsUndoAct::~OTableEditorInsUndoAct()
326 {
327     DBG_DTOR(OTableEditorInsUndoAct,NULL);
328     m_vInsertedRows.clear();
329 }
330 
331 //-------------------------------------------------------------------------
332 void OTableEditorInsUndoAct::Undo()
333 {
334     //////////////////////////////////////////////////////////////////////
335     // Eingefuegte Zeilen wieder loeschen
336     ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
337     for( long i=(m_nInsPos+m_vInsertedRows.size()-1); i>(m_nInsPos-1); i-- )
338     {
339         pOriginalRows->erase(pOriginalRows->begin()+i);
340     }
341 
342     pTabEdCtrl->RowRemoved( m_nInsPos, m_vInsertedRows.size(), sal_True );
343     pTabEdCtrl->InvalidateHandleColumn();
344 
345     OTableEditorUndoAct::Undo();
346 }
347 
348 //-------------------------------------------------------------------------
349 void OTableEditorInsUndoAct::Redo()
350 {
351     //////////////////////////////////////////////////////////////////////
352     // Zeilen wieder einfuegen
353     long nInsertRow = m_nInsPos;
354      ::boost::shared_ptr<OTableRow>  pRow;
355     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aIter = m_vInsertedRows.begin();
356     ::std::vector< ::boost::shared_ptr<OTableRow> >::iterator aEnd = m_vInsertedRows.end();
357     ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
358     for(;aIter != aEnd;++aIter)
359     {
360         pRow.reset(new OTableRow( **aIter ));
361         pRowList->insert( pRowList->begin()+nInsertRow ,pRow );
362         nInsertRow++;
363     }
364 
365     pTabEdCtrl->RowInserted( m_nInsPos, m_vInsertedRows.size(), sal_True );
366     pTabEdCtrl->InvalidateHandleColumn();
367 
368     OTableEditorUndoAct::Redo();
369 }
370 
371 //==============================================================================
372 // class OTableEditorInsNewUndoAct
373 //==============================================================================
374 DBG_NAME(OTableEditorInsNewUndoAct);
375 OTableEditorInsNewUndoAct::OTableEditorInsNewUndoAct( OTableEditorCtrl* pOwner, long nInsertPosition, long nInsertedRows ) :
376      OTableEditorUndoAct( pOwner ,STR_TABED_UNDO_NEWROWINSERTED)
377     ,m_nInsPos( nInsertPosition )
378     ,m_nInsRows( nInsertedRows )
379 {
380     DBG_CTOR(OTableEditorInsNewUndoAct,NULL);
381 }
382 
383 //-------------------------------------------------------------------------
384 OTableEditorInsNewUndoAct::~OTableEditorInsNewUndoAct()
385 {
386     DBG_DTOR(OTableEditorInsNewUndoAct,NULL);
387 }
388 
389 //-------------------------------------------------------------------------
390 void OTableEditorInsNewUndoAct::Undo()
391 {
392     //////////////////////////////////////////////////////////////////////
393     // Eingefuegte Zeilen wieder loeschen
394     ::std::vector< ::boost::shared_ptr<OTableRow> >* pOriginalRows = pTabEdCtrl->GetRowList();
395 
396     for( long i=(m_nInsPos+m_nInsRows-1); i>(m_nInsPos-1); i-- )
397     {
398         pOriginalRows->erase(pOriginalRows->begin()+i);
399     }
400 
401     pTabEdCtrl->RowRemoved( m_nInsPos, m_nInsRows, sal_True );
402     pTabEdCtrl->InvalidateHandleColumn();
403 
404     OTableEditorUndoAct::Undo();
405 }
406 
407 //-------------------------------------------------------------------------
408 void OTableEditorInsNewUndoAct::Redo()
409 {
410     //////////////////////////////////////////////////////////////////////
411     // Zeilen wieder einfuegen
412     ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
413 
414     for( long i=m_nInsPos; i<(m_nInsPos+m_nInsRows); i++ )
415         pRowList->insert( pRowList->begin()+i,::boost::shared_ptr<OTableRow>(new OTableRow()) );
416 
417     pTabEdCtrl->RowInserted( m_nInsPos, m_nInsRows, sal_True );
418     pTabEdCtrl->InvalidateHandleColumn();
419 
420     OTableEditorUndoAct::Redo();
421 }
422 
423 //-------------------------------------------------------------------------
424 //========================================================================
425 // class OPrimKeyUndoAct
426 //========================================================================
427 DBG_NAME(OPrimKeyUndoAct);
428 //-------------------------------------------------------------------------
429 OPrimKeyUndoAct::OPrimKeyUndoAct( OTableEditorCtrl* pOwner, MultiSelection aDeletedKeys, MultiSelection aInsertedKeys) :
430      OTableEditorUndoAct( pOwner ,STR_TABLEDESIGN_UNDO_PRIMKEY)
431     ,m_aDelKeys( aDeletedKeys )
432     ,m_aInsKeys( aInsertedKeys )
433     ,m_pEditorCtrl( pOwner )
434 {
435     DBG_CTOR(OPrimKeyUndoAct,NULL);
436 }
437 
438 //-------------------------------------------------------------------------
439 OPrimKeyUndoAct::~OPrimKeyUndoAct()
440 {
441     DBG_DTOR(OPrimKeyUndoAct,NULL);
442 }
443 
444 //-------------------------------------------------------------------------
445 void OPrimKeyUndoAct::Undo()
446 {
447     ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
448     ::boost::shared_ptr<OTableRow>  pRow;
449     long nIndex;
450 
451     //////////////////////////////////////////////////////////////////////
452     // Die eingefuegten Keys loeschen
453     for( nIndex = m_aInsKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aInsKeys.NextSelected() )
454     {
455         OSL_ENSURE(nIndex <= static_cast<long>(pRowList->size()),"Index for undo isn't valid!");
456         pRow = (*pRowList)[nIndex];
457         pRow->SetPrimaryKey( sal_False );
458     }
459 
460     //////////////////////////////////////////////////////////////////////
461     // Die geloeschten Keys herstellen
462     for( nIndex = m_aDelKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aDelKeys.NextSelected() )
463     {
464         OSL_ENSURE(nIndex <= static_cast<long>(pRowList->size()),"Index for undo isn't valid!");
465         pRow = (*pRowList)[nIndex];
466         pRow->SetPrimaryKey( sal_True );
467     }
468 
469     m_pEditorCtrl->InvalidateHandleColumn();
470     OTableEditorUndoAct::Undo();
471 }
472 
473 //-------------------------------------------------------------------------
474 void OPrimKeyUndoAct::Redo()
475 {
476     ::std::vector< ::boost::shared_ptr<OTableRow> >* pRowList = pTabEdCtrl->GetRowList();
477     long nIndex;
478 
479     //////////////////////////////////////////////////////////////////////
480     // Die geloeschten Keys loeschen
481     for( nIndex = m_aDelKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aDelKeys.NextSelected() )
482         (*pRowList)[nIndex]->SetPrimaryKey( sal_False );
483 
484     //////////////////////////////////////////////////////////////////////
485     // Die eingefuegten Keys herstellen
486     for( nIndex = m_aInsKeys.FirstSelected(); nIndex != (long)SFX_ENDOFSELECTION; nIndex=m_aInsKeys.NextSelected() )
487         (*pRowList)[nIndex]->SetPrimaryKey( sal_True );
488 
489     m_pEditorCtrl->InvalidateHandleColumn();
490     OTableEditorUndoAct::Redo();
491 }
492 
493 
494 
495 
496