Zachary Turner | 1122be8 | 2016-09-07 18:28:55 +0000 | [diff] [blame] | 1 | using System;
|
| 2 | using System.Collections;
|
| 3 | using System.Collections.Generic;
|
| 4 | using System.ComponentModel;
|
| 5 | using System.Globalization;
|
| 6 | using System.Linq;
|
| 7 | using System.Text;
|
| 8 | using System.Threading.Tasks;
|
| 9 |
|
| 10 | namespace LLVM.ClangTidy
|
| 11 | {
|
| 12 | class MagicInheritance
|
| 13 | {
|
| 14 | public static readonly string Value = "{3A27184D-1774-489B-9BB7-7191B8E8E622}";
|
| 15 | public static readonly string Text = "<Inherit from project or parent>";
|
| 16 | }
|
| 17 |
|
| 18 |
|
| 19 | class DynamicPropertyConverter<T> : TypeConverter
|
| 20 | {
|
| 21 | private DynamicPropertyDescriptor<T> Descriptor_;
|
| 22 | private TypeConverter Root_;
|
| 23 |
|
| 24 | public DynamicPropertyConverter(DynamicPropertyDescriptor<T> Descriptor, TypeConverter Root)
|
| 25 | {
|
| 26 | Descriptor_ = Descriptor;
|
| 27 | Root_ = Root;
|
| 28 | }
|
| 29 |
|
| 30 | /// <summary>
|
| 31 | /// Returns true if there are specific values that can be chosen from a dropdown
|
| 32 | /// for this property. Regardless of whether standard values are supported for
|
| 33 | /// the underlying type, we always support standard values because we need to
|
| 34 | /// display the inheritance option.
|
| 35 | /// </summary>
|
| 36 | /// <returns>true</returns>
|
| 37 | public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
|
| 38 | {
|
| 39 | return true;
|
| 40 | }
|
| 41 |
|
| 42 | /// <summary>
|
| 43 | /// Get the set of all standard values that can be chosen from a dropdown for this
|
| 44 | /// property. If the underlying type supports standard values, we want to include
|
| 45 | /// all those. Additionally, we want to display the option to inherit the value,
|
| 46 | /// but only if the value is not already inheriting.
|
| 47 | /// </summary>
|
| 48 | public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
|
| 49 | {
|
| 50 | List<object> Values = new List<object>();
|
| 51 | if (Root_.GetStandardValuesSupported(context))
|
| 52 | {
|
| 53 | StandardValuesCollection RootValues = Root_.GetStandardValues(context);
|
| 54 | Values.AddRange(RootValues.Cast<object>());
|
| 55 | }
|
| 56 | if (!Descriptor_.IsInheriting)
|
| 57 | Values.Add(MagicInheritance.Value);
|
| 58 | StandardValuesCollection Result = new StandardValuesCollection(Values);
|
| 59 | return Result;
|
| 60 | }
|
| 61 |
|
| 62 | /// <summary>
|
| 63 | /// Determines whether this property can accept values other than those specified
|
| 64 | /// in the dropdown (for example by manually typing into the field).
|
| 65 | /// </summary>
|
| 66 | public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
|
| 67 | {
|
| 68 | // Although we add items to the dropdown list, we do not change whether or not
|
| 69 | // the set of values are exclusive. If the user could type into the field before
|
| 70 | // they still can. And if they couldn't before, they still can't.
|
| 71 | return Root_.GetStandardValuesExclusive(context);
|
| 72 | }
|
| 73 |
|
| 74 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
| 75 | {
|
| 76 | return Root_.CanConvertFrom(context, sourceType);
|
| 77 | }
|
| 78 |
|
| 79 | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
|
| 80 | {
|
| 81 | return Root_.CanConvertTo(context, destinationType);
|
| 82 | }
|
| 83 |
|
| 84 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
| 85 | {
|
| 86 | if (value.Equals(MagicInheritance.Value))
|
| 87 | return MagicInheritance.Text;
|
| 88 | return Root_.ConvertFrom(context, culture, value);
|
| 89 | }
|
| 90 |
|
| 91 | public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
| 92 | {
|
| 93 | if (value.GetType() == destinationType)
|
| 94 | return value;
|
| 95 |
|
| 96 | return Root_.ConvertTo(context, culture, value, destinationType);
|
| 97 | }
|
| 98 |
|
| 99 | public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
|
| 100 | {
|
| 101 | return Root_.CreateInstance(context, propertyValues);
|
| 102 | }
|
| 103 |
|
| 104 | public override bool Equals(object obj)
|
| 105 | {
|
| 106 | return Root_.Equals(obj);
|
| 107 | }
|
| 108 |
|
| 109 | public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
|
| 110 | {
|
| 111 | return Root_.GetCreateInstanceSupported(context);
|
| 112 | }
|
| 113 |
|
| 114 | public override int GetHashCode()
|
| 115 | {
|
| 116 | return Root_.GetHashCode();
|
| 117 | }
|
| 118 |
|
| 119 | public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
|
| 120 | {
|
| 121 | return Root_.GetProperties(context, value, attributes);
|
| 122 | }
|
| 123 |
|
| 124 | public override bool GetPropertiesSupported(ITypeDescriptorContext context)
|
| 125 | {
|
| 126 | return Root_.GetPropertiesSupported(context);
|
| 127 | }
|
| 128 |
|
| 129 | public override bool IsValid(ITypeDescriptorContext context, object value)
|
| 130 | {
|
| 131 | return Root_.IsValid(context, value);
|
| 132 | }
|
| 133 |
|
| 134 | public override string ToString()
|
| 135 | {
|
| 136 | return Root_.ToString();
|
| 137 | }
|
| 138 | }
|
| 139 | }
|