action.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************}
  11. {* TBasicActionLink *}
  12. {****************************************************************************}
  13. constructor TBasicActionLink.Create(AClient: TObject);
  14. begin
  15. inherited Create;
  16. AssignClient(AClient);
  17. end;
  18. procedure TBasicActionLink.AssignClient(AClient: TObject);
  19. begin
  20. end;
  21. destructor TBasicActionLink.Destroy;
  22. begin
  23. if FAction <> nil then
  24. FAction.UnRegisterChanges(Self);
  25. inherited Destroy;
  26. end;
  27. procedure TBasicActionLink.Change;
  28. begin
  29. if Assigned(OnChange) then
  30. OnChange(FAction);
  31. end;
  32. function TBasicActionLink.Execute(AComponent: TComponent): Boolean;
  33. begin
  34. FAction.ActionComponent := AComponent;
  35. try
  36. Result := FAction.Execute;
  37. finally
  38. if FAction <> nil then
  39. FAction.ActionComponent := nil;
  40. end;
  41. end;
  42. procedure TBasicActionLink.SetAction(Value: TBasicAction);
  43. begin
  44. if Value <> FAction then
  45. begin
  46. if FAction <> nil then FAction.UnRegisterChanges(Self);
  47. FAction := Value;
  48. if Value <> nil then Value.RegisterChanges(Self);
  49. end;
  50. end;
  51. function TBasicActionLink.IsOnExecuteLinked: Boolean;
  52. begin
  53. Result := True;
  54. end;
  55. procedure TBasicActionLink.SetOnExecute(Value: TNotifyEvent);
  56. begin
  57. end;
  58. function TBasicActionLink.Update: Boolean;
  59. begin
  60. Result := FAction.Update;
  61. end;
  62. {****************************************************************************}
  63. {* TBasicAction *}
  64. {****************************************************************************}
  65. constructor TBasicAction.Create(AOwner: TComponent);
  66. begin
  67. inherited Create(AOwner);
  68. FClients := TFpList.Create;
  69. end;
  70. destructor TBasicAction.Destroy;
  71. begin
  72. inherited Destroy;
  73. while FClients.Count > 0 do
  74. UnRegisterChanges(TBasicActionLink(FClients.Last));
  75. FClients.Free;
  76. end;
  77. function TBasicAction.HandlesTarget(Target: TObject): Boolean;
  78. begin
  79. Result := False;
  80. end;
  81. procedure TBasicAction.ExecuteTarget(Target: TObject);
  82. begin
  83. end;
  84. procedure TBasicAction.UpdateTarget(Target: TObject);
  85. begin
  86. end;
  87. function TBasicAction.Execute: Boolean;
  88. begin
  89. if Assigned(FOnExecute) then
  90. begin
  91. FOnExecute(Self);
  92. Result := True;
  93. end
  94. else
  95. Result := False;
  96. end;
  97. function TBasicAction.Update: Boolean;
  98. begin
  99. if Assigned(FOnUpdate) then
  100. begin
  101. FOnUpdate(Self);
  102. Result := True;
  103. end
  104. else
  105. Result := False;
  106. end;
  107. function TBasicAction.Suspended: Boolean;
  108. begin
  109. Result:=False;
  110. end;
  111. procedure TBasicAction.SetOnExecute(Value: TNotifyEvent);
  112. var
  113. I: Integer;
  114. begin
  115. if (TMethod(Value).Code <> TMethod(OnExecute).Code) or
  116. (TMethod(Value).Data <> TMethod(OnExecute).Data) then
  117. begin
  118. for I := 0 to FClients.Count - 1 do
  119. TBasicActionLink(FClients[I]).SetOnExecute(Value);
  120. FOnExecute := Value;
  121. Change;
  122. end;
  123. end;
  124. function TBasicAction.ClientCount: Integer;
  125. begin
  126. Result:=FClients.Count;
  127. end;
  128. function TBasicAction.GetClient(Idx: Integer): TObject;
  129. begin
  130. Result:=TObject(FClients[Idx]);
  131. end;
  132. procedure TBasicAction.SetActionComponent(AValue: TComponent);
  133. begin
  134. if FActionComponent=AValue then Exit;
  135. if Assigned(FActionComponent) then
  136. FActionComponent.RemoveFreeNotification(Self);
  137. FActionComponent:=AValue;
  138. if Assigned(FActionComponent) then
  139. FActionComponent.FreeNotification(Self);
  140. end;
  141. procedure TBasicAction.Change;
  142. begin
  143. if Assigned(FOnChange) then
  144. FOnChange(Self);
  145. end;
  146. procedure TBasicAction.Notification(AComponent: TComponent;
  147. Operation: TOperation);
  148. begin
  149. inherited Notification(AComponent, Operation);
  150. if (Operation<>OpRemove) then
  151. exit;
  152. if (AComponent=FActionComponent) then
  153. FActionComponent:=nil;
  154. end;
  155. procedure TBasicAction.RegisterChanges(Value: TBasicActionLink);
  156. begin
  157. Value.FAction := Self;
  158. FClients.Add(Value);
  159. end;
  160. procedure TBasicAction.UnRegisterChanges(Value: TBasicActionLink);
  161. var
  162. I: Integer;
  163. begin
  164. for I := 0 to FClients.Count - 1 do
  165. if TBasicActionLink(FClients[I]) = Value then
  166. begin
  167. Value.FAction := nil;
  168. FClients.Delete(I);
  169. break;
  170. end;
  171. end;