783742c5af059f472f442826c0b26820dae0eb90
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Editors / FlagPanel.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors
6 {
7     using System.Windows.Controls;
8     using System.Windows.Data;
9     using System.Globalization;
10     using System.Windows;
11     using System.Windows.Automation;
12     using System.Runtime;
13     sealed class FlagPanel : StackPanel
14     {
15         public static readonly DependencyProperty FlagStringProperty =
16             DependencyProperty.Register("FlagString", typeof(string), typeof(FlagPanel), new PropertyMetadata(string.Empty));
17
18         public static readonly DependencyProperty FlagTypeProperty = DependencyProperty.Register(
19             "FlagType",
20             typeof(Type),
21             typeof(FlagPanel),
22             new PropertyMetadata(null, new PropertyChangedCallback(OnFlagTypeChanged)));       
23
24         public Type FlagType
25         {
26             get { return (Type)GetValue(FlagTypeProperty); }
27             set { SetValue(FlagTypeProperty, value); }
28         }
29
30         public string FlagString
31         {
32             get { return (string)GetValue(FlagStringProperty); }
33             set { SetValue(FlagStringProperty, value); }
34         }
35
36         static void OnFlagTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
37         {
38             Type flagType = args.NewValue as Type;
39             Fx.Assert(flagType == null || flagType.IsEnum, "FlagType should be null or enum");
40             Fx.Assert(flagType == null || Attribute.IsDefined(flagType, typeof(FlagsAttribute)), "FlagType should be null or have flags attribute");
41
42             if (flagType == null)
43             {
44                 return;
45             }
46
47             int index = 0;
48             FlagPanel panel = sender as FlagPanel;
49             string[] flagNames = flagType.GetEnumNames();
50             string zeroValueString = Enum.ToObject(flagType, 0).ToString();
51             foreach (string flagName in flagNames)
52             {
53                 if (zeroValueString.Equals("0") || !flagName.Equals(zeroValueString))
54                 {
55                     CheckBox checkBox = new CheckBox();
56                     panel.Children.Add(checkBox);
57                     checkBox.Content = flagName;
58                     checkBox.DataContext = panel;
59                     checkBox.SetValue(AutomationProperties.AutomationIdProperty, flagName);
60                     Binding binding = new Binding("FlagString");
61                     binding.Mode = BindingMode.TwoWay;
62                     binding.Converter = new CheckBoxStringConverter(index);
63                     binding.ConverterParameter = panel;
64                     checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
65                     index++;
66                 }
67             }
68         }
69
70         sealed class CheckBoxStringConverter : IValueConverter
71         {
72             int index;
73
74             public CheckBoxStringConverter(int index)
75             {
76                 this.index = index;
77             }
78
79             public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
80             {
81                 string str = (value as string).ToUpperInvariant();
82                 FlagPanel panel = parameter as FlagPanel;
83                 if (str.Contains((panel.Children[this.index] as CheckBox).Content.ToString().ToUpperInvariant()))
84                 {
85                     return true;
86                 }
87                 else
88                 {
89                     return false;
90                 }
91             }
92
93             public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
94             {
95                 FlagPanel panel = parameter as FlagPanel;
96                 string str = string.Empty;
97                 for (int i = 0; i < panel.Children.Count; i++)
98                 {
99                     if ((i != this.index && ((bool)(panel.Children[i] as CheckBox).IsChecked)) ||
100                         (i == this.index && (bool)value))
101                     {
102                         if (!string.IsNullOrEmpty(str))
103                         {
104                             str += ", ";
105                         }
106                         str += (panel.Children[i] as CheckBox).Content.ToString();
107                     }
108                 }
109                 if (string.IsNullOrEmpty(str))
110                 {                    
111                     Type flagType = panel.FlagType;
112                     Fx.Assert(flagType != null && flagType.IsEnum, "FlagType should be enum");
113                     Fx.Assert(Attribute.IsDefined(flagType, typeof(FlagsAttribute)), "FlagType should have flags attribute");
114
115                     return Enum.ToObject(flagType, 0).ToString();                    
116                 }
117                 return str;
118             }
119         }
120     }
121 }