/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

/*
 * Created by IntelliJ IDEA.
 * User: yole
 * Date: 30.11.2006
 * Time: 18:12:47
 */
package com.intellij.openapi.vcs.changes.ui;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.CommittedChangesProvider;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.VcsListener;
import com.intellij.peer.PeerFactory;
import com.intellij.ui.content.Content;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CommittedChangesViewManager implements ProjectComponent {
  private ProjectLevelVcsManager myVcsManager;
  private ChangesViewContentManager myContentManager;
  private CommittedChangesPanel myComponent;
  private Content myContent;
  private Project myProject;
  private CommittedChangesProvider myProvider = null;
  private VcsListener myVcsListener = new MyVcsListener();

  public CommittedChangesViewManager(final Project project, final ChangesViewContentManager contentManager,
                                     final ProjectLevelVcsManager vcsManager) {
    myProject = project;
    myContentManager = contentManager;
    myVcsManager = vcsManager;
  }

  public void projectOpened() {
    updateChangesContent();
    myVcsManager.addVcsListener(myVcsListener);
  }

  public void projectClosed() {
    myVcsManager.removeVcsListener(myVcsListener);
  }

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

  public void initComponent() {
  }

  public void disposeComponent() {
  }

  private void updateChangesContent() {
    final AbstractVcs[] vcss = myVcsManager.getAllActiveVcss();
    List<AbstractVcs> vcsWithProviders = new ArrayList<AbstractVcs>();
    for(AbstractVcs vcs: vcss) {
      if (vcs.getCommittedChangesProvider() != null) {
        vcsWithProviders.add(vcs);
      }
    }
    if (vcsWithProviders.size() == 0) {
      myProvider = null;
    }
    else if (vcsWithProviders.size() == 1) {
      myProvider = vcsWithProviders.get(0).getCommittedChangesProvider();
    }
    else {
      myProvider = new CompositeCommittedChangesProvider(vcsWithProviders.toArray(new AbstractVcs[vcsWithProviders.size()]));
    }

    if (myProvider == null) {
      if (myContent != null) {
        myContentManager.removeContent(myContent);
      }
      myContent = null;
    }
    else {
      if (myContent == null) {
        if (myComponent == null) {
          myComponent = new CommittedChangesPanel(myProject, myProvider, myProvider.createDefaultSettings());
          myComponent.setMaxCount(50);
        }
        else {
          myComponent.setProvider(myProvider);
        }
        myContent = PeerFactory.getInstance().getContentFactory().createContent(myComponent, "Committed", false);
        myContent.setCloseable(false);
        myContentManager.addContent(myContent);
      }
    }
  }

  private class MyVcsListener implements VcsListener {
    public void directoryMappingChanged() {
      updateChangesContent();
    }
  }
}
@@ -502,24 +502,84 @@
 csListener;%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import com.i
@@ -3263,24 +3263,127 @@
 Changed() %7B%0A
+      ApplicationManager.getApplication().invokeLater(new Runnable() %7B%0A        public void run() %7B%0A    
       update
@@ -3396,23 +3396,43 @@
 sContent();%0A
+        %7D%0A      %7D);%0A
     %7D%0A  %7D%0A%7D

update in UI thread