public static List<MavenProject> getMavenProjects(DataContext context) {
    Project project = CommonDataKeys.PROJECT.getData(context);
    if (project == null) return Collections.emptyList();

    VirtualFile[] virtualFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(context);
    if (virtualFiles == null || virtualFiles.length == 0) return Collections.emptyList();

    MavenProjectsManager projectsManager = MavenProjectsManager.getInstanceIfCreated(project);
    if (projectsManager == null || !projectsManager.isMavenizedProject()) return Collections.emptyList();

    Set<MavenProject> res = new LinkedHashSet<>();

    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (VirtualFile file : virtualFiles) {
      MavenProject mavenProject;

      if (file.isDirectory()) {
        VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
        if (!file.equals(contentRoot)) return Collections.emptyList();

        Module module = fileIndex.getModuleForFile(file);
        if (module == null || !projectsManager.isMavenizedModule(module)) return Collections.emptyList();

        mavenProject = projectsManager.findProject(module);
      }
      else {
        mavenProject = projectsManager.findProject(file);
      }

      if (mavenProject == null) return Collections.emptyList();

      res.add(mavenProject);
    }

    return new ArrayList<>(res);
  }
public static List<MavenProject> getMavenProjects(DataContext context)