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