CommandDelete.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace DrawTools
  5. {
  6. /// <summary>
  7. /// Delete command
  8. /// </summary>
  9. class CommandDelete : Command
  10. {
  11. List<DrawObject> cloneList; // contains selected items which are deleted
  12. // Create this command BEFORE applying Delete All function.
  13. public CommandDelete(GraphicsList graphicsList)
  14. {
  15. cloneList = new List<DrawObject>();
  16. // Make clone of the list selection.
  17. foreach(DrawObject o in graphicsList.Selection)
  18. {
  19. cloneList.Add(o.Clone());
  20. }
  21. }
  22. public override void Undo(GraphicsList list)
  23. {
  24. list.UnselectAll();
  25. // Add all objects from cloneList to list.
  26. foreach(DrawObject o in cloneList)
  27. {
  28. list.Add(o);
  29. }
  30. }
  31. public override void Redo(GraphicsList list)
  32. {
  33. // Delete from list all objects kept in cloneList
  34. int n = list.Count;
  35. for ( int i = n - 1; i >= 0; i-- )
  36. {
  37. bool toDelete = false;
  38. DrawObject objectToDelete = list[i];
  39. foreach(DrawObject o in cloneList)
  40. {
  41. if ( objectToDelete.ID == o.ID )
  42. {
  43. toDelete = true;
  44. break;
  45. }
  46. }
  47. if ( toDelete )
  48. {
  49. list.RemoveAt(i);
  50. }
  51. }
  52. }
  53. }
  54. }