1 | unit UDataFile;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
38 | resourcestring
|
---|
39 | SDataFileName = 'Data file';
|
---|
40 | SAllFiles = 'All files';
|
---|
41 |
|
---|
42 | const
|
---|
43 | AnyFileExt = '.*';
|
---|
44 |
|
---|
45 |
|
---|
46 | implementation
|
---|
47 |
|
---|
48 | { TDataFile }
|
---|
49 |
|
---|
50 | procedure TDataFile.SetModified(AValue: Boolean);
|
---|
51 | begin
|
---|
52 | if FModified = AValue then Exit;
|
---|
53 | FModified := AValue;
|
---|
54 | DoOnModify;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TDataFile.DoOnModify;
|
---|
58 | begin
|
---|
59 | if Assigned(FOnModify) then FOnModify(Self);
|
---|
60 | end;
|
---|
61 |
|
---|
62 | function TDataFile.GetFileExt: string;
|
---|
63 | begin
|
---|
64 | Result := '.dat';
|
---|
65 | end;
|
---|
66 |
|
---|
67 | function TDataFile.GetFileName: string;
|
---|
68 | begin
|
---|
69 | Result := SDataFileName;
|
---|
70 | end;
|
---|
71 |
|
---|
72 | function TDataFile.GetFileFilter: string;
|
---|
73 | begin
|
---|
74 | Result := SAllFiles + '|*' + AnyFileExt;
|
---|
75 | end;
|
---|
76 |
|
---|
77 | procedure TDataFile.Assign(Source: TPersistent);
|
---|
78 | begin
|
---|
79 | inherited;
|
---|
80 | if Source is TDataFile then begin
|
---|
81 | FFileName := TDataFile(Source).FFileName;
|
---|
82 | FModified := TDataFile(Source).FModified;
|
---|
83 | end;
|
---|
84 | end;
|
---|
85 |
|
---|
86 | procedure TDataFile.LoadFromFile(FileName: string);
|
---|
87 | begin
|
---|
88 | FModified := False;
|
---|
89 | Self.FileName := FileName;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure TDataFile.SaveToFile(FileName: string);
|
---|
93 | begin
|
---|
94 | FModified := False;
|
---|
95 | Self.FileName := FileName;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | constructor TDataFile.Create(AOwner: TComponent);
|
---|
99 | begin
|
---|
100 | inherited;
|
---|
101 | FileName := GetFileName + GetFileExt;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | procedure TDataFile.SetFileName(AValue: string);
|
---|
105 | begin
|
---|
106 | if FFileName = AValue then Exit;
|
---|
107 | FFileName := AValue;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | end.
|
---|
111 |
|
---|