File: frmselectdataset.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (188 lines) | stat: -rw-r--r-- 4,572 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{  $Id: frmselectdataset.pp 43091 2013-10-05 04:13:29Z paul $  }
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Michael Van Canneyt

  This unit registers the TMemDataset components of the FCL.
}
unit frmSelectDataset;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DB, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Buttons, MemDS, ComponentEditors, PropEdits, LazarusPackageIntf;

Type
  TMemDatasetEditor = Class(TComponentEditor)
    FStartIndex : Integer;
  Public
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
    Function GetMemDataset : TMemDataset;
    Procedure CopyDataset; virtual;
    Procedure CreateTable; virtual;
  end;

  { TSelectSrcDatasetForm }

  TSelectSrcDatasetForm = class(TForm)
    BOK: TButton;
    BCancel: TButton;
    CBMetaDataOnly: TCheckBox;
    LLBDatasets: TLabel;
    LBDatasets: TListBox;
    procedure BOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure LBDatasetsClick(Sender: TObject);
  private
  public
  end;

Resourcestring
  SErrComponentNotFound = 'Error: Component "%s" not found';
  SMenuCreateDataset    = 'Create dataset';
  SMenuCopyDataset      = 'Copy data from Dataset';
  SErrSelectDataset = 'Please select a dataset first';

  SCaption = 'Select dataset to copy from';
  SCopyMeta = 'Copy only metadata';
  SDataset = '&Dataset to copy from:';
  SOkBtn  = 'OK';
  SCancelBtn = 'Cancel';

var
  SelectSrcDatasetForm: TSelectSrcDatasetForm;

procedure Register;


implementation

{$R *.lfm}
{$R memdsicons.res}

procedure RegisterUnitMemDS;
begin
  RegisterComponents('Data Access',[TMemDataset]);
end;

procedure Register;
begin
  RegisterUnit('MemDS',@RegisterUnitMemDS);
  RegisterComponentEditor(TMemDataset,TMemdatasetEditor) ;
  RegisterPropertyEditor(TypeInfo(String),TMemDataset,'FileName',
                         TFileNamePropertyEditor);
end;

{ TMemDatasetEditor }

procedure TMemDatasetEditor.ExecuteVerb(Index: Integer);

begin
  If Index<FStartIndex then
    inherited ExecuteVerb(Index)
  else
    case (Index-FstartIndex) of
      0 : CreateTable;
      1 : CopyDataset;
    end;
end;

function TMemDatasetEditor.GetVerb(Index: Integer): string;
begin
  If Index<FStartIndex then
    Result:=inherited GetVerb(Index)
  else
    case (Index-FstartIndex) of
      0 : Result:=SMenuCreateDataset;
      1 : Result:=SMenuCopyDataset;
    end;
end;

function TMemDatasetEditor.GetVerbCount: Integer;
begin
  FStartIndex:=inherited GetVerbCount;
  Result:=FStartIndex+2;
end;

function TMemDatasetEditor.GetMemDataset: TMemDataset;
begin
  Result:=GetComponent as TMemDataset;
end;

procedure TMemDatasetEditor.CopyDataset;

Var
  DSN : String;
  DS  : TDataset;
  I   : integer;
  F   : TComponent;
begin
  With TSelectSrcDatasetForm.Create(Application) do
    Try
      F:=GetDesigner.Form;
      For I:=0 to F.ComponentCount-1 do
        if (F.Components[i] is TDataset) and
           (F.Components[i]<>GetComponent) then
          LBDatasets.Items.Add(F.Components[i].Name);
      If ShowModal=mrOK then
        begin
        With LBDatasets do
          DSN:=Items[ItemIndex];
        DS:=Nil;
        I:=0;
        While (DS=Nil) and (I<F.ComponentCount) do
          if (F.Components[i] is TDataset) and
             (F.Components[i].Name=DSN) then
            DS:=F.Components[i] as TDataset
          else
            Inc(I);
        If (DS=Nil) then
          Raise Exception.CreateFmt(SErrComponentNotFound,[DSN]);
        GetMemDataset.CopyFromDataSet(DS,Not CBMetaDataOnly.Checked);
        // Modified;
        end;
    Finally
      Free;
    end;
end;

procedure TMemDatasetEditor.CreateTable;
begin
  GetMemdataset.CreateTable;
end;

{ TSelectSrcDatasetForm }

procedure TSelectSrcDatasetForm.LBDatasetsClick(Sender: TObject);
begin
  if Sender=nil then ;
  BOK.Enabled:=True;
end;

procedure TSelectSrcDatasetForm.BOKClick(Sender: TObject);
begin
  if Sender=nil then ;
  If LBDatasets.ItemIndex=-1 then
    Raise Exception.Create(SErrSelectDataset)
end;

procedure TSelectSrcDatasetForm.FormCreate(Sender: TObject);
begin
  Caption := SCaption;
  CBMetaDataOnly.Caption := SCopyMeta;
  LLBDatasets.Caption := SDataset;
  BOK.Caption := SOkBtn;
  BCancel.Caption := SCancelBtn;
end;

end.