Einzelnen Beitrag anzeigen

CG2003

Registriert seit: 8. Nov 2003
Ort: Hamburg
470 Beiträge
 
Delphi 2009 Professional
 
#1

Komponente vor dem Platzieren umbenennen / OpenTools API

  Alt 20. Sep 2006, 17:46
Hallo liebe DP,

ich versuche mich gerade an einem kleinen IDE-Experten, mit dem ich Komponenten umbenennen kann, während ich sie auf die Form ziehe. Sowas gibt es ja schon: CNA - Component Naming Assistant,
aber ich will mein eigenes schreiben!

Dazu habe ich folgenden Code im Internet gefunden:

Delphi-Quellcode:
unit ComponentPrefixExpert;

interface

procedure Register;

implementation

uses Windows, SysUtils, Controls, Classes, ToolsAPI, Dialogs;

type
  TPPWIdeNotifier = class( TNotifierObject, IOTANotifier, IOTAIDENotifier)
  public
    constructor Create;
    destructor Destroy; override;
    { IOTAIDENotifier } 
    procedure FileNotification(NotifyCode: TOTAFileNotification;
      const FileName: string; var Cancel: Boolean);
    procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload;
    procedure AfterCompile(Succeeded: Boolean); //overload;
  end;

  TPPWFormNotifier = class( TNotifierObject, IOTANotifier, IOTAFormNotifier )
  private
    FFileName : String;
  public
    constructor Create( FileName : String );
    destructor Destroy; override;
    procedure FormActivated;
    procedure FormSaving;
    procedure ComponentRenamed(ComponentHandle: TOTAHandle;
      const OldName, NewName: string);
    { IOTAModuleNotifier } 
  end;

  TPPWModuleNotifier = class( TNotifierObject, IOTANotifier, IOTAModuleNotifier )
  private
    FOldFileName : String;
    FFileName : String;
  public
    constructor Create (Const FileName : String);
    destructor destroy; override;
    { IOTAModuleNotifier } 
    function CheckOverwrite : Boolean;
    procedure ModuleRenamed(const NewName: string);
  end;

var
  Index : Integer;
  NotifierList : TStringList;

procedure Register;
begin
  Index := (BorlandIDEServices as IOTAServices).AddNotifier(TPPWIdeNotifier.Create);
end;


{ TPPWIdeNotifier } 

procedure TPPWIdeNotifier.AfterCompile(Succeeded: Boolean);
begin
end;

procedure TPPWIdeNotifier.BeforeCompile(const Project: IOTAProject;
  var Cancel: Boolean);
begin
end;

constructor TPPWIdeNotifier.Create;
begin
  Inherited Create;
  ShowMessage ('TPPWIdeNotifier created');
end;

destructor TPPWIdeNotifier.Destroy;
begin
  ShowMessage (' Destroying TPPWIdeNotifier ');
  ShowMessage (' Notifiers Left : ' + inttostr(NotifierList.Count));
  inherited Destroy;
end;

procedure TPPWIdeNotifier.FileNotification(
  NotifyCode: TOTAFileNotification; const FileName: string;
  var Cancel: Boolean);
var
  Module : IOTAModule;
  Editor : IOTAEditor;
  FormEditor : IOTAFormEditor;
  ModuleNotifier : TPPWModuleNotifier;
  FormNotifier : TPPWFormNotifier;

  IModuleNotifier : Integer;
  FormNotifierI, ListI, I : Integer;
begin
  Case NotifyCode of
    ofnFileOpened :
    begin
      { Get the IOTAModule associated to this File } 
      Module := ( BorlandIDEServices as IOTAModuleServices ).FindModule(FileName);
      { Loop over the number of associated File } 
      for i := 0 to Module.GetModuleFileCount - 1 do
      begin
        { Get the FileEditor } 
        Editor := Module.GetModuleFileEditor(i);
        if Editor.QueryInterface( IOTAFormEditor, FormEditor ) = S_OK then
        begin
          { If the File Editor is a FormEditor then Add Our Notifier } 
          FormNotifier := TPPWFormNotifier.Create( FileName );
          FormNotifierI := FormEditor.AddNotifier ( FormNotifier );
          if FormNotifierI < 0 then
          begin
            FormNotifier.Free;
          end
          else
          begin
            ShowMessage( 'Notifier Index' + inttostr( FormNotifierI ));
            NotifierList.AddObject(FileName, Pointer(FormNotifierI));
          end;
          { Also Add a module Notifier } 
          ModuleNotifier := TPPWModuleNotifier.Create( FileName );
          IModuleNotifier := Module.AddNotifier( ModuleNotifier );
          if IModuleNotifier < 0 then
          begin
            ModuleNotifier.Free;
          end;
        end
      end;
    end;
    ofnFileClosing :
    begin
      if NotifierList.Find(FileName, ListI) then
      begin
        Module := ( BorlandIDEServices as IOTAModuleServices ).FindModule(FileName);
        FormNotifierI := Integer(NotifierList.Objects[ListI]);
        for i := 0 to Module.GetModuleFileCount - 1 do
        begin
          Editor := Module.GetModuleFileEditor(i);
          if Editor.QueryInterface( IOTAFormEditor, FormEditor ) = S_OK then
          begin
            FormEditor.RemoveNotifier ( FormNotifierI );
            NotifierList.Delete(ListI);
          end;
        end;
      end;
    end;
  end;
end;

{ TPPWFormNotifier } 

procedure TPPWFormNotifier.ComponentRenamed(ComponentHandle: TOTAHandle;
  const OldName, NewName: string);
var
  Module : IOTAModule;
  Editor : IOTAEditor;
  FormEditor : IOTAFormEditor;
  OTAComponent: IOTAComponent;
  Component: IComponent;
  I: Integer;
  
begin
  if not FRenaming then
  try
    FRenaming := True;
    Module := ( BorlandIDEServices as IOTAModuleServices ).FindModule(FFileName);
    for i := 0 to Module.GetModuleFileCount - 1 do
    begin
      Editor := Module.GetModuleFileEditor(i);
      if Editor.QueryInterface( IOTAFormEditor, FormEditor ) = S_OK then
      begin
        OTAComponent := FormEditor.FindComponent(OldName);
        if OTAComponent = nil then ShowMessage('OTAComponent not found.')
                              else
        begin
          Component := OTAComponent.GetIComponent;
          if Component = nil then ShowMessage('IComponent not found')
                             else Component.Name := 'Test' + NewName;
        end;
      end
    end;
  finally
    FRenaming := False;
  end;
end;
constructor TPPWFormNotifier.Create( FileName : String );
begin
  inherited Create;
  FFileName := FileName;
  ShowMessage ('Form Notifier Created for File : ' + FileName );
end;

destructor TPPWFormNotifier.destroy;
begin
  ShowMessage ('Form Notifier Destroyed for File : ' + FFileName );
  inherited Destroy;
end;

procedure TPPWFormNotifier.FormActivated;
begin
{} 
end;

procedure TPPWFormNotifier.FormSaving;
begin
{} 
end;

{ TPPWModuleNotifier } 

function TPPWModuleNotifier.CheckOverwrite: Boolean;
begin
  Result := True;
end;

constructor TPPWModuleNotifier.Create(const FileName: String);
begin
  inherited Create;
  FOldFileName := FileName;
  FFileName := FileName;
  ShowMessage ('Module Notifier Created for file : ' + FOldFileName);
end;

destructor TPPWModuleNotifier.destroy;
begin
  ShowMessage ('Module Notifier Destroyed for file : ' + FFileName);
  inherited;
end;

procedure TPPWModuleNotifier.ModuleRenamed(const NewName: string);
var
  ListI : Integer;
  FormNotifierI : Integer;
begin
  if NotifierList.Find(FOldFileName, ListI) then
  begin
    FormNotifierI := Integer(NotifierList.Objects[ListI]);
    NotifierList.Delete(ListI);
    NotifierList.AddObject(NewName, Pointer(FormNotifierI));
  end;
  FOldFileName := NewName;
  FFileName := NewName;
  inherited ;
end;

initialization
  NotifierList := TStringList.Create;
  NotifierList.Sorted := True;

finalization
  (BorlandIDEServices as IOTAServices).RemoveNotifier(Index);
  NotifierList.Free;
end.
Das Problem ist nur, das er weder "FRenaming" noch "IComponent" findet/kennt.
Oder gibt es eine einfachere Variante eine Art "on the fly Komponenten-Umbenenner" zu entwickeln?

Bin für jeden Hinweis/Tip dankbar!
Sebastian M.
Viele Grüße aus Hamburg


Meine Website: www.sebastian-mundt.com
  Mit Zitat antworten Zitat