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: /*
043: * DocumentationItem.java
044: *
045: * Created on June 28, 2006, 10:47 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.schema.completion;
052:
053: import java.net.URL;
054: import java.util.List;
055: import javax.swing.Action;
056: import org.netbeans.modules.xml.axi.AXIComponent;
057: import org.netbeans.modules.xml.axi.AXIType;
058: import org.netbeans.modules.xml.axi.AbstractAttribute;
059: import org.netbeans.modules.xml.axi.AbstractElement;
060: import org.netbeans.modules.xml.axi.AnyAttribute;
061: import org.netbeans.modules.xml.axi.AnyElement;
062: import org.netbeans.modules.xml.axi.Attribute;
063: import org.netbeans.modules.xml.axi.datatype.Datatype;
064: import org.netbeans.modules.xml.schema.model.Attribute.Use;
065: import org.netbeans.spi.editor.completion.CompletionDocumentation;
066: import org.openide.util.NbBundle;
067:
068: /**
069: *
070: * @author Samaresh
071: */
072: public abstract class DocumentationItem implements
073: CompletionDocumentation {
074:
075: private CompletionResultItem completionItem;
076:
077: /**
078: * Creates a new instance of DocumentationItem
079: */
080: public DocumentationItem(CompletionResultItem item) {
081: this .completionItem = item;
082: }
083:
084: public static DocumentationItem createDocumentationItem(
085: CompletionResultItem item) {
086: if (item.getAXIComponent() instanceof AbstractElement)
087: return new ElementDocItem(item);
088: if (item.getAXIComponent() instanceof AbstractAttribute)
089: return new AttributeDocItem(item);
090:
091: return null;
092: }
093:
094: public abstract String getText();
095:
096: public final CompletionResultItem getCompletionItem() {
097: return completionItem;
098: }
099:
100: public URL getURL() {
101: return null;
102: }
103:
104: public CompletionDocumentation resolveLink(String link) {
105: return null;
106: }
107:
108: public Action getGotoSourceAction() {
109: return null;
110: }
111:
112: static class ElementDocItem extends DocumentationItem {
113:
114: public ElementDocItem(CompletionResultItem item) {
115: super (item);
116: }
117:
118: public String getText() {
119: AXIComponent axiComponent = getCompletionItem()
120: .getAXIComponent();
121: if (!(axiComponent instanceof AbstractElement))
122: return null;
123: AbstractElement element = (AbstractElement) axiComponent;
124: String[] params = { "", "", "", "", "" };
125: params[0] = element.getTargetNamespace();
126: if (params[0] == null)
127: params[0] = NbBundle.getMessage(
128: DocumentationQuery.class,
129: "Documentation-Text-No-TNS");
130: params[1] = element.getName();
131: params[2] = element.getDocumentation();
132: if (params[2] == null)
133: params[2] = NbBundle.getMessage(
134: DocumentationQuery.class,
135: "Documentation-Text-Element-No-Description");
136: params[3] = formChildElementsHTML(element);
137: if (params[3] == null)
138: params[3] = NbBundle.getMessage(
139: DocumentationQuery.class,
140: "Documentation-Text-Element-No-Child-Elements");
141: params[4] = formAttributesHTML(element);
142: if (params[4] == null)
143: params[4] = NbBundle.getMessage(
144: DocumentationQuery.class,
145: "Documentation-Text-Element-No-Attributes");
146: return NbBundle.getMessage(DocumentationQuery.class,
147: "Documentation-Text-Element", params);
148: }
149:
150: private String formChildElementsHTML(AbstractElement element) {
151: List<AbstractElement> children = element.getChildElements();
152: if (children == null || children.size() == 0)
153: return null;
154: StringBuffer buffer = new StringBuffer();
155: for (AbstractElement e : children) {
156: String min = e.getMinOccurs();
157: if (min != null && min.equals("1")) {
158: buffer.append("<b>" + e.getName() + "</b>");
159: } else {
160: buffer.append(e.getName());
161: }
162: buffer.append(" ");
163: if (e.supportsCardinality()) {
164: buffer.append("[" + e.getMinOccurs() + ".."
165: + e.getMaxOccurs() + "]");
166: }
167: if (e instanceof AnyElement) {
168: buffer.append(" ");
169: buffer.append("{" + e.getTargetNamespace() + "}");
170: }
171: buffer.append("<br>"); //NOI18N
172: }
173:
174: return buffer.toString();
175: }
176:
177: private String formAttributesHTML(AbstractElement element) {
178: List<AbstractAttribute> attrs = element.getAttributes();
179: if (attrs == null || attrs.size() == 0)
180: return null;
181: StringBuffer buffer = new StringBuffer();
182: for (AbstractAttribute attr : attrs) {
183: if (attr instanceof Attribute) {
184: Use use = ((Attribute) attr).getUse();
185: if (use != null && use == Use.REQUIRED) //NOI18N
186: buffer.append("<b>" + attr.getName() + "</b>");
187: else
188: buffer.append(attr.getName());
189: } else
190: buffer.append(attr.getName());
191:
192: if (attr instanceof AnyAttribute) {
193: buffer.append(" ");
194: buffer
195: .append("{" + attr.getTargetNamespace()
196: + "}");
197: }
198: buffer.append("<br>"); //NOI18N
199: }
200: return buffer.toString();
201: }
202:
203: }
204:
205: static class AttributeDocItem extends DocumentationItem {
206:
207: public AttributeDocItem(CompletionResultItem item) {
208: super (item);
209: }
210:
211: public String getText() {
212: AXIComponent axiComponent = getCompletionItem()
213: .getAXIComponent();
214: if (!(axiComponent instanceof AbstractAttribute))
215: return null;
216: AbstractAttribute attribute = (AbstractAttribute) axiComponent;
217: String[] params = { "", "", "", "" };
218: params[0] = attribute.getTargetNamespace();
219: if (params[0] == null)
220: params[0] = NbBundle.getMessage(
221: DocumentationQuery.class,
222: "Documentation-Text-No-TNS");
223: params[1] = attribute.getName();
224: params[2] = attribute.getDocumentation();
225: if (params[2] == null)
226: params[2] = NbBundle.getMessage(
227: DocumentationQuery.class,
228: "Documentation-Text-Attribute-No-Description");
229: if (attribute instanceof Attribute) {
230: AXIType type = ((Attribute) attribute).getType();
231: if (type instanceof Datatype) {
232: params[3] = ((Datatype) type).getKind().getName();
233: }
234: }
235: return NbBundle.getMessage(DocumentationQuery.class,
236: "Documentation-Text-Attribute", params);
237: }
238: }
239:
240: }
|