209 lines
5.6 KiB
C#
209 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Xml;
|
|
|
|
namespace JD.Winforms.XmlGrid
|
|
{
|
|
[TypeConverter(typeof(EnumVarConverter))]
|
|
public class EnumVar : BaseObjectNode
|
|
{
|
|
private List<string> enumNames;
|
|
private List<int> enumValues;
|
|
private int selection;
|
|
private string name;
|
|
|
|
private EnumVar()
|
|
{
|
|
enumNames = new List<string>();
|
|
enumValues = new List<int>();
|
|
selection = 0;
|
|
name = "";
|
|
}
|
|
|
|
public string Name
|
|
{
|
|
get { return name; }
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
if (enumNames.Count > 0 && selection >= 0 && selection < enumNames.Count)
|
|
{
|
|
return enumNames[selection];
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public List<string> GetEnumNames()
|
|
{
|
|
return enumNames;
|
|
}
|
|
|
|
public List<int> GetEnumValues()
|
|
{
|
|
return enumValues;
|
|
}
|
|
|
|
static public EnumVar ParseXml(XmlNode Node, IObjectNodeInterface Parent)
|
|
{
|
|
EnumVar newEnum = new EnumVar();
|
|
|
|
int selection = int.Parse(Node.Attributes["iSEL"].Value);
|
|
int selectionIndex = 0;
|
|
|
|
newEnum.SetParent(Parent);
|
|
newEnum.name = Node.Attributes["strNAME"].Value;
|
|
|
|
foreach (XmlNode child in Node.ChildNodes)
|
|
{
|
|
string enumOption = child.Attributes["strNAME"].Value;
|
|
int enumValue = int.Parse(child.Attributes["iNUM"].Value);
|
|
|
|
if (enumValue == selection)
|
|
{
|
|
selectionIndex = newEnum.enumValues.Count;
|
|
}
|
|
|
|
newEnum.enumNames.Add(enumOption);
|
|
newEnum.enumValues.Add(enumValue);
|
|
}
|
|
|
|
newEnum.selection = selectionIndex;
|
|
|
|
return newEnum;
|
|
}
|
|
|
|
public override void SerializeIntoXml(ref string outText)
|
|
{
|
|
outText += $"<ENUM strNAME=\"{name}\" iSEL=\"{enumValues[selection]}\">";
|
|
|
|
int count = 0;
|
|
|
|
foreach (string s in enumNames)
|
|
{
|
|
outText += $"<VAR strNAME=\"{s}\" iNUM=\"{enumValues[selection]}\"/>";
|
|
count++;
|
|
}
|
|
|
|
outText += "</ENUM>\n\r";
|
|
}
|
|
|
|
public static EnumVar ParseData(string enumName, List<string> NewEnums, List <int> EnumValues, string Value)
|
|
{
|
|
EnumVar newEnum = new EnumVar();
|
|
newEnum.name = enumName;
|
|
newEnum.enumNames = NewEnums;
|
|
newEnum.enumValues = EnumValues;
|
|
|
|
int count = 0;
|
|
|
|
foreach (string s in NewEnums)
|
|
{
|
|
if (s == Value)
|
|
{
|
|
newEnum.selection = count;
|
|
break;
|
|
}
|
|
|
|
count++;
|
|
}
|
|
|
|
return newEnum;
|
|
}
|
|
|
|
public void Select(int i)
|
|
{
|
|
selection = i;
|
|
}
|
|
|
|
public void Select(string value)
|
|
{
|
|
int count = 0;
|
|
|
|
foreach (string s in enumNames)
|
|
{
|
|
if (s == value)
|
|
{
|
|
selection = count;
|
|
break;
|
|
}
|
|
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
|
|
class EnumVarConverter : TypeConverter
|
|
{
|
|
private List<string> enumNames;
|
|
private List<int> enumValues;
|
|
string enumName;
|
|
|
|
public EnumVarConverter()
|
|
{
|
|
enumNames = new List<string>();
|
|
enumValues = new List<int>();
|
|
enumName = "";
|
|
}
|
|
|
|
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
|
{
|
|
return new StandardValuesCollection(enumNames.ToArray());
|
|
}
|
|
|
|
public override bool CanConvertTo(ITypeDescriptorContext context,
|
|
System.Type destinationType)
|
|
{
|
|
if (destinationType == typeof(string))
|
|
return true;
|
|
|
|
return base.CanConvertTo(context, destinationType);
|
|
}
|
|
|
|
public override object ConvertTo(ITypeDescriptorContext context,
|
|
CultureInfo culture,
|
|
object value,
|
|
System.Type destinationType)
|
|
{
|
|
if (destinationType == typeof(System.String) && value is EnumVar)
|
|
{
|
|
EnumVar so = (EnumVar)value;
|
|
enumName = so.Name;
|
|
enumNames = so.GetEnumNames();
|
|
enumValues = so.GetEnumValues();
|
|
return so.ToString();
|
|
}
|
|
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|
}
|
|
|
|
public override bool CanConvertFrom(ITypeDescriptorContext context,
|
|
System.Type sourceType)
|
|
{
|
|
if (sourceType == typeof(string))
|
|
return true;
|
|
|
|
return base.CanConvertFrom(context, sourceType);
|
|
}
|
|
|
|
public override object ConvertFrom(ITypeDescriptorContext context,
|
|
CultureInfo culture, object value)
|
|
{
|
|
if (value is string)
|
|
{
|
|
EnumVar ret = EnumVar.ParseData(enumName,enumNames,enumValues,(string)value);
|
|
return ret;
|
|
}
|
|
return base.ConvertFrom(context, culture, value);
|
|
}
|
|
}
|
|
}
|