1 | unit DataFile;
|
---|
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 | if Source is TDataFile then begin
|
---|
80 | FFileName := TDataFile(Source).FFileName;
|
---|
81 | FModified := TDataFile(Source).FModified;
|
---|
82 | end else inherited;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | procedure TDataFile.LoadFromFile(FileName: string);
|
---|
86 | begin
|
---|
87 | FModified := False;
|
---|
88 | Self.FileName := FileName;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | procedure TDataFile.SaveToFile(FileName: string);
|
---|
92 | begin
|
---|
93 | FModified := False;
|
---|
94 | Self.FileName := FileName;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | constructor TDataFile.Create(AOwner: TComponent);
|
---|
98 | begin
|
---|
99 | inherited;
|
---|
100 | FileName := GetFileName + GetFileExt;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | procedure TDataFile.SetFileName(AValue: string);
|
---|
104 | begin
|
---|
105 | if FFileName = AValue then Exit;
|
---|
106 | FFileName := AValue;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | end.
|
---|