{*******************************************************}
{                                                       }
{       ZaphodsMap Demo                                 }
{                                                       }
{       Quick demo showing how to use ZaphodsMap.pas    }
{       to retrieve values from configuration files.    }
{                                                       }
{       ZaphodsMap is licensed under a Creative Commons }
{       Attribution 2.5 License.                        }
{       See www.ZaphodsMap.com                          }
{                                                       }
{*******************************************************}
unit ZMDemo_fm1;


interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    LabelAttribute: TLabel;
    LabelKey2File: TLabel;
    LabelKey2Folder: TLabel;
    LabelKey2Value: TLabel;
    BtnDisplayData: TBitBtn;
    Label3: TLabel;
    LabelNow: TLabel;
    StatusBar1: TStatusBar;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    LabelZMEnvVar: TLabel;
    LabelDemoBranch: TLabel;
    procedure BtnDisplayDataClick(Sender: TObject);
    procedure LabelZMEnvVarMouseEnter(Sender: TObject);
    procedure Label1MouseLeave(Sender: TObject);
    procedure LabelKey2ValueMouseEnter(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure LabelKey2FileMouseEnter(Sender: TObject);
    procedure LabelKey2FolderMouseEnter(Sender: TObject);
    procedure LabelAttributeMouseEnter(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  ZaphodsMap, uDemoReadConfig;

procedure TForm1.FormCreate(Sender: TObject);
var
  S: string;
begin

  {always display the env var because it is required for everything}
  LabelZMEnvVar.Caption := ReadZMEnvVar(S);
  Label1.Font.Color := clRed;
  Label1.Caption := S;

  LabelDemoBranch.Caption := cDemoBranch;

  {reset remaining labels to empty starting point}
  LabelKey2Value.Caption := '-';
  LabelKey2Folder.Caption := '-';
  LabelKey2File.Caption := '-';
  LabelAttribute.Caption := '-';
  LabelNow.Caption := '-';
end;

procedure TForm1.BtnDisplayDataClick(Sender: TObject);
var
  Warning: string;
  ALabelCaption: string;
begin

  TBitBtn(Sender).Enabled := False;

  LabelZMEnvVar.Caption := ReadZMEnvVar(ALabelCaption);
  Label1.Caption := ALabelCaption;

  LabelKey2Folder.Caption := '';
  LabelKey2File.Caption := '';
  LabelAttribute.Caption := '';
  LabelNow.Caption := FormatDateTime('DDDD dd-MMM @ hh:nn:ss', Now);

  {load an arbitrary keyed value}
  LabelKey2Value.Caption := ReadConfig1(ALabelCaption, Warning);
  Label5.Caption := ALabelCaption;
  if Warning <> '' then
    ShowMessage(Warning);

  {load a folder name}
  LabelKey2Folder.Caption := ReadConfig2(ALabelCaption, Warning);
  Label8.Caption := ALabelCaption;
  if Warning <> '' then
    ShowMessage(Warning);

  {load an arbitrary filename}
  LabelKey2File.Caption := ReadConfig3(ALabelCaption, Warning);
  Label9.Caption := ALabelCaption;
  Application.ProcessMessages;
  if Warning <> '' then
    ShowMessage(Warning);

  {go into a keyed file (XML config file), follow an xpath, load an attribute}
  LabelAttribute.Caption := ReadConfig4(ALabelCaption, Warning);
  Label10.Caption := ALabelCaption;
  Label10.WordWrap := True;
  Application.ProcessMessages;
  if Warning <> '' then
    ShowMessage(Warning);

  TBitBtn(Sender).Enabled := True;
end;

procedure TForm1.LabelZMEnvVarMouseEnter(Sender: TObject);
begin
  {hint on status bar}
  StatusBar1.SimpleText := 'GetEnvironmentVariable(''ZaphodsMap'')';
end;

procedure TForm1.Label1MouseLeave(Sender: TObject);
begin
  {clear status bar}
  StatusBar1.SimpleText := '';
end;

procedure TForm1.LabelKey2ValueMouseEnter(Sender: TObject);
begin
  {hint on status bar}
  StatusBar1.SimpleText := 'ZaphodKeyedValue(''' + cDemoBranch + ''', ' +
    '''AProductArea'',' +
    '''FtpHost'', cxOptional, usrNone, '''', KeyedDetail)';
end;

procedure TForm1.LabelKey2FileMouseEnter(Sender: TObject);
begin
  {hint on status bar}
  StatusBar1.SimpleText := 'ZaphodKeyedFilename(''' + cDemoBranch + ''', ' +
    '''AProductArea'',' +
    '''Security'', cxOptional, usrNone, '''', KeyedDetail)';
end;

procedure TForm1.LabelKey2FolderMouseEnter(Sender: TObject);
begin
  {hint on status bar}
  StatusBar1.SimpleText := 'ZaphodKeyedFolder(''' + cDemoBranch + ''', ' +
    '''AProductArea'',' +
    '''database'', cxOptional, usrNone, '''', KeyedDetail)';
end;

procedure TForm1.LabelAttributeMouseEnter(Sender: TObject);
begin
  {hint on status bar}
  StatusBar1.SimpleText := 'ZaphodKeyedFileZNodeAttr...';
end;

end.
