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);
   }
 }
 @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;
 }
 @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();
 }
 public void actionPerformed(AnActionEvent e) {
   final PackageDependenciesNode leftNode = myLeftTree.getSelectedNode();
   final PackageDependenciesNode rightNode = myRightTree.getSelectedNode();
   if (leftNode != null && rightNode != null) {
     boolean hasDirectDependencies = myTransitiveBorder == 0;
     if (myTransitiveBorder > 0) {
       final Set<PsiFile> searchIn = getSelectedScope(myLeftTree);
       final Set<PsiFile> searchFor = getSelectedScope(myRightTree);
       for (DependenciesBuilder builder : myBuilders) {
         if (hasDirectDependencies) break;
         for (PsiFile from : searchIn) {
           if (hasDirectDependencies) break;
           for (PsiFile to : searchFor) {
             if (hasDirectDependencies) break;
             final List<List<PsiFile>> paths = builder.findPaths(from, to);
             for (List<PsiFile> path : paths) {
               if (path.isEmpty()) {
                 hasDirectDependencies = true;
                 break;
               }
             }
           }
         }
       }
     }
     final PatternDialectProvider provider =
         PatternDialectProvider.getInstance(mySettings.SCOPE_TYPE);
     PackageSet leftPackageSet = provider.createPackageSet(leftNode, true);
     if (leftPackageSet == null) {
       leftPackageSet = provider.createPackageSet(leftNode, false);
     }
     LOG.assertTrue(leftPackageSet != null);
     PackageSet rightPackageSet = provider.createPackageSet(rightNode, true);
     if (rightPackageSet == null) {
       rightPackageSet = provider.createPackageSet(rightNode, false);
     }
     LOG.assertTrue(rightPackageSet != null);
     if (hasDirectDependencies) {
       DependencyValidationManager.getInstance(myProject)
           .addRule(
               new DependencyRule(
                   new NamedScope.UnnamedScope(leftPackageSet),
                   new NamedScope.UnnamedScope(rightPackageSet),
                   true));
       rebuild();
     } else {
       Messages.showErrorDialog(
           DependenciesPanel.this,
           "Rule was not added.\n There is no direct dependency between \'"
               + leftPackageSet.getText()
               + "\' and \'"
               + rightPackageSet.getText()
               + "\'",
           AnalysisScopeBundle.message("mark.dependency.illegal.text"));
     }
   }
 }
  private static void initScopesDescriptors(
      @NotNull List<EditorSchemeAttributeDescriptor> descriptions, @NotNull MyColorScheme scheme) {
    Set<Pair<NamedScope, NamedScopesHolder>> namedScopes =
        new THashSet<Pair<NamedScope, NamedScopesHolder>>(
            new TObjectHashingStrategy<Pair<NamedScope, NamedScopesHolder>>() {
              @Override
              public int computeHashCode(
                  @NotNull final Pair<NamedScope, NamedScopesHolder> object) {
                return object.getFirst().getName().hashCode();
              }

              @Override
              public boolean equals(
                  @NotNull final Pair<NamedScope, NamedScopesHolder> o1,
                  @NotNull final Pair<NamedScope, NamedScopesHolder> o2) {
                return o1.getFirst().getName().equals(o2.getFirst().getName());
              }
            });
    Project[] projects = ProjectManager.getInstance().getOpenProjects();
    for (Project project : projects) {
      DependencyValidationManagerImpl validationManager =
          (DependencyValidationManagerImpl) DependencyValidationManager.getInstance(project);
      List<Pair<NamedScope, NamedScopesHolder>> cachedScopes =
          validationManager.getScopeBasedHighlightingCachedScopes();
      namedScopes.addAll(cachedScopes);
    }

    List<Pair<NamedScope, NamedScopesHolder>> list =
        new ArrayList<Pair<NamedScope, NamedScopesHolder>>(namedScopes);

    Collections.sort(
        list,
        new Comparator<Pair<NamedScope, NamedScopesHolder>>() {
          @Override
          public int compare(
              @NotNull final Pair<NamedScope, NamedScopesHolder> o1,
              @NotNull final Pair<NamedScope, NamedScopesHolder> o2) {
            return o1.getFirst().getName().compareToIgnoreCase(o2.getFirst().getName());
          }
        });
    for (Pair<NamedScope, NamedScopesHolder> pair : list) {
      NamedScope namedScope = pair.getFirst();
      String name = namedScope.getName();
      TextAttributesKey textAttributesKey = ScopeAttributesUtil.getScopeTextAttributeKey(name);
      if (scheme.getAttributes(textAttributesKey) == null) {
        scheme.setAttributes(textAttributesKey, new TextAttributes());
      }
      NamedScopesHolder holder = pair.getSecond();

      PackageSet value = namedScope.getValue();
      String toolTip = holder.getDisplayName() + (value == null ? "" : ": " + value.getText());
      addSchemedDescription(
          descriptions, name, SCOPES_GROUP, textAttributesKey, scheme, holder.getIcon(), toolTip);
    }
  }
 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();
 }
 private void includeSelected(boolean recurse) {
   final ArrayList<PackageSet> selected = getSelectedSets(recurse);
   if (selected == null || selected.isEmpty()) return;
   for (PackageSet set : selected) {
     if (myCurrentScope == null) {
       myCurrentScope = set;
     } else if (myCurrentScope instanceof InvalidPackageSet) {
       myCurrentScope =
           StringUtil.isEmpty(myCurrentScope.getText())
               ? set
               : new UnionPackageSet(myCurrentScope, set);
     } else {
       final boolean[] append = {true};
       final PackageSet simplifiedScope =
           processComplementaryScope(myCurrentScope, set, true, append);
       if (!append[0]) {
         myCurrentScope = simplifiedScope;
       } else {
         myCurrentScope =
             simplifiedScope != null ? new UnionPackageSet(simplifiedScope, set) : set;
       }
     }
   }
   rebuild(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();
 }
  @Nullable
  static PackageSet processComplementaryScope(
      @NotNull PackageSet current, PackageSet added, boolean checkComplementSet, boolean[] append) {
    final String text = added.getText();
    if (current instanceof ComplementPackageSet
        && Comparing.strEqual(
            ((ComplementPackageSet) current).getComplementarySet().getText(), text)) {
      if (checkComplementSet) {
        append[0] = false;
      }
      return null;
    }
    if (Comparing.strEqual(current.getText(), text)) {
      if (!checkComplementSet) {
        append[0] = false;
      }
      return null;
    }

    if (current instanceof UnionPackageSet) {
      final PackageSet left =
          processComplementaryScope(
              ((UnionPackageSet) current).getFirstSet(), added, checkComplementSet, append);
      final PackageSet right =
          processComplementaryScope(
              ((UnionPackageSet) current).getSecondSet(), added, checkComplementSet, append);
      if (left == null) return right;
      if (right == null) return left;
      return new UnionPackageSet(left, right);
    }

    if (current instanceof IntersectionPackageSet) {
      final PackageSet left =
          processComplementaryScope(
              ((IntersectionPackageSet) current).getFirstSet(), added, checkComplementSet, append);
      final PackageSet right =
          processComplementaryScope(
              ((IntersectionPackageSet) current).getSecondSet(), added, checkComplementSet, append);
      if (left == null) return right;
      if (right == null) return left;
      return new IntersectionPackageSet(left, right);
    }

    return current;
  }
 public void reset(PackageSet packageSet, @Nullable Runnable runnable) {
   myCurrentScope = packageSet;
   myPatternField.setText(myCurrentScope == null ? "" : myCurrentScope.getText());
   rebuild(false, runnable, false, 0);
 }