action.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 := TList.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. procedure TBasicAction.SetOnExecute(Value: TNotifyEvent);
  108. var
  109. I: Integer;
  110. begin
  111. if (TMethod(Value).Code <> TMethod(OnExecute).Code) or
  112. (TMethod(Value).Data <> TMethod(OnExecute).Data) then
  113. begin
  114. for I := 0 to FClients.Count - 1 do
  115. TBasicActionLink(FClients[I]).SetOnExecute(Value);
  116. FOnExecute := Value;
  117. Change;
  118. end;
  119. end;
  120. procedure TBasicAction.Change;
  121. begin
  122. if Assigned(FOnChange) then
  123. FOnChange(Self);
  124. end;
  125. procedure TBasicAction.RegisterChanges(Value: TBasicActionLink);
  126. begin
  127. Value.FAction := Self;
  128. FClients.Add(Value);
  129. end;
  130. procedure TBasicAction.UnRegisterChanges(Value: TBasicActionLink);
  131. var
  132. I: Integer;
  133. begin
  134. for I := 0 to FClients.Count - 1 do
  135. if TBasicActionLink(FClients[I]) = Value then
  136. begin
  137. Value.FAction := nil;
  138. FClients.Delete(I);
  139. break;
  140. end;
  141. end;