KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > comments > AutoCommentAction


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javadoc.comments;
21
22 import org.openide.nodes.Node;
23 import org.openide.util.NbBundle;
24 import org.openide.util.HelpCtx;
25 import org.openide.util.Lookup;
26 import org.openide.util.actions.CookieAction;
27 import org.openide.cookies.SourceCookie;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataFolder;
30 import org.openide.filesystems.FileObject;
31 import org.netbeans.jmi.javamodel.Element;
32
33 import java.util.List JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * Searches given folder for java sources and provides their Javadoc
39  * correction dialogs.
40  *
41  * @author Petr Hrebejk
42  */

43 public class AutoCommentAction extends CookieAction {
44     
45     static final long serialVersionUID =4989490116568783623L;
46
47     public AutoCommentAction() {
48         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
49
}
50     
51     /** Human presentable name of the action. This should be
52      * presented as an item in a menu.
53      * @return the name of the action
54      */

55     public String JavaDoc getName() {
56         return NbBundle.getBundle( AutoCommentAction.class ).getString("CTL_AUTOCOMMENT_MenuItem"); //NOI18N
57
}
58     
59     /** Cookie classes contains one class returned by cookie () method.
60      */

61     protected final Class JavaDoc[] cookieClasses() {
62         return new Class JavaDoc[] { DataFolder.class, SourceCookie.Editor.class };
63     }
64     
65     /** All must be DataFolders or JavaDataObjects
66      */

67     protected int mode() {
68         return MODE_ALL;
69     }
70     
71     /** Help context where to find more about the action.
72      * @return the help context for this action
73      */

74     public HelpCtx getHelpCtx() {
75         return new HelpCtx(AutoCommentAction.class);
76     }
77     
78     
79     protected boolean enable( Node[] activatedNodes ) {
80         if( activatedNodes.length == 0 )
81             return false;
82         List JavaDoc/*<Node>*/ unresolvedNodes = new LinkedList JavaDoc();
83         for( int i = 0; i < activatedNodes.length; i++ ){
84             Lookup lkp = activatedNodes[i].getLookup();
85             if (lkp.lookup(Element.class) == null // hierarchy node?
86
&& lkp.lookup(SourceCookie.class) == null) { // java node?
87

88                 unresolvedNodes.add(activatedNodes[i]);
89                 continue;
90             }
91             
92             DataObject doj = (DataObject) lkp.lookup(DataObject.class);
93             if( doj == null || !(doj.getPrimaryFile().canWrite()) )
94                 return false;
95         }
96         if(unresolvedNodes.isEmpty())
97             return true;
98         else {
99             return findInFolder( unresolvedNodes );
100         }
101     }
102     
103     /**
104      * @param unresolvedNodes activated nodes on which look up
105      * @return false if there is any uncommentable source
106      */

107     private boolean findInFolder(List JavaDoc/*<Node>*/ unresolvedNodes){
108         // check if all nodes are folders
109
for (Iterator JavaDoc it = unresolvedNodes.iterator(); it.hasNext();) {
110             Node node = (Node) it.next();
111             DataFolder df = (DataFolder) node.getLookup().lookup(DataFolder.class);
112             if (df == null || !isPackage(df.getPrimaryFile())) {
113                 return false;
114             }
115         }
116         
117
118         // all folders should be packages
119
return true;
120     }
121
122     /**
123      * Optimized detection of packages. Using Classpath API is too expensive
124      * (see issue #67027).
125      * @param fo file to check
126      * @return if it is a package
127      */

128     private static boolean isPackage(FileObject fo) {
129         if (!fo.canWrite()) {
130             return false;
131         }
132
133         FileObject[] children = fo.getChildren();
134
135         for (int i = 0; i < children.length; i++) {
136             FileObject child = children[i];
137             if ("java".equalsIgnoreCase(child.getExt()) // NOI18N
138
&& child.isData() && child.canWrite()) {
139                 return true;
140             }
141         }
142
143         return false;
144     }
145
146     protected boolean asynchronous() {
147         return false;
148     }
149
150     /** This method is called by one of the "invokers" as a result of
151      * some user's action that should lead to actual "performing" of the action.
152      * This default implementation calls the assigned actionPerformer if it
153      * is not null otherwise the action is ignored.
154      */

155     public void performAction( Node[] nodes ) {
156         
157         AutoCommentTopComponent acTopComponent = AutoCommentTopComponent.getDefault();
158         
159         acTopComponent.open();
160         acTopComponent.requestActive();
161         
162         acTopComponent.setAutoCommenter( new AutoCommenter( nodes ));
163     }
164 }
165
Popular Tags