source: trunk/StudioModules/LDModuleProject/UProject.pas

Last change on this file was 15, checked in by chronos, 12 years ago
  • Added: Registration of new file templates for modules.
  • Added: Program stores last opened file list in registry.
File size: 9.6 KB
Line 
1unit UProject;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, Dialogs, DOM, XMLWrite, XMLRead, FileUtil,
9 SpecializedList, USource;
10
11const
12 ProjectExt = '.tppr';
13
14type
15 TProject = class;
16
17 TProjectPackage = class
18 Name: string;
19 end;
20
21 { TProjectPackageList }
22
23 TProjectPackageList = class(TListObject)
24 Parent: TProject;
25 procedure Load;
26 procedure Save;
27 procedure SaveToXMLNode(Node: TDOMNode);
28 procedure LoadFromXMLNode(Node: TDOMNode);
29 end;
30
31 TProjectBuildConfig = class
32 Name: string;
33 end;
34
35 TProjectBuildConfigList = class(TListObject)
36 end;
37
38 { TProjectFile }
39
40 TProjectFile = class
41 private
42 FModified: Boolean;
43 function GetAbsoluteFileName: string;
44 procedure SetModified(const AValue: Boolean);
45 public
46 FileName: string;
47 Source: TStringList;
48 Parent: TProject;
49 constructor Create;
50 destructor Destroy; override;
51 procedure SaveToXMLNode(Node: TDOMNode);
52 procedure LoadFromXMLNode(Node: TDOMNode);
53 procedure Load;
54 procedure Save;
55 property Modified: Boolean read FModified write SetModified;
56 property AbsoluteFileName: string read GetAbsoluteFileName;
57 end;
58
59 { TProjectFileList }
60
61 TProjectFileList = class(TListObject)
62 Parent: TProject;
63 procedure DumpFileList(Files: TListString);
64 procedure LoadFromList(Files: TListString);
65 procedure Load;
66 procedure Save;
67 procedure SaveToXMLNode(Node: TDOMNode);
68 procedure LoadFromXMLNode(Node: TDOMNode);
69 function SearchFile(FileName: string): TProjectFile;
70 function AddFile(FileName: string): TProjectFile;
71 end;
72
73 TProject = class(TSource)
74 private
75 FModified: Boolean;
76 FOnChange: TNotifyEvent;
77 procedure SetModified(AValue: Boolean);
78 public
79 FileName: string;
80 Files: TProjectFileList;
81 Packages: TProjectPackageList;
82 BuildConfigs: TProjectBuildConfigList;
83 MainSource: TProjectFile;
84 procedure LoadFromFile(FileName: string);
85 procedure SaveToFile(FileName: string);
86 procedure Load;
87 procedure Save;
88 procedure Clear;
89 constructor Create;
90 destructor Destroy; override;
91 property Modified: Boolean read FModified write SetModified;
92 property OnChange: TNotifyEvent read FOnChange write FOnChange;
93 end;
94
95 { TProjectTemplate }
96
97 TProjectTemplate = class
98 Name: string;
99 Description: TStringList;
100 IsProject: Boolean;
101 procedure InitProject(var Project: TProject); virtual;
102 constructor Create; virtual;
103 destructor Destroy; override;
104 end;
105
106 { TProjectTemplateList }
107
108 TProjectTemplateList = class(TListObject)
109 procedure AddTemplate(Template: TProjectTemplate);
110 end;
111
112resourcestring
113 SNewProject = 'New project';
114
115implementation
116
117{ TProjectTemplateList }
118
119procedure TProjectTemplateList.AddTemplate(Template: TProjectTemplate);
120begin
121 Add(Template);
122end;
123
124{ TProjectTemplate }
125
126procedure TProjectTemplate.InitProject(var Project: TProject);
127begin
128 Project.FileName := SNewProject;
129end;
130
131constructor TProjectTemplate.Create;
132begin
133 Description := TStringList.Create;
134end;
135
136destructor TProjectTemplate.Destroy;
137begin
138 Description.Free;
139 inherited Destroy;
140end;
141
142{ TProjectPackageList }
143
144procedure TProjectPackageList.Load;
145begin
146
147end;
148
149procedure TProjectPackageList.Save;
150begin
151
152end;
153
154procedure TProjectPackageList.SaveToXMLNode(Node: TDOMNode);
155begin
156
157end;
158
159procedure TProjectPackageList.LoadFromXMLNode(Node: TDOMNode);
160begin
161
162end;
163
164{ TProjectGroup }
165
166procedure TProjectFileList.DumpFileList(Files: TListString);
167var
168 I: Integer;
169begin
170 Files.Clear;
171 for I := 0 to Count - 1 do
172 Files.Add(TProjectFile(Items[I]).AbsoluteFileName);
173end;
174
175procedure TProjectFileList.LoadFromList(Files: TListString);
176var
177 I: Integer;
178 NewFile: TProjectFile;
179 FileName: string;
180begin
181 Clear;
182 for I := 0 to Files.Count - 1 do begin
183 FileName := Files[I];
184 if Copy(FileName, 1, Length(ExtractFileDir(Parent.FileName))) =
185 ExtractFileDir(Parent.FileName) then
186 System.Delete(FileName, 1, Length(ExtractFileDir(Parent.FileName)) + 1);
187 NewFile := AddFile(FileName);
188 NewFile.Load;
189 end;
190end;
191
192procedure TProjectFileList.Load;
193var
194 I: Integer;
195begin
196 for I := 0 to Count - 1 do
197 TProjectFile(Items[I]).Load;
198end;
199
200procedure TProjectFileList.Save;
201var
202 I: Integer;
203begin
204 for I := 0 to Count - 1 do
205 TProjectFile(Items[I]).Save;
206end;
207
208procedure TProjectFileList.SaveToXMLNode(Node: TDOMNode);
209var
210 I: Integer;
211 NewNode: TDOMNode;
212begin
213 with Node do
214 for I := 0 to Count - 1 do begin
215 NewNode := OwnerDocument.CreateElement('File');
216 TProjectFile(Items[I]).SaveToXMLNode(NewNode);
217 AppendChild(NewNode);
218 end
219end;
220
221procedure TProjectFileList.LoadFromXMLNode(Node: TDOMNode);
222var
223 NewNode: TDomNode;
224 NewFile: TProjectFile;
225begin
226 NewNode := Node.FirstChild;
227 while Assigned(NewNode) do begin
228 if NewNode.NodeName = 'File' then begin
229 NewFile := TProjectFile.Create;
230 NewFile.Parent := Parent;
231 NewFile.LoadFromXMLNode(NewNode);
232 Add(NewFile);
233 end;
234 NewNode := NewNode.NextSibling;
235 end;
236end;
237
238function TProjectFileList.SearchFile(FileName: string): TProjectFile;
239var
240 I: Integer;
241 ParentDir: string;
242begin
243 ParentDir := ExtractFileDir(Parent.FileName);
244 if Copy(FileName, 1, Length(ParentDir)) = ParentDir then
245 System.Delete(FileName, 1, Length(ParentDir) + 1);
246 I := 0;
247 while (I < Count) and (TProjectFile(Items[I]).FileName <> FileName) do Inc(I);
248 if I < Count then Result := TProjectFile(Items[I])
249 else Result := nil;
250end;
251
252function TProjectFileList.AddFile(FileName: string): TProjectFile;
253begin
254 Result := TProjectFile(AddNew(TProjectFile.Create));
255 Result.Parent := Parent;
256 Result.FileName := FileName;
257end;
258
259{ TProjectFile }
260
261procedure TProjectFile.SetModified(const AValue: Boolean);
262begin
263 if FModified = AValue then Exit;
264 FModified := True;
265 if AValue then Parent.Modified := True;
266end;
267
268function TProjectFile.GetAbsoluteFileName: string;
269begin
270 Result := ExtractFileDir(Parent.FileName) + DirectorySeparator + FileName;
271end;
272
273constructor TProjectFile.Create;
274begin
275 Source := TStringList.Create;
276end;
277
278destructor TProjectFile.Destroy;
279begin
280 Source.Free;
281 inherited Destroy;
282end;
283
284procedure TProjectFile.SaveToXMLNode(Node: TDOMNode);
285var
286 NewNode: TDOMNode;
287begin
288 with Node do begin
289 NewNode := OwnerDocument.CreateElement('FileName');
290 NewNode.TextContent := UTF8Decode(FileName);
291 AppendChild(NewNode);
292 end;
293 Save;
294end;
295
296procedure TProjectFile.LoadFromXMLNode(Node: TDOMNode);
297var
298 NewNode: TDOMNode;
299begin
300 with Node do begin
301 NewNode := FindNode('FileName');
302 if Assigned(NewNode) then
303 FileName := UTF8Encode(NewNode.TextContent);
304 end;
305 if FileExistsUTF8(AbsoluteFileName) then Load;
306end;
307
308procedure TProjectFile.Load;
309begin
310 Source.LoadFromFile(AbsoluteFileName);
311end;
312
313procedure TProjectFile.Save;
314begin
315 Source.SaveToFile(AbsoluteFileName);
316end;
317
318{ TProject }
319
320procedure TProject.SetModified(AValue: Boolean);
321begin
322 if FModified = AValue then Exit;
323 FModified := AValue;
324 if Assigned(FOnChange) then FOnChange(Self);
325end;
326
327procedure TProject.LoadFromFile(FileName: string);
328var
329 Doc: TXMLDocument;
330 NewNode: TDOMNode;
331 NewSubNode: TDOMNode;
332 I: Integer;
333 Text: string;
334 MainSourceName: string;
335begin
336 try
337 Clear;
338 Self.FileName := FileName;
339 ReadXMLFile(Doc, UTF8Decode(FileName));
340
341 NewNode := Doc.DocumentElement.FindNode('Files');
342 if Assigned(NewNode) then
343 Files.LoadFromXMLNode(NewNode);
344
345 NewNode := Doc.DocumentElement.FindNode('MainSource');
346 if Assigned(NewNode) then begin
347 MainSourceName := UTF8Encode(NewNode.TextContent);
348 MainSource := Files.SearchFile(MainSourceName);
349 end;
350
351 NewNode := Doc.DocumentElement.FindNode('Packages');
352 if Assigned(NewNode) then
353 Packages.LoadFromXMLNode(NewNode);
354 finally
355 Doc.Free;
356 end;
357 FModified := False;
358end;
359
360procedure TProject.SaveToFile(FileName: string);
361var
362 Doc: TXMLDocument;
363 RootNode: TDOMNode;
364 NewNode: TDOMNode;
365 NewSubNode: TDOMNode;
366 I: Integer;
367 Text: string;
368begin
369 Doc := TXMLDocument.Create;
370 Self.FileName := FileName;
371 with Doc do try
372 RootNode := CreateElement('Project');
373 AppendChild(RootNode);
374 with RootNode do begin
375 NewNode := CreateElement('MainSource');
376 if Assigned(MainSource) then
377 NewNode.TextContent := UTF8Decode(MainSource.FileName);
378 AppendChild(NewNode);
379
380 NewNode := CreateElement('Files');
381 Files.SaveToXMLNode(NewNode);
382 AppendChild(NewNode);
383
384 NewNode := CreateElement('Packages');
385 Packages.SaveToXMLNode(NewNode);
386 AppendChild(NewNode);
387 end;
388 WriteXMLFile(Doc, UTF8Decode(FileName));
389 finally
390 Free;
391 end;
392 FModified := False;
393end;
394
395procedure TProject.Load;
396begin
397 Files.Load;
398 Packages.Load;
399end;
400
401procedure TProject.Save;
402var
403 I: Integer;
404begin
405 Files.Save;
406 Packages.Save;
407 for I := 0 to Files.Count - 1 do
408 TProjectFile(Files[I]).Modified := False;
409 Modified := False;
410end;
411
412procedure TProject.Clear;
413begin
414 Files.Clear;
415 Packages.Clear;
416 MainSource := nil;
417end;
418
419constructor TProject.Create;
420begin
421 Files := TProjectFileList.Create;
422 Files.Parent := Self;
423 Packages := TProjectPackageList.Create;
424 Packages.Parent := Self;
425end;
426
427destructor TProject.Destroy;
428begin
429 Files.Free;
430 Packages.Free;
431 BuildConfigs.Free;
432 inherited Destroy;
433end;
434
435end.
436
Note: See TracBrowser for help on using the repository browser.