unit uAttachedFiles;
{< this unit opens form to manage attached files on deleting}

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, System.ImageList, Vcl.ImgList;

type
  TfAttachedFiles = class(TForm)
    bDelete: TButton;
    PageControl: TPageControl;
    tsAllDeletedFiles: TTabSheet;
    tsAttachedFiles: TTabSheet;
    tsNotAttached: TTabSheet;
    ListViewAllDeletedFiles: TListView;
    ListViewAttachedToDocuments: TListView;
    ListViewNotAttached: TListView;
    ilFoldersAndFiles: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure bDeleteClick(Sender: TObject);
    {< deletes elements from one of 3 Lists

      ListViewAllDeletedFiles
      ListViewAttachedToDocuments
      ListViewNotAttached

     }
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }

  procedure  ListViewConstructColumns;

  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;

    procedure  DisposePointers();
    {< Dispose Pointers from outside }


    destructor Destroy; override;

  end;

//var
//  fAttachedFiles: TfAttachedFiles;

implementation

{$R *.dfm}

uses uControlPanel;

var ControlPanel_UnitVar:TControlPanel;


procedure TfAttachedFiles.bDeleteClick(Sender: TObject);
var i:integer; s:string;
    IDListToDelete:TStringList;


begin


IDListToDelete:=TStringList.Create;

try

    try


    case PageControl.ActivePageIndex of

        0: with ListViewAllDeletedFiles do begin

             IDListToDelete.Clear;

             for i := 0 to Items.Count-1 do

             if Items[i].Checked then

                 IDListToDelete.Add(

                  Integer(Items[i].Data^).ToString()

                 );




        end;

        1: with ListViewAttachedToDocuments do begin

           IDListToDelete.Clear;

           for i := 0 to Items.Count-1 do

            if Items[i].Checked then

               IDListToDelete.Add(

                Integer(Items[i].Data^).ToString()

               );


        end;


        2: with ListViewNotAttached do begin

                IDListToDelete.Clear;

           for i := 0 to Items.Count-1 do

            if Items[i].Checked then

               IDListToDelete.Add(

                Integer(Items[i].Data^).ToString()

               );


        end;

        end;



       ControlPanel_UnitVar.Delete(IDListToDelete);

       ModalResult:=mrOk;

    except

    on E:Exception do raise E.Create('Error Message '+ E.Message);

    end;

finally


IDListToDelete.Free;

end;


end;


constructor TfAttachedFiles.Create(AOwner: TComponent);
begin
  inherited;

  if (AOwner is TControlPanel) then
  ControlPanel_UnitVar:=(AOwner as TControlPanel);



end;

destructor TfAttachedFiles.Destroy;
begin

//DisposePointers;

  inherited;
end;

procedure TfAttachedFiles.DisposePointers;
var i,j:Integer;
begin


//ListViewAllDeletedFiles.Items[0].Data

 for i:= 0 to Self.ComponentCount-1 do begin


     if (Self.Components[i] is TListView) then

           with (Self.Components[i] as TListView) do begin


            for j := 0 to Items.Count-1 do

               if Assigned( Items[j].Data ) then Dispose(Items[j].Data);
              


           end;



     end;


 end;





procedure TfAttachedFiles.FormClose(Sender: TObject; var Action: TCloseAction);
begin

// DisposePointers;

end;

procedure TfAttachedFiles.FormCreate(Sender: TObject);
begin

bDelete.Left:=Trunc(

 Self.Width/2 -bDelete.Width/2

);

ListViewConstructColumns;


end;

procedure TfAttachedFiles.ListViewConstructColumns;

var Col:TListColumn; Item:TListItem;
  i: Integer;

begin

// adding columns to ListView


for i := 0 to Self.ComponentCount-1 do begin

  if Self.Components[i] is TListView then

  begin


     Col:=(Self.Components[i] as TListView).Columns.Add;
     Col.Caption:='Имя файла';
     Col.Width:=300;


     Col:=(Self.Components[i] as TListView).Columns.Add;
     Col.Caption:='Используется в документах';
     Col.Width:=100;


     Col:=(Self.Components[i] as TListView).Columns.Add;
     Col.Caption:='Где используется';
     Col.Width:=250;


  end;


end;




 {
     //Demo Data

     for i := 0 to 50 do

    begin


      Item := ListView.Items.Add;
      Item.Caption := 'Первая колонка';
      Item.SubItems.Add('Вторая колонка');
      Item.SubItems.Add('Третья колонка');

    end;

 }


end;




end.
