source: branches/generator/Forms/UFormRules.pas

Last change on this file was 192, checked in by chronos, 6 years ago
  • Modified: Apply interface language to all forms.
File size: 5.4 KB
Line 
1unit UFormRules;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
9 ComCtrls, ActnList, Menus, UGrammer, UListViewSort;
10
11type
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
52var
53 FormRules: TFormRules;
54
55
56implementation
57
58{$R *.lfm}
59
60uses
61 UFormRule, UCore;
62
63const
64 BooleanText: array[Boolean] of string = ('No', 'Yes');
65
66{ TFormRules }
67
68procedure TFormRules.FormShow(Sender: TObject);
69begin
70 Core.PersistentForm1.Load(Self);
71 ReloadList;
72 UpdateInterface;
73end;
74
75procedure TFormRules.AAddExecute(Sender: TObject);
76var
77 NewRule: TRule;
78begin
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);
89end;
90
91procedure TFormRules.AModifyExecute(Sender: TObject);
92var
93 Rule: TRule;
94 FormRule: TFormRule;
95begin
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;
106end;
107
108procedure TFormRules.ARemoveExecute(Sender: TObject);
109var
110 I: Integer;
111begin
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;
120end;
121
122procedure TFormRules.FormClose(Sender: TObject; var CloseAction: TCloseAction);
123begin
124 Core.PersistentForm1.Save(Self);
125end;
126
127procedure TFormRules.FormCreate(Sender: TObject);
128begin
129 Core.CoolTranslator1.TranslateComponentRecursive(Self);
130 Rules := nil;
131end;
132
133procedure TFormRules.ListView1Data(Sender: TObject; Item: TListItem);
134begin
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;
144end;
145
146procedure TFormRules.ListView1DblClick(Sender: TObject);
147begin
148 AModify.Execute;
149end;
150
151procedure TFormRules.ListView1KeyPress(Sender: TObject; var Key: char);
152begin
153 if Key = #13 then AModify.Execute;
154end;
155
156procedure TFormRules.ListView1SelectItem(Sender: TObject; Item: TListItem;
157 Selected: Boolean);
158begin
159 UpdateInterface;
160end;
161
162function TFormRules.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
163begin
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;
178end;
179
180procedure TFormRules.ListViewSort1Filter(ListViewSort: TListViewSort);
181var
182 I: Integer;
183begin
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;
190end;
191
192procedure TFormRules.UpdateInterface;
193begin
194 AAdd.Enabled := Assigned(Rules);
195 AModify.Enabled := Assigned(Rules) and Assigned(ListView1.Selected);
196 ARemove.Enabled := Assigned(Rules) and Assigned(ListView1.Selected);
197end;
198
199procedure TFormRules.ReloadList;
200begin
201 ListViewSort1.Refresh;
202 ListView1.Refresh;
203end;
204
205end.
206
Note: See TracBrowser for help on using the repository browser.