CollectionEditor.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. //
  2. // System.ComponentModel.Design.CollectionEditor
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Ivan N. Zlatev ([email protected])
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. // (C) 2007 Andreas Nahr
  11. // (C) 2007 Ivan N. Zlatev
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Reflection;
  35. using System.Collections;
  36. using System.ComponentModel;
  37. using System.Drawing.Design;
  38. using System.Windows.Forms;
  39. using System.Windows.Forms.Design;
  40. namespace System.ComponentModel.Design
  41. {
  42. public class CollectionEditor : UITypeEditor
  43. {
  44. protected abstract class CollectionForm : Form
  45. {
  46. private CollectionEditor editor;
  47. private object editValue;
  48. public CollectionForm (CollectionEditor editor)
  49. {
  50. this.editor = editor;
  51. }
  52. protected Type CollectionItemType
  53. {
  54. get { return editor.CollectionItemType; }
  55. }
  56. protected Type CollectionType
  57. {
  58. get { return editor.CollectionType; }
  59. }
  60. protected ITypeDescriptorContext Context
  61. {
  62. get { return editor.Context; }
  63. }
  64. public object EditValue
  65. {
  66. get { return editValue; }
  67. set
  68. {
  69. editValue = value;
  70. OnEditValueChanged ();
  71. }
  72. }
  73. protected object[] Items
  74. {
  75. get { return editor.GetItems (editValue); }
  76. set {
  77. if (editValue == null) {
  78. object newEmptyCollection = null;
  79. try {
  80. if (typeof (Array).IsAssignableFrom (CollectionType))
  81. newEmptyCollection = Array.CreateInstance (CollectionItemType, 0);
  82. else
  83. newEmptyCollection = Activator.CreateInstance (CollectionType);
  84. } catch {}
  85. object val = editor.SetItems (newEmptyCollection, value);
  86. if (val != newEmptyCollection)
  87. EditValue = val;
  88. } else {
  89. object val = editor.SetItems (editValue, value);
  90. if (val != editValue)
  91. EditValue = val;
  92. }
  93. }
  94. }
  95. protected Type[] NewItemTypes
  96. {
  97. get { return editor.NewItemTypes; }
  98. }
  99. protected bool CanRemoveInstance (object value)
  100. {
  101. return editor.CanRemoveInstance (value);
  102. }
  103. protected virtual bool CanSelectMultipleInstances ()
  104. {
  105. return editor.CanSelectMultipleInstances ();
  106. }
  107. protected object CreateInstance (Type itemType)
  108. {
  109. return editor.CreateInstance (itemType);
  110. }
  111. protected void DestroyInstance (object instance)
  112. {
  113. editor.DestroyInstance (instance);
  114. }
  115. protected virtual void DisplayError (Exception e)
  116. {
  117. MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
  118. }
  119. protected override object GetService (Type serviceType)
  120. {
  121. return editor.GetService (serviceType);
  122. }
  123. protected abstract void OnEditValueChanged ();
  124. protected internal virtual DialogResult ShowEditorDialog (IWindowsFormsEditorService edSvc)
  125. {
  126. return edSvc.ShowDialog (this);
  127. }
  128. }
  129. private class ConcreteCollectionForm : CollectionForm
  130. {
  131. internal class ObjectContainerConverter : TypeConverter
  132. {
  133. private class ObjectContainerPropertyDescriptor : TypeConverter.SimplePropertyDescriptor
  134. {
  135. private AttributeCollection attributes;
  136. public ObjectContainerPropertyDescriptor (Type componentType, Type propertyType)
  137. : base (componentType, "Value", propertyType)
  138. {
  139. CategoryAttribute cat = new CategoryAttribute (propertyType.Name);
  140. attributes = new AttributeCollection (new Attribute[] { cat });
  141. }
  142. public override object GetValue (object component)
  143. {
  144. ObjectContainer container = (ObjectContainer)component;
  145. return container.Object;
  146. }
  147. public override void SetValue (object component, object value)
  148. {
  149. ObjectContainer container = (ObjectContainer)component;
  150. container.Object = value;
  151. }
  152. public override AttributeCollection Attributes
  153. {
  154. get { return attributes; }
  155. }
  156. }
  157. public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
  158. {
  159. ObjectContainer container = (ObjectContainer)value;
  160. ObjectContainerPropertyDescriptor desc = new ObjectContainerPropertyDescriptor (value.GetType (), container.editor.CollectionItemType);
  161. PropertyDescriptor[] properties = new PropertyDescriptor[] { desc };
  162. PropertyDescriptorCollection pc = new PropertyDescriptorCollection (properties);
  163. return pc;
  164. }
  165. public override bool GetPropertiesSupported (ITypeDescriptorContext context)
  166. {
  167. return true;
  168. }
  169. }
  170. [TypeConverter (typeof (ObjectContainerConverter))]
  171. private class ObjectContainer
  172. {
  173. internal string Name;
  174. internal object Object;
  175. internal CollectionEditor editor;
  176. public ObjectContainer (string name, object obj, CollectionEditor editor)
  177. {
  178. this.Name = name;
  179. this.Object = obj;
  180. this.editor = editor;
  181. }
  182. public override string ToString ()
  183. {
  184. return Name;
  185. }
  186. }
  187. private class UpdateableListbox : ListBox
  188. {
  189. public void DoRefreshItem (int index)
  190. {
  191. base.RefreshItem (index);
  192. }
  193. }
  194. private CollectionEditor editor;
  195. private System.Windows.Forms.Label labelMember;
  196. private System.Windows.Forms.Label labelProperty;
  197. private UpdateableListbox itemsList;
  198. private System.Windows.Forms.PropertyGrid itemDisplay;
  199. private System.Windows.Forms.Button doClose;
  200. private System.Windows.Forms.Button moveUp;
  201. private System.Windows.Forms.Button moveDown;
  202. private System.Windows.Forms.Button doAdd;
  203. private System.Windows.Forms.Button doRemove;
  204. private System.Windows.Forms.Button doCancel;
  205. private System.Windows.Forms.ComboBox addType;
  206. public ConcreteCollectionForm (CollectionEditor editor)
  207. : base (editor)
  208. {
  209. this.editor = editor;
  210. this.labelMember = new System.Windows.Forms.Label ();
  211. this.labelProperty = new System.Windows.Forms.Label ();
  212. this.itemsList = new UpdateableListbox ();
  213. this.itemDisplay = new System.Windows.Forms.PropertyGrid ();
  214. this.doClose = new System.Windows.Forms.Button ();
  215. this.moveUp = new System.Windows.Forms.Button ();
  216. this.moveDown = new System.Windows.Forms.Button ();
  217. this.doAdd = new System.Windows.Forms.Button ();
  218. this.doRemove = new System.Windows.Forms.Button ();
  219. this.doCancel = new System.Windows.Forms.Button ();
  220. this.addType = new System.Windows.Forms.ComboBox ();
  221. this.SuspendLayout ();
  222. //
  223. // labelMember
  224. //
  225. this.labelMember.Location = new System.Drawing.Point (12, 9);
  226. this.labelMember.Size = new System.Drawing.Size (55, 13);
  227. this.labelMember.Text = "Members:";
  228. //
  229. // labelProperty
  230. //
  231. this.labelProperty.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  232. | System.Windows.Forms.AnchorStyles.Right)));
  233. this.labelProperty.Location = new System.Drawing.Point (172, 9);
  234. this.labelProperty.Size = new System.Drawing.Size (347, 13);
  235. this.labelProperty.Text = "Properties:";
  236. //
  237. // itemsList
  238. //
  239. this.itemsList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  240. | System.Windows.Forms.AnchorStyles.Left)));
  241. this.itemsList.HorizontalScrollbar = true;
  242. this.itemsList.Location = new System.Drawing.Point (12, 25);
  243. this.itemsList.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
  244. this.itemsList.Size = new System.Drawing.Size (120, 290);
  245. this.itemsList.TabIndex = 0;
  246. this.itemsList.SelectedIndexChanged += new System.EventHandler (this.itemsList_SelectedIndexChanged);
  247. //
  248. // itemDisplay
  249. //
  250. this.itemDisplay.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  251. | System.Windows.Forms.AnchorStyles.Left)
  252. | System.Windows.Forms.AnchorStyles.Right)));
  253. this.itemDisplay.HelpVisible = false;
  254. this.itemDisplay.Location = new System.Drawing.Point (175, 25);
  255. this.itemDisplay.Size = new System.Drawing.Size (344, 314);
  256. this.itemDisplay.TabIndex = 6;
  257. this.itemDisplay.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler (this.itemDisplay_PropertyValueChanged);
  258. //
  259. // doClose
  260. //
  261. this.doClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  262. this.doClose.Location = new System.Drawing.Point (341, 345);
  263. this.doClose.Size = new System.Drawing.Size (86, 26);
  264. this.doClose.TabIndex = 7;
  265. this.doClose.Text = "OK";
  266. this.doClose.Click += new System.EventHandler (this.doClose_Click);
  267. //
  268. // moveUp
  269. //
  270. this.moveUp.Location = new System.Drawing.Point (138, 25);
  271. this.moveUp.Size = new System.Drawing.Size (31, 28);
  272. this.moveUp.TabIndex = 4;
  273. this.moveUp.Text = "Up";
  274. this.moveUp.Click += new System.EventHandler (this.moveUp_Click);
  275. //
  276. // moveDown
  277. //
  278. this.moveDown.Location = new System.Drawing.Point (138, 59);
  279. this.moveDown.Size = new System.Drawing.Size (31, 28);
  280. this.moveDown.TabIndex = 5;
  281. this.moveDown.Text = "Dn";
  282. this.moveDown.Click += new System.EventHandler (this.moveDown_Click);
  283. //
  284. // doAdd
  285. //
  286. this.doAdd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  287. this.doAdd.Location = new System.Drawing.Point (12, 346);
  288. this.doAdd.Size = new System.Drawing.Size (59, 25);
  289. this.doAdd.TabIndex = 1;
  290. this.doAdd.Text = "Add";
  291. this.doAdd.Click += new System.EventHandler (this.doAdd_Click);
  292. //
  293. // doRemove
  294. //
  295. this.doRemove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  296. this.doRemove.Location = new System.Drawing.Point (77, 346);
  297. this.doRemove.Size = new System.Drawing.Size (55, 25);
  298. this.doRemove.TabIndex = 2;
  299. this.doRemove.Text = "Remove";
  300. this.doRemove.Click += new System.EventHandler (this.doRemove_Click);
  301. //
  302. // doCancel
  303. //
  304. this.doCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  305. this.doCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  306. this.doCancel.Location = new System.Drawing.Point (433, 345);
  307. this.doCancel.Size = new System.Drawing.Size (86, 26);
  308. this.doCancel.TabIndex = 8;
  309. this.doCancel.Text = "Cancel";
  310. this.doCancel.Click += new System.EventHandler (this.doCancel_Click);
  311. //
  312. // addType
  313. //
  314. this.addType.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  315. this.addType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  316. this.addType.Location = new System.Drawing.Point (12, 319);
  317. this.addType.Size = new System.Drawing.Size (120, 21);
  318. this.addType.TabIndex = 3;
  319. //
  320. // DesignerForm
  321. //
  322. this.AcceptButton = this.doClose;
  323. this.CancelButton = this.doCancel;
  324. this.ClientSize = new System.Drawing.Size (531, 381);
  325. this.ControlBox = false;
  326. this.Controls.Add (this.addType);
  327. this.Controls.Add (this.doCancel);
  328. this.Controls.Add (this.doRemove);
  329. this.Controls.Add (this.doAdd);
  330. this.Controls.Add (this.moveDown);
  331. this.Controls.Add (this.moveUp);
  332. this.Controls.Add (this.doClose);
  333. this.Controls.Add (this.itemDisplay);
  334. this.Controls.Add (this.itemsList);
  335. this.Controls.Add (this.labelProperty);
  336. this.Controls.Add (this.labelMember);
  337. this.HelpButton = true;
  338. this.MaximizeBox = false;
  339. this.MinimizeBox = false;
  340. this.MinimumSize = new System.Drawing.Size (400, 300);
  341. this.ShowInTaskbar = false;
  342. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  343. this.ResumeLayout (false);
  344. #if NET_2_0
  345. if (editor.CollectionType.IsGenericType)
  346. this.Text = editor.CollectionItemType.Name + " Collection Editor";
  347. else
  348. this.Text = editor.CollectionType.Name + " Collection Editor";
  349. #else
  350. this.Text = editor.CollectionType.Name + " Collection Editor";
  351. #endif
  352. foreach (Type type in editor.NewItemTypes)
  353. addType.Items.Add (type.Name);
  354. if (addType.Items.Count > 0)
  355. addType.SelectedIndex = 0;
  356. }
  357. private void AddItems ()
  358. {
  359. foreach (object o in editor.GetItems (EditValue))
  360. {
  361. this.itemsList.Items.Add (new ObjectContainer (editor.GetDisplayText (o), o, editor));
  362. }
  363. if (itemsList.Items.Count > 0)
  364. itemsList.SelectedIndex = 0;
  365. }
  366. private void doClose_Click (object sender, EventArgs e)
  367. {
  368. object[] items = new object[itemsList.Items.Count];
  369. for (int i = 0; i < itemsList.Items.Count; i++)
  370. items[i] = ((ObjectContainer)itemsList.Items[i]).Object;
  371. EditValue = editor.SetItems (EditValue, items);
  372. this.Close ();
  373. }
  374. private void doCancel_Click (object sender, EventArgs e)
  375. {
  376. editor.CancelChanges ();
  377. this.Close ();
  378. }
  379. private void itemsList_SelectedIndexChanged (object sender, EventArgs e)
  380. {
  381. if (itemsList.SelectedIndex <= 0 || itemsList.SelectedItems.Count > 1)
  382. moveUp.Enabled = false;
  383. else
  384. moveUp.Enabled = true;
  385. if (itemsList.SelectedIndex > itemsList.Items.Count - 2 || itemsList.SelectedItems.Count > 1)
  386. moveDown.Enabled = false;
  387. else
  388. moveDown.Enabled = true;
  389. if (itemsList.SelectedIndex == -1)
  390. return;
  391. if (itemsList.SelectedItems.Count == 1)
  392. {
  393. ObjectContainer o = (ObjectContainer)itemsList.SelectedItem;
  394. if (Type.GetTypeCode (o.Object.GetType ()) != TypeCode.Object)
  395. itemDisplay.SelectedObject = o;
  396. else
  397. itemDisplay.SelectedObject = o.Object;
  398. }
  399. else
  400. {
  401. object[] items = new object[itemsList.SelectedItems.Count];
  402. for (int i = 0; i < itemsList.SelectedItems.Count; i++)
  403. {
  404. ObjectContainer o = (ObjectContainer)itemsList.SelectedItem;
  405. if (Type.GetTypeCode (o.Object.GetType ()) != TypeCode.Object)
  406. items[i] = ((ObjectContainer)itemsList.SelectedItems[i]);
  407. else
  408. items[i] = ((ObjectContainer)itemsList.SelectedItems[i]).Object;
  409. }
  410. itemDisplay.SelectedObjects = items;
  411. }
  412. labelProperty.Text = ((ObjectContainer)itemsList.SelectedItem).Name + " properties:";
  413. }
  414. private void itemDisplay_PropertyValueChanged (object sender, EventArgs e)
  415. {
  416. int[] indices = new int[itemsList.SelectedIndices.Count];
  417. itemsList.SelectedIndices.CopyTo (indices, 0);
  418. for (int i = 0; i < indices.Length; i++)
  419. {
  420. ObjectContainer o = (ObjectContainer)itemsList.Items [indices[i]];
  421. o.Name = editor.GetDisplayText (o.Object);
  422. itemsList.DoRefreshItem (indices[i]);
  423. }
  424. for (int i = 0; i < indices.Length; i++)
  425. itemsList.SetSelected (indices[i], true);
  426. }
  427. private void moveUp_Click (object sender, EventArgs e)
  428. {
  429. if (itemsList.SelectedIndex <= 0)
  430. return;
  431. object selected = itemsList.SelectedItem;
  432. int index = itemsList.SelectedIndex;
  433. itemsList.Items.RemoveAt (index);
  434. itemsList.Items.Insert (index - 1, selected);
  435. itemsList.SelectedIndex = index - 1;
  436. }
  437. private void moveDown_Click (object sender, EventArgs e)
  438. {
  439. if (itemsList.SelectedIndex > itemsList.Items.Count - 2)
  440. return;
  441. object selected = itemsList.SelectedItem;
  442. int index = itemsList.SelectedIndex;
  443. itemsList.Items.RemoveAt (index);
  444. itemsList.Items.Insert (index + 1, selected);
  445. itemsList.SelectedIndex = index + 1;
  446. }
  447. private void doAdd_Click (object sender, EventArgs e)
  448. {
  449. object o;
  450. try
  451. {
  452. o = editor.CreateInstance (editor.NewItemTypes[addType.SelectedIndex]);
  453. }
  454. catch (Exception ex)
  455. {
  456. DisplayError (ex);
  457. return;
  458. }
  459. itemsList.Items.Add (new ObjectContainer (editor.GetDisplayText (o), o, editor));
  460. }
  461. private void doRemove_Click (object sender, EventArgs e)
  462. {
  463. if (itemsList.SelectedIndex != -1)
  464. itemsList.Items.RemoveAt (itemsList.SelectedIndex);
  465. }
  466. protected override void OnEditValueChanged ()
  467. {
  468. AddItems ();
  469. }
  470. }
  471. private Type type;
  472. private Type collectionItemType;
  473. private Type[] newItemTypes;
  474. private ITypeDescriptorContext context;
  475. private IServiceProvider provider;
  476. private IWindowsFormsEditorService editorService;
  477. public CollectionEditor (Type type)
  478. {
  479. this.type = type;
  480. this.collectionItemType = CreateCollectionItemType ();
  481. this.newItemTypes = CreateNewItemTypes ();
  482. }
  483. protected Type CollectionItemType
  484. {
  485. get { return collectionItemType; }
  486. }
  487. protected Type CollectionType
  488. {
  489. get { return type; }
  490. }
  491. protected ITypeDescriptorContext Context
  492. {
  493. get { return context; }
  494. }
  495. protected virtual string HelpTopic
  496. {
  497. get { return "CollectionEditor"; }
  498. }
  499. protected Type[] NewItemTypes
  500. {
  501. get { return newItemTypes; }
  502. }
  503. protected virtual void CancelChanges ()
  504. {
  505. }
  506. protected virtual bool CanRemoveInstance (object value)
  507. {
  508. return true;
  509. }
  510. protected virtual bool CanSelectMultipleInstances ()
  511. {
  512. return true;
  513. }
  514. protected virtual CollectionEditor.CollectionForm CreateCollectionForm ()
  515. {
  516. return new ConcreteCollectionForm (this);
  517. }
  518. protected virtual Type CreateCollectionItemType ()
  519. {
  520. PropertyInfo[] properties = type.GetProperties ();
  521. foreach (PropertyInfo property in properties)
  522. if (property.Name == "Item")
  523. return property.PropertyType;
  524. return typeof (object);
  525. }
  526. protected virtual object CreateInstance (Type itemType)
  527. {
  528. object instance = null;
  529. if (typeof (IComponent).IsAssignableFrom (itemType)) {
  530. IDesignerHost host = GetService (typeof (IDesignerHost)) as IDesignerHost;
  531. if (host != null)
  532. instance = host.CreateComponent (itemType);
  533. }
  534. if (instance == null) {
  535. #if NET_2_0
  536. instance = TypeDescriptor.CreateInstance (provider, itemType, null, null);
  537. #else
  538. instance = Activator.CreateInstance (itemType);
  539. #endif
  540. }
  541. return instance;
  542. }
  543. protected virtual Type[] CreateNewItemTypes ()
  544. {
  545. return new Type[] { collectionItemType };
  546. }
  547. protected virtual void DestroyInstance (object instance)
  548. {
  549. IComponent component = instance as IComponent;
  550. if (component != null) {
  551. IDesignerHost host = GetService (typeof (IDesignerHost)) as IDesignerHost;
  552. if (host != null)
  553. host.DestroyComponent (component);
  554. }
  555. }
  556. public override object EditValue (ITypeDescriptorContext context, IServiceProvider provider, object value)
  557. {
  558. this.context = context;
  559. this.provider = provider;
  560. if (context != null && provider != null)
  561. {
  562. editorService = (IWindowsFormsEditorService)provider.GetService (typeof (IWindowsFormsEditorService));
  563. if (editorService != null)
  564. {
  565. CollectionForm editorForm = CreateCollectionForm ();
  566. editorForm.EditValue = value;
  567. editorForm.ShowEditorDialog (editorService);
  568. return editorForm.EditValue;
  569. }
  570. }
  571. return base.EditValue (context, provider, value);
  572. }
  573. protected virtual string GetDisplayText (object value)
  574. {
  575. if (value == null)
  576. return string.Empty;
  577. PropertyInfo nameProperty = value.GetType ().GetProperty ("Name");
  578. if (nameProperty != null)
  579. {
  580. string data = (nameProperty.GetValue (value, null)) as string;
  581. if (data != null)
  582. if (data.Length != 0)
  583. return data;
  584. }
  585. if (Type.GetTypeCode (value.GetType ()) == TypeCode.Object)
  586. return value.GetType ().Name;
  587. else
  588. return value.ToString ();
  589. }
  590. public override UITypeEditorEditStyle GetEditStyle (ITypeDescriptorContext context)
  591. {
  592. return UITypeEditorEditStyle.Modal;
  593. }
  594. protected virtual object[] GetItems (object editValue)
  595. {
  596. if (editValue == null)
  597. return new object[0];
  598. ICollection collection = editValue as ICollection;
  599. if (collection == null)
  600. return new object[0];
  601. object[] result = new object[collection.Count];
  602. collection.CopyTo (result, 0);
  603. return result;
  604. }
  605. protected virtual IList GetObjectsFromInstance (object instance)
  606. {
  607. ArrayList list = new ArrayList ();
  608. list.Add (instance);
  609. return list;
  610. }
  611. protected object GetService (Type serviceType)
  612. {
  613. return context.GetService (serviceType);
  614. }
  615. protected virtual object SetItems (object editValue, object[] value)
  616. {
  617. IList list = (IList) editValue;
  618. if (list == null)
  619. return null;
  620. list.Clear ();
  621. foreach (object o in value)
  622. list.Add (o);
  623. return list;
  624. }
  625. protected virtual void ShowHelp ()
  626. {
  627. //TODO: Fixme Add parent and URL
  628. Help.ShowHelp (null, "", HelpTopic);
  629. }
  630. }
  631. }