Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/RangeConverter.cs @ 12503

Last change on this file since 12503 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 2.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ComponentModel;
6using System.Globalization;
7using Microsoft.Research.DynamicDataDisplay.Charts;
8
9namespace Microsoft.Research.DynamicDataDisplay.Common
10{
11  public sealed class RangeConverter : TypeConverter
12  {
13    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
14    {
15      return (sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType);
16    }
17
18    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
19    {
20      return (destinationType == typeof(string)) || base.CanConvertTo(context, destinationType);
21    }
22
23    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
24    {
25      if (value == null)
26      {
27        throw base.GetConvertFromException(value);
28      }
29
30      string source = value as string;
31      if (source != null)
32      {
33        var parts = source.Split('-');
34        var minStr = parts[0];
35        var maxStr = parts[1];
36
37        int minInt32 = 0;
38        double minDouble = 0;
39        DateTime minDateTime = DateTime.Now;
40        if (Int32.TryParse(minStr, NumberStyles.Integer, culture, out minInt32))
41        {
42          int maxInt32 = Int32.Parse(maxStr, NumberStyles.Integer, culture);
43
44          return new Range<int>(minInt32, maxInt32);
45        }
46        else if (Double.TryParse(minStr, NumberStyles.Float, culture, out minDouble))
47        {
48          double maxDouble = Double.Parse(maxStr, NumberStyles.Float, culture);
49          return new Range<double>(minDouble, maxDouble);
50        }
51        else if (DateTime.TryParse(minStr, culture, DateTimeStyles.None, out minDateTime))
52        {
53          DateTime maxDateTime = DateTime.Parse(maxStr, culture);
54          return new Range<DateTime>(minDateTime, maxDateTime);
55        }
56      }
57
58      return base.ConvertFrom(context, culture, value);
59    }
60
61    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
62    {
63      if (destinationType != null && value is DataRect)
64      {
65        DataRect rect = (DataRect)value;
66        if (destinationType == typeof(string))
67        {
68          return rect.ConvertToString(null, culture);
69        }
70      }
71      return base.ConvertTo(context, culture, value, destinationType);
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.