@NotNull
 @Override
 public InspectionToolWrapper getInspectionTool(PsiElement element) {
   if (myTools != null) {
     final PsiFile containingFile = element == null ? null : element.getContainingFile();
     final Project project = containingFile == null ? null : containingFile.getProject();
     for (ScopeToolState state : myTools) {
       if (element == null) {
         return state.getTool();
       }
       NamedScope scope = state.getScope(project);
       if (scope != null) {
         final PackageSet packageSet = scope.getValue();
         if (packageSet != null) {
           if (containingFile != null
               && packageSet.contains(
                   containingFile, DependencyValidationManager.getInstance(project))) {
             return state.getTool();
           }
         }
       }
     }
   }
   return myDefaultState.getTool();
 }
  protected HighlightSeverity getSeverity(@NotNull RefElement element) {
    final PsiElement psiElement = element.getPointer().getContainingFile();
    if (psiElement != null) {
      final GlobalInspectionContextImpl context = getContext();
      final String shortName = getSeverityDelegateName();
      final Tools tools = context.getTools().get(shortName);
      if (tools != null) {
        for (ScopeToolState state : tools.getTools()) {
          InspectionToolWrapper toolWrapper = state.getTool();
          if (toolWrapper == getToolWrapper()) {
            return context
                .getCurrentProfile()
                .getErrorLevel(HighlightDisplayKey.find(shortName), psiElement)
                .getSeverity();
          }
        }
      }

      final InspectionProfile profile =
          InspectionProjectProfileManager.getInstance(context.getProject()).getInspectionProfile();
      final HighlightDisplayLevel level =
          profile.getErrorLevel(HighlightDisplayKey.find(shortName), psiElement);
      return level.getSeverity();
    }
    return null;
  }
 public void setDefaultState(
     @NotNull InspectionToolWrapper toolWrapper,
     boolean enabled,
     @NotNull HighlightDisplayLevel level) {
   myDefaultState.setTool(toolWrapper);
   myDefaultState.setLevel(level);
   myDefaultState.setEnabled(enabled);
 }
 public void disableTool(NamedScope namedScope, Project project) {
   if (myTools != null) {
     for (ScopeToolState state : myTools) {
       if (Comparing.equal(state.getScope(project), namedScope)) {
         state.setEnabled(false);
       }
     }
   }
 }
 @NotNull
 public List<InspectionToolWrapper> getAllTools() {
   List<InspectionToolWrapper> result = new ArrayList<InspectionToolWrapper>();
   for (ScopeToolState state : getTools()) {
     InspectionToolWrapper toolWrapper = state.getTool();
     result.add(toolWrapper);
   }
   return result;
 }
 public boolean isEnabled(NamedScope namedScope, Project project) {
   if (!myEnabled) return false;
   if (namedScope != null && myTools != null) {
     for (ScopeToolState state : myTools) {
       if (Comparing.equal(namedScope, state.getScope(project))) return state.isEnabled();
     }
   }
   return myDefaultState.isEnabled();
 }
 public Set<HighlightSeverity> getUsedSeverities() {
   LOG.assertTrue(myInitialized);
   final Set<HighlightSeverity> result = new HashSet<HighlightSeverity>();
   for (ToolsImpl tools : myTools.values()) {
     for (ScopeToolState state : tools.getTools()) {
       result.add(state.getLevel().getSeverity());
     }
   }
   return result;
 }
 public void setLevel(
     @NotNull HighlightDisplayLevel level, @Nullable String scopeName, Project project) {
   if (scopeName == null) {
     myDefaultState.setLevel(level);
   } else {
     if (myTools == null) {
       return;
     }
     ScopeToolState scopeToolState = null;
     int index = -1;
     for (int i = 0; i < myTools.size(); i++) {
       ScopeToolState tool = myTools.get(i);
       if (scopeName.equals(tool.getScopeName())) {
         scopeToolState = tool;
         myTools.remove(tool);
         index = i;
         break;
       }
     }
     if (index < 0) {
       throw new IllegalStateException("Scope " + scopeName + " not found");
     }
     final InspectionToolWrapper toolWrapper = scopeToolState.getTool();
     final NamedScope scope = scopeToolState.getScope(project);
     if (scope != null) {
       myTools.add(
           index, new ScopeToolState(scope, toolWrapper, scopeToolState.isEnabled(), level));
     } else {
       myTools.add(
           index,
           new ScopeToolState(
               scopeToolState.getScopeName(), toolWrapper, scopeToolState.isEnabled(), level));
     }
   }
 }
 @NotNull
 public HighlightDisplayLevel getLevel(final NamedScope scope, Project project) {
   if (myTools != null && scope != null) {
     for (ScopeToolState state : myTools) {
       if (Comparing.equal(state.getScope(project), scope)) {
         return state.getLevel();
       }
     }
   }
   return myDefaultState.getLevel();
 }
 public void setScope(int idx, NamedScope namedScope) {
   if (myTools != null && myTools.size() > idx && idx >= 0) {
     final ScopeToolState scopeToolState = myTools.get(idx);
     InspectionToolWrapper toolWrapper = scopeToolState.getTool();
     myTools.remove(idx);
     myTools.add(
         idx,
         new ScopeToolState(
             namedScope, toolWrapper, scopeToolState.isEnabled(), scopeToolState.getLevel()));
   }
 }
 public void removeScope(@NotNull final String scopeName) {
   if (myTools != null) {
     for (ScopeToolState tool : myTools) {
       if (scopeName.equals(tool.getScopeName())) {
         myTools.remove(tool);
         break;
       }
     }
     checkToolsIsEmpty();
   }
 }
 public HighlightDisplayLevel getLevel(PsiElement element) {
   if (myTools == null || element == null) return myDefaultState.getLevel();
   final Project project = element.getProject();
   final DependencyValidationManager manager = DependencyValidationManager.getInstance(project);
   for (ScopeToolState state : myTools) {
     final NamedScope scope = state.getScope(project);
     final PackageSet set = scope != null ? scope.getValue() : null;
     if (set != null && set.contains(element.getContainingFile(), manager)) {
       return state.getLevel();
     }
   }
   return myDefaultState.getLevel();
 }
 @Nullable
 @Override
 public InspectionToolWrapper getEnabledTool(
     @Nullable PsiElement element, boolean includeDoNotShow) {
   if (!myEnabled) return null;
   if (myTools != null && element != null) {
     final Project project = element.getProject();
     final DependencyValidationManager manager = DependencyValidationManager.getInstance(project);
     for (ScopeToolState state : myTools) {
       final NamedScope scope = state.getScope(project);
       if (scope != null) {
         final PackageSet set = scope.getValue();
         if (set != null && set.contains(element.getContainingFile(), manager)) {
           return state.isEnabled()
                   && (includeDoNotShow
                       || !HighlightDisplayLevel.DO_NOT_SHOW.equals(state.getLevel()))
               ? state.getTool()
               : null;
         }
       }
     }
   }
   return myDefaultState.isEnabled()
           && (includeDoNotShow
               || !HighlightDisplayLevel.DO_NOT_SHOW.equals(myDefaultState.getLevel()))
       ? myDefaultState.getTool()
       : null;
 }
 @Override
 public boolean equals(Object o) {
   ToolsImpl tools = (ToolsImpl) o;
   if (myEnabled != tools.myEnabled) return false;
   if (getTools().size() != tools.getTools().size()) return false;
   for (int i = 0; i < getTools().size(); i++) {
     final ScopeToolState state = getTools().get(i);
     final ScopeToolState toolState = tools.getTools().get(i);
     if (!state.equalTo(toolState)) {
       return false;
     }
   }
   return true;
 }
 @Override
 public boolean isEnabled(PsiElement element) {
   if (!myEnabled) return false;
   if (myTools == null || element == null) return myDefaultState.isEnabled();
   final Project project = element.getProject();
   final DependencyValidationManager manager = DependencyValidationManager.getInstance(project);
   for (ScopeToolState state : myTools) {
     final NamedScope scope = state.getScope(project);
     if (scope != null) {
       final PackageSet set = scope.getValue();
       if (set != null && set.contains(element.getContainingFile(), manager)) {
         return state.isEnabled();
       }
     }
   }
   return myDefaultState.isEnabled();
 }
 private void reduceConvertedScopes() {
   for (ToolsImpl tools : myTools.values()) {
     final ScopeToolState toolState = tools.getDefaultState();
     final List<ScopeToolState> nonDefaultTools = tools.getNonDefaultTools();
     if (nonDefaultTools != null) {
       boolean equal = true;
       boolean isEnabled = toolState.isEnabled();
       for (ScopeToolState state : nonDefaultTools) {
         isEnabled |= state.isEnabled();
         if (!state.equalTo(toolState)) {
           equal = false;
         }
       }
       tools.setEnabled(isEnabled);
       if (equal) {
         tools.removeAllScopes();
       }
     }
   }
 }
 @NotNull
 private ScopeToolState insertTool(@NotNull final ScopeToolState scopeToolState, final int idx) {
   if (myTools == null) {
     myTools = new ArrayList<ScopeToolState>();
     if (scopeToolState.isEnabled()) {
       setEnabled(true);
     }
   }
   myTools.add(idx, scopeToolState);
   return scopeToolState;
 }
 public void disableTool(@NotNull PsiElement element) {
   final Project project = element.getProject();
   final DependencyValidationManager validationManager =
       DependencyValidationManager.getInstance(project);
   if (myTools != null) {
     for (ScopeToolState state : myTools) {
       final NamedScope scope = state.getScope(project);
       if (scope != null) {
         final PackageSet packageSet = scope.getValue();
         if (packageSet != null) {
           final PsiFile file = element.getContainingFile();
           if (file != null) {
             if (packageSet.contains(file, validationManager)) {
               state.setEnabled(false);
               return;
             }
           } else {
             if (packageSet instanceof PackageSetBase
                 && ((PackageSetBase) packageSet)
                     .contains(PsiUtilCore.getVirtualFile(element), project, validationManager)) {
               state.setEnabled(false);
               return;
             }
           }
         }
       }
     }
     myDefaultState.setEnabled(false);
   } else {
     myDefaultState.setEnabled(false);
     setEnabled(false);
   }
 }
 private boolean buildTree() {
   InspectionProfile profile = myInspectionProfile;
   boolean isGroupedBySeverity = myGlobalInspectionContext.getUIOptions().GROUP_BY_SEVERITY;
   myGroups.clear();
   final Map<String, Tools> tools = myGlobalInspectionContext.getTools();
   boolean resultsFound = false;
   for (Tools currentTools : tools.values()) {
     InspectionToolWrapper defaultToolWrapper = currentTools.getDefaultState().getTool();
     final HighlightDisplayKey key = HighlightDisplayKey.find(defaultToolWrapper.getShortName());
     for (ScopeToolState state : myProvider.getTools(currentTools)) {
       InspectionToolWrapper toolWrapper = state.getTool();
       if (myProvider.checkReportedProblems(myGlobalInspectionContext, toolWrapper)) {
         addTool(
             toolWrapper,
             ((InspectionProfileImpl) profile)
                 .getErrorLevel(key, state.getScope(myProject), myProject),
             isGroupedBySeverity);
         resultsFound = true;
       }
     }
   }
   return resultsFound;
 }
 public void writeExternal(@NotNull Element inspectionElement) throws WriteExternalException {
   if (myTools != null) {
     for (ScopeToolState state : myTools) {
       final Element scopeElement = new Element("scope");
       scopeElement.setAttribute("name", state.getScopeName());
       scopeElement.setAttribute(LEVEL_ATTRIBUTE, state.getLevel().getName());
       scopeElement.setAttribute(ENABLED_ATTRIBUTE, Boolean.toString(state.isEnabled()));
       InspectionToolWrapper toolWrapper = state.getTool();
       if (toolWrapper.isInitialized()) {
         toolWrapper.getTool().writeSettings(scopeElement);
       }
       inspectionElement.addContent(scopeElement);
     }
   }
   inspectionElement.setAttribute(ENABLED_ATTRIBUTE, Boolean.toString(isEnabled()));
   inspectionElement.setAttribute(LEVEL_ATTRIBUTE, getLevel().getName());
   inspectionElement.setAttribute(
       ENABLED_BY_DEFAULT_ATTRIBUTE, Boolean.toString(myDefaultState.isEnabled()));
   InspectionToolWrapper toolWrapper = myDefaultState.getTool();
   if (toolWrapper.isInitialized()) {
     toolWrapper.getTool().writeSettings(inspectionElement);
   }
 }
 public void setLevel(@NotNull HighlightDisplayLevel level) {
   myDefaultState.setLevel(level);
 }
 @NotNull
 @Override
 public InspectionToolWrapper getTool() {
   if (myTools == null) return myDefaultState.getTool();
   return myTools.iterator().next().getTool();
 }
 private void checkToolsIsEmpty() {
   if (myTools.isEmpty()) {
     myTools = null;
     setEnabled(myDefaultState.isEnabled());
   }
 }
 private void copyToolsConfigurations(InspectionProfileImpl profile) {
   try {
     for (ToolsImpl toolList : profile.myTools.values()) {
       final ToolsImpl tools = myTools.get(toolList.getShortName());
       final ScopeToolState defaultState = toolList.getDefaultState();
       tools.setDefaultState(
           copyToolSettings((InspectionTool) defaultState.getTool()),
           defaultState.isEnabled(),
           defaultState.getLevel());
       tools.removeAllScopes();
       tools.setEnabled(toolList.isEnabled());
       final List<ScopeToolState> nonDefaultToolStates = toolList.getNonDefaultTools();
       if (nonDefaultToolStates != null) {
         for (ScopeToolState state : nonDefaultToolStates) {
           final InspectionTool inspectionTool =
               copyToolSettings((InspectionTool) state.getTool());
           if (state.getScope() != null) {
             tools.addTool(state.getScope(), inspectionTool, state.isEnabled(), state.getLevel());
           } else {
             tools.addTool(
                 state.getScopeName(), inspectionTool, state.isEnabled(), state.getLevel());
           }
         }
       }
     }
   } catch (WriteExternalException e) {
     LOG.error(e);
   } catch (InvalidDataException e) {
     LOG.error(e);
   }
 }
 public HighlightDisplayLevel getLevel() {
   return myDefaultState.getLevel();
 }
  void readExternal(
      @NotNull Element toolElement,
      @NotNull InspectionProfile profile,
      Map<String, List<String>> dependencies)
      throws InvalidDataException {
    final String levelName = toolElement.getAttributeValue(LEVEL_ATTRIBUTE);
    final ProfileManager profileManager = profile.getProfileManager();
    final SeverityRegistrar registrar =
        ((SeverityProvider) profileManager).getOwnSeverityRegistrar();
    HighlightDisplayLevel level =
        levelName != null ? HighlightDisplayLevel.find(registrar.getSeverity(levelName)) : null;
    if (level == null) {
      level = HighlightDisplayLevel.WARNING;
    }
    myDefaultState.setLevel(level);
    final String enabled = toolElement.getAttributeValue(ENABLED_ATTRIBUTE);
    final boolean isEnabled = enabled != null && Boolean.parseBoolean(enabled);

    final String enabledTool = toolElement.getAttributeValue(ENABLED_BY_DEFAULT_ATTRIBUTE);
    myDefaultState.setEnabled(enabledTool != null ? Boolean.parseBoolean(enabledTool) : isEnabled);
    final InspectionToolWrapper toolWrapper = myDefaultState.getTool();

    final List scopeElements = toolElement.getChildren(ProfileEx.SCOPE);
    final List<String> scopeNames = new ArrayList<String>();
    for (Object sO : scopeElements) {
      final Element scopeElement = (Element) sO;
      final String scopeName = scopeElement.getAttributeValue(ProfileEx.NAME);
      if (scopeName == null) {
        continue;
      }
      final NamedScopesHolder scopesHolder = profileManager.getScopesManager();
      NamedScope namedScope = null;
      if (scopesHolder != null) {
        namedScope = scopesHolder.getScope(scopeName);
      }
      final String errorLevel = scopeElement.getAttributeValue(LEVEL_ATTRIBUTE);
      final String enabledInScope = scopeElement.getAttributeValue(ENABLED_ATTRIBUTE);
      final InspectionToolWrapper copyToolWrapper = toolWrapper.createCopy();
      // check if unknown children exists
      if (scopeElement.getAttributes().size() > 3 || !scopeElement.getChildren().isEmpty()) {
        copyToolWrapper.getTool().readSettings(scopeElement);
      }
      HighlightDisplayLevel scopeLevel =
          errorLevel != null ? HighlightDisplayLevel.find(registrar.getSeverity(errorLevel)) : null;
      if (scopeLevel == null) {
        scopeLevel = level;
      }
      if (namedScope != null) {
        addTool(
            namedScope,
            copyToolWrapper,
            enabledInScope != null && Boolean.parseBoolean(enabledInScope),
            scopeLevel);
      } else {
        addTool(
            scopeName,
            copyToolWrapper,
            enabledInScope != null && Boolean.parseBoolean(enabledInScope),
            scopeLevel);
      }

      scopeNames.add(scopeName);
    }

    for (int i = 0; i < scopeNames.size(); i++) {
      String scopeName = scopeNames.get(i);
      List<String> order = dependencies.get(scopeName);
      if (order == null) {
        order = new ArrayList<String>();
        dependencies.put(scopeName, order);
      }
      for (int j = i + 1; j < scopeNames.size(); j++) {
        order.add(scopeNames.get(j));
      }
    }

    // check if unknown children exists
    if (toolElement.getAttributes().size() > 4
        || toolElement.getChildren().size() > scopeElements.size()) {
      toolWrapper.getTool().readSettings(toolElement);
    }

    myEnabled = isEnabled;
  }