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
.codeInspection
.ui
;
19 import com
.intellij
.codeInspection
.CommonProblemDescriptor
;
20 import com
.intellij
.codeInspection
.ProblemDescriptor
;
21 import com
.intellij
.codeInspection
.ProblemHighlightType
;
22 import com
.intellij
.codeInspection
.ex
.DescriptorProviderInspection
;
23 import com
.intellij
.codeInspection
.ex
.ProblemDescriptorImpl
;
24 import com
.intellij
.codeInspection
.reference
.RefElement
;
25 import com
.intellij
.codeInspection
.reference
.RefEntity
;
26 import com
.intellij
.openapi
.util
.IconLoader
;
27 import com
.intellij
.openapi
.util
.TextRange
;
28 import com
.intellij
.openapi
.util
.text
.StringUtil
;
29 import com
.intellij
.openapi
.vcs
.FileStatus
;
30 import com
.intellij
.psi
.PsiElement
;
31 import org
.jetbrains
.annotations
.NonNls
;
32 import org
.jetbrains
.annotations
.Nullable
;
39 public class ProblemDescriptionNode
extends InspectionTreeNode
{
40 private static final Icon INFO
= IconLoader
.getIcon("/compiler/information.png");
41 private static final Icon ERROR
= IconLoader
.getIcon("/compiler/error.png");
42 private static final Icon WARNING
= IconLoader
.getIcon("/compiler/warning.png");
43 protected RefEntity myElement
;
44 private CommonProblemDescriptor myDescriptor
;
45 protected DescriptorProviderInspection myTool
;
47 public ProblemDescriptionNode(final Object userObject
,
48 final DescriptorProviderInspection tool
) {
53 public ProblemDescriptionNode(RefEntity element
,
54 CommonProblemDescriptor descriptor
,
55 DescriptorProviderInspection descriptorProviderInspection
) {
58 myDescriptor
= descriptor
;
59 myTool
= descriptorProviderInspection
;
63 public RefEntity
getElement() { return myElement
; }
65 public CommonProblemDescriptor
getDescriptor() { return myDescriptor
; }
67 public Icon
getIcon(boolean expanded
) {
68 if (myDescriptor
instanceof ProblemDescriptorImpl
) {
69 ProblemHighlightType problemHighlightType
= ((ProblemDescriptorImpl
)myDescriptor
).getHighlightType();
70 if (problemHighlightType
== ProblemHighlightType
.ERROR
) return ERROR
;
71 if (problemHighlightType
== ProblemHighlightType
.GENERIC_ERROR_OR_WARNING
) return WARNING
;
76 public int getProblemCount() {
80 public boolean isValid() {
81 if (myElement
instanceof RefElement
&& !((RefElement
)myElement
).isValid()) return false;
82 final CommonProblemDescriptor descriptor
= getDescriptor();
83 if (descriptor
instanceof ProblemDescriptor
) {
84 final PsiElement psiElement
= ((ProblemDescriptor
)descriptor
).getPsiElement();
85 return psiElement
!= null && psiElement
.isValid();
91 public boolean isResolved() {
92 return myElement
instanceof RefElement
&& myTool
.isElementIgnored(myElement
);
95 public void ignoreElement() {
96 myTool
.ignoreCurrentElement(getElement());
99 public void amnesty() {
100 myTool
.amnesty(getElement());
103 public FileStatus
getNodeStatus() {
104 if (myElement
instanceof RefElement
){
105 return myTool
.getProblemStatus(myDescriptor
);
107 return FileStatus
.NOT_CHANGED
;
110 public String
toString() {
111 return renderDescriptionMessage(getDescriptor());
114 private static String
renderDescriptionMessage(@Nullable CommonProblemDescriptor descriptor
) {
115 PsiElement psiElement
= descriptor
instanceof ProblemDescriptor ?
((ProblemDescriptor
)descriptor
).getPsiElement() : null;
116 @NonNls String message
= descriptor
!= null ? descriptor
.getDescriptionTemplate().replaceAll("<[^>]*>", "") : "";
117 message
= StringUtil
.replace(message
, "#loc", "");
118 message
= StringUtil
.replace(message
, "#ref", extractHighlightedText(descriptor
, psiElement
));
119 final int endIndex
= message
.indexOf("#end");
121 message
= message
.substring(0, endIndex
);
123 message
= StringUtil
.unescapeXml(message
);
127 public static String
extractHighlightedText(CommonProblemDescriptor descriptor
, PsiElement psiElement
) {
128 if (psiElement
== null || !psiElement
.isValid()) return "";
129 String ref
= psiElement
.getText();
130 if(descriptor
instanceof ProblemDescriptorImpl
) {
131 TextRange textRange
= ((ProblemDescriptorImpl
)descriptor
).getTextRange();
132 final TextRange elementRange
= psiElement
.getTextRange();
133 if (textRange
!=null && elementRange
!=null) {
134 textRange
= textRange
.shiftRight(-elementRange
.getStartOffset());
135 if(textRange
.getStartOffset() < 0 && textRange
.getEndOffset()<ref
.length())
136 ref
= textRange
.substring(ref
);