@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);
  }
Example #2
0
  @Nullable
  private PsiDirectory chooseDirectory(final Project project, final PsiFile file) {
    PsiDirectory preferredDirectory =
        myWritableDirectoryList.isEmpty() ? null : myWritableDirectoryList.get(0);
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final VirtualFile virtualFile = file.getVirtualFile();
    assert virtualFile != null;
    final Module moduleForFile = fileIndex.getModuleForFile(virtualFile);
    if (myWritableDirectoryList.size() > 1
        && !ApplicationManager.getApplication().isUnitTestMode()) {
      if (moduleForFile != null) {
        for (PsiDirectory directory : myWritableDirectoryList) {
          if (fileIndex.getModuleForFile(directory.getVirtualFile()) == moduleForFile) {
            preferredDirectory = directory;
            break;
          }
        }
      }

      return DirectoryChooserUtil.chooseDirectory(
          myWritableDirectoryList.toArray(new PsiDirectory[myWritableDirectoryList.size()]),
          preferredDirectory,
          project,
          new HashMap<PsiDirectory, String>());
    }
    return preferredDirectory;
  }
  @Override
  public void actionPerformed(final AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    final Project project = e.getData(CommonDataKeys.PROJECT);

    if (view == null || project == null) {
      return;
    }
    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);

    if (directory == null) return;
    final CCProjectService service = CCProjectService.getInstance(project);
    final Course course = service.getCourse();
    final Lesson lesson = course.getLesson(directory.getName());
    final int size = lesson.getTaskList().size();

    final String taskName =
        Messages.showInputDialog("Name:", "Task Name", null, "task" + (size + 1), null);
    if (taskName == null) return;

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                final PsiDirectory taskDirectory =
                    DirectoryUtil.createSubdirectories("task" + (size + 1), directory, "\\/");
                if (taskDirectory != null) {
                  final FileTemplate template =
                      FileTemplateManager.getInstance().getInternalTemplate("task.html");
                  final FileTemplate testsTemplate =
                      FileTemplateManager.getInstance().getInternalTemplate("tests");
                  final FileTemplate taskTemplate =
                      FileTemplateManager.getInstance().getInternalTemplate("task.answer");
                  try {
                    final PsiElement taskFile =
                        FileTemplateUtil.createFromTemplate(
                            template, "task.html", null, taskDirectory);
                    final PsiElement testsFile =
                        FileTemplateUtil.createFromTemplate(
                            testsTemplate, "tests.py", null, taskDirectory);
                    final PsiElement taskPyFile =
                        FileTemplateUtil.createFromTemplate(
                            taskTemplate, "file1", null, taskDirectory);

                    final Task task = new Task(taskName);
                    task.addTaskFile("file1.py", size + 1);
                    task.setIndex(size + 1);
                    lesson.addTask(task, taskDirectory);
                    ApplicationManager.getApplication()
                        .invokeLater(
                            new Runnable() {
                              @Override
                              public void run() {
                                EditorHelper.openInEditor(testsFile, false);
                                EditorHelper.openInEditor(taskPyFile, false);
                                view.selectElement(taskFile);
                              }
                            });
                  } catch (Exception ignored) {
                  }
                }
              }
            });
  }
  /** @deprecated */
  @Nullable
  public static PsiDirectory findOrCreateDirectoryForPackage(
      Project project,
      String packageName,
      PsiDirectory baseDir,
      boolean askUserToCreate,
      boolean filterSourceDirsForTestBaseDir)
      throws IncorrectOperationException {

    PsiDirectory psiDirectory = null;

    if (!"".equals(packageName)) {
      PsiPackage rootPackage = findLongestExistingPackage(project, packageName);
      if (rootPackage != null) {
        int beginIndex = rootPackage.getQualifiedName().length() + 1;
        packageName = beginIndex < packageName.length() ? packageName.substring(beginIndex) : "";
        String postfixToShow = packageName.replace('.', File.separatorChar);
        if (packageName.length() > 0) {
          postfixToShow = File.separatorChar + postfixToShow;
        }
        PsiDirectory[] directories = rootPackage.getDirectories();
        if (filterSourceDirsForTestBaseDir) {
          directories = filterSourceDirectories(baseDir, project, directories);
        }
        psiDirectory =
            DirectoryChooserUtil.selectDirectory(project, directories, baseDir, postfixToShow);
        if (psiDirectory == null) return null;
      }
    }

    if (psiDirectory == null) {
      PsiDirectory[] sourceDirectories = ProjectRootUtil.getSourceRootDirectories(project);
      psiDirectory =
          DirectoryChooserUtil.selectDirectory(
              project,
              sourceDirectories,
              baseDir,
              File.separatorChar + packageName.replace('.', File.separatorChar));
      if (psiDirectory == null) return null;
    }

    String restOfName = packageName;
    boolean askedToCreate = false;
    while (restOfName.length() > 0) {
      final String name = getLeftPart(restOfName);
      PsiDirectory foundExistingDirectory = psiDirectory.findSubdirectory(name);
      if (foundExistingDirectory == null) {
        if (!askedToCreate && askUserToCreate) {
          int toCreate =
              Messages.showYesNoDialog(
                  project,
                  IdeBundle.message("prompt.create.non.existing.package", packageName),
                  IdeBundle.message("title.package.not.found"),
                  Messages.getQuestionIcon());
          if (toCreate != 0) {
            return null;
          }
          askedToCreate = true;
        }
        psiDirectory = createSubdirectory(psiDirectory, name, project);
      } else {
        psiDirectory = foundExistingDirectory;
      }
      restOfName = cutLeftPart(restOfName);
    }
    return psiDirectory;
  }
  @Nullable
  public static PsiDirectory findOrCreateDirectoryForPackage(
      @NotNull Module module,
      String packageName,
      PsiDirectory baseDir,
      boolean askUserToCreate,
      boolean filterSourceDirsForBaseTestDirectory)
      throws IncorrectOperationException {
    final Project project = module.getProject();
    PsiDirectory psiDirectory = null;
    if (!packageName.isEmpty()) {
      PsiPackage rootPackage = findLongestExistingPackage(module, packageName);
      if (rootPackage != null) {
        int beginIndex = rootPackage.getQualifiedName().length() + 1;
        packageName = beginIndex < packageName.length() ? packageName.substring(beginIndex) : "";
        String postfixToShow = packageName.replace('.', File.separatorChar);
        if (packageName.length() > 0) {
          postfixToShow = File.separatorChar + postfixToShow;
        }
        PsiDirectory[] moduleDirectories = getPackageDirectoriesInModule(rootPackage, module);
        if (filterSourceDirsForBaseTestDirectory) {
          moduleDirectories = filterSourceDirectories(baseDir, project, moduleDirectories);
        }
        psiDirectory =
            DirectoryChooserUtil.selectDirectory(
                project, moduleDirectories, baseDir, postfixToShow);
        if (psiDirectory == null) return null;
      }
    }

    if (psiDirectory == null) {
      if (!checkSourceRootsConfigured(module, askUserToCreate)) return null;
      final List<VirtualFile> sourceRoots =
          ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
      List<PsiDirectory> directoryList = new ArrayList<PsiDirectory>();
      for (VirtualFile sourceRoot : sourceRoots) {
        final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(sourceRoot);
        if (directory != null) {
          directoryList.add(directory);
        }
      }
      PsiDirectory[] sourceDirectories =
          directoryList.toArray(new PsiDirectory[directoryList.size()]);
      psiDirectory =
          DirectoryChooserUtil.selectDirectory(
              project,
              sourceDirectories,
              baseDir,
              File.separatorChar + packageName.replace('.', File.separatorChar));
      if (psiDirectory == null) return null;
    }

    String restOfName = packageName;
    boolean askedToCreate = false;
    while (restOfName.length() > 0) {
      final String name = getLeftPart(restOfName);
      PsiDirectory foundExistingDirectory = psiDirectory.findSubdirectory(name);
      if (foundExistingDirectory == null) {
        if (!askedToCreate && askUserToCreate) {
          if (!ApplicationManager.getApplication().isUnitTestMode()) {
            int toCreate =
                Messages.showYesNoDialog(
                    project,
                    IdeBundle.message("prompt.create.non.existing.package", packageName),
                    IdeBundle.message("title.package.not.found"),
                    Messages.getQuestionIcon());
            if (toCreate != 0) {
              return null;
            }
          }
          askedToCreate = true;
        }

        final PsiDirectory psiDirectory1 = psiDirectory;
        try {
          psiDirectory =
              ActionRunner.runInsideWriteAction(
                  new ActionRunner.InterruptibleRunnableWithResult<PsiDirectory>() {
                    public PsiDirectory run() throws Exception {
                      return psiDirectory1.createSubdirectory(name);
                    }
                  });
        } catch (IncorrectOperationException e) {
          throw e;
        } catch (IOException e) {
          throw new IncorrectOperationException(e.toString(), e);
        } catch (Exception e) {
          LOG.error(e);
        }
      } else {
        psiDirectory = foundExistingDirectory;
      }
      restOfName = cutLeftPart(restOfName);
    }
    return psiDirectory;
  }
 public PsiDirectory getOrChooseDirectory() {
   return DirectoryChooserUtil.getOrChooseDirectory(this);
 }