CustomCollectionEditorForm.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Reflection;
  7. using System.ComponentModel.Design.Serialization;
  8. using System.Data;
  9. using CustomControls.Win32Controls;
  10. using CustomControls.Enumerations;
  11. namespace CustomControls.CollectionEditor
  12. {
  13. public class CustomCollectionEditorForm : System.Windows.Forms.Form
  14. {
  15. #region "Variables"
  16. public delegate void InstanceEventHandler(object sender, object instance);
  17. public event InstanceEventHandler InstanceCreated;
  18. public event InstanceEventHandler DestroyingInstance;
  19. public event InstanceEventHandler ItemRemoved;
  20. public event InstanceEventHandler ItemAdded;
  21. private IList _Collection=null;
  22. private Type lastItemType=null;
  23. private ArrayList backupList=null;
  24. private EditLevel _EditLevel=EditLevel.FullEdit;
  25. protected System.Windows.Forms.PropertyGrid pg_PropGrid;
  26. protected CustomControls.Win32Controls.DropDownListBoxButton btn_Add;
  27. protected CustomControls.Win32Controls.PushButton btn_Remove;
  28. protected CustomControls.Win32Controls.PushButton btn_Up;
  29. protected CustomControls.Win32Controls.PushButton btn_Down;
  30. private System.Windows.Forms.Panel pan_Items;
  31. private System.Windows.Forms.Panel pan_MainPan;
  32. private System.Windows.Forms.Splitter spl_Splitter;
  33. private System.Windows.Forms.Panel pan_ButtonsPan;
  34. protected CustomControls.Win32Controls.PushButton btn_OK;
  35. protected CustomControls.Win32Controls.PushButton btn_Cancel;
  36. protected System.Windows.Forms.TreeView tv_Items;
  37. private System.Windows.Forms.Panel pan_PropGridPan;
  38. private CustomCollectionEditor attachedEditor=null;
  39. #endregion
  40. #region "Properties"
  41. public IList Collection
  42. {
  43. get{return _Collection;}
  44. set
  45. {
  46. _Collection=value;
  47. backupList= new ArrayList(value);
  48. ProccessCollection(value);
  49. RefreshValues();
  50. }
  51. }
  52. [Category("Behavior")]
  53. public EditLevel EditLevel
  54. {
  55. get{return _EditLevel;}
  56. set
  57. {
  58. if(value!=_EditLevel)
  59. {
  60. _EditLevel= value;
  61. OnEditLevelChanged(new EventArgs());
  62. }
  63. }
  64. }
  65. [Category("Behavior")]
  66. public ImageList ImageList
  67. {
  68. get{return tv_Items.ImageList;}
  69. set{tv_Items.ImageList= value;}
  70. }
  71. #endregion
  72. #region "Constructors"
  73. public CustomCollectionEditorForm()
  74. {
  75. InitializeComponent();
  76. RefreshValues();
  77. }
  78. #endregion
  79. #region Windows Form Designer generated code
  80. /// <summary>
  81. /// Required method for Designer support - do not modify
  82. /// the contents of this method with the code editor.
  83. /// </summary>
  84. private void InitializeComponent()
  85. {
  86. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CustomCollectionEditorForm));
  87. this.pg_PropGrid = new System.Windows.Forms.PropertyGrid();
  88. this.btn_Add = new CustomControls.Win32Controls.DropDownListBoxButton();
  89. this.btn_Remove = new CustomControls.Win32Controls.PushButton();
  90. this.btn_Up = new CustomControls.Win32Controls.PushButton();
  91. this.btn_Down = new CustomControls.Win32Controls.PushButton();
  92. this.pan_Items = new System.Windows.Forms.Panel();
  93. this.tv_Items = new System.Windows.Forms.TreeView();
  94. this.pan_MainPan = new System.Windows.Forms.Panel();
  95. this.spl_Splitter = new System.Windows.Forms.Splitter();
  96. this.pan_PropGridPan = new System.Windows.Forms.Panel();
  97. this.pan_ButtonsPan = new System.Windows.Forms.Panel();
  98. this.btn_Cancel = new CustomControls.Win32Controls.PushButton();
  99. this.btn_OK = new CustomControls.Win32Controls.PushButton();
  100. ((System.ComponentModel.ISupportInitialize)(this.btn_Add)).BeginInit();
  101. this.pan_Items.SuspendLayout();
  102. this.pan_MainPan.SuspendLayout();
  103. this.pan_PropGridPan.SuspendLayout();
  104. this.pan_ButtonsPan.SuspendLayout();
  105. this.SuspendLayout();
  106. //
  107. // pg_PropGrid
  108. //
  109. this.pg_PropGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  110. | System.Windows.Forms.AnchorStyles.Left)
  111. | System.Windows.Forms.AnchorStyles.Right)));
  112. this.pg_PropGrid.BackColor = System.Drawing.SystemColors.Control;
  113. this.pg_PropGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
  114. this.pg_PropGrid.Location = new System.Drawing.Point(8, 6);
  115. this.pg_PropGrid.Name = "pg_PropGrid";
  116. this.pg_PropGrid.PropertySort = System.Windows.Forms.PropertySort.Categorized;
  117. this.pg_PropGrid.Size = new System.Drawing.Size(269, 340);
  118. this.pg_PropGrid.TabIndex = 3;
  119. this.pg_PropGrid.ToolbarVisible = false;
  120. this.pg_PropGrid.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler(this.pg_PropertyValueChanged);
  121. this.pg_PropGrid.SelectedGridItemChanged += new System.Windows.Forms.SelectedGridItemChangedEventHandler(this.pg_PropGrid_SelectedGridItemChanged);
  122. //
  123. // btn_Add
  124. //
  125. this.btn_Add.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  126. this.btn_Add.ForeColor = System.Drawing.SystemColors.ControlDark;
  127. this.btn_Add.Location = new System.Drawing.Point(10, 321);
  128. this.btn_Add.Name = "btn_Add";
  129. this.btn_Add.Size = new System.Drawing.Size(80, 24);
  130. this.btn_Add.TabIndex = 4;
  131. this.btn_Add.Text = "Add";
  132. this.btn_Add.ItemSelected += new CustomControls.Win32Controls.DropDownListBoxButton.ItemSelectedEventHandler(this.btn_Add_ItemSelected);
  133. //
  134. // btn_Remove
  135. //
  136. this.btn_Remove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  137. this.btn_Remove.Location = new System.Drawing.Point(115, 321);
  138. this.btn_Remove.Name = "btn_Remove";
  139. this.btn_Remove.Size = new System.Drawing.Size(96, 26);
  140. this.btn_Remove.TabIndex = 6;
  141. this.btn_Remove.Text = "Remove";
  142. this.btn_Remove.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  143. this.btn_Remove.Click += new System.EventHandler(this.btn_Remove_Click);
  144. //
  145. // btn_Up
  146. //
  147. this.btn_Up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  148. this.btn_Up.Image = ((System.Drawing.Image)(resources.GetObject("btn_Up.Image")));
  149. this.btn_Up.Location = new System.Drawing.Point(220, 9);
  150. this.btn_Up.Name = "btn_Up";
  151. this.btn_Up.Size = new System.Drawing.Size(28, 34);
  152. this.btn_Up.TabIndex = 1;
  153. this.btn_Up.Click += new System.EventHandler(this.btn_Up_Click);
  154. //
  155. // btn_Down
  156. //
  157. this.btn_Down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  158. this.btn_Down.Image = ((System.Drawing.Image)(resources.GetObject("btn_Down.Image")));
  159. this.btn_Down.Location = new System.Drawing.Point(220, 52);
  160. this.btn_Down.Name = "btn_Down";
  161. this.btn_Down.Size = new System.Drawing.Size(28, 34);
  162. this.btn_Down.TabIndex = 2;
  163. this.btn_Down.Click += new System.EventHandler(this.btn_Down_Click);
  164. //
  165. // pan_Items
  166. //
  167. this.pan_Items.Controls.Add(this.tv_Items);
  168. this.pan_Items.Controls.Add(this.btn_Down);
  169. this.pan_Items.Controls.Add(this.btn_Remove);
  170. this.pan_Items.Controls.Add(this.btn_Add);
  171. this.pan_Items.Controls.Add(this.btn_Up);
  172. this.pan_Items.Dock = System.Windows.Forms.DockStyle.Fill;
  173. this.pan_Items.Location = new System.Drawing.Point(0, 0);
  174. this.pan_Items.Name = "pan_Items";
  175. this.pan_Items.Size = new System.Drawing.Size(259, 355);
  176. this.pan_Items.TabIndex = 9;
  177. //
  178. // tv_Items
  179. //
  180. this.tv_Items.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  181. | System.Windows.Forms.AnchorStyles.Left)
  182. | System.Windows.Forms.AnchorStyles.Right)));
  183. this.tv_Items.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  184. this.tv_Items.FullRowSelect = true;
  185. this.tv_Items.HideSelection = false;
  186. this.tv_Items.Indent = 25;
  187. this.tv_Items.Location = new System.Drawing.Point(8, 8);
  188. this.tv_Items.Name = "tv_Items";
  189. this.tv_Items.Size = new System.Drawing.Size(204, 305);
  190. this.tv_Items.TabIndex = 0;
  191. this.tv_Items.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.tv_Items_BeforeSelect);
  192. this.tv_Items.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tv_Items_AfterSelect);
  193. //
  194. // pan_MainPan
  195. //
  196. this.pan_MainPan.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  197. | System.Windows.Forms.AnchorStyles.Left)
  198. | System.Windows.Forms.AnchorStyles.Right)));
  199. this.pan_MainPan.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  200. this.pan_MainPan.Controls.Add(this.spl_Splitter);
  201. this.pan_MainPan.Controls.Add(this.pan_Items);
  202. this.pan_MainPan.Controls.Add(this.pan_PropGridPan);
  203. this.pan_MainPan.Location = new System.Drawing.Point(7, 6);
  204. this.pan_MainPan.Name = "pan_MainPan";
  205. this.pan_MainPan.Size = new System.Drawing.Size(546, 357);
  206. this.pan_MainPan.TabIndex = 11;
  207. //
  208. // spl_Splitter
  209. //
  210. this.spl_Splitter.BackColor = System.Drawing.SystemColors.ControlDark;
  211. this.spl_Splitter.Dock = System.Windows.Forms.DockStyle.Right;
  212. this.spl_Splitter.Location = new System.Drawing.Point(256, 0);
  213. this.spl_Splitter.MinExtra = 216;
  214. this.spl_Splitter.MinSize = 208;
  215. this.spl_Splitter.Name = "spl_Splitter";
  216. this.spl_Splitter.Size = new System.Drawing.Size(3, 355);
  217. this.spl_Splitter.TabIndex = 10;
  218. this.spl_Splitter.TabStop = false;
  219. //
  220. // pan_PropGridPan
  221. //
  222. this.pan_PropGridPan.Controls.Add(this.pg_PropGrid);
  223. this.pan_PropGridPan.Dock = System.Windows.Forms.DockStyle.Right;
  224. this.pan_PropGridPan.Location = new System.Drawing.Point(259, 0);
  225. this.pan_PropGridPan.Name = "pan_PropGridPan";
  226. this.pan_PropGridPan.Size = new System.Drawing.Size(285, 355);
  227. this.pan_PropGridPan.TabIndex = 10;
  228. //
  229. // pan_ButtonsPan
  230. //
  231. this.pan_ButtonsPan.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  232. | System.Windows.Forms.AnchorStyles.Right)));
  233. this.pan_ButtonsPan.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  234. this.pan_ButtonsPan.Controls.Add(this.btn_Cancel);
  235. this.pan_ButtonsPan.Controls.Add(this.btn_OK);
  236. this.pan_ButtonsPan.Location = new System.Drawing.Point(7, 372);
  237. this.pan_ButtonsPan.Name = "pan_ButtonsPan";
  238. this.pan_ButtonsPan.Size = new System.Drawing.Size(546, 45);
  239. this.pan_ButtonsPan.TabIndex = 12;
  240. //
  241. // btn_Cancel
  242. //
  243. this.btn_Cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  244. this.btn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  245. this.btn_Cancel.Location = new System.Drawing.Point(438, 9);
  246. this.btn_Cancel.Name = "btn_Cancel";
  247. this.btn_Cancel.Size = new System.Drawing.Size(96, 25);
  248. this.btn_Cancel.TabIndex = 7;
  249. this.btn_Cancel.Text = "Cancel";
  250. this.btn_Cancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  251. this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
  252. //
  253. // btn_OK
  254. //
  255. this.btn_OK.DialogResult = System.Windows.Forms.DialogResult.OK;
  256. this.btn_OK.Location = new System.Drawing.Point(10, 9);
  257. this.btn_OK.Name = "btn_OK";
  258. this.btn_OK.Size = new System.Drawing.Size(96, 25);
  259. this.btn_OK.TabIndex = 6;
  260. this.btn_OK.Text = "Ok";
  261. this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click);
  262. //
  263. // CustomCollectionEditorForm
  264. //
  265. this.AcceptButton = this.btn_OK;
  266. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  267. this.BackColor = System.Drawing.SystemColors.Control;
  268. this.CancelButton = this.btn_Cancel;
  269. this.ClientSize = new System.Drawing.Size(560, 424);
  270. this.ControlBox = false;
  271. this.Controls.Add(this.pan_ButtonsPan);
  272. this.Controls.Add(this.pan_MainPan);
  273. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
  274. this.MinimumSize = new System.Drawing.Size(576, 323);
  275. this.Name = "CustomCollectionEditorForm";
  276. this.ShowInTaskbar = false;
  277. this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  278. this.Text = "CustomCollectionEditor";
  279. this.TopMost = true;
  280. ((System.ComponentModel.ISupportInitialize)(this.btn_Add)).EndInit();
  281. this.pan_Items.ResumeLayout(false);
  282. this.pan_MainPan.ResumeLayout(false);
  283. this.pan_PropGridPan.ResumeLayout(false);
  284. this.pan_ButtonsPan.ResumeLayout(false);
  285. this.ResumeLayout(false);
  286. }
  287. #endregion
  288. #region "Collection Item"
  289. /// <summary>
  290. /// Gets the data type of each item in the collection.
  291. /// </summary>
  292. /// <param name="coll"> The collection for which to get the item's type</param>
  293. /// <returns>The data type of the collection items.</returns>
  294. protected virtual Type GetItemType(IList coll)
  295. {
  296. PropertyInfo pi= coll.GetType().GetProperty("Item",new Type[]{typeof(int)});
  297. return pi.PropertyType;
  298. }
  299. /// <summary>
  300. /// Gets the data types that this collection editor can contain
  301. /// </summary>
  302. /// <param name="coll">The collection for which to return the available types</param>
  303. /// <returns>An array of data types that this collection can contain.</returns>
  304. protected virtual Type[] CreateNewItemTypes(IList coll)
  305. {
  306. return new Type[]{ GetItemType(coll)};
  307. }
  308. /// <summary>
  309. /// Creates a new instance of the specified collection item type.
  310. /// </summary>
  311. /// <param name="itemType">The type of item to create. </param>
  312. /// <returns>A new instance of the specified object.</returns>
  313. protected virtual object CreateInstance(Type itemType)
  314. {
  315. /*
  316. //This is just another way of how to remotely create an object
  317. // Try to get a parameterless constructor
  318. ConstructorInfo ci = itemType.GetConstructor(new Type[0]);
  319. InstanceDescriptor id = new InstanceDescriptor(ci,null,false);
  320. return id.Invoke();
  321. */
  322. object instance=Activator.CreateInstance(itemType,true);
  323. OnInstanceCreated(instance);
  324. return instance ;
  325. }
  326. /// <summary>
  327. /// Destroys the specified instance of the object.
  328. /// </summary>
  329. /// <param name="instance">The object to destroy. </param>
  330. protected virtual void DestroyInstance( object instance )
  331. {
  332. OnDestroyingInstance(instance);
  333. if(instance is IDisposable){((IDisposable)instance).Dispose();}
  334. instance=null;
  335. }
  336. protected virtual void OnDestroyingInstance(object instance)
  337. {
  338. if( DestroyingInstance!=null)
  339. {
  340. DestroyingInstance(this,instance);
  341. }
  342. }
  343. protected virtual void OnInstanceCreated(object instance)
  344. {
  345. if (InstanceCreated!=null)
  346. {
  347. InstanceCreated(this,instance);
  348. }
  349. }
  350. protected virtual void OnItemRemoved(object item)
  351. {
  352. if(ItemRemoved!=null)
  353. {
  354. ItemRemoved(this,item);
  355. }
  356. }
  357. protected virtual void OnItemAdded(object Item)
  358. {
  359. if(ItemAdded!=null)
  360. {
  361. ItemAdded(this,Item);
  362. }
  363. }
  364. #endregion
  365. #region "TItem Related"
  366. private void MoveItem(IList list,int index, int step)
  367. {
  368. if(index>-1 && index <list.Count && index+step >-1 && index+step<list.Count)
  369. {
  370. int poss=index+step;
  371. object possObject=list[poss];
  372. list[poss]=list[index];
  373. list[index]=possObject;
  374. possObject=null;
  375. }
  376. }
  377. protected internal TItem[] GenerateTItemArray(IList collection)
  378. {
  379. TItem[] ti=new TItem[0];
  380. if (collection !=null && collection.Count>0)
  381. {
  382. ti= new TItem[collection.Count];
  383. for(int i=0;i<collection.Count;i++)
  384. {
  385. ti[i]=CreateTItem(collection[i]);
  386. }
  387. }
  388. return ti;
  389. }
  390. /// <summary>
  391. /// Creates a new TItem objectfor a collection item.
  392. /// </summary>
  393. /// <param name="reffObject">The collection item for wich to create an TItem object.</param>
  394. /// <returns>A TItem object referencing the collection item received as parameter.</returns>
  395. protected virtual TItem CreateTItem(object reffObject)
  396. {
  397. TItem ti= new TItem(this,reffObject);
  398. SetProperties(ti,reffObject);
  399. return ti;
  400. }
  401. /// <summary>
  402. /// When implemented by a class, customize a TItem object in respect to it's corresponding collection item.
  403. /// </summary>
  404. /// <param name="titem">The TItem object to be customized in respect to it's corresponding collection item.</param>
  405. /// <param name="reffObject">The collection item for which it customizes the TItem object.</param>
  406. protected virtual void SetProperties(TItem titem,object reffObject)
  407. {
  408. // set the display name
  409. PropertyInfo pi=titem.Value.GetType().GetProperty("Name");
  410. if(pi!=null )
  411. {
  412. titem.Text= pi.GetValue(titem.Value,null).ToString();
  413. }
  414. else
  415. {
  416. titem.Text= titem.Value.GetType().Name;
  417. }
  418. }
  419. #endregion
  420. #region "Implementation"
  421. protected virtual void RefreshValues()
  422. {
  423. tv_Items.BeginUpdate();
  424. tv_Items.Nodes.Clear();
  425. tv_Items.Nodes.AddRange(GenerateTItemArray(this.Collection));
  426. tv_Items.EndUpdate();
  427. }
  428. protected virtual EditLevel SetEditLevel(IList collection)
  429. {
  430. return EditLevel.FullEdit;
  431. }
  432. private void SetCollectionEditLevel(IList collection)
  433. {
  434. EditLevel el=SetEditLevel(collection);
  435. switch(el)
  436. {
  437. case EditLevel.FullEdit:
  438. {
  439. this.btn_Remove.Enabled=Remove_CanEnable() ;
  440. this.btn_Add.Enabled=Add_CanEnable();
  441. break;
  442. }
  443. case EditLevel.AddOnly:
  444. {
  445. this.btn_Remove.Enabled=Remove_CanEnable() && false ;
  446. this.btn_Add.Enabled=Add_CanEnable();
  447. break;
  448. }
  449. case EditLevel.RemoveOnly:
  450. {
  451. this.btn_Add.Enabled=Add_CanEnable() && false;
  452. this.btn_Remove.Enabled=Remove_CanEnable() ;
  453. break;
  454. }
  455. case EditLevel.ReadOnly:
  456. {
  457. this.btn_Remove.Enabled=Remove_CanEnable() && false ;
  458. this.btn_Add.Enabled=Add_CanEnable() && false;
  459. break;
  460. }
  461. }
  462. }
  463. private bool Add_CanEnable()
  464. {
  465. if(this.EditLevel==EditLevel.FullEdit ||this.EditLevel== EditLevel.AddOnly){return true;}
  466. return false;
  467. }
  468. private bool Remove_CanEnable()
  469. {
  470. if(this.EditLevel==EditLevel.FullEdit || this.EditLevel==EditLevel.RemoveOnly){return true;}
  471. return false;
  472. }
  473. protected virtual void RefreshAvailableTypes(IList collection)
  474. {
  475. btn_Add.List.Items.Clear();
  476. btn_Add.List.Items.AddRange(CreateNewItemTypes(collection));
  477. if(btn_Add.List.Items.Count<2){btn_Add.CanDropDown=false;}
  478. else{btn_Add.CanDropDown=true;}
  479. btn_Add.MinListWidth=CalculateDDListWidth();
  480. }
  481. private void ProccessCollection(IList collection)
  482. {
  483. RefreshAvailableTypes(collection);
  484. SetCollectionEditLevel(collection);
  485. }
  486. private int CalculateDDListWidth()
  487. {
  488. int width=0;
  489. Graphics g=btn_Add.List.CreateGraphics();
  490. foreach (object item in this.btn_Add.List.Items)
  491. {
  492. int itemWidth=(int)g.MeasureString(item.ToString(),btn_Add.List.Font).Width;
  493. width= Math.Max(width,itemWidth);
  494. }
  495. width=Math.Max(width,btn_Add.Width+20);
  496. return width;
  497. }
  498. private void btn_OK_Click(object sender, System.EventArgs e)
  499. {
  500. this.Close();
  501. }
  502. private void btn_Cancel_Click(object sender, System.EventArgs e)
  503. {
  504. UndoChanges(backupList, Collection);
  505. this.Close();
  506. }
  507. private void btn_Add_ItemSelected(object sender, CustomControls.HelperClasses.ItemSelectedEventArgs e)
  508. {
  509. tv_Items.BeginUpdate();
  510. if(Collection!=null && e.SelectedItem!=null)
  511. {
  512. //create a new item to add to the Collection and a corespondent TItem to add to the treeview nodes
  513. Type type=(Type)e.SelectedItem;
  514. object newCollItem=CreateInstance(type);
  515. TItem newTItem=CreateTItem(newCollItem);
  516. //get the current possition and the parent collections to insert into
  517. TItem selTItem= (TItem)tv_Items.SelectedNode;
  518. if(selTItem!=null)
  519. {
  520. int position =selTItem.Index+1;
  521. IList coll;
  522. TreeNodeCollection TItemColl;
  523. if(selTItem.Parent!=null)
  524. {
  525. coll=(((TItem)selTItem.Parent).SubItems);
  526. TItemColl=selTItem.Parent.Nodes;
  527. }
  528. else
  529. {
  530. coll=Collection;
  531. TItemColl=tv_Items.Nodes;
  532. }
  533. coll.Insert(position,newCollItem);
  534. TItemColl.Insert(position, newTItem);
  535. }
  536. else //empty collection
  537. {
  538. Collection.Add(newCollItem);
  539. tv_Items.Nodes.Add(newTItem);
  540. }
  541. OnItemAdded(newCollItem);
  542. tv_Items.SelectedNode=newTItem;
  543. }
  544. tv_Items.EndUpdate();
  545. }
  546. private void btn_Remove_Click(object sender, System.EventArgs e)
  547. {
  548. tv_Items.BeginUpdate();
  549. TItem selTItem= (TItem)tv_Items.SelectedNode;
  550. if(selTItem!=null)
  551. {
  552. int selIndex=selTItem.Index;
  553. TItem parentTitem= (TItem)selTItem.Parent;
  554. if(parentTitem!=null)
  555. {
  556. parentTitem.Nodes.Remove(selTItem);
  557. parentTitem.SubItems.Remove(selTItem.Value);
  558. if(parentTitem.Nodes.Count>selIndex){tv_Items.SelectedNode=parentTitem.Nodes[selIndex];}
  559. else if(parentTitem.Nodes.Count>0){tv_Items.SelectedNode=parentTitem.Nodes[selIndex-1];}
  560. else{ tv_Items.SelectedNode=parentTitem;}
  561. }
  562. else
  563. {
  564. tv_Items.Nodes.Remove(selTItem);
  565. Collection.Remove(selTItem.Value);
  566. if(tv_Items.Nodes.Count>selIndex){tv_Items.SelectedNode=tv_Items.Nodes[selIndex];}
  567. else if(tv_Items.Nodes.Count>0){tv_Items.SelectedNode=tv_Items.Nodes[selIndex-1];}
  568. else{ this.pg_PropGrid.SelectedObject=null;}
  569. }
  570. OnItemRemoved(selTItem.Value);
  571. }
  572. tv_Items.EndUpdate();
  573. }
  574. private void btn_Up_Click(object sender, System.EventArgs e)
  575. {
  576. tv_Items.BeginUpdate();
  577. TItem selTItem=(TItem)tv_Items.SelectedNode;
  578. if(selTItem!=null && selTItem.PrevNode!=null)
  579. {
  580. int prevIndex=selTItem.PrevNode.Index;
  581. TItem fatherTitem=(TItem)selTItem.Parent;
  582. if(fatherTitem!=null)
  583. {
  584. MoveItem(fatherTitem.SubItems,fatherTitem.SubItems.IndexOf(selTItem.Value),-1);
  585. SetProperties(fatherTitem,fatherTitem.Value);
  586. tv_Items.SelectedNode=fatherTitem.Nodes[prevIndex];
  587. }
  588. else
  589. {
  590. MoveItem(Collection,Collection.IndexOf(selTItem.Value),-1);
  591. tv_Items.Nodes.Clear();
  592. tv_Items.Nodes.AddRange(GenerateTItemArray(this.Collection));
  593. tv_Items.SelectedNode=tv_Items.Nodes[prevIndex];
  594. }
  595. }
  596. tv_Items.EndUpdate();
  597. }
  598. private void btn_Down_Click(object sender, System.EventArgs e)
  599. {
  600. tv_Items.BeginUpdate();
  601. TItem selTItem=(TItem)tv_Items.SelectedNode;
  602. if(selTItem!=null && selTItem.NextNode!=null)
  603. {
  604. int nextIndex=selTItem.NextNode.Index;
  605. TItem fatherTitem=(TItem)selTItem.Parent;
  606. if(fatherTitem!=null)
  607. {
  608. MoveItem(fatherTitem.SubItems,fatherTitem.SubItems.IndexOf(selTItem.Value),1);
  609. SetProperties(fatherTitem,fatherTitem.Value);
  610. tv_Items.SelectedNode=fatherTitem.Nodes[nextIndex];
  611. }
  612. else
  613. {
  614. MoveItem(Collection,Collection.IndexOf(selTItem.Value),1);
  615. tv_Items.Nodes.Clear();
  616. tv_Items.Nodes.AddRange(GenerateTItemArray(this.Collection));
  617. tv_Items.SelectedNode=tv_Items.Nodes[nextIndex];
  618. }
  619. }
  620. tv_Items.EndUpdate();
  621. }
  622. private void pg_PropertyValueChanged(object s, System.Windows.Forms.PropertyValueChangedEventArgs e)
  623. {
  624. tv_Items.BeginUpdate();
  625. TItem selTItem=(TItem)tv_Items.SelectedNode;
  626. SetProperties(selTItem,selTItem.Value);
  627. tv_Items.EndUpdate();
  628. }
  629. private void tv_Items_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
  630. {
  631. pg_PropGrid.SelectedObject=((TItem)e.Node).Value;
  632. }
  633. private void tv_Items_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
  634. {
  635. TItem ti=(TItem)e.Node;
  636. if(ti.Value.GetType()!=lastItemType)
  637. {
  638. lastItemType=ti.Value.GetType();
  639. IList coll;
  640. if(ti.Parent!=null) {coll=((TItem)ti.Parent).SubItems;}
  641. else {coll=Collection;}
  642. ProccessCollection(coll);
  643. }
  644. }
  645. private void pg_PropGrid_SelectedGridItemChanged(object sender, System.Windows.Forms.SelectedGridItemChangedEventArgs e)
  646. {
  647. if (attachedEditor!=null)
  648. {
  649. attachedEditor.CollectionChanged-= new CustomCollectionEditor.CollectionChangedEventHandler(ValChanged);
  650. attachedEditor=null;
  651. }
  652. if ( e.NewSelection.Value is IList )
  653. {
  654. attachedEditor=(CustomCollectionEditor)e.NewSelection.PropertyDescriptor.GetEditor(typeof(System.Drawing.Design.UITypeEditor )) as CustomCollectionEditor;
  655. if (attachedEditor!=null)
  656. {
  657. attachedEditor.CollectionChanged+= new CustomCollectionEditor.CollectionChangedEventHandler(ValChanged);
  658. }
  659. }
  660. }
  661. private void ValChanged(object sender,object instance, object value )
  662. {
  663. tv_Items.BeginUpdate();
  664. TItem ti= (TItem)tv_Items.SelectedNode;
  665. SetProperties(ti,instance);
  666. tv_Items.EndUpdate();
  667. }
  668. private void UndoChanges(IList source, IList dest)
  669. {
  670. foreach(object o in dest )
  671. {
  672. if(!source.Contains(o))
  673. {
  674. DestroyInstance(o);
  675. OnItemRemoved(o);
  676. }
  677. }
  678. dest.Clear();
  679. CopyItems(source,dest);
  680. }
  681. private void CopyItems(IList source, IList dest)
  682. {
  683. foreach (object o in source)
  684. {
  685. dest.Add(o);
  686. OnItemAdded(o);
  687. }
  688. }
  689. protected virtual void OnEditLevelChanged(EventArgs e)
  690. {
  691. switch(EditLevel)
  692. {
  693. case EditLevel.FullEdit:
  694. {
  695. this.btn_Add.Enabled=true;
  696. this.btn_Remove.Enabled=true;
  697. break;
  698. }
  699. case EditLevel.AddOnly:
  700. {
  701. this.btn_Add.Enabled=true;
  702. this.btn_Remove.Enabled=false;
  703. break;
  704. }
  705. case EditLevel.RemoveOnly:
  706. {
  707. this.btn_Add.Enabled=false;
  708. this.btn_Remove.Enabled=true;
  709. break;
  710. }
  711. case EditLevel.ReadOnly:
  712. {
  713. this.btn_Add.Enabled=false;
  714. this.btn_Remove.Enabled=false;
  715. break;
  716. }
  717. }
  718. }
  719. protected override void OnResize(EventArgs e)
  720. {
  721. base.OnResize (e);
  722. this.spl_Splitter.SplitPosition=240;
  723. }
  724. #endregion
  725. }
  726. #region "TItem"
  727. public class TItem:TreeNode
  728. {
  729. private object _Value;
  730. private CustomCollectionEditorForm ced=null;
  731. private IList _SubItems=null;
  732. public object Value
  733. {
  734. get{return _Value;}
  735. set{_Value= value;}
  736. }
  737. public IList SubItems
  738. {
  739. get{return _SubItems;}
  740. set
  741. {
  742. _SubItems= value;
  743. this.Nodes.Clear();
  744. if (value !=null)
  745. {
  746. this.Nodes.AddRange(ced.GenerateTItemArray(value));
  747. }
  748. }
  749. }
  750. public TItem(CustomCollectionEditorForm ced,object Value)
  751. {
  752. this.ced=ced;
  753. this._Value=Value;
  754. }
  755. public TItem(CustomCollectionEditorForm ced,object Value, int ImageIndex)
  756. {
  757. this.ced=ced;
  758. this._Value=Value;
  759. this.ImageIndex=ImageIndex;
  760. }
  761. public TItem(CustomCollectionEditorForm ced,object Value, int ImageIndex, int SelectedImageIndex)
  762. {
  763. this.ced=ced;
  764. this._Value=Value;
  765. this.ImageIndex=ImageIndex;
  766. this.SelectedImageIndex=SelectedImageIndex;
  767. }
  768. }
  769. #endregion
  770. }