RowCollectionEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright © 2005, Mathew Hall
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  21. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  24. * OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.ComponentModel;
  28. using System.ComponentModel.Design;
  29. using XPTable.Events;
  30. using XPTable.Models;
  31. namespace XPTable.Models.Design
  32. {
  33. /// <summary>
  34. /// Provides a user interface that can edit collections of Rows
  35. /// at design time
  36. /// </summary>
  37. public class RowCollectionEditor : HelpfulCollectionEditor
  38. {
  39. /// <summary>
  40. /// The RowCollection being edited
  41. /// </summary>
  42. private RowCollection rows;
  43. /// <summary>
  44. /// Initializes a new instance of the RowCollectionEditor class
  45. /// using the specified collection type
  46. /// </summary>
  47. /// <param name="type">The type of the collection for this editor to edit</param>
  48. public RowCollectionEditor(Type type) : base(type)
  49. {
  50. this.rows = null;
  51. }
  52. /// <summary>
  53. /// Edits the value of the specified object using the specified
  54. /// service provider and context
  55. /// </summary>
  56. /// <param name="context">An ITypeDescriptorContext that can be
  57. /// used to gain additional context information</param>
  58. /// <param name="isp">A service provider object through which
  59. /// editing services can be obtained</param>
  60. /// <param name="value">The object to edit the value of</param>
  61. /// <returns>The new value of the object. If the value of the
  62. /// object has not changed, this should return the same object
  63. /// it was passed</returns>
  64. public override object EditValue(ITypeDescriptorContext context, IServiceProvider isp, object value)
  65. {
  66. this.rows = (RowCollection) value;
  67. object returnObject = base.EditValue(context, isp, value);
  68. TableModel model = (TableModel) context.Instance;
  69. // make sure the TableModel's Table redraws any additions/deletions
  70. if (model.Table != null)
  71. {
  72. model.Table.PerformLayout();
  73. model.Table.Refresh();
  74. }
  75. return returnObject;
  76. }
  77. /// <summary>
  78. /// Creates a new instance of the specified collection item type
  79. /// </summary>
  80. /// <param name="itemType">The type of item to create</param>
  81. /// <returns>A new instance of the specified object</returns>
  82. protected override object CreateInstance(Type itemType)
  83. {
  84. Row row = (Row) base.CreateInstance(itemType);
  85. // newly created items aren't added to the collection
  86. // until editing has finished. we'd like the newly
  87. // created row to show up in the table immediately
  88. // so we'll add it to the RowCollection now
  89. this.rows.Add(row);
  90. return row;
  91. }
  92. /// <summary>
  93. /// Destroys the specified instance of the object
  94. /// </summary>
  95. /// <param name="instance">The object to destroy</param>
  96. protected override void DestroyInstance(object instance)
  97. {
  98. if (instance != null && instance is Row)
  99. {
  100. Row row = (Row) instance;
  101. // the specified row is about to be destroyed so
  102. // we need to remove it from the RowCollection first
  103. this.rows.Remove(row);
  104. }
  105. base.DestroyInstance(instance);
  106. }
  107. }
  108. }