903b05f41ebc4524a77399dfd791dba5988f0b6b
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / projectRoot / daemon / ProjectStructureProblemsHolderImpl.java
1 package com.intellij.openapi.roots.ui.configuration.projectRoot.daemon;
2
3 import com.intellij.util.SmartList;
4 import com.intellij.xml.util.XmlStringUtil;
5 import org.jetbrains.annotations.NotNull;
6 import org.jetbrains.annotations.Nullable;
7
8 import java.util.Collections;
9 import java.util.List;
10
11 public class ProjectStructureProblemsHolderImpl implements ProjectStructureProblemsHolder {
12   private List<ProjectStructureProblemDescription> myProblemDescriptions;
13
14   @Override
15   public void registerProblem(@NotNull String message, @Nullable String description,
16                               @NotNull ProjectStructureProblemType problemType,
17                               @NotNull PlaceInProjectStructure place,
18                               @Nullable ConfigurationErrorQuickFix fix) {
19     final List<ConfigurationErrorQuickFix> fixes = fix != null ? Collections.singletonList(fix) : Collections.emptyList();
20     registerProblem(new ProjectStructureProblemDescription(message, description, place, problemType, fixes));
21   }
22
23   @Override
24   public void registerProblem(final @NotNull ProjectStructureProblemDescription description) {
25     if (myProblemDescriptions == null) {
26       myProblemDescriptions = new SmartList<>();
27     }
28     myProblemDescriptions.add(description);
29   }
30
31   public String composeTooltipMessage() {
32     final StringBuilder buf = new StringBuilder();
33     buf.append("<html><body>");
34     if (myProblemDescriptions != null) {
35       int problems = 0;
36       for (ProjectStructureProblemDescription problemDescription : myProblemDescriptions) {
37         buf.append(XmlStringUtil.convertToHtmlContent(problemDescription.getMessage(false))).append("<br>");
38         problems++;
39         if (problems >= 10 && myProblemDescriptions.size() > 12) {
40           buf.append(myProblemDescriptions.size() - problems).append(" more problems...<br>");
41           break;
42         }
43       }
44     }
45     buf.append("</body></html>");
46     return buf.toString();
47   }
48
49   public boolean containsProblems() {
50     return myProblemDescriptions != null && !myProblemDescriptions.isEmpty();
51   }
52
53   public boolean containsProblems(final ProjectStructureProblemType.Severity severity) {
54     if (myProblemDescriptions != null) {
55       for (ProjectStructureProblemDescription description : myProblemDescriptions) {
56         if (description.getSeverity() == severity) {
57           return true;
58         }
59       }
60     }
61     return false;
62   }
63
64   public void removeProblem(@NotNull ProjectStructureProblemDescription description) {
65     if (myProblemDescriptions != null) {
66       myProblemDescriptions.remove(description);
67     }
68   }
69
70   @Nullable
71   public List<ProjectStructureProblemDescription> getProblemDescriptions() {
72     return myProblemDescriptions;
73   }
74 }