コード例 #1
0
  @NotNull
  public static <T extends PsiElement> JBPopup getPsiElementPopup(
      @NotNull T[] elements,
      @NotNull final PsiElementListCellRenderer<T> renderer,
      @Nullable final String title,
      @NotNull final PsiElementProcessor<T> processor,
      @Nullable final T selection) {
    final JList list =
        new JBListWithHintProvider(elements) {
          @Nullable
          @Override
          protected PsiElement getPsiElementForHint(Object selectedValue) {
            return (PsiElement) selectedValue;
          }
        };
    list.setCellRenderer(renderer);

    list.setFont(EditorUtil.getEditorFont());

    if (selection != null) {
      list.setSelectedValue(selection, true);
    }

    final Runnable runnable =
        () -> {
          int[] ids = list.getSelectedIndices();
          if (ids == null || ids.length == 0) return;
          for (Object element : list.getSelectedValues()) {
            if (element != null) {
              processor.execute((T) element);
            }
          }
        };

    PopupChooserBuilder builder = new PopupChooserBuilder(list);
    if (title != null) {
      builder.setTitle(title);
    }
    renderer.installSpeedSearch(builder, true);

    JBPopup popup = builder.setItemChoosenCallback(runnable).createPopup();

    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);

    return popup;
  }
コード例 #2
0
  private void show(
      @NotNull final Project project,
      @NotNull Editor editor,
      @NotNull PsiFile file,
      @NotNull final GotoData gotoData) {
    final PsiElement[] targets = gotoData.targets;
    final List<AdditionalAction> additionalActions = gotoData.additionalActions;

    if (targets.length == 0 && additionalActions.isEmpty()) {
      HintManager.getInstance().showErrorHint(editor, getNotFoundMessage(project, editor, file));
      return;
    }

    if (targets.length == 1 && additionalActions.isEmpty()) {
      Navigatable descriptor =
          targets[0] instanceof Navigatable
              ? (Navigatable) targets[0]
              : EditSourceUtil.getDescriptor(targets[0]);
      if (descriptor != null && descriptor.canNavigate()) {
        navigateToElement(descriptor);
      }
      return;
    }

    for (PsiElement eachTarget : targets) {
      gotoData.renderers.put(eachTarget, createRenderer(gotoData, eachTarget));
    }

    final String name = ((PsiNamedElement) gotoData.source).getName();
    boolean finished = gotoData.listUpdaterTask == null || gotoData.listUpdaterTask.isFinished();
    final String title = getChooserTitle(gotoData.source, name, targets.length, finished);

    if (shouldSortTargets()) {
      Arrays.sort(targets, createComparator(gotoData.renderers, gotoData));
    }

    List<Object> allElements = new ArrayList<Object>(targets.length + additionalActions.size());
    Collections.addAll(allElements, targets);
    allElements.addAll(additionalActions);

    final JBListWithHintProvider list =
        new JBListWithHintProvider(new CollectionListModel<Object>(allElements)) {
          @Override
          protected PsiElement getPsiElementForHint(final Object selectedValue) {
            return selectedValue instanceof PsiElement ? (PsiElement) selectedValue : null;
          }
        };

    list.setFont(EditorUtil.getEditorFont());

    list.setCellRenderer(
        new DefaultListCellRenderer() {
          @Override
          public Component getListCellRendererComponent(
              JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value == null)
              return super.getListCellRendererComponent(
                  list, value, index, isSelected, cellHasFocus);
            if (value instanceof AdditionalAction) {
              return myActionElementRenderer.getListCellRendererComponent(
                  list, value, index, isSelected, cellHasFocus);
            }
            PsiElementListCellRenderer renderer = getRenderer(value, gotoData.renderers, gotoData);
            return renderer.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
          }
        });

    final Runnable runnable =
        () -> {
          int[] ids = list.getSelectedIndices();
          if (ids == null || ids.length == 0) return;
          Object[] selectedElements = list.getSelectedValues();
          for (Object element : selectedElements) {
            if (element instanceof AdditionalAction) {
              ((AdditionalAction) element).execute();
            } else {
              Navigatable nav =
                  element instanceof Navigatable
                      ? (Navigatable) element
                      : EditSourceUtil.getDescriptor((PsiElement) element);
              try {
                if (nav != null && nav.canNavigate()) {
                  navigateToElement(nav);
                }
              } catch (IndexNotReadyException e) {
                DumbService.getInstance(project)
                    .showDumbModeNotification("Navigation is not available while indexing");
              }
            }
          }
        };

    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setFilteringEnabled(
        o -> {
          if (o instanceof AdditionalAction) {
            return ((AdditionalAction) o).getText();
          }
          return getRenderer(o, gotoData.renderers, gotoData).getElementText((PsiElement) o);
        });

    final Ref<UsageView> usageView = new Ref<UsageView>();
    final JBPopup popup =
        builder
            .setTitle(title)
            .setItemChoosenCallback(runnable)
            .setMovable(true)
            .setCancelCallback(
                () -> {
                  HintUpdateSupply.hideHint(list);
                  final ListBackgroundUpdaterTask task = gotoData.listUpdaterTask;
                  if (task != null) {
                    task.cancelTask();
                  }
                  return true;
                })
            .setCouldPin(
                popup1 -> {
                  usageView.set(
                      FindUtil.showInUsageView(
                          gotoData.source,
                          gotoData.targets,
                          getFindUsagesTitle(gotoData.source, name, gotoData.targets.length),
                          project));
                  popup1.cancel();
                  return false;
                })
            .setAdText(getAdText(gotoData.source, targets.length))
            .createPopup();

    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);

    if (gotoData.listUpdaterTask != null) {
      gotoData.listUpdaterTask.init((AbstractPopup) popup, list, usageView);
      ProgressManager.getInstance().run(gotoData.listUpdaterTask);
    }
    popup.showInBestPositionFor(editor);
  }