19ff0074c6da47eae71e69db73c2396d860ffe34
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Toolbox / ToolboxCategory.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Toolbox
6 {
7     using System.Collections;
8     using System.Collections.Generic;
9     using System.Collections.ObjectModel;
10     using System.Collections.Specialized;
11     using System.ComponentModel;
12     using System.Diagnostics.CodeAnalysis;
13     using System.Runtime;
14
15     [SuppressMessage(FxCop.Category.Design, "CA1039:ListsAreStronglyTyped",
16         Justification = "The nongeneric IList implementation is required for XAML support. It is implmented explicitly.")]
17     [SuppressMessage(FxCop.Category.Naming, "CA1710:IdentifiersShouldHaveCorrectSuffix",
18         Justification = "The collection implemenation is required for XAML support.")]
19     public sealed class ToolboxCategory : INotifyPropertyChanged, IList
20     {
21         string categoryName;
22         ObservableCollection<ToolboxItemWrapper> tools = new ObservableCollection<ToolboxItemWrapper>();
23
24         public ToolboxCategory()
25             : this(string.Empty)
26         {
27         }
28
29         public ToolboxCategory(string name)
30         {
31             this.categoryName = name;
32             this.tools.CollectionChanged += this.OnToolCollectionChanged;
33         }
34
35         public string CategoryName
36         {
37             get { return this.categoryName; }
38             set
39             {
40                 this.categoryName = value;
41                 this.OnPropertyChanged("CategoryName");
42             }
43         }
44
45         public ToolboxItemWrapper this[int index]
46         {
47             get { return this.tools[index]; }
48         }
49
50         [Fx.Tag.KnownXamlExternal]
51         public ICollection<ToolboxItemWrapper> Tools
52         {
53             get { return this.tools; }
54         }
55
56         public void Add(ToolboxItemWrapper tool)
57         {
58             if (null == tool)
59             {
60                 throw FxTrace.Exception.ArgumentNull("tool");
61             }
62             this.tools.Add(tool);
63         }
64
65         public bool Remove(ToolboxItemWrapper tool)
66         {
67             if (null == tool)
68             {
69                 throw FxTrace.Exception.ArgumentNull("tool");
70             }
71             return this.tools.Remove(tool);
72         }
73
74         internal void HandleToolCollectionNotification(NotifyCollectionChangedEventHandler listener, bool register)
75         {
76             if (null == listener)
77             {
78                 throw FxTrace.Exception.ArgumentNull("listener");
79             }
80             if (register)
81             {
82                 this.tools.CollectionChanged += listener;
83             }
84             else
85             {
86                 this.tools.CollectionChanged -= listener;
87             }
88         }
89
90         void OnToolCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
91         {
92             switch (e.Action)
93             {
94                 case NotifyCollectionChangedAction.Add:
95                     foreach (var tool in e.NewItems)
96                     {
97                         if (null == tool)
98                         {
99                             throw FxTrace.Exception.ArgumentNull("tool");
100                         }
101                     }
102                     break;
103             }
104             this.OnPropertyChanged("Tools");
105         }
106
107         void OnPropertyChanged(string propertyName)
108         {
109             if (null != this.PropertyChanged)
110             {
111                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
112             }
113         }
114
115         public event PropertyChangedEventHandler PropertyChanged;
116
117         #region IList Members
118
119         int IList.Add(object value)
120         {
121             this.Add((ToolboxItemWrapper)value);
122             return this.tools.IndexOf((ToolboxItemWrapper)value);
123         }
124
125         void IList.Clear()
126         {
127             this.tools.Clear();
128         }
129
130         bool IList.Contains(object value)
131         {
132             if (null == value)
133             {
134                 throw FxTrace.Exception.ArgumentNull("value");
135             }
136             return this.tools.Contains((ToolboxItemWrapper)value);
137         }
138
139         int IList.IndexOf(object value)
140         {
141             if (null == value)
142             {
143                 throw FxTrace.Exception.ArgumentNull("value");
144             }
145             return this.tools.IndexOf((ToolboxItemWrapper)value);
146         }
147
148         void IList.Insert(int index, object value)
149         {
150             if (null == value)
151             {
152                 throw FxTrace.Exception.ArgumentNull("value");
153             }
154             this.tools.Insert(index, (ToolboxItemWrapper)value);
155         }
156
157         bool IList.IsFixedSize
158         {
159             get { return ((IList)this.tools).IsFixedSize; }
160         }
161
162         bool IList.IsReadOnly
163         {
164             get { return ((IList)this.tools).IsReadOnly; }
165         }
166
167         void IList.Remove(object value)
168         {
169             this.Remove((ToolboxItemWrapper)value);
170         }
171
172         void IList.RemoveAt(int index)
173         {
174             this.tools.RemoveAt(index);
175         }
176
177         object IList.this[int index]
178         {
179             get
180             {
181                 return this.tools[index];
182             }
183             set
184             {
185                 if (null == value)
186                 {
187                     throw FxTrace.Exception.ArgumentNull("value");
188                 }
189                 this.tools[index] = (ToolboxItemWrapper)value;
190             }
191         }
192
193         #endregion
194
195         #region ICollection Members
196
197         void ICollection.CopyTo(Array array, int index)
198         {
199             if (null == array)
200             {
201                 throw FxTrace.Exception.ArgumentNull("array");
202             }
203             ((ICollection)this.tools).CopyTo(array, index);
204         }
205
206         int ICollection.Count
207         {
208             get { return this.tools.Count; }
209         }
210
211         bool ICollection.IsSynchronized
212         {
213             get { return ((ICollection)this.tools).IsSynchronized; }
214         }
215
216         object ICollection.SyncRoot
217         {
218             get { return ((ICollection)this.tools).SyncRoot; }
219         }
220
221         #endregion
222
223         #region IEnumerable Members
224
225         IEnumerator IEnumerable.GetEnumerator()
226         {
227             return this.tools.GetEnumerator();
228         }
229
230         #endregion
231     }
232 }