06366ff7236d0ee56b20f4dad5b54fbf2c0e4ee9
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / FromExpression / Framework / Data / SwitchConverter.cs
1 // -------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All Rights Reserved.
3 // -------------------------------------------------------------------
4 //From \\authoring\Sparkle\Source\1.0.1083.0\Common\Source\Framework\Data
5 namespace System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.Data
6 {
7     using System;
8     using System.Collections.Generic;
9     using System.ComponentModel;
10     using System.Diagnostics;
11     using System.Globalization;
12     using System.Reflection;
13     using System.Windows;
14     using System.Windows.Data;
15     using System.Windows.Markup;
16     using System.Windows.Media;
17     using System.Activities.Presentation;
18
19     // <summary>
20     // Transformer which maps from input values to output values, based on a list of SwitchCase children.
21     // This isn't strictly a C-style 'switch' statement, since cases aren't guaranteed to be unique.
22     // </summary>
23     //
24     [ContentProperty("Cases")]
25     internal class SwitchConverter : DependencyObject, IValueConverter
26     {
27         static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(object), typeof(SwitchConverter));
28         private List<SwitchCase> cases;
29
30         public SwitchConverter()
31         {
32             this.cases = new List<SwitchCase>();
33         }
34
35         public List<SwitchCase> Cases
36         {
37             get { return this.cases; }
38         }
39
40         public object DefaultValue
41         {
42             get { return this.GetValue(SwitchConverter.DefaultValueProperty); }
43             set { this.SetValue(SwitchConverter.DefaultValueProperty, value); }
44         }
45
46         // IValueConverter implementation
47         public object Convert(object o, Type targetType, object parameter, CultureInfo culture)
48         {
49             foreach (SwitchCase switchCase in this.Cases)
50             {
51                 if (object.Equals(switchCase.In, o))
52                 {
53                     return switchCase.Out;
54                 }
55             }
56
57             return this.DefaultValue;
58         }
59
60         public object ConvertBack(object o, Type targetType, object parameter, CultureInfo culture)
61         {
62             throw FxTrace.Exception.AsError(new InvalidOperationException(ExceptionStringTable.SwitchConverterIsOneWay));
63         }
64
65     }
66
67     // <summary>
68     // Represents a mapping from an input value to an output value.
69     // </summary>
70     internal class SwitchCase : DependencyObject
71     {
72         static readonly DependencyProperty InProperty = DependencyProperty.Register("In", typeof(object), typeof(SwitchCase));
73         static readonly DependencyProperty OutProperty = DependencyProperty.Register("Out", typeof(object), typeof(SwitchCase));
74
75         public SwitchCase()
76         {
77         }
78
79         // Properties
80         public object In
81         {
82             get { return (object)this.GetValue(InProperty); }
83             set { this.SetValue(InProperty, value); }
84         }
85
86         public object Out
87         {
88             get { return this.GetValue(OutProperty); }
89             set { this.SetValue(OutProperty, value); }
90         }
91     }
92
93     // <summary>
94     // Convenience class for getting at a particular type.  Useful for databinding.
95     // Used in XAML as: <TypeReference Type="*typeof(Foo)" />
96     // </summary>
97     internal sealed class TypeReference : DependencyObject
98     {
99         static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(Type), typeof(TypeReference));
100
101         public Type Type
102         {
103             get { return (Type)this.GetValue(TypeProperty); }
104             set { this.SetValue(TypeProperty, value); }
105         }
106     }
107 }