package gitlog;

import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.impl.DirectoryIndex;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import git4idea.GitUtil;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import git4idea.repo.GitRepositoryManagerImpl;
import org.hanuna.gitalk.swing_ui.Swing_UI;
import org.hanuna.gitalk.ui.impl.UI_ControllerImpl;

import javax.swing.*;

/**
 * @author Kirill Likhodedov
 */
public class GitLogComponent extends AbstractProjectComponent {

  private final Project myProject;
  private GitRepositoryManager myRepositoryManager;
  private GitRepository myRepository;
  private Swing_UI mySwingUi;
  private UI_ControllerImpl myUiController;

  @SuppressWarnings("UnusedParameters")
  protected GitLogComponent(Project project, GitRepositoryManager makeSureRepositoryManagerIsInitialized,
                            DirectoryIndex makeSureIndexIsInitialized) {
    super(project);
    myProject = project;
  }

  @Override
  public void initComponent() {
    super.initComponent();

    StartupManager.getInstance(myProject).registerPostStartupActivity(new Runnable() {
      @Override
      public void run() {
        myRepositoryManager = GitUtil.getRepositoryManager(myProject);
        ((GitRepositoryManagerImpl)myRepositoryManager).updateRepositoriesCollection();
        myRepository = myRepositoryManager.getRepositories().get(0);

        myUiController = new UI_ControllerImpl(myProject);
        mySwingUi = new Swing_UI(myUiController);
        myUiController.addControllerListener(mySwingUi.getControllerListener());
        myUiController.setDragDropListener(mySwingUi.getDragDropListener());
        myUiController.init(false);

        final ToolWindow toolWindow = ToolWindowManager.getInstance(myProject).getToolWindow("Git Log");
        if (toolWindow != null && toolWindow.isActive()) {
          toolWindow.activate(null);
        }
      }
    });
  }

  public JPanel getMainGitAlkComponent() {
    return mySwingUi.getMainFrame().getMainComponent();
  }

  public GitRepository getRepository() {
    return myRepository;
  }

  public UI_ControllerImpl getUiController() {
    return myUiController;
  }
}

@@ -381,16 +381,66 @@
 sitory;%0A
+import git4idea.repo.GitRepositoryChangeListener;%0A
 import g
@@ -616,16 +616,58 @@
 lerImpl;
+%0Aimport org.jetbrains.annotations.NotNull;
 %0A%0Aimport
@@ -1984,24 +1984,305 @@
 it(false);%0A%0A
+        myProject.getMessageBus().connect().subscribe(GitRepository.GIT_REPO_CHANGE, new GitRepositoryChangeListener() %7B%0A          @Override%0A          public void repositoryChanged(@NotNull GitRepository repository) %7B%0A            myUiController.refresh();%0A          %7D%0A        %7D);%0A%0A
         fina

Refresh the log on external events