summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/objpas/classes/action.inc
blob: c57b8bb32a98d594990e697448d4f54b278046fd (plain)
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
{
    This file is part of the Free Component Library (FCL)
    Copyright (c) 1999-2000 by the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{****************************************************************************}
{*                           TBasicActionLink                               *}
{****************************************************************************}

constructor TBasicActionLink.Create(AClient: TObject);
begin
  inherited Create;
  AssignClient(AClient);
end;


procedure TBasicActionLink.AssignClient(AClient: TObject);
begin
end;


destructor TBasicActionLink.Destroy;
begin
  if FAction <> nil then
    FAction.UnRegisterChanges(Self);
  inherited Destroy;
end;


procedure TBasicActionLink.Change;
begin
  if Assigned(OnChange) then
    OnChange(FAction);
end;


function TBasicActionLink.Execute(AComponent: TComponent): Boolean;
begin
  FAction.ActionComponent := AComponent;
  try
    Result := FAction.Execute;
  finally
    if FAction <> nil then
      FAction.ActionComponent := nil;
  end;
end;


procedure TBasicActionLink.SetAction(Value: TBasicAction);
begin
  if Value <> FAction then
  begin
    if FAction <> nil then FAction.UnRegisterChanges(Self);
    FAction := Value;
    if Value <> nil then Value.RegisterChanges(Self);
  end;
end;


function TBasicActionLink.IsOnExecuteLinked: Boolean;
begin
  Result := True;
end;


procedure TBasicActionLink.SetOnExecute(Value: TNotifyEvent);
begin
end;


function TBasicActionLink.Update: Boolean;
begin
  Result := FAction.Update;
end;

{****************************************************************************}
{*                             TBasicAction                                 *}
{****************************************************************************}

constructor TBasicAction.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FClients := TFpList.Create;
end;


destructor TBasicAction.Destroy;
begin
  inherited Destroy;
  while FClients.Count > 0 do
    UnRegisterChanges(TBasicActionLink(FClients.Last));
  FClients.Free;
end;


function TBasicAction.HandlesTarget(Target: TObject): Boolean;
begin
  Result := False;
end;


procedure TBasicAction.ExecuteTarget(Target: TObject);
begin
end;


procedure TBasicAction.UpdateTarget(Target: TObject);
begin
end;


function TBasicAction.Execute: Boolean;
begin
  if Assigned(FOnExecute) then
   begin
     FOnExecute(Self);
     Result := True;
   end
  else
   Result := False;
end;


function TBasicAction.Update: Boolean;
begin
  if Assigned(FOnUpdate) then
   begin
     FOnUpdate(Self);
     Result := True;
   end
  else
   Result := False;
end;


procedure TBasicAction.SetOnExecute(Value: TNotifyEvent);
var
  I: Integer;
begin
  if (TMethod(Value).Code <> TMethod(OnExecute).Code) or
     (TMethod(Value).Data <> TMethod(OnExecute).Data) then
  begin
    for I := 0 to FClients.Count - 1 do
      TBasicActionLink(FClients[I]).SetOnExecute(Value);
    FOnExecute := Value;
    Change;
  end;
end;


procedure TBasicAction.Change;
begin
  if Assigned(FOnChange) then
    FOnChange(Self);
end;


procedure TBasicAction.RegisterChanges(Value: TBasicActionLink);
begin
  Value.FAction := Self;
  FClients.Add(Value);
end;


procedure TBasicAction.UnRegisterChanges(Value: TBasicActionLink);
var
  I: Integer;
begin
  for I := 0 to FClients.Count - 1 do
    if TBasicActionLink(FClients[I]) = Value then
     begin
       Value.FAction := nil;
       FClients.Delete(I);
       break;
     end;
end;