source: trunk/Packages/Common/UDataFile.pas

Last change on this file was 10, checked in by chronos, 2 years ago
  • Modified: General UTest unit moved into Common package.
File size: 2.3 KB
Line 
1unit UDataFile;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections;
7
8type
9 { TDataFile }
10
11 TDataFile = class(TComponent)
12 private
13 FFileName: string;
14 FModified: Boolean;
15 FOnModify: TNotifyEvent;
16 procedure SetFileName(AValue: string);
17 procedure SetModified(AValue: Boolean);
18 procedure DoOnModify;
19 public
20 function GetFileExt: string; virtual;
21 function GetFileName: string; virtual;
22 function GetFileFilter: string; virtual;
23 procedure Assign(Source: TPersistent); override;
24 procedure LoadFromFile(FileName: string); virtual;
25 procedure SaveToFile(FileName: string); virtual;
26 constructor Create(AOwner: TComponent); override;
27 property FileName: string read FFileName write SetFileName;
28 property Modified: Boolean read FModified write SetModified;
29 published
30 property OnModify: TNotifyEvent read FOnModify write FOnModify;
31 end;
32
33 TDataFileClass = class of TDataFile;
34
35 TDataFiles = class(TObjectList<TDataFile>)
36 end;
37
38resourcestring
39 SDataFileName = 'Data file';
40 SAllFiles = 'All files';
41
42const
43 AnyFileExt = '.*';
44
45
46implementation
47
48{ TDataFile }
49
50procedure TDataFile.SetModified(AValue: Boolean);
51begin
52 if FModified = AValue then Exit;
53 FModified := AValue;
54 DoOnModify;
55end;
56
57procedure TDataFile.DoOnModify;
58begin
59 if Assigned(FOnModify) then FOnModify(Self);
60end;
61
62function TDataFile.GetFileExt: string;
63begin
64 Result := '.dat';
65end;
66
67function TDataFile.GetFileName: string;
68begin
69 Result := SDataFileName;
70end;
71
72function TDataFile.GetFileFilter: string;
73begin
74 Result := SAllFiles + '|*' + AnyFileExt;
75end;
76
77procedure TDataFile.Assign(Source: TPersistent);
78begin
79 inherited;
80 if Source is TDataFile then begin
81 FFileName := TDataFile(Source).FFileName;
82 FModified := TDataFile(Source).FModified;
83 end;
84end;
85
86procedure TDataFile.LoadFromFile(FileName: string);
87begin
88 FModified := False;
89 Self.FileName := FileName;
90end;
91
92procedure TDataFile.SaveToFile(FileName: string);
93begin
94 FModified := False;
95 Self.FileName := FileName;
96end;
97
98constructor TDataFile.Create(AOwner: TComponent);
99begin
100 inherited;
101 FileName := GetFileName + GetFileExt;
102end;
103
104procedure TDataFile.SetFileName(AValue: string);
105begin
106 if FFileName = AValue then Exit;
107 FFileName := AValue;
108end;
109
110end.
111
Note: See TracBrowser for help on using the repository browser.