Exemplo n.º 1
0
 public JBPopup getChildFocusedPopup(@Nullable Component parent) {
   if (parent == null) return null;
   List<JBPopup> popups = getChildPopups(parent);
   for (JBPopup each : popups) {
     if (each.isFocused()) return each;
   }
   return null;
 }
  private void hideCurrentPopup() {
    if (myCurrentPopup != null) {
      myCurrentPopup.cancel();
      myCurrentPopup = null;
    }

    if (myNoSuggestionsPopup != null) {
      myNoSuggestionsPopup.cancel();
      myNoSuggestionsPopup = null;
    }
  }
  private void showNoSuggestions(boolean isExplicit) {
    hideCurrentPopup();

    if (!isExplicit) return;

    final JComponent message =
        HintUtil.createErrorLabel(IdeBundle.message("file.chooser.completion.no.suggestions"));
    final ComponentPopupBuilder builder =
        JBPopupFactory.getInstance().createComponentPopupBuilder(message, message);
    builder
        .setRequestFocus(false)
        .setResizable(false)
        .setAlpha(0.1f)
        .setFocusOwners(new Component[] {myPathTextField});
    myNoSuggestionsPopup = builder.createPopup();
    myNoSuggestionsPopup.showInScreenCoordinates(getField(), getLocationForCaret(myPathTextField));
  }
 @Override
 public boolean isPopupDisplayed() {
   return myCurrentPopup != null && myCurrentPopup.isVisible();
 }
  private void showCompletionPopup(
      final CompletionResult result, int position, boolean isExplicit) {
    if (myList == null) {
      myList = new JBList();
      myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

      myList.setCellRenderer(
          new GroupedItemsListRenderer(
              new ListItemDescriptor() {
                public String getTextFor(final Object value) {
                  final LookupFile file = (LookupFile) value;

                  if (file.getMacro() != null) {
                    return file.getMacro();
                  } else {
                    return (myCurrentCompletion != null
                                && myCurrentCompletion.myKidsAfterSeparator.contains(file)
                            ? myFinder.getSeparator()
                            : "")
                        + file.getName();
                  }
                }

                public String getTooltipFor(final Object value) {
                  return null;
                }

                public Icon getIconFor(final Object value) {
                  final LookupFile file = (LookupFile) value;
                  return file.getIcon();
                }

                @Nullable
                private Separator getSeparatorAboveOf(Object value) {
                  if (myCurrentCompletion == null) return null;
                  final LookupFile file = (LookupFile) value;

                  final int fileIndex = myCurrentCompletion.myToComplete.indexOf(file);
                  if (fileIndex > 0 && !myCurrentCompletion.myMacros.contains(file)) {
                    final LookupFile prev = myCurrentCompletion.myToComplete.get(fileIndex - 1);
                    if (myCurrentCompletion.myMacros.contains(prev)) {
                      return new Separator("");
                    }
                  }

                  if (myCurrentCompletion.myKidsAfterSeparator.indexOf(file) == 0
                      && myCurrentCompletion.mySiblings.size() > 0) {
                    final LookupFile parent = file.getParent();
                    return parent == null ? new Separator("") : new Separator(parent.getName());
                  }

                  if (myCurrentCompletion.myMacros.size() > 0 && fileIndex == 0) {
                    return new Separator(
                        IdeBundle.message("file.chooser.completion.path.variables.text"));
                  }

                  return null;
                }

                public boolean hasSeparatorAboveOf(final Object value) {
                  return getSeparatorAboveOf(value) != null;
                }

                public String getCaptionAboveOf(final Object value) {
                  final FileTextFieldImpl.Separator separator = getSeparatorAboveOf(value);
                  return separator != null ? separator.getText() : null;
                }
              }));
    }

    if (myCurrentPopup != null) {
      closePopup();
    }

    myCurrentCompletion = result;
    myCurrentCompletionsPos = position;

    if (myCurrentCompletion.myToComplete.size() == 0) {
      showNoSuggestions(isExplicit);
      return;
    }

    myList.setModel(
        new AbstractListModel() {
          public int getSize() {
            return myCurrentCompletion.myToComplete.size();
          }

          public Object getElementAt(final int index) {
            return myCurrentCompletion.myToComplete.get(index);
          }
        });
    myList.getSelectionModel().clearSelection();
    final PopupChooserBuilder builder = JBPopupFactory.getInstance().createListPopupBuilder(myList);
    builder.addListener(
        new JBPopupListener() {
          public void beforeShown(LightweightWindowEvent event) {
            myPathTextField.registerKeyboardAction(
                myCancelAction,
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
            for (Action each : myDisabledTextActions) {
              each.setEnabled(false);
            }
          }

          public void onClosed(LightweightWindowEvent event) {
            myPathTextField.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
            for (Action each : myDisabledTextActions) {
              each.setEnabled(true);
            }
          }
        });

    myCurrentPopup =
        builder
            .setRequestFocus(false)
            .setAdText(getAdText(myCurrentCompletion))
            .setAutoSelectIfEmpty(false)
            .setResizable(false)
            .setCancelCallback(
                new Computable<Boolean>() {
                  public Boolean compute() {
                    final int caret = myPathTextField.getCaretPosition();
                    myPathTextField.setSelectionStart(caret);
                    myPathTextField.setSelectionEnd(caret);
                    myPathTextField.setFocusTraversalKeysEnabled(true);
                    SwingUtilities.invokeLater(
                        new Runnable() {
                          public void run() {
                            getField().requestFocus();
                          }
                        });
                    return Boolean.TRUE;
                  }
                })
            .setItemChoosenCallback(
                new Runnable() {
                  public void run() {
                    processChosenFromCompletion(false);
                  }
                })
            .setCancelKeyEnabled(false)
            .setAlpha(0.1f)
            .setFocusOwners(new Component[] {myPathTextField})
            .createPopup();

    if (result.myPreselected != null) {
      myList.setSelectedValue(result.myPreselected, false);
    }

    myPathTextField.setFocusTraversalKeysEnabled(false);

    myCurrentPopup.showInScreenCoordinates(getField(), getLocationForCaret(myPathTextField));
  }