1 | unit UFormRules;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
|
---|
9 | ComCtrls, ActnList, Menus, UGrammer, UListViewSort;
|
---|
10 |
|
---|
11 | type
|
---|
12 |
|
---|
13 | { TFormRules }
|
---|
14 |
|
---|
15 | TFormRules = class(TForm)
|
---|
16 | AAdd: TAction;
|
---|
17 | AModify: TAction;
|
---|
18 | ARemove: TAction;
|
---|
19 | ActionList1: TActionList;
|
---|
20 | CoolBar1: TCoolBar;
|
---|
21 | ListView1: TListView;
|
---|
22 | ListViewSort1: TListViewSort;
|
---|
23 | MenuItem1: TMenuItem;
|
---|
24 | MenuItem2: TMenuItem;
|
---|
25 | MenuItem3: TMenuItem;
|
---|
26 | PopupMenu1: TPopupMenu;
|
---|
27 | ToolBar1: TToolBar;
|
---|
28 | ToolButton1: TToolButton;
|
---|
29 | ToolButton2: TToolButton;
|
---|
30 | ToolButton3: TToolButton;
|
---|
31 | procedure AAddExecute(Sender: TObject);
|
---|
32 | procedure AModifyExecute(Sender: TObject);
|
---|
33 | procedure ARemoveExecute(Sender: TObject);
|
---|
34 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
35 | procedure FormCreate(Sender: TObject);
|
---|
36 | procedure FormShow(Sender: TObject);
|
---|
37 | procedure ListView1Data(Sender: TObject; Item: TListItem);
|
---|
38 | procedure ListView1DblClick(Sender: TObject);
|
---|
39 | procedure ListView1KeyPress(Sender: TObject; var Key: char);
|
---|
40 | procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
41 | Selected: Boolean);
|
---|
42 | function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
43 | procedure ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
44 | private
|
---|
45 | { private declarations }
|
---|
46 | public
|
---|
47 | Rules: TRules;
|
---|
48 | procedure UpdateInterface;
|
---|
49 | procedure ReloadList;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | var
|
---|
53 | FormRules: TFormRules;
|
---|
54 |
|
---|
55 |
|
---|
56 | implementation
|
---|
57 |
|
---|
58 | {$R *.lfm}
|
---|
59 |
|
---|
60 | uses
|
---|
61 | UFormRule, UCore;
|
---|
62 |
|
---|
63 | const
|
---|
64 | BooleanText: array[Boolean] of string = ('No', 'Yes');
|
---|
65 |
|
---|
66 | { TFormRules }
|
---|
67 |
|
---|
68 | procedure TFormRules.FormShow(Sender: TObject);
|
---|
69 | begin
|
---|
70 | Core.PersistentForm1.Load(Self);
|
---|
71 | ReloadList;
|
---|
72 | UpdateInterface;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TFormRules.AAddExecute(Sender: TObject);
|
---|
76 | var
|
---|
77 | NewRule: TRule;
|
---|
78 | begin
|
---|
79 | NewRule := TRule.Create;
|
---|
80 | NewRule.Grammer := Rules.Grammer;
|
---|
81 | FormRule := TFormRule.Create(nil);
|
---|
82 | FormRule.LoadData(NewRule);
|
---|
83 | if FormRule.ShowModal = mrOk then begin
|
---|
84 | FormRule.SaveData(NewRule);
|
---|
85 | Rules.Add(NewRule);
|
---|
86 | ReloadList;
|
---|
87 | end else FreeAndNil(NewRule);
|
---|
88 | FreeAndNil(FormRule);
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TFormRules.AModifyExecute(Sender: TObject);
|
---|
92 | var
|
---|
93 | Rule: TRule;
|
---|
94 | FormRule: TFormRule;
|
---|
95 | begin
|
---|
96 | if Assigned(ListView1.Selected) then begin
|
---|
97 | Rule := TRule(ListView1.Selected.Data);
|
---|
98 | FormRule := TFormRule.Create(nil);
|
---|
99 | FormRule.LoadData(Rule);
|
---|
100 | if FormRule.ShowModal = mrOk then begin
|
---|
101 | FormRule.SaveData(Rule);
|
---|
102 | ReloadList;
|
---|
103 | end;
|
---|
104 | FreeAndNil(FormRule);
|
---|
105 | end;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | procedure TFormRules.ARemoveExecute(Sender: TObject);
|
---|
109 | var
|
---|
110 | I: Integer;
|
---|
111 | begin
|
---|
112 | if MessageDlg('Remove item(s)', 'Do you want to remove selected item(s)?',
|
---|
113 | mtConfirmation, mbYesNo, 0) = mrYes then
|
---|
114 | for I := ListView1.Items.Count - 1 downto 0 do
|
---|
115 | if ListView1.Items[I].Selected then begin
|
---|
116 | Rules.Grammer.Modified := True;
|
---|
117 | Rules.Remove(TRule(ListView1.Items[I].Data));
|
---|
118 | end;
|
---|
119 | ReloadList;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TFormRules.FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
---|
123 | begin
|
---|
124 | Core.PersistentForm1.Save(Self);
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TFormRules.FormCreate(Sender: TObject);
|
---|
128 | begin
|
---|
129 | Core.CoolTranslator1.TranslateComponentRecursive(Self);
|
---|
130 | Rules := nil;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TFormRules.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
134 | begin
|
---|
135 | if (Item.Index >= 0) and (Item.Index < ListViewSort1.List.Count) then
|
---|
136 | with TRule(ListViewSort1.List[Item.Index]) do begin
|
---|
137 | Item.Caption := Name;
|
---|
138 | Item.Data := ListViewSort1.List[Item.Index];
|
---|
139 | Item.SubItems.Add(BooleanText[CreateSourceNode]);
|
---|
140 | Item.SubItems.Add(Items.GetString);
|
---|
141 | Item.SubItems.Add(IntToStr(Items.GetCharLength));
|
---|
142 | Item.SubItems.Add(RuleLevelText[Level]);
|
---|
143 | end;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | procedure TFormRules.ListView1DblClick(Sender: TObject);
|
---|
147 | begin
|
---|
148 | AModify.Execute;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TFormRules.ListView1KeyPress(Sender: TObject; var Key: char);
|
---|
152 | begin
|
---|
153 | if Key = #13 then AModify.Execute;
|
---|
154 | end;
|
---|
155 |
|
---|
156 | procedure TFormRules.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
157 | Selected: Boolean);
|
---|
158 | begin
|
---|
159 | UpdateInterface;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | function TFormRules.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
|
---|
163 | begin
|
---|
164 | Result := 0;
|
---|
165 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
---|
166 | with ListViewSort1 do
|
---|
167 | case Column of
|
---|
168 | 0: Result := CompareString(TRule(Item1).Name, TRule(Item2).Name);
|
---|
169 | 1: Result := CompareString(BooleanText[TRule(Item1).CreateSourceNode], BooleanText[TRule(Item2).CreateSourceNode]);
|
---|
170 | 2: Result := CompareString(TRule(Item1).GetString, TRule(Item2).GetString);
|
---|
171 | 3: Result := CompareInteger(TRule(Item1).Items.GetCharLength,
|
---|
172 | TRule(Item2).Items.GetCharLength);
|
---|
173 | 4: Result := CompareString(RuleLevelText[TRule(Item1).Level],
|
---|
174 | RuleLevelText[TRule(Item2).Level]);
|
---|
175 | end;
|
---|
176 | if ListViewSort1.Order = soDown then Result := -Result;
|
---|
177 | end else Result := 0;
|
---|
178 | end;
|
---|
179 |
|
---|
180 | procedure TFormRules.ListViewSort1Filter(ListViewSort: TListViewSort);
|
---|
181 | var
|
---|
182 | I: Integer;
|
---|
183 | begin
|
---|
184 | if Assigned(Rules) then begin
|
---|
185 | ListViewSort1.List.Count := Rules.Count;
|
---|
186 | for I := 0 to Rules.Count - 1 do
|
---|
187 | ListViewSort1.List[I] := Rules[I];
|
---|
188 | //FilterList(ListViewSort1.List);
|
---|
189 | end else ListViewSort1.List.Count := 0;
|
---|
190 | end;
|
---|
191 |
|
---|
192 | procedure TFormRules.UpdateInterface;
|
---|
193 | begin
|
---|
194 | AAdd.Enabled := Assigned(Rules);
|
---|
195 | AModify.Enabled := Assigned(Rules) and Assigned(ListView1.Selected);
|
---|
196 | ARemove.Enabled := Assigned(Rules) and Assigned(ListView1.Selected);
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TFormRules.ReloadList;
|
---|
200 | begin
|
---|
201 | ListViewSort1.Refresh;
|
---|
202 | ListView1.Refresh;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | end.
|
---|
206 |
|
---|