CustomCollectionEditorTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using System;
  2. using System.Windows.Forms;
  3. using System.ComponentModel;
  4. using System.Collections;
  5. using System.Collections.Specialized;
  6. using System.Drawing.Design;
  7. using System.Drawing;
  8. using System.Globalization;
  9. using System.ComponentModel.Design.Serialization;
  10. using Test.Items;
  11. using Test.CollectionEditors;
  12. using Test.Collections;
  13. using Test.TypeConverters;
  14. using CustomControls.CollectionEditor;
  15. namespace Test
  16. {
  17. namespace CollectionEditors
  18. {
  19. #region "CollectionEditors"
  20. public class CustomCollectionEditorDialog : CustomCollectionEditorForm
  21. {
  22. protected override void SetProperties(TItem titem, object reffObject)
  23. {
  24. if (reffObject is CustomItem)
  25. {
  26. CustomItem ci = reffObject as CustomItem;
  27. titem.SubItems = ci.SpecialItems;
  28. titem.Text = ci.Color.Name;
  29. titem.SelectedImageIndex = 0;
  30. titem.ImageIndex = 1;
  31. titem.ForeColor = ci.Color;
  32. }
  33. else if (reffObject is SpecialItem)
  34. {
  35. SpecialItem si = reffObject as SpecialItem;
  36. titem.Text = "SpecialItem " + si.GetHashCode().ToString();
  37. titem.SubItems = si.SubItems;
  38. titem.ForeColor = Color.Blue;
  39. titem.SelectedImageIndex = 0;
  40. titem.ImageIndex = 2;
  41. }
  42. else if (reffObject is SimpleItem_FullTc)
  43. {
  44. SimpleItem_FullTc siftc = reffObject as SimpleItem_FullTc;
  45. titem.Text = siftc.Id.ToString();
  46. if (siftc.Id > 0) { titem.ForeColor = Color.SeaGreen; }
  47. else { titem.ForeColor = Color.DarkRed; }
  48. titem.SelectedImageIndex = 0;
  49. titem.ImageIndex = 3;
  50. }
  51. else if (reffObject is SimpleItem_Component)
  52. {
  53. SimpleItem_Component sic = reffObject as SimpleItem_Component;
  54. titem.Text = sic.Name;
  55. titem.ForeColor = Color.Navy;
  56. titem.SelectedImageIndex = 0;
  57. titem.ImageIndex = 4;
  58. }
  59. else if (reffObject is SimpleItem_BasicTc)
  60. {
  61. SimpleItem_BasicTc sibtc = reffObject as SimpleItem_BasicTc;
  62. titem.Text = sibtc.Name;
  63. titem.ForeColor = Color.DarkSlateGray;
  64. titem.SelectedImageIndex = 0;
  65. titem.ImageIndex = 5;
  66. }
  67. else if (reffObject is ComplexItem)
  68. {
  69. ComplexItem ci = reffObject as ComplexItem;
  70. titem.Text = ci.Name;
  71. titem.ForeColor = Color.Black;
  72. titem.SelectedImageIndex = 0;
  73. titem.ImageIndex = 6;
  74. }
  75. else
  76. {
  77. // set the default Text
  78. base.SetProperties(titem, reffObject);
  79. }
  80. }
  81. protected override Type[] CreateNewItemTypes(System.Collections.IList coll)
  82. {
  83. if (coll is SimpleItems)
  84. {
  85. return new Type[] { typeof(CustomItem), typeof(SimpleItem_BasicTc), typeof(SimpleItem_FullTc), typeof(SimpleItem_Component), typeof(ComplexItem) };
  86. }
  87. else
  88. {
  89. return base.CreateNewItemTypes(coll);
  90. }
  91. }
  92. protected override CustomControls.Enumerations.EditLevel SetEditLevel(IList collection)
  93. {
  94. if (collection is SpecialItems)
  95. {
  96. return CustomControls.Enumerations.EditLevel.AddOnly;
  97. }
  98. return base.SetEditLevel(collection);
  99. }
  100. }
  101. public class CustomItemCollectionEditor : CustomCollectionEditor
  102. {
  103. protected override CustomCollectionEditorForm CreateForm()
  104. {
  105. return new CustomCollectionEditorDialog();
  106. }
  107. }
  108. #endregion
  109. }
  110. namespace Items
  111. {
  112. #region "Items"
  113. public class TestItem
  114. {
  115. public TestItem(int e, string t)
  116. {}
  117. }
  118. [TypeConverter(typeof(CustomItemConverter))]
  119. public class CustomItem:SimpleItem
  120. {
  121. private SpecialItems _SpecialItems= new SpecialItems();
  122. private Color _Color=Color.SteelBlue;
  123. [Category("TestProperties")]
  124. [DefaultValue(typeof(Color),"SteelBlue")]
  125. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  126. public Color Color
  127. {
  128. get{return _Color;}
  129. set{_Color=value;}
  130. }
  131. [Category ("Collections")]
  132. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  133. [Editor(typeof(CustomItemCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
  134. public SpecialItems SpecialItems
  135. {
  136. get{return _SpecialItems;}
  137. }
  138. public CustomItem():base()
  139. {}
  140. public CustomItem(int Id, string Name,SpecialItem[] specialItems):base(Id,Name)
  141. {
  142. this.SpecialItems.AddRange(specialItems);
  143. }
  144. }
  145. [TypeConverter(typeof(SpecialItemConverter))]
  146. public class SpecialItem
  147. {
  148. private string _Speciality=string.Empty;
  149. private int _Cod=-1;
  150. private SpecialItems _SubItems= new SpecialItems();
  151. [Category("TestProperties")]
  152. [DefaultValue(typeof(string),"")]
  153. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  154. public string Speciality
  155. {
  156. get{return _Speciality;}
  157. set{_Speciality= value;}
  158. }
  159. [Category("TestProperties")]
  160. [DefaultValue(typeof(int),"-1")]
  161. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  162. public int Cod
  163. {
  164. get{return _Cod;}
  165. set{_Cod= value;}
  166. }
  167. [Category ("Collections")]
  168. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  169. [Editor(typeof(CustomItemCollectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
  170. public SpecialItems SubItems
  171. {
  172. get{return _SubItems;}
  173. }
  174. public SpecialItem()
  175. {
  176. }
  177. public SpecialItem(int Cod, string Speciality, SpecialItem[] specialItems)
  178. {
  179. this._Cod=Cod;
  180. this._Speciality= Speciality;
  181. this._SubItems.AddRange(specialItems);
  182. }
  183. }
  184. #endregion
  185. }
  186. namespace Collections
  187. {
  188. #region "Collections"
  189. public class SpecialItems:CollectionBase
  190. {
  191. public SpecialItems()
  192. {}
  193. public SpecialItem Add(SpecialItem si)
  194. {
  195. this.InnerList.Add(si);
  196. return si;
  197. }
  198. public void AddRange(SpecialItem[] si)
  199. {
  200. this.InnerList.AddRange(si);
  201. }
  202. public void Remove(SpecialItem si)
  203. {
  204. this.InnerList.Remove(si);
  205. }
  206. public bool Contains(SpecialItem si)
  207. {
  208. return this.InnerList.Contains(si);
  209. }
  210. public SpecialItem this[int i]
  211. {
  212. get{return (SpecialItem)this.InnerList[i];}
  213. set{this.InnerList[i]=value;}
  214. }
  215. public SpecialItem[] GetValues()
  216. {
  217. SpecialItem[] si= new SpecialItem[this.InnerList.Count];
  218. this.InnerList.CopyTo(0,si,0,this.InnerList.Count);
  219. return si;
  220. }
  221. }
  222. #endregion
  223. }
  224. namespace TypeConverters
  225. {
  226. #region "TypeConverter"
  227. internal class SpecialItemConverter:ExpandableObjectConverter
  228. {
  229. public override bool CanConvertFrom(ITypeDescriptorContext context,Type t)
  230. {
  231. if (t==typeof(string)){return true;}
  232. return base.CanConvertFrom(context ,t);
  233. }
  234. public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
  235. {
  236. if (destType == typeof(InstanceDescriptor) || destType == typeof(string))
  237. {
  238. return true;
  239. }
  240. return base.CanConvertTo(context, destType);
  241. }
  242. public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo info,object value)
  243. {
  244. if (value is string)
  245. {
  246. string str=(string)value;
  247. try
  248. {
  249. string[] elements=str.Split(new char[] {';',':'});
  250. return new SpecialItem(int.Parse(elements[1]),elements[3], null);
  251. }
  252. catch
  253. {
  254. return new SpecialItem();
  255. }
  256. }
  257. return base.ConvertFrom(context,info,value);
  258. }
  259. public override object ConvertTo(ITypeDescriptorContext context,CultureInfo info,object value,Type destType )
  260. {
  261. if ((destType==typeof(string))&&(value is SpecialItem))
  262. {
  263. SpecialItem si=(SpecialItem)value;
  264. return "Cod : "+ si.Cod.ToString() +";"+" Speciality : " + si.Speciality;
  265. }
  266. else if (destType == typeof(InstanceDescriptor))
  267. {
  268. return new InstanceDescriptor(typeof(SpecialItem).GetConstructor(new Type[]{typeof(int), typeof(string), typeof(SpecialItem[])}), new object[]{((SpecialItem)value).Cod,((SpecialItem)value).Speciality,((SpecialItem)value).SubItems.GetValues()});
  269. }
  270. return base.ConvertTo(context,info,value,destType);
  271. }
  272. }
  273. internal class CustomItemConverter:ExpandableObjectConverter
  274. {
  275. public override bool CanConvertFrom(ITypeDescriptorContext context,Type t)
  276. {
  277. if (t==typeof(string)){return true;}
  278. return base.CanConvertFrom(context ,t);
  279. }
  280. public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
  281. {
  282. if (destType == typeof(InstanceDescriptor) || destType == typeof(string))
  283. {
  284. return true;
  285. }
  286. return base.CanConvertTo(context, destType);
  287. }
  288. public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo info,object value)
  289. {
  290. if (value is string)
  291. {
  292. string str=(string)value;
  293. try
  294. {
  295. string[] elements=str.Split(new char[] {';',':'});
  296. return new CustomItem(int.Parse(elements[1]),elements[3],null);
  297. }
  298. catch
  299. {
  300. return new CustomItem();
  301. }
  302. }
  303. return base.ConvertFrom(context,info,value);
  304. }
  305. public override object ConvertTo(ITypeDescriptorContext context,CultureInfo info,object value,Type destType )
  306. {
  307. if ((destType==typeof(string))&&(value is CustomItem))
  308. {
  309. CustomItem ci=(CustomItem)value;
  310. return "Id : "+ ci.Id.ToString() +";"+" Name : " + ci.Name;
  311. }
  312. else if (destType == typeof(InstanceDescriptor))
  313. {
  314. return new InstanceDescriptor(typeof(CustomItem).GetConstructor(new Type[]{typeof(int), typeof(string), typeof(SpecialItem[])}), new object[]{((CustomItem)value).Id,((CustomItem)value).Name,((CustomItem)value).SpecialItems.GetValues()});
  315. }
  316. return base.ConvertTo(context,info,value,destType);
  317. }
  318. }
  319. #endregion
  320. }
  321. }