Java Code Examples for com.intellij.ide.IdeView#getDirectories()

The following examples show how to use com.intellij.ide.IdeView#getDirectories() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtensionUtility.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static PsiDirectory getExtensionDirectory(@NotNull AnActionEvent event) {
    Project project = event.getData(PlatformDataKeys.PROJECT);
    if (project == null) {
        return null;
    }

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

    PsiDirectory[] directories = view.getDirectories();
    if (directories.length == 0) {
        return null;
    }

    return FilesystemUtil.findParentExtensionDirectory(directories[0]);
}
 
Example 2
Source File: GenerateAction.java    From RIBs with Apache License 2.0 6 votes vote down vote up
/**
 * Checked whether or not this action can be enabled.
 * <p>
 * <p>Requirements to be enabled: * User must be in a Java source folder.
 *
 * @param dataContext to figure out where the user is.
 * @return {@code true} when the action is available, {@code false} when the action is not
 * available.
 */
private boolean isAvailable(DataContext dataContext) {
  final Project project = CommonDataKeys.PROJECT.getData(dataContext);
  if (project == null) {
    return false;
  }

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

  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  for (PsiDirectory dir : view.getDirectories()) {
    if (projectFileIndex.isUnderSourceRootOfType(
        dir.getVirtualFile(), JavaModuleSourceRootTypes.SOURCES)
        && checkPackageExists(dir)) {
      return true;
    }
  }

  return false;
}
 
Example 3
Source File: GraphQLCreateConfigFileAction.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@Override
public void update(AnActionEvent e) {

    boolean isAvailable = false;

    if (e.getProject() != null) {
        final DataContext dataContext = e.getDataContext();
        final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
        if (view != null && view.getDirectories().length > 0) {
            final Module module = LangDataKeys.MODULE.getData(dataContext);
            if (module != null) {
                final VirtualFile actionDirectory = getActionDirectory(e);
                if (actionDirectory != null) {
                    isAvailable = (actionDirectory.findChild(GraphQLConfigManager.GRAPHQLCONFIG) == null);
                }
            }
        }
    }

    final Presentation presentation = e.getPresentation();
    presentation.setVisible(isAvailable);
    presentation.setEnabled(isAvailable);
}
 
Example 4
Source File: BundleClassGeneratorUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Nullable
public static PsiDirectory getBundleDirContext(@NotNull AnActionEvent event) {

    Project project = event.getData(PlatformDataKeys.PROJECT);
    if (project == null) {
        return null;
    }

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

    PsiDirectory[] directories = view.getDirectories();
    if(directories.length == 0) {
        return null;
    }

    return FilesystemUtil.findParentBundleFolder(directories[0]);
}
 
Example 5
Source File: CreateInDirectoryActionBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected boolean isAvailable(final DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return false;
  }

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

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

  return true;
}
 
Example 6
Source File: ActionUtil.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
/**
 * Finds the directories on which an action was performed on.
 *
 * @param actionEvent The source action event
 * @return an array of directories the action was performed on
 */
public static PsiDirectory[] findDirectoryFromActionEvent(AnActionEvent actionEvent) {

    DataContext dataContext = actionEvent.getDataContext();
    IdeView data = LangDataKeys.IDE_VIEW.getData(dataContext);

    if (data == null) {
        return new PsiDirectory[]{};
    }

    return data.getDirectories();
}
 
Example 7
Source File: NewBlazePackageAction.java    From intellij with Apache License 2.0 5 votes vote down vote up
private boolean isEnabled(AnActionEvent event) {
  Project project = event.getProject();
  IdeView view = event.getData(LangDataKeys.IDE_VIEW);
  if (project == null || view == null) {
    return false;
  }
  return view.getDirectories().length != 0;
}
 
Example 8
Source File: PolygeneCreateActionGroup.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private boolean shouldActionGroupVisible( AnActionEvent e )
    {
        Module module = e.getData( LangDataKeys.MODULE );
        if( module == null )
        {
            return false;
        }

        // TODO: Enable this once PolygeneFacet can be automatically added/removed
//        if( PolygeneFacet.getInstance( module ) == null )
//        {
//            return false;
//        }

        // Are we on IDE View and under project source folder?
        Project project = e.getData( PlatformDataKeys.PROJECT );
        IdeView view = e.getData( LangDataKeys.IDE_VIEW );
        if( view != null && project != null )
        {
            ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance( project ).getFileIndex();
            PsiDirectory[] dirs = view.getDirectories();
            for( PsiDirectory dir : dirs )
            {
                if( projectFileIndex.isInSourceContent( dir.getVirtualFile() ) && JavaDirectoryService.getInstance().getPackage( dir ) != null )
                {
                    return true;
                }
            }
        }

        return false;
    }
 
Example 9
Source File: NewFileAction.java    From idea-gitignore with MIT License 5 votes vote down vote up
/**
 * Updates visibility of the action presentation in various actions list.
 *
 * @param e action event
 */
@Override
public void update(@NotNull AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);

    final PsiDirectory[] directory = view != null ? view.getDirectories() : null;
    if (directory == null || directory.length == 0 || project == null ||
            !this.fileType.getIgnoreLanguage().isNewAllowed()) {
        e.getPresentation().setVisible(false);
    }
}
 
Example 10
Source File: CreateFromTemplateGroup.java    From consulo with Apache License 2.0 5 votes vote down vote up
static boolean canCreateFromTemplate(AnActionEvent e, FileTemplate template) {
  if (e == null) return false;
  DataContext dataContext = e.getDataContext();
  IdeView view = dataContext.getData(LangDataKeys.IDE_VIEW);
  if (view == null) return false;

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

  return FileTemplateUtil.canCreateFromTemplate(dirs, template);
}
 
Example 11
Source File: DirectoryChooserUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiDirectory getOrChooseDirectory(@Nonnull 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, "");
  }
}
 
Example 12
Source File: ServiceActionUtil.java    From idea-php-shopware-plugin with MIT License 4 votes vote down vote up
public static void buildFile(AnActionEvent event, final Project project, String templatePath, String fileName) {
    DataContext dataContext = event.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null) {
        return;
    }

    PsiDirectory[] directories = view.getDirectories();
    if(directories.length == 0) {
        return;
    }

    final PsiDirectory initialBaseDir = directories[0];
    if (initialBaseDir == null) {
        return;
    }

    if(initialBaseDir.findFile(fileName) != null) {
        Messages.showInfoMessage("File exists", "Error");
        return;
    }

    String content;
    try {
        content = StreamUtil.readText(ServiceActionUtil.class.getResourceAsStream(templatePath), "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(project);

    final PsiFile file = factory.createFileFromText(fileName, XmlFileType.INSTANCE, content);

    ApplicationManager.getApplication().runWriteAction(() -> {
        CodeStyleManager.getInstance(project).reformat(file);
        initialBaseDir.add(file);
    });

    PsiFile psiFile = initialBaseDir.findFile(fileName);
    if(psiFile != null) {
        view.selectElement(psiFile);
    }

}
 
Example 13
Source File: ServiceActionUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public static void buildFile(AnActionEvent event, final Project project, String templatePath) {
    String extension = (templatePath.endsWith(".yml") || templatePath.endsWith(".yaml")) ? "yml" : "xml" ;

    String fileName = Messages.showInputDialog(project, "File name (without extension)", String.format("Create %s Service", extension), Symfony2Icons.SYMFONY);
    if(fileName == null || StringUtils.isBlank(fileName)) {
        return;
    }

    FileType fileType = (templatePath.endsWith(".yml") || templatePath.endsWith(".yaml")) ? YAMLFileType.YML : XmlFileType.INSTANCE ;

    if(!fileName.endsWith("." + extension)) {
        fileName = fileName.concat("." + extension);
    }

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

    PsiDirectory[] directories = view.getDirectories();
    if(directories.length == 0) {
        return;
    }

    final PsiDirectory initialBaseDir = directories[0];
    if (initialBaseDir == null) {
        return;
    }

    if(initialBaseDir.findFile(fileName) != null) {
        Messages.showInfoMessage("File exists", "Error");
        return;
    }

    String content;
    try {
        content = StreamUtil.readText(ServiceActionUtil.class.getResourceAsStream(templatePath), "UTF-8").replace("\r\n", "\n");
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(project);

    String bundleName = "Acme\\DemoBundle";

    SymfonyBundleUtil symfonyBundleUtil = new SymfonyBundleUtil(project);
    SymfonyBundle symfonyBundle = symfonyBundleUtil.getContainingBundle(initialBaseDir);

    if(symfonyBundle != null) {
        bundleName = StringUtils.strip(symfonyBundle.getNamespaceName(), "\\");
    }

    String underscoreBundle = bundleName.replace("\\", ".").toLowerCase();
    if(underscoreBundle.endsWith("bundle")) {
        underscoreBundle = underscoreBundle.substring(0, underscoreBundle.length() - 6);
    }

    content = content.replace("{{ BundleName }}", bundleName).replace("{{ BundleNameUnderscore }}", underscoreBundle);

    final PsiFile file = factory.createFileFromText(fileName, fileType, content);

    ApplicationManager.getApplication().runWriteAction(() -> {
        CodeStyleManager.getInstance(project).reformat(file);
        initialBaseDir.add(file);
    });

    PsiFile psiFile = initialBaseDir.findFile(fileName);
    if(psiFile != null) {
        view.selectElement(psiFile);
    }

}
 
Example 14
Source File: CreateTemplateInPackageAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected boolean isAvailable(final DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  final IdeView view = dataContext.getData(LangDataKeys.IDE_VIEW);
  if (project == null || view == null || view.getDirectories().length == 0) {
    return false;
  }

  final Module module = dataContext.getData(LangDataKeys.MODULE);
  if (module == null) {
    return false;
  }

  final Class moduleExtensionClass = getModuleExtensionClass();
  if (moduleExtensionClass != null && ModuleUtilCore.getExtension(module, moduleExtensionClass) == null) {
    return false;
  }

  if (!myInSourceOnly) {
    return true;
  }

  PsiPackageSupportProvider[] extensions = PsiPackageSupportProvider.EP_NAME.getExtensions();

  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  for (PsiDirectory dir : view.getDirectories()) {
    boolean accepted = false;
    for (PsiPackageSupportProvider provider : extensions) {
      if (provider.acceptVirtualFile(module, dir.getVirtualFile())) {
        accepted = true;
        break;
      }
    }

    if (accepted && projectFileIndex.isInSourceContent(dir.getVirtualFile()) && checkPackageExists(dir)) {
      return true;
    }
  }

  return false;
}
 
Example 15
Source File: CreateDirectoryOrPackageAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
@Override
public void update(@Nonnull AnActionEvent event) {
  Presentation presentation = event.getPresentation();

  Project project = event.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    presentation.setVisible(false);
    presentation.setEnabled(false);
    return;
  }

  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;
  }

  presentation.setVisible(true);
  presentation.setEnabled(true);

  // is more that one directories not show package support
  if (directories.length > 1) {
    presentation.setText(ChildType.Directory.getName());
    presentation.setIcon(AllIcons.Nodes.TreeClosed);
  }
  else {
    Trinity<ContentFolderTypeProvider, PsiDirectory, ChildType> info = getInfo(directories[0]);

    presentation.setText(info.getThird().getName());

    ContentFolderTypeProvider first = info.getFirst();
    Image childIcon;
    if (first == null) {
      childIcon = AllIcons.Nodes.TreeClosed;
    }
    else {
      childIcon = first.getChildPackageIcon() == null ? first.getChildDirectoryIcon() : first.getChildPackageIcon();
    }
    presentation.setIcon(childIcon);
  }
}
 
Example 16
Source File: CreateFromTemplateAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected boolean isAvailable(DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  final IdeView view = dataContext.getData(LangDataKeys.IDE_VIEW);
  return project != null && view != null && view.getDirectories().length != 0;
}