Jelajahi Sumber

Merge pull request #641 from tonihele/bugfix/issue-640

Fix adding custom controls and app states to scenes in Gradle projects
Toni Helenius 7 bulan lalu
induk
melakukan
09279df350

+ 48 - 27
jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java

@@ -32,8 +32,9 @@
 package com.jme3.gde.core.appstates;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.EnumSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 import javax.lang.model.element.TypeElement;
@@ -54,6 +55,7 @@ import org.netbeans.api.project.Project;
 import org.netbeans.api.project.ProjectUtils;
 import org.netbeans.api.project.SourceGroup;
 import org.netbeans.api.project.Sources;
+import org.openide.filesystems.FileObject;
 import org.openide.util.Exceptions;
 
 @SuppressWarnings({"unchecked", "rawtypes"})
@@ -83,37 +85,56 @@ public final class NewAppStateVisualPanel1 extends JPanel {
     }
 
     private List<String> getSources() {
-        Sources sources = ProjectUtils.getSources(proj);
-        final List<String> list = new LinkedList<>();
-        if (sources != null) {
+        Project root = ProjectUtils.rootOf(proj);
+        Set<Project> containedProjects = ProjectUtils.getContainedProjects(root, true);
+        List<Project> projects = new ArrayList<>();
+        projects.add(root);
+        if (containedProjects != null) {
+            projects.addAll(containedProjects);
+        }
+        if (projects.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        List<String> list = new ArrayList<>();
+        for (Project project : projects) {
+            Sources sources = ProjectUtils.getSources(project);
+            if (sources == null) {
+                continue;
+            }
+
             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
-            if (groups != null) {
-                for (SourceGroup sourceGroup : groups) {
-                    ClasspathInfo cpInfo = ClasspathInfo.create(
-                            ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.BOOT),
-                            ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.COMPILE),
-                            ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.SOURCE)
-                    );
-
-                    Set<SearchScope> set = EnumSet.of(ClassIndex.SearchScope.SOURCE);
-                    Set<ElementHandle<TypeElement>> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set);
-                    for (ElementHandle<TypeElement> elementHandle : types) {
-                        JavaSource js = JavaSource.create(cpInfo);
-                        try {
-                            js.runUserActionTask((CompilationController control) -> {
-                                control.toPhase(JavaSource.Phase.RESOLVED);
-                                TypeElement elem = elementHandle.resolve(control);
-                                if (elem != null && doesInheritFromAppState(elem, control.getTypes())) {
-                                    list.add(elem.getQualifiedName().toString());
-                                }
-                            }, false);
-                        } catch (IOException ioe) {
-                            Exceptions.printStackTrace(ioe);
-                        }
+            if (groups == null) {
+                continue;
+            }
+
+            for (SourceGroup sourceGroup : groups) {
+                FileObject rootFolder = sourceGroup.getRootFolder();
+                ClasspathInfo cpInfo = ClasspathInfo.create(
+                        ClassPath.getClassPath(rootFolder, ClassPath.BOOT),
+                        ClassPath.getClassPath(rootFolder, ClassPath.COMPILE),
+                        ClassPath.getClassPath(rootFolder, ClassPath.SOURCE)
+                );
+
+                Set<SearchScope> set = EnumSet.of(ClassIndex.SearchScope.SOURCE);
+                Set<ElementHandle<TypeElement>> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set);
+                for (ElementHandle<TypeElement> elementHandle : types) {
+                    JavaSource js = JavaSource.create(cpInfo);
+                    try {
+                        js.runUserActionTask((CompilationController control) -> {
+                            control.toPhase(JavaSource.Phase.RESOLVED);
+                            TypeElement elem = elementHandle.resolve(control);
+                            if (elem != null && doesInheritFromAppState(elem, control.getTypes())) {
+                                list.add(elem.getQualifiedName().toString());
+                            }
+                        }, false);
+                    } catch (IOException ioe) {
+                        Exceptions.printStackTrace(ioe);
                     }
                 }
             }
         }
+
         return list;
     }
 

+ 22 - 22
jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java

@@ -166,30 +166,30 @@ public class ProjectAssetManager extends DesktopAssetManager {
         }
         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
         List<URL> urls = new LinkedList<>();
-        for (SourceGroup sourceGroup : groups) {
-            ClassPath path = ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.EXECUTE);
-            if (path == null) {
-                continue;
-            }
-
-            classPaths.add(path);
-            path.addPropertyChangeListener(classPathListener);
-            FileObject[] roots = path.getRoots();
-            for (FileObject fileObject : roots) {
-                if (!fileObject.equals(getAssetFolder())) {
-                    fileObject.addRecursiveListener(listener);
-                    logger.log(Level.FINE, "Add classpath:{0}", fileObject);
-                    classPathItems.add(new ClassPathItem(fileObject, listener));
-                    urls.add(fileObject.toURL());
+            for (SourceGroup sourceGroup : groups) {
+                ClassPath path = ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.EXECUTE);
+                if (path == null) {
+                    continue;
                 }
-                if (fileObject.toURL().toExternalForm().startsWith("jar")) {
-                    logger.log(Level.FINE, "Add locator:{0}", fileObject.toURL());
-                    jarItems.add(fileObject);
-                    registerLocator(fileObject.toURL().toExternalForm(),
-                            "com.jme3.asset.plugins.UrlLocator");
+
+                classPaths.add(path);
+                path.addPropertyChangeListener(classPathListener);
+                FileObject[] roots = path.getRoots();
+                for (FileObject fileObject : roots) {
+                    if (!fileObject.equals(getAssetFolder())) {
+                        fileObject.addRecursiveListener(listener);
+                        logger.log(Level.FINE, "Add classpath:{0}", fileObject);
+                        classPathItems.add(new ClassPathItem(fileObject, listener));
+                        urls.add(fileObject.toURL());
+                    }
+                    if (fileObject.toURL().toExternalForm().startsWith("jar")) {
+                        logger.log(Level.FINE, "Add locator:{0}", fileObject.toURL());
+                        jarItems.add(fileObject);
+                        registerLocator(fileObject.toURL().toExternalForm(),
+                                "com.jme3.asset.plugins.UrlLocator");
+                    }
                 }
             }
-        }
 
         loadGradleClassLoader(urls);
 
@@ -223,7 +223,7 @@ public class ProjectAssetManager extends DesktopAssetManager {
         for (File file : runtimeFiles) {
             // logger.info(file.getName() + " : "  + file.getAbsolutePath());
             FileObject fo = FileUtil.toFileObject(file);
-            if (fo != null && !fo.isFolder()) {
+            if (fo != null) {
                 logger.info(fo.toURL().toExternalForm());
                 if (!fo.equals(getAssetFolder())) {
                     fo.addRecursiveListener(listener);

+ 83 - 61
jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java

@@ -31,9 +31,11 @@
  */
 package com.jme3.gde.core.sceneexplorer.nodes.actions.impl;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.EnumSet;
+import java.util.HashSet;
 import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 import javax.lang.model.element.TypeElement;
@@ -53,8 +55,10 @@ import org.netbeans.api.java.source.JavaSource;
 import org.netbeans.api.java.source.JavaSource.Phase;
 import org.netbeans.api.java.source.Task;
 import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectUtils;
 import org.netbeans.api.project.SourceGroup;
 import org.netbeans.api.project.Sources;
+import org.openide.filesystems.FileObject;
 import org.openide.util.Exceptions;
 
 @SuppressWarnings({"unchecked", "rawtypes"})
@@ -84,74 +88,92 @@ public final class NewCustomControlVisualPanel1 extends JPanel {
     }
 
     private List<String> getSources() {
-        Sources sources = proj.getLookup().lookup(Sources.class);
-        final List<String> list = new LinkedList<String>();
-        if (sources != null) {
+        Project root = ProjectUtils.rootOf(proj);
+        Set<Project> containedProjects = ProjectUtils.getContainedProjects(root, true);
+        List<Project> projects = new ArrayList<>();
+        projects.add(root);
+        if (containedProjects != null) {
+            projects.addAll(containedProjects);
+        }
+        if (projects.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        Set<String> list = new HashSet<>();
+        for (Project project : projects) {
+            Sources sources = project.getLookup().lookup(Sources.class);
+            if (sources == null) {
+                continue;
+            }
+
             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
-            if (groups != null) {
-                for (SourceGroup sourceGroup : groups) {
-                    final ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.BOOT),
-                            ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.COMPILE),
-                            ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.SOURCE));
-
-                    Set<SearchScope> set = EnumSet.of(ClassIndex.SearchScope.SOURCE);
-                    Set<ElementHandle<TypeElement>> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set);
-                    for (Iterator<ElementHandle<TypeElement>> it = types.iterator(); it.hasNext();) {
-                        final ElementHandle<TypeElement> elementHandle = it.next();
-                        JavaSource js = JavaSource.create(cpInfo);
-                        try {
-                            js.runUserActionTask(new Task<CompilationController>() {
-                                @Override
-                                public void run(CompilationController control)
-                                        throws Exception {
-                                    control.toPhase(Phase.RESOLVED);
-                                    //TODO: check with proper casting check.. gotta get TypeMirror of Control interface..
-//                                    TypeUtilities util = control.getTypeUtilities();//.isCastable(Types., null)
-//                                    util.isCastable(null, null);
-                                    TypeElement elem = elementHandle.resolve(control);
-                                    if (elem == null)
-                                        return;
-                                    
-                                    String elementName = elem.getQualifiedName().toString();
-                                    
-                                    if (list.contains(elementName)) /* No duplicates */
-                                        return;
-                                    
-                                    do {
-                                        //Check if it implements control interface
-                                        for (TypeMirror typeMirror : elem.getInterfaces()) {
-                                            String interfaceName = typeMirror.toString();
-                                            if ("com.jme3.scene.control.Control".equals(interfaceName)) {
-                                                if (!list.contains(elementName))
-                                                    list.add(elementName);
-                                                break;
-                                            }
-                                        }
-                                        //Check if it is an AbstractControl
-                                        String className = elem.toString();
-                                        if ("com.jme3.scene.control.AbstractControl".equals(className)) {
-                                            if (!list.contains(elementName))
-                                                list.add(elementName);
-                                        }
+            if (groups == null) {
+                continue;
+            }
 
-                                        TypeMirror superClass = elem.getSuperclass();
-                                        if (superClass == null || superClass.getKind() == TypeKind.NONE) {
-                                            break;
-                                        }
+            for (SourceGroup sourceGroup : groups) {
+                FileObject rootFolder = sourceGroup.getRootFolder();
+                final ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.getClassPath(rootFolder, ClassPath.BOOT),
+                        ClassPath.getClassPath(rootFolder, ClassPath.COMPILE),
+                        ClassPath.getClassPath(rootFolder, ClassPath.SOURCE));
+
+                Set<SearchScope> set = EnumSet.of(ClassIndex.SearchScope.SOURCE);
+                Set<ElementHandle<TypeElement>> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set);
+                for (Iterator<ElementHandle<TypeElement>> it = types.iterator(); it.hasNext();) {
+                    final ElementHandle<TypeElement> elementHandle = it.next();
+                    JavaSource js = JavaSource.create(cpInfo);
+                    try {
+                        js.runUserActionTask(new Task<CompilationController>() {
+                            @Override
+                            public void run(CompilationController control)
+                                    throws Exception {
+                                control.toPhase(Phase.RESOLVED);
+                                //TODO: check with proper casting check.. gotta get TypeMirror of Control interface..
+                                //                                    TypeUtilities util = control.getTypeUtilities();//.isCastable(Types., null)
+                                //                                    util.isCastable(null, null);
+                                TypeElement elem = elementHandle.resolve(control);
+                                if (elem == null) {
+                                    return;
+                                }
 
-                                        elem = (TypeElement)((DeclaredType)superClass).asElement(); // Iterate deeper
-                                    } while (elem != null);
+                                String elementName = elem.getQualifiedName().toString();
+
+                                if (list.contains(elementName)) /* No duplicates */ {
+                                    return;
                                 }
-                            }, false);
-                        } catch (Exception ioe) {
-                            Exceptions.printStackTrace(ioe);
-                        }
-                    }
 
+                                do {
+                                    //Check if it implements control interface
+                                    for (TypeMirror typeMirror : elem.getInterfaces()) {
+                                        String interfaceName = typeMirror.toString();
+                                        if ("com.jme3.scene.control.Control".equals(interfaceName) && !list.contains(elementName)) {
+                                            list.add(elementName);
+                                            break;
+                                        }
+                                    }
+                                    //Check if it is an AbstractControl
+                                    String className = elem.toString();
+                                    if ("com.jme3.scene.control.AbstractControl".equals(className) && !list.contains(elementName)) {
+                                        list.add(elementName);
+                                    }
+
+                                    TypeMirror superClass = elem.getSuperclass();
+                                    if (superClass == null || superClass.getKind() == TypeKind.NONE) {
+                                        break;
+                                    }
+
+                                    elem = (TypeElement) ((DeclaredType) superClass).asElement(); // Iterate deeper
+                                } while (elem != null);
+                            }
+                        }, false);
+                    } catch (Exception ioe) {
+                        Exceptions.printStackTrace(ioe);
+                    }
                 }
             }
         }
-        return list;
+
+        return new ArrayList<>(list);
     }
 
     public void load(Project proj) {