Browse Source

Gradle: Make the ProjectAssetManager work and expose the "Assets" Folder under the Project Dialog.

MeFisto94 5 years ago
parent
commit
1fdab22880

+ 26 - 1
jme3-core/src/com/jme3/gde/core/assets/AssetsLookupProvider.java

@@ -34,9 +34,12 @@ package com.jme3.gde.core.assets;
 import com.jme3.gde.core.j2seproject.ProjectExtensionManager;
 import com.jme3.gde.core.j2seproject.ProjectExtensionManager;
 import com.jme3.gde.core.j2seproject.actions.UpgradeProjectWizardAction;
 import com.jme3.gde.core.j2seproject.actions.UpgradeProjectWizardAction;
 import com.jme3.gde.core.projects.SDKProjectUtils;
 import com.jme3.gde.core.projects.SDKProjectUtils;
+import java.io.File;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Properties;
 import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.logging.Logger;
@@ -102,7 +105,29 @@ public class AssetsLookupProvider implements LookupProvider {
             GradleBaseProject gradle = GradleBaseProject.get(project);
             GradleBaseProject gradle = GradleBaseProject.get(project);
             if (gradle.getSubProjects().containsKey("assets")) {
             if (gradle.getSubProjects().containsKey("assets")) {
                 logger.log(Level.FINE, "Found assets subproject, extending with ProjectAssetManager");
                 logger.log(Level.FINE, "Found assets subproject, extending with ProjectAssetManager");
-                return Lookups.fixed(new ProjectAssetManager(prj, gradle.getSubProjects().get("assets").getAbsolutePath()));
+                /* Note: The ProjectAssetManager needs the Subproject's Project,
+                 * because otherwise reading assets won't work (as assets is
+                 * not in the scope of the parent project). But it still needs 
+                 * to be added to the Parent Project's Lookup
+                */
+                
+                /* Complicated code to find the relative path, because the 
+                 * assets subproject could be stored in a folder called "foo"
+                 * or "bar" and we still need to locate it correctly
+                */
+                Path assetsPath = gradle.getSubProjects().get("assets").toPath();
+                Path parentPath = new File(prj.getProjectDirectory().getPath()).toPath();
+                Path relativePath = parentPath.relativize(assetsPath);
+                try {
+                    Project assetsProj = ProjectManager.getDefault().findProject(
+                        prj.getProjectDirectory().getFileObject(relativePath.toString()));
+                    if (assetsProj != null) {
+                        return Lookups.fixed(new ProjectAssetManager(assetsProj,
+                        "")); // the String is seen relative to the project root
+                    }
+                } catch (IOException io) {
+                    Exceptions.printStackTrace(io);
+                }
             }
             }
         } else {
         } else {
             FileObject assetsProperties = prj.getProjectDirectory().getFileObject("nbproject/project.properties");
             FileObject assetsProperties = prj.getProjectDirectory().getFileObject("nbproject/project.properties");

+ 11 - 15
jme3-core/src/com/jme3/gde/core/assets/ProjectAssetsNodeFactory.java

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2009-2010 jMonkeyEngine
+ * Copyright (c) 2009-2020 jMonkeyEngine
  * All rights reserved.
  * All rights reserved.
  *
  *
  * Redistribution and use in source and binary forms, with or without
  * Redistribution and use in source and binary forms, with or without
@@ -37,41 +37,37 @@ import org.netbeans.spi.project.ui.support.NodeFactory;
 import org.netbeans.spi.project.ui.support.NodeFactorySupport;
 import org.netbeans.spi.project.ui.support.NodeFactorySupport;
 import org.netbeans.spi.project.ui.support.NodeList;
 import org.netbeans.spi.project.ui.support.NodeList;
 import org.openide.loaders.DataObject;
 import org.openide.loaders.DataObject;
+import org.openide.loaders.DataObjectNotFoundException;
 import org.openide.nodes.Node;
 import org.openide.nodes.Node;
 import org.openide.util.Exceptions;
 import org.openide.util.Exceptions;
 
 
 /**
 /**
- *
+ * This Factory creates the visual "Project Assets" Node in the Project View
  * @author normenhansen
  * @author normenhansen
  */
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
 @SuppressWarnings({"unchecked", "rawtypes"})
 public class ProjectAssetsNodeFactory implements NodeFactory {
 public class ProjectAssetsNodeFactory implements NodeFactory {
-
     private Project proj;
     private Project proj;
 
 
+    // return a new node for the project view if theres an assets folder
+    @Override
     public NodeList createNodes(Project project) {
     public NodeList createNodes(Project project) {
-
         this.proj = project;
         this.proj = project;
 
 
-        DataObject assetsFolder;
         try {
         try {
-            //return a new node for the project view if theres an assets folder:
             ProjectAssetManager item = project.getLookup().lookup(ProjectAssetManager.class);
             ProjectAssetManager item = project.getLookup().lookup(ProjectAssetManager.class);
             if (item != null) {
             if (item != null) {
-                assetsFolder = DataObject.find(item.getAssetFolder());
-                Node node = assetsFolder.getNodeDelegate();
-//                return NodeFactorySupport.fixedNodeList(node);
-                ProjectAssetsNode nd = new ProjectAssetsNode(item, proj, node);
-//                return NodeFactorySupport.createCompositeChildren(project, item.getAssetFolderName());//fixedNodeList(nd);
+                DataObject assetsFolder = DataObject.find(item.getAssetFolder());
+                ProjectAssetsNode nd = new ProjectAssetsNode(item, proj, assetsFolder.getNodeDelegate());
                 return NodeFactorySupport.fixedNodeList(nd);
                 return NodeFactorySupport.fixedNodeList(nd);
             }
             }
-        } catch (Exception ex) {
+        } catch (DataObjectNotFoundException ex) {
             Exceptions.printStackTrace(ex);
             Exceptions.printStackTrace(ex);
         }
         }
 
 
-        //If our item isn't in the lookup,
-        //then return an empty list of nodes:
+        /* If our item isn't in the lookup, which means it is no recognized project
+         * then return an empty list of nodes.
+         */
         return NodeFactorySupport.fixedNodeList();
         return NodeFactorySupport.fixedNodeList();
-
     }
     }
 }
 }

+ 29 - 2
jme3-core/src/com/jme3/gde/core/importantfiles/ImportantFilesNode.java

@@ -1,6 +1,33 @@
 /*
 /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
+ *  Copyright (c) 2009-2020 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  */
 package com.jme3.gde.core.importantfiles;
 package com.jme3.gde.core.importantfiles;
 
 

+ 51 - 0
jme3-core/src/com/jme3/gde/core/projects/SDKProjectUtils.java

@@ -0,0 +1,51 @@
+/*
+ *  Copyright (c) 2009-2020 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.core.projects;
+
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.gradle.api.GradleBaseProject;
+
+/**
+ * A collection of utility methods to guide the handling of Ant and Gradle Projects
+ * within the SDK<br />
+ * 
+ * @author MeFisto94
+ */
+
+public class SDKProjectUtils {
+    public static boolean isGradleProject(Project proj) {
+        return proj.getClass().getSimpleName().equals("NbGradleProjectImpl") || 
+                // because docs say this above is bad: https://bits.netbeans.org/dev/javadoc/org-netbeans-modules-projectapi/org/netbeans/api/project/Project.html
+                GradleBaseProject.get(proj) != null;
+    }
+    
+}