ValueTypeDialog.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using CommonLang.Property;
  10. using CommonLang;
  11. using CommonFroms.Utils;
  12. using CommonLang.Xml;
  13. using CommonFroms.G2D.DataGrid;
  14. using System.Reflection;
  15. namespace CommonFroms.DescAttributeEdit
  16. {
  17. /// <summary>
  18. /// 用于选择或创建,所有对应其子类的实例
  19. /// 该选择界面先对注释为 [DescAttribute] 的 Catgory 分类,再进行子类划分
  20. /// </summary>
  21. public partial class ValueTypeDialog : Form
  22. {
  23. private readonly Type baseType;
  24. private Catgorys triggerTypes;
  25. private object editValue;
  26. private HashMap<Type, object> editHistory = new HashMap<Type, object>();
  27. public ValueTypeDialog(Type baseType, string title, object edit, IG2DPropertyAdapter[] adapters)
  28. {
  29. this.baseType = baseType;
  30. this.triggerTypes = Catgorys.GetCatgory(baseType);
  31. InitializeComponent();
  32. this.SuspendLayout();
  33. {
  34. if (edit != null)
  35. {
  36. edit = XmlUtil.CloneObject<object>(edit);
  37. }
  38. this.Text = title;
  39. this.editValue = edit;
  40. this.valuePanel1.SetAdapters(adapters);
  41. foreach (string catgory in triggerTypes.CatgoryNames)
  42. {
  43. comboBoxCatgory.Items.Add(catgory);
  44. }
  45. if (editValue != null)
  46. {
  47. string catgory = TypeDescAttribute.GetCatgoryName(editValue.GetType());
  48. comboBoxCatgory.SelectedItem = catgory;
  49. List<TypeDescAttribute> types = triggerTypes.GetCatgoryTypes(catgory);
  50. if (types != null)
  51. {
  52. foreach (TypeDescAttribute type in types)
  53. {
  54. comboBoxType.Items.Add(type);
  55. }
  56. }
  57. comboBoxType.SelectedItem = triggerTypes.GetTypeDescAttribute(editValue.GetType());
  58. SetEditValue(editValue, false);
  59. }
  60. else if (triggerTypes.CatgoryNames.Count > 0)
  61. {
  62. comboBoxCatgory.SelectedItem = triggerTypes.CatgoryNames[0];
  63. List<TypeDescAttribute> types = triggerTypes.GetCatgoryTypes(triggerTypes.CatgoryNames[0]);
  64. if (types != null)
  65. {
  66. foreach (TypeDescAttribute type in types)
  67. {
  68. comboBoxType.Items.Add(type);
  69. }
  70. if (comboBoxType.Items.Count > 0)
  71. {
  72. comboBoxType.SelectedItem = comboBoxType.Items[0];
  73. Type selected = SelectedType;
  74. SetEditValue(selected);
  75. }
  76. }
  77. }
  78. this.comboBoxCatgory.SelectedIndexChanged += new System.EventHandler(this.comboBoxCatgory_SelectedIndexChanged);
  79. this.comboBoxType.SelectedIndexChanged += new System.EventHandler(this.comboBoxType_SelectedIndexChanged);
  80. }
  81. this.ResumeLayout();
  82. }
  83. private void comboBoxCatgory_SelectedIndexChanged(object sender, EventArgs e)
  84. {
  85. comboBoxType.Items.Clear();
  86. string name = comboBoxCatgory.SelectedItem + "";
  87. List<TypeDescAttribute> types = triggerTypes.GetCatgoryTypes(name);
  88. if (types != null)
  89. {
  90. foreach (TypeDescAttribute type in types)
  91. {
  92. comboBoxType.Items.Add(type);
  93. }
  94. if (comboBoxType.Items.Count > 0)
  95. {
  96. comboBoxType.SelectedItem = comboBoxType.Items[0];
  97. }
  98. }
  99. }
  100. private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
  101. {
  102. Type selected = SelectedType;
  103. if (selected != null && (editValue == null || selected != editValue.GetType()))
  104. {
  105. SetEditValue(selected);
  106. }
  107. }
  108. private void buttonOK_Click(object sender, EventArgs e)
  109. {
  110. }
  111. private void buttonCancel_Click(object sender, EventArgs e)
  112. {
  113. }
  114. private void SetEditValue(Type selected)
  115. {
  116. object value = null;
  117. bool is_new = false;
  118. if (!editHistory.TryGetValue(selected, out value))
  119. {
  120. is_new = true;
  121. value = ReflectionUtil.CreateInstance(SelectedType);
  122. }
  123. SetEditValue(value, is_new);
  124. }
  125. private void SetEditValue(object value, bool is_new)
  126. {
  127. if (is_new)
  128. {
  129. }
  130. this.editValue = value;
  131. valuePanel1.SetValue(editValue);
  132. editHistory.Put(editValue.GetType(), editValue);
  133. }
  134. public string SelectedCatgory
  135. {
  136. get { return comboBoxCatgory.SelectedItem + ""; }
  137. }
  138. public TypeDescAttribute SelectedTypeDesc
  139. {
  140. get { return comboBoxType.SelectedItem as TypeDescAttribute; }
  141. }
  142. public Type SelectedType
  143. {
  144. get
  145. {
  146. TypeDescAttribute td = SelectedTypeDesc;
  147. if (td != null)
  148. {
  149. return td.DataType;
  150. }
  151. return null;
  152. }
  153. }
  154. public object EditValue
  155. {
  156. get
  157. {
  158. return editValue;
  159. }
  160. }
  161. public static T ShowAddDialog<T>(
  162. IWin32Window owner,
  163. string title,
  164. params IG2DPropertyAdapter[] adapters) where T : class
  165. {
  166. ValueTypeDialog form = new ValueTypeDialog(typeof(T), title, null, adapters);
  167. if (form.ShowDialog(owner) == DialogResult.OK)
  168. {
  169. return (T)form.EditValue;
  170. }
  171. return null;
  172. }
  173. public static object ShowEditDialog(
  174. IWin32Window owner,
  175. string title,
  176. Type baseType,
  177. object edit,
  178. params IG2DPropertyAdapter[] adapters)
  179. {
  180. ValueTypeDialog form = new ValueTypeDialog(baseType, title, edit, adapters);
  181. if (form.ShowDialog(owner) == DialogResult.OK)
  182. {
  183. return form.EditValue;
  184. }
  185. return null;
  186. }
  187. public static object ShowValueDialog(
  188. IWin32Window owner,
  189. string title,
  190. Type baseType,
  191. object edit,
  192. params IG2DPropertyAdapter[] adapters)
  193. {
  194. ValueTypeDialog form = new ValueTypeDialog(baseType, title, edit, adapters);
  195. if (form.ShowDialog(owner) == DialogResult.OK)
  196. {
  197. return form.EditValue;
  198. }
  199. return null;
  200. }
  201. // class FieldHistory
  202. // {
  203. // class Entry
  204. // {
  205. // Type type;
  206. // string fieldName;
  207. // int fieldIndex;
  208. // object fieldValue;
  209. // }
  210. //
  211. // public void FindFieldValue(FieldInfo field)
  212. // {
  213. //
  214. // }
  215. // }
  216. }
  217. }