123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel;
- using System.Collections;
- using System.Reflection;
- using System.Drawing.Design;
- using System.Windows.Forms.Design;
- using CommonLang.Property;
- using System.Windows.Forms;
- using System.Globalization;
- using System.Drawing;
- using CommonLang;
- namespace CommonFroms.G2D.DataGrid
- {
- // --------------------------------------------------------------------------------------
- public interface IG2DPropertyAdapter
- {
- UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData);
- TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData);
- }
- public interface IG2DPropertyDescriptor
- {
- object ComponentData { get; }
- object FieldValue { get; }
- bool NotNull { get; }
- Type DecleardFieldType { get; }
- }
- public class G2DPropertyDescriptor : ICustomTypeDescriptor
- {
- static G2DPropertyDescriptor()
- {
- RegistPropertyAdapter(new InternalDataAdapters());
- }
- private object Data;
- private List<IG2DPropertyAdapter> adapter_list = new List<IG2DPropertyAdapter>();
- private IG2DPropertyAdapter[] add_adapters;
- public G2DPropertyDescriptor(object obj, params IG2DPropertyAdapter[] adds)
- {
- this.EnablePropertyInfo = false;
- this.EnableFieldInfo = true;
- this.Data = obj;
- this.add_adapters = adds;
- foreach (IG2DPropertyAdapter sa in static_adapters)
- {
- this.AddPropertyAdapter(sa);
- }
- foreach (IG2DPropertyAdapter sa in adds)
- {
- this.AddPropertyAdapter(sa);
- }
- }
- public object EditData
- {
- get { return Data; }
- internal set { Data = value; }
- }
- public bool EnablePropertyInfo { get; set; }
- public bool EnableFieldInfo { get; set; }
- public override string ToString()
- {
- return Data.ToString();
- }
- public void AddPropertyAdapter(IG2DPropertyAdapter adapter)
- {
- if (!adapter_list.Contains(adapter))
- {
- adapter_list.Add(adapter);
- }
- }
- //------------------------------------------------------------------
- #region ICustomTypeDescriptor 成员
- AttributeCollection ICustomTypeDescriptor.GetAttributes()
- {
- return TypeDescriptor.GetAttributes(Data, true);
- }
- string ICustomTypeDescriptor.GetClassName()
- {
- return TypeDescriptor.GetClassName(Data, true);
- }
- string ICustomTypeDescriptor.GetComponentName()
- {
- return TypeDescriptor.GetComponentName(Data, true);
- }
- TypeConverter ICustomTypeDescriptor.GetConverter()
- {
- return TypeDescriptor.GetConverter(Data, true);
- }
- EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
- {
- return TypeDescriptor.GetDefaultEvent(Data, true);
- }
- PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
- {
- return TypeDescriptor.GetDefaultProperty(Data, true);
- }
- object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
- {
- return TypeDescriptor.GetEditor(Data, editorBaseType, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
- {
- return TypeDescriptor.GetEvents(Data, attributes, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
- {
- return TypeDescriptor.GetEvents(Data, true);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
- {
- return ListPropertyDescriptors(Data, this, attributes);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
- {
- return ListPropertyDescriptors(Data, this, new Attribute[0]);
- }
- object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
- {
- return Data;
- }
- #endregion
- //------------------------------------------------------------------
- #region StaticUtils
- public static bool IsNeedG2DEditor(Type listType)
- {
- if (listType.IsArray && listType.GetArrayRank() == 1)
- {
- return IsNeedG2DCollectionEditor(listType);
- }
- else if (typeof(IList).IsAssignableFrom(listType))
- {
- return IsNeedG2DCollectionEditor(listType);
- }
- else if (typeof(IDictionary).IsAssignableFrom(listType))
- {
- return IsNeedG2DCollectionEditor(listType);
- }
- if (listType.Equals(typeof(string)))
- {
- return false;
- }
- if (listType.IsArray)
- {
- return false;
- }
- if (listType.IsPrimitive)
- {
- return false;
- }
- return true;
- }
- public static bool IsNeedG2DCollectionEditor(Type listType)
- {
- if (listType.IsArray && listType.GetArrayRank() == 1)
- {
- Type elementType = listType.GetElementType();
- return IsNeedG2DEditor(elementType);
- }
- else if (listType.GetInterface(typeof(IList).Name) != null)
- {
- if (listType.IsGenericType)
- {
- Type elementType = listType.GetGenericArguments()[0];
- return IsNeedG2DEditor(elementType);
- }
- else
- {
- return true;
- }
- }
- else if (listType.GetInterface(typeof(IDictionary).Name) != null)
- {
- if (listType.IsGenericType)
- {
- var gtypes = listType.GetGenericArguments();
- if (gtypes[0].IsPrimitive || gtypes[0] == typeof(string))
- {
- return IsNeedG2DEditor(gtypes[1]);
- }
- }
- }
- return false;
- }
- private static List<IG2DPropertyAdapter> static_adapters = new List<IG2DPropertyAdapter>();
- public static void RegistPropertyAdapter(IG2DPropertyAdapter adapter)
- {
- if (!static_adapters.Contains(adapter))
- {
- static_adapters.Add(adapter);
- }
- }
- public static GridItemCollection GetAllGridEntries(PropertyGrid grid)
- {
- if (grid == null)
- throw new ArgumentNullException("grid");
- object view = grid.GetType().GetField(
- "gridView",
- BindingFlags.NonPublic | BindingFlags.Instance).GetValue(grid);
- return (GridItemCollection)view.GetType().InvokeMember(
- "GetAllGridEntries",
- BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
- null, view, null);
- }
- public static void ToDescription(StringBuilder sb, object fieldValue)
- {
- if (fieldValue != null)
- {
- if (fieldValue.GetType().IsArray)
- {
- sb.AppendLine(CUtils.ArrayToString((Array)fieldValue));
- }
- else if (fieldValue.GetType().GetInterface(typeof(IDictionary).Name) != null)
- {
- sb.AppendLine(CUtils.MapToString((IDictionary)fieldValue));
- }
- else if (fieldValue.GetType().GetInterface(typeof(IList).Name) != null)
- {
- sb.AppendLine(CUtils.ListToString((IList)fieldValue));
- }
- else
- {
- if (fieldValue.GetType().IsEnum)
- {
- DescAttribute eda = PropertyUtil.GetEnumAttribute<DescAttribute>((Enum)fieldValue);
- if (eda != null)
- {
- sb.AppendLine(fieldValue + " : " + eda.Desc);
- }
- else
- {
- sb.AppendLine(fieldValue + "");
- }
- }
- else
- {
- sb.AppendLine(fieldValue + "");
- }
- }
- }
- }
- #endregion
- //--------------------------------------------------------------------------------------------------------------------------------------------
- public virtual PropertyDescriptorCollection ListPropertyDescriptors(object data, G2DPropertyDescriptor g2ddesc, Attribute[] attributes)
- {
- Type type = data.GetType();
- List<PropertyDescriptor> ret = new List<PropertyDescriptor>();
- if (type.IsArray && type.GetArrayRank() == 1)
- {
- Array array = data as Array;
- Type elementType = type.GetElementType();
- for (int i = 0; i < array.Length; i++)
- {
- ret.Add(new ArrayItemPropertyDescriptor(g2ddesc, array, i, attributes));
- }
- }
- else if (type.GetInterface(typeof(IList).Name) != null)
- {
- IList list = data as IList;
- if (type.IsGenericType)
- {
- Type elementType = type.GetGenericArguments()[0];
- for (int i = 0; i < list.Count; i++)
- {
- ret.Add(new ListItemPropertyDescriptor(g2ddesc, list, i, attributes));
- }
- }
- else
- {
- for (int i = 0; i < list.Count; i++)
- {
- ret.Add(new ListItemPropertyDescriptor(g2ddesc, list, i, attributes));
- }
- }
- }
- else if (type.GetInterface(typeof(IDictionary).Name) != null)
- {
- IDictionary map = data as IDictionary;
- if (type.IsGenericType)
- {
- foreach (var k in map.Keys)
- {
- ret.Add(new MapItemPropertyDescriptor(g2ddesc, map, k, attributes));
- }
- }
- }
- else if ((data == Data) && (type.IsEnum))
- {
- ret.Add(new EnumPropertyDescriptor(g2ddesc, Data, attributes));
- }
- else
- {
- if (EnableFieldInfo)
- {
- FieldInfo[] fields = type.GetFields();
- for (int i = 0; i < fields.Length; i++)
- {
- FieldInfo field = fields[i];
- DescAttribute desc = PropertyUtil.GetDesc(field);
- if (desc == null || desc.Editable)
- {
- Type fieldType = field.FieldType;
- object fieldData = field.GetValue(data);
- if (fieldData != null)
- {
- fieldType = fieldData.GetType();
- }
- ret.Add(new FieldMemberDescriptor(g2ddesc, i, field, data, attributes));
- }
- }
- }
- if (EnablePropertyInfo)
- {
- PropertyInfo[] props = type.GetProperties();
- for (int i = 0; i < props.Length; i++)
- {
- PropertyInfo field = props[i];
- DescAttribute desc = PropertyUtil.GetDesc(field);
- if (desc == null || desc.Editable)
- {
- Type fieldType = field.PropertyType;
- object fieldData = PropertyUtil.GetMemberValue(field, data); ;//field.GetValue(data);
- if (fieldData != null)
- {
- fieldType = fieldData.GetType();
- }
- ret.Add(new PropertyMemberDescriptor(g2ddesc, i, field, data, attributes));
- }
- }
- }
- }
- PropertyDescriptorCollection result = new PropertyDescriptorCollection(ret.ToArray(), true);
- ArrayList propertyNames = new ArrayList();
- foreach (PropertyDescriptor propertyAttributes in ret)
- {
- propertyNames.Add(propertyAttributes.DisplayName);
- }
- return result.Sort((string[])propertyNames.ToArray(typeof(string)));
- }
- //--------------------------------------------------------------------------------------------------------------------------------------------
- public abstract class MemberPropertyDescriptor<M> : PropertyDescriptor, IG2DPropertyDescriptor where M : MemberInfo
- {
- public readonly int index;
- public readonly G2DPropertyDescriptor g2ddesc;
- protected readonly M field;
- protected readonly Type fieldType;
- protected readonly object componentData;
- protected readonly Type componentType;
- private DescAttribute desc;
- private DependOnPropertyAttribute[] depends;
- private TypeConverterAttribute[] type_converters;
- private ExpandableAttribute[] expandables;
- private ExpandableAttribute[] expandables_f;
- private NotNullAttribute notNull;
- public MemberPropertyDescriptor(G2DPropertyDescriptor g2ddesc, int index, M field, object componentData, Attribute[] attributes)
- : base(field.Name, attributes)
- {
- this.g2ddesc = g2ddesc;
- this.index = index;
- this.componentData = componentData;
- this.componentType = componentData.GetType();
- this.field = field;
- this.fieldType = DecleardFieldType;
- this.desc = PropertyUtil.GetDesc(field);
- object fieldValue = this.GetValue(componentData);
- if (fieldValue != null)
- {
- fieldType = fieldValue.GetType();
- }
- this.depends = (DependOnPropertyAttribute[])field.GetCustomAttributes(typeof(DependOnPropertyAttribute), false);
- this.type_converters = (TypeConverterAttribute[])fieldType.GetCustomAttributes(typeof(TypeConverterAttribute), true);
- this.expandables = (ExpandableAttribute[])fieldType.GetCustomAttributes(typeof(ExpandableAttribute), true);
- this.expandables_f = (ExpandableAttribute[])field.GetCustomAttributes(typeof(ExpandableAttribute), true);
- this.notNull = PropertyUtil.GetAttribute<NotNullAttribute>(field);
- }
- public abstract Type DecleardFieldType { get; }
- public M Field { get { return field; } }
- public object ComponentData { get { return componentData; } }
- public bool NotNull { get { return notNull != null; } }
- public object FieldValue { get { return this.GetValue(componentData); } }
- public override Type ComponentType { get { return componentType; } }
- public override bool IsReadOnly
- {
- get
- {
- if (depends != null && depends.Length > 0)
- {
- foreach (DependOnPropertyAttribute depend in depends)
- {
- bool expect = PropertyUtil.GetFieldOrPropertyOrMethodValue<bool>(componentData, depend.PropertyName);
- if (expect != depend.Expect)
- {
- return true;
- }
- }
- }
- return false;
- }
- }
- public override Type PropertyType
- {
- get { return (IsReadOnly) ? typeof(string) : fieldType; }
- }
- public override string DisplayName
- {
- get { return base.DisplayName; }
- }
- public override string Description
- {
- get
- {
- StringBuilder sb = new StringBuilder();
- if (desc != null)
- {
- sb.AppendLine(desc.Desc);
- }
- object fieldValue = this.GetValue(componentData);
- ToDescription(sb, fieldValue);
- sb.AppendLine(base.Description);
- return sb.ToString();
- }
- }
- public override string Category
- {
- get
- {
- if (desc != null)
- {
- return desc.Category;
- }
- return base.Category;
- }
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override void ResetValue(object component)
- {
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- public override object GetEditor(Type editorBaseType)
- {
- if (IsReadOnly) return null;
- foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
- {
- UITypeEditor editor = adapter.GetEditor(field, DecleardFieldType, componentData);
- if (editor != null)
- {
- return editor;
- }
- }
- if (IsNeedG2DCollectionEditor(fieldType))
- {
- return new G2DFieldMemberEditor(field, this.GetValue(componentData), true, g2ddesc.add_adapters);
- }
- else if (IsNeedG2DEditor(fieldType))
- {
- return new G2DFieldMemberEditor(field, this.GetValue(componentData), false, g2ddesc.add_adapters);
- }
- return base.GetEditor(editorBaseType);
- }
- public override TypeConverter Converter
- {
- get
- {
- if (IsReadOnly) return null;
- if (type_converters.Length > 0)
- {
- return base.Converter;
- }
- foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
- {
- TypeConverter converter = adapter.GetConverter(field, DecleardFieldType, componentData);
- if (converter != null)
- {
- return converter;
- }
- }
- if (expandables.Length > 0)
- {
- return new G2DUIFieldExpandableConverter(g2ddesc);
- }
- if (expandables_f.Length > 0)
- {
- return new G2DUIFieldExpandableConverter(g2ddesc);
- }
- if (IsNeedG2DCollectionEditor(fieldType))
- {
- return new G2DUIFieldListConverter(g2ddesc);
- }
- return base.Converter;
- }
- }
- }
- public class FieldMemberDescriptor : MemberPropertyDescriptor<FieldInfo>
- {
- public FieldMemberDescriptor(G2DPropertyDescriptor g2ddesc, int index, FieldInfo field, object componentData, Attribute[] attributes)
- : base(g2ddesc, index, field, componentData, attributes)
- {
- }
- public override Type DecleardFieldType
- {
- get { return Field.FieldType; }
- }
- public override object GetValue(object component)
- {
- return field.GetValue(component);
- }
- public override void SetValue(object component, object value)
- {
- field.SetValue(component, value);
- }
- }
- public class PropertyMemberDescriptor : MemberPropertyDescriptor<PropertyInfo>
- {
- protected readonly MethodInfo set_op;
- protected readonly MethodInfo get_op;
- public PropertyMemberDescriptor(G2DPropertyDescriptor g2ddesc, int index, PropertyInfo field, object componentData, Attribute[] attributes)
- : base(g2ddesc, index, field, componentData, attributes)
- {
- this.get_op = field.GetGetMethod();
- this.set_op = field.GetSetMethod();
- }
- public override bool IsReadOnly
- {
- get
- {
- if (set_op == null) return true;
- return base.IsReadOnly;
- }
- }
- public override Type DecleardFieldType
- {
- get { return Field.PropertyType; }
- }
- public override object GetValue(object component)
- {
- if (get_op != null) return get_op.Invoke(component, new object[] { });
- return null;
- }
- public override void SetValue(object component, object value)
- {
- if (set_op != null) set_op.Invoke(component, new object[] { value });
- }
- }
- //------------------------------------------------------------------
- /*
- public class CustomPropertyDescriptor : PropertyDescriptor, IG2DPropertyDescriptor
- {
- public readonly int index;
- public readonly G2DPropertyDescriptor g2ddesc;
- private FieldInfo field;
- private Type fieldType;
- private object componentData;
- private Type componentType;
- private DescAttribute desc;
- private DependOnPropertyAttribute[] depends;
- private TypeConverterAttribute[] type_converters;
- private ExpandableAttribute[] expandables;
- private ExpandableAttribute[] expandables_f;
- private NotNullAttribute notNull;
- public CustomPropertyDescriptor(
- G2DPropertyDescriptor g2ddesc,
- int index,
- FieldInfo field,
- object componentData,
- Attribute[] attributes)
- : base(field.Name, attributes)
- {
- this.g2ddesc = g2ddesc;
- this.index = index;
- this.field = field;
- this.fieldType = field.FieldType;
- this.componentData = componentData;
- this.componentType = componentData.GetType();
- this.desc = PropertyUtil.GetDesc(field);
- object fieldValue = field.GetValue(componentData);
- if (fieldValue != null)
- {
- fieldType = fieldValue.GetType();
- }
- this.depends = (DependOnPropertyAttribute[])field.GetCustomAttributes(typeof(DependOnPropertyAttribute), false);
- this.type_converters = (TypeConverterAttribute[])fieldType.GetCustomAttributes(typeof(TypeConverterAttribute), true);
- this.expandables = (ExpandableAttribute[])fieldType.GetCustomAttributes(typeof(ExpandableAttribute), true);
- this.expandables_f = (ExpandableAttribute[])field.GetCustomAttributes(typeof(ExpandableAttribute), true);
- this.notNull = PropertyUtil.GetAttribute<NotNullAttribute>(field);
- }
- public object ComponentData { get { return componentData; } }
- public bool NotNull { get { return notNull != null; } }
- public Type DecleardFieldType { get { return field.FieldType; } }
- public object FieldValue { get { return field.GetValue(componentData); } }
- public override Type ComponentType { get { return componentType; } }
- public override bool IsReadOnly
- {
- get
- {
- if (depends != null && depends.Length > 0)
- {
- foreach (DependOnPropertyAttribute depend in depends)
- {
- bool expect = PropertyUtil.GetFieldOrPropertyOrMethodValue<bool>(componentData, depend.PropertyName);
- if (expect != depend.Expect)
- {
- return true;
- }
- }
- }
- return false;
- }
- }
- public override Type PropertyType
- {
- get
- {
- if (IsReadOnly) return typeof(string);
- return fieldType;
- }
- }
- public override string DisplayName
- {
- get
- {
- return base.DisplayName;
- }
- }
- public override string Description
- {
- get
- {
- StringBuilder sb = new StringBuilder();
- if (desc != null)
- {
- sb.AppendLine(desc.Desc);
- }
- object fieldValue = field.GetValue(componentData);
- ToDescription(sb, fieldValue);
- sb.AppendLine(base.Description);
- return sb.ToString();
- }
- }
- public override string Category
- {
- get
- {
- if (desc != null)
- {
- return desc.Category;
- }
- return base.Category;
- }
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override void ResetValue(object component)
- {
- }
- public override object GetValue(object component)
- {
- return field.GetValue(component);
- }
- public override void SetValue(object component, object value)
- {
- field.SetValue(component, value);
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- public override object GetEditor(Type editorBaseType)
- {
- if (IsReadOnly) return null;
- foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
- {
- UITypeEditor editor = adapter.GetEditor(field, componentData);
- if (editor != null)
- {
- return editor;
- }
- }
- if (IsNeedG2DCollectionEditor(fieldType))
- {
- return new G2DFieldMemberEditor(field, field.GetValue(componentData), true, g2ddesc.add_adapters);
- }
- else if (IsNeedG2DEditor(fieldType))
- {
- return new G2DFieldMemberEditor(field, field.GetValue(componentData), false, g2ddesc.add_adapters);
- }
- return base.GetEditor(editorBaseType);
- }
- public override TypeConverter Converter
- {
- get
- {
- if (IsReadOnly) return null;
- if (type_converters.Length > 0)
- {
- return base.Converter;
- }
- foreach (IG2DPropertyAdapter adapter in g2ddesc.adapter_list)
- {
- TypeConverter converter = adapter.GetConverter(field, componentData);
- if (converter != null)
- {
- return converter;
- }
- }
- if (expandables.Length > 0)
- {
- return new G2DUIFieldExpandableConverter(g2ddesc);
- }
- if (expandables_f.Length > 0)
- {
- return new G2DUIFieldExpandableConverter(g2ddesc);
- }
- if (IsNeedG2DCollectionEditor(fieldType))
- {
- return new G2DUIFieldListConverter(g2ddesc);
- }
- return base.Converter;
- }
- }
- }
- */
- //------------------------------------------------------------------
- public class EnumPropertyDescriptor : PropertyDescriptor
- {
- public readonly G2DPropertyDescriptor g2ddesc;
- private object componentData;
- private Type componentType;
- public EnumPropertyDescriptor(
- G2DPropertyDescriptor g2d,
- object componentData,
- Attribute[] attributes)
- : base("Value", attributes)
- {
- this.g2ddesc = g2d;
- this.componentData = componentData;
- this.componentType = componentData.GetType();
- }
- public override Type ComponentType
- {
- get { return componentType; }
- }
- public override bool IsReadOnly
- {
- get { return false; }
- }
- public override Type PropertyType
- {
- get { return componentType; }
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override object GetValue(object component)
- {
- return componentData;
- }
- public override void ResetValue(object component)
- {
- }
- public override void SetValue(object component, object value)
- {
- componentData = value;
- g2ddesc.EditData = value;
- }
- public override bool ShouldSerializeValue(object component)
- {
- return true;
- }
- }
- //------------------------------------------------------------------
- public abstract class CollectionItemPropertyDescriptor : PropertyDescriptor, IG2DPropertyDescriptor
- {
- public int Index { get; private set; }
- public G2DPropertyDescriptor Descriptor { get; private set; }
- public object ListOwner { get; private set; }
- public object ComponentData { get { return ListOwner; } }
- public object FieldValue { get { return ElementItem; } }
- public abstract object ElementItem { get; }
- public abstract Type ElementType { get; }
- public CollectionItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, object listOwner, int index, Attribute[] attributes)
- : base(index.ToString(), attributes)
- {
- this.Descriptor = g2ddesc;
- this.Index = index;
- this.ListOwner = listOwner;
- }
- public abstract Type DecleardFieldType { get; }
- public override Type ComponentType { get { return ListOwner.GetType(); } }
- public bool NotNull { get { return false; } }
- public override bool IsReadOnly { get { return false; } }
- public override Type PropertyType
- {
- get
- {
- if (ElementItem != null) return ElementItem.GetType();
- return ElementType;
- }
- }
- public override string Description
- {
- get
- {
- StringBuilder sb = new StringBuilder();
- ToDescription(sb, ElementItem);
- sb.AppendLine(base.Description);
- return sb.ToString();
- }
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override void ResetValue(object component)
- {
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- public override object GetEditor(Type editorBaseType)
- {
- if (IsNeedG2DEditor(PropertyType))
- {
- return new G2DCollectionItemEditor(ElementItem, ElementType, Descriptor.add_adapters);
- }
- return base.GetEditor(editorBaseType);
- }
- public override TypeConverter Converter
- {
- get
- {
- if (PropertyType.GetCustomAttributes(typeof(ExpandableAttribute), true).Length > 0)
- {
- return new G2DUIFieldExpandableConverter(Descriptor);
- }
- return base.Converter;
- }
- }
- }
- //------------------------------------------------------------------
- public class ListItemPropertyDescriptor : CollectionItemPropertyDescriptor
- {
- private readonly Type genericItemType;
- public override object ElementItem { get { return GetValue(ListOwner); } }
- public override Type ElementType { get { return genericItemType; } }
- public override Type DecleardFieldType { get { return genericItemType; } }
- public ListItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, IList listOwner, int index, Attribute[] attributes)
- : base(g2ddesc, listOwner, index, attributes)
- {
- Type listtype = listOwner.GetType();
- if (listtype.IsGenericType)
- {
- this.genericItemType = listOwner.GetType().GetGenericArguments()[0];
- }
- else
- {
- this.genericItemType = typeof(object);
- }
- }
- public override object GetValue(object component)
- {
- IList list = component as IList;
- if (Index >= 0 && Index < list.Count)
- {
- return list[Index];
- }
- return null;
- }
- public override void SetValue(object component, object value)
- {
- IList list = component as IList;
- if (Index >= 0 && Index < list.Count)
- {
- list[Index] = value;
- }
- }
- }
- //------------------------------------------------------------------
- public class ArrayItemPropertyDescriptor : CollectionItemPropertyDescriptor
- {
- private readonly Type genericItemType;
- public override object ElementItem { get { return GetValue(ListOwner); } }
- public override Type ElementType { get { return genericItemType; } }
- public override Type DecleardFieldType { get { return genericItemType; } }
- public ArrayItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, IList listOwner, int index, Attribute[] attributes)
- : base(g2ddesc, listOwner, index, attributes)
- {
- this.genericItemType = listOwner.GetType().GetElementType();
- }
- public override object GetValue(object component)
- {
- Array list = component as Array;
- if (Index >= 0 && Index < list.Length)
- {
- return list.GetValue(Index);
- }
- return null;
- }
- public override void SetValue(object component, object value)
- {
- Array list = component as Array;
- if (Index >= 0 && Index < list.Length)
- {
- list.SetValue(value, Index);
- }
- }
- }
- //------------------------------------------------------------------
- public class MapItemPropertyDescriptor : PropertyDescriptor, IG2DPropertyDescriptor
- {
- private readonly G2DPropertyDescriptor g2ddesc;
- private readonly IDictionary mapOwner;
- private readonly object key;
- private readonly Type[] genericArgs;
- public G2DPropertyDescriptor Descriptor { get { return g2ddesc; } }
- public IDictionary MapOwner { get { return mapOwner; } }
- public object ComponentData { get { return mapOwner; } }
- public Type DecleardFieldType { get { return genericArgs[1]; } }
- public object FieldValue { get { return GetValue(mapOwner); } }
- public bool NotNull { get { return true; } }
- public object Key { get { return key; } }
- public object Value { get { return GetValue(mapOwner); } }
- public override Type ComponentType { get { return MapOwner.GetType(); } }
- public override bool IsReadOnly { get { return false; } }
- public override Type PropertyType
- {
- get
- {
- var value = Value;
- if (value != null) return value.GetType();
- return DecleardFieldType;
- }
- }
- public override string Description
- {
- get
- {
- StringBuilder sb = new StringBuilder();
- ToDescription(sb, Value);
- sb.AppendLine(base.Description);
- return sb.ToString();
- }
- }
- public MapItemPropertyDescriptor(G2DPropertyDescriptor g2ddesc, IDictionary mapOwner, object key, Attribute[] attributes)
- : base(key.ToString(), attributes)
- {
- this.g2ddesc = g2ddesc;
- this.key = key;
- this.mapOwner = mapOwner;
- this.genericArgs = mapOwner.GetType().GetGenericArguments();
- }
- public override object GetValue(object component)
- {
- return mapOwner[key];
- }
- public override void SetValue(object component, object value)
- {
- mapOwner[key] = value;
- }
- public override bool CanResetValue(object component)
- {
- return false;
- }
- public override void ResetValue(object component)
- {
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- public override object GetEditor(Type editorBaseType)
- {
- var value = Value;
- var keyType = key.GetType();
- var valueType = DecleardFieldType;
- if (value != null)
- {
- valueType = value.GetType();
- }
- if (keyType.IsPrimitive || keyType == typeof(string))
- {
- if (IsNeedG2DEditor(valueType))
- {
- return new G2DCollectionItemEditor(value, valueType, Descriptor.add_adapters);
- }
- }
- return null;
- }
- public override TypeConverter Converter
- {
- get
- {
- if (PropertyType.GetCustomAttributes(typeof(ExpandableAttribute), true).Length > 0)
- {
- return new G2DUIFieldExpandableConverter(Descriptor);
- }
- return base.Converter;
- }
- }
- }
- }
- //--------------------------------------------------------------------------------------------------------------------------------
- /// <summary>
- /// 内部DataAdapters
- /// </summary>
- public class InternalDataAdapters : IG2DPropertyAdapter
- {
- public UITypeEditor GetEditor(MemberInfo field, Type fieldType, object ownerData)
- {
- if (PropertyUtil.GetAttribute<DirectoryPathAttribute>(field) != null && fieldType == typeof(string))
- {
- return new G2DDirectoryDialogEditor(field, ownerData);
- }
- else if (PropertyUtil.GetAttribute<FilePathAttribute>(field) != null && fieldType == typeof(string))
- {
- return new G2DFileDialogEditor(field, ownerData);
- }
- else if (PropertyUtil.GetAttribute<Int32ColorAttribute>(field) != null && fieldType == typeof(int))
- {
- return new G2DColorEditor(field, ownerData);
- }
- else if (PropertyUtil.GetAttribute<OptionalValueAttribute>(field) != null && fieldType == typeof(string))
- {
- return new G2DOptionalEditor(field, ownerData);
- }
- return null;
- }
- public TypeConverter GetConverter(MemberInfo field, Type fieldType, object ownerData)
- {
- if (PropertyUtil.GetAttribute<Int32ColorAttribute>(field) != null && fieldType == typeof(int))
- {
- return new G2DIntToHexTypeConverter();
- }
- return null;
- }
- }
- //--------------------------------------------------------------------------------------------------------------------------------
- #region _TypeConverter_
- public class G2DUIFieldListConverter : TypeConverter
- {
- private readonly G2DPropertyDescriptor g2ddesc;
- public G2DUIFieldListConverter(G2DPropertyDescriptor g2d)
- {
- g2ddesc = g2d;
- }
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- if (destinationType.GetInterface(typeof(ICollection).FullName) != null)
- {
- return true;
- }
- if (destinationType.IsArray)
- {
- return true;
- }
- return false;
- }
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if (value is ICollection)
- {
- ICollection list = (ICollection)value;
- return "集合:[" + list.Count + "]";
- }
- if (destinationType.IsArray)
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("数组:");
- Array array = value as Array;
- int[] ranks = new int[array.Rank];
- foreach (int len in ranks)
- {
- sb.Append("[" + len + "]");
- }
- return sb.ToString();
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
- {
- return g2ddesc.ListPropertyDescriptors(value, g2ddesc, attributes);
- }
- public override bool GetPropertiesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- }
- //--------------------------------------------------------------------------------------
- public class G2DUIFieldExpandableConverter : ExpandableObjectConverter
- {
- private readonly G2DPropertyDescriptor g2ddesc;
- public G2DUIFieldExpandableConverter(G2DPropertyDescriptor g2d)
- {
- g2ddesc = g2d;
- }
- public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
- {
- return g2ddesc.ListPropertyDescriptors(value, g2ddesc, attributes);
- }
- public override bool GetPropertiesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- }
- // --------------------------------------------------------------------------------------
- public class G2DIntToHexTypeConverter : TypeConverter
- {
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- if (sourceType == typeof(string))
- {
- return true;
- }
- else
- {
- return base.CanConvertFrom(context, sourceType);
- }
- }
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- if (destinationType == typeof(string))
- {
- return true;
- }
- else
- {
- return base.CanConvertTo(context, destinationType);
- }
- }
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if (destinationType == typeof(string) && value.GetType() == typeof(int))
- {
- return string.Format("0x{0:X8}", value);
- }
- else
- {
- return base.ConvertTo(context, culture, value, destinationType);
- }
- }
- public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
- {
- if (value.GetType() == typeof(string))
- {
- string input = (string)value;
- if (input.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
- {
- input = input.Substring(2);
- }
- return int.Parse(input, NumberStyles.HexNumber, culture);
- }
- else
- {
- return base.ConvertFrom(context, culture, value);
- }
- }
- }
- //--------------------------------------------------------------------------------------
- #endregion
- //--------------------------------------------------------------------------------------------------------------------------------
- #region _UITypeEditor_
- public class G2DFieldMemberEditor : UITypeEditor
- {
- private readonly MemberInfo fieldInfo;
- private readonly Type fieldType;
- private readonly IG2DPropertyAdapter[] adapters;
- private object fieldValue;
- private readonly ListAttribute listDesc;
- private readonly NotNullAttribute notNull;
- private readonly bool isList;
- public G2DFieldMemberEditor(MemberInfo field, object fieldValue, bool isList, IG2DPropertyAdapter[] adapters)
- {
- this.isList = isList;
- this.fieldInfo = field;
- if (field is FieldInfo)
- {
- this.fieldType = (field as FieldInfo).FieldType;
- }
- else if (field is PropertyInfo)
- {
- this.fieldType = (field as PropertyInfo).PropertyType;
- }
- this.fieldValue = fieldValue;
- this.adapters = adapters;
- if (this.fieldValue != null)
- {
- fieldType = fieldValue.GetType();
- }
- this.listDesc = PropertyUtil.GetListDesc(field);
- this.notNull = PropertyUtil.GetAttribute<NotNullAttribute>(field);
- }
- /// <summary>
- /// 编辑属性值时,在右侧显示...更多按钮
- /// </summary>
- /// <param name="context"></param>
- /// <returns></returns>
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- if (fieldType.IsPrimitive)
- {
- return UITypeEditorEditStyle.None;
- }
- else if (fieldType.IsEnum)
- {
- return UITypeEditorEditStyle.None;
- }
- else if (fieldType.Equals(typeof(string)))
- {
- return UITypeEditorEditStyle.None;
- }
- else if (fieldType.IsClass)
- {
- return UITypeEditorEditStyle.Modal;
- }
- else if (fieldType.IsArray)
- {
- return UITypeEditorEditStyle.Modal;
- }
- return UITypeEditorEditStyle.Modal;
- }
- public override object EditValue(
- ITypeDescriptorContext context,
- IServiceProvider provider,
- object value)
- {
- var edSvc = provider.GetService(typeof(IWindowsFormsEditorService))
- as IWindowsFormsEditorService;
- if (edSvc != null)
- {
- if (isList)
- {
- if(listDesc != null && listDesc.ElementTypes != null)
- {
- var list = listDesc.ElementTypes.ToArray();
- if(list != null && list.Length > 0)
- {
- G2DCollectionEditor editor = new G2DCollectionEditor( fieldType, value, list, adapters);
- if (editor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- value = editor.GetEditCompleteData();
- }
- return value;
- }
- }
- MessageBox.Show($"标识为列表的属性打开出错({fieldType}), 忘记标识ListAttribute?");
- }
- else
- {
- G2DFieldEditor editor = new G2DFieldEditor(fieldType, value, (notNull == null), adapters);
- if (editor.ShowDialog() == DialogResult.OK)
- {
- value = editor.EditObject;
- }
- }
- return value;
- }
- return base.EditValue(context, provider, value);
- }
- }
- public class G2DCollectionItemEditor : UITypeEditor
- {
- public object ElementItem { get; private set; }
- public Type ElementType { get; private set; }
- private IG2DPropertyAdapter[] adapters;
- public G2DCollectionItemEditor(object fieldValue, Type fieldType, IG2DPropertyAdapter[] adapters)
- {
- this.ElementItem = fieldValue;
- this.ElementType = fieldType;
- this.adapters = adapters;
- }
- /// <summary>
- /// 编辑属性值时,在右侧显示...更多按钮
- /// </summary>
- /// <param name="context"></param>
- /// <returns></returns>
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- if (ElementType.IsPrimitive)
- {
- return UITypeEditorEditStyle.None;
- }
- else if (ElementType.IsEnum)
- {
- return UITypeEditorEditStyle.None;
- }
- else if (ElementType.Equals(typeof(string)))
- {
- return UITypeEditorEditStyle.None;
- }
- else if (ElementType.IsClass)
- {
- return UITypeEditorEditStyle.Modal;
- }
- else if (ElementType.IsArray)
- {
- return UITypeEditorEditStyle.Modal;
- }
- return UITypeEditorEditStyle.Modal;
- }
- public override object EditValue(
- ITypeDescriptorContext context,
- IServiceProvider provider,
- object value)
- {
- var edSvc = provider.GetService(typeof(IWindowsFormsEditorService))
- as IWindowsFormsEditorService;
- if (edSvc != null)
- {
- G2DFieldEditor editor = new G2DFieldEditor(ElementType, value, false, adapters);
- if (editor.ShowDialog() == DialogResult.OK)
- {
- value = editor.EditObject;
- }
- return value;
- }
- return base.EditValue(context, provider, value);
- }
- }
- //--------------------------------------------------------------------------------------------------------------------------------
- public abstract class G2DMemberUITypeEditor : UITypeEditor
- {
- protected readonly MemberInfo fieldInfo;
- protected readonly Type fieldType;
- protected readonly DescAttribute desc;
- protected readonly object ownerData;
- public G2DMemberUITypeEditor(MemberInfo field, object ownerData)
- {
- this.desc = PropertyUtil.GetAttribute<DescAttribute>(field);
- this.fieldInfo = field;
- this.ownerData = ownerData;
- if (fieldInfo is FieldInfo)
- {
- fieldType = (fieldInfo as FieldInfo).FieldType;
- }
- else if (fieldInfo is PropertyInfo)
- {
- fieldType = (fieldInfo as PropertyInfo).PropertyType;
- }
- else
- {
- fieldType = null;
- }
- }
- public object GetOwnerValue()
- {
- return PropertyUtil.GetMemberValue(fieldInfo, ownerData);
- }
- public void SetOwnerValue(object value)
- {
- PropertyUtil.SetMemberValue(fieldInfo, ownerData, value);
- }
- }
- public class G2DDirectoryDialogEditor : G2DMemberUITypeEditor
- {
- public G2DDirectoryDialogEditor(MemberInfo field, object ownerData)
- : base(field, ownerData)
- {
- }
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- return UITypeEditorEditStyle.Modal;
- }
- public override object EditValue(
- ITypeDescriptorContext context,
- IServiceProvider provider,
- object value)
- {
- string path = (string)base.GetOwnerValue();
- {
- FolderBrowserDialog fd = new FolderBrowserDialog();
- fd.SelectedPath = path;
- if (fd.ShowDialog() == DialogResult.OK)
- {
- return fd.SelectedPath;
- }
- }
- return base.EditValue(context, provider, value);
- }
- }
- public class G2DFileDialogEditor : G2DMemberUITypeEditor
- {
- public G2DFileDialogEditor(MemberInfo field, object ownerData)
- : base(field, ownerData)
- {
- }
- public override bool GetPaintValueSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- return UITypeEditorEditStyle.Modal;
- }
- public override object EditValue(
- ITypeDescriptorContext context,
- IServiceProvider provider,
- object value)
- {
- string path = (string)base.GetOwnerValue();
- {
- OpenFileDialog fd = new OpenFileDialog();
- fd.InitialDirectory = path;
- if (fd.ShowDialog() == DialogResult.OK)
- {
- return fd.FileName;
- }
- }
- return base.EditValue(context, provider, value);
- }
- }
- public class G2DColorEditor : G2DMemberUITypeEditor
- {
- private SolidBrush brush = new SolidBrush(Color.Green);
- public G2DColorEditor(MemberInfo field, object ownerData)
- : base(field, ownerData)
- {
- }
- public override bool GetPaintValueSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- public override void PaintValue(PaintValueEventArgs e)
- {
- int color = (int)base.GetOwnerValue();
- brush.Color = Color.FromArgb(color);
- e.Graphics.FillRectangle(brush, e.Bounds);
- }
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- return UITypeEditorEditStyle.Modal;
- }
- public override object EditValue(
- ITypeDescriptorContext context,
- IServiceProvider provider,
- object value)
- {
- int color = (int)base.GetOwnerValue();
- {
- ColorDialog cd = new ColorDialog();
- cd.Color = Color.FromArgb(color);
- if (cd.ShowDialog() == DialogResult.OK)
- {
- return cd.Color.ToArgb();
- }
- }
- return base.EditValue(context, provider, value);
- }
- }
- public class G2DOptionalEditor : G2DMemberUITypeEditor
- {
- private OptionalValueAttribute optional;
- public G2DOptionalEditor(MemberInfo field, object ownerData)
- : base(field, ownerData)
- {
- this.optional = PropertyUtil.GetAttribute<OptionalValueAttribute>(fieldInfo);
- }
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- return UITypeEditorEditStyle.DropDown;
- }
- public override object EditValue(
- ITypeDescriptorContext context,
- IServiceProvider provider,
- object value)
- {
- IWindowsFormsEditorService wfes =
- provider.GetService(typeof(IWindowsFormsEditorService)) as
- IWindowsFormsEditorService;
- if (wfes != null)
- {
- var list = new ListBox();
- foreach (var o in optional.Values)
- {
- list.Items.Add(o);
- }
- list.SelectedValueChanged += (s, e) =>
- {
- wfes.CloseDropDown();
- };
- wfes.DropDownControl(list);
- if (list.SelectedItems.Count > 0)
- {
- value = list.SelectedItem.ToString();
- }
- list.Dispose();
- }
- return value;
- }
- }
- #endregion
- //--------------------------------------------------------------------------------------------------------------------------------
- }
|