1 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 ; 34 import java.util.LinkedList ; 35 import java.util.Iterator ; 36 37 43 public class AutoCommentAction extends CookieAction { 44 45 static final long serialVersionUID =4989490116568783623L; 46 47 public AutoCommentAction() { 48 putValue("noIconInMenu", Boolean.TRUE); } 50 51 55 public String getName() { 56 return NbBundle.getBundle( AutoCommentAction.class ).getString("CTL_AUTOCOMMENT_MenuItem"); } 58 59 61 protected final Class [] cookieClasses() { 62 return new Class [] { DataFolder.class, SourceCookie.Editor.class }; 63 } 64 65 67 protected int mode() { 68 return MODE_ALL; 69 } 70 71 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 unresolvedNodes = new LinkedList (); 83 for( int i = 0; i < activatedNodes.length; i++ ){ 84 Lookup lkp = activatedNodes[i].getLookup(); 85 if (lkp.lookup(Element.class) == null && lkp.lookup(SourceCookie.class) == null) { 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 107 private boolean findInFolder(List unresolvedNodes){ 108 for (Iterator 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 return true; 120 } 121 122 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()) && 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 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 |