1 | unit FormFields;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
|
---|
7 | ActnList, ComCtrls, Menus, DbEngine, FormEx;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
48 | implementation
|
---|
49 |
|
---|
50 | uses
|
---|
51 | FormField;
|
---|
52 |
|
---|
53 | {$R *.lfm}
|
---|
54 |
|
---|
55 | resourcestring
|
---|
56 | SNewField = 'New field';
|
---|
57 | SFieldsNotSet = 'Fields not set';
|
---|
58 | STableFields = 'Table fields - %s';
|
---|
59 |
|
---|
60 | { TFormFields }
|
---|
61 |
|
---|
62 | procedure TFormFields.FormShow(Sender: TObject);
|
---|
63 | var
|
---|
64 | I: Integer;
|
---|
65 | begin
|
---|
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;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | procedure TFormFields.AAddExecute(Sender: TObject);
|
---|
76 | var
|
---|
77 | NewField: TField;
|
---|
78 | begin
|
---|
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;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | procedure TFormFields.ACancelExecute(Sender: TObject);
|
---|
99 | begin
|
---|
100 | Close;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TFormFields.AModifyExecute(Sender: TObject);
|
---|
104 | begin
|
---|
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;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | procedure TFormFields.AOkExecute(Sender: TObject);
|
---|
123 | begin
|
---|
124 | ModalResult := mrOk;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TFormFields.ARemoveExecute(Sender: TObject);
|
---|
128 | begin
|
---|
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;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | procedure TFormFields.ListView1Data(Sender: TObject; Item: TListItem);
|
---|
138 | begin
|
---|
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;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TFormFields.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
---|
151 | Selected: Boolean);
|
---|
152 | begin
|
---|
153 | UpdateInterface;
|
---|
154 | end;
|
---|
155 |
|
---|
156 | procedure TFormFields.UpdateInterface;
|
---|
157 | begin
|
---|
158 | AModify.Enabled := Assigned(ListView1.Selected);
|
---|
159 | ARemove.Enabled := Assigned(ListView1.Selected);
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TFormFields.ReloadList;
|
---|
163 | begin
|
---|
164 | Fields.Load;
|
---|
165 |
|
---|
166 | ListView1.Items.Count := Fields.Count;
|
---|
167 | ListView1.Repaint;
|
---|
168 | end;
|
---|
169 |
|
---|
170 | end.
|
---|
171 |
|
---|