SiteMapPath.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. //
  2. // System.Web.UI.WebControls.SiteMapPath.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 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) 2005 Novell, Inc (http://www.novell.com)
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. namespace System.Web.UI.WebControls
  35. {
  36. [Designer ("System.Web.UI.Design.WebControls.SiteMapPathDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  37. public class SiteMapPath: CompositeControl
  38. {
  39. SiteMapProvider provider;
  40. Style currentNodeStyle;
  41. Style nodeStyle;
  42. Style pathSeparatorStyle;
  43. Style rootNodeStyle;
  44. ITemplate currentNodeTemplate;
  45. ITemplate nodeTemplate;
  46. ITemplate pathSeparatorTemplate;
  47. ITemplate rootNodeTemplate;
  48. private static readonly object ItemCreatedEvent = new object();
  49. private static readonly object ItemDataBoundEvent = new object();
  50. public event SiteMapNodeItemEventHandler ItemCreated {
  51. add { Events.AddHandler (ItemCreatedEvent, value); }
  52. remove { Events.RemoveHandler (ItemCreatedEvent, value); }
  53. }
  54. public event SiteMapNodeItemEventHandler ItemDataBound {
  55. add { Events.AddHandler (ItemDataBoundEvent, value); }
  56. remove { Events.RemoveHandler (ItemDataBoundEvent, value); }
  57. }
  58. protected virtual void OnItemCreated (SiteMapNodeItemEventArgs e)
  59. {
  60. if (Events != null) {
  61. SiteMapNodeItemEventHandler eh = (SiteMapNodeItemEventHandler) Events [ItemCreatedEvent];
  62. if (eh != null) eh (this, e);
  63. }
  64. }
  65. protected virtual void OnItemDataBound (SiteMapNodeItemEventArgs e)
  66. {
  67. if (Events != null) {
  68. SiteMapNodeItemEventHandler eh = (SiteMapNodeItemEventHandler) Events [ItemDataBoundEvent];
  69. if (eh != null) eh (this, e);
  70. }
  71. }
  72. [DefaultValueAttribute (null)]
  73. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  74. [NotifyParentPropertyAttribute (true)]
  75. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  76. public Style CurrentNodeStyle {
  77. get {
  78. if (currentNodeStyle == null) {
  79. currentNodeStyle = new Style ();
  80. if (IsTrackingViewState)
  81. ((IStateManager)currentNodeStyle).TrackViewState ();
  82. }
  83. return currentNodeStyle;
  84. }
  85. }
  86. [DefaultValue (null)]
  87. [TemplateContainer (typeof(SiteMapNodeItem), BindingDirection.OneWay)]
  88. [PersistenceMode (PersistenceMode.InnerProperty)]
  89. [Browsable (false)]
  90. public virtual ITemplate CurrentNodeTemplate {
  91. get { return currentNodeTemplate; }
  92. set { currentNodeTemplate = value; UpdateControls (); }
  93. }
  94. [DefaultValueAttribute (null)]
  95. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  96. [NotifyParentPropertyAttribute (true)]
  97. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  98. public Style NodeStyle {
  99. get {
  100. if (nodeStyle == null) {
  101. nodeStyle = new Style ();
  102. if (IsTrackingViewState)
  103. ((IStateManager)nodeStyle).TrackViewState ();
  104. }
  105. return nodeStyle;
  106. }
  107. }
  108. [DefaultValue (null)]
  109. [TemplateContainer (typeof(SiteMapNodeItem), BindingDirection.OneWay)]
  110. [PersistenceMode (PersistenceMode.InnerProperty)]
  111. [Browsable (false)]
  112. public virtual ITemplate NodeTemplate {
  113. get { return nodeTemplate; }
  114. set { nodeTemplate = value; UpdateControls (); }
  115. }
  116. [DefaultValueAttribute (-1)]
  117. [ThemeableAttribute (false)]
  118. public virtual int ParentLevelsDisplayed {
  119. get {
  120. object ob = ViewState ["ParentLevelsDisplayed"];
  121. if (ob != null) return (int) ob;
  122. else return -1;
  123. }
  124. set {
  125. if (value < -1) throw new ArgumentOutOfRangeException ("value");
  126. ViewState ["ParentLevelsDisplayed"] = value;
  127. UpdateControls ();
  128. }
  129. }
  130. [DefaultValueAttribute (PathDirection.RootToCurrent)]
  131. public virtual PathDirection PathDirection {
  132. get {
  133. object ob = ViewState ["PathDirection"];
  134. if (ob != null) return (PathDirection) ob;
  135. else return PathDirection.RootToCurrent;
  136. }
  137. set {
  138. if (value != PathDirection.RootToCurrent && value != PathDirection.CurrentToRoot)
  139. throw new ArgumentOutOfRangeException ("value");
  140. ViewState ["PathDirection"] = value;
  141. UpdateControls ();
  142. }
  143. }
  144. [DefaultValueAttribute (" > ")]
  145. [LocalizableAttribute (true)]
  146. public virtual string PathSeparator {
  147. get {
  148. object ob = ViewState ["PathSeparator"];
  149. if (ob != null) return (string) ob;
  150. else return " > ";
  151. }
  152. set {
  153. ViewState ["PathSeparator"] = value;
  154. UpdateControls ();
  155. }
  156. }
  157. [DefaultValueAttribute (null)]
  158. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  159. [NotifyParentPropertyAttribute (true)]
  160. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  161. public Style PathSeparatorStyle {
  162. get {
  163. if (pathSeparatorStyle == null) {
  164. pathSeparatorStyle = new Style ();
  165. if (IsTrackingViewState)
  166. ((IStateManager)pathSeparatorStyle).TrackViewState ();
  167. }
  168. return pathSeparatorStyle;
  169. }
  170. }
  171. [DefaultValue (null)]
  172. [TemplateContainer (typeof(SiteMapNodeItem), BindingDirection.OneWay)]
  173. [PersistenceMode (PersistenceMode.InnerProperty)]
  174. [Browsable (false)]
  175. public virtual ITemplate PathSeparatorTemplate {
  176. get { return pathSeparatorTemplate; }
  177. set { pathSeparatorTemplate = value; UpdateControls (); }
  178. }
  179. [BrowsableAttribute (false)]
  180. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  181. public SiteMapProvider Provider {
  182. get {
  183. if (provider == null) {
  184. if (this.SiteMapProvider.Length == 0) {
  185. provider = SiteMap.Provider;
  186. if (provider == null)
  187. throw new HttpException ("There is no default provider configured for the site.");
  188. } else {
  189. provider = SiteMap.Providers [this.SiteMapProvider];
  190. if (provider == null)
  191. throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
  192. }
  193. }
  194. return provider;
  195. }
  196. set {
  197. provider = value;
  198. UpdateControls ();
  199. }
  200. }
  201. [DefaultValueAttribute (false)]
  202. public virtual bool RenderCurrentNodeAsLink {
  203. get {
  204. object o = ViewState ["RenderCurrentNodeAsLink"];
  205. if (o != null) return (bool) o;
  206. else return false;
  207. }
  208. set {
  209. ViewState ["RenderCurrentNodeAsLink"] = value;
  210. UpdateControls ();
  211. }
  212. }
  213. [DefaultValueAttribute (null)]
  214. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  215. [NotifyParentPropertyAttribute (true)]
  216. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  217. public Style RootNodeStyle {
  218. get {
  219. if (rootNodeStyle == null) {
  220. rootNodeStyle = new Style ();
  221. if (IsTrackingViewState)
  222. ((IStateManager)rootNodeStyle).TrackViewState ();
  223. }
  224. return rootNodeStyle;
  225. }
  226. }
  227. [DefaultValue (null)]
  228. [TemplateContainer (typeof(SiteMapNodeItem), BindingDirection.OneWay)]
  229. [PersistenceMode (PersistenceMode.InnerProperty)]
  230. [Browsable (false)]
  231. public virtual ITemplate RootNodeTemplate {
  232. get { return rootNodeTemplate; }
  233. set { rootNodeTemplate = value; UpdateControls (); }
  234. }
  235. [DefaultValueAttribute (true)]
  236. [ThemeableAttribute (false)]
  237. public virtual bool ShowToolTips {
  238. get {
  239. object o = ViewState ["ShowToolTips"];
  240. if (o != null) return (bool) o;
  241. else return true;
  242. }
  243. set {
  244. ViewState ["ShowToolTips"] = value;
  245. UpdateControls ();
  246. }
  247. }
  248. [DefaultValueAttribute ("")]
  249. [ThemeableAttribute (false)]
  250. public virtual string SiteMapProvider {
  251. get {
  252. object o = ViewState ["SiteMapProvider"];
  253. if (o != null) return (string) o;
  254. else return string.Empty;
  255. }
  256. set {
  257. ViewState ["SiteMapProvider"] = value;
  258. UpdateControls ();
  259. }
  260. }
  261. [Localizable (true)]
  262. [MonoTODO]
  263. public virtual string SkipLinkText
  264. {
  265. get {
  266. throw new NotImplementedException ();
  267. }
  268. set {
  269. throw new NotImplementedException ();
  270. }
  271. }
  272. void UpdateControls ()
  273. {
  274. ChildControlsCreated = false;
  275. }
  276. [MonoTODO]
  277. public override void DataBind ()
  278. {
  279. throw new NotImplementedException ();
  280. }
  281. protected override void AddAttributesToRender (HtmlTextWriter writer)
  282. {
  283. base.AddAttributesToRender (writer);
  284. }
  285. protected internal override void CreateChildControls ()
  286. {
  287. Controls.Clear ();
  288. CreateControlHierarchy ();
  289. }
  290. protected virtual void CreateControlHierarchy ()
  291. {
  292. ArrayList nodes = new ArrayList ();
  293. SiteMapNode node = Provider.CurrentNode;
  294. if (node == null) return;
  295. int levels = ParentLevelsDisplayed != -1 ? ParentLevelsDisplayed + 1 : int.MaxValue;
  296. while (node != null && levels > 0) {
  297. if (nodes.Count > 0) {
  298. SiteMapNodeItem sep = new SiteMapNodeItem (nodes.Count, SiteMapNodeItemType.PathSeparator);
  299. InitializeItem (sep);
  300. SiteMapNodeItemEventArgs sargs = new SiteMapNodeItemEventArgs (sep);
  301. OnItemCreated (sargs);
  302. nodes.Add (sep);
  303. }
  304. SiteMapNodeItemType nt;
  305. if (nodes.Count == 0)
  306. nt = SiteMapNodeItemType.Current;
  307. else if (node.ParentNode == null)
  308. nt = SiteMapNodeItemType.Root;
  309. else
  310. nt = SiteMapNodeItemType.Parent;
  311. SiteMapNodeItem it = new SiteMapNodeItem (nodes.Count, nt);
  312. it.SiteMapNode = node;
  313. InitializeItem (it);
  314. SiteMapNodeItemEventArgs args = new SiteMapNodeItemEventArgs (it);
  315. OnItemCreated (args);
  316. it.DataBind ();
  317. OnItemDataBound (args);
  318. nodes.Add (it);
  319. node = node.ParentNode;
  320. levels--;
  321. }
  322. if (PathDirection == PathDirection.RootToCurrent) {
  323. for (int n=nodes.Count - 1; n>=0; n--)
  324. Controls.Add ((Control)nodes[n]);
  325. } else {
  326. for (int n=0; n<nodes.Count; n++)
  327. Controls.Add ((Control)nodes[n]);
  328. }
  329. }
  330. protected virtual void InitializeItem (SiteMapNodeItem item)
  331. {
  332. switch (item.ItemType) {
  333. case SiteMapNodeItemType.Root:
  334. if (RootNodeTemplate != null) {
  335. item.ApplyStyle (NodeStyle);
  336. item.ApplyStyle (RootNodeStyle);
  337. RootNodeTemplate.InstantiateIn (item);
  338. }
  339. else {
  340. WebControl c = CreateNodeControl (true, item);
  341. c.ApplyStyle (NodeStyle);
  342. c.ApplyStyle (RootNodeStyle);
  343. item.Controls.Add (c);
  344. }
  345. break;
  346. case SiteMapNodeItemType.Current:
  347. if (CurrentNodeTemplate != null) {
  348. item.ApplyStyle (NodeStyle);
  349. item.ApplyStyle (CurrentNodeStyle);
  350. CurrentNodeTemplate.InstantiateIn (item);
  351. }
  352. else {
  353. WebControl c = CreateNodeControl (RenderCurrentNodeAsLink, item);
  354. c.ApplyStyle (NodeStyle);
  355. c.ApplyStyle (CurrentNodeStyle);
  356. item.Controls.Add (c);
  357. }
  358. break;
  359. case SiteMapNodeItemType.Parent:
  360. if (NodeTemplate != null) {
  361. item.ApplyStyle (NodeStyle);
  362. NodeTemplate.InstantiateIn (item);
  363. }
  364. else {
  365. WebControl c = CreateNodeControl (true, item);
  366. c.ApplyStyle (NodeStyle);
  367. item.Controls.Add (c);
  368. }
  369. break;
  370. case SiteMapNodeItemType.PathSeparator:
  371. if (PathSeparatorTemplate != null) {
  372. item.ApplyStyle (PathSeparatorStyle);
  373. PathSeparatorTemplate.InstantiateIn (item);
  374. }
  375. else {
  376. Label h = new Label ();
  377. h.Text = PathSeparator;
  378. h.ApplyStyle (PathSeparatorStyle);
  379. item.Controls.Add (h);
  380. }
  381. break;
  382. }
  383. }
  384. WebControl CreateNodeControl (bool link, SiteMapNodeItem item)
  385. {
  386. if (link) {
  387. HyperLink h = new HyperLink ();
  388. h.Text = item.SiteMapNode.Title;
  389. h.NavigateUrl = item.SiteMapNode.Url;
  390. if (ShowToolTips)
  391. h.ToolTip = item.SiteMapNode.Description;
  392. return h;
  393. }
  394. else {
  395. Label h = new Label ();
  396. h.Text = item.SiteMapNode.Title;
  397. if (ShowToolTips)
  398. h.ToolTip = item.SiteMapNode.Description;
  399. return h;
  400. }
  401. }
  402. protected override void LoadViewState (object savedState)
  403. {
  404. if (savedState == null) {
  405. base.LoadViewState (null);
  406. return;
  407. }
  408. object[] states = (object[]) savedState;
  409. base.LoadViewState (states [0]);
  410. if (states[1] != null) ((IStateManager)CurrentNodeStyle).LoadViewState (states[1]);
  411. if (states[2] != null) ((IStateManager)NodeStyle).LoadViewState (states[2]);
  412. if (states[3] != null) ((IStateManager)PathSeparatorStyle).LoadViewState (states[3]);
  413. if (states[4] != null) ((IStateManager)RootNodeStyle).LoadViewState (states[4]);
  414. }
  415. [MonoTODO]
  416. protected override void OnDataBinding (EventArgs e)
  417. {
  418. throw new NotImplementedException ();
  419. }
  420. protected internal override void Render (HtmlTextWriter w)
  421. {
  422. base.Render (w);
  423. }
  424. protected internal override void RenderContents (HtmlTextWriter w)
  425. {
  426. base.RenderContents (w);
  427. }
  428. protected override object SaveViewState ()
  429. {
  430. object[] state = new object [5];
  431. state [0] = base.SaveViewState ();
  432. if (currentNodeStyle != null) state [1] = ((IStateManager)currentNodeStyle).SaveViewState ();
  433. if (nodeStyle != null) state [2] = ((IStateManager)nodeStyle).SaveViewState ();
  434. if (pathSeparatorStyle != null) state [3] = ((IStateManager)pathSeparatorStyle).SaveViewState ();
  435. if (rootNodeStyle != null) state [4] = ((IStateManager)rootNodeStyle).SaveViewState ();
  436. for (int n=0; n<state.Length; n++)
  437. if (state [n] != null) return state;
  438. return null;
  439. }
  440. protected override void TrackViewState ()
  441. {
  442. base.TrackViewState();
  443. if (currentNodeStyle != null) ((IStateManager)currentNodeStyle).TrackViewState();
  444. if (nodeStyle != null) ((IStateManager)nodeStyle).TrackViewState();
  445. if (pathSeparatorStyle != null) ((IStateManager)pathSeparatorStyle).TrackViewState();
  446. if (rootNodeStyle != null) ((IStateManager)rootNodeStyle).TrackViewState();
  447. }
  448. }
  449. }
  450. #endif