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
.util
.xml
.highlighting
;
19 import com
.intellij
.codeInsight
.daemon
.DaemonCodeAnalyzer
;
20 import com
.intellij
.codeInsight
.daemon
.impl
.DaemonCodeAnalyzerImpl
;
21 import com
.intellij
.codeInsight
.daemon
.impl
.TrafficLightRenderer
;
22 import com
.intellij
.codeInsight
.daemon
.impl
.SeverityRegistrar
;
23 import com
.intellij
.lang
.annotation
.HighlightSeverity
;
24 import com
.intellij
.openapi
.application
.ApplicationManager
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.util
.Condition
;
27 import com
.intellij
.openapi
.util
.IconLoader
;
28 import com
.intellij
.psi
.PsiDocumentManager
;
29 import com
.intellij
.psi
.PsiFile
;
30 import com
.intellij
.util
.Alarm
;
31 import com
.intellij
.util
.containers
.ContainerUtil
;
32 import com
.intellij
.util
.xml
.DomChangeAdapter
;
33 import com
.intellij
.util
.xml
.DomElement
;
34 import com
.intellij
.util
.xml
.DomManager
;
35 import com
.intellij
.util
.xml
.DomUtil
;
36 import com
.intellij
.util
.xml
.ui
.CommittablePanel
;
37 import com
.intellij
.util
.xml
.ui
.Highlightable
;
43 * User: Sergey.Vasiliev
45 public class DomElementsErrorPanel
extends JPanel
implements CommittablePanel
, Highlightable
{
46 private static final Icon ERRORS_FOUND_ICON
= IconLoader
.getIcon("/general/errorsInProgress.png");
48 private static final int ALARM_PERIOD
= 241;
50 private final Project myProject
;
51 private final DomElement
[] myDomElements
;
53 private final DomElementsTrafficLightRenderer myErrorStripeRenderer
;
54 private final DomElementAnnotationsManagerImpl myAnnotationsManager
;
56 private final Alarm myAlarm
= new Alarm();
58 public DomElementsErrorPanel(final DomElement
... domElements
) {
59 assert domElements
.length
> 0;
61 myDomElements
= domElements
;
62 final DomManager domManager
= domElements
[0].getManager();
63 myProject
= domManager
.getProject();
64 myAnnotationsManager
= (DomElementAnnotationsManagerImpl
)DomElementAnnotationsManager
.getInstance(myProject
);
66 setPreferredSize(getDimension());
68 myErrorStripeRenderer
= new DomElementsTrafficLightRenderer(DomUtil
.getFile(domElements
[0]));
71 domManager
.addDomEventListener(new DomChangeAdapter() {
72 protected void elementChanged(DomElement element
) {
78 public void updateHighlighting() {
82 private boolean areValid() {
83 for (final DomElement domElement
: myDomElements
) {
84 if (!domElement
.isValid()) return false;
89 private void updatePanel() {
90 myAlarm
.cancelAllRequests();
92 if (!areValid()) return;
95 setToolTipText(myErrorStripeRenderer
.getTooltipMessage());
97 if (!isHighlightingFinished()) {
102 private boolean isHighlightingFinished() {
103 return !areValid() || myAnnotationsManager
.isHighlightingFinished(myDomElements
);
106 private void addUpdateRequest() {
107 ApplicationManager
.getApplication().invokeLater(new Runnable() {
110 myAlarm
.addRequest(new Runnable() {
112 if (myProject
.isOpen() && !myProject
.isDisposed()) {
121 protected void paintComponent(Graphics g
) {
122 super.paintComponent(g
);
124 myErrorStripeRenderer
.paint(this, g
, new Rectangle(0, 0, getWidth(), getHeight()));
127 public void dispose() {
128 myAlarm
.cancelAllRequests();
131 public JComponent
getComponent() {
135 public void commit() {
138 public void reset() {
142 private static Dimension
getDimension() {
143 return new Dimension(ERRORS_FOUND_ICON
.getIconWidth() + 2, ERRORS_FOUND_ICON
.getIconHeight() + 2);
146 private class DomElementsTrafficLightRenderer
extends TrafficLightRenderer
{
148 public DomElementsTrafficLightRenderer(final PsiFile xmlFile
) {
149 super(xmlFile
.getProject(), (DaemonCodeAnalyzerImpl
)DaemonCodeAnalyzer
.getInstance(xmlFile
.getProject()),
150 PsiDocumentManager
.getInstance(xmlFile
.getProject()).getDocument(xmlFile
), xmlFile
);
153 protected DaemonCodeAnalyzerStatus
getDaemonCodeAnalyzerStatus(boolean fillErrorsCount
) {
154 final DaemonCodeAnalyzerStatus status
= super.getDaemonCodeAnalyzerStatus(fillErrorsCount
);
155 if (status
!= null && isInspectionCompleted()) {
156 status
.errorAnalyzingFinished
= true;
157 for (final DaemonCodeAnalyzerStatus
.PassStatus passStatus
: status
.passStati
) {
158 passStatus
.inProgressIcon
= null;
165 protected void fillDaemonCodeAnalyzerErrorsSatus(DaemonCodeAnalyzerStatus status
, boolean fillErrorsCount
) {
166 for (int i
= 0; i
< status
.errorCount
.length
; i
++) {
167 final HighlightSeverity minSeverity
= SeverityRegistrar
.getInstance(myProject
).getSeverityByIndex(i
);
168 if (minSeverity
== null) {
173 for (DomElement element
: myDomElements
) {
174 final DomElementsProblemsHolder holder
= myAnnotationsManager
.getCachedProblemHolder(element
);
175 sum
+= (SeverityRegistrar
.getInstance(getProject()).compare(minSeverity
, HighlightSeverity
.WARNING
) >= 0 ? holder
176 .getProblems(element
, true, true) : holder
.getProblems(element
, true, minSeverity
)).size();
178 status
.errorCount
[i
] = sum
;
182 protected boolean isInspectionCompleted() {
183 return ContainerUtil
.and(myDomElements
, new Condition
<DomElement
>() {
184 public boolean value(final DomElement element
) {
185 return myAnnotationsManager
.getHighlightStatus(element
) == DomHighlightStatus
.INSPECTIONS_FINISHED
;
190 protected boolean isErrorAnalyzingFinished() {
191 return ContainerUtil
.and(myDomElements
, new Condition
<DomElement
>() {
192 public boolean value(final DomElement element
) {
193 return myAnnotationsManager
.getHighlightStatus(element
).compareTo(DomHighlightStatus
.ANNOTATORS_FINISHED
) >= 0;