2
0

CollectionEditor.cs 21 KB

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