MenuItem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. return ViewState ["Text"] == null ? String.Empty : (String) ViewState ["Text"];
  198. }
  199. set {
  200. ViewState ["Text"] = value;
  201. }
  202. }
  203. [Localizable (true)]
  204. [DefaultValue ("")]
  205. public string ToolTip {
  206. get {
  207. return ViewState ["ToolTip"] == null ? String.Empty : (String) ViewState ["ToolTip"];
  208. }
  209. set {
  210. ViewState ["ToolTip"] = value;
  211. }
  212. }
  213. [Localizable (true)]
  214. [DefaultValue ("")]
  215. public string Value {
  216. get {
  217. return ViewState ["Value"] == null ? String.Empty : (String) ViewState ["Value"];
  218. }
  219. set {
  220. ViewState ["Value"] = value;
  221. }
  222. }
  223. [DefaultValue ("")]
  224. [UrlProperty]
  225. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  226. public string SeparatorImageUrl {
  227. get {
  228. return ViewState ["SeparatorImageUrl"] == null ? String.Empty : (String) ViewState ["SeparatorImageUrl"];
  229. }
  230. set {
  231. ViewState ["SeparatorImageUrl"] = value;
  232. }
  233. }
  234. [BrowsableAttribute (true)]
  235. [DefaultValueAttribute (true)]
  236. public bool Selectable {
  237. get {
  238. return ViewState ["Selectable"] == null ? true : (bool) ViewState ["Selectable"];
  239. }
  240. set {
  241. ViewState ["Selectable"] = value;
  242. }
  243. }
  244. [BrowsableAttribute (true)]
  245. [DefaultValueAttribute (true)]
  246. public bool Enabled {
  247. get {
  248. return ViewState ["Enabled"] == null ? true : (bool) ViewState ["Enabled"];
  249. }
  250. set {
  251. ViewState ["Enabled"] = value;
  252. }
  253. }
  254. internal bool BranchEnabled {
  255. get { return Enabled && (parent == null || parent.BranchEnabled); }
  256. }
  257. [DefaultValue (false)]
  258. [Browsable (true)]
  259. public bool Selected {
  260. get {
  261. if (menu != null)
  262. return menu.SelectedItem == this;
  263. else
  264. return false;
  265. }
  266. set {
  267. if (menu != null) {
  268. if (!value && menu.SelectedItem == this)
  269. menu.SetSelectedItem (null);
  270. else if (value)
  271. menu.SetSelectedItem (this);
  272. }
  273. }
  274. }
  275. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  276. [Browsable (false)]
  277. public MenuItem Parent {
  278. get { return parent; }
  279. }
  280. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  281. [Browsable (false)]
  282. public string ValuePath {
  283. get {
  284. if (menu == null) return Value;
  285. StringBuilder sb = new StringBuilder (Value);
  286. MenuItem item = parent;
  287. while (item != null) {
  288. sb.Insert (0, menu.PathSeparator);
  289. sb.Insert (0, item.Value);
  290. item = item.Parent;
  291. }
  292. return sb.ToString ();
  293. }
  294. }
  295. internal int Index {
  296. get { return index; }
  297. set { index = value; ResetPathData (); }
  298. }
  299. internal void SetParent (MenuItem item) {
  300. parent = item;
  301. ResetPathData ();
  302. }
  303. internal string Path {
  304. get {
  305. if (path != null) return path;
  306. StringBuilder sb = new StringBuilder (index.ToString());
  307. MenuItem item = parent;
  308. while (item != null) {
  309. sb.Insert (0, '_');
  310. sb.Insert (0, item.Index.ToString ());
  311. item = item.Parent;
  312. }
  313. path = sb.ToString ();
  314. return path;
  315. }
  316. }
  317. internal bool HasChildData {
  318. get { return items != null; }
  319. }
  320. void IStateManager.LoadViewState (object savedState)
  321. {
  322. if (savedState == null)
  323. return;
  324. object[] states = (object[]) savedState;
  325. ViewState.LoadViewState (states [0]);
  326. if (states [1] != null)
  327. ((IStateManager)ChildItems).LoadViewState (states [1]);
  328. }
  329. object IStateManager.SaveViewState ()
  330. {
  331. object[] states = new object[2];
  332. states[0] = ViewState.SaveViewState();
  333. states[1] = (items == null ? null : ((IStateManager)items).SaveViewState());
  334. for (int i = 0; i < states.Length; i++) {
  335. if (states [i] != null)
  336. return states;
  337. }
  338. return null;
  339. }
  340. void IStateManager.TrackViewState ()
  341. {
  342. if (marked) return;
  343. marked = true;
  344. ViewState.TrackViewState();
  345. if (items != null)
  346. ((IStateManager)items).TrackViewState ();
  347. }
  348. bool IStateManager.IsTrackingViewState
  349. {
  350. get { return marked; }
  351. }
  352. internal void SetDirty ()
  353. {
  354. ViewState.SetDirty (true);
  355. if (items != null)
  356. items.SetDirty ();
  357. }
  358. object ICloneable.Clone ()
  359. {
  360. MenuItem nod = new MenuItem ();
  361. foreach (DictionaryEntry e in ViewState)
  362. nod.ViewState [(string)e.Key] = e.Value;
  363. foreach (ICloneable c in ChildItems)
  364. nod.ChildItems.Add ((MenuItem)c.Clone ());
  365. return nod;
  366. }
  367. internal void Bind (IHierarchyData hierarchyData)
  368. {
  369. this.hierarchyData = hierarchyData;
  370. DataBound = true;
  371. DataPath = hierarchyData.Path;
  372. dataItem = hierarchyData.Item;
  373. MenuItemBinding bin = GetBinding ();
  374. if (bin != null) {
  375. // Bind Enabled property
  376. if (bin.EnabledField != "")
  377. try { Enabled = Convert.ToBoolean (GetBoundPropertyValue (bin.EnabledField)); }
  378. catch { Enabled = bin.Enabled; }
  379. else
  380. Enabled = bin.Enabled;
  381. // Bind ImageUrl property
  382. if (bin.ImageUrlField.Length > 0) {
  383. ImageUrl = Convert.ToString (GetBoundPropertyValue (bin.ImageUrlField));
  384. if (ImageUrl.Length == 0)
  385. ImageUrl = bin.ImageUrl;
  386. }
  387. else if (bin.ImageUrl.Length > 0)
  388. ImageUrl = bin.ImageUrl;
  389. // Bind NavigateUrl property
  390. if (bin.NavigateUrlField.Length > 0) {
  391. NavigateUrl = Convert.ToString (GetBoundPropertyValue (bin.NavigateUrlField));
  392. if (NavigateUrl.Length == 0)
  393. NavigateUrl = bin.NavigateUrl;
  394. }
  395. else if (bin.NavigateUrl.Length > 0)
  396. NavigateUrl = bin.NavigateUrl;
  397. // Bind PopOutImageUrl property
  398. if (bin.PopOutImageUrlField.Length > 0) {
  399. PopOutImageUrl = Convert.ToString (GetBoundPropertyValue (bin.PopOutImageUrlField));
  400. if (PopOutImageUrl.Length == 0)
  401. PopOutImageUrl = bin.PopOutImageUrl;
  402. }
  403. else if (bin.PopOutImageUrl.Length > 0)
  404. PopOutImageUrl = bin.PopOutImageUrl;
  405. // Bind Selectable property
  406. if (bin.SelectableField != "")
  407. try { Selectable = Convert.ToBoolean (GetBoundPropertyValue (bin.SelectableField)); }
  408. catch { Selectable = bin.Selectable; }
  409. else
  410. Selectable = bin.Selectable;
  411. // Bind SeparatorImageUrl property
  412. if (bin.SeparatorImageUrlField.Length > 0) {
  413. SeparatorImageUrl = Convert.ToString (GetBoundPropertyValue (bin.SeparatorImageUrlField));
  414. if (SeparatorImageUrl.Length == 0)
  415. SeparatorImageUrl = bin.SeparatorImageUrl;
  416. }
  417. else if (bin.SeparatorImageUrl.Length > 0)
  418. SeparatorImageUrl = bin.SeparatorImageUrl;
  419. // Bind Target property
  420. if (bin.TargetField.Length > 0) {
  421. Target = Convert.ToString (GetBoundPropertyValue (bin.TargetField));
  422. if (Target.Length == 0)
  423. Target = bin.Target;
  424. }
  425. else if (bin.Target.Length > 0)
  426. Target = bin.Target;
  427. // Bind ToolTip property
  428. if (bin.ToolTipField.Length > 0) {
  429. ToolTip = Convert.ToString (GetBoundPropertyValue (bin.ToolTipField));
  430. if (ToolTip.Length == 0)
  431. ToolTip = bin.ToolTip;
  432. }
  433. else if (bin.ToolTip.Length > 0)
  434. ToolTip = bin.ToolTip;
  435. // Bind Value property
  436. if (bin.ValueField.Length > 0) {
  437. Value = Convert.ToString (GetBoundPropertyValue (bin.ValueField));
  438. if (Value.Length == 0)
  439. Value = bin.Value;
  440. if (Value.Length == 0)
  441. Value = bin.Text;
  442. }
  443. else if (bin.Value.Length > 0)
  444. Value = bin.Value;
  445. else if (bin.Text.Length > 0)
  446. Value = bin.Text;
  447. else
  448. Value = GetDefaultBoundText ();
  449. // Bind Text property
  450. if (bin.TextField.Length > 0) {
  451. Text = Convert.ToString (GetBoundPropertyValue (bin.TextField));
  452. if (bin.FormatString.Length > 0)
  453. Text = string.Format (bin.FormatString, Text);
  454. if (Text.Length == 0)
  455. Text = bin.Text;
  456. if (Text.Length == 0)
  457. Text = bin.Value;
  458. }
  459. else if (bin.Text.Length > 0)
  460. Text = bin.Text;
  461. else if (bin.Value.Length > 0)
  462. Text = bin.Value;
  463. else
  464. Text = GetDefaultBoundText ();
  465. }
  466. else {
  467. Text = Value = GetDefaultBoundText ();
  468. }
  469. INavigateUIData navigateUIData = hierarchyData as INavigateUIData;
  470. if (navigateUIData != null) {
  471. Text = navigateUIData.ToString ();
  472. NavigateUrl = navigateUIData.NavigateUrl;
  473. }
  474. }
  475. internal void SetDataItem (object item)
  476. {
  477. dataItem = item;
  478. }
  479. internal void SetDataPath (string path)
  480. {
  481. DataPath = path;
  482. }
  483. internal void SetDataBound (bool bound)
  484. {
  485. DataBound = bound;
  486. }
  487. string GetDefaultBoundText ()
  488. {
  489. if (hierarchyData != null) return hierarchyData.ToString ();
  490. else if (dataItem != null) return dataItem.ToString ();
  491. else return string.Empty;
  492. }
  493. string GetDataItemType ()
  494. {
  495. if (hierarchyData != null) return hierarchyData.Type;
  496. else if (dataItem != null) return dataItem.GetType().ToString ();
  497. else return string.Empty;
  498. }
  499. MenuItemBinding GetBinding ()
  500. {
  501. if (menu == null) return null;
  502. if (gotBinding) return binding;
  503. binding = menu.FindBindingForItem (GetDataItemType (), Depth);
  504. gotBinding = true;
  505. return binding;
  506. }
  507. object GetBoundPropertyValue (string name)
  508. {
  509. if (boundProperties == null) {
  510. if (hierarchyData != null)
  511. boundProperties = TypeDescriptor.GetProperties (hierarchyData);
  512. else
  513. boundProperties = TypeDescriptor.GetProperties (dataItem);
  514. }
  515. PropertyDescriptor prop = boundProperties.Find (name, true);
  516. if (prop == null)
  517. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  518. if (hierarchyData != null)
  519. return prop.GetValue (hierarchyData);
  520. else
  521. return prop.GetValue (dataItem);
  522. }
  523. }
  524. }
  525. #endif