KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > TestAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
36  *
37  * @author Marian Petras
38  */

39 abstract class TestAction extends NodeAction {
40     
41     /** Creates a new instance of TestAction */
42     TestAction() {
43     }
44     
45     public boolean asynchronous() {
46         return true;
47         // yes, this action should run asynchronously
48
// would be better to rewrite it to synchronous (running in AWT thread),
49
// just replanning test generation to RequestProcessor
50
}
51     
52     /**
53      * Perform special enablement check in addition to the normal one.
54      *
55      * protected boolean enable (Node[] nodes) {
56      * if (!super.enable(nodes)) {
57      * return false;
58      * }
59      * }
60      *
61      * if (...) {
62      * ...
63      * }
64      */

65     @Override JavaDoc
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     /**
94      */

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