static void subscribeTo(NavBarPanel panel) {
    if (panel.getClientProperty(LISTENER) != null) {
      unsubscribeFrom(panel);
    }

    final NavBarListener listener = new NavBarListener(panel);
    final Project project = panel.getProject();
    panel.putClientProperty(LISTENER, listener);
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(listener);
    FileStatusManager.getInstance(project).addFileStatusListener(listener);
    PsiManager.getInstance(project).addPsiTreeChangeListener(listener);
    WolfTheProblemSolver.getInstance(project).addProblemListener(listener);
    ActionManager.getInstance().addAnActionListener(listener);

    final MessageBusConnection connection = project.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, listener);
    connection.subscribe(NavBarModelListener.NAV_BAR, listener);
    connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, listener);
    panel.putClientProperty(BUS, connection);
    panel.addKeyListener(listener);

    if (panel.isInFloatingMode()) {
      final Window window = SwingUtilities.windowForComponent(panel);
      if (window != null) {
        window.addWindowFocusListener(listener);
      }
    }
  }
  @Override
  public void keyPressed(final KeyEvent e) {
    if (!(e.isAltDown() || e.isMetaDown() || e.isControlDown() || myPanel.isNodePopupActive())) {
      if (!Character.isLetter(e.getKeyChar())) {
        return;
      }

      final IdeFocusManager focusManager = IdeFocusManager.getInstance(myPanel.getProject());
      final ActionCallback firstCharTyped = new ActionCallback();
      focusManager.typeAheadUntil(firstCharTyped);
      myPanel.moveDown();
      //noinspection SSBasedInspection
      SwingUtilities.invokeLater(
          new Runnable() {
            public void run() {
              try {
                final Robot robot = new Robot();
                final boolean shiftOn = e.isShiftDown();
                final int code = e.getKeyCode();
                if (shiftOn) {
                  robot.keyPress(KeyEvent.VK_SHIFT);
                }
                robot.keyPress(code);
                robot.keyRelease(code);

                // don't release Shift
                firstCharTyped.setDone();
              } catch (AWTException ignored) {
              }
            }
          });
    }
  }
  public void focusLost(final FocusEvent e) {
    if (myPanel.getProject().isDisposed()) {
      myPanel.setContextComponent(null);
      myPanel.hideHint();
      return;
    }
    final DialogWrapper dialog = DialogWrapper.findInstance(e.getOppositeComponent());
    shouldFocusEditor = dialog != null;
    if (dialog != null) {
      Disposer.register(
          dialog.getDisposable(),
          new Disposable() {
            @Override
            public void dispose() {
              if (dialog.getExitCode() == DialogWrapper.CANCEL_EXIT_CODE) {
                shouldFocusEditor = false;
              }
            }
          });
    }

    // required invokeLater since in current call sequence KeyboardFocusManager is not initialized
    // yet
    // but future focused component
    //noinspection SSBasedInspection
    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            processFocusLost(e);
          }
        });
  }
 public void focusGained(final FocusEvent e) {
   if (e.getOppositeComponent() == null && shouldFocusEditor) {
     shouldFocusEditor = false;
     ToolWindowManager.getInstance(myPanel.getProject()).activateEditorComponent();
     return;
   }
   myPanel.updateItems();
   final List<NavBarItem> items = myPanel.getItems();
   if (!myPanel.isInFloatingMode() && items.size() > 0) {
     myPanel.setContextComponent(items.get(items.size() - 1));
   } else {
     myPanel.setContextComponent(null);
   }
 }
 static void unsubscribeFrom(NavBarPanel panel) {
   final NavBarListener listener = (NavBarListener) panel.getClientProperty(LISTENER);
   panel.putClientProperty(LISTENER, null);
   if (listener != null) {
     final Project project = panel.getProject();
     KeyboardFocusManager.getCurrentKeyboardFocusManager().removePropertyChangeListener(listener);
     FileStatusManager.getInstance(project).removeFileStatusListener(listener);
     PsiManager.getInstance(project).removePsiTreeChangeListener(listener);
     WolfTheProblemSolver.getInstance(project).removeProblemListener(listener);
     ActionManager.getInstance().removeAnActionListener(listener);
     final MessageBusConnection connection = (MessageBusConnection) panel.getClientProperty(BUS);
     panel.putClientProperty(BUS, null);
     if (connection != null) {
       connection.disconnect();
     }
   }
 }