462 lines
13 KiB
C#
462 lines
13 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Drawing.Design;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using JD.Winforms.CustomComponent;
|
|
|
|
namespace JD.Winforms.XmlGrid
|
|
{
|
|
public delegate void ContainerItemsChangedHandler(object sender, EventArgs e);
|
|
|
|
[TypeConverter(typeof(ExpandableObjectConverter))]
|
|
public class ContainerCollectionClass : CollectionBase, ICustomTypeDescriptor, IObjectNodeInterface
|
|
{
|
|
public event ContainerItemsChangedHandler ItemsChanged;
|
|
private IObjectNodeInterface parent;
|
|
private TypeManager.DynamicType myType;
|
|
protected string name;
|
|
protected string encapsulateAttribute;
|
|
protected bool isObject;
|
|
protected bool isList;
|
|
private List<string> newObjects;
|
|
private List<IObjectNodeInterface> removedObjects;
|
|
|
|
public ContainerCollectionClass()
|
|
{
|
|
removedObjects = new List<IObjectNodeInterface>();
|
|
ItemsChanged = null;
|
|
encapsulateAttribute = null;
|
|
newObjects = new List<string>();
|
|
isObject = false;
|
|
isList = true;
|
|
}
|
|
|
|
public string Name
|
|
{
|
|
get { return name; }
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return "";
|
|
}
|
|
|
|
public void CreateObject(string NewType)
|
|
{
|
|
newObjects.Add(NewType);
|
|
|
|
if (ItemsChanged != null)
|
|
{
|
|
ItemsChanged(this, new EventArgs());
|
|
}
|
|
}
|
|
|
|
public void SetEncapsulateAttr(string Val)
|
|
{
|
|
encapsulateAttribute = Val;
|
|
}
|
|
|
|
public void SetIsObject(bool val)
|
|
{
|
|
isObject = val;
|
|
}
|
|
|
|
public void SetIsList( bool val )
|
|
{
|
|
isList = val;
|
|
}
|
|
|
|
public void SerializeIntoXml(ref string outText)
|
|
{
|
|
List<IObjectNodeInterface> objects_list = new List<IObjectNodeInterface>();
|
|
|
|
foreach (IObjectNodeInterface obj in List)
|
|
{
|
|
if ( removedObjects.IndexOf(obj) < 0 )
|
|
{
|
|
objects_list.Add(obj);
|
|
}
|
|
}
|
|
|
|
if (objects_list.Count == 0 && newObjects.Count == 0)
|
|
{
|
|
if ( !isList )
|
|
{
|
|
outText += $"<{Name}/>\n\r";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (isList || newObjects.Count == 0)
|
|
{
|
|
foreach (IObjectNodeInterface obj in objects_list)
|
|
{
|
|
if (isObject)
|
|
{
|
|
if (encapsulateAttribute != null)
|
|
{
|
|
outText += $"<{Name} {encapsulateAttribute}=\"{obj.GetMyType().m_name}\">\n\r";
|
|
}
|
|
else
|
|
{
|
|
outText += $"<{Name}>\n\r";
|
|
}
|
|
}
|
|
|
|
obj.SerializeIntoXml(ref outText);
|
|
|
|
if (isObject)
|
|
{
|
|
outText += $"</{Name}>\n\r";
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (string s in newObjects)
|
|
{
|
|
if (isObject)
|
|
{
|
|
if (encapsulateAttribute != null)
|
|
{
|
|
outText += $"<{Name} {encapsulateAttribute}=\"{s}\"/>\n\r";
|
|
}
|
|
else
|
|
{
|
|
outText += $"<{Name}/>\n\r";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
outText += $"<{Name}/>\n\r";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public IObjectNodeInterface GetParent()
|
|
{
|
|
return parent;
|
|
}
|
|
|
|
public void SetParent( IObjectNodeInterface Parent )
|
|
{
|
|
parent = Parent;
|
|
}
|
|
|
|
public TypeManager.DynamicType GetMyType()
|
|
{
|
|
return myType;
|
|
}
|
|
|
|
public void SetMyType(TypeManager.DynamicType DynType)
|
|
{
|
|
myType = DynType;
|
|
}
|
|
|
|
public bool canMoveUp(object _object)
|
|
{
|
|
int index = this.List.IndexOf(_object);
|
|
return (index > 0 && index < this.List.Count);
|
|
}
|
|
|
|
public bool canMoveDown(object _object)
|
|
{
|
|
int index = this.List.IndexOf(_object);
|
|
return (index >= 0 && index < this.List.Count - 1);
|
|
}
|
|
|
|
public void MoveUp(object _object)
|
|
{
|
|
int index = this.List.IndexOf(_object);
|
|
if (canMoveUp(_object))
|
|
{
|
|
this.List.Remove(_object);
|
|
this.List.Insert(index - 1, _object);
|
|
}
|
|
}
|
|
|
|
public void MoveDown(object _object)
|
|
{
|
|
int index = this.List.IndexOf(_object);
|
|
if (canMoveDown(_object))
|
|
{
|
|
this.List.Remove(_object);
|
|
this.List.Insert(index + 1, _object);
|
|
}
|
|
}
|
|
|
|
public void Add(object NewObject)
|
|
{
|
|
this.List.Add(NewObject);
|
|
}
|
|
|
|
public void Remove(IObjectNodeInterface delObject)
|
|
{
|
|
removedObjects.Add(delObject);
|
|
|
|
if (ItemsChanged != null )
|
|
{
|
|
ItemsChanged(this, new EventArgs());
|
|
}
|
|
}
|
|
public object this[int index]
|
|
{
|
|
get
|
|
{
|
|
if (index >= 0 && index < this.List.Count)
|
|
{
|
|
return this.List[index];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public String GetClassName()
|
|
{
|
|
return TypeDescriptor.GetClassName(this, true);
|
|
}
|
|
|
|
public AttributeCollection GetAttributes()
|
|
{
|
|
return TypeDescriptor.GetAttributes(this, true);
|
|
}
|
|
|
|
public String GetComponentName()
|
|
{
|
|
return TypeDescriptor.GetComponentName(this, true);
|
|
}
|
|
|
|
public TypeConverter GetConverter()
|
|
{
|
|
return TypeDescriptor.GetConverter(this, true);
|
|
}
|
|
|
|
public EventDescriptor GetDefaultEvent()
|
|
{
|
|
return TypeDescriptor.GetDefaultEvent(this, true);
|
|
}
|
|
|
|
public PropertyDescriptor GetDefaultProperty()
|
|
{
|
|
return TypeDescriptor.GetDefaultProperty(this, true);
|
|
}
|
|
|
|
public object GetEditor(Type editorBaseType)
|
|
{
|
|
return TypeDescriptor.GetEditor(this, editorBaseType, true);
|
|
}
|
|
|
|
public EventDescriptorCollection GetEvents(Attribute[] attributes)
|
|
{
|
|
return TypeDescriptor.GetEvents(this, attributes, true);
|
|
}
|
|
|
|
public EventDescriptorCollection GetEvents()
|
|
{
|
|
return TypeDescriptor.GetEvents(this, true);
|
|
}
|
|
|
|
public object GetPropertyOwner(PropertyDescriptor pd)
|
|
{
|
|
return this;
|
|
}
|
|
|
|
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
|
|
{
|
|
return GetProperties();
|
|
}
|
|
|
|
public PropertyDescriptorCollection GetProperties()
|
|
{
|
|
PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
|
|
|
|
for (int i = 0; i < this.List.Count; i++)
|
|
{
|
|
ContainerObjectDescriptor pd = new ContainerObjectDescriptor(this, i);
|
|
pds.Add(pd);
|
|
}
|
|
return pds;
|
|
}
|
|
}
|
|
|
|
public class ContainerObjectDescriptor : PropertyDescriptor
|
|
{
|
|
private ContainerCollectionClass collection = null;
|
|
private int index = -1;
|
|
|
|
public ContainerObjectDescriptor(ContainerCollectionClass coll, int idx)
|
|
: base("#" + idx.ToString(), null)
|
|
{
|
|
this.collection = coll;
|
|
this.index = idx;
|
|
}
|
|
|
|
public override AttributeCollection Attributes
|
|
{
|
|
get
|
|
{
|
|
return new AttributeCollection(null);
|
|
}
|
|
}
|
|
|
|
public override bool CanResetValue(object component)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override Type ComponentType
|
|
{
|
|
get
|
|
{
|
|
return this.collection.GetType();
|
|
}
|
|
}
|
|
|
|
public override string DisplayName
|
|
{
|
|
get
|
|
{
|
|
if (index >= 0 && index < this.collection.Count)
|
|
{
|
|
IObjectNodeInterface emp = (IObjectNodeInterface)this.collection[index];
|
|
return emp.GetMyType().m_name;
|
|
}
|
|
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public override string Description
|
|
{
|
|
get
|
|
{
|
|
object emp = this.collection[index];
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append(emp.GetType().Name);
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
|
|
public override object GetValue(object component)
|
|
{
|
|
return this.collection[index];
|
|
}
|
|
|
|
public override bool IsReadOnly
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
public override string Name
|
|
{
|
|
get { return "#" + index.ToString(); }
|
|
}
|
|
|
|
public override Type PropertyType
|
|
{
|
|
get
|
|
{
|
|
if (index >= 0 && index < this.collection.Count)
|
|
{
|
|
return this.collection[index].GetType();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public override void ResetValue(object component) { }
|
|
|
|
public override bool ShouldSerializeValue(object component)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override void SetValue(object component, object value)
|
|
{
|
|
}
|
|
}
|
|
|
|
public class RemoveContainerObjectEditor : System.Drawing.Design.UITypeEditor
|
|
{
|
|
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
|
|
{
|
|
if (context != null && context.Instance != null)
|
|
{
|
|
if (!context.PropertyDescriptor.IsReadOnly)
|
|
{
|
|
return UITypeEditorEditStyle.Modal;
|
|
}
|
|
}
|
|
return UITypeEditorEditStyle.None;
|
|
}
|
|
|
|
[RefreshProperties(RefreshProperties.All)]
|
|
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
|
|
{
|
|
if (context == null || provider == null || context.Instance == null)
|
|
{
|
|
return base.EditValue(provider, value);
|
|
}
|
|
|
|
IObjectNodeInterface objectNode = (IObjectNodeInterface)value;
|
|
IObjectNodeInterface parentNode = objectNode.GetParent();
|
|
ContainerCollectionClass containerParent = (ContainerCollectionClass)objectNode.GetParent();
|
|
|
|
FormMenuObjectOfContainer menu = new FormMenuObjectOfContainer();
|
|
menu.Location = new Point(Cursor.Position.X - menu.Width + 5, Cursor.Position.Y - 5);
|
|
menu.CanMoveUp = containerParent.canMoveUp(objectNode);
|
|
menu.CanMoveDown = containerParent.canMoveDown(objectNode);
|
|
menu.CanSwap = parentNode != null && parentNode is FactoryVar;
|
|
|
|
menu.ShowDialog();
|
|
|
|
if (menu.OptionSelect == CustomComponent.FormMenuObjectOfContainer.Options.Remove)
|
|
{
|
|
containerParent.Remove(objectNode);
|
|
}
|
|
else if (menu.OptionSelect == CustomComponent.FormMenuObjectOfContainer.Options.MoveUp)
|
|
{
|
|
containerParent.MoveUp(objectNode);
|
|
}
|
|
else if (menu.OptionSelect == CustomComponent.FormMenuObjectOfContainer.Options.MoveDown)
|
|
{
|
|
containerParent.MoveDown(objectNode);
|
|
}
|
|
else if (menu.OptionSelect == CustomComponent.FormMenuObjectOfContainer.Options.Swap)
|
|
{
|
|
if (parentNode != null)
|
|
{
|
|
if (parentNode is FactoryVar)
|
|
{
|
|
FactoryVar factory = (FactoryVar)parentNode;
|
|
|
|
CommonDialogs.SelectItemListDlg dlg = new CommonDialogs.SelectItemListDlg();
|
|
|
|
dlg.SetComponents(factory.GetTypes());
|
|
|
|
if (dlg.ShowDialog() == DialogResult.OK)
|
|
{
|
|
containerParent.Remove(objectNode);
|
|
|
|
factory.SetIsObject(true);
|
|
factory.CreateObject(dlg.SelectedComponent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return value;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|