Exemple #1
0
    public void navigate(final Project project) {
      DefaultPsiElementListCellRenderer renderer = new DefaultPsiElementListCellRenderer();

      final JList list = new JList(myPsiFiles);
      list.setCellRenderer(renderer);

      renderer.installSpeedSearch(list);

      final Runnable runnable =
          new Runnable() {
            public void run() {
              int[] ids = list.getSelectedIndices();
              if (ids == null || ids.length == 0) return;
              Object[] selectedElements = list.getSelectedValues();
              for (Object element : selectedElements) {
                Navigatable descriptor = EditSourceUtil.getDescriptor((PsiElement) element);
                if (descriptor != null && descriptor.canNavigate()) {
                  descriptor.navigate(true);
                }
              }
            }
          };

      final Editor editor =
          PlatformDataKeys.EDITOR.getData(DataManager.getInstance().getDataContext());

      new PopupChooserBuilder(list)
          .setTitle("Choose file")
          .setItemChoosenCallback(runnable)
          .createPopup()
          .showInBestPositionFor(editor);
    }
  private static boolean doChooseLanguageToInject(
      Editor editor, final Processor<Injectable> onChosen) {
    final List<Injectable> injectables = getAllInjectables();

    final JList list = new JBList(injectables);
    list.setCellRenderer(
        new ColoredListCellRendererWrapper<Injectable>() {
          @Override
          protected void doCustomize(
              JList list, Injectable language, int index, boolean selected, boolean hasFocus) {
            setIcon(language.getIcon());
            append(language.getDisplayName());
            String description = language.getAdditionalDescription();
            if (description != null) {
              append(description, SimpleTextAttributes.GRAYED_ATTRIBUTES);
            }
          }
        });
    JBPopup popup =
        new PopupChooserBuilder(list)
            .setItemChoosenCallback(
                new Runnable() {
                  public void run() {
                    Injectable value = (Injectable) list.getSelectedValue();
                    onChosen.process(value);
                    PropertiesComponent.getInstance()
                        .setValue(LAST_INJECTED_LANGUAGE, value.getId());
                  }
                })
            .setFilteringEnabled(
                new Function<Object, String>() {
                  @Override
                  public String fun(Object language) {
                    return ((Injectable) language).getDisplayName();
                  }
                })
            .createPopup();
    final String lastInjected = PropertiesComponent.getInstance().getValue(LAST_INJECTED_LANGUAGE);
    if (lastInjected != null) {
      Injectable injectable =
          ContainerUtil.find(
              injectables,
              new Condition<Injectable>() {
                @Override
                public boolean value(Injectable injectable) {
                  return lastInjected.equals(injectable.getId());
                }
              });
      list.setSelectedValue(injectable, true);
    }
    popup.showInBestPositionFor(editor);
    return true;
  }
  private static void chooseAndPerform(
      Editor editor, List<TestFramework> frameworks, final Consumer<TestFramework> consumer) {
    if (frameworks.size() == 1) {
      consumer.consume(frameworks.get(0));
      return;
    }

    final JList list = new JBList(frameworks.toArray(new TestFramework[frameworks.size()]));
    list.setCellRenderer(
        new DefaultListCellRenderer() {
          @Override
          public Component getListCellRendererComponent(
              JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component result =
                super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
            if (value == null) return result;
            TestFramework framework = (TestFramework) value;

            setIcon(framework.getIcon());
            setText(framework.getName());

            return result;
          }
        });

    PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setFilteringEnabled(
        new Function<Object, String>() {
          @Override
          public String fun(Object o) {
            return ((TestFramework) o).getName();
          }
        });

    builder
        .setTitle("Choose Framework")
        .setItemChoosenCallback(
            new Runnable() {
              @Override
              public void run() {
                consumer.consume((TestFramework) list.getSelectedValue());
              }
            })
        .setMovable(true)
        .createPopup()
        .showInBestPositionFor(editor);
  }
 public void applyFix(
     @NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
   final PsiElement element =
       PsiTreeUtil.getParentOfType(
           descriptor.getPsiElement(), PsiJavaCodeReferenceElement.class);
   if (element instanceof PsiJavaCodeReferenceElement) {
     final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) element;
     Collections.sort(
         myClassesToImport, new PsiProximityComparator(referenceElement.getElement()));
     final JList list =
         new JBList(myClassesToImport.toArray(new PsiClass[myClassesToImport.size()]));
     list.setCellRenderer(new FQNameCellRenderer());
     final Runnable runnable =
         new Runnable() {
           public void run() {
             if (!element.isValid()) return;
             final int index = list.getSelectedIndex();
             if (index < 0) return;
             new WriteCommandAction(project, element.getContainingFile()) {
               protected void run(final Result result) throws Throwable {
                 final PsiClass psiClass = myClassesToImport.get(index);
                 if (psiClass.isValid()) {
                   PsiDocumentManager.getInstance(project).commitAllDocuments();
                   referenceElement.bindToElement(psiClass);
                 }
               }
             }.execute();
           }
         };
     final AsyncResult<DataContext> asyncResult =
         DataManager.getInstance().getDataContextFromFocus();
     asyncResult.doWhenDone(
         new AsyncResult.Handler<DataContext>() {
           @Override
           public void run(DataContext dataContext) {
             final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
             assert editor != null; // available for on the fly mode only
             new PopupChooserBuilder(list)
                 .setTitle(QuickFixBundle.message("class.to.import.chooser.title"))
                 .setItemChoosenCallback(runnable)
                 .createPopup()
                 .showInBestPositionFor(editor);
           }
         });
   }
 }
  private void selectSourceAndDo() {
    // GUI part
    ImportCandidateHolder[] items =
        mySources.toArray(
            new ImportCandidateHolder
                [mySources.size()]); // silly JList can't handle modern collections
    final JList list = new JBList(items);
    list.setCellRenderer(new CellRenderer(myName));

    final Runnable runnable =
        new Runnable() {
          public void run() {
            final Object selected = list.getSelectedValue();
            if (selected instanceof ImportCandidateHolder) {
              final ImportCandidateHolder item = (ImportCandidateHolder) selected;
              PsiDocumentManager.getInstance(myTarget.getProject()).commitAllDocuments();
              doWriteAction(item);
            }
          }
        };

    DataManager.getInstance()
        .getDataContextFromFocus()
        .doWhenDone(
            new Consumer<DataContext>() {
              @Override
              public void consume(DataContext dataContext) {
                new PopupChooserBuilder(list)
                    .setTitle(
                        myUseQualifiedImport
                            ? PyBundle.message("ACT.qualify.with.module")
                            : PyBundle.message("ACT.from.some.module.import"))
                    .setItemChoosenCallback(runnable)
                    .setFilteringEnabled(
                        new Function<Object, String>() {
                          @Override
                          public String fun(Object o) {
                            return ((ImportCandidateHolder) o).getPresentableText(myName);
                          }
                        })
                    .createPopup()
                    .showInBestPositionFor(dataContext);
              }
            });
  }
    // value is a QualifiedHolder
    public Component getListCellRendererComponent(
        JList list,
        Object value, // expected to be
        int index,
        boolean isSelected,
        boolean cellHasFocus) {

      clear();

      ImportCandidateHolder item = (ImportCandidateHolder) value;
      setIcon(item.getImportable().getIcon(0));
      String item_name = item.getPresentableText(myName);
      append(item_name, SimpleTextAttributes.REGULAR_ATTRIBUTES);

      setFont(FONT);
      if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
      } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
      }
      return this;
    }