/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.debugger.memory.component;

import com.intellij.debugger.DebuggerManager;
import com.intellij.debugger.engine.DebugProcess;
import com.intellij.debugger.engine.DebugProcessImpl;
import com.intellij.debugger.memory.ui.ClassesFilteredView;
import com.intellij.execution.ui.RunnerLayoutUi;
import com.intellij.execution.ui.layout.PlaceInGrid;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManagerAdapter;
import com.intellij.ui.content.ContentManagerEvent;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.xdebugger.XDebugProcess;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerManager;
import com.intellij.xdebugger.XDebuggerManagerListener;
import org.jetbrains.annotations.NotNull;

/**
 * @author Vitaliy.Bibaev
 */
public class MemoryViewComponent extends AbstractProjectComponent {
  private static final String MEMORY_VIEW_CONTENT_ID = "MemoryView";

  public MemoryViewComponent(@NotNull Project project) {
    super(project);
    final MessageBusConnection connection = project.getMessageBus().connect(project);
    connection.subscribe(XDebuggerManager.TOPIC, new MyDebuggerStatusChangedListener());
  }

  private static final class MyDebuggerStatusChangedListener implements XDebuggerManagerListener {
    @Override
    public void processStarted(@NotNull XDebugProcess debugProcess) {
      final XDebugSession session = debugProcess.getSession();
      final Project project = session.getProject();
      final DebugProcess javaProcess =
        DebuggerManager.getInstance(project).getDebugProcess(debugProcess.getProcessHandler());
      if (javaProcess instanceof DebugProcessImpl) {
        final DebugProcessImpl processImpl = (DebugProcessImpl)javaProcess;
        ApplicationManager.getApplication().invokeLater(() -> {
          if (project.isDisposed()) {
            return;
          }

          final InstancesTracker tracker = InstancesTracker.getInstance(project);
          final RunnerLayoutUi ui = session.getUI();
          final ClassesFilteredView classesFilteredView = new ClassesFilteredView(session, processImpl, tracker);
          classesFilteredView.setActive(true);
          final Content memoryViewContent =
            ui.createContent(MEMORY_VIEW_CONTENT_ID, classesFilteredView, "Memory View",
                             AllIcons.Debugger.MemoryView.Active, null);

          memoryViewContent.setCloseable(false);
          memoryViewContent.setPinned(true);
          memoryViewContent.setShouldDisposeContent(true);

          final MemoryViewDebugProcessData data = new MemoryViewDebugProcessData(classesFilteredView);
          processImpl.putUserData(MemoryViewDebugProcessData.KEY, data);
          ui.addContent(memoryViewContent, 0, PlaceInGrid.right, true);

          ui.addListener(new ContentManagerAdapter() {
            @Override
            public void selectionChanged(ContentManagerEvent event) {
              final Content content = event.getContent();
              if (content == memoryViewContent) {
                if (ContentManagerEvent.ContentOperation.add.equals(event.getOperation())) {
                  classesFilteredView.setActive(true);
                }
                else if (ContentManagerEvent.ContentOperation.remove.equals(event.getOperation())) {
                  classesFilteredView.setActive(false);
                }
              }
            }
          }, classesFilteredView);
        });
      }
    }

    @Override
    public void processStopped(@NotNull XDebugProcess debugProcess) {
      ApplicationManager.getApplication().invokeLater(() -> {
        final Content memoryView = debugProcess.getSession().getUI().findContent(MEMORY_VIEW_CONTENT_ID);
        if (memoryView != null) {
          memoryView.setIcon(AllIcons.Debugger.MemoryView.Inactive);
        }
      });
    }
  }
}

@@ -853,16 +853,122 @@
 edView;%0A
+import com.intellij.execution.process.ProcessAdapter;%0Aimport com.intellij.execution.process.ProcessEvent;%0A
 import c
@@ -2765,79 +2765,8 @@
 %3E %7B%0A
-          if (project.isDisposed()) %7B%0A            return;%0A          %7D%0A%0A
     
@@ -3014,55 +3014,8 @@
 r);%0A
-          classesFilteredView.setActive(true);%0A
     
@@ -3550,80 +3550,8 @@
 ta);
-%0A          ui.addContent(memoryViewContent, 0, PlaceInGrid.right, true);
 %0A%0A  
@@ -3653,22 +3653,18 @@
 oid 
-selectionChang
+contentAdd
 ed(C
@@ -3709,197 +3709,55 @@
     
-final Content content = event.getContent();%0A              if (content == memoryViewContent) %7B%0A                if (ContentManagerEvent.ContentOperation.add.equals(event.getOperation())) %7B%0A  
+if (event.getContent() == memoryViewContent) %7B%0A
     
@@ -3801,32 +3801,44 @@
 tive(true);%0A    
+          %7D%0A
             %7D%0A  
@@ -3827,32 +3827,33 @@
 %7D%0A            %7D%0A
+%0A
                 
@@ -3852,99 +3852,147 @@
     
-    else if (ContentManagerEvent.ContentOperation.remove.equals(event.getOperation())
+@Override%0A            public void contentRemoved(ContentManagerEvent event) %7B%0A              if (event.getContent() == memoryViewContent
 ) %7B%0A
-  
     
@@ -4045,26 +4045,8 @@
 e);%0A
-                %7D%0A
     
@@ -4106,24 +4106,25 @@
 dView);%0A
+%0A
         
 %7D);%0A    
@@ -4119,27 +4119,145 @@
     
-%7D
+  ui.addContent(memoryViewContent, 0, PlaceInGrid.right, true
 );%0A
+%0A 
       
-%7D%0A
+   processImpl.addProcessListener(new ProcessAdapter() %7B%0A  
     
-%7D%0A%0A
+  
     
@@ -4258,32 +4258,40 @@
       @Override%0A
+        
     public void 
@@ -4301,55 +4301,49 @@
 cess
-Stopped(@NotNull XDebugProcess debugProcess) %7B%0A
+Terminated(ProcessEvent event) %7B%0A        
     
@@ -4402,238 +4402,295 @@
  -%3E 
-%7B%0A        final Content memoryView = debugProcess.getSession().getUI().findContent(MEMORY_VIEW_CONTENT_ID);%0A        if (memoryView != null) %7B%0A          memoryView.setIcon(AllIcons.Debugger.MemoryView.Inactive
+memoryViewContent.setIcon(AllIcons.Debugger.MemoryView.Inactive));%0A              processImpl.removeProcessListener(this);%0A            %7D%0A          %7D);%0A        %7D, o -%3E project.isDisposed()
 );%0A    
+  %7D%0A
     %7D%0A
+%0A
     
-  %7D);
+@Override%0A    public void processStopped(@NotNull XDebugProcess debugProcess) %7B
 %0A   

Fix wrong transitions between memory view modes