Initial commit
[mono.git] / mcs / class / referencesource / System / compmod / system / componentmodel / CategoryAttribute.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CategoryAttribute.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  */
9 namespace System.ComponentModel {
10     using System;
11     using System.ComponentModel;
12     using System.Diagnostics;
13     using System.Security.Permissions;
14
15     /// <devdoc>
16     ///    <para>Specifies the category in which the property or event will be displayed in a
17     ///       visual designer.</para>
18     /// </devdoc>
19     [AttributeUsage(AttributeTargets.All)]
20     public class CategoryAttribute : Attribute {
21         private static volatile CategoryAttribute appearance;
22         private static volatile CategoryAttribute asynchronous;
23         private static volatile CategoryAttribute behavior;
24         private static volatile CategoryAttribute data;
25         private static volatile CategoryAttribute design;
26         private static volatile CategoryAttribute action;
27         private static volatile CategoryAttribute format;
28         private static volatile CategoryAttribute layout;
29         private static volatile CategoryAttribute mouse;
30         private static volatile CategoryAttribute key;
31         private static volatile CategoryAttribute focus;
32         private static volatile CategoryAttribute windowStyle;
33         private static volatile CategoryAttribute dragDrop;
34         private static volatile CategoryAttribute defAttr;
35         
36         private bool localized;
37
38         /// <devdoc>
39         ///    <para>
40         ///       Provides the actual category name.
41         ///    </para>
42         /// </devdoc>
43         private string categoryValue;
44
45         /// <devdoc>
46         ///    <para>Gets the action category attribute.</para>
47         /// </devdoc>
48         public static CategoryAttribute Action {
49             get {
50                 if (action == null) {
51                     action = new CategoryAttribute("Action");
52                 }
53                 return action;
54             }
55         }
56
57         /// <devdoc>
58         ///    <para>Gets the appearance category attribute.</para>
59         /// </devdoc>
60         public static CategoryAttribute Appearance {
61             get {
62                 if (appearance == null) {
63                     appearance = new CategoryAttribute("Appearance");
64                 }
65                 return appearance;
66             }
67         }
68
69         /// <devdoc>
70         ///    <para>Gets the asynchronous category attribute.</para>
71         /// </devdoc>
72         public static CategoryAttribute Asynchronous {
73             get {
74                 if (asynchronous == null) {
75                     asynchronous = new CategoryAttribute("Asynchronous");
76                 }
77                 return asynchronous;
78             }
79         }
80
81         /// <devdoc>
82         ///    <para>Gets the behavior category attribute.</para>
83         /// </devdoc>
84         public static CategoryAttribute Behavior {
85             get {
86                 if (behavior == null) {
87                     behavior = new CategoryAttribute("Behavior");
88                 }
89                 return behavior;
90             }
91         }
92
93         /// <devdoc>
94         ///    <para>Gets the data category attribute.</para>
95         /// </devdoc>
96         public static CategoryAttribute Data {
97             get {
98                 if (data == null) {
99                     data = new CategoryAttribute("Data");
100                 }
101                 return data;
102             }
103         }
104
105         /// <devdoc>
106         ///    <para>Gets the default category attribute.</para>
107         /// </devdoc>
108         public static CategoryAttribute Default {
109             get {
110                 if (defAttr == null) {
111                     defAttr = new CategoryAttribute();
112                 }
113                 return defAttr;
114             }
115         }
116         
117         /// <devdoc>
118         ///    <para>Gets the design category attribute.</para>
119         /// </devdoc>
120         public static CategoryAttribute Design {
121             get {
122                 if (design == null) {
123                     design = new CategoryAttribute("Design");
124                 }
125                 return design;
126             }
127         }
128
129         /// <devdoc>
130         ///    <para>Gets the drag and drop category attribute.</para>
131         /// </devdoc>
132         public static CategoryAttribute DragDrop {
133             get {
134                 if (dragDrop == null) {
135                     dragDrop = new CategoryAttribute("DragDrop");
136                 }
137                 return dragDrop;
138             }
139         }
140
141         /// <devdoc>
142         ///    <para>Gets the focus category attribute.</para>
143         /// </devdoc>
144         public static CategoryAttribute Focus {
145             get {
146                 if (focus == null) {
147                     focus = new CategoryAttribute("Focus");
148                 }
149                 return focus;
150             }
151         }
152
153         /// <devdoc>
154         ///    <para>Gets the format category attribute.</para>
155         /// </devdoc>
156         public static CategoryAttribute Format {
157             get {
158                 if (format == null) {
159                     format = new CategoryAttribute("Format");
160                 }
161                 return format;
162             }
163         }
164
165         /// <devdoc>
166         ///    <para>Gets the keyboard category attribute.</para>
167         /// </devdoc>
168         public static CategoryAttribute Key {
169             get {
170                 if (key == null) {
171                     key = new CategoryAttribute("Key");
172                 }
173                 return key;
174             }
175         }
176
177         /// <devdoc>
178         ///    <para>Gets the layout category attribute.</para>
179         /// </devdoc>
180         public static CategoryAttribute Layout {
181             get {
182                 if (layout == null) {
183                     layout = new CategoryAttribute("Layout");
184                 }
185                 return layout;
186             }
187         }
188
189         /// <devdoc>
190         ///    <para>Gets the mouse category attribute.</para>
191         /// </devdoc>
192         public static CategoryAttribute Mouse {
193             get {
194                 if (mouse == null) {
195                     mouse = new CategoryAttribute("Mouse");
196                 }
197                 return mouse;
198             }
199         }
200
201         /// <devdoc>
202         ///    <para> Gets the window style category 
203         ///       attribute.</para>
204         /// </devdoc>
205         public static CategoryAttribute WindowStyle {
206             get {
207                 if (windowStyle == null) {
208                     windowStyle = new CategoryAttribute("WindowStyle");
209                 }
210                 return windowStyle;
211             }
212         }
213
214         /// <devdoc>
215         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.CategoryAttribute'/> 
216         /// class with the default category.</para>
217         /// </devdoc>
218         public CategoryAttribute() : this("Default") {
219         }
220
221         /// <devdoc>
222         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.CategoryAttribute'/> class with
223         ///    the specified category name.</para>
224         /// </devdoc>
225         public CategoryAttribute(string category) {
226             this.categoryValue = category;
227             this.localized = false;
228         }
229
230         /// <devdoc>
231         ///    <para>Gets the name of the category for the property or event 
232         ///       that this attribute is bound to.</para>
233         /// </devdoc>
234         public string Category {
235             get {
236                 if (!localized) {
237                     localized = true;
238                     string localizedValue = GetLocalizedString(categoryValue);
239                     if (localizedValue != null) {
240                         categoryValue = localizedValue;
241                     }
242                 }
243                 return categoryValue;
244             }
245         }
246
247         /// <devdoc>
248         /// </devdoc>
249         /// <devdoc>
250         /// </devdoc>
251         /// <internalonly/>
252         /// <internalonly/>
253         public override bool Equals(object obj){
254             if (obj == this) {
255                 return true;
256             }
257
258             if (obj is CategoryAttribute){
259                 return Category.Equals(((CategoryAttribute)obj).Category);
260             }
261             return false;
262         }
263
264         /// <devdoc>
265         ///    <para>[To be supplied.]</para>
266         /// </devdoc>
267         public override int GetHashCode() {
268             return Category.GetHashCode();
269         }
270
271         /// <devdoc>
272         ///    <para>Looks up the localized name of a given category.</para>
273         /// </devdoc>
274         protected virtual string GetLocalizedString(string value) {
275 #if !SILVERLIGHT
276             return (string)SR.GetObject("PropertyCategory" + value);
277 #else
278             bool usedFallback;
279             string localizedString = SR.GetString("PropertyCategory" + value, out usedFallback);
280             if (usedFallback) {
281                 return null;
282             }
283             return localizedString;
284 #endif
285         }
286         
287 #if !SILVERLIGHT
288         /// <devdoc>
289         /// </devdoc>
290         /// <devdoc>
291         /// </devdoc>
292         /// <internalonly/>
293         /// <internalonly/>
294         public override bool IsDefaultAttribute() {
295             return Category.Equals(Default.Category);
296         }
297 #endif
298     }
299 }
300