KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > ActionFilterNode


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.apisupport.project.ui;
21
22 import java.net.URL JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import javax.swing.Action JavaDoc;
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 // XXX this class is simplified version of the same class in the j2seproject.
43
// Get rid of it as soon as "some" Libraries Node API is provided.
44

45 /**
46  * This class decorates package nodes and file nodes under the Libraries Nodes.
47  * It removes all actions from these nodes except of file node's {@link OpenAction}
48  * and package node's {@link FindAction}. It also adds the {@link ShowJavadocAction}
49  * to both file and package nodes.
50  */

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 JavaDoc[] actionCache;
59     
60     /**
61      * Creates new ActionFilterNode for class path root.
62      * @param original the original node
63      * @return ActionFilterNode
64      */

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 JavaDoc[] getActions(boolean context) {
89         Action JavaDoc[] result = initActions();
90         return result;
91     }
92     
93     public Action JavaDoc getPreferredAction() {
94         if (mode == MODE_FILE) {
95             Action JavaDoc[] 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 JavaDoc[] initActions() {
104         if (actionCache == null) {
105             List JavaDoc<Action JavaDoc> result = new ArrayList JavaDoc<Action JavaDoc>(2);
106             if (mode == MODE_FILE) {
107                 for (Action JavaDoc 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 JavaDoc superAction : super.getActions(false)) {
116                     if (superAction instanceof FindAction) {
117                         result.add(superAction);
118                     }
119                 }
120             }
121             actionCache = result.toArray(new Action JavaDoc[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"; //NOI18N
143
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"; //NOI18N
154
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 JavaDoc relativeName = FileUtil.getRelativePath(cpRoot, resource);
183                 URL JavaDoc[] urls = JavadocForBinaryQuery.findJavadoc(cpRoot.getURL()).getRoots();
184                 URL JavaDoc pageURL;
185                 if (relativeName.length() == 0) {
186                     pageURL = ShowJavadocAction.findJavadoc("overview-summary.html",urls); //NOI18N
187
if (pageURL == null) {
188                         pageURL = ShowJavadocAction.findJavadoc("index.html",urls); //NOI18N
189
}
190                 } else if (resource.isFolder()) {
191                     //XXX Are the names the same also in the localized javadoc?
192
pageURL = ShowJavadocAction.findJavadoc(relativeName + "/package-summary.html", urls); //NOI18N
193
} else {
194                     String JavaDoc javadocFileName = relativeName.substring(0, relativeName.lastIndexOf('.')) + ".html"; //NOI18Ns
195
pageURL = ShowJavadocAction.findJavadoc(javadocFileName, urls);
196                 }
197                 ShowJavadocAction.showJavaDoc(pageURL,relativeName.replace('/','.')); //NOI18N
198
} catch (FileStateInvalidException fsi) {
199                 ErrorManager.getDefault().notify(fsi);
200             }
201         }
202         
203     }
204     
205 }
206
Popular Tags