123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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
- {
- /// <summary>
- /// 用于选择或创建,所有对应其子类的实例
- /// 该选择界面先对注释为 [DescAttribute] 的 Catgory 分类,再进行子类划分
- /// </summary>
- public partial class ValueTypeDialog : Form
- {
- private readonly Type baseType;
- private Catgorys triggerTypes;
- private object editValue;
- private HashMap<Type, object> editHistory = new HashMap<Type, object>();
- 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<object>(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<TypeDescAttribute> 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<TypeDescAttribute> 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<TypeDescAttribute> 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<T>(
- 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)
- // {
- //
- // }
- // }
- }
- }
|