2004-11-23 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TreeNode.cs
1 //
2 // System.Web.UI.WebControls.TreeNode.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.ComponentModel;
35 using System.Web.UI;
36
37 namespace System.Web.UI.WebControls
38 {
39         [ParseChildrenAttribute (true, "ChildNodes")]
40         public class TreeNode: IStateManager, ICloneable
41         {
42                 StateBag ViewState = new StateBag ();
43                 TreeNodeCollection nodes;
44                 bool marked;
45                 TreeView tree;
46                 TreeNode parent;
47                 int index;
48                 
49                 public TreeNode ()
50                 {
51                 }
52                 
53                 public TreeNode (string text)
54                 {
55                         Text = text;
56                 }
57                 
58                 public TreeNode (string text, string value)
59                 {
60                         Text = text;
61                         Value = value;
62                 }
63                 
64                 public TreeNode (string text, string value, string imageUrl)
65                 {
66                         Text = text;
67                         Value = value;
68                         ImageUrl = imageUrl;
69                 }
70                 
71                 public TreeNode (string text, string value, string imageUrl, string navigateUrl, string target)
72                 {
73                         Text = text;
74                         Value = value;
75                         ImageUrl = imageUrl;
76                         NavigateUrl = navigateUrl;
77                         Target = target;
78                 }
79                 
80                 internal TreeView Tree {
81                         get { return tree; }
82                         set {
83                                 if (SelectedFlag) {
84                                         if (value != null)
85                                                 value.SetSelectedNode (this);
86                                         else if (tree != null)
87                                                 tree.SetSelectedNode (null);
88                                 }
89                                 tree = value;
90                                 if (nodes != null)
91                                         nodes.SetTree (tree);
92                         }
93                 }
94                 
95                 public virtual bool Checked {
96                         get {
97                                 object o = ViewState ["Checked"];
98                                 if (o != null) return (bool)o;
99                                 return false;
100                         }
101                         set {
102                                 ViewState ["Checked"] = value;
103                         }
104                 }
105
106                 [DefaultValue (null)]
107                 [MergableProperty (false)]
108                 [Browsable (false)]
109                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
110                 public virtual TreeNodeCollection ChildNodes {
111                         get {
112                                 if (nodes == null) {
113                                         nodes = new TreeNodeCollection (this);
114                                         if (IsTrackingViewState)
115                                                 ((IStateManager)nodes).TrackViewState();
116                                 }
117                                 return nodes;
118                         }
119                 }
120
121                 public virtual bool Expanded {
122                         get {
123                                 object o = ViewState ["Expanded"];
124                                 if (o != null) return (bool)o;
125                                 return false;
126                         }
127                         set {
128                                 ViewState ["Expanded"] = value;
129                         }
130                 }
131
132                 public virtual string ImageToolTip {
133                         get {
134                                 object o = ViewState ["ImageToolTip"];
135                                 if (o != null) return (string)o;
136                                 return "";
137                         }
138                         set {
139                                 ViewState ["ImageToolTip"] = value;
140                         }
141                 }
142
143                 public virtual string ImageUrl {
144                         get {
145                                 object o = ViewState ["ImageUrl"];
146                                 if (o != null) return (string)o;
147                                 return "";
148                         }
149                         set {
150                                 ViewState ["ImageUrl"] = value;
151                         }
152                 }
153
154                 public virtual string NavigateUrl {
155                         get {
156                                 object o = ViewState ["NavigateUrl"];
157                                 if (o != null) return (string)o;
158                                 return "";
159                         }
160                         set {
161                                 ViewState ["NavigateUrl"] = value;
162                         }
163                 }
164
165                 public bool PopulateOnDemand {
166                         get {
167                                 object o = ViewState ["PopulateOnDemand"];
168                                 if (o != null) return (bool)o;
169                                 return false;
170                         }
171                         set {
172                                 ViewState ["PopulateOnDemand"] = value;
173                         }
174                 }
175
176                 public TreeNodeSelectAction SelectAction {
177                         get {
178                                 object o = ViewState ["SelectAction"];
179                                 if (o != null) return (TreeNodeSelectAction)o;
180                                 return TreeNodeSelectAction.Select;
181                         }
182                         set {
183                                 ViewState ["SelectAction"] = value;
184                         }
185                 }
186
187                 public bool ShowCheckBox {
188                         get {
189                                 object o = ViewState ["ShowCheckBox"];
190                                 if (o != null) return (bool)o;
191                                 return false;
192                         }
193                         set {
194                                 ViewState ["ShowCheckBox"] = value;
195                         }
196                 }
197
198                 public virtual string Target {
199                         get {
200                                 object o = ViewState ["Target"];
201                                 if(o != null) return (string)o;
202                                 return "";
203                         }
204                         set {
205                                 ViewState ["Target"] = value;
206                         }
207                 }
208
209                 [Localizable (true)]
210                 [DefaultValue ("")]
211                 [WebSysDescription ("The display text of the tree node.")]
212                 public virtual string Text {
213                         get {
214                                 object o = ViewState ["Text"];
215                                 if(o != null) return (string)o;
216                                 return "";
217                         }
218                         set {
219                                 ViewState ["Text"] = value;
220                         }
221                 }
222
223                 public virtual string ToolTip {
224                         get {
225                                 object o = ViewState ["ToolTip"];
226                                 if(o != null) return (string)o;
227                                 return "";
228                         }
229                         set {
230                                 ViewState ["ToolTip"] = value;
231                         }
232                 }
233
234                 public virtual string Value {
235                         get {
236                                 object o = ViewState ["Value"];
237                                 if(o != null) return (string)o;
238                                 return "";
239                         }
240                         set {
241                                 ViewState ["Value"] = value;
242                         }
243                 }
244                 
245                 public virtual bool Selected {
246                         get {
247                                 return SelectedFlag;
248                         }
249                         set {
250                                 if (tree != null) {
251                                         if (!value && tree.SelectedNode == this)
252                                                 tree.SetSelectedNode (null);
253                                         else if (value)
254                                                 tree.SetSelectedNode (this);
255                                 }
256                                 else
257                                         SelectedFlag = value;
258                         }
259                 }
260                 
261                 internal virtual bool SelectedFlag {
262                         get {
263                                 object o = ViewState ["Selected"];
264                                 if(o != null) return (bool)o;
265                                 return false;
266                         }
267                         set {
268                                 ViewState ["Selected"] = value;
269                         }
270                 }
271                 
272                 internal int Index {
273                         get { return index; }
274                         set { index = value; }
275                 }
276                 
277                 
278                 public TreeNode Parent {
279                         get { return parent; }
280                 }
281                 
282                 internal void SetParent (TreeNode node) {
283                         parent = node;
284                 }
285                 
286                 
287                 public void Collapse ()
288                 {
289                         Expanded = false;
290                 }
291
292                 public void CollapseAll ()
293                 {
294                         SetExpandedRec (false, -1);
295                 }
296
297                 public void Expand ()
298                 {
299                         Expanded = true;
300                 }
301
302                 public void Expand (int depth)
303                 {
304                         SetExpandedRec (true, depth);
305                 }
306
307                 public void ExpandAll ()
308                 {
309                         SetExpandedRec (true, -1);
310                 }
311                 
312                 void SetExpandedRec (bool expanded, int depth)
313                 {
314                         Expanded = expanded;
315                         if (depth == 0) return;
316                         
317                         if (nodes != null) {
318                                 foreach (TreeNode nod in nodes)
319                                         nod.SetExpandedRec (expanded, depth - 1);
320                         }
321                 }
322                 
323                 public void Select ()
324                 {
325                         Selected = true;
326                 }
327                 
328                 public void ToggleExpandState ()
329                 {
330                         Expanded = !Expanded;
331                 }
332
333                 public void LoadViewState (object savedState)
334                 {
335                         if (savedState == null)
336                                 return;
337
338                         object[] states = (object[]) savedState;
339                         ViewState.LoadViewState (states [0]);
340                         
341                         if (tree != null && SelectedFlag)
342                                 tree.SetSelectedNode (this);
343                                 
344                         ((IStateManager)ChildNodes).LoadViewState (states [1]);
345                 }
346                 
347                 public object SaveViewState ()
348                 {
349                         object[] states = new object[2];
350                         states[0] = ViewState.SaveViewState();
351                         states[1] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
352                         
353                         for (int i = 0; i < states.Length; i++) {
354                                 if (states [i] != null)
355                                         return states;
356                         }
357                         return null;
358                 }
359                 
360                 public void TrackViewState ()
361                 {
362                         marked = true;
363                         ViewState.TrackViewState();
364
365                         if (nodes != null)
366                                 ((IStateManager)nodes).TrackViewState ();
367                 }
368                 
369                 public bool IsTrackingViewState
370                 {
371                         get { return marked; }
372                 }
373                 
374                 public object Clone ()
375                 {
376                         object o = SaveViewState ();
377                         TreeNode nod = new TreeNode ();
378                         nod.LoadViewState (o);
379                         return nod;
380                 }
381         }
382 }
383
384 #endif