545fa2f9f1b3aa4e98e15d6acb105364d8336183
[mono.git] / mcs / class / System.Web / System.Web / SiteMapNode.cs
1 //
2 // System.Web.SiteMapNode
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Web.UI;
36 using System.Web.UI.WebControls;
37 using System.ComponentModel;
38
39 namespace System.Web {
40         public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
41         
42                 private SiteMapNode () {}
43                 
44                 public SiteMapNode (SiteMapProvider provider, string key) : this (provider, key, null, null, null, null, null, null) {}
45                 public SiteMapNode (SiteMapProvider provider, string key, string url) : this (provider, key, url, null, null, null, null, null) {}
46                 public SiteMapNode (SiteMapProvider provider, string key, string url, string title) : this (provider, key, url, title, null, null, null, null) {}
47                 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description) : this (provider, key, url, title, description, null, null, null) {}
48                 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description, IList roles, NameValueCollection attributes, NameValueCollection resourceKeys)
49                 {
50                         if (provider == null)
51                                 throw new ArgumentNullException ("provider");
52                         
53                         this.provider = provider;
54                         this.key = key;
55                         this.url = url;
56                         this.title = title;
57                         this.description = description;
58                         this.roles = roles;
59                         this.attributes = attributes;
60                         this.resourceKeys = resourceKeys;
61                 }
62
63                 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
64                 {
65                         return new SiteMapDataSourceView (owner, viewName, this);
66                 }
67                 
68                 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
69                 {
70                         return new SiteMapHierarchicalDataSourceView (this);
71                 }
72                 
73                 [MonoTODO]
74                 public bool IsAccessibleToUser (System.Web.HttpContext ctx)
75                 {
76                         throw new NotImplementedException ();
77                 }
78                 
79                 public override string ToString()
80                 {
81                         return Title;
82                 }
83
84                 public virtual bool HasChildNodes { get { return ChildNodes != null && ChildNodes.Count != 0; } }
85
86                 public SiteMapNodeCollection GetAllNodes ()
87                 {
88                         SiteMapNodeCollection ret;
89                 
90                         ret = new SiteMapNodeCollection ();
91                         GetAllNodesRecursive (ret);
92                         return SiteMapNodeCollection.ReadOnly (ret);
93                 }
94                 
95                 void GetAllNodesRecursive(SiteMapNodeCollection c)
96                 {
97                         SiteMapNodeCollection childNodes = this.ChildNodes;
98                         
99                         if (childNodes.Count > 0) {
100                                 childNodes.AddRange (childNodes);
101                                 foreach (SiteMapNode n in childNodes)
102                                         n.GetAllNodesRecursive (c);
103                         }
104                 }
105
106                 
107                 public virtual bool IsDescendantOf (SiteMapNode node)
108                 {
109                         for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
110                                 if (n == node) return true; 
111
112                         return false; 
113                 }
114                 
115                 public virtual SiteMapNode NextSibling {
116                         get {
117                                 IList siblings = this.SiblingNodes;
118                                 if (siblings == null)
119                                         return null; 
120                                 
121                                 int pos = siblings.IndexOf (this);
122                                 if (pos >= 0 && pos < siblings.Count - 1)
123                                         return (SiteMapNode) siblings [pos + 1]; 
124                                 
125                                 return null; 
126                         }
127                 }
128                 
129                 public virtual SiteMapNode PreviousSibling {
130                         get {
131                                 IList siblings = this.SiblingNodes;
132                                 if (siblings == null)
133                                         return null; 
134                                 
135                                 int pos = siblings.IndexOf (this);
136                                 if (pos > 0 && pos < siblings.Count)
137                                         return (SiteMapNode) siblings [pos - 1]; 
138                                 
139                                 return null; 
140                         }
141                 }
142                 
143                 public virtual SiteMapNode ParentNode {
144                         get {
145                                 if (parent != null) return parent;
146                                 
147                                 SiteMapProvider provider = this.provider;
148                                 
149                                 do {
150                                         parent = provider.GetParentNode (this);
151                                         if (parent != null)
152                                                 return parent; 
153                                         
154                                         provider = provider.ParentProvider;
155                                 } while (provider != null);
156                                 return null;
157                         }
158                         set {
159                                 CheckWritable ();
160                                 parent = value;
161                         }
162                 }
163                 
164                 [MonoTODO ("set")]
165                 public virtual SiteMapNodeCollection ChildNodes {
166                         get { return provider.GetChildNodes (this); } 
167                         set { CheckWritable (); }
168                 }
169
170                 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; }  }
171                 
172                 SiteMapNodeCollection SiblingNodes {
173                         get {
174                                 if (ParentNode != null)
175                                         return ParentNode.ChildNodes;
176                                 
177                                 return null;
178                         }
179                 }
180                 
181                 [MonoTODO]
182                 protected string GetExplicitResourceString (string attributeName, bool b)
183                 {
184                         return null;
185                 }
186                 
187                 [MonoTODO]
188                 protected string GetImplicitResourceString (string attributeName)
189                 {
190                         return null;
191                 }
192                 
193                 [MonoTODO ("resource string?")]
194                 public string this [string key]
195                 {
196                         get {
197                                 string val = null;
198                                 if (provider.EnableLocalization) {
199                                         val = GetExplicitResourceString (key, true);
200                                         if (val == null) val = GetImplicitResourceString (key);
201                                 }
202                                 if (val != null) return null;
203                                 if (attributes != null) return attributes [key];
204                                 return null;
205                         }
206                 }
207                 
208                 object ICloneable.Clone ()
209                 {
210                         return Clone (false);
211                 }
212                 
213                 public virtual SiteMapNode Clone ()
214                 {
215                         return Clone (false);
216                 }
217                 
218                 public virtual SiteMapNode Clone (bool cloneParentNodes)
219                 {
220                         SiteMapNode node = new SiteMapNode ();
221                         node.provider = provider;
222                         node.key = key;
223                         node.url = url;
224                         node.title = title;
225                         node.description = description;
226                         node.roles = new ArrayList (roles);
227                         node.attributes = new NameValueCollection (attributes);
228                         if (cloneParentNodes && ParentNode != null)
229                                 node.parent = (SiteMapNode) ParentNode.Clone (true);
230                         return node;
231                 }
232                 
233                 public override bool Equals (object ob)
234                 {
235                         SiteMapNode node = ob as SiteMapNode;
236                         if (node == null) return false;
237                         
238                         if (node.key != key ||
239                                         node.url != url ||
240                                         node.title != title ||
241                                         node.description != description) {
242                                 return false;
243                         }
244                         
245                         if ((roles == null || node.roles == null) && (roles != node.roles)) return false;
246                         if (roles.Count != node.roles.Count) return false;
247
248                         foreach (object role in roles)
249                                 if (!node.roles.Contains (role)) return false;
250                                 
251                         if ((attributes == null || node.attributes == null) && (attributes != node.attributes)) return false;
252                         if (attributes.Count != node.attributes.Count) return false;
253
254                         foreach (string k in attributes)
255                                 if (attributes [k] != node.attributes [k]) return false;
256
257                         return true;
258                 }
259                 
260                 public override int GetHashCode ()
261                 {
262                         return (key + url + title + description).GetHashCode ();
263                 }
264                 
265                 void CheckWritable ()
266                 {
267                         if (readOnly)
268                                 throw new InvalidOperationException ("Can't modify read-only node");
269                 }
270                                 
271                 #region Field Accessors
272                 
273                 public virtual NameValueCollection Attributes {
274                         get { return attributes; } 
275                         set { CheckWritable (); attributes = value; }
276                 }
277                 
278                 public virtual string Description {
279                         get { return description != null ? description : ""; }
280                         set { CheckWritable (); description = value; }
281                 }
282                 
283                 [LocalizableAttribute (true)]
284                 public virtual string Title {
285                         get { return title != null ? title : ""; }
286                         set { CheckWritable (); title = value; }
287                 }
288                 
289                 public virtual string Url {
290                         get { return url != null ? url : ""; }
291                         set { CheckWritable (); url = value; }
292                 }
293                 
294                 public virtual IList Roles {
295                         get { return roles; }
296                         set { CheckWritable (); roles = value; }
297                 }
298                 
299                 public bool ReadOnly {
300                         get { return readOnly; }
301                         set { readOnly = value; }
302                 }
303                 
304                 [MonoTODO ("Do somethig with this")]
305                 public string ResourceKey {
306                         get { return resourceKey; }
307                         set { resourceKey = value; }
308                 }
309                 
310                 public string Key { get { return key; } }
311                 public SiteMapProvider Provider { get { return provider; } }
312                 
313                 #endregion
314                 
315                 #region INavigateUIData
316                 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
317                 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetParent ()
318                 {
319                         if (ParentNode == null) return null; 
320                         return SiteMapNodeCollection.ReadOnly (new SiteMapNodeCollection (ParentNode));
321                 }
322
323                 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
324                 object System.Web.UI.IHierarchyData.Item { get { return this; } }
325                 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
326                 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
327                 #endregion
328                 
329                 #region INavigateUIData
330                 string INavigateUIData.Name { get { return Title; }  }
331                 string INavigateUIData.NavigateUrl { get { return Url; } }
332                 string INavigateUIData.Value { get { return Title; } }
333                 #endregion
334
335                 #region Fields
336                 SiteMapProvider provider;
337                 string key;
338                 string url;
339                 string title;
340                 string description;
341                 IList roles;
342                 NameValueCollection attributes;
343                 NameValueCollection resourceKeys;
344                 bool readOnly;
345                 string resourceKey;
346                 SiteMapNode parent;
347                 #endregion
348                 
349         }
350 }
351 #endif
352