예제 #1
0
    // Make sure that the text rendered by this method is 'searchable' via
    // com.intellij.openapi.keymap.impl.ui.ActionsTree.filter method.
    public void customizeCellRenderer(
        JTree tree,
        Object value,
        boolean selected,
        boolean expanded,
        boolean leaf,
        int row,
        boolean hasFocus) {
      final boolean showIcons = UISettings.getInstance().SHOW_ICONS_IN_MENUS;
      Keymap originalKeymap = myKeymap != null ? myKeymap.getParent() : null;
      Icon icon = null;
      String text;
      boolean bound = false;
      setToolTipText(null);

      if (value instanceof DefaultMutableTreeNode) {
        Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
        boolean changed;
        if (userObject instanceof Group) {
          Group group = (Group) userObject;
          text = group.getName();

          changed = originalKeymap != null && isGroupChanged(group, originalKeymap, myKeymap);
          icon = group.getIcon();
          if (icon == null) {
            icon = CLOSE_ICON;
          }
        } else if (userObject instanceof String) {
          String actionId = (String) userObject;
          bound = myShowBoundActions && ((KeymapImpl) myKeymap).isActionBound(actionId);
          AnAction action = ActionManager.getInstance().getAction(actionId);
          if (action != null) {
            text = action.getTemplatePresentation().getText();
            if (text == null || text.length() == 0) { // fill dynamic presentation gaps
              text = actionId;
            }
            Icon actionIcon = action.getTemplatePresentation().getIcon();
            if (actionIcon != null) {
              icon = actionIcon;
            }
            setToolTipText(action.getTemplatePresentation().getDescription());
          } else {
            text = actionId;
          }
          changed = originalKeymap != null && isActionChanged(actionId, originalKeymap, myKeymap);
        } else if (userObject instanceof QuickList) {
          QuickList list = (QuickList) userObject;
          icon = AllIcons.Actions.QuickList;
          text = list.getDisplayName();

          changed =
              originalKeymap != null
                  && isActionChanged(list.getActionId(), originalKeymap, myKeymap);
        } else if (userObject instanceof Separator) {
          // TODO[vova,anton]: beautify
          changed = false;
          text = "-------------";
        } else {
          throw new IllegalArgumentException("unknown userObject: " + userObject);
        }

        if (showIcons) {
          setIcon(ActionsTree.getEvenIcon(icon));
        }

        Color foreground;
        if (selected) {
          foreground = UIUtil.getTreeSelectionForeground();
        } else {
          if (changed) {
            foreground = PlatformColors.BLUE;
          } else {
            foreground = UIUtil.getTreeForeground();
          }

          if (bound) {
            foreground = JBColor.MAGENTA;
          }
        }
        SearchUtil.appendFragments(
            myFilter,
            text,
            Font.PLAIN,
            foreground,
            selected ? UIUtil.getTreeSelectionBackground() : UIUtil.getTreeTextBackground(),
            this);
      }
    }
  @Override
  public final Component getTreeCellRendererComponent(
      JTree tree,
      Object value,
      boolean selected,
      boolean expanded,
      boolean leaf,
      int row,
      boolean hasFocus) {
    myTree = tree;

    clear();

    mySelected = selected;
    myFocusedCalculated = false;

    // We paint background if and only if tree path is selected and tree has focus.
    // If path is selected and tree is not focused then we just paint focused border.
    if (UIUtil.isFullRowSelectionLAF()) {
      setBackground(selected ? UIUtil.getTreeSelectionBackground() : null);
    } else if (WideSelectionTreeUI.isWideSelection(tree)) {
      setPaintFocusBorder(false);
      if (selected) {
        setBackground(
            hasFocus
                ? UIUtil.getTreeSelectionBackground()
                : UIUtil.getTreeUnfocusedSelectionBackground());
      }
    } else if (selected) {
      setPaintFocusBorder(true);
      if (isFocused()) {
        setBackground(UIUtil.getTreeSelectionBackground());
      } else {
        setBackground(null);
      }
    } else {
      setBackground(null);
    }

    if (value instanceof LoadingNode) {
      setForeground(JBColor.GRAY);
      setIcon(LOADING_NODE_ICON);
    } else {
      setForeground(tree.getForeground());
      setIcon(null);
    }

    if (UIUtil.isUnderGTKLookAndFeel()) {
      super.setOpaque(false); // avoid nasty background
      super.setIconOpaque(false);
    } else if (UIUtil.isUnderNimbusLookAndFeel() && selected && hasFocus) {
      super.setOpaque(false); // avoid erasing Nimbus focus frame
      super.setIconOpaque(false);
    } else if (WideSelectionTreeUI.isWideSelection(tree)) {
      super.setOpaque(false); // avoid erasing Nimbus focus frame
      super.setIconOpaque(false);
    } else {
      super.setOpaque(
          myOpaque
              || selected && hasFocus
              || selected && isFocused()); // draw selection background even for non-opaque tree
    }

    if (tree.getUI() instanceof WideSelectionTreeUI && UIUtil.isUnderAquaBasedLookAndFeel()) {
      setMyBorder(null);
      setIpad(new Insets(0, 2, 0, 2));
    }

    customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);

    return this;
  }