// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution.dashboard.tree;

import com.intellij.execution.dashboard.hyperlink.RunDashboardHyperlinkComponent;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * @author Konstantin Aleev
 */
public class RunDashboardTreeMouseListener extends RunDashboardLinkMouseListenerBase {
  @NotNull private final Tree myTree;

  public RunDashboardTreeMouseListener(@NotNull Tree tree) {
    myTree = tree;
  }

  @Override
  protected Object getAimedObject(MouseEvent e) {
    Object aimedObject = null;
    final TreePath path = myTree.getPathForLocation(e.getX(), e.getY());
    if (path != null) {
      DefaultMutableTreeNode treeNode = ObjectUtils.tryCast(path.getLastPathComponent(), DefaultMutableTreeNode.class);
      if (treeNode != null) {
        aimedObject = treeNode.getUserObject();
      }
    }
    return aimedObject;
  }

  @Override
  protected void repaintComponent(MouseEvent e) {
    TreePath path = myTree.getPathForLocation(e.getX(), e.getY());
    Rectangle bounds = path == null ? null : myTree.getPathBounds(path);
    if (bounds != null) {
      myTree.repaint(bounds);
    }
  }

  @Nullable
  @Override
  protected RunDashboardHyperlinkComponent getTagAt(@NotNull MouseEvent e) {
    final TreePath path = myTree.getPathForLocation(e.getX(), e.getY());
    if (path == null) return null;

    final Rectangle rectangle = myTree.getPathBounds(path);
    if (rectangle == null) return null;

    int dx = e.getX() - rectangle.x;
    final Object treeNode = path.getLastPathComponent();
    final int row = myTree.getRowForLocation(e.getX(), e.getY());
    boolean isLeaf = myTree.getModel().isLeaf(treeNode);

    Object tag = null;

    Component component = myTree.getCellRenderer().getTreeCellRendererComponent(myTree, treeNode, true, false, isLeaf, row, true);
    if (component instanceof ColoredTreeCellRenderer) {
      tag = ((ColoredTreeCellRenderer)component).getFragmentTagAt(dx);
    }
    else {
      component.setBounds(rectangle);
      component.doLayout();

      Point componentPoint = new Point(dx, 0);
      Component child = component.getComponentAt(componentPoint);
      if (child instanceof ColoredTreeCellRenderer) {
        Point childPoint = SwingUtilities.convertPoint(component, componentPoint, child);
        tag = ((ColoredTreeCellRenderer)child).getFragmentTagAt(childPoint.x);
      }
      else if (child instanceof JLabel) {
        tag = ((JLabel)child).getIcon();
      }
    }

    if (tag instanceof RunDashboardHyperlinkComponent) {
      return (RunDashboardHyperlinkComponent)tag;
    }
    return null;
  }
}


@@ -312,16 +312,63 @@
 nderer;%0A
+import com.intellij.ui.ExpandableItemsHandler;%0A
 import c
@@ -427,27 +427,32 @@
 ij.util.
-Object
+ui.tree.Tree
 Util
-s
 ;%0Aimport
@@ -557,56 +557,8 @@
 .*;%0A
-import javax.swing.tree.DefaultMutableTreeNode;%0A
 impo
@@ -958,61 +958,42 @@
     
-Object aimedObject = null;%0A    final TreePath path = 
+return TreeUtil.getLastUserObject(
 myTr
@@ -1037,505 +1037,690 @@
 Y())
+)
 ;%0A  
-  if (path != null) %7B%0A      DefaultMutableTreeNode treeNode = ObjectUtils.tryCast(path.getLastPathComponent(), DefaultMutableTreeNode.class
+%7D%0A%0A  @Override%0A  protected void repaintComponent(MouseEvent e) %7B%0A    ExpandableItemsHandler%3CInteger%3E handler = myTree.getExpandableItemsHandler(
 );%0A    
- 
- 
 if (
-treeNode != null) %7B%0A        aimedObject = treeNode.getUserObject();%0A      %7D%0A    %7D%0A    return aimedObject;%0A  %7D%0A%0A  @Override%0A  protected void repaintComponent(MouseEvent e) %7B%0A    TreePath path = myTree.getPathForLocation(e.getX(), e.getY());%0A    Rectangle bounds = path == null ? null : myTree.getPathBounds(path);%0A    if (bounds != null) %7B%0A  
+handler.isEnabled() && !handler.getExpandedItems().isEmpty()) %7B%0A      // Dispatch MOUSE_ENTERED in order to repaint ExpandableItemsHandler's tooltip component, since it ignores MOUSE_MOVE.%0A      myTree.dispatchEvent(new MouseEvent((Component)e.getSource(), MouseEvent.MOUSE_ENTERED, e.getWhen(), e.getModifiers(),%0A                                          e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger(), e.getButton()));%0A    %7D%0A    // Repaint all tree since nodes which cursor just leaved should be repaint too.%0A
     
@@ -1738,22 +1738,10 @@
 int(
-bounds);%0A    %7D
+);
 %0A  %7D

fix tree node's link repaint