public void init(
      final Project project,
      final boolean suggestSearchInLibs,
      final boolean prevSearchWholeFiles,
      final String preselect) {
    mySuggestSearchInLibs = suggestSearchInLibs;
    myPrevSearchFiles = prevSearchWholeFiles;
    myProject = project;
    myScopeListener =
        new NamedScopesHolder.ScopeListener() {
          @Override
          public void scopesChanged() {
            final SearchScope selectedScope = getSelectedScope();
            rebuildModel();
            if (selectedScope != null) {
              selectScope(selectedScope.getDisplayName());
            }
          }
        };
    myNamedScopeManager = NamedScopeManager.getInstance(project);
    myNamedScopeManager.addScopeListener(myScopeListener);
    myValidationManager = DependencyValidationManager.getInstance(project);
    myValidationManager.addScopeListener(myScopeListener);
    addActionListener(createScopeChooserListener());

    final JComboBox combo = getComboBox();
    combo.setRenderer(new ScopeDescriptionWithDelimiterRenderer(combo.getRenderer()));

    rebuildModel();

    selectScope(preselect);
  }
 void reloadScopes() {
   ApplicationManager.getApplication().assertIsDispatchThread();
   List<Pair<NamedScope, NamedScopesHolder>> scopeList =
       new ArrayList<Pair<NamedScope, NamedScopesHolder>>();
   final DependencyValidationManager dependencyValidationManager =
       DependencyValidationManager.getInstance(myProject);
   addScopesToList(scopeList, NamedScopeManager.getInstance(myProject));
   addScopesToList(scopeList, dependencyValidationManager);
   myScopes.clear();
   myScopes.addAll(scopeList);
   dependencyValidationManager.reloadRules();
 }
 private static List<PsiClass> filterAllowedDependencies(PsiElement element, PsiClass[] classes) {
   DependencyValidationManager dependencyValidationManager =
       DependencyValidationManager.getInstance(element.getProject());
   PsiFile fromFile = element.getContainingFile();
   List<PsiClass> result = new ArrayList<PsiClass>();
   for (PsiClass psiClass : classes) {
     if (dependencyValidationManager.getViolatorDependencyRule(
             fromFile, psiClass.getContainingFile())
         == null) {
       result.add(psiClass);
     }
   }
   return result;
 }
 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();
 }
 @Override
 public void dispose() {
   super.dispose();
   if (myValidationManager != null) {
     myValidationManager.removeScopeListener(myScopeListener);
     myValidationManager = null;
   }
   if (myNamedScopeManager != null) {
     myNamedScopeManager.removeScopeListener(myScopeListener);
     myNamedScopeManager = null;
   }
   myScopeListener = null;
 }
 @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();
 }