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:
042: package org.netbeans.modules.spring.beans.completion;
043:
044: import java.io.File;
045: import java.net.URL;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.lang.model.element.Element;
049: import javax.swing.Action;
050: import org.netbeans.api.java.source.CompilationController;
051: import org.netbeans.api.java.source.ui.ElementJavadoc;
052: import org.netbeans.modules.spring.beans.utils.StringUtils;
053: import org.netbeans.spi.editor.completion.CompletionDocumentation;
054: import org.openide.util.NbBundle;
055:
056: /**
057: *
058: * @author Rohan Ranade (Rohan.Ranade@Sun.COM)
059: */
060: public abstract class SpringXMLConfigCompletionDoc implements
061: CompletionDocumentation {
062:
063: public static SpringXMLConfigCompletionDoc getAttribValueDoc(
064: String text) {
065: return new AttribValueDoc(text);
066: }
067:
068: public static SpringXMLConfigCompletionDoc createJavaDoc(
069: CompilationController cc, Element element) {
070: return new JavaElementDoc(ElementJavadoc.create(cc, element));
071: }
072:
073: public static SpringXMLConfigCompletionDoc createBeanRefDoc(
074: String beanId, List<String> beanNames,
075: String beanClassName, String beanFileLoc,
076: Action goToBeanAction) {
077: return new BeanRefDoc(beanId, beanNames, beanClassName,
078: beanFileLoc, goToBeanAction);
079: }
080:
081: public URL getURL() {
082: return null;
083: }
084:
085: public CompletionDocumentation resolveLink(String link) {
086: return null;
087: }
088:
089: public Action getGotoSourceAction() {
090: return null;
091: }
092:
093: private static class JavaElementDoc extends
094: SpringXMLConfigCompletionDoc {
095:
096: private ElementJavadoc elementJavadoc;
097:
098: public JavaElementDoc(ElementJavadoc elementJavadoc) {
099: this .elementJavadoc = elementJavadoc;
100: }
101:
102: @Override
103: public JavaElementDoc resolveLink(String link) {
104: ElementJavadoc doc = elementJavadoc.resolveLink(link);
105: return doc != null ? new JavaElementDoc(doc) : null;
106: }
107:
108: @Override
109: public URL getURL() {
110: return elementJavadoc.getURL();
111: }
112:
113: public String getText() {
114: return elementJavadoc.getText();
115: }
116:
117: @Override
118: public Action getGotoSourceAction() {
119: return elementJavadoc.getGotoSourceAction();
120: }
121: }
122:
123: private static class BeanRefDoc extends
124: SpringXMLConfigCompletionDoc {
125:
126: private String beanId;
127: private List<String> beanNames;
128: private String beanClassName;
129: private String beanLocFile;
130: private Action goToBeanAction;
131: private String displayText;
132:
133: public BeanRefDoc(String beanId, List<String> beanNames,
134: String beanClassName, String beanLocFile,
135: Action goToBeanAction) {
136: this .beanId = beanId;
137: this .beanNames = beanNames;
138: this .beanClassName = beanClassName;
139: this .beanLocFile = beanLocFile;
140: this .goToBeanAction = goToBeanAction;
141: }
142:
143: public String getText() {
144: if (displayText == null) {
145: StringBuilder sb = new StringBuilder();
146: if (beanLocFile != null) {
147: sb.append("<b>"); // NOI18N
148: sb.append(new File(beanLocFile).getName());
149: sb.append("</b>"); // NOI18N
150: sb.append("<br>"); // NOI18N
151: sb.append("<br>"); // NOI18N
152: }
153: String beanName = getBeanName();
154: List<String> otherNames = new ArrayList<String>(
155: beanNames);
156: otherNames.remove(beanName);
157: if (otherNames.size() > 0) {
158: sb.append(NbBundle.getMessage(
159: SpringXMLConfigCompletionDoc.class,
160: "LBL_BeanLabelWithAliases", beanName,
161: StringUtils.join(otherNames, ", "))); // NOI18N
162: } else {
163: sb.append(NbBundle.getMessage(
164: SpringXMLConfigCompletionDoc.class,
165: "LBL_BeanLabel", beanName));
166: }
167: if (beanClassName != null) {
168: sb.append("<pre>"); // NOI18N
169: sb.append(beanClassName);
170: sb.append("</pre>"); // NOI18N
171: }
172: displayText = sb.toString();
173: }
174: return displayText;
175: }
176:
177: private String getBeanName() {
178: if (beanId != null && beanId.trim().length() > 0) {
179: return beanId;
180: } else {
181: for (String name : beanNames) {
182: if (name != null && name.trim().length() > 0) {
183: return name;
184: }
185: }
186: }
187: return NbBundle.getMessage(
188: SpringXMLConfigCompletionDoc.class, "LBL_Unnamed");
189: }
190:
191: @Override
192: public Action getGotoSourceAction() {
193: return goToBeanAction;
194: }
195: }
196:
197: private static class AttribValueDoc extends
198: SpringXMLConfigCompletionDoc {
199:
200: private String text;
201:
202: public AttribValueDoc(String text) {
203: this .text = text;
204: }
205:
206: public String getText() {
207: return text;
208: }
209: }
210: }
|