Search in sources :

Example 16 with SimpleTextAttributes

use of com.intellij.ui.SimpleTextAttributes in project intellij-community by JetBrains.

the class JumpToColorsAndFontsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    // todo handle ColorKey's as well
    Project project = e.getData(CommonDataKeys.PROJECT);
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (project == null || editor == null)
        return;
    Map<TextAttributesKey, Pair<ColorSettingsPage, AttributesDescriptor>> keyMap = ContainerUtil.newHashMap();
    Processor<RangeHighlighterEx> processor = r -> {
        Object tt = r.getErrorStripeTooltip();
        TextAttributesKey key = tt instanceof HighlightInfo ? ObjectUtils.chooseNotNull(((HighlightInfo) tt).forcedTextAttributesKey, ((HighlightInfo) tt).type.getAttributesKey()) : null;
        Pair<ColorSettingsPage, AttributesDescriptor> p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
        if (p != null)
            keyMap.put(key, p);
        return true;
    };
    JBIterable<Editor> editors = editor instanceof EditorWindow ? JBIterable.of(editor, ((EditorWindow) editor).getDelegate()) : JBIterable.of(editor);
    for (Editor ed : editors) {
        TextRange selection = EditorUtil.getSelectionInAnyMode(ed);
        MarkupModel forDocument = DocumentMarkupModel.forDocument(ed.getDocument(), project, false);
        if (forDocument != null) {
            ((MarkupModelEx) forDocument).processRangeHighlightersOverlappingWith(selection.getStartOffset(), selection.getEndOffset(), processor);
        }
        ((MarkupModelEx) ed.getMarkupModel()).processRangeHighlightersOverlappingWith(selection.getStartOffset(), selection.getEndOffset(), processor);
        EditorHighlighter highlighter = ed instanceof EditorEx ? ((EditorEx) ed).getHighlighter() : null;
        SyntaxHighlighter syntaxHighlighter = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
        if (syntaxHighlighter != null) {
            HighlighterIterator iterator = highlighter.createIterator(selection.getStartOffset());
            while (!iterator.atEnd()) {
                for (TextAttributesKey key : syntaxHighlighter.getTokenHighlights(iterator.getTokenType())) {
                    Pair<ColorSettingsPage, AttributesDescriptor> p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
                    if (p != null)
                        keyMap.put(key, p);
                }
                if (iterator.getEnd() >= selection.getEndOffset())
                    break;
                iterator.advance();
            }
        }
    }
    if (keyMap.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, "No text attributes found");
    } else if (keyMap.size() == 1) {
        Pair<ColorSettingsPage, AttributesDescriptor> p = keyMap.values().iterator().next();
        if (!openSettingsAndSelectKey(project, p.first, p.second)) {
            HintManager.getInstance().showErrorHint(editor, "No appropriate settings page found");
        }
    } else {
        ArrayList<Pair<ColorSettingsPage, AttributesDescriptor>> attrs = ContainerUtil.newArrayList(keyMap.values());
        Collections.sort(attrs, (o1, o2) -> StringUtil.naturalCompare(o1.first.getDisplayName() + o1.second.getDisplayName(), o2.first.getDisplayName() + o2.second.getDisplayName()));
        EditorColorsScheme colorsScheme = editor.getColorsScheme();
        JBList<Pair<ColorSettingsPage, AttributesDescriptor>> list = new JBList<>(attrs);
        list.setCellRenderer(new ColoredListCellRenderer<Pair<ColorSettingsPage, AttributesDescriptor>>() {

            @Override
            protected void customizeCellRenderer(@NotNull JList<? extends Pair<ColorSettingsPage, AttributesDescriptor>> list, Pair<ColorSettingsPage, AttributesDescriptor> value, int index, boolean selected, boolean hasFocus) {
                TextAttributes ta = colorsScheme.getAttributes(value.second.getKey());
                Color fg = ObjectUtils.chooseNotNull(ta.getForegroundColor(), colorsScheme.getDefaultForeground());
                Color bg = ObjectUtils.chooseNotNull(ta.getBackgroundColor(), colorsScheme.getDefaultBackground());
                SimpleTextAttributes sa = fromTextAttributes(ta);
                SimpleTextAttributes saOpaque = sa.derive(STYLE_OPAQUE | sa.getStyle(), fg, bg, null);
                SimpleTextAttributes saSelected = REGULAR_ATTRIBUTES.derive(sa.getStyle(), null, null, null);
                SimpleTextAttributes saCur = REGULAR_ATTRIBUTES;
                List<String> split = StringUtil.split(value.first.getDisplayName() + "//" + value.second.getDisplayName(), "//");
                for (int i = 0, len = split.size(); i < len; i++) {
                    boolean last = i == len - 1;
                    saCur = !last ? REGULAR_ATTRIBUTES : selected ? saSelected : saOpaque;
                    if (last)
                        append(" ", saCur);
                    append(split.get(i), saCur);
                    if (last)
                        append(" ", saCur);
                    else
                        append(" > ", GRAYED_ATTRIBUTES);
                }
                Color stripeColor = ta.getErrorStripeColor();
                boolean addStripe = stripeColor != null && stripeColor != saCur.getBgColor();
                boolean addBoxed = ta.getEffectType() == EffectType.BOXED && ta.getEffectColor() != null;
                if (addBoxed) {
                    append("▢" + (addStripe ? "" : " "), saCur.derive(-1, ta.getEffectColor(), null, null));
                }
                if (addStripe) {
                    append(" ", saCur.derive(STYLE_OPAQUE, null, stripeColor, null));
                }
            }
        });
        JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(StringUtil.notNullize(e.getPresentation().getText())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
            Pair<ColorSettingsPage, AttributesDescriptor> p = list.getSelectedValue();
            if (p != null && !openSettingsAndSelectKey(project, p.first, p.second)) {
                HintManager.getInstance().showErrorHint(editor, "No appropriate settings page found");
            }
        }).createPopup().showInBestPositionFor(editor);
    }
}
Also used : Settings(com.intellij.openapi.options.ex.Settings) JBIterable(com.intellij.util.containers.JBIterable) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) ColorSettingsPage(com.intellij.openapi.options.colors.ColorSettingsPage) ArrayList(java.util.ArrayList) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) ShowSettingsUtilImpl(com.intellij.ide.actions.ShowSettingsUtilImpl) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) Map(java.util.Map) EffectType(com.intellij.openapi.editor.markup.EffectType) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) JBList(com.intellij.ui.components.JBList) ColorSettingsPages(com.intellij.openapi.options.colors.ColorSettingsPages) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) StringUtil(com.intellij.openapi.util.text.StringUtil) ActionCallback(com.intellij.openapi.util.ActionCallback) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) SettingsDialog(com.intellij.openapi.options.newEditor.SettingsDialog) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) EditorWindow(com.intellij.injected.editor.EditorWindow) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) java.awt(java.awt) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Processor(com.intellij.util.Processor) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Pair(com.intellij.openapi.util.Pair) ObjectUtils(com.intellij.util.ObjectUtils) HintManager(com.intellij.codeInsight.hint.HintManager) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) ArrayList(java.util.ArrayList) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) NotNull(org.jetbrains.annotations.NotNull) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) ColorSettingsPage(com.intellij.openapi.options.colors.ColorSettingsPage) Pair(com.intellij.openapi.util.Pair) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) TextRange(com.intellij.openapi.util.TextRange) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) JBList(com.intellij.ui.components.JBList) Editor(com.intellij.openapi.editor.Editor) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 17 with SimpleTextAttributes

use of com.intellij.ui.SimpleTextAttributes in project intellij-community by JetBrains.

the class InspectionListCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean sel, boolean focus) {
    final BorderLayout layout = new BorderLayout();
    layout.setHgap(5);
    final JPanel panel = new JPanel(layout);
    panel.setOpaque(true);
    final Color bg = sel ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground();
    final Color fg = sel ? UIUtil.getListSelectionForeground() : UIUtil.getListForeground();
    panel.setBackground(bg);
    panel.setForeground(fg);
    if (value instanceof InspectionToolWrapper) {
        final InspectionToolWrapper toolWrapper = (InspectionToolWrapper) value;
        final String inspectionName = "  " + toolWrapper.getDisplayName();
        final String groupName = StringUtil.join(toolWrapper.getGroupPath(), " | ");
        final String matchingText = inspectionName + "|" + groupName;
        Matcher matcher = MatcherHolder.getAssociatedMatcher(list);
        List<TextRange> fragments = matcher == null ? null : ((MinusculeMatcher) matcher).matchingFragments(matchingText);
        List<TextRange> adjustedFragments = new ArrayList<>();
        if (fragments != null) {
            adjustedFragments.addAll(fragments);
        }
        final int splitPoint = adjustRanges(adjustedFragments, inspectionName.length() + 1);
        final SimpleColoredComponent c = new SimpleColoredComponent();
        final boolean matchHighlighting = Registry.is("ide.highlight.match.in.selected.only") && !sel;
        if (matchHighlighting) {
            c.append(inspectionName, myPlain);
        } else {
            final List<TextRange> ranges = adjustedFragments.subList(0, splitPoint);
            SpeedSearchUtil.appendColoredFragments(c, inspectionName, ranges, sel ? mySelected : myPlain, myHighlighted);
        }
        panel.add(c, BorderLayout.WEST);
        final SimpleColoredComponent group = new SimpleColoredComponent();
        if (matchHighlighting) {
            group.append(groupName, SimpleTextAttributes.GRAYED_ATTRIBUTES);
        } else {
            final SimpleTextAttributes attributes = sel ? mySelected : SimpleTextAttributes.GRAYED_ATTRIBUTES;
            final List<TextRange> ranges = adjustedFragments.subList(splitPoint, adjustedFragments.size());
            SpeedSearchUtil.appendColoredFragments(group, groupName, ranges, attributes, myHighlighted);
        }
        final JPanel right = new JPanel(new BorderLayout());
        right.setBackground(bg);
        right.setForeground(fg);
        right.add(group, BorderLayout.CENTER);
        final JLabel icon = new JLabel(getIcon(toolWrapper));
        icon.setBackground(bg);
        icon.setForeground(fg);
        right.add(icon, BorderLayout.EAST);
        panel.add(right, BorderLayout.EAST);
    } else {
        // E.g. "..." item
        return value == ChooseByNameBase.NON_PREFIX_SEPARATOR ? ChooseByNameBase.renderNonPrefixSeparatorComponent(UIUtil.getListBackground()) : super.getListCellRendererComponent(list, value, index, sel, focus);
    }
    return panel;
}
Also used : MinusculeMatcher(com.intellij.psi.codeStyle.MinusculeMatcher) Matcher(com.intellij.util.text.Matcher) JBColor(com.intellij.ui.JBColor) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper) SimpleColoredComponent(com.intellij.ui.SimpleColoredComponent)

Example 18 with SimpleTextAttributes

use of com.intellij.ui.SimpleTextAttributes in project intellij-community by JetBrains.

the class NavBarPresentation method getTextAttributes.

protected SimpleTextAttributes getTextAttributes(final Object object, final boolean selected) {
    if (!NavBarModel.isValid(object))
        return SimpleTextAttributes.REGULAR_ATTRIBUTES;
    if (object instanceof PsiElement) {
        if (!ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

            @Override
            public Boolean compute() {
                return ((PsiElement) object).isValid();
            }
        }).booleanValue())
            return SimpleTextAttributes.GRAYED_ATTRIBUTES;
        PsiFile psiFile = ((PsiElement) object).getContainingFile();
        if (psiFile != null) {
            final VirtualFile virtualFile = psiFile.getVirtualFile();
            return new SimpleTextAttributes(null, selected ? null : FileStatusManager.getInstance(myProject).getStatus(virtualFile).getColor(), JBColor.red, WolfTheProblemSolver.getInstance(myProject).isProblemFile(virtualFile) ? SimpleTextAttributes.STYLE_WAVED : SimpleTextAttributes.STYLE_PLAIN);
        } else {
            if (object instanceof PsiDirectory) {
                VirtualFile vDir = ((PsiDirectory) object).getVirtualFile();
                if (vDir.getParent() == null || ProjectRootsUtil.isModuleContentRoot(vDir, myProject)) {
                    return SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES;
                }
            }
            if (wolfHasProblemFilesBeneath((PsiElement) object)) {
                return WOLFED;
            }
        }
    } else if (object instanceof Module) {
        if (WolfTheProblemSolver.getInstance(myProject).hasProblemFilesBeneath((Module) object)) {
            return WOLFED;
        }
    } else if (object instanceof Project) {
        final Project project = (Project) object;
        final Module[] modules = ApplicationManager.getApplication().runReadAction(new Computable<Module[]>() {

            @Override
            public Module[] compute() {
                return ModuleManager.getInstance(project).getModules();
            }
        });
        for (Module module : modules) {
            if (WolfTheProblemSolver.getInstance(project).hasProblemFilesBeneath(module)) {
                return WOLFED;
            }
        }
    }
    return SimpleTextAttributes.REGULAR_ATTRIBUTES;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 19 with SimpleTextAttributes

use of com.intellij.ui.SimpleTextAttributes in project intellij-community by JetBrains.

the class BlockTreeNode method update.

@Override
protected void update(PresentationData presentation) {
    String name = myBlock.getClass().getSimpleName();
    if (myBlock instanceof DataLanguageBlockWrapper) {
        name += " (" + ((DataLanguageBlockWrapper) myBlock).getOriginal().getClass().getSimpleName() + ")";
    }
    presentation.addText(name, SimpleTextAttributes.REGULAR_ATTRIBUTES);
    if (myBlock.getIndent() != null) {
        presentation.addText(" " + String.valueOf(myBlock.getIndent()).replaceAll("[<>]", " "), SimpleTextAttributes.GRAY_ATTRIBUTES);
    } else {
        presentation.addText(" Indent: null", SimpleTextAttributes.GRAY_ATTRIBUTES);
    }
    if (myBlock.getAlignment() != null) {
        float d = 1.f * System.identityHashCode(myBlock.getAlignment()) / Integer.MAX_VALUE;
        Color color = new JBColor(Color.HSBtoRGB(1.0f * d, .3f, .7f), Color.HSBtoRGB(1.0f * d, .3f, .8f));
        presentation.addText(" " + String.valueOf(myBlock.getAlignment()), new SimpleTextAttributes(SimpleTextAttributes.STYLE_BOLD, color));
    }
    if (myBlock.getWrap() != null) {
        presentation.addText(" " + String.valueOf(myBlock.getWrap()), new SimpleTextAttributes(SimpleTextAttributes.STYLE_ITALIC, PlatformColors.BLUE));
    }
}
Also used : JBColor(com.intellij.ui.JBColor) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) JBColor(com.intellij.ui.JBColor) DataLanguageBlockWrapper(com.intellij.formatting.templateLanguages.DataLanguageBlockWrapper)

Example 20 with SimpleTextAttributes

use of com.intellij.ui.SimpleTextAttributes in project intellij-community by JetBrains.

the class FocusDebugger method propertyChange.

@Override
public void propertyChange(PropertyChangeEvent evt) {
    final Object newValue = evt.getNewValue();
    final Object oldValue = evt.getOldValue();
    boolean affectsDebugger = false;
    if (newValue instanceof Component && isInsideDebuggerDialog((Component) newValue)) {
        affectsDebugger |= true;
    }
    if (oldValue instanceof Component && isInsideDebuggerDialog((Component) oldValue)) {
        affectsDebugger |= true;
    }
    final SimpleColoredText text = new SimpleColoredText();
    text.append(evt.getPropertyName(), maybeGrayOut(new SimpleTextAttributes(SimpleTextAttributes.STYLE_UNDERLINE, null), affectsDebugger));
    text.append(" newValue=", maybeGrayOut(SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES, affectsDebugger));
    text.append(evt.getNewValue() + "", maybeGrayOut(SimpleTextAttributes.REGULAR_ATTRIBUTES, affectsDebugger));
    text.append(" oldValue=" + evt.getOldValue(), maybeGrayOut(SimpleTextAttributes.REGULAR_ATTRIBUTES, affectsDebugger));
    myLogModel.addElement(new FocusElement(text, new Throwable()));
    SwingUtilities.invokeLater(() -> {
        if (myLog != null && myLog.isShowing()) {
            final int h = myLog.getFixedCellHeight();
            myLog.scrollRectToVisible(new Rectangle(0, myLog.getPreferredSize().height - h, myLog.getWidth(), h));
            if (myLog.getModel().getSize() > 0) {
                myLog.setSelectedIndex(myLog.getModel().getSize() - 1);
            }
        }
    });
}
Also used : SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) SimpleColoredText(com.intellij.ui.SimpleColoredText)

Aggregations

SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)87 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)15 JBColor (com.intellij.ui.JBColor)11 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 SimpleColoredComponent (com.intellij.ui.SimpleColoredComponent)7 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)6 TextRange (com.intellij.openapi.util.TextRange)6 NodeDescriptor (com.intellij.ide.util.treeView.NodeDescriptor)5 ItemPresentation (com.intellij.navigation.ItemPresentation)4 FileStatus (com.intellij.openapi.vcs.FileStatus)4 PresentationData (com.intellij.ide.projectView.PresentationData)3 PsiElement (com.intellij.psi.PsiElement)3 SimpleColoredText (com.intellij.ui.SimpleColoredText)3 List (java.util.List)3 PsIssue (com.android.tools.idea.gradle.structure.model.PsIssue)2 PTableItem (com.android.tools.idea.uibuilder.property.ptable.PTableItem)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)2 RefEntity (com.intellij.codeInspection.reference.RefEntity)2