1 19 20 package org.netbeans.modules.junit; 21 22 import org.openide.filesystems.FileObject; 23 import org.openide.loaders.DataFolder; 24 import org.openide.loaders.DataObject; 25 import org.openide.nodes.Node; 26 import org.openide.util.actions.NodeAction; 27 import org.netbeans.api.java.project.JavaProjectConstants; 28 import org.netbeans.api.project.FileOwnerQuery; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectUtils; 31 import org.netbeans.api.project.SourceGroup; 32 import org.netbeans.api.project.Sources; 33 import org.openide.filesystems.FileUtil; 34 35 39 abstract class TestAction extends NodeAction { 40 41 42 TestAction() { 43 } 44 45 public boolean asynchronous() { 46 return true; 47 } 51 52 65 @Override 66 protected boolean enable(Node[] nodes) { 67 if (nodes.length == 0) { 68 return false; 69 } 70 71 for (Node node : nodes) { 72 DataObject dataObj = node.getCookie(DataObject.class); 73 if (dataObj != null) { 74 FileObject fileObj = dataObj.getPrimaryFile(); 75 if ((fileObj == null) || !fileObj.isValid()) { 76 continue; 77 } 78 79 Project prj = FileOwnerQuery.getOwner(fileObj); 80 if ((prj == null) || (getSourceGroup(fileObj, prj) == null)) { 81 continue; 82 } 83 84 if (TestUtil.isJavaFile(fileObj) 85 || (node.getCookie(DataFolder.class) != null)) { 86 return true; 87 } 88 } 89 } 90 return false; 91 } 92 93 95 private static SourceGroup getSourceGroup(FileObject file, Project prj) { 96 Sources src = ProjectUtils.getSources(prj); 97 SourceGroup[] srcGrps = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 98 for (SourceGroup srcGrp : srcGrps) { 99 FileObject rootFolder = srcGrp.getRootFolder(); 100 if (((file == rootFolder) || FileUtil.isParentOf(rootFolder, file)) 101 && srcGrp.contains(file)) { 102 return srcGrp; 103 } 104 } 105 return null; 106 } 107 108 109 } 110 | Popular Tags |