KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import java.beans.*;
24 import java.util.logging.Logger JavaDoc;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import org.netbeans.editor.ext.CompletionQuery;
28 import org.netbeans.modules.editor.NbEditorUtilities;
29 import org.netbeans.modules.web.jsps.parserapi.PageInfo;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileStateInvalidException;
32 import org.openide.filesystems.FileSystem;
33 import org.netbeans.modules.web.core.syntax.*;
34 import org.netbeans.modules.web.core.syntax.completion.JavaJSPCompletionProvider.CompletionQueryDelegatedToJava;
35 import org.netbeans.modules.web.core.syntax.completion.JspCompletionQuery.JspCompletionResult;
36 import org.netbeans.spi.editor.completion.CompletionItem;
37 import org.netbeans.spi.editor.completion.CompletionProvider;
38
39
40 /** Support for code completion of default JSP tags.
41  *
42  * @author pjiricka
43  */

44 public class AttrSupports extends Object JavaDoc {
45     private static final Logger JavaDoc logger = Logger.getLogger(AttrSupports.class.getName());
46     public static class ScopeSupport extends AttributeValueSupport.Default {
47         
48         public ScopeSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
49             super(tag, longName, attrName);
50         }
51         
52         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
53             ArrayList list = new ArrayList();
54             list.add("application"); // NOI18N
55
list.add("page"); // NOI18N
56
list.add("request"); // NOI18N
57
list.add("session"); // NOI18N
58
return list;
59         }
60         
61     }
62     
63     public static class RootVersionSupport extends AttributeValueSupport.Default {
64         
65         public RootVersionSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
66             super(tag, longName, attrName);
67         }
68         
69         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
70             ArrayList list = new ArrayList();
71             list.add("1.2"); // NOI18N
72
list.add("2.0"); // NOI18N
73
return list;
74         }
75         
76     }
77     
78     public static class PluginTypeSupport extends AttributeValueSupport.Default {
79         
80         public PluginTypeSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
81             super(tag, longName, attrName);
82         }
83         
84         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
85             ArrayList list = new ArrayList();
86             list.add("bean"); // NOI18N
87
list.add("applet"); // NOI18N
88
return list;
89         }
90         
91     }
92     
93     public static class VariableScopeSupport extends AttributeValueSupport.Default {
94         
95         public VariableScopeSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
96             super(tag, longName, attrName);
97         }
98         
99         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
100             ArrayList list = new ArrayList();
101             list.add("AT_BEGIN"); // NOI18N
102
list.add("AT_END"); // NOI18N
103
list.add("NESTED"); // NOI18N
104
return list;
105         }
106         
107     }
108     
109     public static class YesNoTrueFalseSupport extends AttributeValueSupport.Default {
110         
111         public YesNoTrueFalseSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
112             super(tag, longName, attrName);
113         }
114         
115         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
116             ArrayList list = new ArrayList();
117             list.add("false"); // NOI18N
118
list.add("no"); // NOI18N
119
list.add("true"); // NOI18N
120
list.add("yes"); // NOI18N
121
return list;
122         }
123         
124     }
125     
126     /**
127      * Provides code completion for a class name context
128      */

129     public static class ClassNameSupport extends AttributeValueSupport.Default {
130         
131         public ClassNameSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
132             super(tag, longName, attrName);
133         }
134         
135         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
136             return new ArrayList();
137         }
138         
139         protected String JavaDoc getFakedClassBody(String JavaDoc prefix){
140             return "class Foo extends " + prefix; //NOI18N
141
}
142         
143         /** Returns the complete result that contains elements from getCompletionItems. */
144         public CompletionQuery.Result getResult(JTextComponent JavaDoc component, int offset,
145                 JspSyntaxSupport sup, SyntaxElement.TagDirective item, String JavaDoc valuePart) {
146             
147             String JavaDoc fakedClassBody = getFakedClassBody(valuePart);
148             int shiftedOffset = fakedClassBody.length();
149             
150             logger.fine("JSP CC: delegating CC query to java file:\n" //NOI18N
151
+ fakedClassBody.substring(0, shiftedOffset)
152                             + "|" + fakedClassBody.substring(shiftedOffset) + "\n"); //NOI18N
153

154             CompletionQueryDelegatedToJava delegate = new CompletionQueryDelegatedToJava(
155                     offset, shiftedOffset, CompletionProvider.COMPLETION_QUERY_TYPE);
156             
157             delegate.create(component.getDocument(), fakedClassBody);
158             List<? extends CompletionItem> items = delegate.getCompletionItems();
159             
160             JspCompletionResult result = new JspCompletionResult(component, null, items, offset, valuePart.length(), -1);
161             //TODO: put the results here
162
return result;
163         }
164         
165         /** Returns generated List of items for completion.
166          * It sets itemLength and itemOffset variables as a side effect
167          */

168         private List completionResults(int offset, JspSyntaxSupport sup, SyntaxElement.TagDirective item, String JavaDoc valuePart) {
169             return null;
170         }
171         
172     }
173     
174     /**
175      * Provides code completion for a comma-separated list of imports context
176      */

177     public static class PackageListSupport extends ClassNameSupport {
178         
179         public PackageListSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
180             super(tag, longName, attrName);
181         }
182         
183         @Override JavaDoc protected String JavaDoc getFakedClassBody(String JavaDoc prefix){
184             int commaPos = prefix.lastIndexOf(",");
185             
186             if (commaPos > -1){
187                 prefix = prefix.substring(commaPos + 1);
188             }
189             
190             return "import " + prefix; //NOI18N
191
}
192     }
193     
194     public static class GetSetPropertyName extends AttributeValueSupport.Default {
195         
196         public GetSetPropertyName(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
197             super(tag, longName, attrName);
198         }
199         
200         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
201             ArrayList list = new ArrayList();
202             PageInfo.BeanData[] beanData = sup.getBeanData();
203             if(beanData != null) {
204                 for (int i = 0; i < beanData.length; i++) {
205                     list.add(beanData[i].getId());
206                 }
207             }
208             return list;
209         }
210         
211     }
212     
213     
214     public static abstract class GetSetPropertyProperty extends AttributeValueSupport.Default {
215         
216         public GetSetPropertyProperty(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
217             super(tag, longName, attrName);
218         }
219         
220         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item, boolean setter) {
221             ArrayList list = new ArrayList();
222             String JavaDoc namePropertyValue = (String JavaDoc)item.getAttributes().get("name"); // NOI18N
223
if (namePropertyValue != null) {
224                 String JavaDoc className = null;
225                 PageInfo.BeanData[] beanData = sup.getBeanData();
226                 for (int i = 0; i < beanData.length; i++) {
227                     if (beanData[i] == null || beanData[i].getId() == null)
228                         continue;
229                     
230                     if (beanData[i].getId().equals(namePropertyValue)) {
231                         className = beanData[i].getClassName();
232                         break;
233                     }
234                 }
235                 
236                 if (className != null) {
237                     try {
238                         FileObject fileObject = NbEditorUtilities.getDataObject( sup.getDocument()).getPrimaryFile();
239                         ClassLoader JavaDoc cld = JspUtils.getModuleClassLoader( sup.getDocument(), fileObject);
240                         Class JavaDoc beanClass = Class.forName(className, false, cld);
241                         Introspector.flushFromCaches(beanClass);
242                         BeanInfo benInfo = Introspector.getBeanInfo(beanClass);
243                         PropertyDescriptor[] properties = benInfo.getPropertyDescriptors();
244                         for (int j = 0; j < properties.length; j++) {
245                             if (setter && (properties[j].getWriteMethod() != null))
246                                 list.add(properties[j].getName());
247                             if (!setter && (properties[j].getReadMethod() != null) && !properties[j].getName().equals("class")) //NOI18N
248
list.add(properties[j].getName());
249                         }
250                     } catch (ClassNotFoundException JavaDoc e) {
251                         //do nothing
252
} catch (IntrospectionException e) {
253                         //do nothing
254
}
255                 }
256             }
257             return list;
258         }
259     }
260     
261     public static class GetPropertyProperty extends GetSetPropertyProperty {
262         
263         public GetPropertyProperty() {
264             super(true, "jsp:getProperty", "property"); // NOI18N
265
}
266         
267         public List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
268             return possibleValues(sup, item, false);
269         }
270         
271     }
272     
273     public static class SetPropertyProperty extends GetSetPropertyProperty {
274         
275         public SetPropertyProperty() {
276             super(true, "jsp:setProperty", "property"); // NOI18N
277
}
278         
279         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
280             List list = possibleValues(sup, item, true);
281             list.add(0, "*"); // NOI18N
282
return list;
283         }
284         
285     }
286     
287     public static class TaglibURI extends AttributeValueSupport.Default {
288         
289         public TaglibURI() {
290             super(false, "taglib", "uri"); // NOI18N
291
}
292         
293         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
294             List list = new ArrayList();
295             Map map = sup.getTagLibraryMappings();
296             if (map != null) {
297                 Iterator iterator = map.keySet().iterator();
298                 while(iterator.hasNext()) {
299                     String JavaDoc s = (String JavaDoc)iterator.next();
300                     list.add(s);
301                 }
302             }
303             // sort alphabetically
304
Collections.sort(list);
305             return list;
306         }
307         
308     }
309     
310     public static class TaglibTagdir extends AttributeValueSupport.Default {
311         
312         public TaglibTagdir() {
313             super(false, "taglib", "tagdir"); // NOI18N
314
}
315         
316         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
317             List l = new ArrayList();
318             FileObject orig = sup.getFileObject();
319             FileObject documentBase = JspUtils.guessWebModuleRoot(sup.getDocument(), orig);
320             if (documentBase != null) {
321                 FileObject webInfTags = JspUtils.findRelativeFileObject(documentBase, "WEB-INF/tags");
322                 if (webInfTags != null) {
323                     // WEB-INF/tags itself
324
if (isValidTagDir(webInfTags)) {
325                         l.add(JspUtils.findRelativeContextPath(documentBase, webInfTags));
326                     }
327                     // subfolders of WEB-INF/tags
328
Enumeration en = webInfTags.getFolders(true);
329                     while (en.hasMoreElements()) {
330                         FileObject subF = (FileObject)en.nextElement();
331                         if (isValidTagDir(subF)) {
332                             l.add(JspUtils.findRelativeContextPath(documentBase, subF));
333                         }
334                     }
335                 }
336             }
337             // sort alphabetically
338
Collections.sort(l);
339             return l;
340         }
341         
342         private boolean isValidTagDir(FileObject subF) {
343             // must contain at least one file
344
return subF.getChildren(false).hasMoreElements();
345         }
346         
347     }
348     
349     
350     /** Support for code completing of package and class. */
351     public static class FilenameSupport extends AttributeValueSupport.Default {
352         static final ImageIcon JavaDoc PACKAGE_ICON =
353                 new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage("org/openide/loaders/defaultFolder.gif")); // NOI18N
354

355         /** Index where to start substitution */
356         private int itemOffset;
357         /** Length of currently substituted text */
358         private int itemLength;
359         
360         public FilenameSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
361             super(tag, longName, attrName);
362         }
363         
364         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
365             return new ArrayList();
366         }
367         
368         /** Returns the complete result that contains elements from getCompletionItems. */
369         public CompletionQuery.Result getResult(JTextComponent JavaDoc component, int offset,
370                 JspSyntaxSupport sup, SyntaxElement.TagDirective item, String JavaDoc valuePart) {
371             List res = completionResults(offset, sup, item, valuePart);
372             return new JspCompletionQuery.JspCompletionResult(component,
373                     completionTitle(), res,
374                     itemOffset, itemLength, -1);
375         }
376         
377         /** Returns generated List of items for completion.
378          * It sets itemLength and itemOffset variables as a side effect
379          */

380         private List completionResults(int offset, JspSyntaxSupport sup, SyntaxElement.TagDirective item, String JavaDoc valuePart) {
381             List res = new ArrayList();
382             String JavaDoc path = ""; // NOI18N
383
String JavaDoc fileNamePart = valuePart;
384             int lastSlash = valuePart.lastIndexOf('/');
385             if (lastSlash == 0) {
386                 path = "/"; // NOI18N
387
fileNamePart = valuePart.substring(1);
388             } else if (lastSlash > 0) { // not a leading slash?
389
path = valuePart.substring(0, lastSlash);
390                 fileNamePart = (lastSlash == valuePart.length())? "": valuePart.substring(lastSlash+1); // NOI18N
391
}
392             
393             try {
394                 FileObject orig = sup.getFileObject();
395                 FileObject documentBase = JspUtils.guessWebModuleRoot(sup.getDocument(), orig);
396                 // need to normalize fileNamePart with respect to orig
397
String JavaDoc ctxPath = JspUtils.resolveRelativeURL("/"+orig.getPath(), path); // NOI18N
398
//is this absolute path?
399
if (path.startsWith("/"))
400                     ctxPath = documentBase.getPath() + path;
401                 else
402                     ctxPath = ctxPath.substring(1);
403                 
404                 
405                 FileSystem fs = orig.getFileSystem();
406                 
407                 FileObject folder = fs.findResource(ctxPath);
408                 if (folder != null) {
409                     res = files(folder, fileNamePart, sup);
410                     if (!folder.equals(documentBase) && !path.startsWith("/") // NOI18N
411
&& (path.length() == 0 || (path.lastIndexOf("../")+3 == path.length()))){ // NOI18N
412
res.add(0, new JspCompletionItem.FileAttributeValue("../", java.awt.Color.BLUE, PACKAGE_ICON)); // NOI18N
413
}
414                 }
415             } catch (FileStateInvalidException ex) {
416                 // unreachable FS - disable completion
417
} catch (IllegalArgumentException JavaDoc ex) {
418                 // resolving failed
419
}
420             itemOffset = offset - valuePart.length() + lastSlash + 1; // works even with -1
421
itemLength = fileNamePart.length();
422             
423             
424             //set substitute offset
425
Iterator i = res.iterator();
426             while(i.hasNext()) {
427                 JspCompletionItem.JspResultItem resultItem = (JspCompletionItem.JspResultItem)i.next();
428                 resultItem.setSubstituteOffset(itemOffset);
429             }
430             
431             return res;
432         }
433         
434         private List files(FileObject folder, String JavaDoc prefix, JspSyntaxSupport sup) {
435             ArrayList res = new ArrayList();
436             TreeMap resFolders = new TreeMap();
437             TreeMap resFiles = new TreeMap();
438             
439             Enumeration files = folder.getChildren(false);
440             while (files.hasMoreElements()) {
441                 FileObject file = (FileObject)files.nextElement();
442                 String JavaDoc fname = file.getNameExt();
443                 if (fname.startsWith(prefix) && !"cvs".equalsIgnoreCase(fname)) {
444                     
445                     if (file.isFolder())
446                         resFolders.put(file.getNameExt(), new JspCompletionItem.FileAttributeValue(file.getNameExt() + "/", java.awt.Color.BLUE, PACKAGE_ICON));
447                     else{
448                         java.awt.Image JavaDoc icon = JspUtils.getIcon(sup.getDocument(), file);
449                         if (icon != null)
450                             resFiles.put(file.getNameExt(), new JspCompletionItem.FileAttributeValue(file.getNameExt(), java.awt.Color.BLACK, new javax.swing.ImageIcon JavaDoc(icon)));
451                         else
452                             resFiles.put(file.getNameExt(), new JspCompletionItem.FileAttributeValue(file.getNameExt(), java.awt.Color.BLACK));
453                     }
454                 }
455             }
456             res.addAll(resFolders.values());
457             res.addAll(resFiles.values());
458             
459             return res;
460         }
461         
462     }
463     
464     public static class TrueFalseSupport extends AttributeValueSupport.Default {
465         
466         public TrueFalseSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
467             super(tag, longName, attrName);
468         }
469         
470         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
471             ArrayList list = new ArrayList();
472             list.add("false"); // NOI18N
473
list.add("true"); // NOI18N
474
return list;
475         }
476         
477     }
478     
479     public static class PageLanguage extends AttributeValueSupport.Default {
480         
481         public PageLanguage() {
482             super(false, "page", "language"); // NOI18N
483
}
484         
485         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
486             ArrayList list = new ArrayList();
487             list.add("java"); // NOI18N
488
return list;
489         }
490         
491     }
492     
493     public static class EncodingSupport extends AttributeValueSupport.Default {
494         
495         public EncodingSupport(boolean tag, String JavaDoc longName, String JavaDoc attrName) {
496             super(tag, longName, attrName);
497         }
498         
499         protected List possibleValues(JspSyntaxSupport sup, SyntaxElement.TagDirective item) {
500             ArrayList list = new ArrayList();
501             Iterator iter = java.nio.charset.Charset.availableCharsets().keySet().iterator();
502             
503             while (iter.hasNext())
504                 list.add(iter.next());
505             
506             return list;
507         }
508         
509     }
510     
511 }
512
Popular Tags