/
JsonTreeView.pas
111 lines (91 loc) · 2.26 KB
/
JsonTreeView.pas
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
unit JsonTreeView;
interface
uses
debug, comctrls, jsonhelpers, typex, stringx, systemx, betterobject, guihelpers, better_collections, classes;
type
TJSONTreeView = class(TTreeView)
private
FJson: IHolder<TJSON>;
FNodeRelations: TStringObjectList<TTreeNode>;
procedure SetJSon(const Value: IHolder<TJSON>);
protected
procedure Expand(Node: TTreeNode); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property json: IHolder<TJSON> read FJson write SetJSon;
procedure LoadNodeChildren(tn: TTreeNode; deep: ni);
procedure AddNodeRelation(addr: string; tn: TTreeNode);
published
end;
implementation
{ TJSONTreeView }
procedure TJSONTreeView.AddNodeRelation(addr: string; tn: TTreeNode);
var
s: string;
begin
s := 'nil';
if tn <> nil then
s := tn.text;
if tn <> nil then begin
FNodeRelations.add(addr, tn);
end;
end;
constructor TJSONTreeView.Create(AOwner: TComponent);
begin
inherited;
FNodeRelations := TStringObjectList<TTreeNode>.create;
FNodeRelations.Duplicates := dupIgnore;
end;
destructor TJSONTreeView.Destroy;
begin
Fnoderelations.free;
FNodeRelations := nil;
inherited;
end;
procedure TJSONTreeView.Expand(Node: TTreeNode);
begin
inherited;
//walk through all nodes and sync
Debug.Log('Expand node with text: '+node.Text);
LoadNodeChildren(node,2);
end;
procedure TJSONTreeView.LoadNodeChildren(tn: TTreeNode; deep: ni);
var
idx: ni;
nodeAddr: string;
t, cnt: ni;
n: TJSON;
ttn: TTreeNode;
begin
if json = nil then
exit;
if json.o = nil then
exit;
if deep = 0 then
exit;
idx := fnodeRelations.IndexOfObject(tn);
if idx < 0 then
exit;
nodeAddr := FNodeRelations.Keys[idx];
// Debug.Log('Expanding '+nodeAddr);
cnt := Treenode_GetChildCount(self, tn);
if cnt > 0 then begin
for t := 0 to cnt-1 do begin
ttn := TreeNode_GetChild(self, tn, t);
LoadNodeChildren(ttn,deep-1);
end;
end else begin
n := json.o.GetNode(nodeAddr);
guihelpers.JSONtoTreeNode(n, self, tn,deep);
end;
end;
procedure TJSONTreeView.SetJSOn(const Value: IHolder<TJSON>);
begin
items.Clear;
FNodeRelations.Clear;
FJson := Value;
if FJSON <> nil then
JSONtoTreeView(FJson.o, self,1);
end;
end.