This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
RangeTypeConverter.cs
252 lines (225 loc) · 10.5 KB
/
RangeTypeConverter.cs
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Accord Control Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Controls
{
using System;
using System.ComponentModel;
using System.Globalization;
using AForge;
using System.Collections;
/// <summary>
/// Type converter for <see cref="DoubleRange"/>
/// and <see cref="IntRange"/> objects.
/// </summary>
///
public class RangeTypeConverter : TypeConverter
{
/// <summary>
/// Returns true.
/// </summary>
///
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
/// <summary>
/// Creates an instance of the type that this <see cref="T:System.ComponentModel.TypeConverter"/> is associated with, using the specified context, given a set of property values for the object.
/// </summary>
///
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="propertyValues">An <see cref="T:System.Collections.IDictionary"/> of new property values.</param>
///
/// <returns>
/// An <see cref="T:System.Object"/> representing the given <see cref="T:System.Collections.IDictionary"/>, or null if the object cannot be created. This method always returns null.
/// </returns>
///
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues != null)
{
if (context.PropertyDescriptor.PropertyType == typeof(DoubleRange))
{
double max = (double)propertyValues["Max"];
double min = (double)propertyValues["Min"];
return new DoubleRange(min, max);
}
else if (context.PropertyDescriptor.PropertyType == typeof(IntRange))
{
int max = (int)propertyValues["Max"];
int min = (int)propertyValues["Min"];
return new IntRange(min, max);
}
}
return base.CreateInstance(context, propertyValues);
}
/// <summary>
/// Returns true.
/// </summary>
///
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
/// <summary>
/// Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.
/// </summary>
///
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="value">An <see cref="T:System.Object"/> that specifies the type of array for which to get properties.</param>
/// <param name="attributes">An array of type <see cref="T:System.Attribute"/> that is used as a filter.</param>
///
/// <returns>
/// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> with the properties that are exposed for this data type, or null if there are no properties.
/// </returns>
///
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(value);
}
/// <summary>
/// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
/// </summary>
///
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="sourceType">A <see cref="T:System.Type"/> that represents the type you want to convert from.</param>
///
/// <returns>
/// true if this converter can perform the conversion; otherwise, false.
/// </returns>
///
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(DoubleRange) ||
sourceType == typeof(IntRange))
return true;
return base.CanConvertFrom(context, sourceType);
}
/// <summary>
/// Returns whether this converter can convert the object to the specified type, using the specified context.
/// </summary>
///
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="destinationType">A <see cref="T:System.Type"/> that represents the type you want to convert to.</param>
///
/// <returns>
/// true if this converter can perform the conversion; otherwise, false.
/// </returns>
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String))
return true;
return base.CanConvertTo(context, destinationType);
}
/// <summary>
/// Converts the given value object to the specified type, using the specified context and culture information.
/// </summary>
///
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"/>. If null is passed, the current culture is assumed.</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param>
///
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value.
/// </returns>
///
/// <exception cref="T:System.ArgumentNullException">
/// The <paramref name="destinationType"/> parameter is null.
/// </exception>
///
/// <exception cref="T:System.NotSupportedException">
/// The conversion cannot be performed.
/// </exception>
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, Type destinationType)
{
if (value is DoubleRange)
{
DoubleRange doubleRange = (DoubleRange)value;
return String.Format(CultureInfo.CurrentCulture,
"[{0}; {1}]", doubleRange.Min, doubleRange.Max);
}
if (value is IntRange)
{
IntRange intRange = (IntRange)value;
return String.Format(CultureInfo.CurrentCulture,
"[{0}; {1}]", intRange.Min, intRange.Max);
}
return base.ConvertTo(context, culture, value, destinationType);
}
/// <summary>
/// Converts the given object to the type of this converter, using the specified context and culture information.
/// </summary>
///
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
///
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value.
/// </returns>
///
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
return base.ConvertFrom(context, culture, value);
String str = value as String;
if (str == null)
return base.ConvertFrom(context, culture, value);
str = str.Replace("(", String.Empty);
str = str.Replace(")", String.Empty);
str = str.Replace("[", String.Empty);
str = str.Replace("]", String.Empty);
String[] parts = str.Split(';');
string a = parts[0];
string b = parts[1];
if (context.PropertyDescriptor.PropertyType == typeof(IntRange))
{
int max = int.Parse(a);
int min = int.Parse(b);
return new IntRange(min, max);
}
else if (context.PropertyDescriptor.PropertyType == typeof(DoubleRange))
{
double max = double.Parse(a);
double min = double.Parse(b);
return new DoubleRange(min, max);
}
return base.ConvertFrom(context, culture, value);
}
/// <summary>
/// Assign this converter to the AForge types. This method
/// should be called before an AForge range type can be
/// bound to controls such as the PropertyGrid.
/// </summary>
///
public static void Assign()
{
TypeDescriptor.AddAttributes(typeof(DoubleRange), new TypeConverterAttribute(typeof(RangeTypeConverter)));
TypeDescriptor.AddAttributes(typeof(IntRange), new TypeConverterAttribute(typeof(RangeTypeConverter)));
}
}
}