protected Tree createTree() {
    final Tree tree = new Tree(new DefaultTreeModel(new DefaultMutableTreeNode()));

    tree.setCellRenderer(getTreeCellRenderer());
    UIUtil.setLineStyleAngled(tree);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.addKeyListener(new TreeKeyListener());
    tree.addTreeSelectionListener(new MyTreeSelectionListener());

    if (!myAllowMultiSelection) {
      tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    }

    new DoubleClickListener() {
      @Override
      protected boolean onDoubleClick(MouseEvent e) {
        if (tree.getPathForLocation(e.getX(), e.getY()) != null) {
          doOKAction();
          return true;
        }
        return false;
      }
    }.installOn(tree);

    TreeUtil.installActions(tree);
    return tree;
  }
  private JComponent createActionsPanel() {
    final JButton include = new JButton(IdeBundle.message("button.include"));
    final JButton includeRec = new JButton(IdeBundle.message("button.include.recursively"));
    final JButton exclude = new JButton(IdeBundle.message("button.exclude"));
    final JButton excludeRec = new JButton(IdeBundle.message("button.exclude.recursively"));
    myPackageTree
        .getSelectionModel()
        .addTreeSelectionListener(
            new TreeSelectionListener() {
              @Override
              public void valueChanged(TreeSelectionEvent e) {
                final boolean recursiveEnabled = isButtonEnabled(true, e.getPaths(), e);
                includeRec.setEnabled(recursiveEnabled);
                excludeRec.setEnabled(recursiveEnabled);

                final boolean nonRecursiveEnabled = isButtonEnabled(false, e.getPaths(), e);
                include.setEnabled(nonRecursiveEnabled);
                exclude.setEnabled(nonRecursiveEnabled);
              }
            });

    JPanel buttonsPanel = new JPanel(new VerticalFlowLayout());
    buttonsPanel.add(include);
    buttonsPanel.add(includeRec);
    buttonsPanel.add(exclude);
    buttonsPanel.add(excludeRec);

    include.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            includeSelected(false);
          }
        });
    includeRec.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            includeSelected(true);
          }
        });
    exclude.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            excludeSelected(false);
          }
        });
    excludeRec.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            excludeSelected(true);
          }
        });

    return buttonsPanel;
  }
  @Override
  protected JComponent createCenterPanel() {
    final DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode());
    myTree = new Tree(model);

    ProjectAbstractTreeStructureBase treeStructure =
        new AbstractProjectTreeStructure(myProject) {
          @Override
          public boolean isFlattenPackages() {
            return false;
          }

          @Override
          public boolean isShowMembers() {
            return myIsShowMembers;
          }

          @Override
          public boolean isHideEmptyMiddlePackages() {
            return true;
          }

          @Override
          public boolean isAbbreviatePackageNames() {
            return false;
          }

          @Override
          public boolean isShowLibraryContents() {
            return myIsShowLibraryContents;
          }

          @Override
          public boolean isShowModules() {
            return false;
          }
        };
    myBuilder =
        new ProjectTreeBuilder(myProject, myTree, model, AlphaComparator.INSTANCE, treeStructure);

    myTree.setRootVisible(false);
    myTree.setShowsRootHandles(true);
    myTree.expandRow(0);
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    myTree.setCellRenderer(new NodeRenderer());
    UIUtil.setLineStyleAngled(myTree);

    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTree);
    scrollPane.setPreferredSize(new Dimension(500, 300));

    myTree.addKeyListener(
        new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
              doOKAction();
            }
          }
        });

    new DoubleClickListener() {
      @Override
      protected boolean onDoubleClick(MouseEvent event) {
        TreePath path = myTree.getPathForLocation(event.getX(), event.getY());
        if (path != null && myTree.isPathSelected(path)) {
          doOKAction();
          return true;
        }
        return false;
      }
    }.installOn(myTree);

    myTree.addTreeSelectionListener(
        new TreeSelectionListener() {
          @Override
          public void valueChanged(TreeSelectionEvent e) {
            handleSelectionChanged();
          }
        });

    new TreeSpeedSearch(myTree);

    myTabbedPane = new TabbedPaneWrapper(getDisposable());

    final JPanel dummyPanel = new JPanel(new BorderLayout());
    String name = null;
    /*
        if (myInitialClass != null) {
          name = myInitialClass.getName();
        }
    */
    myGotoByNamePanel =
        new ChooseByNamePanel(
            myProject,
            createChooseByNameModel(),
            name,
            myScope.isSearchInLibraries(),
            getContext()) {

          @Override
          protected void showTextFieldPanel() {}

          @Override
          protected void close(boolean isOk) {
            super.close(isOk);

            if (isOk) {
              doOKAction();
            } else {
              doCancelAction();
            }
          }

          @NotNull
          @Override
          protected Set<Object> filter(@NotNull Set<Object> elements) {
            return doFilter(elements);
          }

          @Override
          protected void initUI(
              ChooseByNamePopupComponent.Callback callback,
              ModalityState modalityState,
              boolean allowMultipleSelection) {
            super.initUI(callback, modalityState, allowMultipleSelection);
            dummyPanel.add(myGotoByNamePanel.getPanel(), BorderLayout.CENTER);
            IdeFocusTraversalPolicy.getPreferredFocusedComponent(myGotoByNamePanel.getPanel())
                .requestFocus();
          }

          @Override
          protected void showList() {
            super.showList();
            if (myInitialClass != null && myList.getModel().getSize() > 0) {
              myList.setSelectedValue(myInitialClass, true);
              myInitialClass = null;
            }
          }

          @Override
          protected void chosenElementMightChange() {
            handleSelectionChanged();
          }
        };

    Disposer.register(myDisposable, myGotoByNamePanel);

    myTabbedPane.addTab(IdeBundle.message("tab.chooser.search.by.name"), dummyPanel);
    myTabbedPane.addTab(IdeBundle.message("tab.chooser.project"), scrollPane);

    myGotoByNamePanel.invoke(new MyCallback(), getModalityState(), false);

    myTabbedPane.addChangeListener(
        new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            handleSelectionChanged();
          }
        });

    return myTabbedPane.getComponent();
  }
  @NotNull
  private JTree createTree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    final Tree tree = new Tree(new DefaultTreeModel(root)) /* {
      @Override
      protected void paintComponent(Graphics g) {
        DuplicateNodeRenderer.paintDuplicateNodesBackground(g, this);
        super.paintComponent(g);
      }
    }*/;
    tree.setOpaque(false);

    tree.setToggleClickCount(-1);
    SliceUsageCellRenderer renderer = new SliceUsageCellRenderer();
    renderer.setOpaque(false);
    tree.setCellRenderer(renderer);
    UIUtil.setLineStyleAngled(tree);
    tree.setRootVisible(false);

    tree.setShowsRootHandles(true);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setSelectionPath(new TreePath(root.getPath()));
    // ActionGroup group =
    // (ActionGroup)ActionManager.getInstance().getAction(IdeActions.GROUP_METHOD_HIERARCHY_POPUP);
    // PopupHandler.installPopupHandler(tree, group, ActionPlaces.METHOD_HIERARCHY_VIEW_POPUP,
    // ActionManager.getInstance());
    EditSourceOnDoubleClickHandler.install(tree);

    new TreeSpeedSearch(tree);
    TreeUtil.installActions(tree);
    ToolTipManager.sharedInstance().registerComponent(tree);

    myAutoScrollToSourceHandler.install(tree);

    tree.getSelectionModel()
        .addTreeSelectionListener(
            new TreeSelectionListener() {
              @Override
              public void valueChanged(final TreeSelectionEvent e) {
                treeSelectionChanged();
              }
            });

    tree.addKeyListener(
        new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
              List<Navigatable> navigatables = getNavigatables();
              if (navigatables.isEmpty()) return;
              for (Navigatable navigatable : navigatables) {
                if (navigatable instanceof AbstractTreeNode
                    && ((AbstractTreeNode) navigatable).getValue() instanceof Usage) {
                  navigatable = (Usage) ((AbstractTreeNode) navigatable).getValue();
                }
                if (navigatable.canNavigateToSource()) {
                  navigatable.navigate(false);
                  if (navigatable instanceof Usage) {
                    ((Usage) navigatable).highlightInEditor();
                  }
                }
              }
              e.consume();
            }
          }
        });

    tree.addTreeWillExpandListener(
        new TreeWillExpandListener() {
          @Override
          public void treeWillCollapse(TreeExpansionEvent event) {}

          @Override
          public void treeWillExpand(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            SliceNode node = fromPath(path);
            node.calculateDupNode();
          }
        });

    return tree;
  }
  public MavenArchetypesStep(MavenModuleBuilder builder, @Nullable StepAdapter step) {
    myBuilder = builder;
    myStep = step;
    Disposer.register(this, myLoadingIcon);

    myArchetypesTree = new Tree();
    myArchetypesTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
    JScrollPane archetypesScrollPane = ScrollPaneFactory.createScrollPane(myArchetypesTree);

    myArchetypesPanel.add(archetypesScrollPane, "archetypes");

    JPanel loadingPanel = new JPanel(new GridBagLayout());
    JPanel bp = new JPanel(new BorderLayout(10, 10));
    bp.add(new JLabel("Loading archetype list..."), BorderLayout.NORTH);
    bp.add(myLoadingIcon, BorderLayout.CENTER);

    loadingPanel.add(bp, new GridBagConstraints());

    myArchetypesPanel.add(ScrollPaneFactory.createScrollPane(loadingPanel), "loading");
    ((CardLayout) myArchetypesPanel.getLayout()).show(myArchetypesPanel, "archetypes");

    myUseArchetypeCheckBox.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            updateComponents();
            archetypeMayBeChanged();
          }
        });

    myAddArchetypeButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            doAddArchetype();
          }
        });

    myArchetypesTree.setRootVisible(false);
    myArchetypesTree.setShowsRootHandles(true);
    myArchetypesTree.setCellRenderer(new MyRenderer());
    myArchetypesTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    myArchetypesTree
        .getSelectionModel()
        .addTreeSelectionListener(
            new TreeSelectionListener() {
              public void valueChanged(TreeSelectionEvent e) {
                updateArchetypeDescription();
                archetypeMayBeChanged();
              }
            });

    new TreeSpeedSearch(
            myArchetypesTree,
            new Convertor<TreePath, String>() {
              public String convert(TreePath path) {
                MavenArchetype info =
                    getArchetypeInfoFromPathComponent(path.getLastPathComponent());
                return info.groupId + ":" + info.artifactId + ":" + info.version;
              }
            })
        .setComparator(new SpeedSearchComparator(false));

    myArchetypeDescriptionField.setEditable(false);
    myArchetypeDescriptionField.setBackground(UIUtil.getPanelBackground());

    requestUpdate();
    updateComponents();
  }
  private void initializeUI() {
    myTree = new Tree();
    myTreeBuilder = new CertificateTreeBuilder(myTree);

    // are not fully functional by now
    myCheckHostname.setVisible(false);
    myCheckValidityPeriod.setVisible(false);

    myTrustManager = CertificateManager.getInstance().getCustomTrustManager();
    // show newly added certificates
    myTrustManager.addListener(this);

    myTree.getEmptyText().setText("No certificates");
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    myTree.setRootVisible(false);
    // myTree.setShowsRootHandles(false);

    ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myTree).disableUpDownActions();
    decorator
        .setAddAction(
            new AnActionButtonRunnable() {
              @Override
              public void run(AnActionButton button) {
                // show choose file dialog, add certificate
                FileChooser.chooseFile(
                    CERTIFICATE_DESCRIPTOR,
                    null,
                    null,
                    file -> {
                      String path = file.getPath();
                      X509Certificate certificate = CertificateUtil.loadX509Certificate(path);
                      if (certificate == null) {
                        Messages.showErrorDialog(
                            myRootPanel, "Malformed X509 server certificate", "Not Imported");
                      } else if (myCertificates.contains(certificate)) {
                        Messages.showWarningDialog(
                            myRootPanel, "Certificate already exists", "Not Imported");
                      } else {
                        myCertificates.add(certificate);
                        myTreeBuilder.addCertificate(certificate);
                        addCertificatePanel(certificate);
                        myTreeBuilder.selectCertificate(certificate);
                      }
                    });
              }
            })
        .setRemoveAction(
            new AnActionButtonRunnable() {
              @Override
              public void run(AnActionButton button) {
                // allow to delete several certificates at once
                for (X509Certificate certificate : myTreeBuilder.getSelectedCertificates(true)) {
                  myCertificates.remove(certificate);
                  myTreeBuilder.removeCertificate(certificate);
                }
                if (myCertificates.isEmpty()) {
                  showCard(EMPTY_PANEL);
                } else {
                  myTreeBuilder.selectFirstCertificate();
                }
              }
            });

    myTree.addTreeSelectionListener(
        new TreeSelectionListener() {
          @Override
          public void valueChanged(TreeSelectionEvent e) {
            X509Certificate certificate = myTreeBuilder.getFirstSelectedCertificate(true);
            if (certificate != null) {
              showCard(getCardName(certificate));
            }
          }
        });
    myCertificatesListPanel.add(decorator.createPanel(), BorderLayout.CENTER);
  }
  protected JComponent createCenterPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    myModel = new DefaultTreeModel(new DefaultMutableTreeNode());
    createTreeModel();
    myTree = new Tree(myModel);

    UIUtil.setLineStyleAngled(myTree);
    myTree.setCellRenderer(
        new DefaultTreeCellRenderer() {
          public Component getTreeCellRendererComponent(
              JTree tree,
              Object value,
              boolean sel,
              boolean expanded,
              boolean leaf,
              int row,
              boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            setIcon(expanded ? Icons.PACKAGE_OPEN_ICON : Icons.PACKAGE_ICON);

            if (value instanceof DefaultMutableTreeNode) {
              DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
              Object object = node.getUserObject();
              if (object instanceof PsiPackage) {
                String name = ((PsiPackage) object).getName();
                if (name != null && name.length() > 0) {
                  setText(name);
                } else {
                  setText(IdeBundle.message("node.default"));
                }
              }
            }
            return this;
          }
        });

    myTree.setBorder(BorderFactory.createEtchedBorder());
    JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myTree);
    scrollPane.setPreferredSize(new Dimension(500, 300));

    new TreeSpeedSearch(
        myTree,
        new Convertor<TreePath, String>() {
          public String convert(TreePath path) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            Object object = node.getUserObject();
            if (object instanceof PsiPackage) return ((PsiPackage) object).getName();
            else return "";
          }
        });

    myTree
        .getSelectionModel()
        .addTreeSelectionListener(
            new TreeSelectionListener() {
              public void valueChanged(TreeSelectionEvent e) {
                PsiPackage selection = getTreeSelection();
                if (selection != null) {
                  String name = selection.getQualifiedName();
                  setTitle(
                      myTitle
                          + " - "
                          + ("".equals(name) ? IdeBundle.message("node.default.package") : name));
                } else {
                  setTitle(myTitle);
                }
              }
            });

    panel.add(scrollPane, BorderLayout.CENTER);
    DefaultActionGroup group = createActionGroup(myTree);

    ActionToolbar toolBar =
        ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true);
    panel.add(toolBar.getComponent(), BorderLayout.NORTH);
    toolBar.getComponent().setAlignmentX(JComponent.LEFT_ALIGNMENT);

    return panel;
  }
    public ResourcePanel(AndroidFacet facet, ResourceType[] types, boolean system) {
      myTree = new Tree();
      myTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
      myTree.setScrollsOnExpand(true);
      myTree.setRootVisible(false);
      myTree.setShowsRootHandles(true);
      new DoubleClickListener() {
        @Override
        protected boolean onDoubleClick(MouseEvent e) {
          if (!myTreeBuilder.getSelectedElements(ResourceItem.class).isEmpty()) {
            close(OK_EXIT_CODE);
            return true;
          }
          return false;
        }
      }.installOn(myTree);

      ToolTipManager.sharedInstance().registerComponent(myTree);
      TreeUtil.installActions(myTree);

      myManager = facet.getResourceManager(system ? AndroidUtils.SYSTEM_RESOURCE_PACKAGE : null);
      myGroups = new ResourceGroup[types.length];

      for (int i = 0; i < types.length; i++) {
        myGroups[i] = new ResourceGroup(types[i], myManager);
      }

      myTreeBuilder =
          new AbstractTreeBuilder(
              myTree,
              (DefaultTreeModel) myTree.getModel(),
              new TreeContentProvider(myGroups),
              null);
      myTreeBuilder.initRootNode();

      TreeSelectionModel selectionModel = myTree.getSelectionModel();
      selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
      selectionModel.addTreeSelectionListener(ChooseResourceDialog.this);

      myTree.setCellRenderer(new NodeRenderer());
      new TreeSpeedSearch(myTree, TreeSpeedSearch.NODE_DESCRIPTOR_TOSTRING, true);

      myComponent = new JBSplitter(true, 0.8f);
      myComponent.setSplitterProportionKey("android.resource_dialog_splitter");

      myComponent.setFirstComponent(ScrollPaneFactory.createScrollPane(myTree));

      myPreviewPanel = new JPanel(new CardLayout());
      myComponent.setSecondComponent(myPreviewPanel);

      myTextArea = new JTextArea(5, 20);
      myTextArea.setEditable(false);
      myPreviewPanel.add(ScrollPaneFactory.createScrollPane(myTextArea), TEXT);

      myComboTextArea = new JTextArea(5, 20);
      myComboTextArea.setEditable(false);

      myComboBox = new JComboBox();
      myComboBox.setMaximumRowCount(15);
      myComboBox.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              java.util.List<ResourceElement> resources =
                  (java.util.List<ResourceElement>) myComboBox.getClientProperty(COMBO);
              myComboTextArea.setText(
                  getResourceElementValue(resources.get(myComboBox.getSelectedIndex())));
            }
          });

      JPanel comboPanel =
          new JPanel(
              new BorderLayout(0, 1) {
                @Override
                public void layoutContainer(Container target) {
                  super.layoutContainer(target);
                  Rectangle bounds = myComboBox.getBounds();
                  Dimension size = myComboBox.getPreferredSize();
                  size.width += 20;
                  myComboBox.setBounds(
                      (int) bounds.getMaxX() - size.width, bounds.y, size.width, size.height);
                }
              });
      comboPanel.add(ScrollPaneFactory.createScrollPane(myComboTextArea), BorderLayout.CENTER);
      comboPanel.add(myComboBox, BorderLayout.SOUTH);
      myPreviewPanel.add(comboPanel, COMBO);

      myImageComponent = new JLabel();
      myImageComponent.setHorizontalAlignment(SwingConstants.CENTER);
      myImageComponent.setVerticalAlignment(SwingConstants.CENTER);
      myPreviewPanel.add(myImageComponent, IMAGE);

      myNoPreviewComponent = new JLabel("No Preview");
      myNoPreviewComponent.setHorizontalAlignment(SwingConstants.CENTER);
      myNoPreviewComponent.setVerticalAlignment(SwingConstants.CENTER);
      myPreviewPanel.add(myNoPreviewComponent, NONE);
    }
  public GenerateByPatternDialog(Project project, PatternDescriptor[] descriptors) {
    super(project);
    myProject = project;
    setTitle("Generate by Pattern");
    setOKButtonText("Generate");

    myMap = new MultiMap<>();
    for (PatternDescriptor descriptor : descriptors) {
      myMap.putValue(descriptor.getParentId(), descriptor);
    }
    DefaultMutableTreeNode root = createNode(null);

    myTree = new SimpleTree() {};

    myTree.setRootVisible(false);
    myTree.setCellRenderer(
        new DefaultTreeCellRenderer() {
          @NotNull
          @Override
          public Component getTreeCellRendererComponent(
              @NotNull JTree tree,
              Object value,
              boolean sel,
              boolean expanded,
              boolean leaf,
              int row,
              boolean hasFocus) {
            Component component =
                super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

            Object object = ((DefaultMutableTreeNode) value).getUserObject();
            if (object instanceof PatternDescriptor) {
              setText(((PatternDescriptor) object).getName());
              setIcon(((PatternDescriptor) object).getIcon());
            }
            return component;
          }
        });

    myTree.setModel(new DefaultTreeModel(root));
    myTree
        .getSelectionModel()
        .addTreeSelectionListener(
            new TreeSelectionListener() {
              @Override
              public void valueChanged(@NotNull TreeSelectionEvent e) {
                update();
              }
            });
    myEditor = TemplateEditorUtil.createEditor(true, "");

    mySplitter.setFirstComponent(ScrollPaneFactory.createScrollPane(myTree));
    JPanel details = new JPanel(new BorderLayout());
    details.add(myEditor.getComponent(), BorderLayout.CENTER);
    mySplitter.setSecondComponent(details);
    mySplitter.setHonorComponentsMinimumSize(true);
    mySplitter.setShowDividerControls(true);

    myTree.setSelectionRow(0);

    init();
  }
 private DefaultMutableTreeNode getSelectedNode() {
   TreePath path = myTree.getSelectionModel().getSelectionPath();
   return path == null ? null : (DefaultMutableTreeNode) path.getLastPathComponent();
 }