@Override
  protected boolean customizeNonPsiElementLeftRenderer(
      ColoredListCellRenderer renderer,
      JList list,
      Object value,
      int index,
      boolean selected,
      boolean hasFocus) {
    if (!(value instanceof NavigationItem)) return false;

    NavigationItem item = (NavigationItem) value;

    TextAttributes attributes = getNavigationItemAttributes(item);

    SimpleTextAttributes nameAttributes =
        attributes != null ? SimpleTextAttributes.fromTextAttributes(attributes) : null;

    Color color = list.getForeground();
    if (nameAttributes == null) nameAttributes = new SimpleTextAttributes(Font.PLAIN, color);

    renderer.append(item + " ", nameAttributes);
    ItemPresentation itemPresentation = item.getPresentation();
    assert itemPresentation != null;
    renderer.setIcon(itemPresentation.getIcon(true));

    String locationString = itemPresentation.getLocationString();
    if (!StringUtil.isEmpty(locationString)) {
      renderer.append(locationString, new SimpleTextAttributes(Font.PLAIN, JBColor.GRAY));
    }
    return true;
  }
Exemplo n.º 2
0
 @Override
 protected void paintComponent(Graphics g) {
   super.paintComponent(g);
   if (myColorDescriptor != null) {
     final int size = getBounds().height;
     g.setColor(getBackground());
     g.fillRect(0, 0, size + getIconTextGap() + 1, size);
     g.setColor(myColorDescriptor.getResolvedColor());
     g.fillRect(2, 2, size - 4, size - 4);
     g.setColor(Color.BLACK);
     g.drawRect(2, 2, size - 4, size - 4);
   }
 }
  public PaletteItemsComponent(PaletteGroup group) {
    myGroup = group;

    setModel(
        new AbstractListModel() {
          @Override
          public int getSize() {
            return myGroup.getItems().size();
          }

          @Override
          public Object getElementAt(int index) {
            return myGroup.getItems().get(index);
          }
        });

    ColoredListCellRenderer renderer =
        new ColoredListCellRenderer() {
          @Override
          protected void customizeCellRenderer(
              JList list, Object value, int index, boolean selected, boolean hasFocus) {
            clear();
            PaletteItem item = (PaletteItem) value;

            setIcon(item.getIcon());

            String title = item.getTitle();
            append(title, SimpleTextAttributes.REGULAR_ATTRIBUTES);

            String tooltip = item.getTooltip();
            String version = item.getVersion();
            if (version == null) {
              version = "";
            } else {
              version = "<sup><i>" + version + "</i></sup>";
            }
            if (tooltip != null) {
              tooltip =
                  "<html><body><center><b>"
                      + StringUtil.escapeXml(title)
                      + "</b>"
                      + version
                      + "</center><p style='width: 300px'>"
                      + tooltip
                      + "</p></body></html>";
            }
            setToolTipText(tooltip);
          }
        };
    renderer.getIpad().left = UIUtil.getTreeLeftChildIndent();
    renderer.getIpad().right = UIUtil.getTreeRightChildIndent();
    setCellRenderer(renderer);

    setVisibleRowCount(0);
    setLayoutOrientation(HORIZONTAL_WRAP);
    setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    addMouseListener(
        new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent e) {
            myNeedClearSelection =
                SwingUtilities.isLeftMouseButton(e)
                    && myBeforeClickSelectedRow >= 0
                    && locationToIndex(e.getPoint()) == myBeforeClickSelectedRow
                    && !UIUtil.isControlKeyDown(e)
                    && !e.isShiftDown();
          }

          @Override
          public void mouseReleased(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)
                && myBeforeClickSelectedRow >= 0
                && locationToIndex(e.getPoint()) == myBeforeClickSelectedRow
                && !UIUtil.isControlKeyDown(e)
                && !e.isShiftDown()
                && myNeedClearSelection) {
              clearSelection();
            }
          }
        });

    initActions();
  }