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