using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using CommonLang.Property; using CommonLang; using CommonFroms.Utils; using CommonLang.Xml; using CommonFroms.G2D.DataGrid; using System.Reflection; namespace CommonFroms.DescAttributeEdit { /// /// 用于选择或创建,所有对应其子类的实例 /// 该选择界面先对注释为 [DescAttribute] 的 Catgory 分类,再进行子类划分 /// public partial class ValueTypeDialog : Form { private readonly Type baseType; private Catgorys triggerTypes; private object editValue; private HashMap editHistory = new HashMap(); public ValueTypeDialog(Type baseType, string title, object edit, IG2DPropertyAdapter[] adapters) { this.baseType = baseType; this.triggerTypes = Catgorys.GetCatgory(baseType); InitializeComponent(); this.SuspendLayout(); { if (edit != null) { edit = XmlUtil.CloneObject(edit); } this.Text = title; this.editValue = edit; this.valuePanel1.SetAdapters(adapters); foreach (string catgory in triggerTypes.CatgoryNames) { comboBoxCatgory.Items.Add(catgory); } if (editValue != null) { string catgory = TypeDescAttribute.GetCatgoryName(editValue.GetType()); comboBoxCatgory.SelectedItem = catgory; List types = triggerTypes.GetCatgoryTypes(catgory); if (types != null) { foreach (TypeDescAttribute type in types) { comboBoxType.Items.Add(type); } } comboBoxType.SelectedItem = triggerTypes.GetTypeDescAttribute(editValue.GetType()); SetEditValue(editValue, false); } else if (triggerTypes.CatgoryNames.Count > 0) { comboBoxCatgory.SelectedItem = triggerTypes.CatgoryNames[0]; List types = triggerTypes.GetCatgoryTypes(triggerTypes.CatgoryNames[0]); if (types != null) { foreach (TypeDescAttribute type in types) { comboBoxType.Items.Add(type); } if (comboBoxType.Items.Count > 0) { comboBoxType.SelectedItem = comboBoxType.Items[0]; Type selected = SelectedType; SetEditValue(selected); } } } this.comboBoxCatgory.SelectedIndexChanged += new System.EventHandler(this.comboBoxCatgory_SelectedIndexChanged); this.comboBoxType.SelectedIndexChanged += new System.EventHandler(this.comboBoxType_SelectedIndexChanged); } this.ResumeLayout(); } private void comboBoxCatgory_SelectedIndexChanged(object sender, EventArgs e) { comboBoxType.Items.Clear(); string name = comboBoxCatgory.SelectedItem + ""; List types = triggerTypes.GetCatgoryTypes(name); if (types != null) { foreach (TypeDescAttribute type in types) { comboBoxType.Items.Add(type); } if (comboBoxType.Items.Count > 0) { comboBoxType.SelectedItem = comboBoxType.Items[0]; } } } private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e) { Type selected = SelectedType; if (selected != null && (editValue == null || selected != editValue.GetType())) { SetEditValue(selected); } } private void buttonOK_Click(object sender, EventArgs e) { } private void buttonCancel_Click(object sender, EventArgs e) { } private void SetEditValue(Type selected) { object value = null; bool is_new = false; if (!editHistory.TryGetValue(selected, out value)) { is_new = true; value = ReflectionUtil.CreateInstance(SelectedType); } SetEditValue(value, is_new); } private void SetEditValue(object value, bool is_new) { if (is_new) { } this.editValue = value; valuePanel1.SetValue(editValue); editHistory.Put(editValue.GetType(), editValue); } public string SelectedCatgory { get { return comboBoxCatgory.SelectedItem + ""; } } public TypeDescAttribute SelectedTypeDesc { get { return comboBoxType.SelectedItem as TypeDescAttribute; } } public Type SelectedType { get { TypeDescAttribute td = SelectedTypeDesc; if (td != null) { return td.DataType; } return null; } } public object EditValue { get { return editValue; } } public static T ShowAddDialog( IWin32Window owner, string title, params IG2DPropertyAdapter[] adapters) where T : class { ValueTypeDialog form = new ValueTypeDialog(typeof(T), title, null, adapters); if (form.ShowDialog(owner) == DialogResult.OK) { return (T)form.EditValue; } return null; } public static object ShowEditDialog( IWin32Window owner, string title, Type baseType, object edit, params IG2DPropertyAdapter[] adapters) { ValueTypeDialog form = new ValueTypeDialog(baseType, title, edit, adapters); if (form.ShowDialog(owner) == DialogResult.OK) { return form.EditValue; } return null; } public static object ShowValueDialog( IWin32Window owner, string title, Type baseType, object edit, params IG2DPropertyAdapter[] adapters) { ValueTypeDialog form = new ValueTypeDialog(baseType, title, edit, adapters); if (form.ShowDialog(owner) == DialogResult.OK) { return form.EditValue; } return null; } // class FieldHistory // { // class Entry // { // Type type; // string fieldName; // int fieldIndex; // object fieldValue; // } // // public void FindFieldValue(FieldInfo field) // { // // } // } } }