ColumnCollectionEditor.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 System.Drawing;
  30. using System.Windows.Forms;
  31. using XPTable.Events;
  32. using XPTable.Models;
  33. namespace XPTable.Models.Design
  34. {
  35. /// <summary>
  36. /// Provides a user interface that can edit collections of Columns
  37. /// at design time
  38. /// </summary>
  39. public class ColumnCollectionEditor : HelpfulCollectionEditor
  40. {
  41. #region Class Data
  42. /// <summary>
  43. /// The ColumnCollection being edited
  44. /// </summary>
  45. private ColumnCollection columns;
  46. /// <summary>
  47. /// Preview table
  48. /// </summary>
  49. private Table previewTable;
  50. /// <summary>
  51. /// ColumnModel for the preview table
  52. /// </summary>
  53. private ColumnModel previewColumnModel;
  54. /// <summary>
  55. /// TableModel for the preview table
  56. /// </summary>
  57. private TableModel previewTableModel;
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. private Label previewLabel;
  62. #endregion
  63. #region Constructor
  64. /// <summary>
  65. /// Initializes a new instance of the ColumnCollectionEditor class
  66. /// using the specified collection type
  67. /// </summary>
  68. /// <param name="type">The type of the collection for this editor to edit</param>
  69. public ColumnCollectionEditor(Type type) : base(type)
  70. {
  71. this.columns = null;
  72. this.previewColumnModel = new ColumnModel();
  73. this.previewColumnModel.Columns.Add(new TextColumn("Column", 116));
  74. this.previewTableModel = new TableModel();
  75. this.previewTableModel.Rows.Add(new Row());
  76. Cell cell = new Cell();
  77. cell.Editable = false;
  78. cell.ToolTipText = "This is a Cell ToolTip";
  79. this.previewTableModel.Rows[0].Cells.Add(cell);
  80. this.previewTableModel.RowHeight = 20;
  81. this.previewTable = new Table();
  82. this.previewTable.Preview = true;
  83. this.previewTable.Size = new Size(120, 274);
  84. this.previewTable.Location = new Point(246, 24);
  85. this.previewTable.GridLines = GridLines.Both;
  86. this.previewTable.TabStop = false;
  87. this.previewTable.EnableToolTips = true;
  88. this.previewTable.ColumnModel = this.previewColumnModel;
  89. this.previewTable.TableModel = this.previewTableModel;
  90. this.previewLabel = new Label();
  91. this.previewLabel.Text = "Preview:";
  92. this.previewLabel.Size = new Size(140, 16);
  93. this.previewLabel.Location = new Point(247, 8);
  94. }
  95. #endregion
  96. #region Methods
  97. /// <summary>
  98. /// Edits the value of the specified object using the specified
  99. /// service provider and context
  100. /// </summary>
  101. /// <param name="context">An ITypeDescriptorContext that can be
  102. /// used to gain additional context information</param>
  103. /// <param name="isp">A service provider object through which
  104. /// editing services can be obtained</param>
  105. /// <param name="value">The object to edit the value of</param>
  106. /// <returns>The new value of the object. If the value of the
  107. /// object has not changed, this should return the same object
  108. /// it was passed</returns>
  109. public override object EditValue(ITypeDescriptorContext context, IServiceProvider isp, object value)
  110. {
  111. this.columns = (ColumnCollection) value;
  112. // for some reason (might be beacause Column is an
  113. // abstract class) the table doesn't get redrawn
  114. // when a columns property changes, but we can get
  115. // around that by subscribing to the columns
  116. // PropertyChange event and passing the message on
  117. // to the table ourselves. we need to do this for
  118. // all the existing columns in the collection
  119. for (int i=0; i<this.columns.Count; i++)
  120. {
  121. this.columns[i].PropertyChanged += new ColumnEventHandler(column_PropertyChanged);
  122. }
  123. object returnObject = base.EditValue(context, isp, value);
  124. ColumnModel model = (ColumnModel) context.Instance;
  125. if (model.Table != null)
  126. {
  127. model.Table.PerformLayout();
  128. model.Table.Refresh();
  129. }
  130. return returnObject;
  131. }
  132. /// <summary>
  133. /// Gets the data types that this collection editor can contain
  134. /// </summary>
  135. /// <returns>An array of data types that this collection can contain</returns>
  136. protected override Type[] CreateNewItemTypes()
  137. {
  138. return new Type[] {typeof(TextColumn),
  139. typeof(ButtonColumn),
  140. typeof(CheckBoxColumn),
  141. typeof(ColorColumn),
  142. typeof(ComboBoxColumn),
  143. typeof(DateTimeColumn),
  144. typeof(ImageColumn),
  145. typeof(NumberColumn),
  146. typeof(ProgressBarColumn)};
  147. }
  148. /// <summary>
  149. /// Creates a new instance of the specified collection item type
  150. /// </summary>
  151. /// <param name="itemType">The type of item to create</param>
  152. /// <returns>A new instance of the specified object</returns>
  153. protected override object CreateInstance(Type itemType)
  154. {
  155. Column column = (Column) base.CreateInstance(itemType);
  156. // newly created items aren't added to the collection
  157. // until editing has finished. we'd like the newly
  158. // created column to show up in the table immediately
  159. // so we'll add it to the ColumnCollection now
  160. this.columns.Add(column);
  161. // for some reason (might be beacause Column is an
  162. // abstract class) the table doesn't get redrawn
  163. // when a columns property changes, but we can get
  164. // around that by subscribing to the columns
  165. // PropertyChange event and passing the message on
  166. // to the table ourselves
  167. column.PropertyChanged += new XPTable.Events.ColumnEventHandler(column_PropertyChanged);
  168. return column;
  169. }
  170. /// <summary>
  171. /// Destroys the specified instance of the object
  172. /// </summary>
  173. /// <param name="instance">The object to destroy</param>
  174. protected override void DestroyInstance(object instance)
  175. {
  176. if (instance != null && instance is Column)
  177. {
  178. Column column = (Column) instance;
  179. // the specified column is about to be destroyed
  180. // so we need to remove it from the ColumnCollection first
  181. this.columns.Remove(column);
  182. column.PropertyChanged -= new XPTable.Events.ColumnEventHandler(column_PropertyChanged);
  183. }
  184. base.DestroyInstance(instance);
  185. }
  186. /// <summary>
  187. /// Creates a new form to display and edit the current collection
  188. /// </summary>
  189. /// <returns>An instance of CollectionEditor.CollectionForm to provide
  190. /// as the user interface for editing the collection</returns>
  191. protected override CollectionEditor.CollectionForm CreateCollectionForm()
  192. {
  193. CollectionEditor.CollectionForm editor = base.CreateCollectionForm();
  194. editor.Width += 140;
  195. foreach (Control control in editor.Controls)
  196. {
  197. if (control.Name.Equals("propertiesLabel"))
  198. {
  199. control.Location = new Point(control.Left + 140, control.Top);
  200. }
  201. //
  202. if (control is PropertyGrid)
  203. {
  204. PropertyGrid grid = (PropertyGrid) control;
  205. grid.SelectedObjectsChanged += new EventHandler(this.PropertyGrid_SelectedObjectsChanged);
  206. grid.Location = new Point(grid.Left + 140, grid.Top);
  207. grid.Width -= 140;
  208. }
  209. }
  210. editor.Controls.Add(this.previewLabel);
  211. editor.Controls.Add(this.previewTable);
  212. return editor;
  213. }
  214. #endregion
  215. #region Events
  216. /// <summary>
  217. /// Handler for the PropertyGrid's SelectedObjectsChanged event
  218. /// </summary>
  219. /// <param name="sender">The object that raised the event</param>
  220. /// <param name="e">An EventArgs that contains the event data</param>
  221. protected void PropertyGrid_SelectedObjectsChanged(object sender, EventArgs e)
  222. {
  223. object[] objects = ((PropertyGrid) sender).SelectedObjects;
  224. this.previewColumnModel.Columns.Clear();
  225. if (objects.Length == 1)
  226. {
  227. Column column = (Column) objects[0];
  228. Cell cell = this.previewTableModel[0, 0];
  229. if (column is ButtonColumn)
  230. {
  231. cell.Text = "Button";
  232. cell.Data = null;
  233. }
  234. else if (column is CheckBoxColumn)
  235. {
  236. cell.Text = "Checkbox";
  237. cell.Data = null;
  238. cell.Checked = true;
  239. }
  240. else if (column is ColorColumn)
  241. {
  242. cell.Text = null;
  243. cell.Data = Color.Red;
  244. }
  245. else if (column is ComboBoxColumn)
  246. {
  247. cell.Text = "ComboBox";
  248. cell.Data = null;
  249. }
  250. else if (column is DateTimeColumn)
  251. {
  252. cell.Text = null;
  253. cell.Data = DateTime.Now;
  254. }
  255. else if (column is ImageColumn)
  256. {
  257. cell.Text = "Image";
  258. cell.Data = null;
  259. }
  260. else if (column is NumberColumn || column is ProgressBarColumn)
  261. {
  262. cell.Text = null;
  263. cell.Data = 50;
  264. }
  265. else //if (column is TextColumn)
  266. {
  267. cell.Text = "Text";
  268. cell.Data = null;
  269. }
  270. this.previewColumnModel.Columns.Add(column);
  271. }
  272. this.previewTable.Invalidate();
  273. }
  274. /// <summary>
  275. /// Handler for a Column's PropertyChanged event
  276. /// </summary>
  277. /// <param name="sender">The object that raised the event</param>
  278. /// <param name="e">A ColumnEventArgs that contains the event data</param>
  279. private void column_PropertyChanged(object sender, ColumnEventArgs e)
  280. {
  281. this.columns.ColumnModel.OnColumnPropertyChanged(e);
  282. }
  283. #endregion
  284. }
  285. }