<commit_before>package com.intellij.structuralsearch.plugin.ui;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.structuralsearch.SSRBundle;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.plugin.StructuralSearchPlugin;
import com.intellij.ui.*;
import com.intellij.ui.components.JBList;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.containers.Convertor;
import com.intellij.util.ui.tree.TreeUtil;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Apr 2, 2004
 * Time: 1:27:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExistingTemplatesComponent {
  private final Tree patternTree;
  private final DefaultTreeModel patternTreeModel;
  private final DefaultMutableTreeNode userTemplatesNode;
  private final JComponent panel;
  private final DefaultListModel historyModel;
  private final JList historyList;
  private final JComponent historyPanel;
  private DialogWrapper owner;
  private final Project project;

  private ExistingTemplatesComponent(Project project) {

    this.project = project;
    final DefaultMutableTreeNode root;
    patternTreeModel = new DefaultTreeModel(
      root = new DefaultMutableTreeNode(null)
    );

    DefaultMutableTreeNode parent = null;
    String lastCategory = null;
    LinkedList<Object> nodesToExpand = new LinkedList<Object>();

    final List<Configuration> predefined = StructuralSearchUtil.getPredefinedTemplates();
    for (final Configuration info : predefined) {
      final DefaultMutableTreeNode node = new DefaultMutableTreeNode(info);

      if (lastCategory == null || !lastCategory.equals(info.getCategory())) {
        if (info.getCategory().length() > 0) {
          root.add(parent = new DefaultMutableTreeNode(info.getCategory()));
          nodesToExpand.add(parent);
          lastCategory = info.getCategory();
        }
        else {
          root.add(node);
          continue;
        }
      }

      parent.add(node);
    }

    parent = new DefaultMutableTreeNode(SSRBundle.message("user.defined.category"));
    userTemplatesNode = parent;
    root.add(parent);
    nodesToExpand.add(parent);

    final ConfigurationManager configurationManager = StructuralSearchPlugin.getInstance(this.project).getConfigurationManager();
    if (configurationManager.getConfigurations() != null) {
      for (final Configuration config : configurationManager.getConfigurations()) {
        parent.add(new DefaultMutableTreeNode(config));
      }
    }

    patternTree = createTree(patternTreeModel);

    for (final Object aNodesToExpand : nodesToExpand) {
      patternTree.expandPath(
        new TreePath(new Object[]{root, aNodesToExpand})
      );
    }

    panel = ToolbarDecorator.createDecorator(patternTree)
      .setRemoveAction(new AnActionButtonRunnable() {
        @Override
        public void run(AnActionButton button) {
          final Object selection = patternTree.getLastSelectedPathComponent();
          if (!(selection instanceof DefaultMutableTreeNode)) {
            return;
          }
          final DefaultMutableTreeNode node = (DefaultMutableTreeNode)selection;
          if (!(node.getUserObject() instanceof Configuration)) {
            return;
          }
          final Configuration configuration = (Configuration)node.getUserObject();
          if (configuration.isPredefined()) {
            return;
          }
          patternTreeModel.removeNodeFromParent(node);
          configurationManager.removeConfiguration(configuration);
        }
      }).setRemoveActionUpdater(new AnActionButtonUpdater() {
        @Override
        public boolean isEnabled(AnActionEvent e) {
          final Object selection = patternTree.getLastSelectedPathComponent();
          if (selection instanceof DefaultMutableTreeNode) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode)selection;
            final Object userObject = node.getUserObject();
            if (userObject instanceof Configuration) {
              final Configuration configuration = (Configuration)userObject;
              return !configuration.isPredefined();
            }
          }
          return false;
        }
      }).createPanel();

      new JPanel(new BorderLayout());

    configureSelectTemplateAction(patternTree);

    historyModel = new DefaultListModel();
    historyPanel = new JPanel(new BorderLayout());
    historyPanel.add(
      BorderLayout.NORTH,
      new JLabel(SSRBundle.message("used.templates"))
    );
    Component view = historyList = new JBList(historyModel);
    historyPanel.add(
      BorderLayout.CENTER,
      ScrollPaneFactory.createScrollPane(view)
    );

    historyList.setCellRenderer(
      new ListCellRenderer()
    );

    historyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    new ListSpeedSearch(historyList);

    if (configurationManager.getHistoryConfigurations() != null) {<commit_msg>SSR: fix speed search in Used Templates History<commit_after>package com.intellij.structuralsearch.plugin.ui;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.structuralsearch.SSRBundle;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.plugin.StructuralSearchPlugin;
import com.intellij.ui.*;
import com.intellij.ui.components.JBList;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.containers.Convertor;
import com.intellij.util.ui.tree.TreeUtil;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Apr 2, 2004
 * Time: 1:27:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExistingTemplatesComponent {
  private final Tree patternTree;
  private final DefaultTreeModel patternTreeModel;
  private final DefaultMutableTreeNode userTemplatesNode;
  private final JComponent panel;
  private final DefaultListModel historyModel;
  private final JList historyList;
  private final JComponent historyPanel;
  private DialogWrapper owner;
  private final Project project;

  private ExistingTemplatesComponent(Project project) {

    this.project = project;
    final DefaultMutableTreeNode root;
    patternTreeModel = new DefaultTreeModel(
      root = new DefaultMutableTreeNode(null)
    );

    DefaultMutableTreeNode parent = null;
    String lastCategory = null;
    LinkedList<Object> nodesToExpand = new LinkedList<Object>();

    final List<Configuration> predefined = StructuralSearchUtil.getPredefinedTemplates();
    for (final Configuration info : predefined) {
      final DefaultMutableTreeNode node = new DefaultMutableTreeNode(info);

      if (lastCategory == null || !lastCategory.equals(info.getCategory())) {
        if (info.getCategory().length() > 0) {
          root.add(parent = new DefaultMutableTreeNode(info.getCategory()));
          nodesToExpand.add(parent);
          lastCategory = info.getCategory();
        }
        else {
          root.add(node);
          continue;
        }
      }

      parent.add(node);
    }

    parent = new DefaultMutableTreeNode(SSRBundle.message("user.defined.category"));
    userTemplatesNode = parent;
    root.add(parent);
    nodesToExpand.add(parent);

    final ConfigurationManager configurationManager = StructuralSearchPlugin.getInstance(this.project).getConfigurationManager();
    if (configurationManager.getConfigurations() != null) {
      for (final Configuration config : configurationManager.getConfigurations()) {
        parent.add(new DefaultMutableTreeNode(config));
      }
    }

    patternTree = createTree(patternTreeModel);

    for (final Object aNodesToExpand : nodesToExpand) {
      patternTree.expandPath(
        new TreePath(new Object[]{root, aNodesToExpand})
      );
    }

    panel = ToolbarDecorator.createDecorator(patternTree)
      .setRemoveAction(new AnActionButtonRunnable() {
        @Override
        public void run(AnActionButton button) {
          final Object selection = patternTree.getLastSelectedPathComponent();
          if (!(selection instanceof DefaultMutableTreeNode)) {
            return;
          }
          final DefaultMutableTreeNode node = (DefaultMutableTreeNode)selection;
          if (!(node.getUserObject() instanceof Configuration)) {
            return;
          }
          final Configuration configuration = (Configuration)node.getUserObject();
          if (configuration.isPredefined()) {
            return;
          }
          patternTreeModel.removeNodeFromParent(node);
          configurationManager.removeConfiguration(configuration);
        }
      }).setRemoveActionUpdater(new AnActionButtonUpdater() {
        @Override
        public boolean isEnabled(AnActionEvent e) {
          final Object selection = patternTree.getLastSelectedPathComponent();
          if (selection instanceof DefaultMutableTreeNode) {
            final DefaultMutableTreeNode node = (DefaultMutableTreeNode)selection;
            final Object userObject = node.getUserObject();
            if (userObject instanceof Configuration) {
              final Configuration configuration = (Configuration)userObject;
              return !configuration.isPredefined();
            }
          }
          return false;
        }
      }).createPanel();

      new JPanel(new BorderLayout());

    configureSelectTemplateAction(patternTree);

    historyModel = new DefaultListModel();
    historyPanel = new JPanel(new BorderLayout());
    historyPanel.add(
      BorderLayout.NORTH,
      new JLabel(SSRBundle.message("used.templates"))
    );
    Component view = historyList = new JBList(historyModel);
    historyPanel.add(
      BorderLayout.CENTER,
      ScrollPaneFactory.createScrollPane(view)
    );

    historyList.setCellRenderer(
      new ListCellRenderer()
    );

    historyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    new ListSpeedSearch(historyList, new Convertor<Object, String>() {
      @Override
      public String convert(Object o) {
        return o instanceof Configuration ? ((Configuration)o).getName() : o.toString();
      }
    });

    if (configurationManager.getHistoryConfigurations() != null) {
{'fraction_non_alphanumeric': 0.09046567274065893, 'fraction_numerical': 0.0020246640898214613, 'mean_word_length': 3.453688524590164, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941605', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2770, 'n_words': 605}