TreeGridNode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //---------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
  6. //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  8. //PARTICULAR PURPOSE.
  9. //---------------------------------------------------------------------
  10. using System;
  11. using System.ComponentModel;
  12. using System.Drawing;
  13. using System.Drawing.Design;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  17. {
  18. [
  19. ToolboxItem(false),
  20. DesignTimeVisible(false)
  21. ]
  22. public class TreeGridNode : DataGridViewRow//, IComponent
  23. {
  24. internal C_DataGridView _grid;
  25. internal TreeGridNode _parent;
  26. internal TreeGridNodeCollection _owner;
  27. internal bool IsExpanded;
  28. internal bool IsRoot;
  29. internal bool _isSited;
  30. internal bool _isFirstSibling;
  31. internal bool _isLastSibling;
  32. internal Image _image;
  33. internal int _imageIndex;
  34. private Random rndSeed = new Random();
  35. public int UniqueValue = -1;
  36. TreeGridCell _treeCell;
  37. TreeGridNodeCollection childrenNodes;
  38. private int _index;
  39. private int _level;
  40. private bool childCellsCreated = false;
  41. // needed for IComponent
  42. private ISite site = null;
  43. private EventHandler disposed = null;
  44. //internal TreeGridNode(dkDataGridView owner)
  45. // : this()
  46. //{
  47. // this._grid = owner;
  48. // this.IsExpanded = true;
  49. //}
  50. public TreeGridNode()
  51. {
  52. _index = -1;
  53. _level = -1;
  54. IsExpanded = false;
  55. UniqueValue = this.rndSeed.Next();
  56. _isSited = false;
  57. _isFirstSibling = false;
  58. _isLastSibling = false;
  59. _imageIndex = -1;
  60. }
  61. public override object Clone()
  62. {
  63. TreeGridNode r = new TreeGridNode();
  64. r = (TreeGridNode)base.Clone();
  65. r.UniqueValue = -1;
  66. r._level = this._level;
  67. r._grid = this._grid;
  68. r._parent = this.Parent;
  69. r._imageIndex = this._imageIndex;
  70. if (r._imageIndex == -1)
  71. //r.Image = this.Image;
  72. r.IsExpanded = this.IsExpanded;
  73. //r.treeCell = new TreeGridCell();
  74. return r;
  75. }
  76. internal protected virtual void UnSited()
  77. {
  78. // This row is being removed from being displayed on the grid.
  79. TreeGridCell cell;
  80. foreach (DataGridViewCell DGVcell in this.Cells)
  81. {
  82. cell = DGVcell as TreeGridCell;
  83. if (cell != null)
  84. {
  85. cell.UnSited();
  86. }
  87. }
  88. this._isSited = false;
  89. }
  90. internal protected virtual void Sited()
  91. {
  92. // This row is being added to the grid.
  93. this._isSited = true;
  94. this.childCellsCreated = true;
  95. //Debug.Assert(this._grid != null);
  96. TreeGridCell cell;
  97. foreach (DataGridViewCell DGVcell in this.Cells)
  98. {
  99. cell = DGVcell as TreeGridCell;
  100. if (cell != null)
  101. {
  102. cell.Sited();// Level = this.Level;
  103. }
  104. }
  105. }
  106. // Represents the index of this row in the Grid
  107. [System.ComponentModel.Description("Represents the index of this row in the Grid. Advanced usage."),
  108. System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced),
  109. Browsable(false),
  110. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  111. public int RowIndex
  112. {
  113. get
  114. {
  115. return base.Index;
  116. }
  117. }
  118. // Represents the index of this row based upon its position in the collection.
  119. [Browsable(false),
  120. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  121. public new int Index
  122. {
  123. get
  124. {
  125. if (_index == -1)
  126. {
  127. // get the index from the collection if unknown
  128. _index = this._owner.IndexOf(this);
  129. }
  130. return _index;
  131. }
  132. internal set
  133. {
  134. _index = value;
  135. }
  136. }
  137. [Browsable(false),
  138. EditorBrowsable(EditorBrowsableState.Never),
  139. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  140. //public ImageList ImageList
  141. //{
  142. // get
  143. // {
  144. // if (this._grid != null)
  145. // return this._grid.ImageList;
  146. // else
  147. // return null;
  148. // }
  149. //}
  150. private bool ShouldSerializeImageIndex()
  151. {
  152. return (this._imageIndex != -1 && this._image == null);
  153. }
  154. [Category("Appearance"),
  155. Description("..."), DefaultValue(-1),
  156. TypeConverter(typeof(ImageIndexConverter)),
  157. Editor("System.Windows.Forms.Design.ImageIndexEditor", typeof(UITypeEditor))]
  158. public int ImageIndex
  159. {
  160. get { return _imageIndex; }
  161. set
  162. {
  163. _imageIndex = value;
  164. if (_imageIndex != -1)
  165. {
  166. // when a imageIndex is provided we do not store the image.
  167. this._image = null;
  168. }
  169. if (this._isSited)
  170. {
  171. // when the image changes the cell's style must be updated
  172. this._treeCell.UpdateStyle();
  173. if (this.Displayed)
  174. this._grid.InvalidateRow(this.RowIndex);
  175. }
  176. }
  177. }
  178. private bool ShouldSerializeImage()
  179. {
  180. return (this._imageIndex == -1 && this._image != null);
  181. }
  182. //public Image Image
  183. //{
  184. // get
  185. // {
  186. // if (_image == null && _imageIndex != -1)
  187. // {
  188. // if (this.ImageList != null && this._imageIndex < this.ImageList.Images.Count)
  189. // {
  190. // // get image from image index
  191. // return this.ImageList.Images[this._imageIndex];
  192. // }
  193. // else
  194. // return null;
  195. // }
  196. // else
  197. // {
  198. // // image from image property
  199. // return this._image;
  200. // };
  201. // }
  202. // set
  203. // {
  204. // _image = value;
  205. // if (_image != null)
  206. // {
  207. // // when a image is provided we do not store the imageIndex.
  208. // this._imageIndex = -1;
  209. // }
  210. // if (this._isSited)
  211. // {
  212. // // when the image changes the cell's style must be updated
  213. // this._treeCell.UpdateStyle();
  214. // if (this.Displayed)
  215. // this._grid.InvalidateRow(this.RowIndex);
  216. // }
  217. // }
  218. //}
  219. protected override DataGridViewCellCollection CreateCellsInstance()
  220. {
  221. DataGridViewCellCollection cells = base.CreateCellsInstance();
  222. cells.CollectionChanged += cells_CollectionChanged;
  223. return cells;
  224. }
  225. void cells_CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
  226. {
  227. try
  228. {
  229. // Exit if there already is a tree cell for this row
  230. if (_treeCell != null) return;
  231. if (e.Action == System.ComponentModel.CollectionChangeAction.Add
  232. || e.Action == System.ComponentModel.CollectionChangeAction.Refresh)
  233. {
  234. TreeGridCell treeCell = null;
  235. if (e.Element == null)
  236. {
  237. foreach (DataGridViewCell cell in base.Cells)
  238. {
  239. //if (cell.GetType().IsAssignableFrom(typeof(TreeGridCell)))
  240. //{
  241. // treeCell = (TreeGridCell)cell;
  242. // break;
  243. //}
  244. if (cell.GetType().FullName == "Dongke.Finance.Framework.Controls.TreeGridCell")
  245. {
  246. treeCell = (TreeGridCell)cell;
  247. break;
  248. }
  249. }
  250. }
  251. else
  252. {
  253. treeCell = e.Element as TreeGridCell;
  254. }
  255. if (treeCell != null)
  256. _treeCell = treeCell;
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. throw ex;
  262. }
  263. }
  264. [Category("Data"), Description("The collection of root nodes in the treelist.")]
  265. public TreeGridNodeCollection Nodes
  266. {
  267. get
  268. {
  269. if (childrenNodes == null)
  270. {
  271. childrenNodes = new TreeGridNodeCollection(this);
  272. }
  273. return childrenNodes;
  274. }
  275. set { ;}
  276. }
  277. // Create a new Cell property because by default a row is not in the grid and won't
  278. // have any cells. We have to fabricate the cell collection ourself.
  279. [Browsable(false),
  280. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  281. public new DataGridViewCellCollection Cells
  282. {
  283. get
  284. {
  285. if (!childCellsCreated && this.DataGridView == null)
  286. {
  287. if (this._grid == null) return null;
  288. this.CreateCells(this._grid);
  289. childCellsCreated = true;
  290. }
  291. return base.Cells;
  292. }
  293. }
  294. [Browsable(false),
  295. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  296. public int Level
  297. {
  298. get
  299. {
  300. if (this._level == -1)
  301. {
  302. // calculate level
  303. int walk = 0;
  304. TreeGridNode walkRow = this.Parent;
  305. while (walkRow != null)
  306. {
  307. walk++;
  308. walkRow = walkRow.Parent;
  309. }
  310. this._level = walk;
  311. }
  312. return this._level;
  313. }
  314. }
  315. [Browsable(false),
  316. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  317. public TreeGridNode Parent
  318. {
  319. get
  320. {
  321. return this._parent;
  322. }
  323. }
  324. [Browsable(false),
  325. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  326. public virtual bool HasChildren
  327. {
  328. get
  329. {
  330. return (this.childrenNodes != null && this.Nodes.Count != 0);
  331. }
  332. }
  333. [Browsable(false)]
  334. public bool IsSited
  335. {
  336. get
  337. {
  338. return this._isSited;
  339. }
  340. }
  341. [Browsable(false)]
  342. public bool IsFirstSibling
  343. {
  344. get
  345. {
  346. return (this.Index == 0);
  347. }
  348. }
  349. [Browsable(false)]
  350. public bool IsLastSibling
  351. {
  352. get
  353. {
  354. TreeGridNode parent = this.Parent;
  355. if (parent != null && parent.HasChildren)
  356. {
  357. return (this.Index == parent.Nodes.Count - 1);
  358. }
  359. else
  360. return true;
  361. }
  362. }
  363. public virtual bool Collapse()
  364. {
  365. return this._grid.CollapseNode(this);
  366. }
  367. public virtual bool Expand()
  368. {
  369. if (this._grid != null)
  370. return this._grid.ExpandNode(this);
  371. else
  372. {
  373. this.IsExpanded = true;
  374. return true;
  375. }
  376. }
  377. internal protected virtual bool InsertChildNode(int index, TreeGridNode node)
  378. {
  379. node._parent = this;
  380. node._grid = this._grid;
  381. // ensure that all children of this node has their grid set
  382. if (this._grid != null)
  383. UpdateChildNodes(node);
  384. //TODO: do we need to use index parameter?
  385. if ((this._isSited || this.IsRoot) && this.IsExpanded)
  386. this._grid.SiteNode(node);
  387. return true;
  388. }
  389. internal protected virtual bool InsertChildNodes(int index, params TreeGridNode[] nodes)
  390. {
  391. foreach (TreeGridNode node in nodes)
  392. {
  393. this.InsertChildNode(index, node);
  394. }
  395. return true;
  396. }
  397. internal protected virtual bool AddChildNode(TreeGridNode node)
  398. {
  399. node._parent = this;
  400. node._grid = this._grid;
  401. // ensure that all children of this node has their grid set
  402. if (this._grid != null)
  403. UpdateChildNodes(node);
  404. if ((this._isSited || this.IsRoot) && this.IsExpanded && !node._isSited)
  405. this._grid.SiteNode(node);
  406. return true;
  407. }
  408. internal protected virtual bool AddChildNodes(params TreeGridNode[] nodes)
  409. {
  410. //TODO: Convert the final call into an SiteNodes??
  411. foreach (TreeGridNode node in nodes)
  412. {
  413. this.AddChildNode(node);
  414. }
  415. return true;
  416. }
  417. internal protected virtual bool RemoveChildNode(TreeGridNode node)
  418. {
  419. if ((this.IsRoot || this._isSited) && this.IsExpanded)
  420. {
  421. //We only unsite out child node if we are sited and expanded.
  422. this._grid.UnSiteNode(node);
  423. }
  424. node._grid = null;
  425. node._parent = null;
  426. return true;
  427. }
  428. internal protected virtual bool ClearNodes()
  429. {
  430. if (this.HasChildren)
  431. {
  432. for (int i = this.Nodes.Count - 1; i >= 0; i--)
  433. {
  434. this.Nodes.RemoveAt(i);
  435. }
  436. }
  437. return true;
  438. }
  439. [
  440. Browsable(false),
  441. EditorBrowsable(EditorBrowsableState.Advanced)
  442. ]
  443. public event EventHandler Disposed
  444. {
  445. add
  446. {
  447. this.disposed += value;
  448. }
  449. remove
  450. {
  451. this.disposed -= value;
  452. }
  453. }
  454. [
  455. Browsable(false),
  456. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
  457. ]
  458. public ISite Site
  459. {
  460. get
  461. {
  462. return this.site;
  463. }
  464. set
  465. {
  466. this.site = value;
  467. }
  468. }
  469. private void UpdateChildNodes(TreeGridNode node)
  470. {
  471. if (node.HasChildren)
  472. {
  473. foreach (TreeGridNode childNode in node.Nodes)
  474. {
  475. childNode._grid = node._grid;
  476. this.UpdateChildNodes(childNode);
  477. }
  478. }
  479. }
  480. public override string ToString()
  481. {
  482. StringBuilder sb = new StringBuilder(36);
  483. sb.Append("TreeGridNode { Index=");
  484. sb.Append(this.RowIndex.ToString(System.Globalization.CultureInfo.CurrentCulture));
  485. sb.Append(" }");
  486. return sb.ToString();
  487. }
  488. //protected override void Dispose(bool disposing) {
  489. // if (disposing)
  490. // {
  491. // lock(this)
  492. // {
  493. // if (this.site != null && this.site.Container != null)
  494. // {
  495. // this.site.Container.Remove(this);
  496. // }
  497. // if (this.disposed != null)
  498. // {
  499. // this.disposed(this, EventArgs.Empty);
  500. // }
  501. // }
  502. // }
  503. // base.Dispose(disposing);
  504. //}
  505. }
  506. }