MenuItem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. IHierarchyData hierarchyData;
  50. bool gotBinding;
  51. MenuItemBinding binding;
  52. PropertyDescriptorCollection boundProperties;
  53. public MenuItem ()
  54. {
  55. }
  56. public MenuItem (string text)
  57. {
  58. Text = text;
  59. }
  60. public MenuItem (string text, string value)
  61. {
  62. Text = text;
  63. Value = value;
  64. }
  65. public MenuItem (string text, string value, string imageUrl)
  66. {
  67. Text = text;
  68. Value = value;
  69. ImageUrl = imageUrl;
  70. }
  71. public MenuItem (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. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  80. [Browsable (false)]
  81. public int Depth {
  82. get {
  83. if (depth != -1) return depth;
  84. depth = 0;
  85. MenuItem nod = parent;
  86. while (nod != null) {
  87. depth++;
  88. nod = nod.parent;
  89. }
  90. return depth;
  91. }
  92. }
  93. void ResetPathData ()
  94. {
  95. path = null;
  96. depth = -1;
  97. gotBinding = false;
  98. }
  99. internal Menu Menu {
  100. get { return menu; }
  101. set {
  102. if (SelectedFlag) {
  103. if (value != null)
  104. value.SetSelectedItem (this);
  105. else if (menu != null)
  106. menu.SetSelectedItem (null);
  107. }
  108. menu = value;
  109. if (items != null)
  110. items.SetMenu (menu);
  111. ResetPathData ();
  112. }
  113. }
  114. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  115. [DefaultValue (false)]
  116. [Browsable (false)]
  117. public bool DataBound {
  118. get { return hierarchyData != null; }
  119. }
  120. [DefaultValue (null)]
  121. [Browsable (false)]
  122. public object DataItem {
  123. get {
  124. if (hierarchyData == null) throw new InvalidOperationException ("MenuItem is not data bound.");
  125. return hierarchyData.Item;
  126. }
  127. }
  128. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  129. [DefaultValue ("")]
  130. [Browsable (false)]
  131. public string DataPath {
  132. get {
  133. if (hierarchyData == null) throw new InvalidOperationException ("MenuItem is not data bound.");
  134. return hierarchyData.Path;
  135. }
  136. }
  137. [MergableProperty (false)]
  138. [Browsable (false)]
  139. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  140. public MenuItemCollection ChildItems {
  141. get {
  142. if (items == null) {
  143. if (DataBound)
  144. FillBoundChildren ();
  145. else
  146. items = new MenuItemCollection (this);
  147. if (((IStateManager)this).IsTrackingViewState)
  148. ((IStateManager)items).TrackViewState();
  149. }
  150. return items;
  151. }
  152. }
  153. [DefaultValue ("")]
  154. [UrlProperty]
  155. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  156. public string ImageUrl {
  157. get {
  158. object o = ViewState ["ImageUrl"];
  159. if (o != null) return (string)o;
  160. if (DataBound) {
  161. MenuItemBinding bin = GetBinding ();
  162. if (bin != null) {
  163. if (bin.ImageUrlField != "")
  164. return (string) GetBoundPropertyValue (bin.ImageUrlField);
  165. return bin.ImageUrl;
  166. }
  167. }
  168. return "";
  169. }
  170. set {
  171. ViewState ["ImageUrl"] = value;
  172. }
  173. }
  174. [DefaultValue ("")]
  175. [UrlProperty]
  176. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  177. public string NavigateUrl {
  178. get {
  179. object o = ViewState ["NavigateUrl"];
  180. if (o != null) return (string)o;
  181. if (DataBound) {
  182. MenuItemBinding bin = GetBinding ();
  183. if (bin != null) {
  184. if (bin.NavigateUrlField != "")
  185. return (string) GetBoundPropertyValue (bin.NavigateUrlField);
  186. return bin.NavigateUrl;
  187. }
  188. }
  189. return "";
  190. }
  191. set {
  192. ViewState ["NavigateUrl"] = value;
  193. }
  194. }
  195. [DefaultValue ("")]
  196. public string Target {
  197. get {
  198. object o = ViewState ["Target"];
  199. if(o != null) return (string)o;
  200. if (DataBound) {
  201. MenuItemBinding bin = GetBinding ();
  202. if (bin != null) {
  203. if (bin.TargetField != "")
  204. return (string) GetBoundPropertyValue (bin.TargetField);
  205. return bin.Target;
  206. }
  207. }
  208. return "";
  209. }
  210. set {
  211. ViewState ["Target"] = value;
  212. }
  213. }
  214. [Localizable (true)]
  215. [DefaultValue ("")]
  216. public string Text {
  217. get {
  218. object o = ViewState ["Text"];
  219. if (o != null) return (string)o;
  220. if (DataBound) {
  221. MenuItemBinding bin = GetBinding ();
  222. if (bin != null) {
  223. string text;
  224. if (bin.TextField != "")
  225. text = (string) GetBoundPropertyValue (bin.TextField);
  226. else if (bin.Text != "")
  227. text = bin.Text;
  228. else
  229. text = hierarchyData.ToString ();
  230. if (bin.FormatString.Length != 0)
  231. text = string.Format (bin.FormatString, text);
  232. return text;
  233. }
  234. return hierarchyData.ToString ();
  235. }
  236. return "";
  237. }
  238. set {
  239. ViewState ["Text"] = value;
  240. }
  241. }
  242. [Localizable (true)]
  243. [DefaultValue ("")]
  244. public string ToolTip {
  245. get {
  246. object o = ViewState ["ToolTip"];
  247. if(o != null) return (string)o;
  248. if (DataBound) {
  249. MenuItemBinding bin = GetBinding ();
  250. if (bin != null) {
  251. if (bin.ToolTipField != "")
  252. return (string) GetBoundPropertyValue (bin.ToolTipField);
  253. return bin.ToolTip;
  254. }
  255. }
  256. return "";
  257. }
  258. set {
  259. ViewState ["ToolTip"] = value;
  260. }
  261. }
  262. [Localizable (true)]
  263. [DefaultValue ("")]
  264. public string Value {
  265. get {
  266. object o = ViewState ["Value"];
  267. if(o != null) return (string)o;
  268. if (DataBound) {
  269. MenuItemBinding bin = GetBinding ();
  270. if (bin != null) {
  271. if (bin.ValueField != "")
  272. return (string) GetBoundPropertyValue (bin.ValueField);
  273. if (bin.Value != "")
  274. return bin.Value;
  275. }
  276. return hierarchyData.ToString ();
  277. }
  278. return "";
  279. }
  280. set {
  281. ViewState ["Value"] = value;
  282. }
  283. }
  284. [DefaultValue (false)]
  285. public bool Selected {
  286. get {
  287. return SelectedFlag;
  288. }
  289. set {
  290. if (menu != null) {
  291. if (!value && menu.SelectedItem == this)
  292. menu.SetSelectedItem (null);
  293. else if (value)
  294. menu.SetSelectedItem (this);
  295. }
  296. else
  297. SelectedFlag = value;
  298. }
  299. }
  300. internal bool SelectedFlag {
  301. get {
  302. object o = ViewState ["Selected"];
  303. if(o != null) return (bool)o;
  304. return false;
  305. }
  306. set {
  307. ViewState ["Selected"] = value;
  308. }
  309. }
  310. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  311. [Browsable (false)]
  312. public MenuItem Parent {
  313. get { return parent; }
  314. }
  315. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  316. [Browsable (false)]
  317. public string ValuePath {
  318. get {
  319. if (menu == null) return Value;
  320. StringBuilder sb = new StringBuilder (Value);
  321. MenuItem item = parent;
  322. while (item != null) {
  323. sb.Insert (0, menu.PathSeparator);
  324. sb.Insert (0, item.Value);
  325. item = item.Parent;
  326. }
  327. return sb.ToString ();
  328. }
  329. }
  330. internal int Index {
  331. get { return index; }
  332. set { index = value; ResetPathData (); }
  333. }
  334. internal void SetParent (MenuItem item) {
  335. parent = item;
  336. ResetPathData ();
  337. }
  338. internal string Path {
  339. get {
  340. if (path != null) return path;
  341. StringBuilder sb = new StringBuilder (index.ToString());
  342. MenuItem item = parent;
  343. while (item != null) {
  344. sb.Insert (0, '_');
  345. sb.Insert (0, item.Index.ToString ());
  346. item = item.Parent;
  347. }
  348. path = sb.ToString ();
  349. return path;
  350. }
  351. }
  352. internal bool HasChildData {
  353. get { return items != null; }
  354. }
  355. void IStateManager.LoadViewState (object savedState)
  356. {
  357. if (savedState == null)
  358. return;
  359. object[] states = (object[]) savedState;
  360. ViewState.LoadViewState (states [0]);
  361. if (menu != null && SelectedFlag)
  362. menu.SetSelectedItem (this);
  363. if (states [1] != null)
  364. ((IStateManager)ChildItems).LoadViewState (states [1]);
  365. }
  366. object IStateManager.SaveViewState ()
  367. {
  368. object[] states = new object[2];
  369. states[0] = ViewState.SaveViewState();
  370. states[1] = (items == null ? null : ((IStateManager)items).SaveViewState());
  371. for (int i = 0; i < states.Length; i++) {
  372. if (states [i] != null)
  373. return states;
  374. }
  375. return null;
  376. }
  377. void IStateManager.TrackViewState ()
  378. {
  379. if (marked) return;
  380. marked = true;
  381. ViewState.TrackViewState();
  382. if (items != null)
  383. ((IStateManager)items).TrackViewState ();
  384. }
  385. bool IStateManager.IsTrackingViewState
  386. {
  387. get { return marked; }
  388. }
  389. internal void SetDirty ()
  390. {
  391. ViewState.SetDirty ();
  392. }
  393. object ICloneable.Clone ()
  394. {
  395. MenuItem nod = new MenuItem ();
  396. foreach (DictionaryEntry e in ViewState)
  397. nod.ViewState [(string)e.Key] = e.Value;
  398. foreach (ICloneable c in ChildItems)
  399. nod.ChildItems.Add ((MenuItem)c.Clone ());
  400. return nod;
  401. }
  402. internal void Bind (IHierarchyData hierarchyData)
  403. {
  404. this.hierarchyData = hierarchyData;
  405. }
  406. MenuItemBinding GetBinding ()
  407. {
  408. if (menu == null) return null;
  409. if (gotBinding) return binding;
  410. binding = menu.FindBindingForItem (hierarchyData.Type, Depth);
  411. gotBinding = true;
  412. return binding;
  413. }
  414. object GetBoundPropertyValue (string name)
  415. {
  416. if (boundProperties == null) {
  417. ICustomTypeDescriptor desc = hierarchyData as ICustomTypeDescriptor;
  418. if (desc == null)
  419. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  420. boundProperties = desc.GetProperties ();
  421. }
  422. PropertyDescriptor prop = boundProperties.Find (name, true);
  423. if (prop == null)
  424. throw new InvalidOperationException ("Property '" + name + "' not found in data bound item");
  425. return prop.GetValue (hierarchyData);
  426. }
  427. void FillBoundChildren ()
  428. {
  429. items = new MenuItemCollection (this);
  430. if (!hierarchyData.HasChildren) return;
  431. IHierarchicalEnumerable e = hierarchyData.GetChildren ();
  432. foreach (object obj in e) {
  433. IHierarchyData hdata = e.GetHierarchyData (obj);
  434. MenuItem item = new MenuItem ();
  435. item.Bind (hdata);
  436. items.Add (item);
  437. }
  438. }
  439. }
  440. }
  441. #endif