Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / System.ComponentModel.DataAnnotations / DisplayAttribute.cs
1 //
2 // DisplayAttribute.cs
3 //
4 // Author:
5 //      David Stone <david@gixug.com>
6 //
7 // Copyright (C) 2010 David Stone
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.ComponentModel;
31
32 namespace System.ComponentModel.DataAnnotations
33 {
34 #if NET_4_0
35         [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Method, AllowMultiple = false)]
36         public sealed class DisplayAttribute : Attribute
37         {
38                 public Type ResourceType { get; set; }
39
40                 public string Description { get; set; }
41                 public string GroupName { get; set; }
42                 public string Name { get; set; }
43                 public string ShortName { get; set; }
44                 public string Prompt { get; set; }
45
46                 const string property_not_set_message = "The {0} property has not been set.  Use the Get{0} method to get the value.";
47                 const string localization_failed_message = "Cannot retrieve property '{0}' because localization failed. Type '{1} is not public or does not contain a public static string property with the name '{2}'.";
48
49                 bool? _autoGenerateField;
50                 public bool AutoGenerateField {
51                         get {
52                                 if (!_autoGenerateField.HasValue) {
53                                         throw new InvalidOperationException (string.Format (property_not_set_message, "AutoGenerateField"));
54                                 }
55                                 
56                                 return _autoGenerateField.Value;
57                         }
58                         set { _autoGenerateField = value; }
59                 }
60
61                 bool? _autoGenerateFilter;
62                 public bool AutoGenerateFilter {
63                         get {
64                                 if (_autoGenerateFilter == null) {
65                                         throw new InvalidOperationException (string.Format (property_not_set_message, "AutoGenerateFilter"));
66                                 }
67                                 
68                                 return _autoGenerateFilter.Value;
69                         }
70                         set { _autoGenerateFilter = value; }
71                 }
72
73                 int? _order;
74                 public int Order {
75                         get {
76                                 if (_order == null)
77                                         throw new InvalidOperationException (string.Format (property_not_set_message, "Order"));
78                                         
79                                 return _order.Value;
80                         }
81                         set { _order = value; }
82                 }
83
84                 private string GetLocalizedString (string propertyName, string key)
85                 {
86                         // If we don't have a resource or a key, go ahead and fall back on the key
87                         if (ResourceType == null || key == null)
88                                 return key;
89                         
90                         var property = ResourceType.GetProperty (key);
91                         
92                         // Strings are only valid if they are public static strings
93                         var isValid = false;
94                         if (ResourceType.IsVisible && property != null && property.PropertyType == typeof(string)) {
95                                 var getter = property.GetGetMethod ();
96                                 
97                                 // Gotta have a public static getter on the property
98                                 if (getter != null && getter.IsStatic && getter.IsPublic) {
99                                         isValid = true;
100                                 }
101                         }
102                         
103                         // If it's not valid, go ahead and throw an InvalidOperationException
104                         if (!isValid) {
105                                 var message = string.Format (localization_failed_message, propertyName, ResourceType.ToString (), key);
106                                 throw new InvalidOperationException (message);
107                         }
108                         
109                         return (string)property.GetValue (null, null);
110                         
111                 }
112
113                 #region Consumer Methods
114                 public bool? GetAutoGenerateField ()
115                 {
116                         return _autoGenerateField;
117                 }
118
119                 public bool? GetAutoGenerateFilter ()
120                 {
121                         return _autoGenerateFilter;
122                 }
123                 
124                 public int? GetOrder ()
125                 {
126                         return _order;
127                 }
128
129                 public string GetName ()
130                 {
131                         return GetLocalizedString ("Name", Name);
132                 }
133
134                 public string GetShortName ()
135                 {
136                         // Short name falls back on Name if the short name isn't set
137                         return GetLocalizedString ("ShortName", ShortName) ?? GetName ();
138                 }
139
140                 public string GetDescription ()
141                 {
142                         return GetLocalizedString ("Description", Description);
143                 }
144
145                 public string GetPrompt ()
146                 {
147                         return GetLocalizedString ("Prompt", Prompt);
148                 }
149                 
150                 public string GetGroupName ()
151                 {
152                         return GetLocalizedString ("GroupName", GroupName);
153                 }
154                 
155                 #endregion
156                 
157         }
158 #endif
159 }
160