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.
21 package com
.intellij
.codeInspection
.ex
;
23 import com
.intellij
.codeHighlighting
.HighlightDisplayLevel
;
24 import com
.intellij
.codeInspection
.InspectionProfile
;
25 import com
.intellij
.codeInspection
.InspectionProfileEntry
;
26 import com
.intellij
.openapi
.diagnostic
.Logger
;
27 import com
.intellij
.openapi
.util
.Comparing
;
28 import com
.intellij
.openapi
.util
.InvalidDataException
;
29 import com
.intellij
.openapi
.util
.WriteExternalException
;
30 import com
.intellij
.packageDependencies
.DefaultScopesProvider
;
31 import com
.intellij
.packageDependencies
.DependencyValidationManager
;
32 import com
.intellij
.profile
.ProfileManager
;
33 import com
.intellij
.profile
.codeInspection
.SeverityProvider
;
34 import com
.intellij
.psi
.PsiElement
;
35 import com
.intellij
.psi
.search
.scope
.packageSet
.NamedScope
;
36 import com
.intellij
.psi
.search
.scope
.packageSet
.NamedScopesHolder
;
37 import com
.intellij
.psi
.search
.scope
.packageSet
.PackageSet
;
38 import org
.jdom
.Element
;
39 import org
.jetbrains
.annotations
.NotNull
;
40 import org
.jetbrains
.annotations
.Nullable
;
42 import java
.util
.ArrayList
;
43 import java
.util
.Collections
;
44 import java
.util
.List
;
46 public class ToolsImpl
implements Tools
{
47 private static final Logger LOG
= Logger
.getInstance("#" + ToolsImpl
.class.getName());
48 public static final String ENABLED_TAG
= "enabled_by_default";
50 private final String myShortName
;
51 private final ScopeToolState myDefaultState
;
52 private List
<ScopeToolState
> myTools
;
53 private boolean myEnabled
;
55 public ToolsImpl(@NotNull InspectionProfileEntry tool
, HighlightDisplayLevel level
, boolean enabled
) {
56 myShortName
= tool
.getShortName();
58 myDefaultState
= new ScopeToolState(DefaultScopesProvider
.getAllScope(), tool
, enabled
, level
);
62 public ScopeToolState
addTool(NamedScope scope
, @NotNull InspectionProfileEntry tool
, boolean enabled
, HighlightDisplayLevel level
) {
63 return insertTool(scope
, tool
, enabled
, level
, myTools
!= null ? myTools
.size() : 0);
66 public ScopeToolState
prependTool(NamedScope scope
, @NotNull InspectionProfileEntry tool
, boolean enabled
, HighlightDisplayLevel level
) {
67 return insertTool(scope
, tool
, enabled
, level
, 0);
70 private ScopeToolState
insertTool(NamedScope scope
, InspectionProfileEntry tool
, boolean enabled
, HighlightDisplayLevel level
, int idx
) {
71 if (myTools
== null) {
72 myTools
= new ArrayList
<ScopeToolState
>();
75 final ScopeToolState scopeToolState
= new ScopeToolState(scope
, tool
, enabled
, level
);
76 myTools
.add(idx
, scopeToolState
);
77 return scopeToolState
;
81 public InspectionProfileEntry
getInspectionTool(PsiElement element
) {
82 if (myTools
!= null) {
83 for (ScopeToolState state
: myTools
) {
84 if (element
== null) {
85 return state
.getTool();
88 final DependencyValidationManager validationManager
= DependencyValidationManager
.getInstance(element
.getProject());
89 final NamedScope scope
= state
.getScope();
91 final PackageSet packageSet
= scope
.getValue();
92 if (packageSet
!= null && packageSet
.contains(element
.getContainingFile(), validationManager
)) {
93 return state
.getTool();
99 for (ScopeToolState state
: getTools()) {
100 if (state
.getScope() == null) {
101 return state
.getTool();
105 return myDefaultState
.getTool();
108 public String
getShortName() {
112 public List
<InspectionProfileEntry
> getAllTools() {
113 final List
<InspectionProfileEntry
> result
= new ArrayList
<InspectionProfileEntry
>();
114 for (ScopeToolState state
: getTools()) {
115 result
.add(state
.getTool());
120 public void writeExternal(Element inspectionElement
) throws WriteExternalException
{
121 if (myTools
!= null) {
122 for (ScopeToolState state
: myTools
) {
123 final Element scopeElement
= new Element("scope");
124 scopeElement
.setAttribute("name", state
.getScopeName());
125 scopeElement
.setAttribute(InspectionProfileImpl
.LEVEL_TAG
, state
.getLevel().toString());
126 scopeElement
.setAttribute(InspectionProfileImpl
.ENABLED_TAG
, Boolean
.toString(state
.isEnabled()));
127 InspectionProfileEntry InspectionProfileEntry
= state
.getTool();
128 InspectionProfileEntry
.writeSettings(scopeElement
);
129 inspectionElement
.addContent(scopeElement
);
132 inspectionElement
.setAttribute(InspectionProfileImpl
.ENABLED_TAG
, Boolean
.toString(isEnabled()));
133 inspectionElement
.setAttribute(InspectionProfileImpl
.LEVEL_TAG
, getLevel().toString());
134 inspectionElement
.setAttribute(ENABLED_TAG
, Boolean
.toString(myDefaultState
.isEnabled()));
135 myDefaultState
.getTool().writeSettings(inspectionElement
);
138 public void readExternal(Element toolElement
, InspectionProfile profile
) throws InvalidDataException
{
139 final String levelName
= toolElement
.getAttributeValue(InspectionProfileImpl
.LEVEL_TAG
);
140 final ProfileManager profileManager
= profile
.getProfileManager();
141 HighlightDisplayLevel level
=
142 HighlightDisplayLevel
.find(((SeverityProvider
)profileManager
).getOwnSeverityRegistrar().getSeverity(levelName
));
143 if (level
== null || level
== HighlightDisplayLevel
.DO_NOT_SHOW
) {//from old profiles
144 level
= HighlightDisplayLevel
.WARNING
;
146 myDefaultState
.setLevel(level
);
147 final String enabled
= toolElement
.getAttributeValue(InspectionProfileImpl
.ENABLED_TAG
);
148 final boolean isEnabled
= enabled
!= null && Boolean
.parseBoolean(enabled
);
149 myEnabled
= isEnabled
;
151 final String enabledTool
= toolElement
.getAttributeValue(ENABLED_TAG
);
152 myDefaultState
.setEnabled(enabledTool
!= null ? Boolean
.parseBoolean(enabledTool
) : isEnabled
);
153 final InspectionProfileEntry tool
= myDefaultState
.getTool();
154 tool
.readSettings(toolElement
);
155 final List children
= toolElement
.getChildren(InspectionProfileImpl
.SCOPE
);
156 if (!children
.isEmpty()) {
157 for (Object sO
: children
) {
158 final Element scopeElement
= (Element
)sO
;
159 final String scopeName
= scopeElement
.getAttributeValue(InspectionProfileImpl
.NAME
);
160 if (scopeName
!= null) {
161 final NamedScopesHolder scopesHolder
= profileManager
.getScopesManager();
162 NamedScope namedScope
= null;
163 if (scopesHolder
!= null) {
164 namedScope
= scopesHolder
.getScope(scopeName
);
166 final String errorLevel
= scopeElement
.getAttributeValue(InspectionProfileImpl
.LEVEL_TAG
);
167 final String enabledInScope
= scopeElement
.getAttributeValue(InspectionProfileImpl
.ENABLED_TAG
);
168 final InspectionProfileEntry copyTool
=
169 ((InspectionProfileImpl
)profile
).myRegistrar
.createInspectionTool(myShortName
, tool
);
170 copyTool
.readSettings(scopeElement
);
171 HighlightDisplayLevel scopeLevel
= errorLevel
!= null ?
172 HighlightDisplayLevel
.find(((SeverityProvider
)profileManager
).getOwnSeverityRegistrar().getSeverity(errorLevel
)) : null;
173 if (scopeLevel
== null) {
176 if (namedScope
!= null) {
177 addTool(namedScope
, copyTool
, enabledInScope
!= null && Boolean
.parseBoolean(enabledInScope
), scopeLevel
);
179 addTool(scopeName
, copyTool
, enabledInScope
!= null && Boolean
.parseBoolean(enabledInScope
), scopeLevel
);
186 public ScopeToolState
addTool(String scopeName
, InspectionProfileEntry tool
, boolean enabled
, HighlightDisplayLevel level
) {
187 if (myTools
== null) {
188 myTools
= new ArrayList
<ScopeToolState
>();
191 final ScopeToolState scopeToolState
= new ScopeToolState(scopeName
, tool
, enabled
, level
);
192 myTools
.add(scopeToolState
);
193 return scopeToolState
;
196 public InspectionProfileEntry
getTool() {
197 if (myTools
== null) return myDefaultState
.getTool();
198 return myTools
.iterator().next().getTool();
202 public List
<ScopeToolState
> getTools() {
203 if (myTools
== null) return Collections
.singletonList(myDefaultState
);
204 List
<ScopeToolState
> result
= new ArrayList
<ScopeToolState
>(myTools
);
205 result
.add(myDefaultState
);
210 public ScopeToolState
getDefaultState() {
211 return myDefaultState
;
214 public List
<NamedScope
> getScopes() {
215 final List
<NamedScope
> result
= new ArrayList
<NamedScope
>();
216 if (myTools
!= null) {
217 for (ScopeToolState state
: myTools
) {
218 result
.add(state
.getScope());
227 public void removeScope(int scopeIdx
) {
228 if (myTools
!= null && scopeIdx
>= 0 && myTools
.size() > scopeIdx
) {
229 myTools
.remove(scopeIdx
);
230 if (myTools
.isEmpty()) {
232 setEnabled(myDefaultState
.isEnabled());
237 public void removeAllScopes() {
241 public void setScope(int idx
, NamedScope namedScope
) {
242 if (myTools
!= null && myTools
.size() > idx
&& idx
>= 0) {
243 final ScopeToolState scopeToolState
= myTools
.get(idx
);
244 final InspectionProfileEntry tool
= scopeToolState
.getTool();
246 myTools
.add(idx
, new ScopeToolState(namedScope
, tool
, scopeToolState
.isEnabled(), scopeToolState
.getLevel()));
250 public void moveScope(int idx
, int dir
) {
251 if (myTools
!= null && idx
>= 0 && idx
< myTools
.size() && idx
+ dir
>= 0 && idx
+ dir
< myTools
.size()) {
252 final ScopeToolState state
= myTools
.get(idx
);
253 myTools
.set(idx
, myTools
.get(idx
+ dir
));
254 myTools
.set(idx
+ dir
, state
);
258 public boolean isEnabled(NamedScope namedScope
) {
259 if (!myEnabled
) return false;
260 if (namedScope
!= null && myTools
!= null) {
261 for (ScopeToolState state
: myTools
) {
262 if (Comparing
.equal(namedScope
, state
.getScope())) return state
.isEnabled();
265 return myDefaultState
.isEnabled();
268 public HighlightDisplayLevel
getLevel(PsiElement element
) {
269 if (myTools
== null || element
== null) return myDefaultState
.getLevel();
270 final DependencyValidationManager manager
= DependencyValidationManager
.getInstance(element
.getProject());
271 for (ScopeToolState state
: myTools
) {
272 final NamedScope scope
= state
.getScope();
273 final PackageSet set
= scope
!= null ? scope
.getValue() : null;
274 if (set
!= null && set
.contains(element
.getContainingFile(), manager
)) {
275 return state
.getLevel();
278 return myDefaultState
.getLevel();
283 public HighlightDisplayLevel
getLevel() {
284 return myDefaultState
.getLevel();
287 public boolean isEnabled() {
292 public boolean isEnabled(PsiElement element
) {
293 if (!myEnabled
) return false;
294 if (myTools
== null || element
== null) return myDefaultState
.isEnabled();
295 final DependencyValidationManager manager
= DependencyValidationManager
.getInstance(element
.getProject());
296 for (ScopeToolState state
: myTools
) {
297 final NamedScope scope
= state
.getScope();
299 final PackageSet set
= scope
.getValue();
300 if (set
!= null && set
.contains(element
.getContainingFile(), manager
)) {
301 return state
.isEnabled();
305 return myDefaultState
.isEnabled();
309 public InspectionTool
getEnabledTool(PsiElement element
) {
310 if (!myEnabled
) return null;
311 if (myTools
== null || element
== null) return myDefaultState
.isEnabled() ?
(InspectionTool
)myDefaultState
.getTool() : null;
312 final DependencyValidationManager manager
= DependencyValidationManager
.getInstance(element
.getProject());
313 for (ScopeToolState state
: myTools
) {
314 final NamedScope scope
= state
.getScope();
316 final PackageSet set
= scope
.getValue();
317 if (set
!= null && set
.contains(element
.getContainingFile(), manager
)) {
318 return state
.isEnabled() ?
(InspectionTool
)state
.getTool() : null;
322 return myDefaultState
.isEnabled() ?
(InspectionTool
)myDefaultState
.getTool() : null;
325 public void setEnabled(boolean enabled
) {
329 public void enableTool(NamedScope namedScope
) {
330 if (myTools
!= null) {
331 for (ScopeToolState state
: myTools
) {
332 if (Comparing
.equal(state
.getScope(), namedScope
)) {
333 state
.setEnabled(true);
340 public void disableTool(NamedScope namedScope
) {
341 if (myTools
!= null) {
342 for (ScopeToolState state
: myTools
) {
343 if (Comparing
.equal(state
.getScope(), namedScope
)) {
344 state
.setEnabled(false);
351 public void disableTool(PsiElement element
) {
352 if (element
== null){
353 myDefaultState
.setEnabled(false);
357 final DependencyValidationManager validationManager
= DependencyValidationManager
.getInstance(element
.getProject());
358 if (myTools
!= null) {
359 for (ScopeToolState state
: myTools
) {
360 final NamedScope scope
= state
.getScope();
362 final PackageSet packageSet
= scope
.getValue();
363 if (packageSet
!= null && packageSet
.contains(element
.getContainingFile(), validationManager
)) {
364 state
.setEnabled(false);
369 myDefaultState
.setEnabled(false);
371 myDefaultState
.setEnabled(false);
376 public HighlightDisplayLevel
getLevel(final NamedScope scope
) {
377 if (myTools
!= null && scope
!= null){
378 for (ScopeToolState state
: myTools
) {
379 if (Comparing
.equal(state
.getScope(), scope
)) {
380 return state
.getLevel();
384 return myDefaultState
.getLevel();
387 public boolean equalTo(ToolsImpl tools
) {
388 if (myEnabled
!= tools
.myEnabled
) return false;
389 if (getTools().size() != tools
.getTools().size()) return false;
390 for (int i
= 0; i
< getTools().size(); i
++) {
391 final ScopeToolState state
= getTools().get(i
);
392 final ScopeToolState toolState
= tools
.getTools().get(i
);
393 if (!state
.equalTo(toolState
)){
403 public void setLevel(HighlightDisplayLevel level
, int idx
) {
404 if (myTools
!= null && myTools
.size() > idx
&& idx
>= 0) {
405 final ScopeToolState scopeToolState
= myTools
.get(idx
);
407 final NamedScope scope
= scopeToolState
.getScope();
409 myTools
.add(idx
, new ScopeToolState(scope
, scopeToolState
.getTool(), scopeToolState
.isEnabled(), level
));
411 myTools
.add(idx
, new ScopeToolState(scopeToolState
.getScopeName(), scopeToolState
.getTool(), scopeToolState
.isEnabled(), level
));
413 } else if (idx
== - 1) {
414 myDefaultState
.setLevel(level
);
418 public void setDefaultState(InspectionProfileEntry inspectionTool
, boolean enabled
, HighlightDisplayLevel level
) {
419 myDefaultState
.setTool(inspectionTool
);
420 myDefaultState
.setLevel(level
);
421 myDefaultState
.setEnabled(enabled
);
424 public void setLevel(HighlightDisplayLevel level
) {
425 myDefaultState
.setLevel(level
);
429 public List
<ScopeToolState
> getNonDefaultTools() {