coverage: show statistics for file in 'Project Files' view (WEB-17006)
[idea/community.git] / plugins / coverage-common / src / com / intellij / coverage / CoverageProjectViewDirectoryNodeDecorator.java
1 package com.intellij.coverage;
2
3 import com.intellij.ide.projectView.PresentationData;
4 import com.intellij.ide.projectView.ProjectViewNode;
5 import com.intellij.packageDependencies.ui.PackageDependenciesNode;
6 import com.intellij.psi.PsiDirectory;
7 import com.intellij.psi.PsiElement;
8 import com.intellij.psi.PsiFile;
9 import com.intellij.psi.SmartPsiElementPointer;
10 import com.intellij.ui.ColoredTreeCellRenderer;
11
12 /**
13  * @author yole
14  */
15 public class CoverageProjectViewDirectoryNodeDecorator extends AbstractCoverageProjectViewNodeDecorator {
16   public CoverageProjectViewDirectoryNodeDecorator(final CoverageDataManager coverageDataManager) {
17     super(coverageDataManager);
18   }
19
20   public void decorate(PackageDependenciesNode node, ColoredTreeCellRenderer cellRenderer) {
21     final PsiElement element = node.getPsiElement();
22     if (element == null || !element.isValid()) {
23       return;
24     }
25
26     final CoverageDataManager manager = getCoverageDataManager();
27     final CoverageSuitesBundle currentSuite = manager.getCurrentSuitesBundle();
28     final CoverageAnnotator coverageAnnotator = currentSuite != null ? currentSuite.getAnnotator(element.getProject()) : null;
29     if (coverageAnnotator == null) {
30       // N/A
31       return;
32     }
33
34     String informationString = null;
35     if (element instanceof PsiDirectory) {
36       informationString = coverageAnnotator.getDirCoverageInformationString((PsiDirectory) element, currentSuite, manager);
37     }
38     else if (element instanceof PsiFile) {
39       informationString = coverageAnnotator.getFileCoverageInformationString((PsiFile)element, currentSuite, manager);
40     }
41     if (informationString != null) {
42       appendCoverageInfo(cellRenderer, informationString);
43     }
44   }
45
46   public void decorate(ProjectViewNode node, PresentationData data) {
47     final CoverageDataManager manager = getCoverageDataManager();
48     final CoverageSuitesBundle currentSuite = manager.getCurrentSuitesBundle();
49     final CoverageAnnotator coverageAnnotator = currentSuite != null ? currentSuite.getAnnotator(node.getProject())
50                                                                      : null;
51     if (coverageAnnotator == null) {
52       // N/A
53       return;
54     }
55
56     final Object value = node.getValue();
57     PsiElement element = null;
58     if (value instanceof PsiElement) {
59       element = (PsiElement)value;
60     }
61     else if (value instanceof SmartPsiElementPointer) {
62       element = ((SmartPsiElementPointer)value).getElement();
63     }
64
65     String informationString = null;
66     if (element instanceof PsiDirectory) {
67       informationString = coverageAnnotator.getDirCoverageInformationString((PsiDirectory)element, currentSuite, manager);
68     } else if (element instanceof PsiFile) {
69       informationString = coverageAnnotator.getFileCoverageInformationString((PsiFile)element, currentSuite, manager);
70     }
71
72     if (informationString != null) {
73       data.setLocationString(informationString);
74     }
75   }
76
77 }