1 19 20 package org.netbeans.modules.apisupport.project.ui; 21 22 import java.net.URL ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import javax.swing.Action ; 26 import org.openide.ErrorManager; 27 import org.openide.actions.EditAction; 28 import org.openide.actions.FindAction; 29 import org.openide.loaders.DataObject; 30 import org.openide.actions.OpenAction; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileStateInvalidException; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.nodes.FilterNode; 35 import org.openide.nodes.Node; 36 import org.openide.util.Lookup; 37 import org.openide.util.actions.SystemAction; 38 import org.openide.util.lookup.Lookups; 39 import org.openide.util.lookup.ProxyLookup; 40 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 41 42 45 51 class ActionFilterNode extends FilterNode { 52 53 private static final int MODE_PACKAGE = 2; 54 private static final int MODE_FILE = 3; 55 private static final int MODE_FILE_CONTENT = 4; 56 57 private final int mode; 58 private Action [] actionCache; 59 60 65 static ActionFilterNode create(Node original) { 66 DataObject dobj = original.getLookup().lookup(DataObject.class); 67 assert dobj != null; 68 FileObject root = dobj.getPrimaryFile(); 69 Lookup lkp = new ProxyLookup(original.getLookup(), Lookups.singleton(new JavadocProvider(root, root))); 70 return new ActionFilterNode(original, MODE_PACKAGE, root, lkp); 71 } 72 73 private ActionFilterNode(Node original, int mode, FileObject cpRoot, FileObject resource) { 74 this(original, mode, cpRoot, 75 new ProxyLookup(original.getLookup(),Lookups.singleton(new JavadocProvider(cpRoot,resource)))); 76 } 77 78 private ActionFilterNode(Node original, int mode) { 79 super(original, original.isLeaf() ? Children.LEAF : new ActionFilterChildren(original, mode, null)); 80 this.mode = mode; 81 } 82 83 private ActionFilterNode(Node original, int mode, FileObject root, Lookup lkp) { 84 super(original, original.isLeaf() ? Children.LEAF : new ActionFilterChildren(original, mode,root),lkp); 85 this.mode = mode; 86 } 87 88 public Action [] getActions(boolean context) { 89 Action [] result = initActions(); 90 return result; 91 } 92 93 public Action getPreferredAction() { 94 if (mode == MODE_FILE) { 95 Action [] actions = initActions(); 96 if (actions.length > 0 && (actions[0] instanceof OpenAction || actions[0] instanceof EditAction )) { 97 return actions[0]; 98 } 99 } 100 return null; 101 } 102 103 private Action [] initActions() { 104 if (actionCache == null) { 105 List <Action > result = new ArrayList <Action >(2); 106 if (mode == MODE_FILE) { 107 for (Action superAction : super.getActions(false)) { 108 if (superAction instanceof OpenAction || superAction instanceof EditAction) { 109 result.add(superAction); 110 } 111 } 112 result.add(SystemAction.get(ShowJavadocAction.class)); 113 } else if (mode == MODE_PACKAGE) { 114 result.add(SystemAction.get(ShowJavadocAction.class)); 115 for (Action superAction : super.getActions(false)) { 116 if (superAction instanceof FindAction) { 117 result.add(superAction); 118 } 119 } 120 } 121 actionCache = result.toArray(new Action [result.size()]); 122 } 123 return actionCache; 124 } 125 126 private static class ActionFilterChildren extends FilterNode.Children { 127 128 private final int mode; 129 private final FileObject cpRoot; 130 131 ActionFilterChildren(Node original, int mode, FileObject cpRooot) { 132 super(original); 133 this.mode = mode; 134 this.cpRoot = cpRooot; 135 } 136 137 protected Node[] createNodes(Node n) { 138 switch (mode) { 139 case MODE_PACKAGE: 140 DataObject dobj = n.getCookie(DataObject.class); 141 if (dobj == null) { 142 assert false : "DataNode without DataObject in Lookup"; return new Node[0]; 144 } else if (dobj.getPrimaryFile().isFolder()) { 145 return new Node[] {new ActionFilterNode(n, MODE_PACKAGE, cpRoot, dobj.getPrimaryFile())}; 146 } else { 147 return new Node[] {new ActionFilterNode(n, MODE_FILE, cpRoot, dobj.getPrimaryFile())}; 148 } 149 case MODE_FILE: 150 case MODE_FILE_CONTENT: 151 return new Node[] {new ActionFilterNode(n, MODE_FILE_CONTENT)}; 152 default: 153 assert false : "Unknown mode"; return new Node[0]; 155 } 156 } 157 158 } 159 160 private static class JavadocProvider implements ShowJavadocAction.JavadocProvider { 161 162 private final FileObject cpRoot; 163 private final FileObject resource; 164 165 JavadocProvider(FileObject cpRoot, FileObject resource) { 166 this.cpRoot = cpRoot; 167 this.resource = resource; 168 } 169 170 public boolean hasJavadoc() { 171 try { 172 boolean rNotNull = resource != null; 173 int jLength = JavadocForBinaryQuery.findJavadoc(cpRoot.getURL()).getRoots().length; 174 return rNotNull && jLength > 0; 175 } catch (FileStateInvalidException fsi) { 176 return false; 177 } 178 } 179 180 public void showJavadoc() { 181 try { 182 String relativeName = FileUtil.getRelativePath(cpRoot, resource); 183 URL [] urls = JavadocForBinaryQuery.findJavadoc(cpRoot.getURL()).getRoots(); 184 URL pageURL; 185 if (relativeName.length() == 0) { 186 pageURL = ShowJavadocAction.findJavadoc("overview-summary.html",urls); if (pageURL == null) { 188 pageURL = ShowJavadocAction.findJavadoc("index.html",urls); } 190 } else if (resource.isFolder()) { 191 pageURL = ShowJavadocAction.findJavadoc(relativeName + "/package-summary.html", urls); } else { 194 String javadocFileName = relativeName.substring(0, relativeName.lastIndexOf('.')) + ".html"; pageURL = ShowJavadocAction.findJavadoc(javadocFileName, urls); 196 } 197 ShowJavadocAction.showJavaDoc(pageURL,relativeName.replace('/','.')); } catch (FileStateInvalidException fsi) { 199 ErrorManager.getDefault().notify(fsi); 200 } 201 } 202 203 } 204 205 } 206 | Popular Tags |