-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortable.cs
More file actions
82 lines (68 loc) · 1.67 KB
/
Sortable.cs
File metadata and controls
82 lines (68 loc) · 1.67 KB
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
using System;
using System.Collections;
using System.Windows.Forms;
namespace generalgff
{
/// <summary>
/// A TreeNode that can be sorted according to a specified label (instead of
/// its displayed text).
/// </summary>
sealed class Sortable
: TreeNode
{
internal string _label;
internal Sortable(string text, string label)
: base(text)
{
_label = label;
}
/// <summary>
/// Duplicates a specified Sortable since Clone() is effed regardless.
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
internal static Sortable Duplicate(Sortable src)
{
var dst = new Sortable(src.Text, src._label);
dst.Tag = GffData.Field.Duplicate((GffData.Field)src.Tag);
AddSubs(src, dst);
return dst;
}
/// <summary>
/// Recurses subnodes for Duplicate().
/// </summary>
/// <param name="src"></param>
/// <param name="dst"></param>
static void AddSubs(TreeNode src, TreeNode dst)
{
for (int i = 0; i != src.Nodes.Count; ++i)
{
var node = new Sortable(src.Nodes[i].Text, ((Sortable)src.Nodes[i])._label);
node.Tag = GffData.Field.Duplicate((GffData.Field)src.Nodes[i].Tag);
dst.Nodes.Add(node);
AddSubs((Sortable)src.Nodes[i], node);
}
}
}
/// <summary>
/// A sorter of Sortable treenodes.
/// </summary>
sealed class NodeSorter
: IComparer
{
public int Compare(object a, object b)
{
var a_ = a as Sortable;
var b_ = b as Sortable;
int ai, bi;
if ( Int32.TryParse(a_._label, out ai)
&& Int32.TryParse(b_._label, out bi))
{
return ai.CompareTo(bi);
}
return String.Compare(a_._label,
b_._label,
StringComparison.OrdinalIgnoreCase);
}
}
}