source: trunk/Forms/FormFields.pas

Last change on this file was 32, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from units names.
  • Modified: Use TFormEx for all forms.
File size: 4.3 KB
Line 
1unit FormFields;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
7 ActnList, ComCtrls, Menus, DbEngine, FormEx;
8
9type
10
11 { TFormFields }
12
13 TFormFields = class(TFormEx)
14 AAdd: TAction;
15 AOk: TAction;
16 ACancel: TAction;
17 ActionList1: TActionList;
18 AModify: TAction;
19 ARemove: TAction;
20 ListView1: TListView;
21 MenuItem1: TMenuItem;
22 MenuItem2: TMenuItem;
23 MenuItem3: TMenuItem;
24 PopupMenu1: TPopupMenu;
25 ToolBar1: TToolBar;
26 ToolButton1: TToolButton;
27 ToolButton2: TToolButton;
28 ToolButton3: TToolButton;
29 ToolButton4: TToolButton;
30 ToolButton5: TToolButton;
31 procedure AAddExecute(Sender: TObject);
32 procedure ACancelExecute(Sender: TObject);
33 procedure AModifyExecute(Sender: TObject);
34 procedure AOkExecute(Sender: TObject);
35 procedure ARemoveExecute(Sender: TObject);
36 procedure FormShow(Sender: TObject);
37 procedure ListView1Data(Sender: TObject; Item: TListItem);
38 procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
39 Selected: Boolean);
40 public
41 Fields: TFields;
42 TableName: string;
43 procedure UpdateInterface;
44 procedure ReloadList;
45 end;
46
47
48implementation
49
50uses
51 FormField;
52
53{$R *.lfm}
54
55resourcestring
56 SNewField = 'New field';
57 SFieldsNotSet = 'Fields not set';
58 STableFields = 'Table fields - %s';
59
60{ TFormFields }
61
62procedure TFormFields.FormShow(Sender: TObject);
63var
64 I: Integer;
65begin
66 for I := 0 to ToolBar1.ButtonCount - 1 do
67 ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
68 if not Assigned(Fields) then
69 raise Exception.Create(SFieldsNotSet);
70 Caption := Format(STableFields, [TableName]);
71 UpdateInterface;
72 ReloadList;
73end;
74
75procedure TFormFields.AAddExecute(Sender: TObject);
76var
77 NewField: TField;
78begin
79 NewField := TField.Create;
80 NewField.Table := Fields.Table;
81 NewField.Name := SNewField;
82 if Fields.Table.DbClient.ConnectProfile.ClientType.DataTypes.Count > 0 then
83 NewField.DataType := TDataType(Fields.Table.DbClient.ConnectProfile.ClientType.DataTypes[0]);
84 with TFormField.Create(nil) do
85 try
86 Load(NewField);
87 if ShowModal = mrOk then begin
88 Save(NewField);
89 Fields.Table.DbClient.Query('INSERT INTO ModelField ( Name , Caption , Model , DataType ) VALUES ( ' +
90 NewField.Name + ' , ' + NewField.TextBefore + ' , ' + TableName + ' , ' + NewField.DataType.Name + ' )');
91 ReloadList;
92 end else NewField.Free;
93 finally
94 Free;
95 end;
96end;
97
98procedure TFormFields.ACancelExecute(Sender: TObject);
99begin
100 Close;
101end;
102
103procedure TFormFields.AModifyExecute(Sender: TObject);
104begin
105 if Assigned(ListView1.Selected) then begin
106 with TFormField.Create(nil) do
107 try
108 Load(TField(ListView1.Selected.Data));
109 if ShowModal = mrOk then begin
110 Save(TField(ListView1.Selected.Data));
111 Fields.Table.DbClient.Query('UPDATE ModelField SET Caption = ' + TField(ListView1.Selected.Data).TextBefore +
112 ' , DataType = ' + TField(ListView1.Selected.Data).DataType.Name +
113 ' WHERE Model = ' + Fields.Table.Name +' , Name = ' + TField(ListView1.Selected.Data).Name);
114 ReloadList;
115 end;
116 finally
117 Free;
118 end;
119 end;
120end;
121
122procedure TFormFields.AOkExecute(Sender: TObject);
123begin
124 ModalResult := mrOk;
125end;
126
127procedure TFormFields.ARemoveExecute(Sender: TObject);
128begin
129 if Assigned(ListView1.Selected) then begin
130 Fields.Table.DbClient.Query('DELETE FROM ModelField WHERE Model = ' + Fields.Table.Name +
131 ' , Name = ' + TField(ListView1.Selected.Data).Name);
132 ReloadList;
133 UpdateInterface;
134 end;
135end;
136
137procedure TFormFields.ListView1Data(Sender: TObject; Item: TListItem);
138begin
139 if (Item.Index >= 0) and (Item.Index < Fields.Count) then
140 with TField(Fields[Item.Index]) do begin
141 Item.Caption := Name;
142 Item.SubItems.Add(DataType.Title);
143 Item.SubItems.Add(BoolToStr(Required));
144 Item.SubItems.Add(TextBefore);
145 Item.SubItems.Add(TextAfter);
146 Item.Data := Fields[Item.Index];
147 end;
148end;
149
150procedure TFormFields.ListView1SelectItem(Sender: TObject; Item: TListItem;
151 Selected: Boolean);
152begin
153 UpdateInterface;
154end;
155
156procedure TFormFields.UpdateInterface;
157begin
158 AModify.Enabled := Assigned(ListView1.Selected);
159 ARemove.Enabled := Assigned(ListView1.Selected);
160end;
161
162procedure TFormFields.ReloadList;
163begin
164 Fields.Load;
165
166 ListView1.Items.Count := Fields.Count;
167 ListView1.Repaint;
168end;
169
170end.
171
Note: See TracBrowser for help on using the repository browser.