| 1 | unit UFormLookupTables;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
|---|
| 9 | ActnList, Menus, UListViewSort, UGrammer;
|
|---|
| 10 |
|
|---|
| 11 | type
|
|---|
| 12 |
|
|---|
| 13 | { TFormLookupTables }
|
|---|
| 14 |
|
|---|
| 15 | TFormLookupTables = class(TForm)
|
|---|
| 16 | AAdd: TAction;
|
|---|
| 17 | ActionList1: TActionList;
|
|---|
| 18 | AModify: TAction;
|
|---|
| 19 | ARemove: TAction;
|
|---|
| 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 |
|
|---|
| 46 | public
|
|---|
| 47 | LookupTables: TLookupTables;
|
|---|
| 48 | procedure ReloadList;
|
|---|
| 49 | procedure UpdateInterface;
|
|---|
| 50 | end;
|
|---|
| 51 |
|
|---|
| 52 | var
|
|---|
| 53 | FormLookupTables: TFormLookupTables;
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | implementation
|
|---|
| 57 |
|
|---|
| 58 | uses
|
|---|
| 59 | UFormLookupTable, UCore;
|
|---|
| 60 |
|
|---|
| 61 | {$R *.lfm}
|
|---|
| 62 |
|
|---|
| 63 | { TFormLookupTables }
|
|---|
| 64 |
|
|---|
| 65 | procedure TFormLookupTables.AAddExecute(Sender: TObject);
|
|---|
| 66 | var
|
|---|
| 67 | NewItem: TLookupTable;
|
|---|
| 68 | begin
|
|---|
| 69 | NewItem := TLookupTable.Create;
|
|---|
| 70 | NewItem.Grammer := LookupTables.Grammer;
|
|---|
| 71 | FormLookupTable := TFormLookupTable.Create(nil);
|
|---|
| 72 | FormLookupTable.LoadData(NewItem);
|
|---|
| 73 | if FormLookupTable.ShowModal = mrOk then begin
|
|---|
| 74 | FormLookupTable.SaveData(NewItem);
|
|---|
| 75 | LookupTables.Add(NewItem);
|
|---|
| 76 | ReloadList;
|
|---|
| 77 | end else FreeAndNil(NewItem);
|
|---|
| 78 | FreeAndNil(FormLookupTable);
|
|---|
| 79 | end;
|
|---|
| 80 |
|
|---|
| 81 | procedure TFormLookupTables.AModifyExecute(Sender: TObject);
|
|---|
| 82 | var
|
|---|
| 83 | Item: TLookupTable;
|
|---|
| 84 | FormItem: TFormLookupTable;
|
|---|
| 85 | begin
|
|---|
| 86 | if Assigned(ListView1.Selected) then begin
|
|---|
| 87 | Item := TLookupTable(ListView1.Selected.Data);
|
|---|
| 88 | FormItem := TFormLookupTable.Create(nil);
|
|---|
| 89 | FormItem.LoadData(Item);
|
|---|
| 90 | if FormItem.ShowModal = mrOk then begin
|
|---|
| 91 | FormItem.SaveData(Item);
|
|---|
| 92 | ReloadList;
|
|---|
| 93 | end;
|
|---|
| 94 | FreeAndNil(FormItem);
|
|---|
| 95 | end;
|
|---|
| 96 | end;
|
|---|
| 97 |
|
|---|
| 98 | procedure TFormLookupTables.ARemoveExecute(Sender: TObject);
|
|---|
| 99 | var
|
|---|
| 100 | I: Integer;
|
|---|
| 101 | begin
|
|---|
| 102 | if MessageDlg('Remove item(s)', 'Do you want to remove selected item(s)?',
|
|---|
| 103 | mtConfirmation, mbYesNo, 0) = mrYes then
|
|---|
| 104 | for I := ListView1.Items.Count - 1 downto 0 do
|
|---|
| 105 | if ListView1.Items[I].Selected then begin
|
|---|
| 106 | LookupTables.Grammer.Modified := True;
|
|---|
| 107 | LookupTables.Remove(TLookupTable(ListView1.Items[I].Data));
|
|---|
| 108 | end;
|
|---|
| 109 | ReloadList;
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | procedure TFormLookupTables.FormClose(Sender: TObject;
|
|---|
| 113 | var CloseAction: TCloseAction);
|
|---|
| 114 | begin
|
|---|
| 115 | Core.PersistentForm1.Save(Self);
|
|---|
| 116 | end;
|
|---|
| 117 |
|
|---|
| 118 | procedure TFormLookupTables.FormCreate(Sender: TObject);
|
|---|
| 119 | begin
|
|---|
| 120 | Core.CoolTranslator1.TranslateComponentRecursive(Self);
|
|---|
| 121 | LookupTables := nil;
|
|---|
| 122 | end;
|
|---|
| 123 |
|
|---|
| 124 | procedure TFormLookupTables.FormShow(Sender: TObject);
|
|---|
| 125 | begin
|
|---|
| 126 | Core.PersistentForm1.Load(Self);
|
|---|
| 127 | ReloadList;
|
|---|
| 128 | UpdateInterface;
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | procedure TFormLookupTables.ListView1Data(Sender: TObject; Item: TListItem);
|
|---|
| 132 | begin
|
|---|
| 133 | if (Item.Index >= 0) and (Item.Index < ListViewSort1.List.Count) then
|
|---|
| 134 | with TLookupTable(ListViewSort1.List[Item.Index]) do begin
|
|---|
| 135 | Item.Caption := Name;
|
|---|
| 136 | Item.Data := ListViewSort1.List[Item.Index];
|
|---|
| 137 | end;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | procedure TFormLookupTables.ListView1DblClick(Sender: TObject);
|
|---|
| 141 | begin
|
|---|
| 142 | AModify.Execute;
|
|---|
| 143 | end;
|
|---|
| 144 |
|
|---|
| 145 | procedure TFormLookupTables.ListView1KeyPress(Sender: TObject; var Key: char);
|
|---|
| 146 | begin
|
|---|
| 147 | if Key = #13 then AModify.Execute;
|
|---|
| 148 | end;
|
|---|
| 149 |
|
|---|
| 150 | procedure TFormLookupTables.ListView1SelectItem(Sender: TObject;
|
|---|
| 151 | Item: TListItem; Selected: Boolean);
|
|---|
| 152 | begin
|
|---|
| 153 | UpdateInterface;
|
|---|
| 154 | end;
|
|---|
| 155 |
|
|---|
| 156 | function TFormLookupTables.ListViewSort1CompareItem(Item1, Item2: TObject
|
|---|
| 157 | ): Integer;
|
|---|
| 158 | begin
|
|---|
| 159 | Result := 0;
|
|---|
| 160 | if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
|
|---|
| 161 | with ListViewSort1 do
|
|---|
| 162 | case Column of
|
|---|
| 163 | 0: Result := CompareString(TLookupTable(Item1).Name, TLookupTable(Item2).Name);
|
|---|
| 164 | end;
|
|---|
| 165 | if ListViewSort1.Order = soDown then Result := -Result;
|
|---|
| 166 | end else Result := 0;
|
|---|
| 167 | end;
|
|---|
| 168 |
|
|---|
| 169 | procedure TFormLookupTables.ListViewSort1Filter(ListViewSort: TListViewSort);
|
|---|
| 170 | var
|
|---|
| 171 | I: Integer;
|
|---|
| 172 | begin
|
|---|
| 173 | if Assigned(LookupTables) then begin
|
|---|
| 174 | ListViewSort1.List.Count := LookupTables.Count;
|
|---|
| 175 | for I := 0 to LookupTables.Count - 1 do
|
|---|
| 176 | ListViewSort1.List[I] := LookupTables[I];
|
|---|
| 177 | end else ListViewSort1.List.Count := 0;
|
|---|
| 178 | end;
|
|---|
| 179 |
|
|---|
| 180 | procedure TFormLookupTables.ReloadList;
|
|---|
| 181 | begin
|
|---|
| 182 | ListViewSort1.Refresh;
|
|---|
| 183 | ListView1.Refresh;
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | procedure TFormLookupTables.UpdateInterface;
|
|---|
| 187 | begin
|
|---|
| 188 | AAdd.Enabled := Assigned(LookupTables);
|
|---|
| 189 | AModify.Enabled := Assigned(LookupTables) and Assigned(ListView1.Selected);
|
|---|
| 190 | ARemove.Enabled := Assigned(LookupTables) and Assigned(ListView1.Selected);
|
|---|
| 191 | end;
|
|---|
| 192 |
|
|---|
| 193 | end.
|
|---|
| 194 |
|
|---|