KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > completion > AttributeValueSupport


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.core.syntax.completion;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import javax.swing.text.JTextComponent JavaDoc;
27
28 import org.netbeans.editor.ext.CompletionQuery;
29 import org.openide.util.NbBundle;
30 import org.netbeans.modules.web.core.syntax.*;
31
32 /** Support for attribute value completion for JSP tags and directives.
33  *
34  * @author Petr Jiricka
35  * @version
36  */

37 public abstract class AttributeValueSupport extends Object JavaDoc {
38
39     private static Map JavaDoc supports;
40
41     public static void putSupport(AttributeValueSupport support) {
42         if (supports == null)
43             initialize();
44         // trick so we can construct a 'dummy' key element and get the 'real' element
45
supports.put(support, support);
46     }
47     
48     public static AttributeValueSupport getSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
49         if (supports == null)
50             initialize();
51         AttributeValueSupport support = new AttributeValueSupport.Default (tag, longName, attrName);
52         return (AttributeValueSupport)supports.get(support);
53     }
54     
55     private static void initialize() {
56         supports = new HashMap JavaDoc();
57         // jsp:useBean
58
putSupport(new AttrSupports.ScopeSupport(true, "jsp:useBean", "scope")); // NOI18N
59
putSupport(new AttrSupports.ClassNameSupport(true, "jsp:useBean", "class")); // NOI18N
60
// jsp:getProperty, jsp:setProperty
61
putSupport(new AttrSupports.GetSetPropertyName(true, "jsp:getProperty", "name")); // NOI18N
62
putSupport(new AttrSupports.GetSetPropertyName(true, "jsp:setProperty", "name")); // NOI18N
63
putSupport(new AttrSupports.GetPropertyProperty());
64         putSupport(new AttrSupports.SetPropertyProperty());
65         // @taglib
66
putSupport(new AttrSupports.TaglibURI());
67         putSupport(new AttrSupports.TaglibTagdir());
68         // @page
69
putSupport(new AttrSupports.PackageListSupport(false, "page", "import")); // NOI18N
70
putSupport(new AttrSupports.ClassNameSupport(false, "page", "extends")); // NOI18N
71
putSupport(new AttrSupports.PageLanguage());
72         putSupport(new AttrSupports.TrueFalseSupport(false, "page", "session")); // NOI18N
73
putSupport(new AttrSupports.TrueFalseSupport(false, "page", "autoFlush")); // NOI18N
74
putSupport(new AttrSupports.TrueFalseSupport(false, "page", "isThreadSafe")); // NOI18N
75
putSupport(new AttrSupports.TrueFalseSupport(false, "page", "isErrorPage")); // NOI18N
76
putSupport(new AttrSupports.FilenameSupport (false, "page", "errorPage")); //NOI18N
77
putSupport(new AttrSupports.EncodingSupport(false, "page", "pageEncoding")); // NOI18N
78
putSupport(new AttrSupports.TrueFalseSupport(false, "page", "isELIgnored")); // NOI18N
79
// @tag
80
putSupport(new AttrSupports.PackageListSupport(false, "tag", "import")); // NOI18N
81
putSupport(new AttrSupports.EncodingSupport(false, "tag", "pageEncoding")); // NOI18N
82
putSupport(new AttrSupports.TrueFalseSupport(false, "tag", "isELIgnored")); // NOI18N
83
putSupport(new AttrSupports.FilenameSupport(false, "tag", "small-icon")); // NOI18N
84
putSupport(new AttrSupports.FilenameSupport(false, "tag", "large-icon")); // NOI18N
85
// @attribute
86
putSupport(new AttrSupports.TrueFalseSupport(false, "attribute", "required")); // NOI18N
87
putSupport(new AttrSupports.TrueFalseSupport(false, "attribute", "fragment")); // NOI18N
88
putSupport(new AttrSupports.TrueFalseSupport(false, "attribute", "rtexprvalue")); // NOI18N
89
// @variable
90
putSupport(new AttrSupports.TrueFalseSupport(false, "variable", "declare")); // NOI18N
91
putSupport(new AttrSupports.VariableScopeSupport(false, "variable", "scope")); // NOI18N
92
putSupport(new AttrSupports.ClassNameSupport(false, "variable", "variable-class")); // NOI18N
93
// @include
94
putSupport(new AttrSupports.FilenameSupport (false, "include", "file")); //NOI18N
95
putSupport(new AttrSupports.FilenameSupport (true, "jsp:directive.include", "file")); //NOI18N
96

97         // jsp:include, jsp:forward
98
putSupport(new AttrSupports.FilenameSupport (true, "jsp:include", "page")); // NOI18N
99
putSupport(new AttrSupports.FilenameSupport (true, "jsp:forward", "page")); // NOI18N
100
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:include", "flush")); // NOI18N
101

102         putSupport(new AttrSupports.ScopeSupport(true, "jsp:doBody", "scope")); // NOI18N
103

104         putSupport(new AttrSupports.ScopeSupport(true, "jsp:invoke", "scope")); // NOI18N
105
// PENDING - add supports for known attributes
106

107         // jsp:directive.page
108
putSupport(new AttrSupports.PackageListSupport(true, "jsp:directive.page", "import")); // NOI18N
109
putSupport(new AttrSupports.ClassNameSupport(true, "jsp:directive.page", "extends")); // NOI18N
110
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:directive.page", "session")); // NOI18N
111
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:directive.page", "autoFlush")); // NOI18N
112
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:directive.page", "isThreadSafe")); // NOI18N
113
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:directive.page", "isErrorPage")); // NOI18N
114
putSupport(new AttrSupports.FilenameSupport (true, "jsp:directive.page", "errorPage")); //NOI18N
115
putSupport(new AttrSupports.EncodingSupport(true, "jsp:directive.page", "pageEncoding")); // NOI18N
116
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:directive.page", "isELIgnored")); // NOI18N
117

118         putSupport(new AttrSupports.YesNoTrueFalseSupport(true, "jsp:output", "omit-xml-declaration")); // NOI18N
119
putSupport(new AttrSupports.RootVersionSupport(true, "jsp:root", "version")); // NOI18N
120
putSupport(new AttrSupports.PluginTypeSupport(true, "jsp:plugin", "type")); // NOI18N
121
putSupport(new AttrSupports.TrueFalseSupport(true, "jsp:attribute", "trim")); // NOI18N
122

123     }
124     
125     protected boolean tag;
126     protected String JavaDoc longName;
127     protected String JavaDoc attrName;
128
129     /** Creates new AttributeValueSupport
130      * @param isTag whether this support is for tag or directive
131      * @param longName either directive name or tag name including prefix
132      * @param attribute name
133      */

134     public AttributeValueSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
135         this.tag = tag;
136         this.longName = longName;
137         this.attrName = attrName;
138     }
139     
140     public boolean equals(Object JavaDoc obj) {
141         AttributeValueSupport sup2 = (AttributeValueSupport)obj;
142         return (tag == sup2.tag) &&
143                (longName.equals(sup2.longName)) &&
144                (attrName.equals(sup2.attrName));
145     }
146     
147     public int hashCode() {
148         return longName.hashCode() + attrName.hashCode();
149     }
150
151     /** Returns the complete result. */
152     public abstract CompletionQuery.Result getResult(JTextComponent JavaDoc component,
153         int offset, JspSyntaxSupport sup, SyntaxElement.TagDirective item,
154         String JavaDoc valuePart);
155     
156     /** Default implementation of AttributeValueSupport.
157      * Only getPossibleValues method needs to be overriden for simple
158      * attribute support.
159      */

160     public static class Default extends AttributeValueSupport {
161         
162         /** Creates new DefaultAttributeValueSupport
163          * @param isTag whether this support is for tag or directive
164          * @param longName either directive name or tag name including prefix
165          * @param attribute name
166          */

167         public Default(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
168             super(tag, longName, attrName);
169         }
170         
171         /** Allows subclasses to override the default title. */
172         protected String JavaDoc completionTitle() {
173             return NbBundle.getMessage (JSPKit.class, "CTL_JSP_Completion_Title");
174         }
175     
176         /** Builds List of completion items.
177          * It uses results from <CODE>possibleValues</CODE> to build the list.
178          */

179         protected List JavaDoc createCompletionItems(int offset, JspSyntaxSupport sup, SyntaxElement.TagDirective item, String JavaDoc valuePart) {
180             //int valuePartLength = valuePart.length();
181
List JavaDoc values = sup.filterList(possibleValues(sup, item), valuePart);
182             List JavaDoc items = new ArrayList JavaDoc();
183             for (int i = 0; i < values.size(); i++) {
184                 items.add(new JspCompletionItem.AttributeValue((String JavaDoc)values.get(i)));
185             }
186             return items;
187         }
188     
189         /** Should return a list of Strings containing all possible values
190          * for this attribute. May return null if no options are available.
191          */

192         protected List JavaDoc possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
193             return new ArrayList JavaDoc ();
194         }
195         
196         /** Returns the complete result that contains elements from getCompletionItems.
197          * This implemantation uses createCompletionItems for obtaing of results but may be
198          * overriden.
199          */

200         public CompletionQuery.Result getResult (JTextComponent JavaDoc component, int offset,
201             JspSyntaxSupport sup, SyntaxElement.TagDirective item, String JavaDoc valuePart) {
202             List JavaDoc items = createCompletionItems (offset, sup, item, valuePart);
203             int valuePartLength = valuePart.length ();
204             
205             return new JspCompletionQuery.JspCompletionResult(component, completionTitle(),
206                 items, offset - valuePartLength, valuePartLength, -1);
207         }
208         
209     }
210     
211 }
Popular Tags