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.
16 package com
.intellij
.ide
;
18 import com
.intellij
.openapi
.compiler
.CompilerManager
;
19 import com
.intellij
.openapi
.fileTypes
.FileType
;
20 import com
.intellij
.openapi
.fileTypes
.FileTypeManager
;
21 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.roots
.FileIndexUtil
;
24 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
25 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
26 import com
.intellij
.openapi
.util
.Comparing
;
27 import com
.intellij
.openapi
.vfs
.VirtualFile
;
28 import com
.intellij
.psi
.*;
29 import com
.intellij
.ui
.LayeredIcon
;
30 import com
.intellij
.util
.Icons
;
37 public class JavaFileIconPatcher
implements FileIconPatcher
{
38 public Icon
patchIcon(final Icon baseIcon
, final VirtualFile file
, final int flags
, final Project project
) {
39 if (project
== null) {
43 Icon icon
= replaceIcon(file
, flags
, project
, baseIcon
);
45 final ProjectFileIndex fileIndex
= ProjectRootManager
.getInstance(project
).getFileIndex();
46 if (fileIndex
.isInSource(file
) && CompilerManager
.getInstance(project
).isExcludedFromCompilation(file
)) {
47 return new LayeredIcon(icon
, Icons
.EXCLUDED_FROM_COMPILE_ICON
);
53 private static Icon
replaceIcon(VirtualFile file
, int flags
, Project project
, Icon baseIcon
) {
54 FileType fileType
= FileTypeManager
.getInstance().getFileTypeByFile(file
);
55 if (fileType
== StdFileTypes
.JAVA
&& !FileIndexUtil
.isJavaSourceFile(project
, file
)) {
56 return Icons
.JAVA_OUTSIDE_SOURCE_ICON
;
59 PsiFile psiFile
= PsiManager
.getInstance(project
).findFile(file
);
60 if (psiFile
instanceof PsiClassOwner
) {
61 PsiClass
[] classes
= ((PsiClassOwner
)psiFile
).getClasses();
62 if (classes
.length
> 0) {
63 // prefer icon of the class named after file
64 final String fileName
= file
.getNameWithoutExtension();
65 for (PsiClass aClass
: classes
) {
66 if (!(aClass
instanceof SyntheticElement
) && Comparing
.strEqual(aClass
.getName(), fileName
)) {
67 return aClass
.getIcon(flags
);
70 final PsiClass lastClass
= classes
[classes
.length
- 1];
71 if (!(lastClass
instanceof SyntheticElement
)) {
72 return lastClass
.getIcon(flags
);