2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.ide.commander;
19 import com.intellij.ide.ui.search.SearchUtil;
20 import com.intellij.ide.util.treeView.AbstractTreeNode;
21 import com.intellij.ide.util.treeView.NodeDescriptor;
22 import com.intellij.openapi.editor.colors.EditorColorsManager;
23 import com.intellij.openapi.editor.colors.TextAttributesKey;
24 import com.intellij.openapi.editor.markup.TextAttributes;
25 import com.intellij.openapi.util.Pair;
26 import com.intellij.ui.ColoredListCellRenderer;
27 import com.intellij.ui.SimpleTextAttributes;
28 import com.intellij.ui.SpeedSearchBase;
29 import com.intellij.util.ui.UIUtil;
30 import org.jetbrains.annotations.NotNull;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.regex.Matcher;
38 final class ColoredCommanderRenderer extends ColoredListCellRenderer {
39 private final CommanderPanel myCommanderPanel;
41 public ColoredCommanderRenderer(@NotNull final CommanderPanel commanderPanel) {
42 myCommanderPanel = commanderPanel;
45 public Component getListCellRendererComponent(final JList list, final Object value, final int index, boolean selected, boolean hasFocus){
46 hasFocus = selected; // border around inactive items
48 if (!myCommanderPanel.isActive()) {
52 return super.getListCellRendererComponent(list, value, index, selected, hasFocus);
55 protected void customizeCellRenderer(final JList list, final Object value, final int index, final boolean selected, final boolean hasFocus) {
56 Color color = UIUtil.getListForeground();
57 SimpleTextAttributes attributes = null;
58 String locationString = null;
60 if (value instanceof NodeDescriptor) {
61 final NodeDescriptor descriptor = (NodeDescriptor)value;
62 setIcon(descriptor.getClosedIcon());
63 final Color elementColor = descriptor.getColor();
65 if (elementColor != null) {
69 if (descriptor instanceof AbstractTreeNode) {
70 final AbstractTreeNode treeNode = (AbstractTreeNode)descriptor;
71 final TextAttributesKey attributesKey = treeNode.getAttributesKey();
73 if (attributesKey != null) {
74 final TextAttributes textAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(attributesKey);
76 if (textAttributes != null) attributes = SimpleTextAttributes.fromTextAttributes(textAttributes);
78 locationString = treeNode.getLocationString();
82 if(attributes == null) attributes = new SimpleTextAttributes(Font.PLAIN, color);
83 final String text = value.toString();
85 if (myCommanderPanel.isEnableSearchHighlighting() && SpeedSearchBase.hasActiveSpeedSearch(list)) {
86 final SpeedSearchBase.SpeedSearchComparator comparator = myCommanderPanel.getListSpeedSearch().getComparator();
87 final String recentSearchText = comparator.getRecentSearchText();
88 if (recentSearchText != null && recentSearchText.length() > 0 && comparator.doCompare(recentSearchText, text)) {
89 final Matcher matcher = comparator.getRecentSearchMatcher();
90 final List<Pair<String, Integer>> searchTerms = new ArrayList<Pair<String, Integer>>();
91 for (int i = 0; i < matcher.groupCount(); i++) {
92 final int start = matcher.start(i + 1);
93 if (searchTerms.size() > 0) {
94 final Pair<String, Integer> recent = searchTerms.get(searchTerms.size() - 1);
95 if (start == recent.second + recent.first.length()) {
96 searchTerms.set(searchTerms.size() - 1, Pair.create(recent.first + matcher.group(i+1), recent.second));
101 searchTerms.add(Pair.create(matcher.group(i+1), start));
104 SearchUtil.appendFragmentsStrict(text, searchTerms, Font.PLAIN, attributes.getFgColor(),
105 selected ? UIUtil.getTreeSelectionBackground() : UIUtil.getTreeTextBackground(), this);
107 append(text != null ? text : "", attributes);
111 append(text != null ? text : "", attributes);
114 if (locationString != null && locationString.length() > 0) {
115 append(" (" + locationString + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);