@Override
  public void update(@NotNull AnActionEvent event) {
    final Presentation presentation = event.getPresentation();
    final Project project = event.getData(CommonDataKeys.PROJECT);
    if (project == null) {
      presentation.setVisible(false);
      presentation.setEnabled(false);
      return;
    }

    final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
    if (view == null) {
      presentation.setVisible(false);
      presentation.setEnabled(false);
      return;
    }

    final PsiDirectory[] directories = view.getDirectories();
    if (directories.length == 0) {
      presentation.setVisible(false);
      presentation.setEnabled(false);
      return;
    }
    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
    final CCProjectService service = CCProjectService.getInstance(project);
    final Course course = service.getCourse();
    if (course != null && directory != null && course.getLesson(directory.getName()) == null) {
      presentation.setVisible(false);
      presentation.setEnabled(false);
      return;
    }

    presentation.setVisible(true);
    presentation.setEnabled(true);
  }
  static boolean canCreateFromTemplate(AnActionEvent e, FileTemplate template) {
    if (e == null) return false;
    DataContext dataContext = e.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) return false;

    PsiDirectory[] dirs = view.getDirectories();
    if (dirs.length == 0) return false;

    return FileTemplateUtil.canCreateFromTemplate(dirs, template);
  }
  public static boolean isInsideProjectSources(AnActionEvent event) {
    ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(event.getProject()).getFileIndex();
    final IdeView view = event.getData(DataKeys.IDE_VIEW);

    for (PsiDirectory dir : view.getDirectories()) {
      if (projectFileIndex.isInSourceContent(dir.getVirtualFile())
          && JavaDirectoryService.getInstance().getPackage(dir) != null) {
        return true;
      }
    }
    return false;
  }
  protected boolean isAvailable(final DataContext dataContext) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
      return false;
    }

    if (DumbService.getInstance(project).isDumb() && !isDumbAware()) {
      return false;
    }

    final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null || view.getDirectories().length == 0) {
      return false;
    }

    return true;
  }
  @Override
  public final void actionPerformed(final AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();

    final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
      return;
    }

    final Project project = CommonDataKeys.PROJECT.getData(dataContext);

    final PsiDirectory dir = view.getOrChooseDirectory();
    if (dir == null) return;
    final PsiElement[] createdElements = invokeDialog(project, dir);

    for (PsiElement createdElement : createdElements) {
      view.selectElement(createdElement);
    }
  }
 @Nullable
 public static PsiDirectory getOrChooseDirectory(@NotNull IdeView view) {
   PsiDirectory[] dirs = view.getDirectories();
   if (dirs.length == 0) return null;
   if (dirs.length == 1) {
     return dirs[0];
   } else {
     Project project = dirs[0].getProject();
     return selectDirectory(project, dirs, null, "");
   }
 }
 @Override
 public void update(@NotNull AnActionEvent event) {
   final Project project = event.getProject();
   final Presentation presentation = event.getPresentation();
   if (project == null) {
     return;
   }
   presentation.setEnabledAndVisible(false);
   if (!CCUtils.isCourseCreator(project)) {
     return;
   }
   final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
   if (view == null) {
     return;
   }
   final PsiDirectory[] directories = view.getDirectories();
   if (directories.length == 0) {
     return;
   }
   presentation.setEnabledAndVisible(true);
 }
  public final void actionPerformed(final AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();

    final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
      return;
    }

    final Project project = PlatformDataKeys.PROJECT.getData(dataContext);

    final PsiDirectory dir = view.getOrChooseDirectory();
    if (dir == null || project == null) return;

    final CreateFileFromTemplateDialog.Builder builder =
        CreateFileFromTemplateDialog.createDialog(project);
    buildDialog(project, dir, builder);

    final Ref<String> selectedTemplateName = Ref.create(null);
    final T createdElement =
        builder.show(
            getErrorTitle(),
            getDefaultTemplateName(dir),
            new CreateFileFromTemplateDialog.FileCreator<T>() {

              public T createFile(@NotNull String name, @NotNull String templateName) {
                selectedTemplateName.set(templateName);
                return CreateFromTemplateAction.this.createFile(name, templateName, dir);
              }

              @NotNull
              public String getActionName(@NotNull String name, @NotNull String templateName) {
                return CreateFromTemplateAction.this.getActionName(dir, name, templateName);
              }
            });
    if (createdElement != null) {
      view.selectElement(createdElement);
      postProcess(createdElement, selectedTemplateName.get(), builder.getCustomProperties());
    }
  }
 protected boolean isAvailable(DataContext dataContext) {
   final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
   final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
   return project != null && view != null && view.getDirectories().length != 0;
 }