1 | using System;
|
---|
2 | using System.ComponentModel;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Globalization;
|
---|
5 | namespace Netron.Diagramming.Core {
|
---|
6 | /// <summary>
|
---|
7 | /// Type converter for a <see cref="Point"/> structure.
|
---|
8 | /// </summary>
|
---|
9 | public class PointConverter : TypeConverter {
|
---|
10 | /// <summary>
|
---|
11 | /// Determines whether this instance can convert from the specified context.
|
---|
12 | /// </summary>
|
---|
13 | /// <param name="context">The context.</param>
|
---|
14 | /// <param name="t">The t.</param>
|
---|
15 | /// <returns>
|
---|
16 | /// <c>true</c> if this instance [can convert from] the specified context; otherwise, <c>false</c>.
|
---|
17 | /// </returns>
|
---|
18 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type t) {
|
---|
19 | if (t == typeof(string)) {
|
---|
20 | return true;
|
---|
21 | }
|
---|
22 | return base.CanConvertFrom(context, t);
|
---|
23 | }
|
---|
24 | /// <summary>
|
---|
25 | /// Converts the point to a string representation.
|
---|
26 | /// </summary>
|
---|
27 | /// <param name="context">The context.</param>
|
---|
28 | /// <param name="culture">The culture.</param>
|
---|
29 | /// <param name="value">The value.</param>
|
---|
30 | /// <param name="destinationType">Type of the destination.</param>
|
---|
31 | /// <returns></returns>
|
---|
32 | public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
|
---|
33 | if (value is Point) {
|
---|
34 | Point point = (Point)value;
|
---|
35 | return "(" + point.X + "," + point.Y + ")";
|
---|
36 | }
|
---|
37 | return base.ConvertTo(context, culture, value, destinationType);
|
---|
38 |
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// Converts the given object to the type of this converter, using the specified context and culture information.
|
---|
44 | /// </summary>
|
---|
45 | /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
|
---|
46 | /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"></see> to use as the current culture.</param>
|
---|
47 | /// <param name="value">The <see cref="T:System.Object"></see> to convert.</param>
|
---|
48 | /// <returns>
|
---|
49 | /// An <see cref="T:System.Object"></see> that represents the converted value.
|
---|
50 | /// </returns>
|
---|
51 | /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
|
---|
52 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
|
---|
53 | if (value is string) {
|
---|
54 |
|
---|
55 | try {
|
---|
56 | string thething = (string)value;
|
---|
57 | //remove brackets if any
|
---|
58 | thething = thething.Replace("(", "").Replace(")", "");
|
---|
59 | //now we should have only a comma
|
---|
60 | string[] parts = thething.Split(new char[] { ',' });
|
---|
61 | return new Point(int.Parse(parts[0]), int.Parse(parts[1]));
|
---|
62 | }
|
---|
63 | catch (Exception) {
|
---|
64 |
|
---|
65 | return Point.Empty;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return base.ConvertFrom(context, culture, value);
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Returns whether this object supports properties, using the specified context.
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
|
---|
78 | /// <returns>
|
---|
79 | /// true if <see cref="M:System.ComponentModel.TypeConverter.GetProperties(System.Object)"></see> should be called to find the properties of this object; otherwise, false.
|
---|
80 | /// </returns>
|
---|
81 | public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /// <summary>
|
---|
86 | /// Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.
|
---|
87 | /// </summary>
|
---|
88 | /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
|
---|
89 | /// <param name="value">An <see cref="T:System.Object"></see> that specifies the type of array for which to get properties.</param>
|
---|
90 | /// <param name="attributes">An array of type <see cref="T:System.Attribute"></see> that is used as a filter.</param>
|
---|
91 | /// <returns>
|
---|
92 | /// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"></see> with the properties that are exposed for this data type, or null if there are no properties.
|
---|
93 | /// </returns>
|
---|
94 | public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
|
---|
95 | return TypeDescriptor.GetProperties(value, attributes);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|