unit FormImportFormats;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  ActnList, Menus, Acronym, FormEx;

type

  { TFormImportFormats }

  TFormImportFormats = class(TFormEx)
    AAdd: TAction;
    ActionList1: TActionList;
    AModify: TAction;
    ARemove: TAction;
    ListView1: TListView;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    PopupMenuImportSource: TPopupMenu;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    procedure AAddExecute(Sender: TObject);
    procedure AModifyExecute(Sender: TObject);
    procedure ARemoveExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    procedure ListView1DblClick(Sender: TObject);
    procedure ListView1KeyPress(Sender: TObject; var Key: char);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
  public
    AcronymDb: TAcronymDb;
    ImportFormats: TImportFormats;
    procedure UpdateList;
    procedure UpdateInterface;
  end;


implementation

{$R *.lfm}

uses
  FormImportFormat;

resourcestring
  SRemoveImportFormat = 'Remove import formats';
  SRemoveImportFormatQuery = 'Do you really want to remove selected import formats?';
  SImportFormatAlreadyExists = 'Import format %s already exists!';


{ TFormImportFormats }

procedure TFormImportFormats.ListView1Data(Sender: TObject; Item: TListItem);
begin
  if Item.Index < ImportFormats.Count then
  with TImportFormat(ImportFormats[Item.Index]) do begin
    Item.Caption := Name;
    Item.Data := ImportFormats[Item.Index];
  end;
end;

procedure TFormImportFormats.ListView1DblClick(Sender: TObject);
begin
  AModify.Execute;
end;

procedure TFormImportFormats.ListView1KeyPress(Sender: TObject; var Key: char);
begin
  if Key = #27 then Close;
end;

procedure TFormImportFormats.ListView1SelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  UpdateInterface;
end;

procedure TFormImportFormats.UpdateList;
begin
  ListView1.Items.Count := ImportFormats.Count;
  ListView1.Refresh;
  UpdateInterface;
end;

procedure TFormImportFormats.UpdateInterface;
begin
  ARemove.Enabled := Assigned(ListView1.Selected);
  AModify.Enabled := Assigned(ListView1.Selected);
end;

procedure TFormImportFormats.FormShow(Sender: TObject);
begin
  UpdateList;
  ScaleDPI.ScaleControl(ToolBar1, ScaleDPI.DesignDPI);
end;

procedure TFormImportFormats.AAddExecute(Sender: TObject);
var
  NewImportFormat: TImportFormat;
  FormImportFormat: TFormImportFormat;
begin
  NewImportFormat := TImportFormat.Create;
  NewImportFormat.Formats := ImportFormats;
  FormImportFormat := TFormImportFormat.Create(Self);
  try
    FormImportFormat.Load(NewImportFormat);
    if FormImportFormat.ShowModal = mrOk then begin
      FormImportFormat.Save(NewImportFormat);
      if not Assigned(ImportFormats.SearchByName(NewImportFormat.Name)) then begin;
        ImportFormats.Add(NewImportFormat);
        NewImportFormat := nil;
        AcronymDb.Modified := True;
        UpdateList;
      end else ShowMessage(Format(SImportFormatAlreadyExists, [NewImportFormat.Name]));
    end;
    if Assigned(NewImportFormat) then NewImportFormat.Free;
  finally
    FreeAndNil(FormImportFormat);
  end;
end;

procedure TFormImportFormats.AModifyExecute(Sender: TObject);
var
  NewImportFormat: TImportFormat;
  FormImportFormat: TFormImportFormat;
begin
  if Assigned(ListView1.Selected) then begin
    NewImportFormat := TImportFormat.Create;
    NewImportFormat.Assign(ListView1.Selected.Data);
    FormImportFormat := TFormImportFormat.Create(Self);
    try
      FormImportFormat.Load(NewImportFormat);
      if FormImportFormat.ShowModal = mrOk then begin
        FormImportFormat.Save(NewImportFormat);
        if (NewImportFormat.Name <> TImportFormat(ListView1.Selected.Data).Name) then begin
          if not Assigned(ImportFormats.SearchByName(NewImportFormat.Name)) then begin;
            TImportFormat(ListView1.Selected.Data).Assign(NewImportFormat);
            AcronymDb.Modified := True;
            UpdateList;
          end else ShowMessage(Format(SImportFormatAlreadyExists, [NewImportFormat.Name]));
        end else begin
          TImportFormat(ListView1.Selected.Data).Assign(NewImportFormat);
          AcronymDb.Modified := True;
          UpdateList;
        end;
      end;
      if Assigned(NewImportFormat) then NewImportFormat.Free;
    finally
      FreeAndNil(FormImportFormat);
    end;
  end;
end;

procedure TFormImportFormats.ARemoveExecute(Sender: TObject);
var
  I: Integer;
begin
  if Assigned(ListView1.Selected) then begin
    if MessageDlg(SRemoveImportFormat, SRemoveImportFormatQuery,
    TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
      for I := ListView1.Items.Count - 1 downto 0 do
      if ListView1.Items[I].Selected then
        ImportFormats.Remove(ListView1.Items[I].Data);
      UpdateList;
    end;
  end;
end;

procedure TFormImportFormats.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to ToolBar1.ButtonCount - 1 do
    ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
end;

end.

