<commit_before>package com.intellij.ide.hierarchy.type;

import com.intellij.history.LocalHistory;
import com.intellij.history.LocalHistoryAction;
import com.intellij.ide.DeleteProvider;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.OccurenceNavigator;
import com.intellij.ide.OccurenceNavigatorSupport;
import com.intellij.ide.hierarchy.*;
import com.intellij.ide.util.DeleteHandler;
import com.intellij.ide.util.treeView.NodeDescriptor;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.IconLoader;
import com.intellij.pom.Navigatable;
import com.intellij.psi.*;
import com.intellij.psi.presentation.java.ClassPresentationUtil;
import com.intellij.ui.AutoScrollToSourceHandler;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.TreeSpeedSearch;
import com.intellij.ui.TreeToolTipHandler;
import com.intellij.util.Alarm;
import com.intellij.util.EditSourceOnDoubleClickHandler;
import com.intellij.util.ui.Tree;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import java.text.MessageFormat;
import java.util.*;
import java.util.List;

public final class TypeHierarchyBrowser extends HierarchyBrowserBase implements DataProvider, OccurenceNavigator, Disposable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.ide.hierarchy.type.TypeHierarchyBrowser");

  @NonNls private static final String HELP_ID = "reference.toolWindows.hierarchy";
  @NonNls
  static final String TYPE_HIERARCHY_BROWSER_ID = "TYPE_HIERARCHY_BROWSER_ID";

  private final Project myProject;
  private final Hashtable<String, HierarchyTreeBuilder> myBuilders = new Hashtable<String, HierarchyTreeBuilder>();
  private final Hashtable<String, JTree> myTrees = new Hashtable<String, JTree>();

  private final RefreshAction myRefreshAction = new RefreshAction();
  private final Alarm myAlarm = new Alarm(Alarm.ThreadToUse.SHARED_THREAD);
  private SmartPsiElementPointer mySmartPsiElementPointer;
  private boolean myIsInterface;
  private final CardLayout myCardLayout;
  private final JPanel myTreePanel;
  private String myCurrentViewName;

  private final AutoScrollToSourceHandler myAutoScrollToSourceHandler;
  private final MyDeleteProvider myDeleteElementProvider = new MyDeleteProvider();

  private boolean myCachedIsValidBase = false;

  private static final String TYPE_HIERARCHY_BROWSER_DATA_CONSTANT = "com.intellij.ide.hierarchy.type.TypeHierarchyBrowser";
  private final List<Runnable> myRunOnDisposeList = new ArrayList<Runnable>();
  private final HashMap<String, OccurenceNavigator> myOccurenceNavigators = new HashMap<String, OccurenceNavigator>();
  private static final OccurenceNavigator EMPTY_NAVIGATOR = new OccurenceNavigator() {
    public boolean hasNextOccurence() {
      return false;
    }

    public boolean hasPreviousOccurence() {
      return false;
    }

    public OccurenceInfo goNextOccurence() {
      return null;
    }

    public OccurenceInfo goPreviousOccurence() {
      return null;
    }

    public String getNextOccurenceActionName() {
      return "";
    }

    public String getPreviousOccurenceActionName() {
      return "";
    }
  };

  public TypeHierarchyBrowser(final Project project, final PsiClass psiClass) {
    myProject = project;

    myAutoScrollToSourceHandler = new AutoScrollToSourceHandler() {
      protected boolean isAutoScrollMode() {
        return HierarchyBrowserManager.getInstance(myProject).getState().IS_AUTOSCROLL_TO_SOURCE;
      }

      protected void setAutoScrollMode(final boolean state) {
        HierarchyBrowserManager.getInstance(myProject).getState().IS_AUTOSCROLL_TO_SOURCE = state;
      }
    };

    setHierarchyBase(psiClass);


    myCardLayout = new CardLayout();
    myTreePanel = new JPanel(myCardLayout);
    myTrees.put(TypeHierarchyTreeStructure.TYPE, createTree());
    myTrees.put(SupertypesHierarchyTreeStructure.TYPE, createTree());
    myTrees.put(SubtypesHierarchyTreeStructure.TYPE, createTree());
    final Enumeration<String> keys = myTrees.keys();
    while (keys.hasMoreElements()) {
      final String key = keys.nextElement();
      final JTree tree = myTrees.get(key);
      myOccurenceNavigators.put(key, new OccurenceNavigatorSupport(tree) {
        @Nullable
        protected Navigatable createDescriptorForNode(DefaultMutableTreeNode node) {
          final Object userObject = node.getUserObject();
          if (userObject instanceof String) return null;
          TypeHierarchyNodeDescriptor nodeDescriptor = (TypeHierarchyNodeDescriptor)userObject;
          final PsiElement psiElement = nodeDescriptor.getPsiClass();
          if (psiElement == null || !psiElement.isValid()) return null;
          PsiElement navigationElement = psiElement.getNavigationElement();
          return new OpenFileDescriptor(psiElement.getProject(), navigationElement.getContainingFile().getVirtualFile(),
                                        navigationElement.getTextOffset());
        }

        public String getNextOccurenceActionName() {
          return IdeBundle.message("hierarchy.type.next.occurence.name");
        }

        public String getPreviousOccurenceActionName() {
          return IdeBundle.message("hierarchy.type.prev.occurence.name");
        }
      });
      myTreePanel.add(new JScrollPane(tree), key);
    }

    buildUi(createToolbar(ActionPlaces.TYPE_HIERARCHY_VIEW_TOOLBAR, HELP_ID).getComponent(), myTreePanel);
  }

  public String getCurrentViewName() {
    return myCurrentViewName;
  }

  public boolean isInterface() {
    return myIsInterface;
  }

  private JTree createTree() {
    final Tree tree = new Tree(new DefaultTreeModel(new DefaultMutableTreeNode("")));
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    tree.setToggleClickCount(-1);
    tree.setCellRenderer(new HierarchyNodeRenderer());
    UIUtil.setLineStyleAngled(tree);
    EditSourceOnDoubleClickHandler.install(tree);
    ActionGroup group = (ActionGroup)ActionManager.getInstance().getAction(IdeActions.GROUP_TYPE_HIERARCHY_POPUP);
    PopupHandler.installPopupHandler(tree, group, ActionPlaces.TYPE_HIERARCHY_VIEW_POPUP, ActionManager.getInstance());

    myRefreshAction.registerShortcutOn(tree);
    myRunOnDisposeList.add(new Runnable() {<commit_msg>enable drag from type hierarchy view<commit_after>package com.intellij.ide.hierarchy.type;

import com.intellij.history.LocalHistory;
import com.intellij.history.LocalHistoryAction;
import com.intellij.ide.DeleteProvider;
import com.intellij.ide.IdeBundle;
import com.intellij.ide.OccurenceNavigator;
import com.intellij.ide.OccurenceNavigatorSupport;
import com.intellij.ide.dnd.DnDAction;
import com.intellij.ide.dnd.DnDDragStartBean;
import com.intellij.ide.dnd.DnDManager;
import com.intellij.ide.dnd.DnDSource;
import com.intellij.ide.dnd.aware.DnDAwareTree;
import com.intellij.ide.hierarchy.*;
import com.intellij.ide.projectView.impl.AbstractProjectViewPane;
import com.intellij.ide.util.DeleteHandler;
import com.intellij.ide.util.treeView.NodeDescriptor;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.Pair;
import com.intellij.pom.Navigatable;
import com.intellij.psi.*;
import com.intellij.psi.presentation.java.ClassPresentationUtil;
import com.intellij.ui.AutoScrollToSourceHandler;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.TreeSpeedSearch;
import com.intellij.ui.TreeToolTipHandler;
import com.intellij.util.Alarm;
import com.intellij.util.EditSourceOnDoubleClickHandler;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.text.MessageFormat;
import java.util.*;
import java.util.List;

public final class TypeHierarchyBrowser extends HierarchyBrowserBase implements DataProvider, OccurenceNavigator, Disposable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.ide.hierarchy.type.TypeHierarchyBrowser");

  @NonNls private static final String HELP_ID = "reference.toolWindows.hierarchy";
  @NonNls
  static final String TYPE_HIERARCHY_BROWSER_ID = "TYPE_HIERARCHY_BROWSER_ID";

  private final Project myProject;
  private final Hashtable<String, HierarchyTreeBuilder> myBuilders = new Hashtable<String, HierarchyTreeBuilder>();
  private final Hashtable<String, JTree> myTrees = new Hashtable<String, JTree>();

  private final RefreshAction myRefreshAction = new RefreshAction();
  private final Alarm myAlarm = new Alarm(Alarm.ThreadToUse.SHARED_THREAD);
  private SmartPsiElementPointer mySmartPsiElementPointer;
  private boolean myIsInterface;
  private final CardLayout myCardLayout;
  private final JPanel myTreePanel;
  private String myCurrentViewName;

  private final AutoScrollToSourceHandler myAutoScrollToSourceHandler;
  private final MyDeleteProvider myDeleteElementProvider = new MyDeleteProvider();

  private boolean myCachedIsValidBase = false;

  private static final String TYPE_HIERARCHY_BROWSER_DATA_CONSTANT = "com.intellij.ide.hierarchy.type.TypeHierarchyBrowser";
  private final List<Runnable> myRunOnDisposeList = new ArrayList<Runnable>();
  private final HashMap<String, OccurenceNavigator> myOccurenceNavigators = new HashMap<String, OccurenceNavigator>();
  private static final OccurenceNavigator EMPTY_NAVIGATOR = new OccurenceNavigator() {
    public boolean hasNextOccurence() {
      return false;
    }

    public boolean hasPreviousOccurence() {
      return false;
    }

    public OccurenceInfo goNextOccurence() {
      return null;
    }

    public OccurenceInfo goPreviousOccurence() {
      return null;
    }

    public String getNextOccurenceActionName() {
      return "";
    }

    public String getPreviousOccurenceActionName() {
      return "";
    }
  };

  public TypeHierarchyBrowser(final Project project, final PsiClass psiClass) {
    myProject = project;

    myAutoScrollToSourceHandler = new AutoScrollToSourceHandler() {
      protected boolean isAutoScrollMode() {
        return HierarchyBrowserManager.getInstance(myProject).getState().IS_AUTOSCROLL_TO_SOURCE;
      }

      protected void setAutoScrollMode(final boolean state) {
        HierarchyBrowserManager.getInstance(myProject).getState().IS_AUTOSCROLL_TO_SOURCE = state;
      }
    };

    setHierarchyBase(psiClass);


    myCardLayout = new CardLayout();
    myTreePanel = new JPanel(myCardLayout);
    myTrees.put(TypeHierarchyTreeStructure.TYPE, createTree());
    myTrees.put(SupertypesHierarchyTreeStructure.TYPE, createTree());
    myTrees.put(SubtypesHierarchyTreeStructure.TYPE, createTree());
    final Enumeration<String> keys = myTrees.keys();
    while (keys.hasMoreElements()) {
      final String key = keys.nextElement();
      final JTree tree = myTrees.get(key);
      myOccurenceNavigators.put(key, new OccurenceNavigatorSupport(tree) {
        @Nullable
        protected Navigatable createDescriptorForNode(DefaultMutableTreeNode node) {
          final Object userObject = node.getUserObject();
          if (userObject instanceof String) return null;
          TypeHierarchyNodeDescriptor nodeDescriptor = (TypeHierarchyNodeDescriptor)userObject;
          final PsiElement psiElement = nodeDescriptor.getPsiClass();
          if (psiElement == null || !psiElement.isValid()) return null;
          PsiElement navigationElement = psiElement.getNavigationElement();
          return new OpenFileDescriptor(psiElement.getProject(), navigationElement.getContainingFile().getVirtualFile(),
                                        navigationElement.getTextOffset());
        }

        public String getNextOccurenceActionName() {
          return IdeBundle.message("hierarchy.type.next.occurence.name");
        }

        public String getPreviousOccurenceActionName() {
          return IdeBundle.message("hierarchy.type.prev.occurence.name");
        }
      });
      myTreePanel.add(new JScrollPane(tree), key);
    }

    buildUi(createToolbar(ActionPlaces.TYPE_HIERARCHY_VIEW_TOOLBAR, HELP_ID).getComponent(), myTreePanel);
  }

  public String getCurrentViewName() {
    return myCurrentViewName;
  }

  public boolean isInterface() {
    return myIsInterface;
  }

  private JTree createTree() {
    final DnDAwareTree tree = new DnDAwareTree(new DefaultTreeModel(new DefaultMutableTreeNode("")));
    if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
      tree.enableDnd(this);
      DnDManager.getInstance().registerSource(new DnDSource() {
        public boolean canStartDragging(final DnDAction action, final Point dragOrigin) {
          return getSelectedElements().length > 0;
        }

        public DnDDragStartBean startDragging(final DnDAction action, final Point dragOrigin) {
          return new DnDDragStartBean(new AbstractProjectViewPane.TransferableWrapper(){
            public TreeNode[] getTreeNodes() {
              return tree.getSelectedNodes(TreeNode.class, null);
            }

            public PsiElement[] getPsiElements() {
              return getSelectedElements();
            }
          });
        }

        public Pair<Image, Point> createDraggedImage(final DnDAction action, final Point dragOrigin) {
          return null;
        }
        public void dragDropEnd() {}
        public void dropActionChanged(final int gestureModifiers) {}
      }, tree);
    }
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    tree.setToggleClickCount(-1);
    tree.setCellRenderer(new HierarchyNodeRenderer());
    UIUtil.setLineStyleAngled(tree);
    EditSourceOnDoubleClickHandler.install(tree);
    ActionGroup group = (ActionGroup)ActionManager.getInstance().getAction(IdeActions.GROUP_TYPE_HIERARCHY_POPUP);
    PopupHandler.installPopupHandler(tree, group, ActionPlaces.TYPE_HIERARCHY_VIEW_POPUP, ActionManager.getInstance());

    myRefreshAction.registerShortcutOn(tree);
    myRunOnDisposeList.add(new Runnable() {
{'fraction_non_alphanumeric': 0.08889037076553748, 'fraction_numerical': 0.00020005334755934917, 'mean_word_length': 4.629504504504505, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4263757', 'n_tokens_mistral': 4435, 'n_tokens_neox': 4348, 'n_words': 812}