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-2006 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: package org.netbeans.modules.gsfret.editor.completion;
042:
043: import java.awt.event.ActionEvent;
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import javax.swing.AbstractAction;
047: import javax.swing.Action;
048: import org.netbeans.api.editor.completion.Completion;
049: import org.netbeans.modules.gsf.api.Completable;
050: import org.netbeans.modules.gsf.api.ElementHandle;
051: import org.netbeans.modules.gsf.LanguageRegistry;
052: import org.netbeans.napi.gsfret.source.CompilationController;
053: import org.netbeans.napi.gsfret.source.UiUtils;
054: import org.netbeans.modules.gsf.Language;
055: import org.netbeans.spi.editor.completion.CompletionDocumentation;
056: import org.openide.awt.HtmlBrowser;
057: import org.openide.util.Exceptions;
058:
059: /**
060: * Produce completion-popup help for elements
061: *
062: * @author Tor Norbye
063: */
064: public class GsfCompletionDoc implements CompletionDocumentation {
065: private String content = null;
066: private URL docURL = null;
067: private AbstractAction goToSource = null;
068: private ElementHandle elementHandle;
069: private Language language;
070: private CompilationController controller;
071:
072: private GsfCompletionDoc(final CompilationController controller,
073: final ElementHandle elementHandle, URL url) {
074: this .controller = controller;
075: this .language = controller.getLanguage();
076: if (elementHandle != null
077: && elementHandle.getMimeType() != null) {
078: Language embeddedLanguage = LanguageRegistry.getInstance()
079: .getLanguageByMimeType(elementHandle.getMimeType());
080: if (embeddedLanguage != null
081: && embeddedLanguage.getParser() != null) {
082: language = embeddedLanguage;
083: }
084: }
085:
086: Completable completer = language.getCompletionProvider();
087:
088: this .elementHandle = elementHandle;
089:
090: if (elementHandle != null) {
091: goToSource = new AbstractAction() {
092: public void actionPerformed(ActionEvent evt) {
093: Completion.get().hideAll();
094: UiUtils.open(controller.getSource(), elementHandle);
095: }
096: };
097: if (url != null) {
098: docURL = url;
099: } else {
100: docURL = null;
101: }
102: }
103:
104: this .content = completer.document(controller, elementHandle);
105:
106: if (this .content == null) {
107: Completion.get().hideDocumentation();
108: }
109: }
110:
111: public static final GsfCompletionDoc create(
112: CompilationController controller,
113: ElementHandle elementHandle) {
114: return new GsfCompletionDoc(controller, elementHandle, null);
115: }
116:
117: public String getText() {
118: return content;
119: }
120:
121: public URL getURL() {
122: return docURL;
123: }
124:
125: public CompletionDocumentation resolveLink(String link) {
126: if (link.startsWith("www.")) {
127: link = "http://" + link;
128: }
129: if (link.matches("[a-z]+://.*")) { // NOI18N
130: try {
131: URL url = new URL(link);
132: HtmlBrowser.URLDisplayer.getDefault().showURL(url);
133: return null;
134: } catch (MalformedURLException mue) {
135: Exceptions.printStackTrace(mue);
136: }
137: }
138:
139: ElementHandle handle = language.getCompletionProvider()
140: .resolveLink(link, elementHandle);
141: if (handle != null) {
142: return new GsfCompletionDoc(controller, handle, null);
143: }
144: return null;
145: }
146:
147: public Action getGotoSourceAction() {
148: return goToSource;
149: }
150: }
|