2 // System.Web.UI.WebControls.TreeNode.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
34 using System.ComponentModel;
37 namespace System.Web.UI.WebControls
39 [ParseChildrenAttribute (true, "ChildNodes")]
40 public class TreeNode: IStateManager, ICloneable
42 StateBag ViewState = new StateBag ();
43 TreeNodeCollection nodes;
53 public TreeNode (string text)
58 public TreeNode (string text, string value)
64 public TreeNode (string text, string value, string imageUrl)
71 public TreeNode (string text, string value, string imageUrl, string navigateUrl, string target)
76 NavigateUrl = navigateUrl;
80 internal TreeView Tree {
85 value.SetSelectedNode (this);
86 else if (tree != null)
87 tree.SetSelectedNode (null);
95 public virtual bool Checked {
97 object o = ViewState ["Checked"];
98 if (o != null) return (bool)o;
102 ViewState ["Checked"] = value;
106 [DefaultValue (null)]
107 [MergableProperty (false)]
109 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
110 public virtual TreeNodeCollection ChildNodes {
113 nodes = new TreeNodeCollection (this);
114 if (IsTrackingViewState)
115 ((IStateManager)nodes).TrackViewState();
121 public virtual bool Expanded {
123 object o = ViewState ["Expanded"];
124 if (o != null) return (bool)o;
128 ViewState ["Expanded"] = value;
132 public virtual string ImageToolTip {
134 object o = ViewState ["ImageToolTip"];
135 if (o != null) return (string)o;
139 ViewState ["ImageToolTip"] = value;
143 public virtual string ImageUrl {
145 object o = ViewState ["ImageUrl"];
146 if (o != null) return (string)o;
150 ViewState ["ImageUrl"] = value;
154 public virtual string NavigateUrl {
156 object o = ViewState ["NavigateUrl"];
157 if (o != null) return (string)o;
161 ViewState ["NavigateUrl"] = value;
165 public bool PopulateOnDemand {
167 object o = ViewState ["PopulateOnDemand"];
168 if (o != null) return (bool)o;
172 ViewState ["PopulateOnDemand"] = value;
176 public TreeNodeSelectAction SelectAction {
178 object o = ViewState ["SelectAction"];
179 if (o != null) return (TreeNodeSelectAction)o;
180 return TreeNodeSelectAction.Select;
183 ViewState ["SelectAction"] = value;
187 public bool ShowCheckBox {
189 object o = ViewState ["ShowCheckBox"];
190 if (o != null) return (bool)o;
194 ViewState ["ShowCheckBox"] = value;
198 public virtual string Target {
200 object o = ViewState ["Target"];
201 if(o != null) return (string)o;
205 ViewState ["Target"] = value;
211 [WebSysDescription ("The display text of the tree node.")]
212 public virtual string Text {
214 object o = ViewState ["Text"];
215 if(o != null) return (string)o;
219 ViewState ["Text"] = value;
223 public virtual string ToolTip {
225 object o = ViewState ["ToolTip"];
226 if(o != null) return (string)o;
230 ViewState ["ToolTip"] = value;
234 public virtual string Value {
236 object o = ViewState ["Value"];
237 if(o != null) return (string)o;
241 ViewState ["Value"] = value;
245 public virtual bool Selected {
251 if (!value && tree.SelectedNode == this)
252 tree.SetSelectedNode (null);
254 tree.SetSelectedNode (this);
257 SelectedFlag = value;
261 internal virtual bool SelectedFlag {
263 object o = ViewState ["Selected"];
264 if(o != null) return (bool)o;
268 ViewState ["Selected"] = value;
273 get { return index; }
274 set { index = value; }
278 public TreeNode Parent {
279 get { return parent; }
282 internal void SetParent (TreeNode node) {
287 public void Collapse ()
292 public void CollapseAll ()
294 SetExpandedRec (false, -1);
297 public void Expand ()
302 public void Expand (int depth)
304 SetExpandedRec (true, depth);
307 public void ExpandAll ()
309 SetExpandedRec (true, -1);
312 void SetExpandedRec (bool expanded, int depth)
315 if (depth == 0) return;
318 foreach (TreeNode nod in nodes)
319 nod.SetExpandedRec (expanded, depth - 1);
323 public void Select ()
328 public void ToggleExpandState ()
330 Expanded = !Expanded;
333 public void LoadViewState (object savedState)
335 if (savedState == null)
338 object[] states = (object[]) savedState;
339 ViewState.LoadViewState (states [0]);
341 if (tree != null && SelectedFlag)
342 tree.SetSelectedNode (this);
344 ((IStateManager)ChildNodes).LoadViewState (states [1]);
347 public object SaveViewState ()
349 object[] states = new object[2];
350 states[0] = ViewState.SaveViewState();
351 states[1] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
353 for (int i = 0; i < states.Length; i++) {
354 if (states [i] != null)
360 public void TrackViewState ()
363 ViewState.TrackViewState();
366 ((IStateManager)nodes).TrackViewState ();
369 public bool IsTrackingViewState
371 get { return marked; }
374 public object Clone ()
376 object o = SaveViewState ();
377 TreeNode nod = new TreeNode ();
378 nod.LoadViewState (o);