Search in sources :

Example 1 with RealLookupElementPresentation

use of com.intellij.codeInsight.lookup.RealLookupElementPresentation in project intellij-community by JetBrains.

the class LookupCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(final JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
    boolean nonFocusedSelection = isSelected && myLookup.getFocusDegree() == LookupImpl.FocusDegree.SEMI_FOCUSED;
    if (!myLookup.isFocused()) {
        isSelected = false;
    }
    myIsSelected = isSelected;
    final LookupElement item = (LookupElement) value;
    final Color foreground = getForegroundColor(isSelected);
    final Color background = nonFocusedSelection ? SELECTED_NON_FOCUSED_BACKGROUND_COLOR : isSelected ? SELECTED_BACKGROUND_COLOR : BACKGROUND_COLOR;
    int allowedWidth = list.getWidth() - calcSpacing(myNameComponent, myEmptyIcon) - calcSpacing(myTailComponent, null) - calcSpacing(myTypeLabel, null);
    FontMetrics normalMetrics = getRealFontMetrics(item, false);
    FontMetrics boldMetrics = getRealFontMetrics(item, true);
    final LookupElementPresentation presentation = new RealLookupElementPresentation(isSelected ? getMaxWidth() : allowedWidth, normalMetrics, boldMetrics, myLookup);
    AccessToken token = ReadAction.start();
    try {
        if (item.isValid()) {
            try {
                item.renderElement(presentation);
            } catch (ProcessCanceledException e) {
                LOG.info(e);
                presentation.setItemTextForeground(JBColor.RED);
                presentation.setItemText("Error occurred, see the log in Help | Show Log");
            } catch (Exception | Error e) {
                LOG.error(e);
            }
        } else {
            presentation.setItemTextForeground(JBColor.RED);
            presentation.setItemText("Invalid");
        }
    } finally {
        token.finish();
    }
    myNameComponent.clear();
    myNameComponent.setBackground(background);
    allowedWidth -= setItemTextLabel(item, new JBColor(isSelected ? SELECTED_FOREGROUND_COLOR : presentation.getItemTextForeground(), presentation.getItemTextForeground()), isSelected, presentation, allowedWidth);
    Font font = myLookup.getCustomFont(item, false);
    if (font == null) {
        font = myNormalFont;
    }
    myTailComponent.setFont(font);
    myTypeLabel.setFont(font);
    myNameComponent.setIcon(augmentIcon(myLookup.getEditor(), presentation.getIcon(), myEmptyIcon));
    myTypeLabel.clear();
    if (allowedWidth > 0) {
        allowedWidth -= setTypeTextLabel(item, background, foreground, presentation, isSelected ? getMaxWidth() : allowedWidth, isSelected, nonFocusedSelection, normalMetrics);
    }
    myTailComponent.clear();
    myTailComponent.setBackground(background);
    if (isSelected || allowedWidth >= 0) {
        setTailTextLabel(isSelected, presentation, foreground, isSelected ? getMaxWidth() : allowedWidth, nonFocusedSelection, normalMetrics);
    }
    if (mySelected.containsKey(index)) {
        if (!isSelected && mySelected.get(index)) {
            myPanel.setUpdateExtender(true);
        }
    }
    mySelected.put(index, isSelected);
    final double w = myNameComponent.getPreferredSize().getWidth() + myTailComponent.getPreferredSize().getWidth() + myTypeLabel.getPreferredSize().getWidth();
    boolean useBoxLayout = isSelected && w > list.getWidth() && ((JBList) list).getExpandableItemsHandler().isEnabled();
    if (useBoxLayout != myPanel.getLayout() instanceof BoxLayout) {
        myPanel.removeAll();
        if (useBoxLayout) {
            myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
            myPanel.add(myNameComponent);
            myPanel.add(myTailComponent);
            myPanel.add(myTypeLabel);
        } else {
            myPanel.setLayout(new BorderLayout());
            myPanel.add(myNameComponent, BorderLayout.WEST);
            myPanel.add(myTailComponent, BorderLayout.CENTER);
            myPanel.add(myTypeLabel, BorderLayout.EAST);
        }
    }
    AccessibleContextUtil.setCombinedName(myPanel, myNameComponent, "", myTailComponent, " - ", myTypeLabel);
    AccessibleContextUtil.setCombinedDescription(myPanel, myNameComponent, "", myTailComponent, " - ", myTypeLabel);
    return myPanel;
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) LookupValueWithUIHint(com.intellij.codeInsight.lookup.LookupValueWithUIHint) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) RealLookupElementPresentation(com.intellij.codeInsight.lookup.RealLookupElementPresentation) LookupElementPresentation(com.intellij.codeInsight.lookup.LookupElementPresentation) AccessToken(com.intellij.openapi.application.AccessToken) JBList(com.intellij.ui.components.JBList) RealLookupElementPresentation(com.intellij.codeInsight.lookup.RealLookupElementPresentation) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupElementPresentation (com.intellij.codeInsight.lookup.LookupElementPresentation)1 LookupValueWithUIHint (com.intellij.codeInsight.lookup.LookupValueWithUIHint)1 RealLookupElementPresentation (com.intellij.codeInsight.lookup.RealLookupElementPresentation)1 AccessToken (com.intellij.openapi.application.AccessToken)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 JBList (com.intellij.ui.components.JBList)1