PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Design/System.ComponentModel.Design/CollectionEditor.cs

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