goto file action - cell renderer works well with NavigationItems

This commit is contained in:
Mihail Muhin
2010-10-30 15:04:30 +04:00
committed by Maxim Shafirov
parent e97357299a
commit 825403ed8f
2 changed files with 66 additions and 32 deletions
@@ -56,18 +56,12 @@ public abstract class PsiElementListCellRenderer<T extends PsiElement> extends J
myModuleName = moduleName;
}
protected void customizeCellRenderer(
JList list,
Object value,
int index,
boolean selected,
boolean hasFocus
) {
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
Color bgColor = UIUtil.getListBackground();
Color color = list.getForeground();
if (value instanceof PsiElement) {
T element = (T)value;
String name = getElementText((T)element);
Color color = list.getForeground();
String name = getElementText(element);
PsiFile psiFile = element.getContainingFile();
boolean isProblemFile = false;
@@ -101,14 +95,14 @@ public abstract class PsiElementListCellRenderer<T extends PsiElement> extends J
}
if (isProblemFile) {
attributes = TextAttributes.merge(new TextAttributes(color, null, Color.red, EffectType.WAVE_UNDERSCORE, Font.PLAIN),attributes);
attributes = TextAttributes.merge(new TextAttributes(color, null, Color.red, EffectType.WAVE_UNDERSCORE, Font.PLAIN), attributes);
}
SimpleTextAttributes nameAttributes = attributes != null ? SimpleTextAttributes.fromTextAttributes(attributes) : null;
if (nameAttributes == null) nameAttributes = new SimpleTextAttributes(Font.PLAIN, color);
if (nameAttributes == null) nameAttributes = new SimpleTextAttributes(Font.PLAIN, color);
assert name != null: "Null name for PSI element " + element;
assert name != null : "Null name for PSI element " + element;
append(name, nameAttributes);
setIcon(PsiElementListCellRenderer.this.getIcon(element));
@@ -117,32 +111,25 @@ public abstract class PsiElementListCellRenderer<T extends PsiElement> extends J
append(" " + containerText, new SimpleTextAttributes(Font.PLAIN, Color.GRAY));
}
}
else {
else if (!customizeLeftRenderer(this, list, value, index, selected, hasFocus)) {
setIcon(IconUtil.getEmptyIcon(false));
append(value == null ? "" : value.toString(), new SimpleTextAttributes(Font.PLAIN, list.getForeground()));
}
setPaintFocusBorder(false);
setBackground(selected ? UIUtil.getListSelectionBackground() : bgColor);
}
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
removeAll();
String moduleName = null;
DefaultListCellRenderer rightRenderer = getRightCellRenderer();
ListCellRenderer leftRenderer = new LeftRenderer(null);
final Component leftCellRendererComponent = leftRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (rightRenderer != null) {
moduleName = rightRenderer.getText();
}
final Component leftCellRendererComponent =
new LeftRenderer(moduleName).getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (rightRenderer != null) {
final Component rightCellRendererComponent =
rightRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
rightCellRendererComponent.setBackground(isSelected ? UIUtil.getListSelectionBackground() : leftCellRendererComponent.getBackground());
final Component rightCellRendererComponent = rightRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
rightCellRendererComponent
.setBackground(isSelected ? UIUtil.getListSelectionBackground() : leftCellRendererComponent.getBackground());
add(rightCellRendererComponent, BorderLayout.EAST);
final JPanel spacer = new JPanel();
spacer.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
@@ -154,6 +141,15 @@ public abstract class PsiElementListCellRenderer<T extends PsiElement> extends J
return this;
}
protected boolean customizeLeftRenderer(ColoredListCellRenderer renderer,
JList list,
Object value,
int index,
boolean selected,
boolean hasFocus) {
return false;
}
@Nullable
protected DefaultListCellRenderer getRightCellRenderer() {
if (UISettings.getInstance().SHOW_ICONS_IN_QUICK_NAVIGATION) {
@@ -203,7 +199,7 @@ public abstract class PsiElementListCellRenderer<T extends PsiElement> extends J
if (o instanceof PsiElement) {
final String elementText = PsiElementListCellRenderer.this.getElementText((T)o);
if (includeContainerText) {
return elementText + " " + getContainerText((T) o, elementText);
return elementText + " " + getContainerText((T)o, elementText);
}
return elementText;
}
@@ -223,7 +219,7 @@ public abstract class PsiElementListCellRenderer<T extends PsiElement> extends J
protected String getElementText(Object o) {
if (o instanceof PsiElement) {
final String elementText = PsiElementListCellRenderer.this.getElementText((T)o);
return elementText + " " + getContainerText((T) o, elementText);
return elementText + " " + getContainerText((T)o, elementText);
}
else {
return o.toString();
@@ -18,20 +18,27 @@ package com.intellij.ide.util.gotoByName;
import com.intellij.ide.util.PlatformModuleRendererFactory;
import com.intellij.ide.util.PsiElementListCellRenderer;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.ui.FilePathSplittingPolicy;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.io.File;
public class GotoFileCellRenderer extends PsiElementListCellRenderer<PsiFile>{
public class GotoFileCellRenderer extends PsiElementListCellRenderer<PsiFile> {
private final int myMaxWidth;
public GotoFileCellRenderer(int maxSize) {
@@ -47,11 +54,12 @@ public class GotoFileCellRenderer extends PsiElementListCellRenderer<PsiFile>{
protected String getContainerText(PsiFile element, String name) {
final PsiDirectory psiDirectory = element.getContainingDirectory();
if (psiDirectory == null) return null;
if (psiDirectory == null) return null;
final VirtualFile virtualFile = psiDirectory.getVirtualFile();
final String relativePath = getRelativePath(virtualFile, element.getProject());
if (relativePath == null) return "( " + File.separator + " )";
String path = FilePathSplittingPolicy.SPLIT_BY_SEPARATOR.getOptimalTextForComponent(name + " ", new File(relativePath), this, myMaxWidth);
String path =
FilePathSplittingPolicy.SPLIT_BY_SEPARATOR.getOptimalTextForComponent(name + " ", new File(relativePath), this, myMaxWidth);
return "(" + path + ")";
}
@@ -74,6 +82,36 @@ public class GotoFileCellRenderer extends PsiElementListCellRenderer<PsiFile>{
return url;
}
@Override
protected boolean customizeLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
if (!(value instanceof NavigationItem)) return false;
NavigationItem item = (NavigationItem)value;
TextAttributes attributes = null;
TextAttributesKey attributesKey = null;
final ItemPresentation presentation = ((NavigationItem)value).getPresentation();
if (presentation != null) attributesKey = presentation.getTextAttributesKey();
if (attributesKey != null) {
attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(attributesKey);
}
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));
renderer.append(itemPresentation.getLocationString(), new SimpleTextAttributes(Font.PLAIN, Color.GRAY));
return true;
}
@Override
protected DefaultListCellRenderer getRightCellRenderer() {
final DefaultListCellRenderer rightRenderer = super.getRightCellRenderer();