source: trunk/Packages/Common/DataFile.pas

Last change on this file was 85, checked in by chronos, 7 months ago
  • Modified: Updated Common package.
File size: 2.3 KB
Line 
1unit DataFile;
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 if Source is TDataFile then begin
80 FFileName := TDataFile(Source).FFileName;
81 FModified := TDataFile(Source).FModified;
82 end else inherited;
83end;
84
85procedure TDataFile.LoadFromFile(FileName: string);
86begin
87 FModified := False;
88 Self.FileName := FileName;
89end;
90
91procedure TDataFile.SaveToFile(FileName: string);
92begin
93 FModified := False;
94 Self.FileName := FileName;
95end;
96
97constructor TDataFile.Create(AOwner: TComponent);
98begin
99 inherited;
100 FileName := GetFileName + GetFileExt;
101end;
102
103procedure TDataFile.SetFileName(AValue: string);
104begin
105 if FFileName = AValue then Exit;
106 FFileName := AValue;
107end;
108
109end.
Note: See TracBrowser for help on using the repository browser.