-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameConfigEditor.pas
More file actions
273 lines (234 loc) · 7.61 KB
/
FrameConfigEditor.pas
File metadata and controls
273 lines (234 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
unit FrameConfigEditor;
interface
uses
System.SysUtils, System.Classes, System.JSON, JSONHelpers, Vcl.Controls, Vcl.Forms,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.Graphics, Vcl.Dialogs,
ConfigIntf, ConfigFrameBase, System.UITypes, System.Generics.Collections, UtilsTypes;
type
TframeConfigEditor = class(TFrame, IConfigEditor)
pnlMain: TPanel;
tabControl: TTabControl;
pnlEditor: TPanel;
ButtonPanel: TPanel;
btnSave: TButton;
btnCancel: TButton;
procedure btnSaveClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure tabControlChange(Sender: TObject);
private
{ Private declarations }
FINIConfig: IINIConfig;
FJSONConfig: IJSONConfig;
FCurrentSection: string;
FCurrentKey: string;
FCurrentEditor: TFrame;
FModified: Boolean;
function CreateEditorForType(EditorType: TConfigType): TFrame;
procedure UpdateButtonState;
procedure HandleEditorModified(Sender: TObject);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// IConfigEditor�ӿ�ʵ��
procedure SetINIConfig(const Value: IINIConfig);
procedure SetJSONConfig(const Value: IJSONConfig);
procedure EditValue(const Section, Key: string);
function GetModified: Boolean;
procedure SetModified(const Value: Boolean);
property Modified: Boolean read GetModified write SetModified;
end;
implementation
uses
FramesComplexEditor;
{$R *.dfm}
{ TframeConfigEditor }
constructor TframeConfigEditor.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FModified := False;
end;
destructor TframeConfigEditor.Destroy;
begin
if Assigned(FCurrentEditor) then
FCurrentEditor.Free;
inherited;
end;
procedure TframeConfigEditor.btnCancelClick(Sender: TObject);
begin
if Assigned(FCurrentEditor) then
begin
FCurrentEditor.Free;
FCurrentEditor := nil;
end;
FModified := False;
UpdateButtonState;
end;
procedure TframeConfigEditor.btnSaveClick(Sender: TObject);
var
BaseFrame: TBaseConfigFrame;
ConfigType: TConfigType;
JsonValue: TJSONValue;
begin
if Assigned(FCurrentEditor) and
(FCurrentEditor is TBaseConfigFrame) and
Assigned(FINIConfig) and
Assigned(FJSONConfig) then
begin
BaseFrame := TBaseConfigFrame(FCurrentEditor);
// ���浽JSON����
BaseFrame.SaveToJSON;
// ��ȡ��������
if BaseFrame.JSONObject.FindValue('_type') <> nil then
begin
ConfigType := StringToConfigType(BaseFrame.JSONObject.GetValue('_type').Value);
// ���浽INI����
case ConfigType of
ctPlain:
if BaseFrame.JSONObject.FindValue('value') <> nil then
begin
JsonValue := BaseFrame.JSONObject.GetValue('value');
if JsonValue is TJSONString then
FINIConfig.WriteString(FCurrentSection, FCurrentKey, TJSONString(JsonValue).Value);
end;
ctInteger:
if BaseFrame.JSONObject.FindValue('value') <> nil then
begin
JsonValue := BaseFrame.JSONObject.GetValue('value');
if JsonValue is TJSONNumber then
FINIConfig.WriteInteger(FCurrentSection, FCurrentKey, TJSONNumber(JsonValue).AsInt);
end;
ctFloat:
if BaseFrame.JSONObject.FindValue('value') <> nil then
begin
JsonValue := BaseFrame.JSONObject.GetValue('value');
if JsonValue is TJSONNumber then
FINIConfig.WriteFloat(FCurrentSection, FCurrentKey, TJSONNumber(JsonValue).AsDouble);
end;
ctBoolean:
if BaseFrame.JSONObject.FindValue('value') <> nil then
begin
JsonValue := BaseFrame.JSONObject.GetValue('value');
if JsonValue is TJSONBool then
FINIConfig.WriteBool(FCurrentSection, FCurrentKey, TJSONBool(JsonValue).AsBoolean);
end;
ctColor:
if BaseFrame.JSONObject.FindValue('value') <> nil then
begin
JsonValue := BaseFrame.JSONObject.GetValue('value');
if JsonValue is TJSONString then
FINIConfig.WriteString(FCurrentSection, FCurrentKey, TJSONString(JsonValue).Value);
end;
ctFont, ctAIAPI, ctDatabase, ctList, ctObject, ctArray:
begin
// �������ͣ�ֱ��ʹ�ñ����JSON�ַ���
FINIConfig.WriteString(FCurrentSection, FCurrentKey,
BaseFrame.JSONObject.ToString);
end;
end;
end;
FModified := False;
UpdateButtonState;
end;
end;
function TframeConfigEditor.CreateEditorForType(EditorType: TConfigType): TFrame;
var
BaseFrame: TBaseConfigFrame;
begin
Result := CreateEditorFrame(Self, EditorType);
if (Result is TBaseConfigFrame) and Assigned(FJSONConfig) then
begin
BaseFrame := TBaseConfigFrame(Result);
BaseFrame.JSONObject := FJSONConfig.GetJSONObject(FCurrentSection, FCurrentKey);
BaseFrame.OnModified := HandleEditorModified;
// ���� LoadFromJSON ����
// ����LoadFromJSON��protected�����Բ���ֱ�ӵ���
// ���ǿ���ͨ������JSONObject��Ӵ���LoadFromJSON
BaseFrame.JSONObject := BaseFrame.JSONObject;
end;
end;
procedure TframeConfigEditor.HandleEditorModified(Sender: TObject);
begin
FModified := True;
UpdateButtonState;
end;
procedure TframeConfigEditor.EditValue(const Section, Key: string);
var
EditorType: TConfigType;
begin
FCurrentSection := Section;
FCurrentKey := Key;
// �����ǰ�༭��
if Assigned(FCurrentEditor) then
begin
FCurrentEditor.Free;
FCurrentEditor := nil;
end;
if not Assigned(FJSONConfig) then
begin
ShowMessage('JSON���ýӿ�δ��ʼ��');
Exit;
end;
// ��ȡ��������
EditorType := FJSONConfig.GetConfigType(Section, Key);
// ������Ӧ�ı༭��
FCurrentEditor := CreateEditorForType(EditorType);
if Assigned(FCurrentEditor) then
begin
FCurrentEditor.Parent := pnlEditor;
FCurrentEditor.Align := alClient;
end;
FModified := False;
UpdateButtonState;
end;
function TframeConfigEditor.GetModified: Boolean;
begin
Result := FModified;
end;
procedure TframeConfigEditor.SetINIConfig(const Value: IINIConfig);
begin
FINIConfig := Value;
end;
procedure TframeConfigEditor.SetJSONConfig(const Value: IJSONConfig);
begin
FJSONConfig := Value;
end;
procedure TframeConfigEditor.SetModified(const Value: Boolean);
begin
FModified := Value;
UpdateButtonState;
end;
procedure TframeConfigEditor.tabControlChange(Sender: TObject);
begin
// ���ݵ�ǰѡ��ı�ǩҳ���±༭��
case tabControl.TabIndex of
0: begin
// �༭����ͼ
if Assigned(FCurrentEditor) then
FCurrentEditor.Visible := True;
end;
1: begin
// ԭʼ������ͼ (JSON)
if Assigned(FCurrentEditor) then
FCurrentEditor.Visible := False;
// ��������һ���ı��༭����ʾJSON����
end;
end;
end;
procedure TframeConfigEditor.UpdateButtonState;
begin
// ������״̬���°�ť״̬
btnSave.Enabled := FModified and Assigned(FCurrentEditor);
btnCancel.Enabled := Assigned(FCurrentEditor);
end;
{$IFDEF DESIGNTIME}
procedure Register;
begin
RegisterComponents('Custom', [TframeConfigEditor]);
end;
{$ENDIF}
{$IFDEF DESIGNTIME}
initialization
// ��Ҫ������ʱ����Register
{$ENDIF}
end.