2003-12-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / AttributeCollection.cs
1 //
2 // System.Web.UI.AttributeCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com
9 //
10
11 using System;
12 using System.Collections;
13
14 namespace System.Web.UI {
15
16         public sealed class AttributeCollection
17         {
18                 private StateBag bag;
19                 private CssStyleCollection styleCollection;
20                 
21                 public AttributeCollection (StateBag bag)
22                 {
23                         this.bag = bag;
24                 }
25
26                 public int Count {
27                         get { return bag.Count; }
28                 }
29
30                 public CssStyleCollection CssStyle {
31                         get {
32                                 if (styleCollection == null)
33                                         styleCollection = new CssStyleCollection (bag);
34                                 return styleCollection;
35                         }
36                 }
37
38                 public string this [string key] {
39                         get { return bag [key] as string; }
40
41                         set { bag.Add (key, value); }
42                 }
43
44                 public ICollection Keys {
45                         get { return bag.Keys; }
46                 }
47
48                 public void Add (string key, string value)
49                 {
50                         if (styleCollection != null && 0 == String.Compare (key, "style", true))
51                                 styleCollection.FillStyle (value);
52                         else
53                                 bag.Add (key, value);
54                 }
55
56                 public void AddAttributes (HtmlTextWriter writer)
57                 {
58                         foreach (string key in bag.Keys) {
59                                 string value = bag [key] as string;
60                                 writer.AddAttribute (key, value);
61                         }
62                 }
63
64                 public void Clear ()
65                 {
66                         bag.Clear ();
67                 }
68
69                 public void Remove (string key)
70                 {
71                         bag.Remove (key);
72                 }
73
74                 public void Render (HtmlTextWriter writer)
75                 {
76                         foreach (string key in bag.Keys) {
77                                 string value = bag [key] as string;
78                                 if (value != null)
79                                         writer.WriteAttribute (key, value);
80                         }
81                 }
82         }
83 }