unit addtemplate;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Engine;

type
  TAddTempDlg = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Edit4: TEdit;
    Bevel1: TBevel;
    Button1: TButton;
    Button2: TButton;
    Label5: TLabel;
    Edit5: TEdit;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure Button1Click(Sender: TObject);
  private
    Procedure ScanForRequired(Node: TEdRawNode; Var Visuals: String; Var Generals: String);
  public
    Function Execute(Node: TEdRawNode): boolean;
  end;

var
  AddTempDlg: TAddTempDlg;

implementation

{$R *.dfm}

Uses Main, templatesystem;

{ TAddTempDlg }

procedure TAddTempDlg.ScanForRequired(Node: TEdRawNode; var Visuals, Generals: String);
Var
 I: Integer;
begin
 Case Node.NodeType of
  ntVisual: begin
             Visuals := Visuals + '|' + GUIDToString((Node as TEdVisualNode).VisualGUID);
            end;
  ntSensor: begin
             Generals := Generals + '|' + GUIDToString((Node as TEdSensorNode).PlugInGUID);
            end;
 end;
 For I := 0 to Node.Count - 1 do begin
  ScanForRequired(Node[I], Visuals, Generals);
 end;
end;

function TAddTempDlg.Execute(Node: TEdRawNode): boolean;
Var
 Visuals, Generals: String;
 Temp: TTemplate;
 Sel: Integer;
begin
 Result := False;
 Edit5.Text := DateToStr(Now);
 Case Node.Nodetype of
  ntForm: Begin
           Edit4.text := 'Form';
          end;
  ntVisual: Begin
             Edit4.Text := 'Visual';
            end;
  ntSensor: Begin
             Edit4.Text := 'Sensor';
            end;
  else begin
   MessageDlg('You cannot add this node type as template.', mtInformation, [mbOK], 0);
   Exit;
  end;
 end;
 Result := Showmodal = mrOK;
 If Result then begin
  Visuals := '';
  Generals := '';
  ScanForRequired(Node, Visuals, Generals);
  Generals := Copy(Generals, 2, length(Generals));
  Visuals := Copy(Visuals, 2, Length(Visuals));
  Sel := oeMain.TemplateList.Add;
  With Temp do begin
   Information.Creator := Edit2.Text;
   Information.Email := Edit3.Text;
   Information.ScreenName := Edit1.Text;
   Information.CMLType := Edit4.Text;
   Information.CreationDate := Edit5.Text;
   Required.Visuals := Visuals;
   Required.Generals := Generals;
   Filename := 'template_' + Information.CMLType + '_' + Information.ScreenName + '.xml';
   ActualCML := Node.WriteXML('    ', RadioButton1.Checked);
  end;
  oeMain.TemplateList[Sel] := Temp;
 end;
end;

procedure TAddTempDlg.Button1Click(Sender: TObject);
begin
 If edit1.text = '' then begin
  MessageDlg('You must give the template a name', mtInformation, [mbOK], 0);
  edit1.setfocus;
  exit;
 end;
 ModalResult := mrOk;
end;

end.
