constructor reference: don't ignore constructor parameters during method reference...
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / OrderEntryAppearanceServiceImpl.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.openapi.roots.ui;
3
4 import com.intellij.openapi.module.Module;
5 import com.intellij.openapi.module.ModuleType;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.project.ProjectBundle;
8 import com.intellij.openapi.projectRoots.Sdk;
9 import com.intellij.openapi.projectRoots.SdkType;
10 import com.intellij.openapi.roots.*;
11 import com.intellij.openapi.roots.impl.libraries.LibraryEx;
12 import com.intellij.openapi.roots.libraries.Library;
13 import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
14 import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager;
15 import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
16 import com.intellij.openapi.roots.ui.util.CompositeAppearance;
17 import com.intellij.openapi.roots.ui.util.SimpleTextCellAppearance;
18 import com.intellij.openapi.util.IconLoader;
19 import com.intellij.openapi.util.SystemInfo;
20 import com.intellij.openapi.util.SystemInfo;
21 import com.intellij.openapi.util.text.StringUtil;
22 import com.intellij.openapi.vfs.JarFileSystem;
23 import com.intellij.openapi.vfs.VfsUtilCore;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.openapi.vfs.VirtualFileManager;
26 import com.intellij.openapi.vfs.impl.LightFilePointer;
27 import com.intellij.ui.JBColor;
28 import com.intellij.ui.SimpleTextAttributes;
29 import com.intellij.util.PathUtil;
30 import com.intellij.util.PlatformIcons;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 import javax.swing.*;
35 import java.awt.*;
36 import java.io.File;
37
38 public class OrderEntryAppearanceServiceImpl extends OrderEntryAppearanceService {
39
40   private static final String NO_JDK = ProjectBundle.message("jdk.missing.item");
41
42   @NotNull
43   @Override
44   public CellAppearanceEx forOrderEntry(Project project, @NotNull final OrderEntry orderEntry, final boolean selected) {
45     if (orderEntry instanceof JdkOrderEntry) {
46       JdkOrderEntry jdkLibraryEntry = (JdkOrderEntry)orderEntry;
47       Sdk jdk = jdkLibraryEntry.getJdk();
48       if (!orderEntry.isValid()) {
49         final String oldJdkName = jdkLibraryEntry.getJdkName();
50         return FileAppearanceService.getInstance().forInvalidUrl(oldJdkName != null ? oldJdkName : NO_JDK);
51       }
52       return forJdk(jdk, false, selected, true);
53     }
54     else if (!orderEntry.isValid()) {
55       return FileAppearanceService.getInstance().forInvalidUrl(orderEntry.getPresentableName());
56     }
57     else if (orderEntry instanceof LibraryOrderEntry) {
58       LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)orderEntry;
59       if (!libraryOrderEntry.isValid()) { //library can be removed
60         return FileAppearanceService.getInstance().forInvalidUrl(orderEntry.getPresentableName());
61       }
62       Library library = libraryOrderEntry.getLibrary();
63       assert library != null : libraryOrderEntry;
64       return forLibrary(project, library, !((LibraryEx)library).getInvalidRootUrls(OrderRootType.CLASSES).isEmpty());
65     }
66     else if (orderEntry.isSynthetic()) {
67       String presentableName = orderEntry.getPresentableName();
68       Icon icon = orderEntry instanceof ModuleSourceOrderEntry ? sourceFolderIcon(false) : null;
69       return new SimpleTextCellAppearance(presentableName, icon, SimpleTextAttributes.SYNTHETIC_ATTRIBUTES);
70     }
71     else if (orderEntry instanceof ModuleOrderEntry) {
72       final Icon icon = ModuleType.get(((ModuleOrderEntry)orderEntry).getModule()).getIcon();
73       return SimpleTextCellAppearance.regular(orderEntry.getPresentableName(), icon);
74     }
75     else {
76       return CompositeAppearance.single(orderEntry.getPresentableName());
77     }
78   }
79
80   @NotNull
81   @Override
82   public CellAppearanceEx forLibrary(Project project, @NotNull final Library library, final boolean hasInvalidRoots) {
83     final StructureConfigurableContext context = ProjectStructureConfigurable.getInstance(project).getContext();
84     final Icon icon = LibraryPresentationManager.getInstance().getCustomIcon(library, context);
85
86     final String name = library.getName();
87     if (name != null) {
88       return normalOrRedWaved(name, (icon != null ? icon : PlatformIcons.LIBRARY_ICON), hasInvalidRoots);
89     }
90
91     final String[] files = library.getUrls(OrderRootType.CLASSES);
92     if (files.length == 0) {
93       return SimpleTextCellAppearance.invalid(ProjectBundle.message("library.empty.library.item"), PlatformIcons.LIBRARY_ICON);
94     }
95     else if (files.length == 1) {
96       return forVirtualFilePointer(new LightFilePointer(files[0]));
97     }
98
99     final String url = StringUtil.trimEnd(files[0], JarFileSystem.JAR_SEPARATOR);
100     String text = ProjectBundle.message("library.unnamed.text", PathUtil.getFileName(url), files.length - 1);
101     return SimpleTextCellAppearance.regular(text, PlatformIcons.LIBRARY_ICON);
102   }
103
104   @NotNull
105   @Override
106   public CellAppearanceEx forJdk(@Nullable final Sdk jdk, final boolean isInComboBox, final boolean selected, final boolean showVersion) {
107     if (jdk == null) {
108       return FileAppearanceService.getInstance().forInvalidUrl(NO_JDK);
109     }
110
111     String name = jdk.getName();
112     CompositeAppearance appearance = new CompositeAppearance();
113     SdkType sdkType = (SdkType)jdk.getSdkType();
114     appearance.setIcon(sdkType.getIcon());
115     SimpleTextAttributes attributes = getTextAttributes(sdkType.sdkHasValidPath(jdk), selected);
116     CompositeAppearance.DequeEnd ending = appearance.getEnding();
117     ending.addText(name, attributes);
118
119     if (showVersion) {
120       String versionString = jdk.getVersionString();
121       if (versionString != null && !versionString.equals(name)) {
122         SimpleTextAttributes textAttributes = isInComboBox && !selected ? SimpleTextAttributes.SYNTHETIC_ATTRIBUTES :
123                                               SystemInfo.isMac && selected ? new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN,
124                                                                                                         Color.WHITE) : SimpleTextAttributes.GRAY_ATTRIBUTES;
125         ending.addComment(versionString, textAttributes);
126       }
127     }
128
129     return ending.getAppearance();
130   }
131
132   private static SimpleTextAttributes getTextAttributes(final boolean valid, final boolean selected) {
133     if (!valid) {
134       return SimpleTextAttributes.ERROR_ATTRIBUTES;
135     }
136     else if (selected && !(SystemInfo.isWinVistaOrNewer && UIManager.getLookAndFeel().getName().contains("Windows"))) {
137       return SimpleTextAttributes.SELECTED_SIMPLE_CELL_ATTRIBUTES;
138     }
139     else {
140       return SimpleTextAttributes.SIMPLE_CELL_ATTRIBUTES;
141     }
142   }
143
144   @NotNull
145   @Override
146   public CellAppearanceEx forContentFolder(@NotNull final ContentFolder folder) {
147     if (folder instanceof SourceFolder) {
148       return formatRelativePath(folder, PlatformIcons.FOLDER_ICON);
149     }
150     else if (folder instanceof ExcludeFolder) {
151       return formatRelativePath(folder, IconLoader.getDisabledIcon(PlatformIcons.FOLDER_ICON));
152     }
153     else {
154       throw new RuntimeException(folder.getClass().getName());
155     }
156   }
157
158   @NotNull
159   @Override
160   public CellAppearanceEx forModule(@NotNull final Module module) {
161     return SimpleTextCellAppearance.regular(module.getName(), ModuleType.get(module).getIcon());
162   }
163
164   @NotNull
165   private static Icon sourceFolderIcon(final boolean testSource) {
166     return testSource ? PlatformIcons.TEST_SOURCE_FOLDER : PlatformIcons.SOURCE_FOLDERS_ICON;
167   }
168
169   @NotNull
170   private static CellAppearanceEx normalOrRedWaved(@NotNull final String text, @Nullable final Icon icon, final boolean waved) {
171     return waved ? new SimpleTextCellAppearance(text, icon, new SimpleTextAttributes(SimpleTextAttributes.STYLE_WAVED, null, JBColor.RED))
172                  : SimpleTextCellAppearance.regular(text, icon);
173   }
174
175   @NotNull
176   private static CellAppearanceEx forVirtualFilePointer(@NotNull final LightFilePointer filePointer) {
177     final VirtualFile file = filePointer.getFile();
178     return file != null ? FileAppearanceService.getInstance().forVirtualFile(file)
179                         : FileAppearanceService.getInstance().forInvalidUrl(filePointer.getPresentableUrl());
180   }
181
182   @NotNull
183   private static CellAppearanceEx formatRelativePath(@NotNull final ContentFolder folder, @NotNull final Icon icon) {
184     LightFilePointer folderFile = new LightFilePointer(folder.getUrl());
185     VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(folder.getContentEntry().getUrl());
186     if (file == null) return FileAppearanceService.getInstance().forInvalidUrl(folderFile.getPresentableUrl());
187
188     String contentPath = file.getPath();
189     String relativePath;
190     SimpleTextAttributes textAttributes;
191     VirtualFile folderFileFile = folderFile.getFile();
192     if (folderFileFile == null) {
193       String absolutePath = folderFile.getPresentableUrl();
194       relativePath = absolutePath.startsWith(contentPath) ? absolutePath.substring(contentPath.length()) : absolutePath;
195       textAttributes = SimpleTextAttributes.ERROR_ATTRIBUTES;
196     }
197     else {
198       relativePath = VfsUtilCore.getRelativePath(folderFileFile, file, File.separatorChar);
199       textAttributes = SimpleTextAttributes.REGULAR_ATTRIBUTES;
200     }
201
202     relativePath = StringUtil.isEmpty(relativePath) ? "." + File.separatorChar : relativePath;
203     return new SimpleTextCellAppearance(relativePath, icon, textAttributes);
204   }
205 }