Changeset 47 for trunk/IDE/UProject.pas
- Timestamp:
- Mar 4, 2012, 3:38:44 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/IDE/UProject.pas
r39 r47 1 1 unit UProject; 2 2 3 {$mode objfpc}{$H+} 4 {$MACRO ON} 3 {$mode Delphi}{$H+} 5 4 6 5 interface … … 14 13 15 14 type 16 17 { TProjectNode } 18 19 TProjectNode = class 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 20 41 private 21 42 FModified: Boolean; 22 43 procedure SetModified(const AValue: Boolean); 23 44 public 24 Parent: TProjectNode; 25 Name: string; 26 ProducedFileName: string; 27 procedure SaveToXMLNode(Node: TDOMNode); virtual; 28 procedure LoadFromXMLNode(Node: TDOMNode); virtual; 29 procedure Load; virtual; 30 procedure Save; virtual; 31 function GetDir(IncludeRoot: Boolean = False): string; virtual; 32 property Modified: Boolean read FModified write SetModified; 33 end; 34 35 36 // TListProjectNode = TGObjectList<Integer, TProjectNode> 37 TListProjectNode = class(TListObject); 38 39 { TProjectFile } 40 41 TProjectFile = class(TProjectNode) 45 FileName: string; 42 46 Source: TStringList; 47 Parent: TProject; 43 48 constructor Create; 44 49 destructor Destroy; override; 45 procedure SaveToXMLNode(Node: TDOMNode); override; 46 procedure LoadFromXMLNode(Node: TDOMNode); override; 47 procedure Load; override; 48 procedure Save; override; 49 end; 50 51 { TProjectGroup } 52 53 TProjectGroup = class(TProjectNode) 54 Items: TListProjectNode; 50 procedure SaveToXMLNode(Node: TDOMNode); 51 procedure LoadFromXMLNode(Node: TDOMNode); 52 procedure Load; 53 procedure Save; 54 property Modified: Boolean read FModified write SetModified; 55 end; 56 57 { TProjectFileList } 58 59 TProjectFileList = class(TListObject) 60 Parent: TProject; 55 61 procedure DumpFileList(Files: TListString); 62 procedure Load; 63 procedure Save; 64 procedure SaveToXMLNode(Node: TDOMNode); 65 procedure LoadFromXMLNode(Node: TDOMNode); 66 function SearchFile(FileName: string): TProjectFile; 67 procedure AddFile(FileName: string); 68 end; 69 70 TProject = class 71 private 72 FActive: Boolean; 73 FModified: Boolean; 74 function GetActive: Boolean; 75 procedure SetActive(AValue: Boolean); 76 procedure SetModified(AValue: Boolean); 77 public 78 FileName: string; 79 Files: TProjectFileList; 80 Packages: TProjectPackageList; 81 BuildConfigs: TProjectBuildConfigList; 82 procedure LoadFromFile(FileName: string); 83 procedure SaveToFile(FileName: string); 84 procedure Load; 85 procedure Save; 86 procedure Clear; 56 87 constructor Create; 57 88 destructor Destroy; override; 58 procedure Load; override;59 procedure Save; override;60 procedure SaveToXMLNode(Node: TDOMNode); override;61 procedure LoadFromXMLNode(Node: TDOMNode); override;62 function SearchFile(FileName: string): TProjectFile;63 end;64 65 TProject = class(TProjectGroup)66 private67 FActive: Boolean;68 function GetActive: Boolean;69 function GetFileName: string;70 procedure SetActive(AValue: Boolean);71 public72 RootDir: string;73 procedure LoadFromFile(FileName: string);74 procedure SaveToFile(FileName: string);75 function GetDir(IncludeRoot: Boolean = False): string; override;76 procedure Load; override;77 procedure Save; override;78 procedure SaveToXMLNode(Node: TDOMNode); override;79 procedure LoadFromXMLNode(Node: TDOMNode); override;80 procedure Clear;81 89 property Active: Boolean read GetActive write SetActive; 82 property FileName: string read GetFileName;90 property Modified: Boolean read FModified write SetModified; 83 91 end; 84 92 85 93 implementation 86 94 95 { TProjectPackageList } 96 97 procedure TProjectPackageList.Load; 98 begin 99 100 end; 101 102 procedure TProjectPackageList.Save; 103 begin 104 105 end; 106 107 procedure TProjectPackageList.SaveToXMLNode(Node: TDOMNode); 108 begin 109 110 end; 111 112 procedure TProjectPackageList.LoadFromXMLNode(Node: TDOMNode); 113 begin 114 115 end; 116 87 117 88 118 89 119 { TProjectGroup } 90 120 91 procedure TProjectGroup.DumpFileList(Files: TListString); 92 var 93 I: Integer; 94 begin 95 for I := 0 to Items.Count - 1 do 96 if TProjectNode(Items[I]) is TProjectGroup then 97 TProjectGroup(Items[I]).DumpFileList(Files) 98 else Files.Add(GetDir(True) + TProjectFile(Items[I]).Name); 99 end; 100 101 constructor TProjectGroup.Create; 102 begin 103 inherited; 104 Items := TListProjectNode.Create; 105 end; 106 107 destructor TProjectGroup.Destroy; 108 begin 109 Items.Free; 110 inherited Destroy; 111 end; 112 113 procedure TProjectGroup.Load; 114 var 115 I: Integer; 116 begin 117 inherited Load; 118 for I := 0 to Items.Count - 1 do 119 TProjectNode(Items[I]).Load; 120 end; 121 122 procedure TProjectGroup.Save; 123 var 124 I: Integer; 125 begin 126 for I := 0 to Items.Count - 1 do 127 TProjectNode(Items[I]).Save; 128 end; 129 130 procedure TProjectGroup.SaveToXMLNode(Node: TDOMNode); 131 var 132 I: Integer; 133 NewNode: TDOMNode; 134 begin 135 TDOMElement(Node).SetAttribute('Name', UTF8Decode(Name)); 136 inherited SaveToXMLNode(Node); 137 with Node do begin 138 NewNode := OwnerDocument.CreateElement('Folder'); 139 TDOMElement(NewNode).SetAttribute('Name', UTF8Decode(Name)); 121 procedure TProjectFileList.DumpFileList(Files: TListString); 122 var 123 I: Integer; 124 begin 125 Files.Clear; 126 for I := 0 to Count - 1 do 127 Files.Add(TProjectFile(Items[I]).FileName); 128 end; 129 130 procedure TProjectFileList.Load; 131 var 132 I: Integer; 133 begin 134 for I := 0 to Count - 1 do 135 TProjectFile(Items[I]).Load; 136 end; 137 138 procedure TProjectFileList.Save; 139 var 140 I: Integer; 141 begin 142 for I := 0 to Count - 1 do 143 TProjectFile(Items[I]).Save; 144 end; 145 146 procedure TProjectFileList.SaveToXMLNode(Node: TDOMNode); 147 var 148 I: Integer; 149 NewNode: TDOMNode; 150 begin 151 with Node do 152 for I := 0 to Count - 1 do begin 153 NewNode := OwnerDocument.CreateElement('File'); 154 TProjectFile(Items[I]).SaveToXMLNode(NewNode); 140 155 AppendChild(NewNode); 141 end;142 for I := 0 to Items.Count - 1 do begin143 TProjectNode(Items[I]).SaveToXMLNode(NewNode);144 156 end 145 157 end; 146 158 147 function TProjectGroup.SearchFile(FileName: string): TProjectFile; 148 var 149 NamePart: string; 150 I: Integer; 151 begin 152 if Pos('/', FileName) > 0 then 153 NamePart := Copy(FileName, 1, Pos('/', FileName)) 154 else NamePart := FileName; 159 procedure TProjectFileList.LoadFromXMLNode(Node: TDOMNode); 160 var 161 NewNode: TDomNode; 162 NewFile: TProjectFile; 163 begin 164 NewNode := Node.FirstChild; 165 while Assigned(NewNode) do begin 166 if NewNode.NodeName = 'File' then begin 167 NewFile := TProjectFile.Create; 168 NewFile.Parent := Parent; 169 NewFile.LoadFromXMLNode(NewNode); 170 Add(NewFile); 171 end; 172 NewNode := NewNode.NextSibling; 173 end; 174 end; 175 176 function TProjectFileList.SearchFile(FileName: string): TProjectFile; 177 var 178 I: Integer; 179 begin 155 180 I := 0; 156 while (I < Items.Count) and (ExtractFileNameOnly(TProjectNode(Items[I]).Name) <> NamePart) do Inc(I); 157 if I < Items.Count then begin 158 if TProjectNode(Items[I]) is TProjectFile then 159 Result := TProjectFile(Items[I]) 160 else begin 161 Delete(FileName, 1, Length(FileName) + 1); 162 TProjectGroup(Items[I]).SearchFile(FileName); 163 end; 164 end else Result := nil; 181 while (I < Count) and (TProjectFile(Items[I]).FileName <> FileName) do Inc(I); 182 if I < Count then Result := TProjectFile(Items[I]) 183 else Result := nil; 184 end; 185 186 procedure TProjectFileList.AddFile(FileName: string); 187 var 188 NewFile: TProjectFile; 189 begin 190 NewFile := TProjectFile(AddNew(TProjectFile.Create)); 191 NewFile.Parent := Parent; 192 NewFile.FileName := FileName; 165 193 end; 166 194 167 195 { TProjectFile } 196 197 procedure TProjectFile.SetModified(const AValue: Boolean); 198 begin 199 if FModified = AValue then Exit; 200 FModified := True; 201 if AValue then Parent.Modified := True; 202 end; 168 203 169 204 constructor TProjectFile.Create; … … 182 217 NewNode: TDOMNode; 183 218 begin 184 inherited SaveToXMLNode(Node);185 219 with Node do begin 186 NewNode := OwnerDocument.CreateElement('File ');187 TDOMElement(NewNode).SetAttribute('Name', UTF8Decode(Name));220 NewNode := OwnerDocument.CreateElement('FileName'); 221 NewNode.TextContent := UTF8Decode(FileName); 188 222 AppendChild(NewNode); 189 223 end; … … 192 226 193 227 procedure TProjectFile.LoadFromXMLNode(Node: TDOMNode); 194 begin 195 inherited LoadFromXMLNode(Node); 196 Load; 228 var 229 NewNode: TDOMNode; 230 begin 231 with Node do begin 232 NewNode := FindNode('FileName'); 233 if Assigned(NewNode) then 234 FileName := UTF8Encode(NewNode.TextContent); 235 end; 236 if FileExistsUTF8(FileName) then Load; 197 237 end; 198 238 199 239 procedure TProjectFile.Load; 200 240 begin 201 inherited Load; 202 Source.LoadFromFile(Parent.GetDir(True) + Name); 241 Source.LoadFromFile(ExtractFileDir(Parent.FileName) + DirectorySeparator + FileName); 203 242 end; 204 243 … … 206 245 begin 207 246 if Modified then 208 Source.SaveToFile(Parent.GetDir(True) + Name); 209 inherited Save; 210 end; 211 212 { TProjectNode } 213 214 procedure TProjectNode.SetModified(const AValue: Boolean); 215 begin 216 if FModified=AValue then exit; 217 FModified:=AValue; 218 if Assigned(Parent) then Parent.Modified := AValue; 219 end; 220 221 procedure TProjectNode.SaveToXMLNode(Node: TDOMNode); 222 begin 223 224 end; 225 226 procedure TProjectNode.LoadFromXMLNode(Node: TDOMNode); 227 begin 228 Name := UTF8Encode(string(TDOMElement(Node).GetAttribute('Name'))); 229 end; 230 231 procedure TProjectNode.Load; 232 begin 233 234 end; 235 236 procedure TProjectNode.Save; 237 begin 238 Modified := False; 239 end; 240 241 function TProjectNode.GetDir(IncludeRoot: Boolean = False): string; 242 begin 243 if Assigned(Parent) then Result := Parent.GetDir(IncludeRoot) + Name + DirectorySeparator 244 else Result := Name + DirectorySeparator; 247 Source.SaveToFile(FileName); 245 248 end; 246 249 … … 250 253 begin 251 254 Result := FActive; 252 end;253 254 function TProject.GetFileName: string;255 begin256 Result := GetDir(True) + Name + ProjectExt;257 255 end; 258 256 … … 264 262 end; 265 263 264 procedure TProject.SetModified(AValue: Boolean); 265 begin 266 if FModified = AValue then Exit; 267 FModified := AValue; 268 end; 269 266 270 procedure TProject.LoadFromFile(FileName: string); 267 271 var … … 274 278 try 275 279 Clear; 276 RootDir := ExtractFileDir(FileName) + '/'; 277 //NewNode := Doc.DocumentElement.FindNode('Files'); 278 Name := ExtractFileName(FileName); 279 Name := Copy(Name, 1, Length(Name) - Length(ExtractFileExt(FileName))); 280 280 Self.FileName := FileName; 281 281 ReadXMLFile(Doc, UTF8Decode(FileName)); 282 282 283 NewNode := Doc.DocumentElement.FindNode('F older');283 NewNode := Doc.DocumentElement.FindNode('Files'); 284 284 if Assigned(NewNode) then 285 LoadFromXMLNode(NewNode); 285 Files.LoadFromXMLNode(NewNode); 286 NewNode := Doc.DocumentElement.FindNode('Packages'); 287 if Assigned(NewNode) then 288 Packages.LoadFromXMLNode(NewNode); 286 289 finally 287 290 Doc.Free; 288 291 end; 289 292 FModified := False; 290 end;291 292 procedure TProjectGroup.LoadFromXMLNode(Node: TDOMNode);293 var294 Child: TDOMNode;295 ProjectNode: TProjectNode;296 NodeType: string;297 begin298 Name := UTF8Encode(TDOMElement(Node).GetAttribute('Name'));299 Child := Node.FirstChild;300 while Assigned(Child) do begin301 if Child.NodeName = 'Folder' then302 ProjectNode := TProjectGroup.Create303 else if Child.NodeName = 'File' then304 ProjectNode := TProjectFile.Create305 else ProjectNode := nil;306 if Assigned(ProjectNode) then begin307 ProjectNode.Parent := Self;308 ProjectNode.LoadFromXMLNode(Child);309 Items.Add(ProjectNode);310 end;311 312 Child := Child.NextSibling;313 end;314 293 end; 315 294 … … 328 307 AppendChild(RootNode); 329 308 with RootNode do begin 309 NewNode := CreateElement('Files'); 310 Files.SaveToXMLNode(NewNode); 311 AppendChild(NewNode); 312 313 NewNode := CreateElement('Packages'); 314 Packages.SaveToXMLNode(NewNode); 315 AppendChild(NewNode); 330 316 end; 331 SaveToXMLNode(RootNode);332 317 WriteXMLFile(Doc, UTF8Decode(FileName)); 333 318 finally … … 335 320 end; 336 321 FModified := False; 337 inherited Save;338 end;339 340 function TProject.GetDir(IncludeRoot: Boolean): string;341 begin342 if IncludeRoot then Result := RootDir else Result := '';343 322 end; 344 323 345 324 procedure TProject.Load; 346 325 begin 347 inherited Load; 326 Files.Load; 327 Packages.Load; 348 328 end; 349 329 350 330 procedure TProject.Save; 351 331 begin 352 inherited Save; 353 end; 354 355 procedure TProject.SaveToXMLNode(Node: TDOMNode); 356 begin 357 inherited SaveToXMLNode(Node); 358 end; 359 360 procedure TProject.LoadFromXMLNode(Node: TDOMNode); 361 begin 362 inherited LoadFromXMLNode(Node); 332 Files.Save; 333 Packages.Save; 363 334 end; 364 335 365 336 procedure TProject.Clear; 366 337 begin 367 Items.Clear; 338 Files.Clear; 339 Packages.Clear; 340 end; 341 342 constructor TProject.Create; 343 begin 344 Files := TProjectFileList.Create; 345 Files.Parent := Self; 346 Packages := TProjectPackageList.Create; 347 Packages.Parent := Self; 348 end; 349 350 destructor TProject.Destroy; 351 begin 352 Files.Free; 353 Packages.Free; 354 BuildConfigs.Free; 355 inherited Destroy; 368 356 end; 369 357
Note:
See TracChangeset
for help on using the changeset viewer.