package org.editorconfig.plugincomponents;

import com.intellij.AppTopics;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.util.messages.MessageBus;
import org.editorconfig.configmanagement.CodeStyleManager;
import org.editorconfig.configmanagement.EncodingManager;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

public class ConfigProjectComponent implements ProjectComponent {
    private final Project project;
    private final CodeStyleManager codeStyleManager;

    public ConfigProjectComponent(Project project) {
        this.project = project;

        // Register project-level config managers
        MessageBus bus = project.getMessageBus();
        codeStyleManager = new CodeStyleManager(project);
        EncodingManager encodingManager = new EncodingManager(project);
        bus.connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, codeStyleManager);
        bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, encodingManager);
    }

    public void initComponent() {
        // TODO: insert component initialization logic here
    }

    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }

    @NotNull
    public String getComponentName() {
        return "ConfigProjectComponent";
    }

    public void projectOpened() {
        // called when project is opened
        IdeFrame frame = WindowManager.getInstance().getIdeFrame(project);
        final Window window = (Window)frame;
        window.addWindowFocusListener(codeStyleManager);
        Disposer.register(project, new Disposable() {
            @Override
            public void dispose() {
                window.removeWindowFocusListener(codeStyleManager);
            }
        });
    }

    public void projectClosed() {
        // called when project is being closed
    }
}

@@ -72,48 +72,8 @@
 cs;%0A
-import com.intellij.openapi.Disposable;%0A
 impo
@@ -240,51 +240,8 @@
 ct;%0A
-import com.intellij.openapi.util.Disposer;%0A
 impo
@@ -1764,297 +1764,272 @@
     
+%7D%0A%0A
     
-Disposer.register(project, new Disposable() %7B%0A            @Override%0A            public void dispose() %7B%0A                window.removeWindowFocusListener(codeStyleManager);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void projectClosed() %7B%0A        // called when project is being closed
+public void projectClosed() %7B%0A        // called when project is being closed%0A        IdeFrame frame = WindowManager.getInstance().getIdeFrame(project);%0A        final Window window = (Window) frame;%0A        window.removeWindowFocusListener(codeStyleManager);
 %0A   

use projectClosed instead of disposer