MenuItem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. //
  2. // System.Web.UI.WebControls.MenuItem.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  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. #if NET_2_0
  31. using System;
  32. using System.Collections;
  33. using System.Text;
  34. using System.ComponentModel;
  35. using System.Web.UI;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [ParseChildrenAttribute (true, "ChildItems")]
  39. public sealed class MenuItem: IStateManager, ICloneable
  40. {
  41. StateBag ViewState = new StateBag ();
  42. MenuItemCollection items;
  43. bool marked;
  44. Menu menu;
  45. MenuItem parent;
  46. int index;
  47. string path;
  48. int depth = -1;
  49. object dataItem;
  50. IHierarchyData hierarchyData;
  51. bool gotBinding;
  52. MenuItemBinding binding;
  53. PropertyDescriptorCollection boundProperties;
  54. public MenuItem ()
  55. {
  56. }
  57. public MenuItem (string text)
  58. {
  59. Text = text;
  60. }
  61. public MenuItem (string text, string value)
  62. {
  63. Text = text;
  64. Value = value;
  65. }
  66. public MenuItem (string text, string value, string imageUrl)
  67. {
  68. Text = text;
  69. Value = value;
  70. ImageUrl = imageUrl;
  71. }
  72. public MenuItem (string text, string value, string imageUrl, string navigateUrl)
  73. {
  74. Text = text;
  75. Value = value;
  76. ImageUrl = imageUrl;
  77. NavigateUrl = navigateUrl;
  78. }
  79. public MenuItem (string text, string value, string imageUrl, string navigateUrl, string target)
  80. {
  81. Text = text;
  82. Value = value;
  83. ImageUrl = imageUrl;
  84. NavigateUrl = navigateUrl;
  85. Target = target;
  86. }
  87. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  88. [Browsable (false)]
  89. public int Depth {
  90. get {
  91. if (depth != -1) return depth;
  92. if (Parent == null) depth = 0;
  93. else depth = Parent.Depth + 1;
  94. return depth;
  95. }
  96. }
  97. void ResetPathData ()
  98. {
  99. path = null;
  100. depth = -1;
  101. gotBinding = false;
  102. }
  103. internal Menu Menu {
  104. get { return menu; }
  105. set {
  106. menu = value;
  107. if (items != null)
  108. items.SetMenu (menu);
  109. ResetPathData ();
  110. }
  111. }
  112. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  113. [DefaultValue (false)]
  114. [Browsable (false)]
  115. public bool DataBound {
  116. get { return ViewState ["DataBound"] == null ? false : (bool) ViewState ["DataBound"]; }
  117. private set { ViewState ["DataBound"] = value; }
  118. }
  119. [DefaultValue (null)]
  120. [Browsable (false)]
  121. public object DataItem {
  122. get {
  123. if (!DataBound) throw new InvalidOperationException ("MenuItem is not data bound.");
  124. return dataItem;
  125. }
  126. }
  127. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  128. [DefaultValue ("")]
  129. [Browsable (false)]
  130. public string DataPath {
  131. get {
  132. return ViewState ["DataPath"] == null ? String.Empty : (String) ViewState ["DataPath"];
  133. }
  134. private set {
  135. ViewState ["DataPath"] = value;
  136. }
  137. }
  138. [MergableProperty (false)]
  139. [Browsable (false)]
  140. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  141. public MenuItemCollection ChildItems {
  142. get {
  143. if (items == null) {
  144. items = new MenuItemCollection (this);
  145. if (((IStateManager)this).IsTrackingViewState)
  146. ((IStateManager)items).TrackViewState();
  147. }
  148. return items;
  149. }
  150. }
  151. [DefaultValue ("")]
  152. [UrlProperty]
  153. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  154. public string ImageUrl {
  155. get {
  156. return ViewState ["ImageUrl"] == null ? String.Empty : (String) ViewState ["ImageUrl"];
  157. }
  158. set {
  159. ViewState ["ImageUrl"] = value;
  160. }
  161. }
  162. [DefaultValue ("")]
  163. [UrlProperty]
  164. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  165. public string NavigateUrl {
  166. get {
  167. return ViewState ["NavigateUrl"] == null ? String.Empty : (String) ViewState ["NavigateUrl"];
  168. }
  169. set {
  170. ViewState ["NavigateUrl"] = value;
  171. }
  172. }
  173. [DefaultValue ("")]
  174. [UrlProperty]
  175. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  176. public string PopOutImageUrl {
  177. get {
  178. return ViewState ["PopOutImageUrl"] == null ? String.Empty : (String) ViewState ["PopOutImageUrl"];
  179. }
  180. set {
  181. ViewState ["PopOutImageUrl"] = value;
  182. }
  183. }
  184. [DefaultValue ("")]
  185. public string Target {
  186. get {
  187. return ViewState ["Target"] == null ? String.Empty : (String) ViewState ["Target"];
  188. }
  189. set {
  190. ViewState ["Target"] = value;
  191. }
  192. }
  193. [Localizable (true)]
  194. [DefaultValue ("")]
  195. public string Text {
  196. get {
  197. object o = ViewState ["Text"];
  198. if (o == null)
  199. o = ViewState ["Value"];
  200. if (o != null)
  201. return (string) o;
  202. return String.Empty;
  203. }
  204. set {
  205. ViewState ["Text"] = value;
  206. }
  207. }
  208. [Localizable (true)]
  209. [DefaultValue ("")]
  210. public string ToolTip {
  211. get {
  212. return ViewState ["ToolTip"] == null ? String.Empty : (String) ViewState ["ToolTip"];
  213. }
  214. set {
  215. ViewState ["ToolTip"] = value;
  216. }
  217. }
  218. [Localizable (true)]
  219. [DefaultValue ("")]
  220. public string Value {
  221. get {
  222. object o = ViewState ["Value"];
  223. if (o == null)
  224. o = ViewState ["Text"];
  225. if (o != null)
  226. return (string) o;
  227. return String.Empty;
  228. }
  229. set {
  230. ViewState ["Value"] = value;
  231. }
  232. }
  233. [DefaultValue ("")]
  234. [UrlProperty]
  235. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  236. public string SeparatorImageUrl {
  237. get {
  238. return ViewState ["SeparatorImageUrl"] == null ? String.Empty : (String) ViewState ["SeparatorImageUrl"];
  239. }
  240. set {
  241. ViewState ["SeparatorImageUrl"] = value;
  242. }
  243. }
  244. [BrowsableAttribute (true)]
  245. [DefaultValueAttribute (true)]
  246. public bool Selectable {
  247. get {
  248. return ViewState ["Selectable"] == null ? true : (bool) ViewState ["Selectable"];
  249. }
  250. set {
  251. ViewState ["Selectable"] = value;
  252. }
  253. }
  254. [BrowsableAttribute (true)]
  255. [DefaultValueAttribute (true)]
  256. public bool Enabled {
  257. get {
  258. return ViewState ["Enabled"] == null ? true : (bool) ViewState ["Enabled"];
  259. }
  260. set {
  261. ViewState ["Enabled"] = value;
  262. }
  263. }
  264. internal bool BranchEnabled {
  265. get { return Enabled && (parent == null || parent.BranchEnabled); }
  266. }
  267. [DefaultValue (false)]
  268. [Browsable (true)]
  269. public bool Selected {
  270. get {
  271. if (menu != null)
  272. return menu.SelectedItem == this;
  273. else
  274. return false;
  275. }
  276. set {
  277. if (menu != null) {
  278. if (!value && menu.SelectedItem == this)
  279. menu.SetSelectedItem (null);
  280. else if (value)
  281. menu.SetSelectedItem (this);
  282. }
  283. }
  284. }
  285. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  286. [Browsable (false)]
  287. public MenuItem Parent {
  288. get { return parent; }
  289. }
  290. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  291. [Browsable (false)]
  292. public string ValuePath {
  293. get {
  294. if (menu == null) return Value;
  295. StringBuilder sb = new StringBuilder (Value);
  296. MenuItem item = parent;
  297. while (item != null) {
  298. sb.Insert (0, menu.PathSeparator);
  299. sb.Insert (0, item.Value);
  300. item = item.Parent;
  301. }
  302. return sb.ToString ();
  303. }
  304. }
  305. internal int Index {
  306. get { return index; }
  307. set { index = value; ResetPathData (); }
  308. }
  309. internal void SetParent (MenuItem item) {
  310. parent = item;
  311. ResetPathData ();
  312. }
  313. internal string Path {
  314. get {
  315. if (path != null) return path;
  316. StringBuilder sb = new StringBuilder (index.ToString());
  317. MenuItem item = parent;
  318. while (item != null) {
  319. sb.Insert (0, '_');
  320. sb.Insert (0, item.Index.ToString ());
  321. item = item.Parent;
  322. }
  323. path = sb.ToString ();
  324. return path;
  325. }
  326. }
  327. internal bool HasChildData {
  328. get { return items != null; }
  329. }
  330. void IStateManager.LoadViewState (object savedState)
  331. {
  332. if (savedState == null)
  333. return;
  334. object[] states = (object[]) savedState;
  335. ViewState.LoadViewState (states [0]);
  336. if (states [1] != null)
  337. ((IStateManager)ChildItems).LoadViewState (states [1]);
  338. }
  339. object IStateManager.SaveViewState ()
  340. {
  341. object[] states = new object[2];
  342. states[0] = ViewState.SaveViewState();
  343. states[1] = (items == null ? null : ((IStateManager)items).SaveViewState());
  344. for (int i = 0; i < states.Length; i++) {
  345. if (states [i] != null)
  346. return states;
  347. }
  348. return null;
  349. }
  350. void IStateManager.TrackViewState ()
  351. {
  352. if (marked) return;
  353. marked = true;
  354. ViewState.TrackViewState();
  355. if (items != null)
  356. ((IStateManager)items).TrackViewState ();
  357. }
  358. bool IStateManager.IsTrackingViewState
  359. {
  360. get { return marked; }
  361. }
  362. internal void SetDirty ()
  363. {
  364. ViewState.SetDirty (true);
  365. if (items != null)
  366. items.SetDirty ();
  367. }
  368. object ICloneable.Clone ()
  369. {
  370. MenuItem nod = new MenuItem ();
  371. foreach (DictionaryEntry e in ViewState)
  372. nod.ViewState [(string)e.Key] = e.Value;
  373. foreach (ICloneable c in ChildItems)
  374. nod.ChildItems.Add ((MenuItem)c.Clone ());
  375. return nod;
  376. }
  377. internal void Bind (IHierarchyData hierarchyData)
  378. {
  379. this.hierarchyData = hierarchyData;
  380. DataBound = true;
  381. DataPath = hierarchyData.Path;
  382. dataItem = hierarchyData.Item;
  383. MenuItemBinding bin = GetBinding ();
  384. if (bin != null) {
  385. // Bind Enabled property
  386. if (bin.EnabledField != "")
  387. try { Enabled = Convert.ToBoolean (GetBoundPropertyValue (bin.EnabledField)); }
  388. catch { Enabled = bin.Enabled; }
  389. else
  390. Enabled = bin.Enabled;
  391. // Bind ImageUrl property
  392. if (bin.ImageUrlField.Length > 0) {
  393. ImageUrl = Convert.ToString (GetBoundPropertyValue (bin.ImageUrlField));
  394. if (ImageUrl.Length == 0)
  395. ImageUrl = bin.ImageUrl;
  396. }
  397. else if (bin.ImageUrl.Length > 0)
  398. ImageUrl = bin.ImageUrl;
  399. // Bind NavigateUrl property
  400. if (bin.NavigateUrlField.Length > 0) {
  401. NavigateUrl = Convert.ToString (GetBoundPropertyValue (bin.NavigateUrlField));
  402. if (NavigateUrl.Length == 0)
  403. NavigateUrl = bin.NavigateUrl;
  404. }
  405. else if (bin.NavigateUrl.Length > 0)
  406. NavigateUrl = bin.NavigateUrl;
  407. // Bind PopOutImageUrl property
  408. if (bin.PopOutImageUrlField.Length > 0) {
  409. PopOutImageUrl = Convert.ToString (GetBoundPropertyValue (bin.PopOutImageUrlField));
  410. if (PopOutImageUrl.Length == 0)
  411. PopOutImageUrl = bin.PopOutImageUrl;
  412. }
  413. else if (bin.PopOutImageUrl.Length > 0)
  414. PopOutImageUrl = bin.PopOutImageUrl;
  415. // Bind Selectable property
  416. if (bin.SelectableField != "")
  417. try { Selectable = Convert.ToBoolean (GetBoundPropertyValue (bin.SelectableField)); }
  418. catch { Selectable = bin.Selectable; }
  419. else
  420. Selectable = bin.Selectable;
  421. // Bind SeparatorImageUrl property
  422. if (bin.SeparatorImageUrlField.Length > 0) {
  423. SeparatorImageUrl = Convert.ToString (GetBoundPropertyValue (bin.SeparatorImageUrlField));
  424. if (SeparatorImageUrl.Length == 0)
  425. SeparatorImageUrl = bin.SeparatorImageUrl;
  426. }
  427. else if (bin.SeparatorImageUrl.Length > 0)
  428. SeparatorImageUrl = bin.SeparatorImageUrl;
  429. // Bind Target property
  430. if (bin.TargetField.Length > 0) {
  431. Target = Convert.ToString (GetBoundPropertyValue (bin.TargetField));
  432. if (Target.Length == 0)
  433. Target = bin.Target;
  434. }
  435. else if (bin.Target.Length > 0)
  436. Target = bin.Target;
  437. // Bind ToolTip property
  438. if (bin.ToolTipField.Length > 0) {
  439. ToolTip = Convert.ToString (GetBoundPropertyValue (bin.ToolTipField));
  440. if (ToolTip.Length == 0)
  441. ToolTip = bin.ToolTip;
  442. }
  443. else if (bin.ToolTip.Length > 0)
  444. ToolTip = bin.ToolTip;
  445. // Bind Value property
  446. string value = null;
  447. if (bin.ValueField.Length > 0) {
  448. value = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
  449. }
  450. if (String.IsNullOrEmpty (value)) {
  451. if (bin.Value.Length > 0)
  452. value = bin.Value;
  453. else if (bin.Text.Length > 0)
  454. value = bin.Text;
  455. else
  456. value = String.Empty;
  457. }
  458. Value = value;
  459. // Bind Text property
  460. string text = null;
  461. if (bin.TextField.Length > 0) {
  462. text = Convert.ToString (GetBoundPropertyValue (bin.TextField));
  463. if (bin.FormatString.Length > 0)
  464. text = string.Format (bin.FormatString, text);
  465. }
  466. if (String.IsNullOrEmpty (text)) {
  467. if (bin.Text.Length > 0)
  468. text = bin.Text;
  469. else if (bin.Value.Length > 0)
  470. text = bin.Value;
  471. else
  472. text = String.Empty;
  473. }
  474. Text = text;
  475. }
  476. else {
  477. Text = Value = GetDefaultBoundText ();
  478. }
  479. INavigateUIData navigateUIData = hierarchyData as INavigateUIData;
  480. if (navigateUIData != null) {
  481. ToolTip = navigateUIData.Description;
  482. Text = navigateUIData.ToString ();
  483. NavigateUrl = navigateUIData.NavigateUrl;
  484. }
  485. }
  486. internal void SetDataItem (object item)
  487. {
  488. dataItem = item;
  489. }
  490. internal void SetDataPath (string path)
  491. {
  492. DataPath = path;
  493. }
  494. internal void SetDataBound (bool bound)
  495. {
  496. DataBound = bound;
  497. }
  498. string GetDefaultBoundText ()
  499. {
  500. if (hierarchyData != null) return hierarchyData.ToString ();
  501. else if (dataItem != null) return dataItem.ToString ();
  502. else return string.Empty;
  503. }
  504. string GetDataItemType ()
  505. {
  506. if (hierarchyData != null) return hierarchyData.Type;
  507. else if (dataItem != null) return dataItem.GetType().ToString ();
  508. else return string.Empty;
  509. }
  510. MenuItemBinding GetBinding ()
  511. {
  512. if (menu == null) return null;
  513. if (gotBinding) return binding;
  514. binding = menu.FindBindingForItem (GetDataItemType (), Depth);
  515. gotBinding = true;
  516. return binding;
  517. }
  518. object GetBoundPropertyValue (string name)
  519. {
  520. if (boundProperties == null) {
  521. if (hierarchyData != null)
  522. boundProperties = TypeDescriptor.GetProperties (hierarchyData);
  523. else
  524. boundProperties = TypeDescriptor.GetProperties (dataItem);
  525. }
  526. PropertyDescriptor prop = boundProperties.Find (name, true);
  527. if (prop == null)
  528. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  529. if (hierarchyData != null)
  530. return prop.GetValue (hierarchyData);
  531. else
  532. return prop.GetValue (dataItem);
  533. }
  534. }
  535. }
  536. #endif