G2DProperty.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. using System.Collections;
  6. using System.Reflection;
  7. using System.Drawing.Design;
  8. using System.Windows.Forms.Design;
  9. using CommonLang.Property;
  10. using System.Windows.Forms;
  11. using System.Globalization;
  12. using System.Drawing;
  13. using CommonLang;
  14. namespace CommonFroms.G2D.DataGrid
  15. {
  16. // --------------------------------------------------------------------------------------
  17. public interface IG2DPropertyAdapter
  18. {
  19. UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData);
  20. TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData);
  21. }
  22. public interface IG2DPropertyDescriptor
  23. {
  24. object ComponentData { get; }
  25. object FieldValue { get; }
  26. bool NotNull { get; }
  27. Type DecleardFieldType { get; }
  28. }
  29. public class G2DPropertyDescriptor : ICustomTypeDescriptor
  30. {
  31. static G2DPropertyDescriptor()
  32. {
  33. RegistPropertyAdapter(new InternalDataAdapters());
  34. }
  35. private object Data;
  36. private List<IG2DPropertyAdapter> adapter_list = new List<IG2DPropertyAdapter>();
  37. private IG2DPropertyAdapter[] add_adapters;
  38. public G2DPropertyDescriptor(object obj, params IG2DPropertyAdapter[] adds)
  39. {
  40. this.EnablePropertyInfo = false;
  41. this.EnableFieldInfo = true;
  42. this.Data = obj;
  43. this.add_adapters = adds;
  44. foreach (IG2DPropertyAdapter sa in static_adapters)
  45. {
  46. this.AddPropertyAdapter(sa);
  47. }
  48. foreach (IG2DPropertyAdapter sa in adds)
  49. {
  50. this.AddPropertyAdapter(sa);
  51. }
  52. }
  53. public object EditData
  54. {
  55. get { return Data; }
  56. internal set { Data = value; }
  57. }
  58. public bool EnablePropertyInfo { get; set; }
  59. public bool EnableFieldInfo { get; set; }
  60. public override string ToString()
  61. {
  62. return Data.ToString();
  63. }
  64. public void AddPropertyAdapter(IG2DPropertyAdapter adapter)
  65. {
  66. if (!adapter_list.Contains(adapter))
  67. {
  68. adapter_list.Add(adapter);
  69. }
  70. }
  71. //------------------------------------------------------------------
  72. #region ICustomTypeDescriptor 成员
  73. AttributeCollection ICustomTypeDescriptor.GetAttributes()
  74. {
  75. return TypeDescriptor.GetAttributes(Data, true);
  76. }
  77. string ICustomTypeDescriptor.GetClassName()
  78. {
  79. return TypeDescriptor.GetClassName(Data, true);
  80. }
  81. string ICustomTypeDescriptor.GetComponentName()
  82. {
  83. return TypeDescriptor.GetComponentName(Data, true);
  84. }
  85. TypeConverter ICustomTypeDescriptor.GetConverter()
  86. {
  87. return TypeDescriptor.GetConverter(Data, true);
  88. }
  89. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
  90. {
  91. return TypeDescriptor.GetDefaultEvent(Data, true);
  92. }
  93. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
  94. {
  95. return TypeDescriptor.GetDefaultProperty(Data, true);
  96. }
  97. object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
  98. {
  99. return TypeDescriptor.GetEditor(Data, editorBaseType, true);
  100. }
  101. EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
  102. {
  103. return TypeDescriptor.GetEvents(Data, attributes, true);
  104. }
  105. EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
  106. {
  107. return TypeDescriptor.GetEvents(Data, true);
  108. }
  109. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
  110. {
  111. return ListPropertyDescriptors(Data, this, attributes);
  112. }
  113. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
  114. {
  115. return ListPropertyDescriptors(Data, this, new Attribute[0]);
  116. }
  117. object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
  118. {
  119. return Data;
  120. }
  121. #endregion
  122. //------------------------------------------------------------------
  123. #region StaticUtils
  124. public static bool IsNeedG2DEditor(Type listType)
  125. {
  126. if (listType.IsArray && listType.GetArrayRank() == 1)
  127. {
  128. return IsNeedG2DCollectionEditor(listType);
  129. }
  130. else if (typeof(IList).IsAssignableFrom(listType))
  131. {
  132. return IsNeedG2DCollectionEditor(listType);
  133. }
  134. else if (typeof(IDictionary).IsAssignableFrom(listType))
  135. {
  136. return IsNeedG2DCollectionEditor(listType);
  137. }
  138. if (listType.Equals(typeof(string)))
  139. {
  140. return false;
  141. }
  142. if (listType.IsArray)
  143. {
  144. return false;
  145. }
  146. if (listType.IsPrimitive)
  147. {
  148. return false;
  149. }
  150. return true;
  151. }
  152. public static bool IsNeedG2DCollectionEditor(Type listType)
  153. {
  154. if (listType.IsArray && listType.GetArrayRank() == 1)
  155. {
  156. Type elementType = listType.GetElementType();
  157. return IsNeedG2DEditor(elementType);
  158. }
  159. else if (listType.GetInterface(typeof(IList).Name) != null)
  160. {
  161. if (listType.IsGenericType)
  162. {
  163. Type elementType = listType.GetGenericArguments()[0];
  164. return IsNeedG2DEditor(elementType);
  165. }
  166. else
  167. {
  168. return true;
  169. }
  170. }
  171. else if (listType.GetInterface(typeof(IDictionary).Name) != null)
  172. {
  173. if (listType.IsGenericType)
  174. {
  175. var gtypes = listType.GetGenericArguments();
  176. if (gtypes[0].IsPrimitive || gtypes[0] == typeof(string))
  177. {
  178. return IsNeedG2DEditor(gtypes[1]);
  179. }
  180. }
  181. }
  182. return false;
  183. }
  184. private static List<IG2DPropertyAdapter> static_adapters = new List<IG2DPropertyAdapter>();
  185. public static void RegistPropertyAdapter(IG2DPropertyAdapter adapter)
  186. {
  187. if (!static_adapters.Contains(adapter))
  188. {
  189. static_adapters.Add(adapter);
  190. }
  191. }
  192. public static GridItemCollection GetAllGridEntries(PropertyGrid grid)
  193. {
  194. if (grid == null)
  195. throw new ArgumentNullException("grid");
  196. object view = grid.GetType().GetField(
  197. "gridView",
  198. BindingFlags.NonPublic | BindingFlags.Instance).GetValue(grid);
  199. return (GridItemCollection)view.GetType().InvokeMember(
  200. "GetAllGridEntries",
  201. BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
  202. null, view, null);
  203. }
  204. public static void ToDescription(StringBuilder sb, object fieldValue)
  205. {
  206. if (fieldValue != null)
  207. {
  208. if (fieldValue.GetType().IsArray)
  209. {
  210. sb.AppendLine(CUtils.ArrayToString((Array)fieldValue));
  211. }
  212. else if (fieldValue.GetType().GetInterface(typeof(IDictionary).Name) != null)
  213. {
  214. sb.AppendLine(CUtils.MapToString((IDictionary)fieldValue));
  215. }
  216. else if (fieldValue.GetType().GetInterface(typeof(IList).Name) != null)
  217. {
  218. sb.AppendLine(CUtils.ListToString((IList)fieldValue));
  219. }
  220. else
  221. {
  222. if (fieldValue.GetType().IsEnum)
  223. {
  224. DescAttribute eda = PropertyUtil.GetEnumAttribute<DescAttribute>((Enum)fieldValue);
  225. if (eda != null)
  226. {
  227. sb.AppendLine(fieldValue + " : " + eda.Desc);
  228. }
  229. else
  230. {
  231. sb.AppendLine(fieldValue + "");
  232. }
  233. }
  234. else
  235. {
  236. sb.AppendLine(fieldValue + "");
  237. }
  238. }
  239. }
  240. }
  241. #endregion
  242. //--------------------------------------------------------------------------------------------------------------------------------------------
  243. public virtual PropertyDescriptorCollection ListPropertyDescriptors(object data, G2DPropertyDescriptor g2ddesc, Attribute[] attributes)
  244. {
  245. Type type = data.GetType();
  246. List<PropertyDescriptor> ret = new List<PropertyDescriptor>();
  247. if (type.IsArray && type.GetArrayRank() == 1)
  248. {
  249. Array array = data as Array;
  250. Type elementType = type.GetElementType();
  251. for (int i = 0; i < array.Length; i++)
  252. {
  253. ret.Add(new ArrayItemPropertyDescriptor(g2ddesc, array, i, attributes));
  254. }
  255. }
  256. else if (type.GetInterface(typeof(IList).Name) != null)
  257. {
  258. IList list = data as IList;
  259. if (type.IsGenericType)
  260. {
  261. Type elementType = type.GetGenericArguments()[0];
  262. for (int i = 0; i < list.Count; i++)
  263. {
  264. ret.Add(new ListItemPropertyDescriptor(g2ddesc, list, i, attributes));
  265. }
  266. }
  267. else
  268. {
  269. for (int i = 0; i < list.Count; i++)
  270. {
  271. ret.Add(new ListItemPropertyDescriptor(g2ddesc, list, i, attributes));
  272. }
  273. }
  274. }
  275. else if (type.GetInterface(typeof(IDictionary).Name) != null)
  276. {
  277. IDictionary map = data as IDictionary;
  278. if (type.IsGenericType)
  279. {
  280. foreach (var k in map.Keys)
  281. {
  282. ret.Add(new MapItemPropertyDescriptor(g2ddesc, map, k, attributes));
  283. }
  284. }
  285. }
  286. else if ((data == Data) && (type.IsEnum))
  287. {
  288. ret.Add(new EnumPropertyDescriptor(g2ddesc, Data, attributes));
  289. }
  290. else
  291. {
  292. if (EnableFieldInfo)
  293. {
  294. FieldInfo[] fields = type.GetFields();
  295. for (int i = 0; i < fields.Length; i++)
  296. {
  297. FieldInfo field = fields[i];
  298. DescAttribute desc = PropertyUtil.GetDesc(field);
  299. if (desc == null || desc.Editable)
  300. {
  301. Type fieldType = field.FieldType;
  302. object fieldData = field.GetValue(data);
  303. if (fieldData != null)
  304. {
  305. fieldType = fieldData.GetType();
  306. }
  307. ret.Add(new FieldMemberDescriptor(g2ddesc, i, field, data, attributes));
  308. }
  309. }
  310. }
  311. if (EnablePropertyInfo)
  312. {
  313. PropertyInfo[] props = type.GetProperties();
  314. for (int i = 0; i < props.Length; i++)
  315. {
  316. PropertyInfo field = props[i];
  317. DescAttribute desc = PropertyUtil.GetDesc(field);
  318. if (desc == null || desc.Editable)
  319. {
  320. Type fieldType = field.PropertyType;
  321. object fieldData = PropertyUtil.GetMemberValue(field, data); ;//field.GetValue(data);
  322. if (fieldData != null)
  323. {
  324. fieldType = fieldData.GetType();
  325. }
  326. ret.Add(new PropertyMemberDescriptor(g2ddesc, i, field, data, attributes));
  327. }
  328. }
  329. }
  330. }
  331. PropertyDescriptorCollection result = new PropertyDescriptorCollection(ret.ToArray(), true);
  332. ArrayList propertyNames = new ArrayList();
  333. foreach (PropertyDescriptor propertyAttributes in ret)
  334. {
  335. propertyNames.Add(propertyAttributes.DisplayName);
  336. }
  337. return result.Sort((string[])propertyNames.ToArray(typeof(string)));
  338. }
  339. //--------------------------------------------------------------------------------------------------------------------------------------------
  340. public abstract class MemberPropertyDescriptor<M> : PropertyDescriptor, IG2DPropertyDescriptor where M : MemberInfo
  341. {
  342. public readonly int index;
  343. public readonly G2DPropertyDescriptor g2ddesc;
  344. protected readonly M field;
  345. protected readonly Type fieldType;
  346. protected readonly object componentData;
  347. protected readonly Type componentType;
  348. private DescAttribute desc;
  349. private DependOnPropertyAttribute[] depends;
  350. private TypeConverterAttribute[] type_converters;
  351. private ExpandableAttribute[] expandables;
  352. private ExpandableAttribute[] expandables_f;
  353. private NotNullAttribute notNull;
  354. public MemberPropertyDescriptor(G2DPropertyDescriptor g2ddesc, int index, M field, object componentData, Attribute[] attributes)
  355. : base(field.Name, attributes)
  356. {
  357. this.g2ddesc = g2ddesc;
  358. this.index = index;
  359. this.componentData = componentData;
  360. this.componentType = componentData.GetType();
  361. this.field = field;
  362. this.fieldType = DecleardFieldType;
  363. this.desc = PropertyUtil.GetDesc(field);
  364. object fieldValue = this.GetValue(componentData);
  365. if (fieldValue != null)
  366. {
  367. fieldType = fieldValue.GetType();
  368. }
  369. this.depends = (DependOnPropertyAttribute[])field.GetCustomAttributes(typeof(DependOnPropertyAttribute), false);
  370. this.type_converters = (TypeConverterAttribute[])fieldType.GetCustomAttributes(typeof(TypeConverterAttribute), true);
  371. this.expandables = (ExpandableAttribute[])fieldType.GetCustomAttributes(typeof(ExpandableAttribute), true);
  372. this.expandables_f = (ExpandableAttribute[])field.GetCustomAttributes(typeof(ExpandableAttribute), true);
  373. this.notNull = PropertyUtil.GetAttribute<NotNullAttribute>(field);
  374. }
  375. public abstract Type DecleardFieldType { get; }
  376. public M Field { get { return field; } }
  377. public object ComponentData { get { return componentData; } }
  378. public bool NotNull { get { return notNull != null; } }
  379. public object FieldValue { get { return this.GetValue(componentData); } }
  380. public override Type ComponentType { get { return componentType; } }
  381. public override bool IsReadOnly
  382. {
  383. get
  384. {
  385. if (depends != null && depends.Length > 0)
  386. {
  387. foreach (DependOnPropertyAttribute depend in depends)
  388. {
  389. bool expect = PropertyUtil.GetFieldOrPropertyOrMethodValue<bool>(componentData, depend.PropertyName);
  390. if (expect != depend.Expect)
  391. {
  392. return true;
  393. }
  394. }
  395. }
  396. return false;
  397. }
  398. }
  399. public override Type PropertyType
  400. {
  401. get { return (IsReadOnly) ? typeof(string) : fieldType; }
  402. }
  403. public override string DisplayName
  404. {
  405. get { return base.DisplayName; }
  406. }
  407. public override string Description
  408. {
  409. get
  410. {
  411. StringBuilder sb = new StringBuilder();
  412. if (desc != null)
  413. {
  414. sb.AppendLine(desc.Desc);
  415. }
  416. object fieldValue = this.GetValue(componentData);
  417. ToDescription(sb, fieldValue);
  418. sb.AppendLine(base.Description);
  419. return sb.ToString();
  420. }
  421. }
  422. public override string Category
  423. {
  424. get
  425. {
  426. if (desc != null)
  427. {
  428. return desc.Category;
  429. }
  430. return base.Category;
  431. }
  432. }
  433. public override bool CanResetValue(object component)
  434. {
  435. return false;
  436. }
  437. public override void ResetValue(object component)
  438. {
  439. }
  440. public override bool ShouldSerializeValue(object component)
  441. {
  442. return false;
  443. }
  444. public override object GetEditor(Type editorBaseType)
  445. {
  446. if (IsReadOnly) return null;
  447. foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
  448. {
  449. UITypeEditor editor = adapter.GetEditor(field, DecleardFieldType, componentData);
  450. if (editor != null)
  451. {
  452. return editor;
  453. }
  454. }
  455. if (IsNeedG2DCollectionEditor(fieldType))
  456. {
  457. return new G2DFieldMemberEditor(field, this.GetValue(componentData), true, g2ddesc.add_adapters);
  458. }
  459. else if (IsNeedG2DEditor(fieldType))
  460. {
  461. return new G2DFieldMemberEditor(field, this.GetValue(componentData), false, g2ddesc.add_adapters);
  462. }
  463. return base.GetEditor(editorBaseType);
  464. }
  465. public override TypeConverter Converter
  466. {
  467. get
  468. {
  469. if (IsReadOnly) return null;
  470. if (type_converters.Length > 0)
  471. {
  472. return base.Converter;
  473. }
  474. foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
  475. {
  476. TypeConverter converter = adapter.GetConverter(field, DecleardFieldType, componentData);
  477. if (converter != null)
  478. {
  479. return converter;
  480. }
  481. }
  482. if (expandables.Length > 0)
  483. {
  484. return new G2DUIFieldExpandableConverter(g2ddesc);
  485. }
  486. if (expandables_f.Length > 0)
  487. {
  488. return new G2DUIFieldExpandableConverter(g2ddesc);
  489. }
  490. if (IsNeedG2DCollectionEditor(fieldType))
  491. {
  492. return new G2DUIFieldListConverter(g2ddesc);
  493. }
  494. return base.Converter;
  495. }
  496. }
  497. }
  498. public class FieldMemberDescriptor : MemberPropertyDescriptor<FieldInfo>
  499. {
  500. public FieldMemberDescriptor(G2DPropertyDescriptor g2ddesc, int index, FieldInfo field, object componentData, Attribute[] attributes)
  501. : base(g2ddesc, index, field, componentData, attributes)
  502. {
  503. }
  504. public override Type DecleardFieldType
  505. {
  506. get { return Field.FieldType; }
  507. }
  508. public override object GetValue(object component)
  509. {
  510. return field.GetValue(component);
  511. }
  512. public override void SetValue(object component, object value)
  513. {
  514. field.SetValue(component, value);
  515. }
  516. }
  517. public class PropertyMemberDescriptor : MemberPropertyDescriptor<PropertyInfo>
  518. {
  519. protected readonly MethodInfo set_op;
  520. protected readonly MethodInfo get_op;
  521. public PropertyMemberDescriptor(G2DPropertyDescriptor g2ddesc, int index, PropertyInfo field, object componentData, Attribute[] attributes)
  522. : base(g2ddesc, index, field, componentData, attributes)
  523. {
  524. this.get_op = field.GetGetMethod();
  525. this.set_op = field.GetSetMethod();
  526. }
  527. public override bool IsReadOnly
  528. {
  529. get
  530. {
  531. if (set_op == null) return true;
  532. return base.IsReadOnly;
  533. }
  534. }
  535. public override Type DecleardFieldType
  536. {
  537. get { return Field.PropertyType; }
  538. }
  539. public override object GetValue(object component)
  540. {
  541. if (get_op != null) return get_op.Invoke(component, new object[] { });
  542. return null;
  543. }
  544. public override void SetValue(object component, object value)
  545. {
  546. if (set_op != null) set_op.Invoke(component, new object[] { value });
  547. }
  548. }
  549. //------------------------------------------------------------------
  550. /*
  551. public class CustomPropertyDescriptor : PropertyDescriptor, IG2DPropertyDescriptor
  552. {
  553. public readonly int index;
  554. public readonly G2DPropertyDescriptor g2ddesc;
  555. private FieldInfo field;
  556. private Type fieldType;
  557. private object componentData;
  558. private Type componentType;
  559. private DescAttribute desc;
  560. private DependOnPropertyAttribute[] depends;
  561. private TypeConverterAttribute[] type_converters;
  562. private ExpandableAttribute[] expandables;
  563. private ExpandableAttribute[] expandables_f;
  564. private NotNullAttribute notNull;
  565. public CustomPropertyDescriptor(
  566. G2DPropertyDescriptor g2ddesc,
  567. int index,
  568. FieldInfo field,
  569. object componentData,
  570. Attribute[] attributes)
  571. : base(field.Name, attributes)
  572. {
  573. this.g2ddesc = g2ddesc;
  574. this.index = index;
  575. this.field = field;
  576. this.fieldType = field.FieldType;
  577. this.componentData = componentData;
  578. this.componentType = componentData.GetType();
  579. this.desc = PropertyUtil.GetDesc(field);
  580. object fieldValue = field.GetValue(componentData);
  581. if (fieldValue != null)
  582. {
  583. fieldType = fieldValue.GetType();
  584. }
  585. this.depends = (DependOnPropertyAttribute[])field.GetCustomAttributes(typeof(DependOnPropertyAttribute), false);
  586. this.type_converters = (TypeConverterAttribute[])fieldType.GetCustomAttributes(typeof(TypeConverterAttribute), true);
  587. this.expandables = (ExpandableAttribute[])fieldType.GetCustomAttributes(typeof(ExpandableAttribute), true);
  588. this.expandables_f = (ExpandableAttribute[])field.GetCustomAttributes(typeof(ExpandableAttribute), true);
  589. this.notNull = PropertyUtil.GetAttribute<NotNullAttribute>(field);
  590. }
  591. public object ComponentData { get { return componentData; } }
  592. public bool NotNull { get { return notNull != null; } }
  593. public Type DecleardFieldType { get { return field.FieldType; } }
  594. public object FieldValue { get { return field.GetValue(componentData); } }
  595. public override Type ComponentType { get { return componentType; } }
  596. public override bool IsReadOnly
  597. {
  598. get
  599. {
  600. if (depends != null && depends.Length > 0)
  601. {
  602. foreach (DependOnPropertyAttribute depend in depends)
  603. {
  604. bool expect = PropertyUtil.GetFieldOrPropertyOrMethodValue<bool>(componentData, depend.PropertyName);
  605. if (expect != depend.Expect)
  606. {
  607. return true;
  608. }
  609. }
  610. }
  611. return false;
  612. }
  613. }
  614. public override Type PropertyType
  615. {
  616. get
  617. {
  618. if (IsReadOnly) return typeof(string);
  619. return fieldType;
  620. }
  621. }
  622. public override string DisplayName
  623. {
  624. get
  625. {
  626. return base.DisplayName;
  627. }
  628. }
  629. public override string Description
  630. {
  631. get
  632. {
  633. StringBuilder sb = new StringBuilder();
  634. if (desc != null)
  635. {
  636. sb.AppendLine(desc.Desc);
  637. }
  638. object fieldValue = field.GetValue(componentData);
  639. ToDescription(sb, fieldValue);
  640. sb.AppendLine(base.Description);
  641. return sb.ToString();
  642. }
  643. }
  644. public override string Category
  645. {
  646. get
  647. {
  648. if (desc != null)
  649. {
  650. return desc.Category;
  651. }
  652. return base.Category;
  653. }
  654. }
  655. public override bool CanResetValue(object component)
  656. {
  657. return false;
  658. }
  659. public override void ResetValue(object component)
  660. {
  661. }
  662. public override object GetValue(object component)
  663. {
  664. return field.GetValue(component);
  665. }
  666. public override void SetValue(object component, object value)
  667. {
  668. field.SetValue(component, value);
  669. }
  670. public override bool ShouldSerializeValue(object component)
  671. {
  672. return false;
  673. }
  674. public override object GetEditor(Type editorBaseType)
  675. {
  676. if (IsReadOnly) return null;
  677. foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
  678. {
  679. UITypeEditor editor = adapter.GetEditor(field, componentData);
  680. if (editor != null)
  681. {
  682. return editor;
  683. }
  684. }
  685. if (IsNeedG2DCollectionEditor(fieldType))
  686. {
  687. return new G2DFieldMemberEditor(field, field.GetValue(componentData), true, g2ddesc.add_adapters);
  688. }
  689. else if (IsNeedG2DEditor(fieldType))
  690. {
  691. return new G2DFieldMemberEditor(field, field.GetValue(componentData), false, g2ddesc.add_adapters);
  692. }
  693. return base.GetEditor(editorBaseType);
  694. }
  695. public override TypeConverter Converter
  696. {
  697. get
  698. {
  699. if (IsReadOnly) return null;
  700. if (type_converters.Length > 0)
  701. {
  702. return base.Converter;
  703. }
  704. foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
  705. {
  706. TypeConverter converter = adapter.GetConverter(field, componentData);
  707. if (converter != null)
  708. {
  709. return converter;
  710. }
  711. }
  712. if (expandables.Length > 0)
  713. {
  714. return new G2DUIFieldExpandableConverter(g2ddesc);
  715. }
  716. if (expandables_f.Length > 0)
  717. {
  718. return new G2DUIFieldExpandableConverter(g2ddesc);
  719. }
  720. if (IsNeedG2DCollectionEditor(fieldType))
  721. {
  722. return new G2DUIFieldListConverter(g2ddesc);
  723. }
  724. return base.Converter;
  725. }
  726. }
  727. }
  728. */
  729. //------------------------------------------------------------------
  730. public class EnumPropertyDescriptor : PropertyDescriptor
  731. {
  732. public readonly G2DPropertyDescriptor g2ddesc;
  733. private object componentData;
  734. private Type componentType;
  735. public EnumPropertyDescriptor(
  736. G2DPropertyDescriptor g2d,
  737. object componentData,
  738. Attribute[] attributes)
  739. : base("Value", attributes)
  740. {
  741. this.g2ddesc = g2d;
  742. this.componentData = componentData;
  743. this.componentType = componentData.GetType();
  744. }
  745. public override Type ComponentType
  746. {
  747. get { return componentType; }
  748. }
  749. public override bool IsReadOnly
  750. {
  751. get { return false; }
  752. }
  753. public override Type PropertyType
  754. {
  755. get { return componentType; }
  756. }
  757. public override bool CanResetValue(object component)
  758. {
  759. return false;
  760. }
  761. public override object GetValue(object component)
  762. {
  763. return componentData;
  764. }
  765. public override void ResetValue(object component)
  766. {
  767. }
  768. public override void SetValue(object component, object value)
  769. {
  770. componentData = value;
  771. g2ddesc.EditData = value;
  772. }
  773. public override bool ShouldSerializeValue(object component)
  774. {
  775. return true;
  776. }
  777. }
  778. //------------------------------------------------------------------
  779. public abstract class CollectionItemPropertyDescriptor : PropertyDescriptor, IG2DPropertyDescriptor
  780. {
  781. public int Index { get; private set; }
  782. public G2DPropertyDescriptor Descriptor { get; private set; }
  783. public object ListOwner { get; private set; }
  784. public object ComponentData { get { return ListOwner; } }
  785. public object FieldValue { get { return ElementItem; } }
  786. public abstract object ElementItem { get; }
  787. public abstract Type ElementType { get; }
  788. public CollectionItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, object listOwner, int index, Attribute[] attributes)
  789. : base(index.ToString(), attributes)
  790. {
  791. this.Descriptor = g2ddesc;
  792. this.Index = index;
  793. this.ListOwner = listOwner;
  794. }
  795. public abstract Type DecleardFieldType { get; }
  796. public override Type ComponentType { get { return ListOwner.GetType(); } }
  797. public bool NotNull { get { return false; } }
  798. public override bool IsReadOnly { get { return false; } }
  799. public override Type PropertyType
  800. {
  801. get
  802. {
  803. if (ElementItem != null) return ElementItem.GetType();
  804. return ElementType;
  805. }
  806. }
  807. public override string Description
  808. {
  809. get
  810. {
  811. StringBuilder sb = new StringBuilder();
  812. ToDescription(sb, ElementItem);
  813. sb.AppendLine(base.Description);
  814. return sb.ToString();
  815. }
  816. }
  817. public override bool CanResetValue(object component)
  818. {
  819. return false;
  820. }
  821. public override void ResetValue(object component)
  822. {
  823. }
  824. public override bool ShouldSerializeValue(object component)
  825. {
  826. return false;
  827. }
  828. public override object GetEditor(Type editorBaseType)
  829. {
  830. if (IsNeedG2DEditor(PropertyType))
  831. {
  832. return new G2DCollectionItemEditor(ElementItem, ElementType, Descriptor.add_adapters);
  833. }
  834. return base.GetEditor(editorBaseType);
  835. }
  836. public override TypeConverter Converter
  837. {
  838. get
  839. {
  840. if (PropertyType.GetCustomAttributes(typeof(ExpandableAttribute), true).Length > 0)
  841. {
  842. return new G2DUIFieldExpandableConverter(Descriptor);
  843. }
  844. return base.Converter;
  845. }
  846. }
  847. }
  848. //------------------------------------------------------------------
  849. public class ListItemPropertyDescriptor : CollectionItemPropertyDescriptor
  850. {
  851. private readonly Type genericItemType;
  852. public override object ElementItem { get { return GetValue(ListOwner); } }
  853. public override Type ElementType { get { return genericItemType; } }
  854. public override Type DecleardFieldType { get { return genericItemType; } }
  855. public ListItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, IList listOwner, int index, Attribute[] attributes)
  856. : base(g2ddesc, listOwner, index, attributes)
  857. {
  858. Type listtype = listOwner.GetType();
  859. if (listtype.IsGenericType)
  860. {
  861. this.genericItemType = listOwner.GetType().GetGenericArguments()[0];
  862. }
  863. else
  864. {
  865. this.genericItemType = typeof(object);
  866. }
  867. }
  868. public override object GetValue(object component)
  869. {
  870. IList list = component as IList;
  871. if (Index >= 0 && Index < list.Count)
  872. {
  873. return list[Index];
  874. }
  875. return null;
  876. }
  877. public override void SetValue(object component, object value)
  878. {
  879. IList list = component as IList;
  880. if (Index >= 0 && Index < list.Count)
  881. {
  882. list[Index] = value;
  883. }
  884. }
  885. }
  886. //------------------------------------------------------------------
  887. public class ArrayItemPropertyDescriptor : CollectionItemPropertyDescriptor
  888. {
  889. private readonly Type genericItemType;
  890. public override object ElementItem { get { return GetValue(ListOwner); } }
  891. public override Type ElementType { get { return genericItemType; } }
  892. public override Type DecleardFieldType { get { return genericItemType; } }
  893. public ArrayItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, IList listOwner, int index, Attribute[] attributes)
  894. : base(g2ddesc, listOwner, index, attributes)
  895. {
  896. this.genericItemType = listOwner.GetType().GetElementType();
  897. }
  898. public override object GetValue(object component)
  899. {
  900. Array list = component as Array;
  901. if (Index >= 0 && Index < list.Length)
  902. {
  903. return list.GetValue(Index);
  904. }
  905. return null;
  906. }
  907. public override void SetValue(object component, object value)
  908. {
  909. Array list = component as Array;
  910. if (Index >= 0 && Index < list.Length)
  911. {
  912. list.SetValue(value, Index);
  913. }
  914. }
  915. }
  916. //------------------------------------------------------------------
  917. public class MapItemPropertyDescriptor : PropertyDescriptor, IG2DPropertyDescriptor
  918. {
  919. private readonly G2DPropertyDescriptor g2ddesc;
  920. private readonly IDictionary mapOwner;
  921. private readonly object key;
  922. private readonly Type[] genericArgs;
  923. public G2DPropertyDescriptor Descriptor { get { return g2ddesc; } }
  924. public IDictionary MapOwner { get { return mapOwner; } }
  925. public object ComponentData { get { return mapOwner; } }
  926. public Type DecleardFieldType { get { return genericArgs[1]; } }
  927. public object FieldValue { get { return GetValue(mapOwner); } }
  928. public bool NotNull { get { return true; } }
  929. public object Key { get { return key; } }
  930. public object Value { get { return GetValue(mapOwner); } }
  931. public override Type ComponentType { get { return MapOwner.GetType(); } }
  932. public override bool IsReadOnly { get { return false; } }
  933. public override Type PropertyType
  934. {
  935. get
  936. {
  937. var value = Value;
  938. if (value != null) return value.GetType();
  939. return DecleardFieldType;
  940. }
  941. }
  942. public override string Description
  943. {
  944. get
  945. {
  946. StringBuilder sb = new StringBuilder();
  947. ToDescription(sb, Value);
  948. sb.AppendLine(base.Description);
  949. return sb.ToString();
  950. }
  951. }
  952. public MapItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, IDictionary mapOwner, object key, Attribute[] attributes)
  953. : base(key.ToString(), attributes)
  954. {
  955. this.g2ddesc = g2ddesc;
  956. this.key = key;
  957. this.mapOwner = mapOwner;
  958. this.genericArgs = mapOwner.GetType().GetGenericArguments();
  959. }
  960. public override object GetValue(object component)
  961. {
  962. return mapOwner[key];
  963. }
  964. public override void SetValue(object component, object value)
  965. {
  966. mapOwner[key] = value;
  967. }
  968. public override bool CanResetValue(object component)
  969. {
  970. return false;
  971. }
  972. public override void ResetValue(object component)
  973. {
  974. }
  975. public override bool ShouldSerializeValue(object component)
  976. {
  977. return false;
  978. }
  979. public override object GetEditor(Type editorBaseType)
  980. {
  981. var value = Value;
  982. var keyType = key.GetType();
  983. var valueType = DecleardFieldType;
  984. if (value != null)
  985. {
  986. valueType = value.GetType();
  987. }
  988. if (keyType.IsPrimitive || keyType == typeof(string))
  989. {
  990. if (IsNeedG2DEditor(valueType))
  991. {
  992. return new G2DCollectionItemEditor(value, valueType, Descriptor.add_adapters);
  993. }
  994. }
  995. return null;
  996. }
  997. public override TypeConverter Converter
  998. {
  999. get
  1000. {
  1001. if (PropertyType.GetCustomAttributes(typeof(ExpandableAttribute), true).Length > 0)
  1002. {
  1003. return new G2DUIFieldExpandableConverter(Descriptor);
  1004. }
  1005. return base.Converter;
  1006. }
  1007. }
  1008. }
  1009. }
  1010. //--------------------------------------------------------------------------------------------------------------------------------
  1011. /// <summary>
  1012. /// 内部DataAdapters
  1013. /// </summary>
  1014. public class InternalDataAdapters : IG2DPropertyAdapter
  1015. {
  1016. public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData)
  1017. {
  1018. if (PropertyUtil.GetAttribute<DirectoryPathAttribute>(field) != null && fieldType == typeof(string))
  1019. {
  1020. return new G2DDirectoryDialogEditor(field, ownerData);
  1021. }
  1022. else if (PropertyUtil.GetAttribute<FilePathAttribute>(field) != null && fieldType == typeof(string))
  1023. {
  1024. return new G2DFileDialogEditor(field, ownerData);
  1025. }
  1026. else if (PropertyUtil.GetAttribute<Int32ColorAttribute>(field) != null && fieldType == typeof(int))
  1027. {
  1028. return new G2DColorEditor(field, ownerData);
  1029. }
  1030. else if (PropertyUtil.GetAttribute<OptionalValueAttribute>(field) != null && fieldType == typeof(string))
  1031. {
  1032. return new G2DOptionalEditor(field, ownerData);
  1033. }
  1034. return null;
  1035. }
  1036. public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData)
  1037. {
  1038. if (PropertyUtil.GetAttribute<Int32ColorAttribute>(field) != null && fieldType == typeof(int))
  1039. {
  1040. return new G2DIntToHexTypeConverter();
  1041. }
  1042. return null;
  1043. }
  1044. }
  1045. //--------------------------------------------------------------------------------------------------------------------------------
  1046. #region _TypeConverter_
  1047. public class G2DUIFieldListConverter : TypeConverter
  1048. {
  1049. private readonly G2DPropertyDescriptor g2ddesc;
  1050. public G2DUIFieldListConverter(G2DPropertyDescriptor g2d)
  1051. {
  1052. g2ddesc = g2d;
  1053. }
  1054. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  1055. {
  1056. if (destinationType.GetInterface(typeof(ICollection).FullName) != null)
  1057. {
  1058. return true;
  1059. }
  1060. if (destinationType.IsArray)
  1061. {
  1062. return true;
  1063. }
  1064. return false;
  1065. }
  1066. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  1067. {
  1068. if (value is ICollection)
  1069. {
  1070. ICollection list = (ICollection)value;
  1071. return "集合:[" + list.Count + "]";
  1072. }
  1073. if (destinationType.IsArray)
  1074. {
  1075. StringBuilder sb = new StringBuilder();
  1076. sb.Append("数组:");
  1077. Array array = value as Array;
  1078. int[] ranks = new int[array.Rank];
  1079. foreach (int len in ranks)
  1080. {
  1081. sb.Append("[" + len + "]");
  1082. }
  1083. return sb.ToString();
  1084. }
  1085. return base.ConvertTo(context, culture, value, destinationType);
  1086. }
  1087. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  1088. {
  1089. return g2ddesc.ListPropertyDescriptors(value, g2ddesc, attributes);
  1090. }
  1091. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  1092. {
  1093. return true;
  1094. }
  1095. }
  1096. //--------------------------------------------------------------------------------------
  1097. public class G2DUIFieldExpandableConverter : ExpandableObjectConverter
  1098. {
  1099. private readonly G2DPropertyDescriptor g2ddesc;
  1100. public G2DUIFieldExpandableConverter(G2DPropertyDescriptor g2d)
  1101. {
  1102. g2ddesc = g2d;
  1103. }
  1104. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  1105. {
  1106. return g2ddesc.ListPropertyDescriptors(value, g2ddesc, attributes);
  1107. }
  1108. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  1109. {
  1110. return true;
  1111. }
  1112. }
  1113. // --------------------------------------------------------------------------------------
  1114. public class G2DIntToHexTypeConverter : TypeConverter
  1115. {
  1116. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  1117. {
  1118. if (sourceType == typeof(string))
  1119. {
  1120. return true;
  1121. }
  1122. else
  1123. {
  1124. return base.CanConvertFrom(context, sourceType);
  1125. }
  1126. }
  1127. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  1128. {
  1129. if (destinationType == typeof(string))
  1130. {
  1131. return true;
  1132. }
  1133. else
  1134. {
  1135. return base.CanConvertTo(context, destinationType);
  1136. }
  1137. }
  1138. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  1139. {
  1140. if (destinationType == typeof(string) && value.GetType() == typeof(int))
  1141. {
  1142. return string.Format("0x{0:X8}", value);
  1143. }
  1144. else
  1145. {
  1146. return base.ConvertTo(context, culture, value, destinationType);
  1147. }
  1148. }
  1149. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  1150. {
  1151. if (value.GetType() == typeof(string))
  1152. {
  1153. string input = (string)value;
  1154. if (input.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
  1155. {
  1156. input = input.Substring(2);
  1157. }
  1158. return int.Parse(input, NumberStyles.HexNumber, culture);
  1159. }
  1160. else
  1161. {
  1162. return base.ConvertFrom(context, culture, value);
  1163. }
  1164. }
  1165. }
  1166. //--------------------------------------------------------------------------------------
  1167. #endregion
  1168. //--------------------------------------------------------------------------------------------------------------------------------
  1169. #region _UITypeEditor_
  1170. public class G2DFieldMemberEditor : UITypeEditor
  1171. {
  1172. private readonly MemberInfo fieldInfo;
  1173. private readonly Type fieldType;
  1174. private readonly IG2DPropertyAdapter[] adapters;
  1175. private object fieldValue;
  1176. private readonly ListAttribute listDesc;
  1177. private readonly NotNullAttribute notNull;
  1178. private readonly bool isList;
  1179. public G2DFieldMemberEditor(MemberInfo field, object fieldValue, bool isList, IG2DPropertyAdapter[] adapters)
  1180. {
  1181. this.isList = isList;
  1182. this.fieldInfo = field;
  1183. if (field is FieldInfo)
  1184. {
  1185. this.fieldType = (field as FieldInfo).FieldType;
  1186. }
  1187. else if (field is PropertyInfo)
  1188. {
  1189. this.fieldType = (field as PropertyInfo).PropertyType;
  1190. }
  1191. this.fieldValue = fieldValue;
  1192. this.adapters = adapters;
  1193. if (this.fieldValue != null)
  1194. {
  1195. fieldType = fieldValue.GetType();
  1196. }
  1197. this.listDesc = PropertyUtil.GetListDesc(field);
  1198. this.notNull = PropertyUtil.GetAttribute<NotNullAttribute>(field);
  1199. }
  1200. /// <summary>
  1201. /// 编辑属性值时,在右侧显示...更多按钮
  1202. /// </summary>
  1203. /// <param name="context"></param>
  1204. /// <returns></returns>
  1205. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  1206. {
  1207. if (fieldType.IsPrimitive)
  1208. {
  1209. return UITypeEditorEditStyle.None;
  1210. }
  1211. else if (fieldType.IsEnum)
  1212. {
  1213. return UITypeEditorEditStyle.None;
  1214. }
  1215. else if (fieldType.Equals(typeof(string)))
  1216. {
  1217. return UITypeEditorEditStyle.None;
  1218. }
  1219. else if (fieldType.IsClass)
  1220. {
  1221. return UITypeEditorEditStyle.Modal;
  1222. }
  1223. else if (fieldType.IsArray)
  1224. {
  1225. return UITypeEditorEditStyle.Modal;
  1226. }
  1227. return UITypeEditorEditStyle.Modal;
  1228. }
  1229. public override object EditValue(
  1230. ITypeDescriptorContext context,
  1231. IServiceProvider provider,
  1232. object value)
  1233. {
  1234. var edSvc = provider.GetService(typeof(IWindowsFormsEditorService))
  1235. as IWindowsFormsEditorService;
  1236. if (edSvc != null)
  1237. {
  1238. if (isList)
  1239. {
  1240. if(listDesc != null && listDesc.ElementTypes != null)
  1241. {
  1242. var list = listDesc.ElementTypes.ToArray();
  1243. if(list != null && list.Length > 0)
  1244. {
  1245. G2DCollectionEditor editor = new G2DCollectionEditor( fieldType, value, list, adapters);
  1246. if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1247. {
  1248. value = editor.GetEditCompleteData();
  1249. }
  1250. return value;
  1251. }
  1252. }
  1253. MessageBox.Show($"标识为列表的属性打开出错({fieldType}), 忘记标识ListAttribute?");
  1254. }
  1255. else
  1256. {
  1257. G2DFieldEditor editor = new G2DFieldEditor(fieldType, value, (notNull == null), adapters);
  1258. if (editor.ShowDialog() == DialogResult.OK)
  1259. {
  1260. value = editor.EditObject;
  1261. }
  1262. }
  1263. return value;
  1264. }
  1265. return base.EditValue(context, provider, value);
  1266. }
  1267. }
  1268. public class G2DCollectionItemEditor : UITypeEditor
  1269. {
  1270. public object ElementItem { get; private set; }
  1271. public Type ElementType { get; private set; }
  1272. private IG2DPropertyAdapter[] adapters;
  1273. public G2DCollectionItemEditor(object fieldValue, Type fieldType, IG2DPropertyAdapter[] adapters)
  1274. {
  1275. this.ElementItem = fieldValue;
  1276. this.ElementType = fieldType;
  1277. this.adapters = adapters;
  1278. }
  1279. /// <summary>
  1280. /// 编辑属性值时,在右侧显示...更多按钮
  1281. /// </summary>
  1282. /// <param name="context"></param>
  1283. /// <returns></returns>
  1284. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  1285. {
  1286. if (ElementType.IsPrimitive)
  1287. {
  1288. return UITypeEditorEditStyle.None;
  1289. }
  1290. else if (ElementType.IsEnum)
  1291. {
  1292. return UITypeEditorEditStyle.None;
  1293. }
  1294. else if (ElementType.Equals(typeof(string)))
  1295. {
  1296. return UITypeEditorEditStyle.None;
  1297. }
  1298. else if (ElementType.IsClass)
  1299. {
  1300. return UITypeEditorEditStyle.Modal;
  1301. }
  1302. else if (ElementType.IsArray)
  1303. {
  1304. return UITypeEditorEditStyle.Modal;
  1305. }
  1306. return UITypeEditorEditStyle.Modal;
  1307. }
  1308. public override object EditValue(
  1309. ITypeDescriptorContext context,
  1310. IServiceProvider provider,
  1311. object value)
  1312. {
  1313. var edSvc = provider.GetService(typeof(IWindowsFormsEditorService))
  1314. as IWindowsFormsEditorService;
  1315. if (edSvc != null)
  1316. {
  1317. G2DFieldEditor editor = new G2DFieldEditor(ElementType, value, false, adapters);
  1318. if (editor.ShowDialog() == DialogResult.OK)
  1319. {
  1320. value = editor.EditObject;
  1321. }
  1322. return value;
  1323. }
  1324. return base.EditValue(context, provider, value);
  1325. }
  1326. }
  1327. //--------------------------------------------------------------------------------------------------------------------------------
  1328. public abstract class G2DMemberUITypeEditor : UITypeEditor
  1329. {
  1330. protected readonly MemberInfo fieldInfo;
  1331. protected readonly Type fieldType;
  1332. protected readonly DescAttribute desc;
  1333. protected readonly object ownerData;
  1334. public G2DMemberUITypeEditor(MemberInfo field, object ownerData)
  1335. {
  1336. this.desc = PropertyUtil.GetAttribute<DescAttribute>(field);
  1337. this.fieldInfo = field;
  1338. this.ownerData = ownerData;
  1339. if (fieldInfo is FieldInfo)
  1340. {
  1341. fieldType = (fieldInfo as FieldInfo).FieldType;
  1342. }
  1343. else if (fieldInfo is PropertyInfo)
  1344. {
  1345. fieldType = (fieldInfo as PropertyInfo).PropertyType;
  1346. }
  1347. else
  1348. {
  1349. fieldType = null;
  1350. }
  1351. }
  1352. public object GetOwnerValue()
  1353. {
  1354. return PropertyUtil.GetMemberValue(fieldInfo, ownerData);
  1355. }
  1356. public void SetOwnerValue(object value)
  1357. {
  1358. PropertyUtil.SetMemberValue(fieldInfo, ownerData, value);
  1359. }
  1360. }
  1361. public class G2DDirectoryDialogEditor : G2DMemberUITypeEditor
  1362. {
  1363. public G2DDirectoryDialogEditor(MemberInfo field, object ownerData)
  1364. : base(field, ownerData)
  1365. {
  1366. }
  1367. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  1368. {
  1369. return UITypeEditorEditStyle.Modal;
  1370. }
  1371. public override object EditValue(
  1372. ITypeDescriptorContext context,
  1373. IServiceProvider provider,
  1374. object value)
  1375. {
  1376. string path = (string)base.GetOwnerValue();
  1377. {
  1378. FolderBrowserDialog fd = new FolderBrowserDialog();
  1379. fd.SelectedPath = path;
  1380. if (fd.ShowDialog() == DialogResult.OK)
  1381. {
  1382. return fd.SelectedPath;
  1383. }
  1384. }
  1385. return base.EditValue(context, provider, value);
  1386. }
  1387. }
  1388. public class G2DFileDialogEditor : G2DMemberUITypeEditor
  1389. {
  1390. public G2DFileDialogEditor(MemberInfo field, object ownerData)
  1391. : base(field, ownerData)
  1392. {
  1393. }
  1394. public override bool GetPaintValueSupported(ITypeDescriptorContext context)
  1395. {
  1396. return true;
  1397. }
  1398. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  1399. {
  1400. return UITypeEditorEditStyle.Modal;
  1401. }
  1402. public override object EditValue(
  1403. ITypeDescriptorContext context,
  1404. IServiceProvider provider,
  1405. object value)
  1406. {
  1407. string path = (string)base.GetOwnerValue();
  1408. {
  1409. OpenFileDialog fd = new OpenFileDialog();
  1410. fd.InitialDirectory = path;
  1411. if (fd.ShowDialog() == DialogResult.OK)
  1412. {
  1413. return fd.FileName;
  1414. }
  1415. }
  1416. return base.EditValue(context, provider, value);
  1417. }
  1418. }
  1419. public class G2DColorEditor : G2DMemberUITypeEditor
  1420. {
  1421. private SolidBrush brush = new SolidBrush(Color.Green);
  1422. public G2DColorEditor(MemberInfo field, object ownerData)
  1423. : base(field, ownerData)
  1424. {
  1425. }
  1426. public override bool GetPaintValueSupported(ITypeDescriptorContext context)
  1427. {
  1428. return true;
  1429. }
  1430. public override void PaintValue(PaintValueEventArgs e)
  1431. {
  1432. int color = (int)base.GetOwnerValue();
  1433. brush.Color = Color.FromArgb(color);
  1434. e.Graphics.FillRectangle(brush, e.Bounds);
  1435. }
  1436. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  1437. {
  1438. return UITypeEditorEditStyle.Modal;
  1439. }
  1440. public override object EditValue(
  1441. ITypeDescriptorContext context,
  1442. IServiceProvider provider,
  1443. object value)
  1444. {
  1445. int color = (int)base.GetOwnerValue();
  1446. {
  1447. ColorDialog cd = new ColorDialog();
  1448. cd.Color = Color.FromArgb(color);
  1449. if (cd.ShowDialog() == DialogResult.OK)
  1450. {
  1451. return cd.Color.ToArgb();
  1452. }
  1453. }
  1454. return base.EditValue(context, provider, value);
  1455. }
  1456. }
  1457. public class G2DOptionalEditor : G2DMemberUITypeEditor
  1458. {
  1459. private OptionalValueAttribute optional;
  1460. public G2DOptionalEditor(MemberInfo field, object ownerData)
  1461. : base(field, ownerData)
  1462. {
  1463. this.optional = PropertyUtil.GetAttribute<OptionalValueAttribute>(fieldInfo);
  1464. }
  1465. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  1466. {
  1467. return UITypeEditorEditStyle.DropDown;
  1468. }
  1469. public override object EditValue(
  1470. ITypeDescriptorContext context,
  1471. IServiceProvider provider,
  1472. object value)
  1473. {
  1474. IWindowsFormsEditorService wfes =
  1475. provider.GetService(typeof(IWindowsFormsEditorService)) as
  1476. IWindowsFormsEditorService;
  1477. if (wfes != null)
  1478. {
  1479. var list = new ListBox();
  1480. foreach (var o in optional.Values)
  1481. {
  1482. list.Items.Add(o);
  1483. }
  1484. list.SelectedValueChanged += (s, e) =>
  1485. {
  1486. wfes.CloseDropDown();
  1487. };
  1488. wfes.DropDownControl(list);
  1489. if (list.SelectedItems.Count > 0)
  1490. {
  1491. value = list.SelectedItem.ToString();
  1492. }
  1493. list.Dispose();
  1494. }
  1495. return value;
  1496. }
  1497. }
  1498. #endregion
  1499. //--------------------------------------------------------------------------------------------------------------------------------
  1500. }