FindInProjectTask(
      @NotNull final FindModel findModel,
      @NotNull final Project project,
      @Nullable final PsiDirectory psiDirectory) {
    myFindModel = findModel;
    myProject = project;
    myPsiDirectory = psiDirectory;
    myPsiManager = PsiManager.getInstance(project);

    final String moduleName = findModel.getModuleName();
    myModule =
        moduleName == null
            ? null
            : ApplicationManager.getApplication()
                .runReadAction(
                    new Computable<Module>() {
                      @Override
                      public Module compute() {
                        return ModuleManager.getInstance(project).findModuleByName(moduleName);
                      }
                    });
    myFileIndex =
        myModule == null
            ? ProjectRootManager.getInstance(project).getFileIndex()
            : ModuleRootManager.getInstance(myModule).getFileIndex();

    final String filter = findModel.getFileFilter();
    final Pattern pattern = FindInProjectUtil.createFileMaskRegExp(filter);

    //noinspection unchecked
    myFileMask =
        pattern == null
            ? Condition.TRUE
            : new Condition<VirtualFile>() {
              @Override
              public boolean value(VirtualFile file) {
                return file != null && pattern.matcher(file.getName()).matches();
              }
            };

    final ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
    myProgress = progress != null ? progress : new EmptyProgressIndicator();
  }