/*
 * 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.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
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.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 {
  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 DebugProcess javaProcess =
        DebuggerManager.getInstance(session.getProject()).getDebugProcess(debugProcess.getProcessHandler());
      if (javaProcess instanceof DebugProcessImpl) {
        final DebugProcessImpl processImpl = (DebugProcessImpl)javaProcess;
        ApplicationManager.getApplication().invokeLater(() -> {
          final RunnerLayoutUi ui = session.getUI();
          final ClassesFilteredView classesFilteredView = new ClassesFilteredView(session);
          classesFilteredView.setActive(true);
          final Content content =
            ui.createContent("MemoryView", classesFilteredView, "Memory View",
                             AllIcons.Debugger.MemoryView.ToolWindowEnabled, null);
          content.setCloseable(false);
          content.setPinned(true);
          content.setShouldDisposeContent(true);

          final MemoryViewDebugProcessData data = new MemoryViewDebugProcessData(classesFilteredView);
          processImpl.putUserData(MemoryViewDebugProcessData.KEY, data);
          session.getUI().addContent(content, -1, PlaceInGrid.right, true);
        });
      }
    }

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

@@ -995,173 +995,8 @@
 ns;%0A
-import com.intellij.openapi.actionSystem.ActionGroup;%0Aimport com.intellij.openapi.actionSystem.ActionManager;%0Aimport com.intellij.openapi.actionSystem.ActionPlaces;%0A
 impo
@@ -1197,24 +1197,130 @@
 nt.Content;%0A
+import com.intellij.ui.content.ContentManagerAdapter;%0Aimport com.intellij.ui.content.ContentManagerEvent;%0A
 import com.i
@@ -1702,16 +1702,86 @@
 onent %7B%0A
+  private static final String MEMORY_VIEW_CONTENT_ID = %22MemoryView%22;%0A%0A
   public
@@ -2832,17 +2832,27 @@
 Content 
-c
+memoryViewC
 ontent =
@@ -2881,28 +2881,38 @@
 Content(
-%22MemoryView%22
+MEMORY_VIEW_CONTENT_ID
 , classe
@@ -3025,16 +3025,17 @@
  null);%0A
+%0A
         
@@ -3028,33 +3028,43 @@
 ll);%0A%0A          
-c
+memoryViewC
 ontent.setClosea
@@ -3077,33 +3077,43 @@
 lse);%0A          
-c
+memoryViewC
 ontent.setPinned
@@ -3130,17 +3130,27 @@
         
-c
+memoryViewC
 ontent.s
@@ -3393,17 +3393,27 @@
 Content(
-c
+memoryViewC
 ontent, 
@@ -3437,24 +3437,699 @@
 ight, true);
+%0A%0A          session.getUI().addListener(new ContentManagerAdapter() %7B%0A            @Override%0A            public void selectionChanged(ContentManagerEvent event) %7B%0A              final Content content = event.getContent();%0A              if (content == memoryViewContent) %7B%0A                if (ContentManagerEvent.ContentOperation.add.equals(event.getOperation())) %7B%0A                  classesFilteredView.setActive(true);%0A                %7D%0A                else if (ContentManagerEvent.ContentOperation.remove.equals(event.getOperation())) %7B%0A                  classesFilteredView.setActive(false);%0A                %7D%0A              %7D%0A            %7D%0A          %7D, classesFilteredView);
 %0A        %7D);
@@ -4287,16 +4287,17 @@
 er(() -%3E
+ 
 %7B%0A      
@@ -4371,28 +4371,38 @@
 Content(
-%22MemoryView%22
+MEMORY_VIEW_CONTENT_ID
 );%0A     

Disable tracking for new instances while Memory View is hidden