source: branches/test1/Client/Common/UTreeState.pas

Last change on this file was 4, checked in by george, 14 years ago
  • Přidáno: Knihovna pro uchování stavu rozevření stromu.
  • Přidáno: Ikonky stromu skupin.
File size: 2.4 KB
Line 
1unit UTreeState;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Contnrs, StdCtrls, ComCtrls;
9
10type
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
34implementation
35
36{ TTreeState }
37
38procedure TTreeState.SaveNode(Node: TTreeNode; NodeState: TTreeNodeState);
39var
40 I: Integer;
41 NewNodeState: TTreeNodeState;
42begin
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;
52end;
53
54procedure TTreeState.LoadNode(Node: TTreeNode; NodeState: TTreeNodeState);
55var
56 I: Integer;
57 FindNodeState: TTreeNodeState;
58begin
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;
67end;
68
69procedure TTreeState.SaveTree(Tree: TTreeView);
70begin
71 TopItem.Free;
72 if Assigned(Tree.TopItem) then begin
73 TopItem := TTreeNodeState.Create;
74 SaveNode(Tree.TopItem, TopItem);
75 end;
76end;
77
78procedure TTreeState.LoadTree(Tree: TTreeView);
79begin
80 if Assigned(Tree.TopItem) and Assigned(TopItem) then
81 LoadNode(Tree.TopItem, TopItem);
82end;
83
84destructor TTreeState.Destroy;
85begin
86 inherited Destroy;
87 TopItem.Free;
88end;
89
90{ TTreeNodeState }
91
92function TTreeNodeState.Search(Text:string):TTreeNodeState;
93var
94 I: Integer;
95begin
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;
100end;
101
102end.
103
Note: See TracBrowser for help on using the repository browser.