private JComponent createActionLink(
     final String text, final String groupId, Icon icon, boolean focusListOnLeft) {
   final Ref<ActionLink> ref = new Ref<ActionLink>(null);
   AnAction action =
       new AnAction() {
         @Override
         public void actionPerformed(@NotNull AnActionEvent e) {
           ActionGroup configureGroup =
               (ActionGroup) ActionManager.getInstance().getAction(groupId);
           final PopupFactoryImpl.ActionGroupPopup popup =
               (PopupFactoryImpl.ActionGroupPopup)
                   JBPopupFactory.getInstance()
                       .createActionGroupPopup(
                           null,
                           new IconsFreeActionGroup(configureGroup),
                           e.getDataContext(),
                           JBPopupFactory.ActionSelectionAid.SPEEDSEARCH,
                           false,
                           ActionPlaces.WELCOME_SCREEN);
           popup.showUnderneathOfLabel(ref.get());
           UsageTrigger.trigger("welcome.screen." + groupId);
         }
       };
   ref.set(new ActionLink(text, icon, action));
   ref.get().setPaintUnderline(false);
   ref.get().setNormalColor(getLinkNormalColor());
   NonOpaquePanel panel = new NonOpaquePanel(new BorderLayout());
   panel.setBorder(JBUI.Borders.empty(4, 6, 4, 6));
   panel.add(ref.get());
   panel.add(createArrow(ref.get()), BorderLayout.EAST);
   installFocusable(panel, action, KeyEvent.VK_UP, KeyEvent.VK_DOWN, focusListOnLeft);
   return panel;
 }
 protected String addToHistoryInner(
     final TextRange textRange,
     final EditorEx editor,
     final boolean erase,
     final boolean preserveMarkup) {
   final Ref<String> ref = Ref.create("");
   final Runnable action =
       new Runnable() {
         public void run() {
           ref.set(addTextRangeToHistory(textRange, editor, preserveMarkup));
           if (erase) {
             editor
                 .getDocument()
                 .deleteString(textRange.getStartOffset(), textRange.getEndOffset());
           }
         }
       };
   if (erase) {
     ApplicationManager.getApplication().runWriteAction(action);
   } else {
     ApplicationManager.getApplication().runReadAction(action);
   }
   // always scroll to end on user input
   scrollHistoryToEnd();
   queueUiUpdate(true);
   return ref.get();
 }
  public boolean navigateSelectedElement() {
    final Ref<Boolean> succeeded = new Ref<Boolean>();
    final CommandProcessor commandProcessor = CommandProcessor.getInstance();
    commandProcessor.executeCommand(
        myProject,
        new Runnable() {
          public void run() {
            final AbstractTreeNode selectedNode = getSelectedNode();
            if (selectedNode != null) {
              if (selectedNode.canNavigateToSource()) {
                myPopup.cancel();
                selectedNode.navigate(true);
                succeeded.set(true);
              } else {
                succeeded.set(false);
              }
            } else {
              succeeded.set(false);
            }

            IdeDocumentHistory.getInstance(myProject).includeCurrentCommandAsNavigation();
          }
        },
        "Navigate",
        null);
    return succeeded.get();
  }
  private boolean disposeSelf() {
    myDisposeInProgress = true;
    final CommandProcessor commandProcessor = CommandProcessor.getInstance();
    final Ref<Boolean> canClose = new Ref<Boolean>(Boolean.TRUE);
    for (final Project project : ProjectManagerEx.getInstanceEx().getOpenProjects()) {
      try {
        commandProcessor.executeCommand(
            project,
            new Runnable() {
              public void run() {
                canClose.set(ProjectUtil.closeAndDispose(project));
              }
            },
            ApplicationBundle.message("command.exit"),
            null);
      } catch (Throwable e) {
        LOG.error(e);
      }
      if (!canClose.get()) {
        myDisposeInProgress = false;
        return false;
      }
    }
    Disposer.dispose(this);

    Disposer.assertIsEmpty();
    return true;
  }
 public <T> T runWriteAction(@NotNull final Computable<T> computation) {
   final Ref<T> ref = Ref.create(null);
   runWriteAction(
       new Runnable() {
         public void run() {
           ref.set(computation.compute());
         }
       });
   return ref.get();
 }
 @Override
 public <T> T commitAndRunReadAction(@NotNull final Computable<T> computation) {
   final Ref<T> ref = Ref.create(null);
   commitAndRunReadAction(
       new Runnable() {
         @Override
         public void run() {
           ref.set(computation.compute());
         }
       });
   return ref.get();
 }
 private static TextRange adjustRange(final PsiElement element, final TextRange originalRange) {
   final Ref<TextRange> rangeRef = new Ref<TextRange>(originalRange);
   element.accept(
       new JavaRecursiveElementVisitor() {
         @Override
         public void visitExpressionStatement(final PsiExpressionStatement statement) {
           final TextRange stRange = statement.getTextRange();
           if (originalRange.intersects(stRange)) {
             final TextRange currentRange = rangeRef.get();
             final int start = Math.min(currentRange.getStartOffset(), stRange.getStartOffset());
             final int end = Math.max(currentRange.getEndOffset(), stRange.getEndOffset());
             rangeRef.set(new TextRange(start, end));
           }
         }
       });
   return rangeRef.get();
 }
    public SwitchTarget getCurrentTarget() {
      TabInfo selected = myTabs.getSelectedInfo();
      final Ref<SwitchTarget> targetRef = new Ref<SwitchTarget>();
      new AwtVisitor(selected.getComponent()) {
        @Override
        public boolean visit(Component component) {
          if (component instanceof JBTabs) {
            JBTabs tabs = (JBTabs) component;
            if (tabs != myTabs) {
              targetRef.set(tabs.getCurrentTarget());
              return true;
            }
          }
          return false;
        }
      };

      return targetRef.get();
    }
 public void openFiles() {
   if (mySplittersElement != null) {
     Ref<EditorWindow> currentWindow = new Ref<EditorWindow>();
     final JPanel comp = readExternalPanel(mySplittersElement, getTopPanel(), currentWindow);
     if (comp != null) {
       removeAll();
       add(comp, BorderLayout.CENTER);
       mySplittersElement = null;
     }
     // clear empty splitters
     for (EditorWindow window : getWindows()) {
       if (window.getEditors().length == 0) {
         for (EditorWindow sibling : window.findSiblings()) {
           sibling.unsplit(false);
         }
       }
     }
     if (!currentWindow.isNull()) {
       setCurrentWindow(currentWindow.get(), true);
     }
   }
 }
 @Override
 protected JPanel processSplitter(
     @NotNull Element splitterElement,
     Element firstChild,
     Element secondChild,
     final JPanel context) {
   if (context == null) {
     final boolean orientation =
         "vertical".equals(splitterElement.getAttributeValue("split-orientation"));
     final float proportion =
         Float.valueOf(splitterElement.getAttributeValue("split-proportion")).floatValue();
     final JPanel firstComponent = process(firstChild, null);
     final JPanel secondComponent = process(secondChild, null);
     final Ref<JPanel> panelRef = new Ref<JPanel>();
     UIUtil.invokeAndWaitIfNeeded(
         new Runnable() {
           @Override
           public void run() {
             JPanel panel = new JPanel(new BorderLayout());
             panel.setOpaque(false);
             Splitter splitter = new OnePixelSplitter(orientation, proportion, 0.1f, 0.9f);
             panel.add(splitter, BorderLayout.CENTER);
             splitter.setFirstComponent(firstComponent);
             splitter.setSecondComponent(secondComponent);
             panelRef.set(panel);
           }
         });
     return panelRef.get();
   }
   final Ref<JPanel> firstComponent = new Ref<JPanel>();
   final Ref<JPanel> secondComponent = new Ref<JPanel>();
   UIUtil.invokeAndWaitIfNeeded(
       new Runnable() {
         @Override
         public void run() {
           if (context.getComponent(0) instanceof Splitter) {
             Splitter splitter = (Splitter) context.getComponent(0);
             firstComponent.set((JPanel) splitter.getFirstComponent());
             secondComponent.set((JPanel) splitter.getSecondComponent());
           } else {
             firstComponent.set(context);
             secondComponent.set(context);
           }
         }
       });
   process(firstChild, firstComponent.get());
   process(secondChild, secondComponent.get());
   return context;
 }
  public static Pair<JPanel, JBList> createActionGroupPanel(
      ActionGroup action, final JComponent parent, final Runnable backAction) {
    JPanel actionsListPanel = new JPanel(new BorderLayout());
    actionsListPanel.setBackground(getProjectsBackground());
    final JBList list = new JBList(action.getChildren(null));
    list.setBackground(getProjectsBackground());
    list.installCellRenderer(
        new NotNullFunction<AnAction, JComponent>() {
          final JLabel label = new JLabel();

          {
            label.setBorder(JBUI.Borders.empty(3, 7));
          }

          @NotNull
          @Override
          public JComponent fun(AnAction action) {
            label.setText(action.getTemplatePresentation().getText());
            Icon icon = action.getTemplatePresentation().getIcon();
            label.setIcon(icon);
            return label;
          }
        });
    JScrollPane pane = ScrollPaneFactory.createScrollPane(list, true);
    pane.setBackground(getProjectsBackground());
    actionsListPanel.add(pane, BorderLayout.CENTER);
    if (backAction != null) {
      final JLabel back = new JLabel(AllIcons.Actions.Back);
      back.setBorder(JBUI.Borders.empty(3, 7, 10, 7));
      back.setHorizontalAlignment(SwingConstants.LEFT);
      new ClickListener() {
        @Override
        public boolean onClick(@NotNull MouseEvent event, int clickCount) {
          backAction.run();
          return true;
        }
      }.installOn(back);
      actionsListPanel.add(back, BorderLayout.SOUTH);
    }
    final Ref<Component> selected = Ref.create();
    final JPanel main = new JPanel(new BorderLayout());
    main.add(actionsListPanel, BorderLayout.WEST);

    ListSelectionListener selectionListener =
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
              // Update when a change has been finalized.
              // For instance, selecting an element with mouse fires two consecutive
              // ListSelectionEvent events.
              return;
            }
            if (!selected.isNull()) {
              main.remove(selected.get());
            }
            Object value = list.getSelectedValue();
            if (value instanceof AbstractActionWithPanel) {
              JPanel panel = ((AbstractActionWithPanel) value).createPanel();
              panel.setBorder(JBUI.Borders.empty(7, 10));
              selected.set(panel);
              main.add(selected.get());

              for (JButton button : UIUtil.findComponentsOfType(main, JButton.class)) {
                if (button.getClientProperty(DialogWrapper.DEFAULT_ACTION) == Boolean.TRUE) {
                  parent.getRootPane().setDefaultButton(button);
                  break;
                }
              }

              main.revalidate();
              main.repaint();
            }
          }
        };
    list.addListSelectionListener(selectionListener);
    if (backAction != null) {
      new AnAction() {
        @Override
        public void actionPerformed(@NotNull AnActionEvent e) {
          backAction.run();
        }
      }.registerCustomShortcutSet(KeyEvent.VK_ESCAPE, 0, main);
    }
    return Pair.create(main, list);
  }
    @Override
    protected JPanel processFiles(@NotNull List<Element> fileElements, final JPanel context) {
      final Ref<EditorWindow> windowRef = new Ref<EditorWindow>();
      UIUtil.invokeAndWaitIfNeeded(
          new Runnable() {
            @Override
            public void run() {
              windowRef.set(context == null ? createEditorWindow() : findWindowWith(context));
            }
          });
      final EditorWindow window = windowRef.get();
      LOG.assertTrue(window != null);
      VirtualFile focusedFile = null;

      for (int i = 0; i < fileElements.size(); i++) {
        final Element file = fileElements.get(i);
        if (i == 0) {
          EditorTabbedContainer tabbedPane = window.getTabbedPane();
          if (tabbedPane != null) {
            try {
              int limit =
                  Integer.parseInt(
                      file.getParentElement()
                          .getAttributeValue(
                              JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY.toString(),
                              String.valueOf(JBTabsImpl.DEFAULT_MAX_TAB_WIDTH)));
              UIUtil.putClientProperty(
                  tabbedPane.getComponent(), JBTabsImpl.SIDE_TABS_SIZE_LIMIT_KEY, limit);
            } catch (NumberFormatException e) {
              // ignore
            }
          }
        }
        try {
          final FileEditorManagerImpl fileEditorManager = getManager();
          Element historyElement = file.getChild(HistoryEntry.TAG);
          final HistoryEntry entry =
              HistoryEntry.createLight(fileEditorManager.getProject(), historyElement);
          final VirtualFile virtualFile = entry.getFile();
          if (virtualFile == null)
            throw new InvalidDataException("No file exists: " + entry.getFilePointer().getUrl());
          Document document =
              ApplicationManager.getApplication()
                  .runReadAction(
                      new Computable<Document>() {
                        @Override
                        public Document compute() {
                          return virtualFile.isValid()
                              ? FileDocumentManager.getInstance().getDocument(virtualFile)
                              : null;
                        }
                      });
          final boolean isCurrentInTab =
              Boolean.valueOf(file.getAttributeValue(CURRENT_IN_TAB)).booleanValue();
          Boolean pin = Boolean.valueOf(file.getAttributeValue(PINNED));
          fileEditorManager.openFileImpl4(
              window, virtualFile, entry, isCurrentInTab, isCurrentInTab, pin, i);
          if (isCurrentInTab) {
            focusedFile = virtualFile;
          }
          if (document != null) {
            // This is just to make sure document reference is kept on stack till this point
            // so that document is available for folding state deserialization in HistoryEntry
            // constructor
            // and that document will be created only once during file opening
            document.putUserData(DUMMY_KEY, null);
          }
          updateProgress();
        } catch (InvalidDataException e) {
          if (ApplicationManager.getApplication().isUnitTestMode()) {
            LOG.error(e);
          }
        }
      }
      if (focusedFile != null) {
        getManager().addSelectionRecord(focusedFile, window);
      }
      return window.myPanel;
    }
  private static Trinity<PsiElement, TextRange, Value> getSelectedExpression(
      final Project project, final Editor editor, final Point point, final ValueHintType type) {
    final Ref<PsiElement> selectedExpression = Ref.create(null);
    final Ref<TextRange> currentRange = Ref.create(null);
    final Ref<Value> preCalculatedValue = Ref.create(null);

    PsiDocumentManager.getInstance(project)
        .commitAndRunReadAction(
            new Runnable() {
              public void run() {
                // Point -> offset
                final int offset = calculateOffset(editor, point);

                PsiFile psiFile =
                    PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());

                if (psiFile == null || !psiFile.isValid()) return;

                int selectionStart = editor.getSelectionModel().getSelectionStart();
                int selectionEnd = editor.getSelectionModel().getSelectionEnd();

                if ((type == ValueHintType.MOUSE_CLICK_HINT
                        || type == ValueHintType.MOUSE_ALT_OVER_HINT)
                    && (selectionStart <= offset && offset <= selectionEnd)) {
                  PsiElement ctx =
                      (selectionStart > 0)
                          ? psiFile.findElementAt(selectionStart - 1)
                          : psiFile.findElementAt(selectionStart);
                  try {
                    String text = editor.getSelectionModel().getSelectedText();
                    if (text != null && ctx != null) {
                      selectedExpression.set(
                          JVMElementFactories.getFactory(ctx.getLanguage(), project)
                              .createExpressionFromText(text, ctx));
                      currentRange.set(
                          new TextRange(
                              editor.getSelectionModel().getSelectionStart(),
                              editor.getSelectionModel().getSelectionEnd()));
                    }
                  } catch (IncorrectOperationException ignored) {
                  }
                }

                if (currentRange.get() == null) {
                  PsiElement elementAtCursor = psiFile.findElementAt(offset);
                  if (elementAtCursor == null) {
                    return;
                  }
                  Pair<PsiElement, TextRange> pair =
                      findExpression(
                          elementAtCursor,
                          type == ValueHintType.MOUSE_CLICK_HINT
                              || type == ValueHintType.MOUSE_ALT_OVER_HINT);
                  if (pair == null) {
                    if (type == ValueHintType.MOUSE_OVER_HINT) {
                      final DebuggerSession debuggerSession =
                          DebuggerManagerEx.getInstanceEx(project)
                              .getContext()
                              .getDebuggerSession();
                      if (debuggerSession != null && debuggerSession.isPaused()) {
                        final Pair<Method, Value> lastExecuted =
                            debuggerSession.getProcess().getLastExecutedMethod();
                        if (lastExecuted != null) {
                          final Method method = lastExecuted.getFirst();
                          if (method != null) {
                            final Pair<PsiElement, TextRange> expressionPair =
                                findExpression(elementAtCursor, true);
                            if (expressionPair != null
                                && expressionPair.getFirst() instanceof PsiMethodCallExpression) {
                              final PsiMethodCallExpression methodCallExpression =
                                  (PsiMethodCallExpression) expressionPair.getFirst();
                              final PsiMethod psiMethod = methodCallExpression.resolveMethod();
                              if (psiMethod != null) {
                                final JVMName jvmSignature = JVMNameUtil.getJVMSignature(psiMethod);
                                try {
                                  if (method.name().equals(psiMethod.getName())
                                      && method
                                          .signature()
                                          .equals(
                                              jvmSignature.getName(debuggerSession.getProcess()))) {
                                    pair = expressionPair;
                                    preCalculatedValue.set(lastExecuted.getSecond());
                                  }
                                } catch (EvaluateException ignored) {
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                  if (pair == null) {
                    return;
                  }
                  selectedExpression.set(pair.getFirst());
                  currentRange.set(pair.getSecond());
                }
              }
            });
    return Trinity.create(selectedExpression.get(), currentRange.get(), preCalculatedValue.get());
  }