From 38559575802b46b54f1c4a622b802d456d34f980 Mon Sep 17 00:00:00 2001 From: Toni Helenius Date: Sat, 21 Dec 2024 17:23:48 +0200 Subject: [PATCH 1/5] Traverse through the project structure for app states (Gradle) --- .../appstates/NewAppStateVisualPanel1.java | 75 ++++++++++++------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java b/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java index 84b8352d2..d85ecefe3 100644 --- a/jme3-core/src/com/jme3/gde/core/appstates/NewAppStateVisualPanel1.java +++ b/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.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 @@ private void scanControls() { } private List getSources() { - Sources sources = ProjectUtils.getSources(proj); - final List list = new LinkedList<>(); - if (sources != null) { + Project root = ProjectUtils.rootOf(proj); + Set containedProjects = ProjectUtils.getContainedProjects(root, true); + List projects = new ArrayList<>(); + projects.add(root); + if (containedProjects != null) { + projects.addAll(containedProjects); + } + if (projects.isEmpty()) { + return Collections.emptyList(); + } + + List 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 set = EnumSet.of(ClassIndex.SearchScope.SOURCE); - Set> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set); - for (ElementHandle 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 set = EnumSet.of(ClassIndex.SearchScope.SOURCE); + Set> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set); + for (ElementHandle 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; } From 88208f19ecd2e7b609f89a38444c019da4ed1ede Mon Sep 17 00:00:00 2001 From: Toni Helenius Date: Sat, 21 Dec 2024 19:00:53 +0200 Subject: [PATCH 2/5] Also add folders (adds the source compilation directory) --- .../gde/core/assets/ProjectAssetManager.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java b/jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java index d19e45d93..aeb49776d 100644 --- a/jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java +++ b/jme3-core/src/com/jme3/gde/core/assets/ProjectAssetManager.java @@ -166,30 +166,30 @@ private void loadClassLoader() { } SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); List 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 @@ private void loadGradleClassLoader(List urls) { 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); From 45b17520aa887998ff6b8bc54b1e82fb2f074297 Mon Sep 17 00:00:00 2001 From: Toni Helenius Date: Sat, 21 Dec 2024 19:09:54 +0200 Subject: [PATCH 3/5] Traverse through the project structure for controls (Gradle) --- .../impl/NewCustomControlVisualPanel1.java | 143 ++++++++++-------- 1 file changed, 83 insertions(+), 60 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java b/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java index 3adbe5b93..a6728eb51 100644 --- a/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java +++ b/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java @@ -31,9 +31,10 @@ */ package com.jme3.gde.core.sceneexplorer.nodes.actions.impl; +import java.util.ArrayList; +import java.util.Collections; import java.util.EnumSet; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Set; import javax.lang.model.element.TypeElement; @@ -53,6 +54,7 @@ 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.util.Exceptions; @@ -84,73 +86,94 @@ private void scanControls() { } private List getSources() { - Sources sources = proj.getLookup().lookup(Sources.class); - final List list = new LinkedList(); - if (sources != null) { + Project root = ProjectUtils.rootOf(proj); + Set containedProjects = ProjectUtils.getContainedProjects(root, true); + List projects = new ArrayList<>(); + projects.add(root); + if (containedProjects != null) { + projects.addAll(containedProjects); + } + if (projects.isEmpty()) { + return Collections.emptyList(); + } + + List list = new ArrayList<>(); + 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 set = EnumSet.of(ClassIndex.SearchScope.SOURCE); - Set> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set); - for (Iterator> it = types.iterator(); it.hasNext();) { - final ElementHandle elementHandle = it.next(); - JavaSource js = JavaSource.create(cpInfo); - try { - js.runUserActionTask(new Task() { - @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) { + final ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.BOOT), + ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.COMPILE), + ClassPath.getClassPath(sourceGroup.getRootFolder(), ClassPath.SOURCE)); + + Set set = EnumSet.of(ClassIndex.SearchScope.SOURCE); + Set> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set); + for (Iterator> it = types.iterator(); it.hasNext();) { + final ElementHandle elementHandle = it.next(); + JavaSource js = JavaSource.create(cpInfo); + try { + js.runUserActionTask(new Task() { + @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(); - elem = (TypeElement)((DeclaredType)superClass).asElement(); // Iterate deeper - } while (elem != null); + 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)) { + 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); + } + } + + 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; } From 39f65d389741c0412682ec66411d13a815752ffd Mon Sep 17 00:00:00 2001 From: Toni Helenius Date: Sun, 22 Dec 2024 15:14:32 +0200 Subject: [PATCH 4/5] Improve code --- .../impl/NewCustomControlVisualPanel1.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java b/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java index a6728eb51..eec167592 100644 --- a/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java +++ b/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.EnumSet; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -57,6 +58,7 @@ 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"}) @@ -97,7 +99,7 @@ private List getSources() { return Collections.emptyList(); } - List list = new ArrayList<>(); + Set list = new HashSet<>(); for (Project project : projects) { Sources sources = project.getLookup().lookup(Sources.class); if (sources == null) { @@ -110,9 +112,10 @@ private List getSources() { } 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)); + 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 set = EnumSet.of(ClassIndex.SearchScope.SOURCE); Set> types = cpInfo.getClassIndex().getDeclaredTypes("", NameKind.PREFIX, set); @@ -143,10 +146,8 @@ public void run(CompilationController control) //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); - } + if ("com.jme3.scene.control.Control".equals(interfaceName) && !list.contains(elementName)) { + list.add(elementName); break; } } @@ -174,7 +175,7 @@ public void run(CompilationController control) } } - return list; + return new ArrayList<>(list); } public void load(Project proj) { From cf7f9f0bbb907bd36ccc9749ef7af38e0f0343a8 Mon Sep 17 00:00:00 2001 From: Toni Helenius Date: Sun, 22 Dec 2024 15:20:50 +0200 Subject: [PATCH 5/5] Combine nested statement --- .../nodes/actions/impl/NewCustomControlVisualPanel1.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java b/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java index eec167592..6b2acf103 100644 --- a/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java +++ b/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/actions/impl/NewCustomControlVisualPanel1.java @@ -153,10 +153,8 @@ public void run(CompilationController control) } //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 ("com.jme3.scene.control.AbstractControl".equals(className) && !list.contains(elementName)) { + list.add(elementName); } TypeMirror superClass = elem.getSuperclass();