Calling a function on plugin load


I am trying to use virtualfilelistener to do custom actions after any file add, edit, etc. I have implemented below function which creates a virtualfilelistener and add it to virtualfilemanager. Currently I am calling this function on a button action. But I am looking if it can be called automatically on plugin load or IDE load with plugin installed. Any suggestions on how that can be done?




public void flitener() {

public VirtualFileListener mylistener;

mylistener = new VirtualFileAdapter() {
 @Override
      public void fileCreated(@NotNull VirtualFileEvent event) {
 }
 
 @Override
      public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
 }
 
 @Override
      public void contentsChanged(@NotNull VirtualFileEvent event) {
 }
 
 @Override
      public void fileDeleted(@NotNull VirtualFileEvent event) {
 }
 
 @Override
      public void fileMoved(@NotNull VirtualFileMoveEvent event) {
 }
 
 @Override
      public void fileCopied(@NotNull VirtualFileCopyEvent event) {
 }
 
 @Override
      public void beforePropertyChange(@NotNull VirtualFilePropertyEvent event) {
 }
 @Override
      public void beforeContentsChange(@NotNull VirtualFileEvent event) {
 }
 @Override
      public void beforeFileDeletion(@NotNull VirtualFileEvent event) {
 }
 @Override
      public void beforeFileMovement(@NotNull VirtualFileMoveEvent event) {
 }
}


VirtualFileManager.getInstance().addVirtualFileListener(mylistener);

}
3 comments
Comment actions Permalink

You should register your listener from an ApplicationComponent or ProjectComponent.


0
Comment actions Permalink
 
public class FileProjectComponent implements ProjectComponent{
    public FileProjectComponent () {
        Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "ALOKR-", "constructor", NotificationType.INFORMATION));
    }

    @Override
    public void
initComponent() {
        // TODO: insert component initialization logic here
        
Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "ALOKR-", "init component", NotificationType.INFORMATION));
    }

    @Override
    public void
disposeComponent() {
        // TODO: insert component disposal logic here
        
Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "ALOKR-", "dispose component", NotificationType.INFORMATION));
    }

    @Override
    @NotNull
    public
String getComponentName() {
        Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "ALOKR-", "get component name", NotificationType.INFORMATION));
        return "filelistener";
    }

    @Override
    public void
projectOpened() {
        // called when project is opened
        
Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "ALOKR-", "project opened", NotificationType.INFORMATION));
    }

    @Override
    public void
projectClosed() {
        Notifications.Bus.notify(new Notification(ConsoleView.CONSOLE_CONTENT_ID, "ALOKR-", "project closed", NotificationType.INFORMATION));
        // called when project is being closed
    
}
}






With above code, I don't see any of the logs getting printed. Is this correct? Do I need to make change to any other plugin.xml file too?

0
Comment actions Permalink

Yes, you do. Why don't you read the documentation explaining this in detail? http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure.html

0

Please sign in to leave a comment.