package com.godroup.plugins.movetogroup;

import java.io.File;
import java.util.ArrayList;
import static java.util.Arrays.asList;
import java.util.Collection;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import static java.util.stream.Collectors.toList;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.api.project.ui.ProjectGroup;
import org.openide.util.Exceptions;
import org.openide.util.NbPreferences;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;

public final class ProjectGroupUtils {

    /**
     * 获取当前项目组
     *
     * @return
     */
    public static String getActiveProjectGroup() {
        ProjectGroup activeProjectGroup = OpenProjects.getDefault().getActiveProjectGroup();
        if (activeProjectGroup == null) {
            return null;
        }

        return activeProjectGroup.getName();
    }

    /**
     * 获取当前待定的项目
     *
     * @return
     */
    public static List<ProjectInformation> getCurrentProjectList() {
        Collection<? extends Project> currentProjects = TopComponent.getRegistry().getActivated().getLookup().lookupAll(Project.class);
        if (currentProjects == null) {
            return null;
        }

        return currentProjects.stream().map(t -> ProjectUtils.getInformation(t)).collect(toList());
    }

    /**
     * 将当前项目移至指定的项目组
     *
     * @param groupId
     * @param projectInfo
     * @return
     */
    public static boolean moveToGroup(String groupId, ProjectInformation projectInfo) {
        String preferPath = "org/netbeans/modules/projectui/groups/" + groupId;

        Preferences groupNode = getPreferences(preferPath);
        if (null != groupNode) {
            // Append new project path
            String oldPath = groupNode.get("path", null);
            String newPath = Utilities.toURI(new File(projectInfo.getProject().getProjectDirectory().getPath())).toString();
            if (oldPath != null) {
                newPath = oldPath + " " + newPath;
            }

            groupNode.put("path", newPath.replace("null ", ""));

            // Close current project.
            OpenProjects.getDefault().close(new Project[]{projectInfo.getProject()});
            return true;
        }

        return false;
    }

    /**
     * 获取所有项目组
     *
     * @return
     */
    public static List<String[]> getProjectGroups() {
        Preferences groupNode = getPreferences("org/netbeans/modules/projectui/groups");
        if (groupNode != null) {
            try {
                List<String> childrenNames = asList(groupNode.childrenNames());
                return childrenNames.stream().map(t -> {
                    String groupId = t;
                    String groupName = groupId;

                    Preferences childGroupNode = getPreferences("org/netbeans/modules/projectui/groups/" + groupId);
                    if (childGroupNode != null) {
                        groupName = childGroupNode.get("name", null);
                    }

                    return new String[] {groupId, groupName};
                }).collect(toList());
            } catch (BackingStoreException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

        return new ArrayList<>(0);
    }

    /**
     * Get the preference for the given node path.
     *
     * @param path configuration path like "org/netbeans/modules/projectui"
     * @return {@link Preferences} or null
     */
    private static Preferences getPreferences(String path) {
        try {
            if (NbPreferences.root().nodeExists(path)) {
                return NbPreferences.root().node(path);
            }
        } catch (BackingStoreException ex) {
            Exceptions.printStackTrace(ex);
        }

        return null;
    }
}

@@ -755,15 +755,38 @@
   * 
-%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E9%A1%B9%E7%9B%AE%E7%BB%84
+Get active project group name.
 %0A   
@@ -1105,17 +1105,46 @@
   * 
-%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E5%BE%85%E5%AE%9A%E7%9A%84%E9%A1%B9%E7%9B%AE
+Get selected project information list.
 %0A   
@@ -1576,21 +1576,65 @@
   * 
-%E5%B0%86%E5%BD%93%E5%89%8D%E9%A1%B9%E7%9B%AE%E7%A7%BB%E8%87%B3%E6%8C%87%E5%AE%9A%E7%9A%84%E9%A1%B9%E7%9B%AE%E7%BB%84
+Add project path to group, and close selected project(s).
 %0A   
@@ -2567,37 +2567,92 @@
   * 
-%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E9%A1%B9%E7%9B%AE%E7%BB%84%0A     *%0A     * @return
+Get all project groups.%0A     *%0A     * @return String%5B0%5D=groupId, String%5B1%5D=groupName
 %0A   

change comments.