Example #1
0
  public boolean isEnabled(AnActionEvent e) {
    PsiElement psiElement = e.getData(LangDataKeys.PSI_ELEMENT);
    if (psiElement == null || !(psiElement instanceof PsiDirectory)) {
      // Can be used only on package
      return false;
    }
    VirtualFile targetDir = ((PsiDirectory) psiElement).getVirtualFile();

    boolean isUnderSourceRoot = false;
    if (psiElement instanceof MPSPsiModel) {
      isUnderSourceRoot = true;
    } else {
      Module m = e.getData(LangDataKeys.MODULE);
      VirtualFile[] sourceRoots = ModuleRootManager.getInstance(m).getSourceRoots(true);
      for (VirtualFile root : sourceRoots) {
        if (targetDir.getPath().equals(root.getPath())) {
          // Can't be source or test folder
          return false;
        }
        isUnderSourceRoot =
            isUnderSourceRoot || FileUtil.isSubPath(root.getPath(), targetDir.getPath());
      }
    }

    return isUnderSourceRoot
        && myOperationContext != null
        && (myModelDescriptor != null || myNewModel)
        && myProject != null;
  }