001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.ui;
043:
044: import java.net.URL;
045: import java.util.List;
046: import java.util.ArrayList;
047: import javax.swing.Action;
048: import org.openide.ErrorManager;
049: import org.openide.actions.EditAction;
050: import org.openide.actions.FindAction;
051: import org.openide.loaders.DataObject;
052: import org.openide.actions.OpenAction;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileStateInvalidException;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.nodes.FilterNode;
057: import org.openide.nodes.Node;
058: import org.openide.util.Lookup;
059: import org.openide.util.actions.SystemAction;
060: import org.openide.util.lookup.Lookups;
061: import org.openide.util.lookup.ProxyLookup;
062: import org.netbeans.api.java.queries.JavadocForBinaryQuery;
063:
064: // XXX this class is simplified version of the same class in the j2seproject.
065: // Get rid of it as soon as "some" Libraries Node API is provided.
066:
067: /**
068: * This class decorates package nodes and file nodes under the Libraries Nodes.
069: * It removes all actions from these nodes except of file node's {@link OpenAction}
070: * and package node's {@link FindAction}. It also adds the {@link ShowJavadocAction}
071: * to both file and package nodes.
072: */
073: class ActionFilterNode extends FilterNode {
074:
075: private static final int MODE_PACKAGE = 2;
076: private static final int MODE_FILE = 3;
077: private static final int MODE_FILE_CONTENT = 4;
078:
079: private final int mode;
080: private Action[] actionCache;
081:
082: /**
083: * Creates new ActionFilterNode for class path root.
084: * @param original the original node
085: * @return ActionFilterNode
086: */
087: static ActionFilterNode create(Node original) {
088: DataObject dobj = original.getLookup().lookup(DataObject.class);
089: assert dobj != null;
090: FileObject root = dobj.getPrimaryFile();
091: Lookup lkp = new ProxyLookup(original.getLookup(), Lookups
092: .singleton(new JavadocProvider(root, root)));
093: return new ActionFilterNode(original, MODE_PACKAGE, root, lkp);
094: }
095:
096: private ActionFilterNode(Node original, int mode,
097: FileObject cpRoot, FileObject resource) {
098: this (original, mode, cpRoot, new ProxyLookup(original
099: .getLookup(), Lookups.singleton(new JavadocProvider(
100: cpRoot, resource))));
101: }
102:
103: private ActionFilterNode(Node original, int mode) {
104: super (original, original.isLeaf() ? Children.LEAF
105: : new ActionFilterChildren(original, mode, null));
106: this .mode = mode;
107: }
108:
109: private ActionFilterNode(Node original, int mode, FileObject root,
110: Lookup lkp) {
111: super (original, original.isLeaf() ? Children.LEAF
112: : new ActionFilterChildren(original, mode, root), lkp);
113: this .mode = mode;
114: }
115:
116: public Action[] getActions(boolean context) {
117: Action[] result = initActions();
118: return result;
119: }
120:
121: public Action getPreferredAction() {
122: if (mode == MODE_FILE) {
123: Action[] actions = initActions();
124: if (actions.length > 0
125: && (actions[0] instanceof OpenAction || actions[0] instanceof EditAction)) {
126: return actions[0];
127: }
128: }
129: return null;
130: }
131:
132: private Action[] initActions() {
133: if (actionCache == null) {
134: List<Action> result = new ArrayList<Action>(2);
135: if (mode == MODE_FILE) {
136: for (Action super Action : super .getActions(false)) {
137: if (super Action instanceof OpenAction
138: || super Action instanceof EditAction) {
139: result.add(super Action);
140: }
141: }
142: result.add(SystemAction.get(ShowJavadocAction.class));
143: } else if (mode == MODE_PACKAGE) {
144: result.add(SystemAction.get(ShowJavadocAction.class));
145: for (Action super Action : super .getActions(false)) {
146: if (super Action instanceof FindAction) {
147: result.add(super Action);
148: }
149: }
150: }
151: actionCache = result.toArray(new Action[result.size()]);
152: }
153: return actionCache;
154: }
155:
156: private static class ActionFilterChildren extends
157: FilterNode.Children {
158:
159: private final int mode;
160: private final FileObject cpRoot;
161:
162: ActionFilterChildren(Node original, int mode, FileObject cpRooot) {
163: super (original);
164: this .mode = mode;
165: this .cpRoot = cpRooot;
166: }
167:
168: protected Node[] createNodes(Node n) {
169: switch (mode) {
170: case MODE_PACKAGE:
171: DataObject dobj = n.getCookie(DataObject.class);
172: if (dobj == null) {
173: assert false : "DataNode without DataObject in Lookup"; //NOI18N
174: return new Node[0];
175: } else if (dobj.getPrimaryFile().isFolder()) {
176: return new Node[] { new ActionFilterNode(n,
177: MODE_PACKAGE, cpRoot, dobj.getPrimaryFile()) };
178: } else {
179: return new Node[] { new ActionFilterNode(n,
180: MODE_FILE, cpRoot, dobj.getPrimaryFile()) };
181: }
182: case MODE_FILE:
183: case MODE_FILE_CONTENT:
184: return new Node[] { new ActionFilterNode(n,
185: MODE_FILE_CONTENT) };
186: default:
187: assert false : "Unknown mode"; //NOI18N
188: return new Node[0];
189: }
190: }
191:
192: }
193:
194: private static class JavadocProvider implements
195: ShowJavadocAction.JavadocProvider {
196:
197: private final FileObject cpRoot;
198: private final FileObject resource;
199:
200: JavadocProvider(FileObject cpRoot, FileObject resource) {
201: this .cpRoot = cpRoot;
202: this .resource = resource;
203: }
204:
205: public boolean hasJavadoc() {
206: try {
207: boolean rNotNull = resource != null;
208: int jLength = JavadocForBinaryQuery.findJavadoc(
209: cpRoot.getURL()).getRoots().length;
210: return rNotNull && jLength > 0;
211: } catch (FileStateInvalidException fsi) {
212: return false;
213: }
214: }
215:
216: public void showJavadoc() {
217: try {
218: String relativeName = FileUtil.getRelativePath(cpRoot,
219: resource);
220: URL[] urls = JavadocForBinaryQuery.findJavadoc(
221: cpRoot.getURL()).getRoots();
222: URL pageURL;
223: if (relativeName.length() == 0) {
224: pageURL = ShowJavadocAction.findJavadoc(
225: "overview-summary.html", urls); //NOI18N
226: if (pageURL == null) {
227: pageURL = ShowJavadocAction.findJavadoc(
228: "index.html", urls); //NOI18N
229: }
230: } else if (resource.isFolder()) {
231: //XXX Are the names the same also in the localized javadoc?
232: pageURL = ShowJavadocAction.findJavadoc(
233: relativeName + "/package-summary.html",
234: urls); //NOI18N
235: } else {
236: String javadocFileName = relativeName.substring(0,
237: relativeName.lastIndexOf('.'))
238: + ".html"; //NOI18Ns
239: pageURL = ShowJavadocAction.findJavadoc(
240: javadocFileName, urls);
241: }
242: ShowJavadocAction.showJavaDoc(pageURL, relativeName
243: .replace('/', '.')); //NOI18N
244: } catch (FileStateInvalidException fsi) {
245: ErrorManager.getDefault().notify(fsi);
246: }
247: }
248:
249: }
250:
251: }
|