-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorSettings.pas
More file actions
484 lines (428 loc) · 12.9 KB
/
EditorSettings.pas
File metadata and controls
484 lines (428 loc) · 12.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{ TODO : Next: Speichern der Listbox in der Ini}
unit EditorSettings;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, System.UITypes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, IniFiles, ShellAPI,
System.IOUtils, System.RegularExpressions, ConverterConst;
type
TEditorProfile = class
public
Name : String;
Path : String;
Parameter: String;
isActive : boolean;
end;
type
TfrmEditorSettings = class(TForm)
pnlMain: TPanel;
pnlBot: TPanel;
lbxEditors: TListBox;
pnlButtons: TPanel;
btnCancel: TButton;
btnSave: TButton;
pnlEditors: TPanel;
pnlProperties: TPanel;
pnlPropertiesHeader: TPanel;
pnlEditorsHeader: TPanel;
lblEditors: TLabel;
lblProperties: TLabel;
splMain: TSplitter;
lblName: TLabel;
lblPath: TLabel;
edtName: TEdit;
edtPath: TEdit;
chkUseEditor: TCheckBox;
btnTestEditor: TButton;
btnEditorButtons: TPanel;
btnAdd: TButton;
btnDelete: TButton;
dlgOpen: TOpenDialog;
btnSelectPath: TButton;
edtParameter: TEdit;
lblParameter: TLabel;
procedure FormDestroy(Sender: TObject);
procedure lbxEditorsClick(Sender: TObject);
procedure btnAddClick(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
procedure btnSelectPathClick(Sender: TObject);
procedure btnTestEditorClick(Sender: TObject);
procedure edtNameExit(Sender: TObject);
procedure edtPathExit(Sender: TObject);
procedure chkUseEditorClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure edtParameterExit(Sender: TObject);
private
isRefreshing: boolean;
procedure CreateTestFile(var sFile: String);
procedure LoadEditorList;
procedure LoadEditorSettings;
procedure RefreshEditorList;
procedure ClearEditorConfig;
public
EditorsFile: TIniFile;
constructor Create(AOwner: TComponent; var AEditorsFile: TIniFile); reintroduce;
end;
var
frmEditorSettings: TfrmEditorSettings;
implementation
{$R *.dfm}
{ TfrmEditorSettings }
procedure TfrmEditorSettings.btnAddClick(Sender: TObject);
var
eProfile : TEditorProfile;
sBaseName : String;
sName : String;
ii : integer;
iCounter : integer;
nameExists: boolean;
begin
isRefreshing := True;
sBaseName := 'Editor';
sName := sBaseName;
iCounter := 1;
repeat
nameExists := False;
for ii := 0 to lbxEditors.Count - 1 do begin
if (SameText(TEditorProfile(lbxEditors.Items.Objects[ii]).Name, sName)) then begin
nameExists := True;
Break;
end;
end;
if (nameExists) then begin
inc(iCounter);
sName := sBaseName + IntToStr(iCounter);
end;
until not nameExists;
eProfile := TEditorProfile.Create;
with eProfile do begin
Name := sName;
Path := '';
Parameter:= '';
isActive := False;
end;
lbxEditors.Items.AddObject(eProfile.Name, eProfile);
lbxEditors.ItemIndex := lbxEditors.Count - 1;
LoadEditorSettings;
edtName.SetFocus;
isRefreshing := False;
end;
procedure TfrmEditorSettings.btnDeleteClick(Sender: TObject);
var
eProfile : TEditorProfile;
iIndex : Integer;
begin
iIndex := lbxEditors.ItemIndex;
if (iIndex < 0) then
Exit
;
//Profil löschen & freigeben
eProfile := TEditorProfile(lbxEditors.Items.Objects[iIndex]);
eProfile.Free;
lbxEditors.Items.Delete(iIndex);
//Nächstes Profil selektieren
if (lbxEditors.Count > 0) then begin
if (iIndex >= lbxEditors.Count) then
iIndex := lbxEditors.Count - 1
;
lbxEditors.ItemIndex := iIndex;
end
else
lbxEditors.ItemIndex := -1
;
LoadEditorSettings;
end;
procedure TfrmEditorSettings.btnSaveClick(Sender: TObject);
var
eProfile: TEditorProfile;
sSection: String;
ii : integer;
begin
if (not assigned(EditorsFile)) then
Exit
;
//Zuerst die aktuelle Editor-Settings löschen
ClearEditorConfig;
//Alle Editor-Profile neu eintragen
for ii := 0 to lbxEditors.Count - 1 do begin
eProfile := TEditorProfile(lbxEditors.Items.Objects[ii]);
sSection := EDITORS_SEC_EDITOR_X + eProfile.Name;
EditorsFile.WriteString(sSection, EDITORS_KEY_PATH, eProfile.Path);
EditorsFile.WriteString(sSection, EDITORS_KEY_PARAMETER, eProfile.Parameter);
if eProfile.isActive then
EditorsFile.WriteString(EDITORS_SEC_EDITOR, EDITORS_KEY_ACTIVE, sSection)
;
end;
end;
procedure TfrmEditorSettings.ClearEditorConfig;
var
slSections: TStringList;
sSection : String;
begin
slSections := TStringList.Create;
try
EditorsFile.ReadSections(slSections);
for sSection in slSections do begin
if sSection.StartsWith(EDITORS_SEC_EDITOR_X) then
EditorsFile.EraseSection(sSection)
;
end;
finally
slSections.Free;
end;
end;
procedure TfrmEditorSettings.btnSelectPathClick(Sender: TObject);
begin
if (dlgOpen.Execute) then
edtPath.Text := dlgOpen.FileName
;
end;
procedure TfrmEditorSettings.btnTestEditorClick(Sender: TObject);
var
sTempFile : String;
sParameter: String;
hRes : HINST;
begin
//Pfad prüfen
if (Trim(edtPath.Text) = '') then begin
MessageDlg('Pfad darf nicht leer sein!', mtError, [mbOK], 0);
edtPath.SetFocus;
Exit;
end;
//Falls Pfad nicht leer & kein Systembefehl ist, dann Pfad prüfen
if (ExtractFilePath(edtPath.Text) <> '') and (not FileExists(edtPath.Text)) then begin
MessageDlg('Datei existiert nicht:' + CRLF + edtPath.Text, mtError, [mbOK], 0);
Exit;
end;
//Temp. Test-Datei erzeugen
CreateTestFile(sTempFile);
//Parameter zusammensetzen
sParameter := Trim(edtParameter.Text) + ' "' + sTempFile + '"';
//Datei im Editor öffnen
hRes := ShellExecute(
Handle,
'open',
PChar(edtPath.Text),
PChar(sParameter),
nil,
SW_SHOWNORMAL
);
//Fehlerfall anzeigen
if (hRes <= 32) then
MessageDlg(
'Editor konnte nicht gestartet werden!' + CR + SysErrorMessage(GetLastError),
mtError,
[mbOK],
0
)
;
end;
procedure TfrmEditorSettings.CreateTestFile(var sFile: String);
var
sProgramPath : String;
begin
sProgramPath := TPath.Combine(GetEnvironmentVariable('APPDATA'), PROGRAMM_NAME);
if (not ForceDirectories(sProgramPath)) then
//Falls das nicht geht, dann im Verzeichnis der Exe
sProgramPath := ExtractFilePath(Application.ExeName)
;
sFile := TPath.Combine(sProgramPath, TEST_EDITOR_FILENAME);
TFile.WriteAllText(
sFile,
'--Testdatei für den Editor' + CRLF +
'BEGIN' + CRLF +
' DECLARE varDB_Name LONG VARCHAR;' + CRLF + CRLF +
' SELECT' + CRLF +
' DB_PROPERTY(''Name'')' + CRLF +
' INTO' + CRLF +
' varDB_Name' + CRLF +
' ;' + CRLF + CRLF +
' MESSAGE ''Aktuelle Datenbank: '' || varDB_Name TO CLIENT;' + CRLF + CRLF +
' IF (varDB_Name = ''Production'') THEN' + CRLF +
' EXECUTE IMMEDIATE ''DROP DATABASE '' || varDB_Name;' + CRLF +
' MESSAGE ''Ups ...'' TO CLIENT;' + CRLF +
' END IF;' + CRLF + CRLF +
' MESSAGE ''System signed by <CSX>'' TO CLIENT;' + CRLF +
'END;' + CRLF
);
end;
procedure TfrmEditorSettings.edtNameExit(Sender: TObject);
var
eProfile : TEditorProfile;
ii : integer;
sName : string;
begin
sName := Trim(TRegEx.Replace(edtName.Text, '[^a-zA-Z0-9+\-_!?%$&<>#*~()]', ''));
if (sName = '') then begin
MessageDlg('Bezeichnung darf nicht leer sein!', mtError, [mbOK], 0);
edtName.SetFocus;
Exit;
end;
//Doppelte Einträge prüfen
for ii := 0 to lbxEditors.Count - 1 do begin
if (ii = lbxEditors.ItemIndex) then
Continue
;
eProfile := TEditorProfile(lbxEditors.Items.Objects[ii]);
if SameText(eProfile.Name, sName) then begin
MessageDlg('Bezeichnung existiert bereits!', mtError, [mbOK], 0);
edtName.SetFocus;
Exit;
end;
end;
edtName.Text := sName;
eProfile := TEditorProfile(lbxEditors.Items.Objects[lbxEditors.ItemIndex]);
eProfile.Name := sName;
RefreshEditorList;
end;
procedure TfrmEditorSettings.edtParameterExit(Sender: TObject);
var
eProfile : TEditorProfile;
begin
eProfile := TEditorProfile(lbxEditors.Items.Objects[lbxEditors.ItemIndex]);
eProfile.Parameter := edtParameter.Text;
end;
procedure TfrmEditorSettings.edtPathExit(Sender: TObject);
var
eProfile : TEditorProfile;
begin
eProfile := TEditorProfile(lbxEditors.Items.Objects[lbxEditors.ItemIndex]);
eProfile.Path := edtPath.Text;
end;
procedure TfrmEditorSettings.chkUseEditorClick(Sender: TObject);
var
eProfile : TEditorProfile;
ii : integer;
begin
if (chkUseEditor.Checked) then begin
//Alle Profile deaktiveren
for ii := 0 to lbxEditors.Count - 1 do begin
eProfile := TEditorProfile(lbxEditors.Items.Objects[ii]);
eProfile.isActive := False;
end;
//Aktuelles aktivieren
eProfile := TEditorProfile(lbxEditors.Items.Objects[lbxEditors.ItemIndex]);
eProfile.isActive := chkUseEditor.Checked;
RefreshEditorList;
btnDelete.Enabled := not eProfile.isActive;
end;
if (not chkUseEditor.Checked and not isRefreshing) then begin
chkUseEditor.Checked := True;
Exit;
end;
end;
constructor TfrmEditorSettings.Create(AOwner: TComponent; var AEditorsFile: TIniFile);
begin
inherited Create(AOwner);
EditorsFile := AEditorsFile;
LoadEditorList;
end;
procedure TfrmEditorSettings.FormDestroy(Sender: TObject);
var
ii: integer;
begin
//Listbox-Objekte wieder freigeben
for ii := 0 to lbxEditors.Count -1 do begin
lbxEditors.Items.Objects[ii].Free;
end;
end;
procedure TfrmEditorSettings.lbxEditorsClick(Sender: TObject);
begin
LoadEditorSettings;
end;
procedure TfrmEditorSettings.LoadEditorList;
var
eProfile : TEditorProfile;
slSections : TStringlist;
sActiveEditor : String;
sSectionName : String;
sEditorName : String;
sDisplayName : String;
iLbxItemIndex : integer;
begin
isRefreshing := True;
lbxEditors.Clear;
iLbxItemIndex := -1;
slSections := TStringlist.Create;
try
//Den aktiven Editor rauslesen
sActiveEditor := EditorsFile.ReadString(EDITORS_SEC_EDITOR, EDITORS_KEY_ACTIVE, '');
//Profile aus den Editorsettings auslesen
EditorsFile.ReadSections(slSections);
for sSectionName in slSections do begin
//Nur Sections mit "Editor_..." berücksichtigen
if not sSectionName.StartsWith(EDITORS_SEC_EDITOR_X) then
Continue
;
sEditorName := Copy(sSectionName, Length(EDITORS_SEC_EDITOR_X) + 1, MaxInt);
sDisplayName := sEditorName;
//Profile auslesen & anzeigen
eProfile := TEditorProfile.Create;
with eProfile do begin
Name := sEditorName;
Path := EditorsFile.ReadString(sSectionName, EDITORS_KEY_PATH, '');
Parameter := EditorsFile.ReadString(sSectionName, EDITORS_KEY_PARAMETER, '');
isActive := SameText(sActiveEditor, sSectionName);
if (isActive) then
sDisplayName := SELECTED_EDITOR_SYMBOL + ' ' + sDisplayName
;
end;
lbxEditors.Items.AddObject(sDisplayName, eProfile);
inc(iLbxItemIndex);
//Aktive-Editor selektieren
if (eProfile.isActive) then begin
lbxEditors.ItemIndex := iLbxItemIndex;
LoadEditorSettings;
end;
end;
finally
slSections.Free;
isRefreshing := False;
end;
end;
procedure TfrmEditorSettings.RefreshEditorList;
var
eProfile : TEditorProfile;
ii : integer;
sDisplayName : String;
begin
isRefreshing := True;
lbxEditors.Items.BeginUpdate;
try
for ii := 0 to lbxEditors.Items.Count - 1 do begin
eProfile := TEditorProfile(lbxEditors.Items.Objects[ii]);
sDisplayName := eProfile.Name;
if (eProfile.isActive) then
sDisplayName := SELECTED_EDITOR_SYMBOL + ' ' + sDisplayName
;
lbxEditors.Items[ii] := sDisplayName;
end;
finally
lbxEditors.Items.EndUpdate;
isRefreshing := False;
end;
end;
procedure TfrmEditorSettings.LoadEditorSettings;
var
eProfile : TEditorProfile;
begin
isRefreshing := True;
if (lbxEditors.ItemIndex >= 0) then begin
eProfile := TEditorProfile(lbxEditors.Items.Objects[lbxEditors.ItemIndex]);
with eProfile do begin
edtName.Text := Name;
edtPath.Text := Path;
edtParameter.Text := Parameter;
chkUseEditor.Checked := isActive;
end;
end
else begin
edtName.Text := '';
edtPath.Text := '';
edtParameter.Text := '';
chkUseEditor.Checked := False;
end;
btnDelete.Enabled := not chkUseEditor.Checked;
isRefreshing := False;
end;
end.