using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
namespace Netron.Diagramming.Core {
///
/// Type converter for a structure.
///
public class PointConverter : TypeConverter {
///
/// Determines whether this instance can convert from the specified context.
///
/// The context.
/// The t.
///
/// true if this instance [can convert from] the specified context; otherwise, false.
///
public override bool CanConvertFrom(ITypeDescriptorContext context, Type t) {
if (t == typeof(string)) {
return true;
}
return base.CanConvertFrom(context, t);
}
///
/// Converts the point to a string representation.
///
/// The context.
/// The culture.
/// The value.
/// Type of the destination.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (value is Point) {
Point point = (Point)value;
return "(" + point.X + "," + point.Y + ")";
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
/// Converts the given object to the type of this converter, using the specified context and culture information.
///
/// An that provides a format context.
/// The to use as the current culture.
/// The to convert.
///
/// An that represents the converted value.
///
/// The conversion cannot be performed.
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
try {
string thething = (string)value;
//remove brackets if any
thething = thething.Replace("(", "").Replace(")", "");
//now we should have only a comma
string[] parts = thething.Split(new char[] { ',' });
return new Point(int.Parse(parts[0]), int.Parse(parts[1]));
}
catch (Exception) {
return Point.Empty;
}
}
return base.ConvertFrom(context, culture, value);
}
///
/// Returns whether this object supports properties, using the specified context.
///
/// An that provides a format context.
///
/// true if should be called to find the properties of this object; otherwise, false.
///
public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
return true;
}
///
/// Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.
///
/// An that provides a format context.
/// An that specifies the type of array for which to get properties.
/// An array of type that is used as a filter.
///
/// A with the properties that are exposed for this data type, or null if there are no properties.
///
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
return TypeDescriptor.GetProperties(value, attributes);
}
}
}