home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 February / Chip_2003-02_cd1.bin / zkuste / delphi / kompon / d34567 / KADAO77.ZIP / SortByDialog.pas < prev   
Pascal/Delphi Source File  |  2001-06-22  |  7KB  |  276 lines

  1. unit SortByDialog;
  2. //******************************************************************************
  3. //                    Delphi Dao Project Version 1.6
  4. //                 Copyright (c) 2000 by Kiril Antonov
  5. //******************************************************************************
  6.  
  7. interface
  8.  
  9. uses
  10.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  11.   StdCtrls, Buttons;
  12.  
  13. type
  14.   TSortByDialog = class(TForm)
  15.     Label1: TLabel;
  16.     Label2: TLabel;
  17.     ListBox2: TListBox;
  18.     AddBtn: TBitBtn;
  19.     RemoveBtn: TBitBtn;
  20.     UpBtn: TBitBtn;
  21.     DownBtn: TBitBtn;
  22.     OKButton: TButton;
  23.     CancelButton: TButton;
  24.     ListBox3: TListBox;
  25.     Label3: TLabel;
  26.     ComboBox1: TComboBox;
  27.     Label4: TLabel;
  28.     ListBox1: TListBox;
  29.     procedure CancelButtonClick(Sender: TObject);
  30.     procedure OKButtonClick(Sender: TObject);
  31.     procedure AddBtnClick(Sender: TObject);
  32.     procedure RemoveBtnClick(Sender: TObject);
  33.     procedure UpBtnClick(Sender: TObject);
  34.     procedure DownBtnClick(Sender: TObject);
  35.     procedure ListBox3Click(Sender: TObject);
  36.     procedure ListBox1Click(Sender: TObject);
  37.     procedure ComboBox1Change(Sender: TObject);
  38.     procedure ListBox2DblClick(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     Function Execute(FieldNames:TStrings; OrderList:TStrings;UseQuotes:Boolean):Boolean;
  43.     { Public declarations }
  44.   end;
  45.  
  46. var
  47.   SortDialog: TSortByDialog;
  48.  
  49.  
  50. implementation
  51. {$R *.DFM}
  52. var
  53.  OrderListTemp  : TStrings;
  54.  F_UseQuotes    : Boolean;
  55.  
  56. Procedure RemoveExtraSpaces(var S:String);
  57. Var
  58.   SS : String;
  59.   P  : Integer;
  60. Begin
  61.   SS:='  ';
  62.   Repeat
  63.     P:=Pos(SS,S);
  64.     if P > 0 Then Delete(S,P,1);
  65.   Until P=0;
  66.  
  67.   SS:='[';
  68.   Repeat
  69.     P:=Pos(SS,S);
  70.     if P > 0 Then Delete(S,P,1);
  71.   Until P=0;
  72.  
  73.   SS:=']';
  74.   Repeat
  75.     P:=Pos(SS,S);
  76.     if P > 0 Then Delete(S,P,1);
  77.   Until P=0;
  78. End;
  79.  
  80. procedure TSortByDialog.CancelButtonClick(Sender: TObject);
  81. begin
  82.   ModalResult := mrCancel;
  83. end;
  84.  
  85. procedure TSortByDialog.OKButtonClick(Sender: TObject);
  86. Var
  87.  X:Integer;
  88. begin
  89.  if ListBox1.Items.Count > 0 Then
  90.     Begin
  91.      OrderListTemp.Clear;
  92.      For X:=0 To ListBox1.Items.Count-1 do
  93.          Begin
  94.           if ListBox3.Items[X]='Ascending' Then
  95.              Begin
  96.                if F_UseQuotes Then
  97.                   OrderListTemp.Add('['+ListBox1.Items[X]+'] ASC')
  98.                Else
  99.                   OrderListTemp.Add(ListBox1.Items[X]+' ASC');
  100.              End
  101.           Else
  102.              Begin
  103.                if F_UseQuotes Then
  104.                   OrderListTemp.Add('['+ListBox1.Items[X]+'] DESC')
  105.                Else
  106.                   OrderListTemp.Add(ListBox1.Items[X]+' DESC')
  107.              End;
  108.          End;
  109.     End;
  110.  ModalResult := mrOK;
  111. end;
  112.  
  113. procedure TSortByDialog.AddBtnClick(Sender: TObject);
  114. begin
  115.  if ListBox2.ItemIndex <> -1 Then
  116.     Begin
  117.       ListBox1.Items.Add(ListBox2.Items.Strings[ListBox2.ItemIndex]);
  118.       ListBox3.Items.Add(ComboBox1.Items[ComboBox1.ItemIndex]);
  119.       ListBox2.Items.Delete(ListBox2.ItemIndex);
  120.       ListBox1.ItemIndex:=ListBox1.Items.Count-1;
  121.       ListBox1Click(ListBox1);
  122.     End;
  123. end;
  124.  
  125. procedure TSortByDialog.RemoveBtnClick(Sender: TObject);
  126. begin
  127.  if ListBox1.ItemIndex <> -1 Then
  128.     Begin
  129.       ListBox2.Items.Add(ListBox1.Items.Strings[ListBox1.ItemIndex]);
  130.       ListBox3.Items.Delete(ListBox1.ItemIndex);
  131.       ListBox1.Items.Delete(ListBox1.ItemIndex);
  132.     End;
  133. end;
  134.  
  135. procedure TSortByDialog.UpBtnClick(Sender: TObject);
  136. Var
  137.   TmpField:String;
  138.   TmpOrder:String;
  139.   Poz     : Integer;
  140. begin
  141.   if ListBox1.ItemIndex > 0 Then
  142.      Begin
  143.        Poz:=ListBox1.ItemIndex;
  144.        TmpField:=ListBox1.Items[Poz-1];
  145.        TmpOrder:=ListBox3.Items[Poz-1];
  146.        ListBox1.Items[Poz-1]:=ListBox1.Items[Poz];
  147.        ListBox3.Items[Poz-1]:=ListBox3.Items[Poz];
  148.        ListBox1.Items[Poz]:=TmpField;
  149.        ListBox3.Items[Poz]:=TmpOrder;
  150.        ListBox1.ItemIndex:=Poz-1;
  151.        ListBox3.ItemIndex:=Poz-1;
  152.      End;
  153. end;
  154.  
  155. procedure TSortByDialog.DownBtnClick(Sender: TObject);
  156. Var
  157.   TmpField:String;
  158.   TmpOrder:String;
  159.   Poz     : Integer;
  160. begin
  161.   if ListBox1.ItemIndex < ListBox1.Items.Count-1 Then
  162.      Begin
  163.        Poz:=ListBox1.ItemIndex;
  164.        TmpField:=ListBox1.Items[Poz+1];
  165.        TmpOrder:=ListBox3.Items[Poz+1];
  166.        ListBox1.Items[Poz+1]:=ListBox1.Items[Poz];
  167.        ListBox3.Items[Poz+1]:=ListBox3.Items[Poz];
  168.        ListBox1.Items[Poz]:=TmpField;
  169.        ListBox3.Items[Poz]:=TmpOrder;
  170.        ListBox1.ItemIndex:=Poz+1;
  171.        ListBox3.ItemIndex:=Poz+1;
  172.      End;
  173. end;
  174.  
  175. Function TSortByDialog.Execute(FieldNames:TStrings; OrderList:TStrings;UseQuotes:Boolean):Boolean;
  176. Var
  177.  X,P           : Integer;
  178.  S             : String;
  179.  FieldNamesTmp : TStringList;
  180.  Tmp           : TStringList;
  181. Begin
  182.  Result:=False;
  183.  F_UseQuotes:=UseQuotes;
  184.  ComboBox1.Clear;
  185.  ComboBox1.Items.Add('Ascending');
  186.  ComboBox1.Items.Add('Descending');
  187.  ComboBox1.ItemIndex:=0;
  188.  Tmp := TStringList.Create;
  189.  Tmp.SetText(OrderList.GetText);
  190.  if OrderList.Count > 0 Then
  191.     begin
  192.       ListBox1.Clear;
  193.       For X:=0 To OrderList.Count-1 Do
  194.           Begin
  195.             S:=OrderList.Strings[X];
  196.             RemoveExtraSpaces(S);
  197.             P:=Pos(' DESC',S);
  198.             if P > 0 Then
  199.                Begin
  200.                  ListBox3.Items.Add('Descending');
  201.                  S:=Copy(S,1,P-1);
  202.                End
  203.             Else
  204.                Begin
  205.                  P:=Pos(' ASC',S);
  206.                  if P > 0 Then
  207.                     Begin
  208.                       S:=Copy(S,1,P-1);
  209.                     End;
  210.                  ListBox3.Items.Add('Ascending');
  211.                 End;
  212.             ListBox1.Items.Add(S);
  213.             OrderList.Strings[X]:=S;
  214.           End;
  215.     End;
  216.  FieldNamesTmp:=TStringList.Create;
  217.  FieldNamesTmp.SetText(FieldNames.GetText);
  218.  For X:=0 To OrderList.Count-1 do
  219.      Begin
  220.       P:=FieldNamesTmp.IndexOF(OrderList.Strings[X]);
  221.       if P > -1 Then FieldNamesTmp.Delete(P);
  222.      End;
  223.  if FieldNamesTmp.Count > 0 Then
  224.     Begin
  225.        ListBox2.Clear;
  226.        For X:=0 To FieldNamesTmp.Count-1 Do ListBox2.Items.Add(FieldNamesTmp.Strings[X]);
  227.     End;
  228.  FieldNamesTmp.Free;
  229.  OrderListTemp:=TStringList.Create;
  230.  OrderListTemp.Clear;
  231.  if ShowModal = mrOK Then
  232.     Begin
  233.       if OrderListTemp.Count > 0 Then
  234.          Begin
  235.            OrderList.SetText(OrderListTemp.GetText);
  236.          End
  237.       Else
  238.          OrderList.Clear;
  239.       Result:=True;
  240.     End                                         
  241.  Else
  242.     Begin
  243.       OrderList.SetText(Tmp.GetText);
  244.     End;
  245.  OrderListTemp.Free;
  246.  Tmp.Free;
  247. End;
  248.  
  249. procedure TSortByDialog.ListBox3Click(Sender: TObject);
  250. begin
  251.   ListBox1.ItemIndex:=ListBox3.ItemIndex;
  252.   ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf(ListBox3.Items.Strings[ListBox3.ItemIndex]);
  253. end;
  254.  
  255. procedure TSortByDialog.ListBox1Click(Sender: TObject);
  256. begin
  257.  ListBox3.ItemIndex:=ListBox1.ItemIndex;
  258.  ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf(ListBox3.Items.Strings[ListBox3.ItemIndex]);
  259. end;
  260.  
  261. procedure TSortByDialog.ComboBox1Change(Sender: TObject);
  262. Var
  263.   P: Integer;
  264. begin
  265.   P:=ListBox3.ItemIndex;
  266.   if P <> -1 Then
  267.      ListBox3.Items[P]:=ComboBox1.Items[ComboBox1.ItemIndex];
  268. end;
  269.  
  270. procedure TSortByDialog.ListBox2DblClick(Sender: TObject);
  271. begin
  272.  AddBtn.Click;
  273. end;
  274.  
  275. end.
  276.