CellCollectionEditor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Cells
  35. /// at design time
  36. /// </summary>
  37. public class CellCollectionEditor : HelpfulCollectionEditor
  38. {
  39. /// <summary>
  40. /// The CellCollection being edited
  41. /// </summary>
  42. private CellCollection cells;
  43. /// <summary>
  44. /// Initializes a new instance of the CellCollectionEditor 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 CellCollectionEditor(Type type) : base(type)
  49. {
  50. this.cells = 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.cells = (CellCollection) value;
  67. object returnObject = base.EditValue(context, isp, value);
  68. Row row = (Row) context.Instance;
  69. if (row.TableModel != null && row.TableModel.Table != null)
  70. {
  71. row.TableModel.Table.PerformLayout();
  72. row.TableModel.Table.Refresh();
  73. }
  74. return returnObject;
  75. }
  76. /// <summary>
  77. /// Creates a new instance of the specified collection item type
  78. /// </summary>
  79. /// <param name="itemType">The type of item to create</param>
  80. /// <returns>A new instance of the specified object</returns>
  81. protected override object CreateInstance(Type itemType)
  82. {
  83. Cell cell = (Cell) base.CreateInstance(itemType);
  84. // newly created items aren't added to the collection
  85. // until editing has finished. we'd like the newly
  86. // created cell to show up in the table immediately
  87. // so we'll add it to the CellCollection now
  88. this.cells.Add(cell);
  89. return cell;
  90. }
  91. /// <summary>
  92. /// Destroys the specified instance of the object
  93. /// </summary>
  94. /// <param name="instance">The object to destroy</param>
  95. protected override void DestroyInstance(object instance)
  96. {
  97. if (instance != null && instance is Cell)
  98. {
  99. Cell cell = (Cell) instance;
  100. // the specified cell is about to be destroyed so
  101. // we need to remove it from the CellCollection first
  102. this.cells.Remove(cell);
  103. }
  104. base.DestroyInstance(instance);
  105. }
  106. }
  107. }