ability to set predefined text in choose by name popup
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / util / PsiElementModuleRenderer.java
blob41c3eec12e57988047bfd5c07f8f702563dc2f7c
1 /*
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.util;
19 import com.intellij.openapi.module.Module;
20 import com.intellij.openapi.module.ModuleUtil;
21 import com.intellij.openapi.roots.*;
22 import com.intellij.openapi.util.IconLoader;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.psi.PsiElement;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.util.ui.UIUtil;
28 import javax.swing.*;
29 import java.awt.*;
31 public class PsiElementModuleRenderer extends DefaultListCellRenderer{
32 private static final Icon TEST_ICON = IconLoader.getIcon("/nodes/testSourceFolder.png");
33 private static final Icon LIB_ICON = IconLoader.getIcon("/nodes/ppLibClosed.png");
35 private String myText;
37 public Component getListCellRendererComponent(
38 JList list,
39 Object value,
40 int index,
41 boolean isSelected,
42 boolean cellHasFocus) {
43 final Component listCellRendererComponent = super.getListCellRendererComponent(list, value, index, isSelected,
44 cellHasFocus);
45 customizeCellRenderer(value, index, isSelected, cellHasFocus);
46 return listCellRendererComponent;
49 public String getText() {
50 return myText;
53 protected void customizeCellRenderer(
54 Object value,
55 int index,
56 boolean selected,
57 boolean hasFocus
58 ) {
59 if (value instanceof PsiElement) {
60 PsiElement element = (PsiElement)value;
61 if (element.isValid()) {
62 PsiFile psiFile = element.getContainingFile();
63 Module module = ModuleUtil.findModuleForPsiElement(element);
64 final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex();
65 if (module != null) {
66 boolean inTestSource = false;
67 if (psiFile != null) {
68 VirtualFile vFile = psiFile.getVirtualFile();
69 if (vFile != null) {
70 inTestSource = fileIndex.isInTestSourceContent(vFile);
73 myText = module.getName();
74 if (inTestSource) {
75 setIcon(TEST_ICON);
77 else {
78 setIcon(module.getModuleType().getNodeIcon(false));
80 } else {
81 if (psiFile != null) {
82 VirtualFile vFile = psiFile.getVirtualFile();
83 if (vFile != null) {
84 final boolean isInLibraries = fileIndex.isInLibrarySource(vFile) || fileIndex.isInLibraryClasses(vFile);
85 if (isInLibraries){
86 setIcon(LIB_ICON);
87 for (OrderEntry order : fileIndex.getOrderEntriesForFile(vFile)) {
88 if (order instanceof LibraryOrderEntry || order instanceof JdkOrderEntry) {
89 myText = order.getPresentableName();
90 break;
93 } /*else {
94 setIcon(IconUtilEx.getEmptyIcon(false));
95 setText(value.toString());
96 }*/
101 else {
102 myText = "";
105 /*else {
106 setIcon(IconUtilEx.getEmptyIcon(false));
107 setText(value == null ? "" : value.toString());
109 setText(myText);
110 setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
111 setHorizontalTextPosition(SwingConstants.LEFT);
112 setBackground(selected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground());
113 setForeground(selected ? UIUtil.getListSelectionForeground() : UIUtil.getInactiveTextColor());