@Nullable
 private AbstractTreeNode getSelectedNode() {
   final TreePath path = myTree.getSelectionPath();
   if (path != null) {
     Object component = path.getLastPathComponent();
     if (component instanceof DefaultMutableTreeNode) {
       component = ((DefaultMutableTreeNode) component).getUserObject();
       if (component instanceof FilteringTreeStructure.FilteringNode) {
         component = ((FilteringTreeStructure.FilteringNode) component).getDelegate();
         if (component instanceof AbstractTreeNode) {
           return (AbstractTreeNode) component;
         }
       }
     }
   }
   return null;
 }
  public void show() {
    // final long time = System.currentTimeMillis();
    JComponent panel = createCenterPanel();
    new MnemonicHelper().register(panel);
    boolean shouldSetWidth =
        DimensionService.getInstance().getSize(getDimensionServiceKey(), myProject) == null;
    myPopup =
        JBPopupFactory.getInstance()
            .createComponentPopupBuilder(panel, null)
            .setTitle(myTitle)
            .setResizable(true)
            .setModalContext(false)
            .setFocusable(true)
            .setMovable(true)
            .setBelongsToGlobalPopupStack(true)
            // .setCancelOnClickOutside(false) //for debug and snapshots
            .setCancelKeyEnabled(false)
            .setDimensionServiceKey(null, getDimensionServiceKey(), false)
            .setCancelCallback(
                new Computable<Boolean>() {
                  @Override
                  public Boolean compute() {
                    DimensionService.getInstance()
                        .setLocation(
                            getDimensionServiceKey(), myPopup.getLocationOnScreen(), myProject);
                    return true;
                  }
                })
            .createPopup();

    myTree.addTreeSelectionListener(
        new TreeSelectionListener() {
          @Override
          public void valueChanged(TreeSelectionEvent e) {
            if (myPopup.isVisible()) {
              final PopupUpdateProcessor updateProcessor =
                  myPopup.getUserData(PopupUpdateProcessor.class);
              if (updateProcessor != null) {
                final AbstractTreeNode node = getSelectedNode();
                updateProcessor.updatePopup(node);
              }
            }
          }
        });
    Disposer.register(myPopup, this);
    Disposer.register(
        myPopup,
        new Disposable() {
          @Override
          public void dispose() {
            if (!myTreeHasBuilt.isDone()) {
              myTreeHasBuilt.setRejected();
            }
          }
        });
    myTree.getEmptyText().setText("Loading...");
    final Point location =
        DimensionService.getInstance().getLocation(getDimensionServiceKey(), myProject);
    if (location != null) {
      myPopup.showInScreenCoordinates(myEditor.getContentComponent(), location);
    } else {
      myPopup.showCenteredInCurrentWindow(myProject);
    }

    ((AbstractPopup) myPopup).setShowHints(true);
    if (shouldSetWidth) {
      myPopup.setSize(new Dimension(myPreferredWidth + 10, myPopup.getSize().height));
    }

    IdeFocusManager.getInstance(myProject).requestFocus(myTree, true);
    ApplicationManager.getApplication()
        .executeOnPooledThread(
            new Runnable() {
              @Override
              public void run() {
                final AccessToken token =
                    ApplicationManager.getApplication().acquireReadActionLock();
                try {
                  myFilteringStructure.rebuild();
                } finally {
                  token.finish();
                }

                SwingUtilities.invokeLater(
                    new Runnable() {
                      @Override
                      public void run() {
                        myAbstractTreeBuilder
                            .queueUpdate()
                            .doWhenDone(
                                new Runnable() {
                                  @Override
                                  public void run() {
                                    myTreeHasBuilt.setDone();
                                    //noinspection SSBasedInspection
                                    SwingUtilities.invokeLater(
                                        new Runnable() {
                                          @Override
                                          public void run() {
                                            selectPsiElement(myInitialPsiElement);
                                          }
                                        });
                                  }
                                });
                      }
                    });
                installUpdater();
              }
            });
  }
  public FileStructurePopup(
      StructureViewModel structureViewModel,
      @Nullable Editor editor,
      Project project,
      @NotNull final Disposable auxDisposable,
      final boolean applySortAndFilter) {
    myProject = project;
    myEditor = editor;

    // Stop code analyzer to speedup EDT
    final DaemonCodeAnalyzer analyzer = DaemonCodeAnalyzer.getInstance(myProject);
    if (analyzer != null) {
      myDaemonUpdateEnabled = ((DaemonCodeAnalyzerImpl) analyzer).isUpdateByTimerEnabled();
      analyzer.setUpdateByTimerEnabled(false);
    } else {
      myDaemonUpdateEnabled = false;
    }

    IdeFocusManager.getInstance(myProject).typeAheadUntil(myTreeHasBuilt);
    myBaseTreeModel = structureViewModel;
    Disposer.register(this, auxDisposable);
    if (applySortAndFilter) {
      myTreeActionsOwner = new TreeStructureActionsOwner(myBaseTreeModel);
      myTreeModel = new TreeModelWrapper(structureViewModel, myTreeActionsOwner);
    } else {
      myTreeActionsOwner = null;
      myTreeModel = structureViewModel;
    }

    myTreeStructure =
        new SmartTreeStructure(project, myTreeModel) {
          public void rebuildTree() {
            if (ApplicationManager.getApplication().isUnitTestMode() || !myPopup.isDisposed()) {
              super.rebuildTree();
            }
          }

          public boolean isToBuildChildrenInBackground(final Object element) {
            return getRootElement() == element;
          }

          protected TreeElementWrapper createTree() {
            return new StructureViewComponent.StructureViewTreeElementWrapper(
                myProject, myModel.getRoot(), myModel);
          }

          @Override
          public String toString() {
            return "structure view tree structure(model=" + myTreeModel + ")";
          }
        };

    myTree =
        new FileStructureTree(
            myTreeStructure.getRootElement(), Registry.is("fast.tree.expand.in.structure.view"));

    myTree.setCellRenderer(
        new NodeRenderer() {
          @Override
          protected void doAppend(
              @NotNull @Nls String fragment,
              @NotNull SimpleTextAttributes attributes,
              boolean isMainText,
              boolean selected) {
            SpeedSearchUtil.appendFragmentsForSpeedSearch(
                myTree, fragment, attributes, selected, this);
          }

          @Override
          public void doAppend(
              @NotNull String fragment,
              @NotNull SimpleTextAttributes attributes,
              boolean selected) {
            SpeedSearchUtil.appendFragmentsForSpeedSearch(
                myTree, fragment, attributes, selected, this);
          }

          @Override
          public void doAppend(String fragment, boolean selected) {
            SpeedSearchUtil.appendFragmentsForSpeedSearch(
                myTree, fragment, SimpleTextAttributes.REGULAR_ATTRIBUTES, selected, this);
          }
        });

    mySpeedSearch = new MyTreeSpeedSearch();
    mySpeedSearch.setComparator(new SpeedSearchComparator(false, true));

    final FileStructurePopupFilter filter = new FileStructurePopupFilter();
    myFilteringStructure =
        new FilteringTreeStructure(
            filter, myTreeStructure, ApplicationManager.getApplication().isUnitTestMode());
    myAbstractTreeBuilder =
        new FilteringTreeBuilder(myTree, filter, myFilteringStructure, null) {
          @Override
          public void initRootNode() {}

          @Override
          protected boolean validateNode(Object child) {
            return StructureViewComponent.isValid(child);
          }

          @Override
          public void revalidateTree() {
            // myTree.revalidate();
            // myTree.repaint();
          }

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

    myTreeExpander = new DefaultTreeExpander(myTree);

    // myAbstractTreeBuilder.getUi().setPassthroughMode(true);
    myAbstractTreeBuilder.getUi().getUpdater().setDelay(1);
    myInitialPsiElement = getCurrentElement(getPsiFile(myProject));
    // myAbstractTreeBuilder.setCanYieldUpdate(true);
    Disposer.register(this, myAbstractTreeBuilder);
  }