1 | unit UTreeState;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Contnrs, StdCtrls, ComCtrls;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TTreeNodeState }
|
---|
13 |
|
---|
14 | TTreeNodeState = class(TObjectList)
|
---|
15 | Text: string;
|
---|
16 | Expanded: Boolean;
|
---|
17 | Selected: Boolean;
|
---|
18 | function Search(Text: string): TTreeNodeState;
|
---|
19 | end;
|
---|
20 |
|
---|
21 | { TTreeViewExpandState }
|
---|
22 |
|
---|
23 | { TTreeState }
|
---|
24 |
|
---|
25 | TTreeState = class
|
---|
26 | TopItem: TTreeNodeState;
|
---|
27 | procedure SaveNode(Node: TTreeNode; NodeState: TTreeNodeState);
|
---|
28 | procedure LoadNode(Node: TTreeNode; NodeState: TTreeNodeState);
|
---|
29 | procedure SaveTree(Tree: TTreeView);
|
---|
30 | procedure LoadTree(Tree: TTreeView);
|
---|
31 | destructor Destroy; override;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | implementation
|
---|
35 |
|
---|
36 | { TTreeState }
|
---|
37 |
|
---|
38 | procedure TTreeState.SaveNode(Node: TTreeNode; NodeState: TTreeNodeState);
|
---|
39 | var
|
---|
40 | I: Integer;
|
---|
41 | NewNodeState: TTreeNodeState;
|
---|
42 | begin
|
---|
43 | NodeState.Clear;
|
---|
44 | for I := 0 to Node.Count - 1 do begin
|
---|
45 | NewNodeState := TTreeNodeState.Create;
|
---|
46 | NewNodeState.Text := Node.Items[I].Text;
|
---|
47 | NewNodeState.Expanded := Node.Items[I].Expanded;
|
---|
48 | NewNodeState.Selected := Node.Items[I].Selected;
|
---|
49 | SaveNode(Node.Items[I], NewNodeState);
|
---|
50 | NodeState.Add(NewNodeState);
|
---|
51 | end;
|
---|
52 | end;
|
---|
53 |
|
---|
54 | procedure TTreeState.LoadNode(Node: TTreeNode; NodeState: TTreeNodeState);
|
---|
55 | var
|
---|
56 | I: Integer;
|
---|
57 | FindNodeState: TTreeNodeState;
|
---|
58 | begin
|
---|
59 | for I := 0 to Node.Count - 1 do begin
|
---|
60 | FindNodeState := NodeState.Search(Node.Items[I].Text);
|
---|
61 | if Assigned(FindNodeState) then begin
|
---|
62 | Node.Items[I].Expanded := FindNodeState.Expanded;
|
---|
63 | Node.Items[I].Selected := FindNodeState.Selected;
|
---|
64 | LoadNode(Node.Items[I], FindNodeState);
|
---|
65 | end;
|
---|
66 | end;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure TTreeState.SaveTree(Tree: TTreeView);
|
---|
70 | begin
|
---|
71 | TopItem.Free;
|
---|
72 | if Assigned(Tree.TopItem) then begin
|
---|
73 | TopItem := TTreeNodeState.Create;
|
---|
74 | SaveNode(Tree.TopItem, TopItem);
|
---|
75 | end;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | procedure TTreeState.LoadTree(Tree: TTreeView);
|
---|
79 | begin
|
---|
80 | if Assigned(Tree.TopItem) and Assigned(TopItem) then
|
---|
81 | LoadNode(Tree.TopItem, TopItem);
|
---|
82 | end;
|
---|
83 |
|
---|
84 | destructor TTreeState.Destroy;
|
---|
85 | begin
|
---|
86 | inherited Destroy;
|
---|
87 | TopItem.Free;
|
---|
88 | end;
|
---|
89 |
|
---|
90 | { TTreeNodeState }
|
---|
91 |
|
---|
92 | function TTreeNodeState.Search(Text:string):TTreeNodeState;
|
---|
93 | var
|
---|
94 | I: Integer;
|
---|
95 | begin
|
---|
96 | I := 0;
|
---|
97 | while (I < Count) and (TTreeNodeState(Items[I]).Text <> Text) do Inc(I);
|
---|
98 | if I < Count then Result := TTreeNodeState(Items[I])
|
---|
99 | else Result := nil;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | end.
|
---|
103 |
|
---|