KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > completion > DocumentationItem


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 /*
21  * DocumentationItem.java
22  *
23  * Created on June 28, 2006, 10:47 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.schema.completion;
30
31 import java.net.URL JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import org.netbeans.modules.xml.axi.AXIComponent;
35 import org.netbeans.modules.xml.axi.AXIType;
36 import org.netbeans.modules.xml.axi.AbstractAttribute;
37 import org.netbeans.modules.xml.axi.AbstractElement;
38 import org.netbeans.modules.xml.axi.AnyAttribute;
39 import org.netbeans.modules.xml.axi.AnyElement;
40 import org.netbeans.modules.xml.axi.Attribute;
41 import org.netbeans.modules.xml.axi.datatype.Datatype;
42 import org.netbeans.modules.xml.schema.model.Attribute.Use;
43 import org.netbeans.spi.editor.completion.CompletionDocumentation;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author Samaresh
49  */

50 public abstract class DocumentationItem implements CompletionDocumentation {
51     
52     private CompletionResultItem completionItem;
53     
54     /**
55      * Creates a new instance of DocumentationItem
56      */

57     public DocumentationItem(CompletionResultItem item) {
58         this.completionItem = item;
59     }
60     
61     public static DocumentationItem createDocumentationItem(CompletionResultItem item) {
62         if(item.getAXIComponent() instanceof AbstractElement)
63             return new ElementDocItem(item);
64         if(item.getAXIComponent() instanceof AbstractAttribute)
65             return new AttributeDocItem(item);
66         
67         return null;
68     }
69                 
70     public abstract String JavaDoc getText();
71     
72     public final CompletionResultItem getCompletionItem() {
73         return completionItem;
74     }
75     
76     public URL JavaDoc getURL() {
77         return null;
78     }
79     
80     public CompletionDocumentation resolveLink(String JavaDoc link) {
81         return null;
82     }
83     
84     public Action JavaDoc getGotoSourceAction() {
85         return null;
86     }
87     
88     static class ElementDocItem extends DocumentationItem {
89                 
90         public ElementDocItem(CompletionResultItem item) {
91             super(item);
92         }
93         
94         public String JavaDoc getText() {
95             AXIComponent axiComponent = getCompletionItem().getAXIComponent();
96             if(!(axiComponent instanceof AbstractElement))
97                 return null;
98             AbstractElement element = (AbstractElement)axiComponent;
99             String JavaDoc[] params = {"","","","",""};
100             params[0] = element.getTargetNamespace();
101             if(params[0] == null)
102                 params[0] = NbBundle.getMessage(DocumentationQuery.class,
103                     "Documentation-Text-No-TNS");
104             params[1] = element.getName();
105             params[2] = element.getDocumentation();
106             if(params[2] == null)
107                 params[2] = NbBundle.getMessage(DocumentationQuery.class,
108                     "Documentation-Text-Element-No-Description");
109             params[3] = formChildElementsHTML(element);
110             if(params[3] == null)
111                 params[3] = NbBundle.getMessage(DocumentationQuery.class,
112                     "Documentation-Text-Element-No-Child-Elements");
113             params[4] = formAttributesHTML(element);
114             if(params[4] == null)
115                 params[4] = NbBundle.getMessage(DocumentationQuery.class,
116                     "Documentation-Text-Element-No-Attributes");
117             return NbBundle.getMessage(DocumentationQuery.class,
118                     "Documentation-Text-Element", params);
119         }
120         
121         private String JavaDoc formChildElementsHTML(AbstractElement element) {
122             List JavaDoc<AbstractElement> children = element.getChildElements();
123             if(children == null || children.size() == 0)
124                 return null;
125             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
126             for(AbstractElement e: children) {
127                 String JavaDoc min = e.getMinOccurs();
128                 if(min != null && min.equals("1")) {
129                     buffer.append("<b>" + e.getName() + "</b>");
130                 } else {
131                     buffer.append(e.getName());
132                 }
133                 buffer.append(" ");
134                 if(e.supportsCardinality()) {
135                     buffer.append("[" + e.getMinOccurs() + ".." + e.getMaxOccurs() + "]");
136                 }
137                 if(e instanceof AnyElement) {
138                     buffer.append(" ");
139                     buffer.append("{" + e.getTargetNamespace() + "}");
140                 }
141                 buffer.append("<br>"); //NOI18N
142
}
143             
144             return buffer.toString();
145         }
146         
147         private String JavaDoc formAttributesHTML(AbstractElement element) {
148             List JavaDoc<AbstractAttribute> attrs = element.getAttributes();
149             if(attrs == null || attrs.size() == 0)
150                 return null;
151             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
152             for(AbstractAttribute attr: attrs) {
153                 if(attr instanceof Attribute) {
154                     Use use = ((Attribute)attr).getUse();
155                     if(use != null && use == Use.REQUIRED) //NOI18N
156
buffer.append("<b>" + attr.getName() + "</b>");
157                     else
158                         buffer.append(attr.getName());
159                 } else
160                     buffer.append(attr.getName());
161                 
162                 if(attr instanceof AnyAttribute) {
163                     buffer.append(" ");
164                     buffer.append("{" + attr.getTargetNamespace() + "}");
165                 }
166                 buffer.append("<br>"); //NOI18N
167
}
168             return buffer.toString();
169         }
170         
171     }
172
173         
174     static class AttributeDocItem extends DocumentationItem {
175                 
176         public AttributeDocItem(CompletionResultItem item) {
177             super(item);
178         }
179         
180         public String JavaDoc getText() {
181             AXIComponent axiComponent = getCompletionItem().getAXIComponent();
182             if(!(axiComponent instanceof AbstractAttribute))
183                 return null;
184             AbstractAttribute attribute = (AbstractAttribute)axiComponent;
185             String JavaDoc[] params = {"","","",""};
186             params[0] = attribute.getTargetNamespace();
187             if(params[0] == null)
188                 params[0] = NbBundle.getMessage(DocumentationQuery.class,
189                     "Documentation-Text-No-TNS");
190             params[1] = attribute.getName();
191             params[2] = attribute.getDocumentation();
192             if(params[2] == null)
193                 params[2] = NbBundle.getMessage(DocumentationQuery.class,
194                     "Documentation-Text-Attribute-No-Description");
195             if(attribute instanceof Attribute) {
196                 AXIType type = ((Attribute)attribute).getType();
197                 if(type instanceof Datatype) {
198                     params[3] = ((Datatype)type).getKind().getName();
199                 }
200             }
201             return NbBundle.getMessage(DocumentationQuery.class, "Documentation-Text-Attribute", params);
202         }
203     }
204     
205 }
206
Popular Tags