1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
using System;
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Text.RegularExpressions;
namespace ThermometerControl
{
// [Serializable()]
[TypeConverter(typeof(SensorIndicatorConverter))]
public struct IndicatorProperty
{
float _value;
Color _color;
string _label;
public IndicatorProperty(string label, Color color, float value)
: this()
{
_value = value;
_color = color;
_label = label;
}
/// <summary>
/// instance utilisé lors d'une modifications des attributs sous format texte.
/// </summary>
/// <param name="str">Chaine modifiée dans "property grid" pour l'enssemble des attributs de "Indicator" à décoder</param>
public IndicatorProperty(string str)
{
_value = 0;
_color = Color.Black;
_label = "";
if (string.IsNullOrEmpty(str)) throw new ArgumentNullException();
/* decode le string, pour déterminer les valeurs */
string[] attributs = str.Split(';');
/* control des valeurs */
if (attributs.Length == 3)
{
try
{
_label = attributs[0];
_color = extracColor(attributs[1]);
_value = float.Parse(attributs[2]);
}
catch(Exception ex)
{
throw new Exception("Init ThermometerIndicator: erreur value, " + ex.Message);
// MessageBox.Show("Init ThermometerIndicator: erreur value, " + ex.Message);
}
}
else throw new Exception("Init ThermometerIndicator: erreur value");
}
/// <summary>
/// Chaine afficher dans "property grid" pour l'enssemble des attributs de "Indicator"
/// </summary>
/// <returns></returns>
public override string ToString()
{
return _label + ";" + _color.ToString() + ";" + _value.ToString();
}
public float Value
{
get { return _value; }
set { _value = value; }
}
public Color Color
{
get { return _color; }
set { _color = value; }
}
public String Label
{
get { return _label; }
set { _label = value; }
}
/// <summary>
/// Extrait la couleur d'une chaine ex: "Color [xxx]" (Avec xxx la couleur ex: "Color [Red]")
/// </summary>
/// <param name="str">chaine comprenant la couleur</param>
/// <returns>object Color</returns>
private Color extracColor(String str)
{
string pattern = @"^(Color \[)(.*)(\])$";
//string strInput = "Color [Red]";
string strColor;
Color color = Color.Empty;
try
{
Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase);
if (match.Success && match.Groups.Count == 4)
{
strColor = match.Groups[2].Value;
color = Color.FromName(strColor);
}
}
catch (Exception)
{
}
return color;
}
}
//Converter for define behavior on property grid
public class SensorIndicatorConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string strValue = value as string;
if (strValue != null)
{
return new IndicatorProperty(strValue);
}
return base.ConvertFrom(context, culture, value);
}
/// <summary>
/// Demande de Convertion des propriétées dans "property grid" de la propriété typée avec toutes les sous propriétés (Super Propriétée)
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value"></param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is IndicatorProperty)
{
if (destinationType == typeof(string))
{
IndicatorProperty ThermometerIndicator = (IndicatorProperty)value;
return ThermometerIndicator.ToString();
}
if (destinationType == typeof(InstanceDescriptor))
{
IndicatorProperty ThermometerIndicator = (IndicatorProperty)value;
ConstructorInfo ctor = typeof(IndicatorProperty).GetConstructor(new Type[] { typeof(string), typeof(Color), typeof(float) });
if (ctor != null)
{
return new InstanceDescriptor(ctor, new object[] { ThermometerIndicator.Label, ThermometerIndicator.Color, ThermometerIndicator.Value });
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
object label = propertyValues["Label"];
object color = propertyValues["Color"];
object value = propertyValues["Value"];
if (label == null || color == null || value == null || !(label is string) || !(color is Color) || !(value is float))
{
return null;
// throw new ArgumentException("values is not valid");
}
return new IndicatorProperty((string)label, (Color)color, (float)value);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(IndicatorProperty), attributes);
return props.Sort(new string[] { "Label", "Color" ,"Value"});
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
} |
Partager